playercontroller.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. emit audioStopped(); // 音频结束
  174. }
  175. void PlayerController::videoPlayStopped()
  176. {
  177. m_videoPlayThread.reset();
  178. qDebug("************* Video play thread stopped.");
  179. emit videoStopped(); // 视频结束
  180. }
  181. // 线程管理槽函数
  182. void PlayerController::setThreads()
  183. {
  184. if (!m_videoState)
  185. return;
  186. Threads threads;
  187. threads.read_tid = m_packetReadThread.get();
  188. threads.video_decode_tid = m_decodeVideoThread.get();
  189. threads.audio_decode_tid = m_decodeAudioThread.get();
  190. threads.video_play_tid = m_videoPlayThread.get();
  191. threads.audio_play_tid = m_audioPlayThread.get();
  192. threads.subtitle_decode_tid = m_decodeSubtitleThread.get();
  193. m_videoState->threads_setting(m_videoState->get_state(), threads);
  194. }
  195. void PlayerController::startSendData(bool send)
  196. {
  197. if (m_audioPlayThread)
  198. m_audioPlayThread->send_visual_open(send);
  199. }
  200. void PlayerController::videoSeek(double position, double increment)
  201. {
  202. if (!m_videoState)
  203. return;
  204. auto state = m_videoState->get_state();
  205. if (!state)
  206. return;
  207. if (state->ic->start_time != AV_NOPTS_VALUE
  208. && position < state->ic->start_time / static_cast<double>(AV_TIME_BASE)) {
  209. position = state->ic->start_time / static_cast<double>(AV_TIME_BASE);
  210. }
  211. stream_seek(state,
  212. static_cast<int64_t>(position * AV_TIME_BASE),
  213. static_cast<int64_t>(increment * AV_TIME_BASE),
  214. 0);
  215. }
  216. // 核心私有实现
  217. bool PlayerController::startPlay()
  218. {
  219. QElapsedTimer timer;
  220. timer.start();
  221. if (m_currentFile.isEmpty()) {
  222. qWarning("Filename is invalid. Please select a valid media file.");
  223. return false;
  224. }
  225. qInfo("Starting playback: %s", qUtf8Printable(toNativePath(m_currentFile)));
  226. // 创建数据包读取线程
  227. if (!createReadThread()) {
  228. qWarning("Packet read thread creation failed");
  229. return false;
  230. }
  231. // 创建视频状态对象
  232. if (!createVideoState(m_currentFile)) {
  233. qWarning("Video state creation failed");
  234. readPacketStopped();
  235. return false;
  236. }
  237. // 检查状态有效性
  238. assert(m_videoState);
  239. if (!m_videoState) {
  240. qWarning("Video state initialization error");
  241. return false;
  242. }
  243. m_packetReadThread->set_video_state(m_videoState->get_state());
  244. const bool hasVideo = playingHasVideo();
  245. const bool hasAudio = playingHasAudio();
  246. const bool hasSubtitle = playingHasSubtitle();
  247. // 创建视频相关线程
  248. if (hasVideo) {
  249. if (!createDecodeVideoThread() || !createVideoPlayThread()) {
  250. qWarning("Video processing setup failed");
  251. return false;
  252. }
  253. }
  254. // 创建音频相关线程
  255. if (hasAudio) {
  256. if (!createDecodeAudioThread() || !createAudioPlayThread()) {
  257. qWarning("Audio processing setup failed");
  258. return false;
  259. }
  260. }
  261. // 创建字幕线程
  262. if (hasSubtitle && !createDecodeSubtitleThread()) {
  263. qWarning("Subtitle processing setup failed");
  264. return false;
  265. }
  266. // 开始播放
  267. if (hasAudio) {
  268. startPlayThread(); // 异步启动(处理音频设备初始化)
  269. } else {
  270. playStarted(); // 同步启动(无音频流)
  271. }
  272. qDebug("Playback initialized in %lld ms", timer.elapsed());
  273. return true;
  274. }
  275. void PlayerController::waitStopPlay(const QString& file)
  276. {
  277. m_stopPlayWaitingThread = std::make_unique<StopWaitingThread>(this, file);
  278. connect(m_stopPlayWaitingThread.get(),
  279. &StopWaitingThread::stopPlay,
  280. this,
  281. &PlayerController::stopPlay);
  282. connect(m_stopPlayWaitingThread.get(),
  283. &StopWaitingThread::startPlay,
  284. this,
  285. &PlayerController::startToPlay);
  286. m_stopPlayWaitingThread->start();
  287. qDebug("++++++++++ StopPlay waiting thread started");
  288. }
  289. void PlayerController::allThreadStart()
  290. {
  291. // 启动所有创建的线程
  292. if (m_packetReadThread) {
  293. m_packetReadThread->start();
  294. qDebug("++++++++++ Read packets thread started");
  295. }
  296. if (m_decodeVideoThread) {
  297. m_decodeVideoThread->start();
  298. qDebug("++++++++++ Video decode thread started");
  299. }
  300. if (m_decodeAudioThread) {
  301. m_decodeAudioThread->start(QThread::Priority::HighPriority);
  302. qDebug("++++++++++ Audio decode thread started");
  303. }
  304. if (m_decodeSubtitleThread) {
  305. m_decodeSubtitleThread->start();
  306. qDebug("++++++++++ Subtitle decode thread started");
  307. }
  308. if (m_videoPlayThread) {
  309. m_videoPlayThread->start();
  310. qDebug("++++++++++ Video play thread started");
  311. }
  312. if (m_audioPlayThread) {
  313. m_audioPlayThread->start();
  314. qDebug("++++++++++ Audio play thread started");
  315. }
  316. // 通知UI更新
  317. emit setPlayControlWnd(true);
  318. emit updatePlayControlVolume();
  319. emit updatePlayControlStatus();
  320. }
  321. // 辅助函数
  322. void PlayerController::videoSeekInc(double increment)
  323. {
  324. if (!m_videoState)
  325. return;
  326. auto state = m_videoState->get_state();
  327. if (!state)
  328. return;
  329. double position = get_master_clock(state);
  330. if (std::isnan(position)) {
  331. position = static_cast<double>(state->seek_pos) / AV_TIME_BASE;
  332. }
  333. position += increment;
  334. videoSeek(position, increment);
  335. }
  336. // 线程创建方法
  337. bool PlayerController::createVideoState(const QString& file)
  338. {
  339. const bool useHardware = false; // 待实现:来自UI设置
  340. const bool loop = false; // 待实现:来自UI设置
  341. if (m_videoState)
  342. return false;
  343. m_videoState = std::make_unique<VideoStateData>(useHardware, loop);
  344. const int ret = m_videoState->create_video_state(file.toUtf8().constData());
  345. if (ret < 0) {
  346. m_videoState.reset();
  347. qWarning("Video state creation failed (error: %d)", ret);
  348. return false;
  349. }
  350. return true;
  351. }
  352. void PlayerController::deleteVideoState()
  353. {
  354. m_videoState.reset();
  355. }
  356. bool PlayerController::createReadThread()
  357. {
  358. if (m_packetReadThread)
  359. return false;
  360. m_packetReadThread = std::make_unique<ReadThread>(this);
  361. connect(m_packetReadThread.get(),
  362. &ReadThread::finished,
  363. this,
  364. &PlayerController::readPacketStopped);
  365. return true;
  366. }
  367. bool PlayerController::createDecodeVideoThread()
  368. {
  369. if (!m_videoState || m_decodeVideoThread)
  370. return false;
  371. auto state = m_videoState->get_state();
  372. if (!state)
  373. return false;
  374. m_decodeVideoThread = std::make_unique<VideoDecodeThread>(this, state);
  375. connect(m_decodeVideoThread.get(),
  376. &VideoDecodeThread::finished,
  377. this,
  378. &PlayerController::decodeVideoStopped);
  379. auto codecContext = m_videoState->get_contex(AVMEDIA_TYPE_VIDEO);
  380. // 初始化视频解码器
  381. int ret = decoder_init(&state->viddec,
  382. codecContext,
  383. &state->videoq,
  384. state->continue_read_thread);
  385. if (ret < 0) {
  386. qWarning("Video decoder initialization failed (error: %d)", ret);
  387. return false;
  388. }
  389. ret = decoder_start(&state->viddec, m_decodeVideoThread.get(), "video_decoder");
  390. if (ret < 0) {
  391. qWarning("Video decoder start failed (error: %d)", ret);
  392. return false;
  393. }
  394. state->queue_attachments_req = 1;
  395. return true;
  396. }
  397. bool PlayerController::createDecodeAudioThread()
  398. {
  399. if (!m_videoState || m_decodeAudioThread)
  400. return false;
  401. auto state = m_videoState->get_state();
  402. if (!state)
  403. return false;
  404. m_decodeAudioThread = std::make_unique<AudioDecodeThread>(this, state);
  405. connect(m_decodeAudioThread.get(),
  406. &AudioDecodeThread::finished,
  407. this,
  408. &PlayerController::decodeAudioStopped);
  409. auto codecContext = m_videoState->get_contex(AVMEDIA_TYPE_AUDIO);
  410. // 初始化音频解码器
  411. int ret = decoder_init(&state->auddec,
  412. codecContext,
  413. &state->audioq,
  414. state->continue_read_thread);
  415. if (ret < 0) {
  416. qWarning("Audio decoder initialization failed (error: %d)", ret);
  417. return false;
  418. }
  419. ret = decoder_start(&state->auddec, m_decodeAudioThread.get(), "audio_decoder");
  420. if (ret < 0) {
  421. qWarning("Audio decoder start failed (error: %d)", ret);
  422. return false;
  423. }
  424. return true;
  425. }
  426. bool PlayerController::createDecodeSubtitleThread()
  427. {
  428. if (!m_videoState || m_decodeSubtitleThread)
  429. return false;
  430. auto state = m_videoState->get_state();
  431. if (!state)
  432. return false;
  433. m_decodeSubtitleThread = std::make_unique<SubtitleDecodeThread>(this, state);
  434. connect(m_decodeSubtitleThread.get(),
  435. &SubtitleDecodeThread::finished,
  436. this,
  437. &PlayerController::decodeSubtitleStopped);
  438. auto codecContext = m_videoState->get_contex(AVMEDIA_TYPE_SUBTITLE);
  439. // 初始化字幕解码器
  440. int ret = decoder_init(&state->subdec,
  441. codecContext,
  442. &state->subtitleq,
  443. state->continue_read_thread);
  444. if (ret < 0) {
  445. qWarning("Subtitle decoder initialization failed (error: %d)", ret);
  446. return false;
  447. }
  448. ret = decoder_start(&state->subdec, m_decodeSubtitleThread.get(), "subtitle_decoder");
  449. if (ret < 0) {
  450. qWarning("Subtitle decoder start failed (error: %d)", ret);
  451. return false;
  452. }
  453. return true;
  454. }
  455. bool PlayerController::createVideoPlayThread()
  456. {
  457. if (!m_videoState || m_videoPlayThread)
  458. return false;
  459. auto state = m_videoState->get_state();
  460. if (!state)
  461. return false;
  462. m_videoPlayThread = std::make_unique<VideoPlayThread>(this, state);
  463. // 连接信号
  464. connect(m_videoPlayThread.get(),
  465. &VideoPlayThread::finished,
  466. this,
  467. &PlayerController::videoPlayStopped);
  468. connect(m_videoPlayThread.get(),
  469. &VideoPlayThread::frameReady,
  470. this,
  471. &PlayerController::frameReady);
  472. connect(m_videoPlayThread.get(),
  473. &VideoPlayThread::subtitle_ready,
  474. this,
  475. &PlayerController::subtitleReady);
  476. connect(this,
  477. &PlayerController::stopVideoPlayThread,
  478. m_videoPlayThread.get(),
  479. &VideoPlayThread::stop_thread);
  480. // 初始化参数
  481. auto videoContext = m_videoState->get_contex(AVMEDIA_TYPE_VIDEO);
  482. const bool useHardware = m_videoState->is_hardware_decode();
  483. if (!m_videoPlayThread->init_resample_param(videoContext, useHardware)) {
  484. qWarning("Video resample parameters initialization failed");
  485. return false;
  486. }
  487. return true;
  488. }
  489. bool PlayerController::createAudioPlayThread()
  490. {
  491. if (!m_videoState || m_audioPlayThread)
  492. return false;
  493. auto state = m_videoState->get_state();
  494. if (!state)
  495. return false;
  496. m_audioPlayThread = std::make_unique<AudioPlayThread>(this, state);
  497. // 连接信号
  498. connect(m_audioPlayThread.get(),
  499. &AudioPlayThread::finished,
  500. this,
  501. &PlayerController::audioPlayStopped);
  502. connect(this,
  503. &PlayerController::stopAudioPlayThread,
  504. m_audioPlayThread.get(),
  505. &AudioPlayThread::stop_thread);
  506. connect(m_audioPlayThread.get(),
  507. &AudioPlayThread::update_play_time,
  508. this,
  509. &PlayerController::updatePlayTime);
  510. connect(m_audioPlayThread.get(),
  511. &AudioPlayThread::data_visual_ready,
  512. this,
  513. &PlayerController::audioData);
  514. // 音频设备初始化在独立线程中完成
  515. return true;
  516. }
  517. bool PlayerController::startPlayThread()
  518. {
  519. if (m_beforePlayThread)
  520. return false;
  521. m_beforePlayThread = std::make_unique<StartPlayThread>(this, this);
  522. connect(m_beforePlayThread.get(),
  523. &StartPlayThread::audio_device_init,
  524. this,
  525. &PlayerController::playStarted);
  526. m_beforePlayThread->start();
  527. qDebug("++++++++++ StartPlay thread (audio init) started");
  528. return true;
  529. }
  530. // 调试辅助函数
  531. void PlayerController::printDecodeContext(const AVCodecContext* codecCtx, bool isVideo) const
  532. {
  533. if (!codecCtx)
  534. return;
  535. qInfo("%s codec: %s", isVideo ? "Video" : "Audio", codecCtx->codec->name);
  536. qInfo(" Type: %d, ID: %d, Tag: %d",
  537. codecCtx->codec_type,
  538. codecCtx->codec_id,
  539. codecCtx->codec_tag);
  540. if (isVideo) {
  541. qInfo(" Dimensions: %dx%d", codecCtx->width, codecCtx->height);
  542. } else {
  543. qInfo(" Sample rate: %d Hz, Channels: %d, Format: %d",
  544. codecCtx->sample_rate,
  545. codecCtx->ch_layout.nb_channels,
  546. codecCtx->sample_fmt);
  547. qInfo(" Frame size: %d, Block align: %d", codecCtx->frame_size, codecCtx->block_align);
  548. }
  549. }