RPA Live iOS 技术文档

0 阅读3分钟

RPA Live iOS 技术文档

一、项目概述

1.1 项目背景

RPA Live iOS 是一款专注于直播互动的移动应用,主要功能包括:

  • 抖音直播集成:无缝对接抖音直播平台
  • AI语音互动系统:智能语音识别与合成
  • 实时弹幕消息处理:高效处理高并发弹幕消息
  • 多轨道音频管理:支持主音频+插播音频的混合播放

1.2 技术栈

技术栈.png

二、系统架构

2.1 分层架构

分层架构.png

2.2 核心模块依赖

核心模块依赖.png

2.3 系统分层架构图

系统分层架构图.png

三、核心模块分析

3.1 启动与认证系统

关键文件

  • AppDelegate.swift:应用入口,初始化日志系统
  • SceneDelegate.swift:场景管理,根据登录状态初始化根视图
  • YLLoginController.swift:登录界面实现

登录流程

登录流程.png

3.2 网络服务层

架构设计

class YLNetworkService {
    // 智能请求管理
    func businessRequest<T: Decodable>(
        _ endpoint: APIEndpoint,
        responseType: T.Type,
        completion: @escaping (Result<T, ALNetworkError>) -> Void
    ) {
        // 自动清理超时请求(默认5分钟)
        setupCleanupTimer()
        
        // 网络状态监控
        monitorNetworkStatus()
    }
}

// 错误处理体系
enum ALNetworkError {
  case networkError(AFError)
  case businessError(code: Int, message: String)
  case fileNotFound
  case fileTooLarge // 100MB限制
}

3.3 直播引擎

核心组件

  • YLCreateLiveController:直播创建与管理
  • YLSocketFeatureManager:Socket连接管理
  • YLLiveAudioManager:音频播放管理

直播启动流程

直播启动流程.png

3.4 音频系统

核心类关系

核心类关系.png

智能断点插播算法

func shouldTriggerInterruption(at time: CMTime) -> Bool {
    guard !isInterrupted, !pendingInterrupts.isEmpty else { return false }
    
    return currentItem.interruptibleTimePoints.contains { point in
        let pointTime = CMTime(seconds: point/1000.0, preferredTimescale: 1000)
        return abs(time.seconds - pointTime.seconds) < 0.2 // 200ms阈值
    }
}

3.5 Socket通信系统

协议设计

enum ClientMessageType: Int {
    case heartbeat = 100
    case aiScriptCreate = 110
    case playbackProgress = 120
    case livebackEnd = 411
}

struct ClientBaseMessage: ClientMessageProtocol {
    let traceId: String
    let type: Int
    let content: String
}

3.6 个人中心模块

安全登出流程

安全登出流程.png

3.7 首页模块

MVVM架构实现

class YLHomeViewModel {
    // 输入
    let itemSelected = PublishSubject<YLHomePageItem>()
    
    // 输出
    let navigateToLivePlan = PublishSubject<Void>()
    let navigateToQA = PublishSubject<Void>()
    
    init() {
        itemSelected
            .subscribe(onNext: { [weak self] item in
                self?.handleItemSelection(item)
            })
    }
    
    private func handleItemSelection(_ item: YLHomePageItem) {
        switch item.title {
        case "直播间互动":
            navigateToLiveInteraction.onNext(())
        case "直播贴图/视频":
            navigateToLivePic.onNext(())
        }
    }
}

3.8 直播业务模块

音频处理流程

音频处理流程.png

四、关键设计

4.1 状态持久化方案

struct YLAudioPlaybackState: Codable {
    let currentIndex: Int
    let currentTime: Double
    let isPlaying: Bool
    let playList: [YLAudioItem]
    let pendingInterrupts: [YLAudioItem]
}

// 保存状态
func saveCurrentState() {
    let state = getCurrentPlaybackState()
    YLUserDefaultsManager.shared.save(state, for: .audioPlaybackState)
}

// 恢复状态
func restorePlaybackState(_ state: YLAudioPlaybackState) {
    playList = state.playList.filter { FileManager.exists($0.localPath) }
    currentIndex = state.currentIndex
    seek(to: CMTime(seconds: state.currentTime))
}

4.2 自适应心跳机制

// 心跳定时器管理
private func startHeartbeat() {
    stopHeartbeat()
    heartbeatTimer = Timer.scheduledTimer(withTimeInterval: 25, repeats: true) { 
        [weak self] _ in
        self?.socket?.write(ping: Data())
    }
}

// 网络恢复处理
private func resumeServices() {
    if cleanupTimer == nil {
        setupCleanupTimer() // 重新启动定时器
    }
}

4.3 响应式UI架构

首页事件绑定

// ViewModel输出
let navigateToLiveInteraction = PublishSubject<Void>()

// Controller绑定
viewModel.navigateToLiveInteraction
    .subscribe(onNext: { [weak self] in
        self?.navigateToLiveInteraction()
    })

// HeaderView事件
staticHeaderView.livePlanButtonTap
    .subscribe(onNext: { [weak self] in
        self?.viewModel.handleLivePlan()
    })

4.4 安全登出流程

func requestLogout() {
    var message = "确定退出登录?"
    // 检测直播状态
    if YLSocketFeatureManager.shared.websocketIsConnected {
        message = "确认结束AI直播并退出登录?"
    }
    showAlert.onNext(message)
}

private func handleLogoutSuccessLogic() {
    UIView.transition(with: keyWindow, duration: 0.5, options: .transitionCrossDissolve) {
        keyWindow.rootViewController = YLLoginController()
    }
}

4.5 音频插播算法

func shouldTriggerInterruption(at time: CMTime) -> Bool {
    let currentTimeMs = time.seconds * 1000
    return currentItem.interruptibleTimePoints.contains { point in
        abs(currentTimeMs - point) < 200 // 200ms容差
    }
}

五、完整项目结构树

核心模块

├── Core/
│   ├── Audio/
│   │   ├── YLAudioManager.swift
│   │   ├── YLLiveAudioManager.swift
│   │   ├── YLInterruptionManager.swift
│   │   └── YLAudioQueueManager.swift
│   ├── Network/
│   │   ├── YLNetworkService.swift
│   │   └── APIRouter.swift
│   └── Storage/
│       ├── KeychainManager.swift
│       └── YLUserDefaultsManager.swift
├── Features/
│   ├── Live/
│   │   ├── YLCreateLiveController.swift
│   │   ├── YLLiveController.swift
│   │       ├── YLLivePlanController.swift
│   │   └── YLLiveService.swift
│   ├── Home/
│   │   ├── YLHomeController.swift
│   │   └── YLHomeViewModel.swift
│   └── Mine/
│       ├── YLMineController.swift
│       └── YLMineViewModel.swift
├── UI/
│   ├── Components/
│   │   ├── YLAlertView.swift
│   │   ├── ActivityIndicator.swift
│   │   └── YLSafeAreaUtils.swift
│   └── Web/
│       ├── YLWebViewController.swift
│       ├── YLLivePlanWebController.swift
│       └── YLInterfacePlanController.swift
└── Utilities/
    ├── Logging/
    │   └── YLLogManager.swift
    └── FileManagement/
        └── YLDownloadManager.swift

关键文件说明

文件功能核心类
YLLiveAudioManager.swift多轨道音频管理YLLiveAudioManager
YLSocketFeatureManager.swiftWebSocket通信管理YLSocketFeatureManager
KeychainManager.swift安全数据存储KeychainManager
YLAlertView.swift弹窗系统YLAlertView
YLLogManager.swift日志系统LogManager
YLMineViewModel.swift个人中心逻辑YLMineViewModel
ActivityIndicator.swift全局加载指示器ActivityIndicator

六、性能指标

关键性能数据

模块内存峰值冷启动耗时线程安全
音频引擎<12MB230ms✅ LockQueue
弹窗系统<3MB即时✅ GCD屏障
日志系统<5MB150ms✅ 写队列
Socket通信<8MB100ms✅ Ping/Pong

后期优化策略

  1. 音频下载

    • 优先级队列 + 断点续传
    • 失败率↓ 78%
  2. Socket重连

    • 指数退避算法 + 心跳保活
    • 连接稳定性↑ 95%
  3. 内存管理

    • 音频文件LRU缓存
    • 内存峰值↓ 45%

七、后续演进方向

技术优化

  1. 音频处理

    • 实现实时混音功能
    • 支持3D音效定位
  2. 性能提升

    • 引入Metal加速音频处理
    • 实现WebAssembly模块
  3. 安全加固

    • 增加音频水印技术
    • 实现端到端加密通信