playercontroller.cpp 17 KB

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