feat(drone-dock): 接入火山实时图传并保留禁飞保护
This commit is contained in:
@@ -312,7 +312,7 @@ final class DroneDockHomeViewController: BaseViewController {
|
||||
planeStatusService: DroneDockPlaneStatusAPI(client: NetworkServices.shared.apiClient),
|
||||
taskCreationService: DroneDockTaskCreationAPI(client: NetworkServices.shared.apiClient),
|
||||
shootService: DroneDockShootAPI(client: NetworkServices.shared.apiClient),
|
||||
allowsTaskSubmission: !DeviceCommandDebugPolicy.blocksLiveRequests,
|
||||
allowsTaskSubmission: !DeviceCommandDebugPolicy.blocksFlightRequests,
|
||||
currentContext: { Self.currentContext() }
|
||||
))
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
private let deliveryControllerFactory: (DroneDockHomeOrder, DroneDockHomeContext, Int?) -> UIViewController
|
||||
private let downloadSourceDescription: String?
|
||||
private let downloadIsSimulated: Bool
|
||||
private let taskControllerFactory: (Int, DroneDockHomeOrder, DroneDockHomeContext) -> UIViewController
|
||||
private var taskButton: UIBarButtonItem?
|
||||
|
||||
/// 元数据与媒体分区独立,任务没有素材时也保留其状态。
|
||||
private enum Section: Hashable { case order, filters, summary, task(Int), media(Int), message }
|
||||
@@ -43,12 +45,16 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
mediaPreviewControllerFactory: ((URL, Bool, String) -> UIViewController)? = nil,
|
||||
downloadViewModel: DroneDockMaterialDownloadViewModel? = nil,
|
||||
downloadSourceDescription: String? = nil,
|
||||
downloadIsSimulated: Bool = false) {
|
||||
downloadIsSimulated: Bool = false,
|
||||
taskControllerFactory: ((Int, DroneDockHomeOrder, DroneDockHomeContext) -> UIViewController)? = nil) {
|
||||
self.viewModel = viewModel
|
||||
self.downloadViewModel = downloadViewModel ?? DroneDockMaterialDownloadViewModel(
|
||||
isContextValid: { [weak viewModel] in viewModel?.acceptsDownloadContext() == true })
|
||||
self.downloadSourceDescription = downloadSourceDescription
|
||||
self.downloadIsSimulated = downloadIsSimulated
|
||||
self.taskControllerFactory = taskControllerFactory ?? { id, order, context in
|
||||
DroneDockShootingTaskEntry.makeController(taskID: id, order: order, context: context)
|
||||
}
|
||||
self.thumbnailProvider = thumbnailProvider
|
||||
self.mediaPreviewControllerFactory = mediaPreviewControllerFactory
|
||||
self.deliveryControllerFactory = deliveryControllerFactory ?? DroneDockDeliveryEntry.makeController
|
||||
@@ -67,7 +73,11 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
let refresh = UIBarButtonItem(image: UIImage(systemName: "arrow.clockwise"), style: .plain, target: self, action: #selector(refreshTapped))
|
||||
refresh.accessibilityLabel = "刷新素材状态"
|
||||
refresh.accessibilityIdentifier = "drone.materials.remote.refresh"
|
||||
navigationItem.rightBarButtonItem = refresh
|
||||
let tasks = UIBarButtonItem(title: "查看任务", style: .plain, target: self, action: #selector(viewTasksTapped))
|
||||
tasks.accessibilityIdentifier = "drone.materials.remote.viewTasks"
|
||||
tasks.isEnabled = false
|
||||
taskButton = tasks
|
||||
navigationItem.rightBarButtonItems = [refresh, tasks]
|
||||
}
|
||||
|
||||
override func setupUI() {
|
||||
@@ -257,6 +267,7 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
|
||||
private func applySnapshot(animated: Bool) {
|
||||
guard dataSource != nil else { return }
|
||||
taskButton?.isEnabled = operation == nil && !selectionLocked && viewModel.canViewRecordedTasks
|
||||
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
||||
if viewModel.detail != nil {
|
||||
snapshot.appendSections([.order, .filters])
|
||||
@@ -364,6 +375,7 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
|
||||
private func updateDownloadFooter() {
|
||||
navigationItem.rightBarButtonItem?.isEnabled = !selectionLocked
|
||||
taskButton?.isEnabled = operation == nil && !selectionLocked && viewModel.canViewRecordedTasks
|
||||
let model = downloadViewModel
|
||||
let identifier = "drone.materials.remote.download"
|
||||
switch model.phase {
|
||||
@@ -389,6 +401,31 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
}
|
||||
}
|
||||
|
||||
/// 从张工已返回的任务中选择并进入状态页,不创建新任务或调用召唤接口。
|
||||
@objc private func viewTasksTapped() {
|
||||
guard operation == nil, !selectionLocked, viewModel.canViewRecordedTasks,
|
||||
viewModel.acceptsDownloadContext(), presentedViewController == nil else { return }
|
||||
if let task = viewModel.tasks.first, viewModel.tasks.count == 1 {
|
||||
openRecordedTask(id: task.id)
|
||||
return
|
||||
}
|
||||
let sheet = UIAlertController(title: "选择拍摄任务", message: nil, preferredStyle: .actionSheet)
|
||||
for task in viewModel.tasks {
|
||||
let spot = task.spotName.isEmpty ? "拍摄任务" : task.spotName
|
||||
let sequence = task.sequence.map { "第\($0)次拍摄" } ?? "拍摄记录"
|
||||
let title = "\(spot) · \(sequence)" + (task.createdAt.map { " · \($0)" } ?? "")
|
||||
sheet.addAction(UIAlertAction(title: title, style: .default) { [weak self] _ in self?.openRecordedTask(id: task.id) })
|
||||
}
|
||||
sheet.addAction(UIAlertAction(title: "取消", style: .cancel))
|
||||
present(sheet, animated: true)
|
||||
}
|
||||
|
||||
private func openRecordedTask(id: Int) {
|
||||
guard operation == nil, !selectionLocked, viewModel.taskForViewing(id: id) != nil,
|
||||
navigationController?.topViewController === self else { return }
|
||||
navigationController?.pushViewController(taskControllerFactory(id, viewModel.order, viewModel.context), animated: true)
|
||||
}
|
||||
|
||||
private func previewFile(taskID: Int, fileID: Int) {
|
||||
guard operation == nil, let file = task(taskID)?.materials.first(where: { $0.id == fileID }), let url = file.previewURL else { return }
|
||||
preview(url: url, isVideo: file.fileType == 2, title: file.fileName ?? "原素材")
|
||||
|
||||
@@ -3,13 +3,19 @@ import UIKit
|
||||
/// 组装真实拍摄任务页;构造时不发请求,页面出现后才读取同一任务。
|
||||
@MainActor
|
||||
enum DroneDockShootingTaskEntry {
|
||||
/// 使用当前共享网络服务读取已创建、已召唤任务,不创建或控制无人机。
|
||||
/// 使用已有任务读取状态,Release 可手动图传;不注入任务创建或飞控服务。
|
||||
static func makeController(taskID: Int, order: DroneDockHomeOrder,
|
||||
context: DroneDockHomeContext) -> UIViewController {
|
||||
DroneDockShootingTaskViewController(viewModel: DroneDockShootingTaskViewModel(
|
||||
let client = NetworkServices.shared.apiClient
|
||||
let live = DroneDockLiveViewModel(
|
||||
context: context, taskID: taskID,
|
||||
service: DeviceCommandDebugPolicy.blocksLiveRequests ? nil : DroneDockLiveAPI(client: client),
|
||||
allowsDevicePlayback: !DeviceCommandDebugPolicy.blocksLiveRequests,
|
||||
currentContext: { DroneDockHomeViewController.currentContext() })
|
||||
return DroneDockShootingTaskViewController(viewModel: DroneDockShootingTaskViewModel(
|
||||
context: context, taskID: taskID, order: order,
|
||||
service: DroneDockShootingAPI(client: NetworkServices.shared.apiClient),
|
||||
currentContext: { DroneDockHomeViewController.currentContext() }
|
||||
))
|
||||
), liveViewModel: live)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import SnapKit
|
||||
import UIKit
|
||||
|
||||
/// 拍摄任务页:真实入口只读,本地模拟入口可验证图传、重召唤、开拍与安全指令合同。
|
||||
/// 拍摄任务页:正式入口读取状态并手动开启图传,飞控仅在隔离模拟入口注入。
|
||||
@MainActor
|
||||
final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
/// 入口传入的固定任务身份及只读状态,供导航协调和页面测试检查。
|
||||
@@ -173,6 +173,13 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
statusView.onDetails = { [weak self] in self?.showStatusDetails() }
|
||||
safetyView.onCommand = { [weak self] command in self?.safetyCommandTriggered(command) }
|
||||
feedPlaceholder.onRequest = { [weak self] in self?.refreshLiveIfVisible() }
|
||||
feedPlaceholder.onPlaybackEvent = { [weak self] event, sessionID in
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self, self.isPageVisible, self.isApplicationActive else { return }
|
||||
self.liveViewModel.receivePlaybackEvent(event, sessionID: sessionID)
|
||||
self.applySnapshot()
|
||||
}
|
||||
}
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive),
|
||||
name: UIApplication.willResignActiveNotification, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive),
|
||||
@@ -185,7 +192,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
isPageVisible = true
|
||||
isApplicationActive = UIApplication.shared.applicationState == .active
|
||||
refreshTaskIfVisible()
|
||||
refreshLiveIfVisible()
|
||||
refreshLiveIfVisible(automatically: true)
|
||||
}
|
||||
|
||||
/// 离页立即取消未完成的读取,使迟到响应失效。
|
||||
@@ -352,8 +359,9 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
}
|
||||
}
|
||||
|
||||
/// 图传只读取一次,不轮询;正式入口没有图传服务,不能向设备发出开流请求。
|
||||
private func refreshLiveIfVisible() {
|
||||
/// 图传请求不轮询;正式入口仅响应用户点击,本地样片允许随页面加载。
|
||||
private func refreshLiveIfVisible(automatically: Bool = false) {
|
||||
guard !automatically || liveViewModel.loadsAutomatically else { return }
|
||||
guard isPageVisible, isApplicationActive, liveTask == nil, liveViewModel.canLoad else { return }
|
||||
liveGeneration &+= 1
|
||||
let generation = liveGeneration
|
||||
@@ -446,7 +454,7 @@ final class DroneDockShootingTaskViewController: BaseViewController {
|
||||
guard !isApplicationActive else { return }
|
||||
isApplicationActive = true
|
||||
refreshTaskIfVisible()
|
||||
refreshLiveIfVisible()
|
||||
refreshLiveIfVisible(automatically: true)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,13 @@ import AVFoundation
|
||||
import SnapKit
|
||||
import UIKit
|
||||
|
||||
/// 固定图传区域:展示接口状态或明确标记的本地样片,不自行发请求或读取远程视频。
|
||||
/// 固定图传区域:渲染已验证的 RTC 会话或明确标记的本地样片。
|
||||
@MainActor
|
||||
final class DroneDockTaskLiveView: UIView {
|
||||
/// 用户主动获取或重试图传信息,具体请求由控制器协调。
|
||||
var onRequest: (() -> Void)?
|
||||
/// 播放事件附带会话 ID,控制器据此丢弃离页或重连后的旧回调。
|
||||
var onPlaybackEvent: ((DroneDockLivePlaybackEvent, UUID) -> Void)?
|
||||
|
||||
private let playerLayer = AVPlayerLayer()
|
||||
private let titleLabel = DroneDockStyle.label(size: 15, weight: .medium, color: .white)
|
||||
@@ -19,14 +21,28 @@ final class DroneDockTaskLiveView: UIView {
|
||||
private var requestedURL: URL?
|
||||
private var activeURL: URL?
|
||||
private var isPlaybackActive = false
|
||||
private let rtcView = UIView()
|
||||
private let rtcPlayer: any DroneDockLivePlaying
|
||||
private var requestedRTC: DroneDockVolcLiveConfiguration?
|
||||
private var requestedSessionID: UUID?
|
||||
private var activeSessionID: UUID?
|
||||
|
||||
/// 保留原任务页图传区域的访问标识,外部负责 16:9 约束。
|
||||
override init(frame: CGRect) {
|
||||
rtcPlayer = DroneDockVolcLivePlayer()
|
||||
super.init(frame: frame)
|
||||
setupUI()
|
||||
setupConstraints()
|
||||
}
|
||||
|
||||
/// 测试注入无网络播放器,覆盖首帧、失败和会话释放。
|
||||
init(rtcPlayer: any DroneDockLivePlaying) {
|
||||
self.rtcPlayer = rtcPlayer
|
||||
super.init(frame: .zero)
|
||||
setupUI()
|
||||
setupConstraints()
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
||||
|
||||
@@ -39,6 +55,12 @@ final class DroneDockTaskLiveView: UIView {
|
||||
CATransaction.commit()
|
||||
}
|
||||
|
||||
/// 移出窗口也释放播放资源,覆盖交互返回和容器移除。
|
||||
override func didMoveToWindow() {
|
||||
super.didMoveToWindow()
|
||||
if window == nil { setPlaybackActive(false) }
|
||||
}
|
||||
|
||||
/// 在明确时机应用模型;远程 URL 即使错误传入也不会创建播放器。
|
||||
func apply(_ model: DroneDockLiveViewModel) {
|
||||
titleLabel.text = model.title
|
||||
@@ -47,12 +69,15 @@ final class DroneDockTaskLiveView: UIView {
|
||||
requestButton.isHidden = model.actionTitle == nil
|
||||
requestButton.isEnabled = model.canLoad
|
||||
requestedURL = model.playbackURL.flatMap { $0.isFileURL ? $0 : nil }
|
||||
requestedRTC = model.rtcConfiguration
|
||||
requestedSessionID = model.playbackSessionID
|
||||
let hasLocalPreview = requestedURL != nil
|
||||
sourceLabel.text = hasLocalPreview ? "本地模拟画面 · 未连接无人机" : nil
|
||||
sourceLabel.isHidden = !hasLocalPreview
|
||||
titleLabel.isHidden = hasLocalPreview
|
||||
messageLabel.isHidden = hasLocalPreview
|
||||
accessibilityLabel = hasLocalPreview ? sourceLabel.text : "\(model.title),\(model.message)"
|
||||
let isPlaying = model.state == .playing
|
||||
sourceLabel.text = hasLocalPreview ? "本地模拟画面 · 未连接无人机" : (isPlaying ? model.message + " · 实时图传" : nil)
|
||||
sourceLabel.isHidden = !hasLocalPreview && !isPlaying
|
||||
titleLabel.isHidden = hasLocalPreview || isPlaying
|
||||
messageLabel.isHidden = hasLocalPreview || isPlaying
|
||||
accessibilityLabel = hasLocalPreview || isPlaying ? sourceLabel.text : "\(model.title),\(model.message)"
|
||||
updatePlayback()
|
||||
}
|
||||
|
||||
@@ -67,6 +92,8 @@ final class DroneDockTaskLiveView: UIView {
|
||||
clipsToBounds = true
|
||||
accessibilityIdentifier = "drone.task.remote.feedPlaceholder"
|
||||
layer.addSublayer(playerLayer)
|
||||
rtcView.backgroundColor = .clear
|
||||
addSubview(rtcView)
|
||||
playerLayer.videoGravity = .resizeAspect
|
||||
titleLabel.textAlignment = .center
|
||||
messageLabel.textAlignment = .center
|
||||
@@ -96,6 +123,7 @@ final class DroneDockTaskLiveView: UIView {
|
||||
}
|
||||
|
||||
private func setupConstraints() {
|
||||
rtcView.snp.makeConstraints { $0.edges.equalToSuperview() }
|
||||
labels.snp.makeConstraints { make in
|
||||
make.centerY.equalToSuperview().offset(-15)
|
||||
make.leading.trailing.equalToSuperview().inset(20)
|
||||
@@ -114,7 +142,24 @@ final class DroneDockTaskLiveView: UIView {
|
||||
}
|
||||
|
||||
private func updatePlayback() {
|
||||
guard isPlaybackActive, let requestedURL, requestedURL.isFileURL else {
|
||||
guard isPlaybackActive else {
|
||||
releasePlayer()
|
||||
releaseRTC()
|
||||
return
|
||||
}
|
||||
if let config = requestedRTC, let sessionID = requestedSessionID {
|
||||
releasePlayer()
|
||||
guard activeSessionID != sessionID else { return }
|
||||
releaseRTC()
|
||||
activeSessionID = sessionID
|
||||
rtcPlayer.start(config, in: rtcView) { [weak self] event in
|
||||
guard let self, self.activeSessionID == sessionID else { return }
|
||||
self.onPlaybackEvent?(event, sessionID)
|
||||
}
|
||||
return
|
||||
}
|
||||
releaseRTC()
|
||||
guard let requestedURL, requestedURL.isFileURL else {
|
||||
releasePlayer()
|
||||
return
|
||||
}
|
||||
@@ -129,6 +174,12 @@ final class DroneDockTaskLiveView: UIView {
|
||||
queue.play()
|
||||
}
|
||||
|
||||
private func releaseRTC() {
|
||||
guard activeSessionID != nil else { return }
|
||||
activeSessionID = nil
|
||||
rtcPlayer.stop()
|
||||
}
|
||||
|
||||
private func releasePlayer() {
|
||||
player?.pause()
|
||||
looper?.disableLooping()
|
||||
|
||||
@@ -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