playercontroller.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. #include "playercontroller.h"
  2. #include "common.h"
  3. #include <QApplication>
  4. #include <QElapsedTimer>
  5. #include <QStatusBar>
  6. #include <cassert>
  7. #include <memory>
  8. #include "ffmpeg_init.h"
  9. #include "start_play_thread.h"
  10. #include "video_state.h"
  11. #include "audio_decode_thread.h"
  12. #include "audio_effect_helper.h"
  13. #include "audio_play_thread.h"
  14. #include "play_control_window.h"
  15. #include "read_thread.h"
  16. #include "start_play_thread.h"
  17. #include "stopplay_waiting_thread.h"
  18. #include "subtitle_decode_thread.h"
  19. #include "video_decode_thread.h"
  20. #include "video_play_thread.h"
  21. #include "video_state.h"
  22. Q_LOGGING_CATEGORY(playerControllerLog, "player.controller")
  23. PlayerController::PlayerController(QWidget* parent)
  24. : QObject(parent)
  25. {
  26. ffmpeg_init();
  27. m_state = PlayerState::Idle;
  28. }
  29. PlayerController::~PlayerController()
  30. {
  31. if (m_initThread.joinable()) {
  32. m_initThread.join();
  33. }
  34. stopPlay();
  35. }
  36. void PlayerController::startToPlay(const QString& file)
  37. {
  38. std::lock_guard<std::mutex> lock(m_stopMutex);
  39. qCDebug(playerControllerLog) << "[PlayerController] m_state" << (int) m_state.load();
  40. // 自愈:如果状态为Playing但所有线程都已退出,强制Idle
  41. if (m_state == PlayerState::Playing && areAllThreadsStopped()) {
  42. stopAndResetThreads();
  43. m_videoState.reset();
  44. m_currentFile.clear();
  45. m_state = PlayerState::Idle;
  46. qCDebug(playerControllerLog)
  47. << "[PlayerController] All threads stopped, force reset to Idle.";
  48. }
  49. // 初始化中,忽略新请求
  50. if (m_state == PlayerState::Initializing) {
  51. qCDebug(playerControllerLog) << "Player is initializing. Ignoring request.";
  52. return;
  53. }
  54. // 正在播放中,检查是否需要切换文件
  55. if (m_state == PlayerState::Playing) {
  56. if (m_currentFile == file) {
  57. qCDebug(playerControllerLog) << "Already playing the same file. Ignoring request.";
  58. return;
  59. } else {
  60. qCDebug(playerControllerLog)
  61. << "Player is busy with another file, stopping and switching to:" << file;
  62. stopPlay();
  63. // 这里直接 fallthrough 到 Idle 状态
  64. }
  65. }
  66. // 空闲状态,开始新播放
  67. if (m_state == PlayerState::Idle) {
  68. // 确保所有线程已停止
  69. if (!areAllThreadsStopped()) {
  70. qCDebug(playerControllerLog) << "Some threads still running, stopping them first";
  71. stopAndResetThreads();
  72. }
  73. // 重置状态
  74. m_videoState.reset();
  75. m_currentFile.clear();
  76. qCDebug(playerControllerLog) << "Player is idle. Starting playback for:" << file;
  77. m_state = PlayerState::Initializing;
  78. m_currentFile = file;
  79. // 启动异步初始化线程
  80. if (m_initThread.joinable()) {
  81. m_initThread.join();
  82. }
  83. m_initThread = std::thread(&PlayerController::asyncInit, this, file);
  84. }
  85. }
  86. void PlayerController::asyncInit(const QString& file)
  87. {
  88. bool success = false;
  89. QElapsedTimer timer;
  90. timer.start();
  91. qCDebug(playerControllerLog) << "[Init] asyncInit started";
  92. // 检查文件有效性
  93. if (file.isEmpty()) {
  94. qCWarning(playerControllerLog) << "Filename is invalid. Please select a valid media file.";
  95. success = false;
  96. } else {
  97. // 检查文件是否存在和可访问
  98. QFileInfo fileInfo(file);
  99. if (!fileInfo.exists() || !fileInfo.isReadable()) {
  100. qCWarning(playerControllerLog)
  101. << "File does not exist or is not readable:" << toNativePath(file);
  102. success = false;
  103. } else {
  104. qCInfo(playerControllerLog) << "File check passed:" << toNativePath(file);
  105. success = true;
  106. }
  107. }
  108. m_initSuccess = success;
  109. onAsyncInitFinished(file, success);
  110. qCDebug(playerControllerLog) << "[Init] asyncInit finished in " << timer.elapsed() << " ms";
  111. }
  112. void PlayerController::onAsyncInitFinished(const QString& file, bool success)
  113. {
  114. std::lock_guard<std::mutex> lock(m_stopMutex);
  115. QElapsedTimer timer;
  116. timer.start();
  117. // 初始化失败处理
  118. if (!success) {
  119. playFailed(m_currentFile);
  120. m_state = PlayerState::Idle;
  121. return;
  122. }
  123. // 创建视频状态对象
  124. qCDebug(playerControllerLog) << "[Init] createVideoState...";
  125. if (!createVideoState(m_currentFile)) {
  126. qCWarning(playerControllerLog) << "Video state creation failed";
  127. readPacketStopped();
  128. playFailed(m_currentFile);
  129. m_state = PlayerState::Idle;
  130. return;
  131. }
  132. // 检查状态有效性
  133. assert(m_videoState);
  134. if (!m_videoState) {
  135. qCWarning(playerControllerLog) << "Video state initialization error";
  136. playFailed(m_currentFile);
  137. m_state = PlayerState::Idle;
  138. return;
  139. }
  140. // 创建数据包读取线程
  141. qCDebug(playerControllerLog) << "[Init] createReadThread...";
  142. if (!createReadThread()) {
  143. qCWarning(playerControllerLog) << "Packet read thread creation failed";
  144. playFailed(m_currentFile);
  145. m_state = PlayerState::Idle;
  146. return;
  147. }
  148. // 设置视频状态
  149. m_packetReadThread->set_video_state(m_videoState->get_state());
  150. // 检查媒体流类型
  151. const bool hasVideo = playingHasVideo();
  152. const bool hasAudio = playingHasAudio();
  153. const bool hasSubtitle = playingHasSubtitle();
  154. // 创建视频相关线程
  155. if (hasVideo) {
  156. if (!createDecodeVideoThread() || !createVideoPlayThread()) {
  157. qCWarning(playerControllerLog) << "Video processing setup failed";
  158. playFailed(m_currentFile);
  159. stopPlay();
  160. m_state = PlayerState::Idle;
  161. return;
  162. }
  163. }
  164. // 创建音频相关线程
  165. if (hasAudio) {
  166. if (!createDecodeAudioThread() || !createAudioPlayThread()) {
  167. qCWarning(playerControllerLog) << "Audio processing setup failed";
  168. playFailed(m_currentFile);
  169. stopPlay();
  170. m_state = PlayerState::Idle;
  171. return;
  172. }
  173. }
  174. // 创建字幕线程
  175. if (hasSubtitle && !createDecodeSubtitleThread()) {
  176. qCWarning(playerControllerLog) << "Subtitle processing setup failed";
  177. playFailed(m_currentFile);
  178. stopPlay();
  179. m_state = PlayerState::Idle;
  180. return;
  181. }
  182. // 开始播放
  183. if (hasAudio) {
  184. startPlayThread(); // 异步启动(处理音频设备初始化)
  185. } else {
  186. playStarted(); // 同步启动(无音频流)
  187. }
  188. emit startToPlaySignal();
  189. m_state = PlayerState::Playing;
  190. qCDebug(playerControllerLog) << "Playback initialized in " << timer.elapsed() << " ms";
  191. }
  192. void PlayerController::stopPlay()
  193. {
  194. std::lock_guard<std::mutex> lock(m_stopMutex);
  195. if (m_state == PlayerState::Idle)
  196. return;
  197. qCDebug(playerControllerLog) << "Stopping playback...";
  198. m_state = PlayerState::Stopping;
  199. // 停止并重置所有线程
  200. stopAndResetThreads();
  201. // 清理视频状态和文件信息
  202. m_videoState.reset();
  203. m_currentFile.clear();
  204. m_state = PlayerState::Idle;
  205. qCDebug(playerControllerLog) << "Playback stopped.";
  206. }
  207. void PlayerController::pausePlay()
  208. {
  209. if (!m_videoState)
  210. return;
  211. if (auto state = m_videoState->get_state())
  212. toggle_pause(state, !state->paused);
  213. emit updatePlayControlStatus();
  214. }
  215. void PlayerController::playMute(bool mute)
  216. {
  217. if (!m_videoState)
  218. return;
  219. if (auto state = m_videoState->get_state())
  220. toggle_mute(state, mute);
  221. }
  222. void PlayerController::playStartSeek()
  223. {
  224. emit playSeek();
  225. pausePlay();
  226. }
  227. void PlayerController::playSeekPre()
  228. {
  229. videoSeekInc(-2);
  230. }
  231. void PlayerController::playSeekNext()
  232. {
  233. videoSeekInc(2);
  234. }
  235. void PlayerController::setVolume(int volume, int maxValue)
  236. {
  237. if (!m_audioPlayThread)
  238. return;
  239. const float vol = static_cast<float>(volume) / maxValue;
  240. m_audioPlayThread->set_device_volume(vol);
  241. }
  242. void PlayerController::setPlaySpeed(double speed)
  243. {
  244. if (m_videoState) {
  245. if (auto state = m_videoState->get_state()) {
  246. #if USE_AVFILTER_AUDIO
  247. set_audio_playspeed(state, speed);
  248. #endif
  249. }
  250. }
  251. }
  252. // 状态访问接口
  253. QString PlayerController::playingFile() const
  254. {
  255. return isPlaying() ? m_currentFile : QString();
  256. }
  257. bool PlayerController::isPlaying() const
  258. {
  259. return m_state == PlayerState::Playing;
  260. }
  261. bool PlayerController::playingHasVideo()
  262. {
  263. return m_videoState ? m_videoState->has_video() : false;
  264. }
  265. bool PlayerController::playingHasAudio()
  266. {
  267. return m_videoState ? m_videoState->has_audio() : false;
  268. }
  269. bool PlayerController::playingHasSubtitle()
  270. {
  271. return m_videoState ? m_videoState->has_subtitle() : false;
  272. }
  273. VideoState* PlayerController::state()
  274. {
  275. return m_videoState ? m_videoState->get_state() : nullptr;
  276. }
  277. float PlayerController::deviceVolume() const
  278. {
  279. return m_audioPlayThread ? m_audioPlayThread->get_device_volume() : 0.0f;
  280. }
  281. void PlayerController::setDeviceVolume(float volume)
  282. {
  283. if (m_audioPlayThread)
  284. m_audioPlayThread->set_device_volume(volume);
  285. }
  286. // 播放状态回调槽函数
  287. void PlayerController::playStarted(bool success)
  288. {
  289. if (!success) {
  290. qCWarning(playerControllerLog) << "Audio device initialization failed!";
  291. return;
  292. }
  293. allThreadStart();
  294. setThreads();
  295. }
  296. void PlayerController::playFailed(const QString& file)
  297. {
  298. emit showMessage(QString("Playback failed: %1").arg(toNativePath(file)), "Warning", "");
  299. }
  300. // 线程 finished 槽函数只做日志和信号
  301. void PlayerController::readPacketStopped()
  302. {
  303. qCDebug(playerControllerLog) << "************* Read packets thread stopped signal received.";
  304. //m_packetReadThread.reset();
  305. // stopPlay();
  306. if (m_videoState) {
  307. m_videoState->delete_video_state();
  308. }
  309. emit audioStopped();
  310. }
  311. void PlayerController::decodeVideoStopped()
  312. {
  313. qCDebug(playerControllerLog) << "************* Video decode thread stopped.";
  314. }
  315. void PlayerController::decodeAudioStopped()
  316. {
  317. qCDebug(playerControllerLog) << "************* Audio decode thread stopped.";
  318. }
  319. void PlayerController::decodeSubtitleStopped()
  320. {
  321. qCDebug(playerControllerLog) << "************* Subtitle decode thread stopped.";
  322. }
  323. void PlayerController::audioPlayStopped()
  324. {
  325. qCDebug(playerControllerLog) << "************* Audio play thread stopped.";
  326. emit audioStopped();
  327. }
  328. void PlayerController::videoPlayStopped()
  329. {
  330. qCDebug(playerControllerLog) << "************* Video play thread stopped.";
  331. emit videoStopped();
  332. }
  333. // 线程管理槽函数
  334. void PlayerController::setThreads()
  335. {
  336. if (!m_videoState)
  337. return;
  338. Threads threads;
  339. threads.read_tid = m_packetReadThread.get();
  340. threads.video_decode_tid = m_decodeVideoThread.get();
  341. threads.audio_decode_tid = m_decodeAudioThread.get();
  342. threads.video_play_tid = m_videoPlayThread.get();
  343. threads.audio_play_tid = m_audioPlayThread.get();
  344. threads.subtitle_decode_tid = m_decodeSubtitleThread.get();
  345. m_videoState->threads_setting(m_videoState->get_state(), threads);
  346. }
  347. void PlayerController::startSendData(bool send)
  348. {
  349. if (m_audioPlayThread)
  350. m_audioPlayThread->send_visual_open(send);
  351. }
  352. void PlayerController::videoSeek(double position, double increment)
  353. {
  354. if (!m_videoState)
  355. return;
  356. auto state = m_videoState->get_state();
  357. if (!state)
  358. return;
  359. if (state->ic->start_time != AV_NOPTS_VALUE
  360. && position < state->ic->start_time / static_cast<double>(AV_TIME_BASE)) {
  361. position = state->ic->start_time / static_cast<double>(AV_TIME_BASE);
  362. }
  363. stream_seek(state,
  364. static_cast<int64_t>(position * AV_TIME_BASE),
  365. static_cast<int64_t>(increment * AV_TIME_BASE),
  366. 0);
  367. }
  368. // 线程管理辅助方法
  369. void PlayerController::stopAndResetThreads()
  370. {
  371. auto stopAndReset = [](auto& threadPtr) {
  372. if (threadPtr) {
  373. qCDebug(playerControllerLog)
  374. << "[stopAndReset] try stop/join thread, isRunning=" << threadPtr->isRunning();
  375. threadPtr->stop();
  376. threadPtr->join();
  377. qCDebug(playerControllerLog) << "[stopAndReset] thread joined and will reset.";
  378. threadPtr.reset();
  379. }
  380. };
  381. // 按依赖顺序停止线程
  382. stopAndReset(m_stopPlayWaitingThread);
  383. stopAndReset(m_beforePlayThread);
  384. stopAndReset(m_packetReadThread);
  385. stopAndReset(m_decodeVideoThread);
  386. stopAndReset(m_decodeAudioThread);
  387. stopAndReset(m_decodeSubtitleThread);
  388. stopAndReset(m_videoPlayThread);
  389. stopAndReset(m_audioPlayThread);
  390. }
  391. bool PlayerController::areAllThreadsStopped() const
  392. {
  393. // 检查所有线程是否已停止
  394. return (!m_packetReadThread || !m_packetReadThread->isRunning())
  395. && (!m_decodeVideoThread || !m_decodeVideoThread->isRunning())
  396. && (!m_decodeAudioThread || !m_decodeAudioThread->isRunning())
  397. && (!m_audioPlayThread || !m_audioPlayThread->isRunning())
  398. && (!m_videoPlayThread || !m_videoPlayThread->isRunning())
  399. && (!m_decodeSubtitleThread || !m_decodeSubtitleThread->isRunning());
  400. }
  401. bool PlayerController::waitStopPlay(const QString& file)
  402. {
  403. m_stopPlayWaitingThread = std::make_unique<StopWaitingThread>(this, file.toStdString());
  404. m_stopPlayWaitingThread->setOnFinished([this]() {
  405. // 可根据需要添加额外处理
  406. //m_stopPlayWaitingThread.reset();
  407. });
  408. m_stopPlayWaitingThread->start();
  409. qCDebug(playerControllerLog) << "++++++++++ StopPlay waiting thread started";
  410. return true;
  411. }
  412. void PlayerController::allThreadStart()
  413. {
  414. // 启动所有创建的线程
  415. if (m_packetReadThread) {
  416. if (!m_videoState || !m_videoState->get_state()) {
  417. qCWarning(playerControllerLog) << "VideoState invalid, skip starting read thread";
  418. } else {
  419. m_packetReadThread->start();
  420. }
  421. qCDebug(playerControllerLog) << "++++++++++ Read packets thread started";
  422. }
  423. if (m_decodeVideoThread) {
  424. m_decodeVideoThread->start();
  425. qCDebug(playerControllerLog) << "++++++++++ Video decode thread started";
  426. }
  427. if (m_decodeAudioThread) {
  428. m_decodeAudioThread->start();
  429. qCDebug(playerControllerLog) << "++++++++++ Audio decode thread started";
  430. }
  431. if (m_decodeSubtitleThread) {
  432. m_decodeSubtitleThread->start();
  433. qCDebug(playerControllerLog) << "++++++++++ Subtitle decode thread started";
  434. }
  435. if (m_videoPlayThread) {
  436. m_videoPlayThread->start();
  437. qCDebug(playerControllerLog) << "++++++++++ Video play thread started";
  438. }
  439. if (m_audioPlayThread) {
  440. m_audioPlayThread->start();
  441. qCDebug(playerControllerLog) << "++++++++++ Audio play thread started";
  442. }
  443. // 通知UI更新
  444. emit setPlayControlWnd(true);
  445. emit updatePlayControlVolume();
  446. emit updatePlayControlStatus();
  447. }
  448. // 辅助函数
  449. void PlayerController::videoSeekInc(double increment)
  450. {
  451. if (!m_videoState)
  452. return;
  453. auto state = m_videoState->get_state();
  454. if (!state)
  455. return;
  456. double position = get_master_clock(state);
  457. if (std::isnan(position)) {
  458. position = static_cast<double>(state->seek_pos) / AV_TIME_BASE;
  459. }
  460. position += increment;
  461. videoSeek(position, increment);
  462. }
  463. // 线程创建方法
  464. bool PlayerController::createVideoState(const QString& file)
  465. {
  466. const bool useHardware = false; // 待实现:来自UI设置
  467. const bool loop = false; // 待实现:来自UI设置
  468. if (m_videoState)
  469. return false;
  470. m_videoState = std::make_unique<VideoStateData>(useHardware, loop);
  471. const int ret = m_videoState->create_video_state(file.toUtf8().constData());
  472. if (ret < 0) {
  473. m_videoState.reset();
  474. qCWarning(playerControllerLog) << "Video state creation failed (error: " << ret << ")";
  475. return false;
  476. }
  477. return true;
  478. }
  479. void PlayerController::deleteVideoState()
  480. {
  481. m_videoState.reset();
  482. }
  483. bool PlayerController::createReadThread()
  484. {
  485. if (m_packetReadThread)
  486. return false;
  487. m_packetReadThread = std::make_unique<ReadThread>(m_videoState ? m_videoState->get_state()
  488. : nullptr);
  489. m_packetReadThread->setOnFinished([this]() { readPacketStopped(); });
  490. return true;
  491. }
  492. bool PlayerController::createDecodeVideoThread()
  493. {
  494. if (!m_videoState || m_decodeVideoThread)
  495. return false;
  496. auto state = m_videoState->get_state();
  497. if (!state)
  498. return false;
  499. m_decodeVideoThread = std::make_unique<VideoDecodeThread>(state);
  500. m_decodeVideoThread->setOnFinished([this]() { decodeVideoStopped(); });
  501. auto codecContext = m_videoState->get_contex(AVMEDIA_TYPE_VIDEO);
  502. // 初始化视频解码器
  503. int ret = decoder_init(&state->viddec,
  504. codecContext,
  505. &state->videoq,
  506. state->continue_read_thread);
  507. if (ret < 0) {
  508. qCWarning(playerControllerLog)
  509. << "Video decoder initialization failed (error: " << ret << ")";
  510. return false;
  511. }
  512. ret = decoder_start(&state->viddec, m_decodeVideoThread.get(), "video_decoder");
  513. if (ret < 0) {
  514. qCWarning(playerControllerLog) << "Video decoder start failed (error: " << ret << ")";
  515. return false;
  516. }
  517. state->queue_attachments_req = 1;
  518. return true;
  519. }
  520. bool PlayerController::createDecodeAudioThread()
  521. {
  522. if (!m_videoState || m_decodeAudioThread)
  523. return false;
  524. auto state = m_videoState->get_state();
  525. if (!state)
  526. return false;
  527. m_decodeAudioThread = std::make_unique<AudioDecodeThread>(state);
  528. m_decodeAudioThread->setOnFinished([this]() { decodeAudioStopped(); });
  529. auto codecContext = m_videoState->get_contex(AVMEDIA_TYPE_AUDIO);
  530. // 初始化音频解码器
  531. int ret = decoder_init(&state->auddec,
  532. codecContext,
  533. &state->audioq,
  534. state->continue_read_thread);
  535. if (ret < 0) {
  536. qCWarning(playerControllerLog)
  537. << "Audio decoder initialization failed (error: " << ret << ")";
  538. return false;
  539. }
  540. ret = decoder_start(&state->auddec, m_decodeAudioThread.get(), "audio_decoder");
  541. if (ret < 0) {
  542. qCWarning(playerControllerLog) << "Audio decoder start failed (error: " << ret << ")";
  543. return false;
  544. }
  545. return true;
  546. }
  547. bool PlayerController::createDecodeSubtitleThread()
  548. {
  549. if (!m_videoState || m_decodeSubtitleThread)
  550. return false;
  551. auto state = m_videoState->get_state();
  552. if (!state)
  553. return false;
  554. m_decodeSubtitleThread = std::make_unique<SubtitleDecodeThread>(state);
  555. m_decodeSubtitleThread->setOnFinished([this]() { decodeSubtitleStopped(); });
  556. auto codecContext = m_videoState->get_contex(AVMEDIA_TYPE_SUBTITLE);
  557. // 初始化字幕解码器
  558. int ret = decoder_init(&state->subdec,
  559. codecContext,
  560. &state->subtitleq,
  561. state->continue_read_thread);
  562. if (ret < 0) {
  563. qCWarning(playerControllerLog)
  564. << "Subtitle decoder initialization failed (error: " << ret << ")";
  565. return false;
  566. }
  567. ret = decoder_start(&state->subdec, m_decodeSubtitleThread.get(), "subtitle_decoder");
  568. if (ret < 0) {
  569. qCWarning(playerControllerLog) << "Subtitle decoder start failed (error: " << ret << ")";
  570. return false;
  571. }
  572. return true;
  573. }
  574. bool PlayerController::createVideoPlayThread()
  575. {
  576. if (!m_videoState || m_videoPlayThread)
  577. return false;
  578. auto state = m_videoState->get_state();
  579. if (!state)
  580. return false;
  581. m_videoPlayThread = std::make_unique<VideoPlayThread>(state);
  582. m_videoPlayThread->setOnFinished([this]() { videoPlayStopped(); });
  583. m_videoPlayThread->setOnFrameReady([this](AVFrame* frame) { this->onFrameReady(frame); });
  584. m_videoPlayThread->setOnSubtitleReady([this](const QString& text) {
  585. // TODO: 实现 PlayerController::onSubtitleReady(const QString&) 处理字幕
  586. // onSubtitleReady(text);
  587. });
  588. // 初始化参数
  589. auto videoContext = m_videoState->get_contex(AVMEDIA_TYPE_VIDEO);
  590. const bool useHardware = m_videoState->is_hardware_decode();
  591. if (!m_videoPlayThread->init_resample_param(videoContext, useHardware)) {
  592. qCWarning(playerControllerLog) << "Video resample parameters initialization failed";
  593. return false;
  594. }
  595. return true;
  596. }
  597. bool PlayerController::createAudioPlayThread()
  598. {
  599. if (!m_videoState || m_audioPlayThread)
  600. return false;
  601. auto state = m_videoState->get_state();
  602. if (!state)
  603. return false;
  604. m_audioPlayThread = std::make_unique<AudioPlayThread>(state);
  605. m_audioPlayThread->setOnFinished([this]() { audioPlayStopped(); });
  606. m_audioPlayThread->setOnUpdatePlayTime([this]() {
  607. // TODO: 实现 PlayerController::onUpdatePlayTime() 处理播放时间更新
  608. // emit updatePlayTime();
  609. });
  610. m_audioPlayThread->setOnDataVisualReady([this](const AudioData& data) {
  611. // 异步 ?
  612. // emit audioData(data);
  613. });
  614. // 音频设备初始化在独立线程中完成
  615. return true;
  616. }
  617. bool PlayerController::startPlayThread()
  618. {
  619. if (m_beforePlayThread)
  620. return false;
  621. m_beforePlayThread = std::make_unique<StartPlayThread>(m_audioPlayThread.get(),
  622. m_videoState.get());
  623. m_beforePlayThread->setOnFinished([this]() {
  624. qCDebug(playerControllerLog) << "[StartPlayThread] finished, call playStarted()";
  625. playStarted();
  626. });
  627. m_beforePlayThread->start();
  628. qCDebug(playerControllerLog) << "++++++++++ StartPlay thread (audio init) started";
  629. return true;
  630. }
  631. // 调试辅助函数
  632. void PlayerController::printDecodeContext(const AVCodecContext* codecCtx, bool isVideo) const
  633. {
  634. if (!codecCtx)
  635. return;
  636. qCInfo(playerControllerLog) << (isVideo ? "Video" : "Audio")
  637. << " codec: " << codecCtx->codec->name;
  638. qCInfo(playerControllerLog) << " Type:" << codecCtx->codec_type << "ID:" << codecCtx->codec_id
  639. << "Tag:" << codecCtx->codec_tag;
  640. if (isVideo) {
  641. qCInfo(playerControllerLog)
  642. << " Dimensions: " << codecCtx->width << "x" << codecCtx->height;
  643. } else {
  644. qCInfo(playerControllerLog) << " Sample rate: " << codecCtx->sample_rate
  645. << " Hz, Channels: " << codecCtx->ch_layout.nb_channels
  646. << ", Format: " << codecCtx->sample_fmt;
  647. qCInfo(playerControllerLog) << " Frame size: " << codecCtx->frame_size
  648. << ", Block align: " << codecCtx->block_align;
  649. }
  650. }
  651. // 在合适位置实现 onFrameReady
  652. void PlayerController::onFrameReady(AVFrame* frame)
  653. {
  654. // 这里可以做帧处理、缓存、同步等操作
  655. emit frameReady(frame); // 直接转发给 UI 层
  656. }