feat(drone-dock): 接入火山实时图传并保留禁飞保护
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
import UIKit
|
||||
@preconcurrency import VolcEngineRTC
|
||||
|
||||
/// 可替换的图传播放器;测试注入内存替身,不初始化 RTC 或连接设备。
|
||||
@MainActor
|
||||
protocol DroneDockLivePlaying: AnyObject {
|
||||
func start(_ configuration: DroneDockVolcLiveConfiguration, in view: UIView,
|
||||
onEvent: @escaping @MainActor (DroneDockLivePlaybackEvent) -> Void)
|
||||
func stop()
|
||||
}
|
||||
|
||||
/// 火山 RTC 视频接收端:不采集、不发布、不订阅音频,也不发送设备控制消息。
|
||||
@MainActor
|
||||
final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
private var engine: ByteRTCEngine?
|
||||
private var room: ByteRTCRoom?
|
||||
private var delegate: DroneDockVolcDelegate?
|
||||
private weak var renderView: UIView?
|
||||
private var onEvent: (@MainActor (DroneDockLivePlaybackEvent) -> Void)?
|
||||
private var streamID: String?
|
||||
private var generation = UUID()
|
||||
private var timeoutTask: Task<Void, Never>?
|
||||
private var expiryTask: Task<Void, Never>?
|
||||
|
||||
/// 加入凭证指定的房间,等待已发布的主视频流;不调用业务 live 接口。
|
||||
func start(_ configuration: DroneDockVolcLiveConfiguration, in view: UIView,
|
||||
onEvent: @escaping @MainActor (DroneDockLivePlaybackEvent) -> Void) {
|
||||
stop()
|
||||
guard !DeviceCommandDebugPolicy.blocksLiveRequests else {
|
||||
onEvent(.failed(DeviceCommandDebugPolicy.blockedMessage)); return
|
||||
}
|
||||
guard configuration.expiresAt.timeIntervalSinceNow > 5 else { onEvent(.expired); return }
|
||||
self.onEvent = onEvent
|
||||
renderView = view
|
||||
let generation = generation
|
||||
let delegate = DroneDockVolcDelegate { [weak self] event in
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self, self.generation == generation else { return }
|
||||
self.receive(event, roomID: configuration.roomID)
|
||||
}
|
||||
}
|
||||
self.delegate = delegate
|
||||
let config = ByteRTCEngineConfig()
|
||||
config.appID = configuration.appID
|
||||
guard let engine = ByteRTCEngine.createRTCEngine(config, delegate: delegate) else {
|
||||
onEvent(.failed("图传播放器初始化失败")); stop(); return
|
||||
}
|
||||
self.engine = engine
|
||||
guard let room = engine.createRTCRoom(configuration.roomID) else {
|
||||
onEvent(.failed("无法创建图传接收会话")); stop(); return
|
||||
}
|
||||
self.room = room
|
||||
room.setRTCRoomDelegate(delegate)
|
||||
let roomConfig = ByteRTCRoomConfig()
|
||||
roomConfig.isPublishAudio = false
|
||||
roomConfig.isPublishVideo = false
|
||||
roomConfig.isAutoSubscribeAudio = false
|
||||
roomConfig.isAutoSubscribeVideo = false
|
||||
let user = ByteRTCUserInfo()
|
||||
user.userId = configuration.userID
|
||||
let result = room.joinRoom(configuration.token, userInfo: user, userVisibility: false, roomConfig: roomConfig)
|
||||
guard result == 0 else {
|
||||
onEvent(.failed("图传进房失败(\(result))")); stop(); return
|
||||
}
|
||||
timeoutTask = Task { @MainActor [weak self] in
|
||||
try? await Task.sleep(for: .seconds(20))
|
||||
guard !Task.isCancelled, let self, self.generation == generation else { return }
|
||||
self.onEvent?(.failed("暂未收到图传画面,请重试"))
|
||||
}
|
||||
expiryTask = Task { @MainActor [weak self] in
|
||||
try? await Task.sleep(for: .seconds(max(1, configuration.expiresAt.timeIntervalSinceNow - 5)))
|
||||
guard !Task.isCancelled, let self, self.generation == generation else { return }
|
||||
self.onEvent?(.expired)
|
||||
}
|
||||
}
|
||||
|
||||
/// 销毁房间会退出订阅;只结束本机 RTC 连接,不调用设备停止推流接口。
|
||||
func stop() {
|
||||
generation = UUID()
|
||||
timeoutTask?.cancel(); timeoutTask = nil
|
||||
expiryTask?.cancel(); expiryTask = nil
|
||||
onEvent = nil
|
||||
if let streamID { engine?.setRemoteVideoCanvas(streamID, withCanvas: nil) }
|
||||
room?.setRTCRoomDelegate(nil)
|
||||
room?.destroy()
|
||||
room = nil
|
||||
engine?.delegate = nil
|
||||
if engine != nil { ByteRTCEngine.destroyRTCEngine() }
|
||||
engine = nil
|
||||
delegate = nil
|
||||
streamID = nil
|
||||
renderView = nil
|
||||
}
|
||||
|
||||
private func receive(_ event: DroneDockVolcEvent, roomID: String) {
|
||||
switch event {
|
||||
case .published(let id, let sourceRoom, let index, let published):
|
||||
guard sourceRoom == roomID, index == 0 else { return }
|
||||
if !published {
|
||||
if streamID == id { onEvent?(.failed("图传已停止,请重新连接")) }
|
||||
return
|
||||
}
|
||||
guard streamID == nil, let renderView else { return }
|
||||
streamID = id
|
||||
let canvas = ByteRTCVideoCanvas()
|
||||
canvas.view = renderView
|
||||
canvas.renderMode = .fit
|
||||
let renderResult = engine?.setRemoteVideoCanvas(id, withCanvas: canvas)
|
||||
let subscribeResult = room?.subscribeStreamVideo(id, subscribe: true)
|
||||
if renderResult != 0 || subscribeResult != 0 { onEvent?(.failed("图传订阅失败,请重试")) }
|
||||
case .frame(let id):
|
||||
guard streamID == id else { return }
|
||||
timeoutTask?.cancel(); timeoutTask = nil
|
||||
onEvent?(.firstFrame)
|
||||
case .failed(let code): onEvent?(.failed("图传连接失败(\(code)),请重试"))
|
||||
case .expired: onEvent?(.expired)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 将 SDK 工作线程的回调转换为可发送的值,不跨线程传递 UIKit 或 SDK 对象。
|
||||
private enum DroneDockVolcEvent: Sendable {
|
||||
case published(String, String, Int, Bool)
|
||||
case frame(String)
|
||||
case failed(Int)
|
||||
case expired
|
||||
}
|
||||
|
||||
/// SDK 回调桥接;所有 UI 与引擎操作由上层切回主 Actor 后执行。
|
||||
private final class DroneDockVolcDelegate: NSObject, ByteRTCEngineDelegate, ByteRTCRoomDelegate {
|
||||
private let emit: @Sendable (DroneDockVolcEvent) -> Void
|
||||
init(emit: @escaping @Sendable (DroneDockVolcEvent) -> Void) { self.emit = emit }
|
||||
|
||||
func rtcRoom(_ rtcRoom: ByteRTCRoom, onUserPublishStreamVideo streamId: String,
|
||||
info: ByteRTCStreamInfo, isPublish: Bool) {
|
||||
emit(.published(streamId, info.roomId, info.streamIndex, isPublish))
|
||||
}
|
||||
|
||||
func rtcRoom(_ rtcRoom: ByteRTCRoom, onRoomStateChanged roomId: String, withUid uid: String,
|
||||
state: Int, extraInfo: String) {
|
||||
if state != 0 { emit(.failed(state)) }
|
||||
}
|
||||
|
||||
func rtcEngine(_ engine: ByteRTCEngine, onFirstRemoteVideoFrameRendered streamId: String,
|
||||
info: ByteRTCStreamInfo, withFrameInfo frameInfo: ByteRTCVideoFrameInfo) {
|
||||
emit(.frame(streamId))
|
||||
}
|
||||
|
||||
func rtcEngine(_ engine: ByteRTCEngine, onError errorCode: ByteRTCErrorCode) {
|
||||
emit(.failed(errorCode.rawValue))
|
||||
}
|
||||
|
||||
func rtcEngine(_ engine: ByteRTCEngine, onConnectionStateChanged state: ByteRTCConnectionState) {
|
||||
if state == .reconnecting || state == .failed { emit(.failed(state.rawValue)) }
|
||||
}
|
||||
|
||||
func onTokenWillExpire(_ rtcRoom: ByteRTCRoom) { emit(.expired) }
|
||||
func onSubscribePrivilegeTokenWillExpire(_ rtcRoom: ByteRTCRoom) { emit(.expired) }
|
||||
}
|
||||
Reference in New Issue
Block a user