start_play_thread.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // ***********************************************************/
  2. // start_play_thread.cpp
  3. //
  4. // Copy Right @ Steven Huang. All rights reserved.
  5. //
  6. // start play thread. Do the audio device initialization work
  7. // which is time-consuming to avoid GUI freezing.
  8. // ***********************************************************/
  9. #include "start_play_thread.h"
  10. #include "AVPlayer2/video_state.h"
  11. #include "playercontroller.h"
  12. #include <QLoggingCategory>
  13. Q_LOGGING_CATEGORY(playerControllerStartPlayThread, "player.controller.StartPlayThread")
  14. StartPlayThread::StartPlayThread(PlayerController* playerController)
  15. : m_playerController(playerController)
  16. {}
  17. StartPlayThread::~StartPlayThread() {}
  18. void StartPlayThread::stop()
  19. {
  20. *m_exit = true;
  21. m_cv.notify_all();
  22. }
  23. void StartPlayThread::run()
  24. {
  25. assert(m_playerController);
  26. if (m_exit && *m_exit) return;
  27. bool ret = false;
  28. float vol = 1.0;
  29. AVSampleFormat sample_fmt = AV_SAMPLE_FMT_S16; // play out format
  30. VideoStateData* pVideoStateData = m_playerController->videoStateData();
  31. if (pVideoStateData) {
  32. AVCodecContext* pAudio = pVideoStateData->get_contex(AVMEDIA_TYPE_AUDIO);
  33. VideoState* pState = pVideoStateData->get_state();
  34. if (pAudio) {
  35. auto* pThread = m_playerController->audioPlayThread();
  36. if (pThread) {
  37. ret = pThread->init_device(pAudio->sample_rate,
  38. pAudio->ch_layout.nb_channels,
  39. sample_fmt,
  40. vol); // pAudio->sample_fmt
  41. if (ret) {
  42. ret = pThread->init_resample_param(pAudio, sample_fmt, pState);
  43. }
  44. }
  45. }
  46. }
  47. // 可加回调或日志
  48. qCDebug(playerControllerStartPlayThread) << "[StartPlayThread] run finished, ret=" << ret;
  49. }