文章

MiguDelay 架构 01:应用启动与关闭

MiguDelay(DDLive)项目架构系列文档:应用启动与关闭。内容基于 2026-08-04 对指定重构分支的源码扫描结果整理。

MiguDelay 架构 01:应用启动与关闭

系列导航系列总览 · 上一篇:MiguDelay 架构 00:项目架构总览 · 下一篇:MiguDelay 架构 02:PlaybackRuntime 组合根

扫描基线

  • 项目:MiguDelay(DDLive)
  • 分支:refactor/video-frame-provider
  • HEAD:d28ec061406e4e3df93e3d1a92478d71a9ad4908
  • 工作树:clean
  • 扫描日期:2026-08-04
  • 说明:内容以当前 HEAD 源码为准;仓库旧架构文档只作为检索线索。

1. 职责边界

src/code/App.cpp 是进程入口,负责基础设施启动、Runtime 创建、QML 暴露和最终关闭。它不决定任务 phase,也不直接管理媒体资源。

2. 核心对象

对象 创建者 关键调用 作用
QGuiApplication main() exec() GUI 进程与事件分发
PlaybackRuntime main() initialize()shutdown() 播放对象图组合根
TaskHandle 单例 startRun()stopRun() legacy 任务工作线程
ThreadPool 单例 startPool(15)stopPool() 通用后台线程池
MessageHandle 栈对象 dispatchMessage() topic 到具体协议 Handler
MqttClient 单例 initClient()uninitClient() MQTT 连接、消费和发布
QQmlApplicationEngine 栈对象 load() QML 引擎

3. 启动调用链

1
2
3
4
5
6
7
8
9
10
11
12
13
14
main
  -> QGuiApplication
  -> Breakpad and logging
  -> QLockFile single-instance gate
  -> translation loading
  -> PlaybackRuntime constructor
  -> TaskHandle.startRun
  -> ThreadPool.startPool(15)
  -> PlaybackRuntime.initialize
  -> MessageHandle(ApplicationIngress)
  -> MqttClient.initClient
  -> register QML types and context properties
  -> engine.load(qrc:/qml/App.qml)
  -> app.exec
sequenceDiagram
    participant Main as App.cpp
    participant TH as TaskHandle
    participant TP as ThreadPool
    participant RT as PlaybackRuntime
    participant MQ as MqttClient
    participant QML as QQmlEngine

    Main->>TH: startRun
    Main->>TP: startPool 15
    Main->>RT: initialize
    alt runtime initialized
        Main->>MQ: initClient with ApplicationIngress
        Main->>QML: expose context properties
        Main->>QML: load App.qml
    else runtime failed
        Main->>RT: shutdown
        Main->>TH: stopRun
        Main->>TP: stopPool
    end

4. QML 暴露对象

App.cpp 暴露 AppConfigTimeTickerDiskInfoAppInfoAuxFolderListplaybackInputFocusWatcher。其中 playback 指向 PlaybackUiContext,是新架构给 QML 的稳定聚合入口。

5. 关闭调用链

1
2
3
4
5
6
7
8
app.exec returns
  -> launch 10 second hard-exit watchdog
  -> MqttClient.uninitClient
  -> PlaybackRuntime.shutdown
  -> TaskHandle.stopRun
  -> ThreadPool.stopPool
  -> logger shutdown
  -> process exit
flowchart TD
    E["Qt event loop exits"] --> MQ["Stop MQTT ingress and threads"]
    MQ --> RT["PlaybackRuntime shutdown"]
    RT --> TH["Stop TaskHandle worker"]
    TH --> TP["Stop ThreadPool"]
    TP --> LOG["Flush and shutdown logs"]
    LOG --> EXIT["Process exit"]

MQTT 先停,防止协议线程在 Runtime 销毁后继续投递。10 秒 watchdog 是第三方 I/O 永久阻塞时的最后保险,不能替代正常 drain。

6. 源码证据

  • src/code/App.cpp
  • src/qml/App.qml
  • src/CMakeLists.txt

系列导航系列总览 · 上一篇:MiguDelay 架构 00:项目架构总览 · 下一篇:MiguDelay 架构 02:PlaybackRuntime 组合根

本文由作者按照 CC BY 4.0 进行授权