import UIKit @preconcurrency import VolcEngineRTC /// 接收端最小能力,隔离测试无需初始化 SDK 或使用真实凭证。 @MainActor protocol DroneDockRTCReceivingEngine: AnyObject { /// 创建一个独立接收房间并转交事件。 func makeRoom(_ id: String, onEvent: @escaping @Sendable (DroneDockVolcEvent) -> Void) -> (any DroneDockRTCReceivingRoom)? /// 按 SDK 提供的流 ID 绑定或解绑画布。 func setVideoView(_ view: UIView?, streamID: String) -> Int /// 销毁引擎及本地接收资源。 func destroy() } /// 单路只收视频的房间;不提供发布、采集或设备控制能力。 @MainActor protocol DroneDockRTCReceivingRoom: AnyObject { /// 使用与当前用户匹配的凭证入房。 func join(_ configuration: DroneDockVolcLiveConfiguration) -> Int /// 同用户凭证续期。 func updateToken(_ token: String) -> Int /// 订阅或取消订阅远端视频。 func subscribeVideo(_ streamID: String, subscribe: Bool) -> Int /// 退出并销毁接收房间。 func destroy() } /// 火山 SDK 适配器;真实创建点再次拦截 Debug,测试不能通过注入绕开保护。 @MainActor final class DroneDockVolcEngineAdapter: DroneDockRTCReceivingEngine { private let engine: ByteRTCEngine private let delegate: DroneDockVolcDelegate private init(engine: ByteRTCEngine, delegate: DroneDockVolcDelegate) { self.engine = engine self.delegate = delegate } /// 仅在构建策略允许真实图传时构造 SDK。 static func make(appID: String, onEvent: @escaping @Sendable (DroneDockVolcEvent) -> Void) -> (any DroneDockRTCReceivingEngine)? { guard !DeviceCommandDebugPolicy.blocksLiveRequests else { return nil } let delegate = DroneDockVolcDelegate(emit: onEvent) let config = ByteRTCEngineConfig() config.appID = appID guard let engine = ByteRTCEngine.createRTCEngine(config, delegate: delegate) else { return nil } return DroneDockVolcEngineAdapter(engine: engine, delegate: delegate) } /// 每个房间独立保留回调桥接,避免弱引用导致事件丢失。 func makeRoom(_ id: String, onEvent: @escaping @Sendable (DroneDockVolcEvent) -> Void) -> (any DroneDockRTCReceivingRoom)? { guard let room = engine.createRTCRoom(id) else { return nil } return DroneDockVolcRoomAdapter(room: room, delegate: DroneDockVolcDelegate(emit: onEvent)) } /// 画布只依赖发布回调的 streamID,不假定远端流索引为零。 func setVideoView(_ view: UIView?, streamID: String) -> Int { let canvas = ByteRTCVideoCanvas() canvas.view = view canvas.renderMode = .fit return Int(engine.setRemoteVideoCanvas(streamID, withCanvas: canvas)) } /// 房间由接收器先释放,再销毁共享引擎。 func destroy() { engine.delegate = nil ByteRTCEngine.destroyRTCEngine() } } /// 将只收视频的房间配置封装在 SDK 边界,所有回调由接收器切回主线程。 @MainActor private final class DroneDockVolcRoomAdapter: DroneDockRTCReceivingRoom { private let room: ByteRTCRoom private let delegate: DroneDockVolcDelegate init(room: ByteRTCRoom, delegate: DroneDockVolcDelegate) { self.room = room self.delegate = delegate room.setRTCRoomDelegate(delegate) } func join(_ configuration: DroneDockVolcLiveConfiguration) -> Int { let config = ByteRTCRoomConfig() config.isPublishAudio = false config.isPublishVideo = false config.isAutoSubscribeAudio = false config.isAutoSubscribeVideo = false let user = ByteRTCUserInfo() user.userId = configuration.userID return Int(room.joinRoom(configuration.token, userInfo: user, userVisibility: false, roomConfig: config)) } func updateToken(_ token: String) -> Int { Int(room.updateToken(token)) } func subscribeVideo(_ streamID: String, subscribe: Bool) -> Int { Int(room.subscribeStreamVideo(streamID, subscribe: subscribe)) } func destroy() { room.setRTCRoomDelegate(nil) room.destroy() } }