playercontroller.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #pragma once
  2. #include <QElapsedTimer>
  3. #include <QMutex>
  4. #include <QObject>
  5. #include <QThread>
  6. #include <memory>
  7. #include <thread>
  8. #include <atomic>
  9. #include <condition_variable>
  10. #include <functional>
  11. #include "AVPlayer2/audio_play_thread.h"
  12. class ReadThread;
  13. class VideoDecodeThread;
  14. class AudioDecodeThread;
  15. class SubtitleDecodeThread;
  16. class AudioPlayThread;
  17. class VideoPlayThread;
  18. class VideoStateData;
  19. class StartPlayThread;
  20. class StopWaitingThread;
  21. struct VideoState;
  22. class OpenGLVideoWidget;
  23. class PlayerController : public QObject
  24. {
  25. Q_OBJECT
  26. public:
  27. enum class PlayerState {
  28. Idle,
  29. Initializing,
  30. Playing,
  31. Stopping
  32. };
  33. explicit PlayerController(QWidget* parent = nullptr);
  34. ~PlayerController();
  35. // 播放控制接口
  36. void startToPlay(const QString& file);
  37. void stopPlay();
  38. void pausePlay();
  39. void playMute(bool mute);
  40. void playStartSeek();
  41. void playSeekPre();
  42. void playSeekNext();
  43. void setVolume(int volume, int maxValue = 100);
  44. void setPlaySpeed(double speed);
  45. bool isPlaying() const;
  46. QString playingFile() const;
  47. bool playingHasVideo();
  48. bool playingHasAudio();
  49. bool playingHasSubtitle();
  50. // 状态访问接口
  51. VideoState* state();
  52. float deviceVolume() const;
  53. void setDeviceVolume(float volume);
  54. // 线程访问接口
  55. AudioPlayThread* audioPlayThread() const { return m_audioPlayThread.get(); }
  56. VideoPlayThread* videoPlayThread() const { return m_videoPlayThread.get(); }
  57. VideoStateData* videoStateData() const { return m_videoState.get(); }
  58. public slots:
  59. // 线程生命周期管理
  60. void readPacketStopped();
  61. void decodeVideoStopped();
  62. void decodeAudioStopped();
  63. void decodeSubtitleStopped();
  64. void audioPlayStopped();
  65. void videoPlayStopped();
  66. // 播放状态回调
  67. void playStarted(bool success = true);
  68. void playFailed(const QString& file);
  69. // 线程管理
  70. void setThreads();
  71. void startSendData(bool send = true);
  72. void videoSeek(double pos = 0, double incr = 0);
  73. signals:
  74. // 线程控制信号
  75. void startToPlaySignal();
  76. void stopAudioPlayThread();
  77. void stopVideoPlayThread();
  78. void stopDecodeThread();
  79. void stopReadPacketThread();
  80. void waitStopAudioPlayThread();
  81. void waitStopVideoPlayThread();
  82. void audioStopped(); // 音频结束
  83. void videoStopped(); // 视频结束
  84. void playbackFinished(); // 新增:播放自然结束
  85. // 多媒体数据处理
  86. void frameReady(AVFrame*);
  87. void audioData(const AudioData& data);
  88. void subtitleReady(const QString& text);
  89. // UI更新信号
  90. void setPlayControlWnd(bool set);
  91. void updatePlayControlVolume();
  92. void updatePlayControlStatus();
  93. void updatePlayTime();
  94. void playSeek();
  95. void showMessage(const QString& message, const QString& windowTitle, const QString& styleSheet);
  96. void requestFullscreen(bool fullscreen);
  97. void requestHideStatusBar(bool hide);
  98. void asyncInitFinished(const QString& file, bool success);
  99. private slots:
  100. void onAsyncInitFinished(const QString& file, bool success);
  101. private:
  102. // 核心播放逻辑
  103. bool startPlay();
  104. void waitStopPlay(const QString& file);
  105. void allThreadStart();
  106. // 线程创建方法
  107. bool createVideoState(const QString& file);
  108. void deleteVideoState();
  109. bool createReadThread();
  110. bool createDecodeVideoThread();
  111. bool createDecodeAudioThread();
  112. bool createDecodeSubtitleThread();
  113. bool createVideoPlayThread();
  114. bool createAudioPlayThread();
  115. bool startPlayThread(); // 避免UI冻结的线程
  116. // 播放辅助方法
  117. void videoSeekInc(double incr);
  118. void printDecodeContext(const AVCodecContext* videoCtx, bool isVideo = true) const;
  119. void displayStatusMessage(const QString& message);
  120. QSize displayVideoSize(AVCodecContext* videoCtx) const;
  121. void setVolumeUpdown(bool increase = true, float unit = 0.05f);
  122. QString strippedName(const QString& fullPath) const;
  123. void hideCursor(bool hide = true);
  124. bool cursorInWindow(QWidget* widget);
  125. // 异步初始化相关
  126. void asyncInit(const QString& file);
  127. std::thread m_initThread;
  128. std::atomic<bool> m_initInProgress{false};
  129. std::atomic<bool> m_initSuccess{false};
  130. std::mutex m_stopMutex;
  131. // 状态机
  132. std::atomic<PlayerState> m_state{PlayerState::Idle};
  133. private:
  134. // 多媒体线程
  135. std::unique_ptr<ReadThread> m_packetReadThread;
  136. std::unique_ptr<VideoDecodeThread> m_decodeVideoThread;
  137. std::unique_ptr<AudioDecodeThread> m_decodeAudioThread;
  138. std::unique_ptr<SubtitleDecodeThread> m_decodeSubtitleThread;
  139. std::unique_ptr<AudioPlayThread> m_audioPlayThread;
  140. std::unique_ptr<VideoPlayThread> m_videoPlayThread;
  141. // 状态管理
  142. std::unique_ptr<VideoStateData> m_videoState;
  143. std::unique_ptr<StartPlayThread> m_beforePlayThread;
  144. std::unique_ptr<StopWaitingThread> m_stopPlayWaitingThread;
  145. QString m_currentFile; // 更清晰的命名
  146. void checkAndResetState();
  147. };