playercontroller.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. PlayerController::PlayerController(QWidget* parent)
  23. : QObject(parent)
  24. {
  25. ffmpeg_init();
  26. }
  27. PlayerController::~PlayerController()
  28. {
  29. stopPlay();
  30. }
  31. // 播放控制接口
  32. void PlayerController::startToPlay(const QString& file)
  33. {
  34. if (isPlaying()) {
  35. if (m_currentFile == file)
  36. return;
  37. waitStopPlay(file);
  38. return;
  39. }
  40. m_currentFile = file;
  41. if (!startPlay()) {
  42. playFailed(m_currentFile);
  43. return;
  44. }
  45. emit startToPlaySignal();
  46. }
  47. void PlayerController::stopPlay()
  48. {
  49. deleteVideoState();
  50. }
  51. void PlayerController::pausePlay()
  52. {
  53. if (!m_videoState)
  54. return;
  55. if (auto state = m_videoState->get_state())
  56. toggle_pause(state, !state->paused);
  57. emit updatePlayControlStatus();
  58. }
  59. void PlayerController::playMute(bool mute)
  60. {
  61. if (!m_videoState)
  62. return;
  63. if (auto state = m_videoState->get_state())
  64. toggle_mute(state, mute);
  65. }
  66. void PlayerController::playStartSeek()
  67. {
  68. emit playSeek();
  69. pausePlay();
  70. }
  71. void PlayerController::playSeekPre()
  72. {
  73. videoSeekInc(-2);
  74. }
  75. void PlayerController::playSeekNext()
  76. {
  77. videoSeekInc(2);
  78. }
  79. void PlayerController::setVolume(int volume, int maxValue)
  80. {
  81. if (!m_audioPlayThread)
  82. return;
  83. const float vol = static_cast<float>(volume) / maxValue;
  84. m_audioPlayThread->set_device_volume(vol);
  85. }
  86. void PlayerController::setPlaySpeed(double speed)
  87. {
  88. if (m_videoState) {
  89. if (auto state = m_videoState->get_state()) {
  90. #if USE_AVFILTER_AUDIO
  91. set_audio_playspeed(state, speed);
  92. #endif
  93. }
  94. }
  95. }
  96. // 状态访问接口
  97. QString PlayerController::playingFile() const
  98. {
  99. return isPlaying() ? m_currentFile : QString();
  100. }
  101. bool PlayerController::isPlaying() const
  102. {
  103. return m_videoState || m_packetReadThread || m_decodeVideoThread || m_decodeAudioThread
  104. || m_audioPlayThread || m_videoPlayThread || m_decodeSubtitleThread;
  105. }
  106. bool PlayerController::playingHasVideo()
  107. {
  108. return m_videoState ? m_videoState->has_video() : false;
  109. }
  110. bool PlayerController::playingHasAudio()
  111. {
  112. return m_videoState ? m_videoState->has_audio() : false;
  113. }
  114. bool PlayerController::playingHasSubtitle()
  115. {
  116. return m_videoState ? m_videoState->has_subtitle() : false;
  117. }
  118. VideoState* PlayerController::state()
  119. {
  120. return m_videoState ? m_videoState->get_state() : nullptr;
  121. }
  122. float PlayerController::deviceVolume() const
  123. {
  124. return m_audioPlayThread ? m_audioPlayThread->get_device_volume() : 0.0f;
  125. }
  126. void PlayerController::setDeviceVolume(float volume)
  127. {
  128. if (m_audioPlayThread)
  129. m_audioPlayThread->set_device_volume(volume);
  130. }
  131. // 播放状态回调槽函数
  132. void PlayerController::playStarted(bool success)
  133. {
  134. if (!success) {
  135. qWarning("Audio device initialization failed!");
  136. return;
  137. }
  138. allThreadStart();
  139. setThreads();
  140. }
  141. void PlayerController::playFailed(const QString& file)
  142. {
  143. emit showMessage(QString("Playback failed: %1").arg(toNativePath(file)), "Warning", "");
  144. }
  145. // 线程生命周期管理槽函数
  146. void PlayerController::readPacketStopped()
  147. {
  148. if (m_packetReadThread) {
  149. m_packetReadThread.reset();
  150. qDebug("************* Read packets thread stopped.");
  151. }
  152. stopPlay();
  153. }
  154. void PlayerController::decodeVideoStopped()
  155. {
  156. m_decodeVideoThread.reset();
  157. qDebug("************* Video decode thread stopped.");
  158. }
  159. void PlayerController::decodeAudioStopped()
  160. {
  161. m_decodeAudioThread.reset();
  162. qDebug("************* Audio decode thread stopped.");
  163. }
  164. void PlayerController::decodeSubtitleStopped()
  165. {
  166. m_decodeSubtitleThread.reset();
  167. qDebug("************* Subtitle decode thread stopped.");
  168. }
  169. void PlayerController::audioPlayStopped()
  170. {
  171. m_audioPlayThread.reset();
  172. qDebug("************* Audio play thread stopped.");
  173. }
  174. void PlayerController::videoPlayStopped()
  175. {
  176. m_videoPlayThread.reset();
  177. qDebug("************* Video play thread stopped.");
  178. }
  179. // 线程管理槽函数
  180. void PlayerController::setThreads()
  181. {
  182. if (!m_videoState)
  183. return;
  184. Threads threads;
  185. threads.read_tid = m_packetReadThread.get();
  186. threads.video_decode_tid = m_decodeVideoThread.get();
  187. threads.audio_decode_tid = m_decodeAudioThread.get();
  188. threads.video_play_tid = m_videoPlayThread.get();
  189. threads.audio_play_tid = m_audioPlayThread.get();
  190. threads.subtitle_decode_tid = m_decodeSubtitleThread.get();
  191. m_videoState->threads_setting(m_videoState->get_state(), threads);
  192. }
  193. void PlayerController::startSendData(bool send)
  194. {
  195. if (m_audioPlayThread)
  196. m_audioPlayThread->send_visual_open(send);
  197. }
  198. void PlayerController::videoSeek(double position, double increment)
  199. {
  200. if (!m_videoState)
  201. return;
  202. auto state = m_videoState->get_state();
  203. if (!state)
  204. return;
  205. if (state->ic->start_time != AV_NOPTS_VALUE
  206. && position < state->ic->start_time / static_cast<double>(AV_TIME_BASE)) {
  207. position = state->ic->start_time / static_cast<double>(AV_TIME_BASE);
  208. }
  209. stream_seek(state,
  210. static_cast<int64_t>(position * AV_TIME_BASE),
  211. static_cast<int64_t>(increment * AV_TIME_BASE),
  212. 0);
  213. }
  214. // 核心私有实现
  215. bool PlayerController::startPlay()
  216. {
  217. QElapsedTimer timer;
  218. timer.start();
  219. if (m_currentFile.isEmpty()) {
  220. qWarning("Filename is invalid. Please select a valid media file.");
  221. return false;
  222. }
  223. qInfo("Starting playback: %s", qUtf8Printable(toNativePath(m_currentFile)));
  224. // 创建数据包读取线程
  225. if (!createReadThread()) {
  226. qWarning("Packet read thread creation failed");
  227. return false;
  228. }
  229. // 创建视频状态对象
  230. if (!createVideoState(m_currentFile)) {
  231. qWarning("Video state creation failed");
  232. readPacketStopped();
  233. return false;
  234. }
  235. // 检查状态有效性
  236. assert(m_videoState);
  237. if (!m_videoState) {
  238. qWarning("Video state initialization error");
  239. return false;
  240. }
  241. m_packetReadThread->set_video_state(m_videoState->get_state());
  242. const bool hasVideo = playingHasVideo();
  243. const bool hasAudio = playingHasAudio();
  244. const bool hasSubtitle = playingHasSubtitle();
  245. // 创建视频相关线程
  246. if (hasVideo) {
  247. if (!createDecodeVideoThread() || !createVideoPlayThread()) {
  248. qWarning("Video processing setup failed");
  249. return false;
  250. }
  251. }
  252. // 创建音频相关线程
  253. if (hasAudio) {
  254. if (!createDecodeAudioThread() || !createAudioPlayThread()) {
  255. qWarning("Audio processing setup failed");
  256. return false;
  257. }
  258. }
  259. // 创建字幕线程
  260. if (hasSubtitle && !createDecodeSubtitleThread()) {
  261. qWarning("Subtitle processing setup failed");
  262. return false;
  263. }
  264. // 开始播放
  265. if (hasAudio) {
  266. startPlayThread(); // 异步启动(处理音频设备初始化)
  267. } else {
  268. playStarted(); // 同步启动(无音频流)
  269. }
  270. qDebug("Playback initialized in %lld ms", timer.elapsed());
  271. return true;
  272. }
  273. void PlayerController::waitStopPlay(const QString& file)
  274. {
  275. m_stopPlayWaitingThread = std::make_unique<StopWaitingThread>(this, file);
  276. connect(m_stopPlayWaitingThread.get(),
  277. &StopWaitingThread::stopPlay,
  278. this,
  279. &PlayerController::stopPlay);
  280. connect(m_stopPlayWaitingThread.get(),
  281. &StopWaitingThread::startPlay,
  282. this,
  283. &PlayerController::startToPlay);
  284. m_stopPlayWaitingThread->start();
  285. qDebug("++++++++++ StopPlay waiting thread started");
  286. }
  287. void PlayerController::allThreadStart()
  288. {
  289. // 启动所有创建的线程
  290. if (m_packetReadThread) {
  291. m_packetReadThread->start();
  292. qDebug("++++++++++ Read packets thread started");
  293. }
  294. if (m_decodeVideoThread) {
  295. m_decodeVideoThread->start();
  296. qDebug("++++++++++ Video decode thread started");
  297. }
  298. if (m_decodeAudioThread) {
  299. m_decodeAudioThread->start(QThread::Priority::HighPriority);
  300. qDebug("++++++++++ Audio decode thread started");
  301. }
  302. if (m_decodeSubtitleThread) {
  303. m_decodeSubtitleThread->start();
  304. qDebug("++++++++++ Subtitle decode thread started");
  305. }
  306. if (m_videoPlayThread) {
  307. m_videoPlayThread->start();
  308. qDebug("++++++++++ Video play thread started");
  309. }
  310. if (m_audioPlayThread) {
  311. m_audioPlayThread->start();
  312. qDebug("++++++++++ Audio play thread started");
  313. }
  314. // 通知UI更新
  315. emit setPlayControlWnd(true);
  316. emit updatePlayControlVolume();
  317. emit updatePlayControlStatus();
  318. }
  319. // 辅助函数
  320. void PlayerController::videoSeekInc(double increment)
  321. {
  322. if (!m_videoState)
  323. return;
  324. auto state = m_videoState->get_state();
  325. if (!state)
  326. return;
  327. double position = get_master_clock(state);
  328. if (std::isnan(position)) {
  329. position = static_cast<double>(state->seek_pos) / AV_TIME_BASE;
  330. }
  331. position += increment;
  332. videoSeek(position, increment);
  333. }
  334. // 线程创建方法
  335. bool PlayerController::createVideoState(const QString& file)
  336. {
  337. const bool useHardware = false; // 待实现:来自UI设置
  338. const bool loop = false; // 待实现:来自UI设置
  339. if (m_videoState)
  340. return false;
  341. m_videoState = std::make_unique<VideoStateData>(useHardware, loop);
  342. const int ret = m_videoState->create_video_state(file.toUtf8().constData());
  343. if (ret < 0) {
  344. m_videoState.reset();
  345. qWarning("Video state creation failed (error: %d)", ret);
  346. return false;
  347. }
  348. return true;
  349. }
  350. void PlayerController::deleteVideoState()
  351. {
  352. m_videoState.reset();
  353. }
  354. bool PlayerController::createReadThread()
  355. {
  356. if (m_packetReadThread)
  357. return false;
  358. m_packetReadThread = std::make_unique<ReadThread>(this);
  359. connect(m_packetReadThread.get(),
  360. &ReadThread::finished,
  361. this,
  362. &PlayerController::readPacketStopped);
  363. return true;
  364. }
  365. bool PlayerController::createDecodeVideoThread()
  366. {
  367. if (!m_videoState || m_decodeVideoThread)
  368. return false;
  369. auto state = m_videoState->get_state();
  370. if (!state)
  371. return false;
  372. m_decodeVideoThread = std::make_unique<VideoDecodeThread>(this, state);
  373. connect(m_decodeVideoThread.get(),
  374. &VideoDecodeThread::finished,
  375. this,
  376. &PlayerController::decodeVideoStopped);
  377. auto codecContext = m_videoState->get_contex(AVMEDIA_TYPE_VIDEO);
  378. // 初始化视频解码器
  379. int ret = decoder_init(&state->viddec,
  380. codecContext,
  381. &state->videoq,
  382. state->continue_read_thread);
  383. if (ret < 0) {
  384. qWarning("Video decoder initialization failed (error: %d)", ret);
  385. return false;
  386. }
  387. ret = decoder_start(&state->viddec, m_decodeVideoThread.get(), "video_decoder");
  388. if (ret < 0) {
  389. qWarning("Video decoder start failed (error: %d)", ret);
  390. return false;
  391. }
  392. state->queue_attachments_req = 1;
  393. return true;
  394. }
  395. bool PlayerController::createDecodeAudioThread()
  396. {
  397. if (!m_videoState || m_decodeAudioThread)
  398. return false;
  399. auto state = m_videoState->get_state();
  400. if (!state)
  401. return false;
  402. m_decodeAudioThread = std::make_unique<AudioDecodeThread>(this, state);
  403. connect(m_decodeAudioThread.get(),
  404. &AudioDecodeThread::finished,
  405. this,
  406. &PlayerController::decodeAudioStopped);
  407. auto codecContext = m_videoState->get_contex(AVMEDIA_TYPE_AUDIO);
  408. // 初始化音频解码器
  409. int ret = decoder_init(&state->auddec,
  410. codecContext,
  411. &state->audioq,
  412. state->continue_read_thread);
  413. if (ret < 0) {
  414. qWarning("Audio decoder initialization failed (error: %d)", ret);
  415. return false;
  416. }
  417. ret = decoder_start(&state->auddec, m_decodeAudioThread.get(), "audio_decoder");
  418. if (ret < 0) {
  419. qWarning("Audio decoder start failed (error: %d)", ret);
  420. return false;
  421. }
  422. return true;
  423. }
  424. bool PlayerController::createDecodeSubtitleThread()
  425. {
  426. if (!m_videoState || m_decodeSubtitleThread)
  427. return false;
  428. auto state = m_videoState->get_state();
  429. if (!state)
  430. return false;
  431. m_decodeSubtitleThread = std::make_unique<SubtitleDecodeThread>(this, state);
  432. connect(m_decodeSubtitleThread.get(),
  433. &SubtitleDecodeThread::finished,
  434. this,
  435. &PlayerController::decodeSubtitleStopped);
  436. auto codecContext = m_videoState->get_contex(AVMEDIA_TYPE_SUBTITLE);
  437. // 初始化字幕解码器
  438. int ret = decoder_init(&state->subdec,
  439. codecContext,
  440. &state->subtitleq,
  441. state->continue_read_thread);
  442. if (ret < 0) {
  443. qWarning("Subtitle decoder initialization failed (error: %d)", ret);
  444. return false;
  445. }
  446. ret = decoder_start(&state->subdec, m_decodeSubtitleThread.get(), "subtitle_decoder");
  447. if (ret < 0) {
  448. qWarning("Subtitle decoder start failed (error: %d)", ret);
  449. return false;
  450. }
  451. return true;
  452. }
  453. bool PlayerController::createVideoPlayThread()
  454. {
  455. if (!m_videoState || m_videoPlayThread)
  456. return false;
  457. auto state = m_videoState->get_state();
  458. if (!state)
  459. return false;
  460. m_videoPlayThread = std::make_unique<VideoPlayThread>(this, state);
  461. // 连接信号
  462. connect(m_videoPlayThread.get(),
  463. &VideoPlayThread::finished,
  464. this,
  465. &PlayerController::videoPlayStopped);
  466. connect(m_videoPlayThread.get(),
  467. &VideoPlayThread::frameReady,
  468. this,
  469. &PlayerController::frameReady);
  470. connect(m_videoPlayThread.get(),
  471. &VideoPlayThread::subtitle_ready,
  472. this,
  473. &PlayerController::subtitleReady);
  474. connect(this,
  475. &PlayerController::stopVideoPlayThread,
  476. m_videoPlayThread.get(),
  477. &VideoPlayThread::stop_thread);
  478. // 初始化参数
  479. auto videoContext = m_videoState->get_contex(AVMEDIA_TYPE_VIDEO);
  480. const bool useHardware = m_videoState->is_hardware_decode();
  481. if (!m_videoPlayThread->init_resample_param(videoContext, useHardware)) {
  482. qWarning("Video resample parameters initialization failed");
  483. return false;
  484. }
  485. return true;
  486. }
  487. bool PlayerController::createAudioPlayThread()
  488. {
  489. if (!m_videoState || m_audioPlayThread)
  490. return false;
  491. auto state = m_videoState->get_state();
  492. if (!state)
  493. return false;
  494. m_audioPlayThread = std::make_unique<AudioPlayThread>(this, state);
  495. // 连接信号
  496. connect(m_audioPlayThread.get(),
  497. &AudioPlayThread::finished,
  498. this,
  499. &PlayerController::audioPlayStopped);
  500. connect(this,
  501. &PlayerController::stopAudioPlayThread,
  502. m_audioPlayThread.get(),
  503. &AudioPlayThread::stop_thread);
  504. connect(m_audioPlayThread.get(),
  505. &AudioPlayThread::update_play_time,
  506. this,
  507. &PlayerController::updatePlayTime);
  508. connect(m_audioPlayThread.get(),
  509. &AudioPlayThread::data_visual_ready,
  510. this,
  511. &PlayerController::audioData);
  512. // 音频设备初始化在独立线程中完成
  513. return true;
  514. }
  515. bool PlayerController::startPlayThread()
  516. {
  517. if (m_beforePlayThread)
  518. return false;
  519. m_beforePlayThread = std::make_unique<StartPlayThread>(this);
  520. connect(m_beforePlayThread.get(),
  521. &StartPlayThread::audio_device_init,
  522. this,
  523. &PlayerController::playStarted);
  524. m_beforePlayThread->start();
  525. qDebug("++++++++++ StartPlay thread (audio init) started");
  526. return true;
  527. }
  528. // 调试辅助函数
  529. void PlayerController::printDecodeContext(const AVCodecContext* codecCtx, bool isVideo) const
  530. {
  531. if (!codecCtx)
  532. return;
  533. qInfo("%s codec: %s", isVideo ? "Video" : "Audio", codecCtx->codec->name);
  534. qInfo(" Type: %d, ID: %d, Tag: %d",
  535. codecCtx->codec_type,
  536. codecCtx->codec_id,
  537. codecCtx->codec_tag);
  538. if (isVideo) {
  539. qInfo(" Dimensions: %dx%d", codecCtx->width, codecCtx->height);
  540. } else {
  541. qInfo(" Sample rate: %d Hz, Channels: %d, Format: %d",
  542. codecCtx->sample_rate,
  543. codecCtx->ch_layout.nb_channels,
  544. codecCtx->sample_fmt);
  545. qInfo(" Frame size: %d, Block align: %d", codecCtx->frame_size, codecCtx->block_align);
  546. }
  547. }