feat: 完善机巢图传、拍摄阶段与任务交互
- 完善双路 RTC 接收、首帧切换及拍摄结束后的本地播放释放 - 对齐无人机状态枚举与正常拍完返航标记,新增到达后本地倒计时 - 补充准备页电量与前往回执,进入任务时移除准备页路由 - 对齐拍摄中和拍摄完成原型布局,移除详情按钮 - 补充隔离测试、接口记录与项目约定 验证:最近 157 项相关模拟器隔离测试通过,Release 无签名编译通过;未调用真实飞控或图传。
This commit is contained in:
@@ -11,11 +11,37 @@ protocol DroneDockLivePlaying: AnyObject {
|
||||
func stop()
|
||||
}
|
||||
|
||||
/// 一个 RTC 引擎接收两个房间,机巢先接入,无人机有帧后覆盖,停流后恢复机巢。
|
||||
/// 真实 SDK 入口始终执行编译期禁飞保护,没有运行时放行参数。
|
||||
@MainActor
|
||||
final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
private var engine: ByteRTCEngine?
|
||||
private var delegate: DroneDockVolcDelegate?
|
||||
private let receiver = DroneDockLiveReceiver(makeEngine: DroneDockVolcEngineAdapter.make)
|
||||
|
||||
/// Release 通过接收器播放;Debug 在创建 SDK 之前拒绝连接。
|
||||
func start(_ configurations: [DroneDockVolcLiveConfiguration], in view: UIView,
|
||||
onEvent: @escaping @MainActor (DroneDockLivePlaybackEvent) -> Void) {
|
||||
guard !DeviceCommandDebugPolicy.blocksLiveRequests else {
|
||||
stop(); onEvent(.failed(DeviceCommandDebugPolicy.blockedMessage)); return
|
||||
}
|
||||
receiver.start(configurations, in: view, onEvent: onEvent)
|
||||
}
|
||||
|
||||
/// 释放本机播放资源,不发送任何设备控制请求。
|
||||
func stop() { receiver.stop() }
|
||||
}
|
||||
|
||||
/// 一个 RTC 引擎接收两个房间,机巢先接入,无人机有帧后覆盖,停流后恢复机巢。
|
||||
@MainActor
|
||||
final class DroneDockLiveReceiver: DroneDockLivePlaying {
|
||||
private var engine: (any DroneDockRTCReceivingEngine)?
|
||||
private let makeEngine: (String, @escaping @Sendable (DroneDockVolcEvent) -> Void) -> (any DroneDockRTCReceivingEngine)?
|
||||
private let firstFrameDelay: @Sendable () async throws -> Void
|
||||
|
||||
/// 生产环境注入带 Debug 保护的适配器;测试只注入内存接收端。
|
||||
init(makeEngine: @escaping (String, @escaping @Sendable (DroneDockVolcEvent) -> Void) -> (any DroneDockRTCReceivingEngine)?,
|
||||
firstFrameDelay: @escaping @Sendable () async throws -> Void = { try await Task.sleep(for: .seconds(20)) }) {
|
||||
self.makeEngine = makeEngine
|
||||
self.firstFrameDelay = firstFrameDelay
|
||||
}
|
||||
private weak var container: UIView?
|
||||
private var sources: [String: DroneDockVolcSource] = [:]
|
||||
private var configurations: [DroneDockVolcLiveConfiguration] = []
|
||||
@@ -29,9 +55,6 @@ final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
/// 加入只收视频的房间;同一来源只更新 Token,避免后台重查导致画面闪断。
|
||||
func start(_ configurations: [DroneDockVolcLiveConfiguration], in view: UIView,
|
||||
onEvent: @escaping @MainActor (DroneDockLivePlaybackEvent) -> Void) {
|
||||
guard !DeviceCommandDebugPolicy.blocksLiveRequests else {
|
||||
stop(); onEvent(.failed(DeviceCommandDebugPolicy.blockedMessage)); return
|
||||
}
|
||||
guard let first = configurations.first else { stop(); return }
|
||||
if container !== view || self.configurations.first?.appID != first.appID { stop() }
|
||||
self.onEvent = onEvent
|
||||
@@ -39,16 +62,12 @@ final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
self.configurations = configurations
|
||||
if engine == nil {
|
||||
let generation = generation
|
||||
let delegate = DroneDockVolcDelegate { [weak self] event in
|
||||
guard let engine = makeEngine(first.appID, { [weak self] event in
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self, self.generation == generation else { return }
|
||||
self.receive(event)
|
||||
}
|
||||
}
|
||||
self.delegate = delegate
|
||||
let config = ByteRTCEngineConfig()
|
||||
config.appID = first.appID
|
||||
guard let engine = ByteRTCEngine.createRTCEngine(config, delegate: delegate) else {
|
||||
}) else {
|
||||
stop(); onEvent(.failed("图传播放器初始化失败")); return
|
||||
}
|
||||
self.engine = engine
|
||||
@@ -74,10 +93,8 @@ final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
generation = UUID()
|
||||
onEvent = nil
|
||||
for role in Array(sources.keys) { removeSource(role) }
|
||||
engine?.delegate = nil
|
||||
if engine != nil { ByteRTCEngine.destroyRTCEngine() }
|
||||
engine?.destroy()
|
||||
engine = nil
|
||||
delegate = nil
|
||||
container = nil
|
||||
configurations = []
|
||||
selection = DroneDockLiveSourceSelection()
|
||||
@@ -105,7 +122,15 @@ final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
scheduleExpiry(source)
|
||||
return
|
||||
}
|
||||
guard let container, let room = engine?.createRTCRoom(configuration.roomID) else {
|
||||
guard let container else { return }
|
||||
let generation = generation
|
||||
let sourceID = UUID()
|
||||
guard let room = engine?.makeRoom(configuration.roomID, onEvent: { [weak self] event in
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self, self.generation == generation, self.sources[role]?.id == sourceID else { return }
|
||||
self.receive(event)
|
||||
}
|
||||
}) else {
|
||||
failedRoles.insert(role)
|
||||
if role == "airport" { allowsDroneConnection = true }
|
||||
return
|
||||
@@ -115,25 +140,9 @@ final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
view.backgroundColor = .black
|
||||
view.isHidden = true
|
||||
container.addSubview(view)
|
||||
let generation = generation
|
||||
let sourceID = UUID()
|
||||
let delegate = DroneDockVolcDelegate { [weak self] event in
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self, self.generation == generation, self.sources[role]?.id == sourceID else { return }
|
||||
self.receive(event)
|
||||
}
|
||||
}
|
||||
let source = DroneDockVolcSource(id: sourceID, configuration: configuration, room: room, view: view, delegate: delegate)
|
||||
let source = DroneDockVolcSource(id: sourceID, configuration: configuration, room: room, view: view)
|
||||
sources[role] = source
|
||||
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)
|
||||
let result = room.join(configuration)
|
||||
guard result == 0 else {
|
||||
removeSource(role)
|
||||
failedRoles.insert(role)
|
||||
@@ -141,10 +150,12 @@ final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
return
|
||||
}
|
||||
// 机巢无首帧时不能无限等待,仍尝试无人机;已连房间继续等待发布事件。
|
||||
let delay = firstFrameDelay
|
||||
source.timeoutTask = Task { @MainActor [weak self] in
|
||||
try? await Task.sleep(for: .seconds(20))
|
||||
do { try await delay() } catch { return }
|
||||
guard !Task.isCancelled, let self, self.generation == generation, self.sources[role]?.id == sourceID else { return }
|
||||
self.sourceFailed(role)
|
||||
// 首帧等待超时只开放另一路;不能销毁仍在等待发布的房间或取消补取参数。
|
||||
self.markReady(false, source: source)
|
||||
}
|
||||
scheduleExpiry(source)
|
||||
}
|
||||
@@ -163,6 +174,11 @@ final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
guard !Task.isCancelled, let self, self.generation == generation, self.sources[role]?.id == sourceID else { return }
|
||||
self.removeSource(role)
|
||||
self.displaySelectedSource()
|
||||
if self.sources.isEmpty {
|
||||
let callback = self.onEvent
|
||||
self.stop()
|
||||
callback?(.expired)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,32 +187,28 @@ final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
guard let source = sources.removeValue(forKey: role) else { return }
|
||||
source.timeoutTask?.cancel()
|
||||
source.expiryTask?.cancel()
|
||||
if let streamID = source.streamID { engine?.setRemoteVideoCanvas(streamID, withCanvas: nil) }
|
||||
source.room.setRTCRoomDelegate(nil)
|
||||
if let streamID = source.streamID { _ = engine?.setVideoView(nil, streamID: streamID) }
|
||||
source.room.destroy()
|
||||
source.view.removeFromSuperview()
|
||||
}
|
||||
|
||||
private func receive(_ event: DroneDockVolcEvent) {
|
||||
switch event {
|
||||
case .published(let id, let roomID, let index, let published):
|
||||
guard index == 0, let source = source(in: roomID) else { return }
|
||||
case .published(let id, let roomID, let published):
|
||||
guard !id.isEmpty, let source = source(in: roomID) else { return }
|
||||
if !published {
|
||||
guard source.streamID == id else { return }
|
||||
engine?.setRemoteVideoCanvas(id, withCanvas: nil)
|
||||
source.room.subscribeStreamVideo(id, subscribe: false)
|
||||
_ = engine?.setVideoView(nil, streamID: id)
|
||||
_ = source.room.subscribeVideo(id, subscribe: false)
|
||||
source.streamID = nil
|
||||
markReady(false, source: source)
|
||||
return
|
||||
}
|
||||
guard source.streamID == nil else { return }
|
||||
source.streamID = id
|
||||
let canvas = ByteRTCVideoCanvas()
|
||||
canvas.view = source.view
|
||||
canvas.renderMode = .fit
|
||||
let render = engine?.setRemoteVideoCanvas(id, withCanvas: canvas)
|
||||
let subscribe = source.room.subscribeStreamVideo(id, subscribe: true)
|
||||
if render != 0 || subscribe != 0 { sourceFailed(source.configuration.role) }
|
||||
let render = engine?.setVideoView(source.view, streamID: id)
|
||||
let subscribe = source.room.subscribeVideo(id, subscribe: true)
|
||||
if render != 0 || subscribe != 0 { sourceFailed(source.configuration.role, message: "图传订阅失败(\(render ?? -1)/\(subscribe)),请重试") }
|
||||
case .frame(let id, let roomID):
|
||||
guard let source = source(in: roomID), source.streamID == id else { return }
|
||||
markReady(true, source: source)
|
||||
@@ -207,9 +219,9 @@ final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
case .stopped, .frozen, .failed: markReady(false, source: source)
|
||||
default: break
|
||||
}
|
||||
case .roomFailed(let roomID):
|
||||
case .roomFailed(let roomID, let code):
|
||||
guard let source = source(in: roomID) else { return }
|
||||
sourceFailed(source.configuration.role)
|
||||
sourceFailed(source.configuration.role, message: "图传入房失败(\(code)),请重试")
|
||||
case .connectionLost:
|
||||
for source in sources.values { selection.setReady(false, role: source.configuration.role) }
|
||||
displaySelectedSource()
|
||||
@@ -237,7 +249,7 @@ final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
}
|
||||
}
|
||||
|
||||
private func sourceFailed(_ role: String) {
|
||||
private func sourceFailed(_ role: String, message: String) {
|
||||
failedRoles.insert(role)
|
||||
selection.setReady(false, role: role)
|
||||
if role == "airport" { allowsDroneConnection = true; connectDroneIfAllowed() }
|
||||
@@ -245,7 +257,7 @@ final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
if selection.selectedRole == nil, Set(configurations.map(\.role)).isSubset(of: failedRoles) {
|
||||
let callback = onEvent
|
||||
stop()
|
||||
callback?(.failed("暂未收到可用的图传画面,请重试"))
|
||||
callback?(.failed(message))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,39 +276,38 @@ final class DroneDockVolcLivePlayer: DroneDockLivePlaying {
|
||||
private final class DroneDockVolcSource {
|
||||
let id: UUID
|
||||
var configuration: DroneDockVolcLiveConfiguration
|
||||
let room: ByteRTCRoom
|
||||
let room: any DroneDockRTCReceivingRoom
|
||||
let view: UIView
|
||||
let delegate: DroneDockVolcDelegate
|
||||
var streamID: String?
|
||||
var timeoutTask: Task<Void, Never>?
|
||||
var expiryTask: Task<Void, Never>?
|
||||
|
||||
init(id: UUID, configuration: DroneDockVolcLiveConfiguration, room: ByteRTCRoom, view: UIView, delegate: DroneDockVolcDelegate) {
|
||||
self.id = id; self.configuration = configuration; self.room = room; self.view = view; self.delegate = delegate
|
||||
init(id: UUID, configuration: DroneDockVolcLiveConfiguration, room: any DroneDockRTCReceivingRoom, view: UIView) {
|
||||
self.id = id; self.configuration = configuration; self.room = room; self.view = view
|
||||
}
|
||||
}
|
||||
|
||||
/// SDK 工作线程只转交值类型事件;事件带房间和流身份以隔离两路回调。
|
||||
private enum DroneDockVolcEvent: Sendable {
|
||||
case published(String, String, Int, Bool)
|
||||
enum DroneDockVolcEvent: Sendable {
|
||||
case published(String, String, Bool)
|
||||
case frame(String, String)
|
||||
case videoState(String, String, ByteRTCRemoteVideoState)
|
||||
case roomFailed(String)
|
||||
case roomFailed(String, Int)
|
||||
case connectionLost
|
||||
case failed(Int)
|
||||
}
|
||||
|
||||
/// SDK 回调桥接,不采集音视频,不处理设备控制消息。
|
||||
private final class DroneDockVolcDelegate: NSObject, ByteRTCEngineDelegate, ByteRTCRoomDelegate {
|
||||
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))
|
||||
emit(.published(streamId, info.roomId, isPublish))
|
||||
}
|
||||
|
||||
func rtcRoom(_ rtcRoom: ByteRTCRoom, onRoomStateChanged roomId: String, withUid uid: String, state: Int, extraInfo: String) {
|
||||
if state != 0 { emit(.roomFailed(roomId)) }
|
||||
if state != 0 { emit(.roomFailed(roomId, state)) }
|
||||
}
|
||||
|
||||
func rtcEngine(_ engine: ByteRTCEngine, onFirstRemoteVideoFrameDecoded streamId: String,
|
||||
|
||||
Reference in New Issue
Block a user