playercontroller.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #ifndef AVPLAYER2_PLAYERCONTROLLER_H
  2. #define AVPLAYER2_PLAYERCONTROLLER_H
  3. #pragma once
  4. #include <QElapsedTimer>
  5. #include <QMutex>
  6. #include <QObject>
  7. #include <QThread>
  8. #include <atomic>
  9. #include <condition_variable>
  10. #include <functional>
  11. #include <memory>
  12. #include <thread>
  13. #include <unordered_map>
  14. #include <QLoggingCategory>
  15. #include "AVPlayer2/audio_play_thread.h"
  16. Q_DECLARE_LOGGING_CATEGORY(playerControllerLog)
  17. class ReadThread;
  18. class VideoDecodeThread;
  19. class AudioDecodeThread;
  20. class SubtitleDecodeThread;
  21. class AudioPlayThread;
  22. class VideoPlayThread;
  23. class VideoStateData;
  24. class StartPlayThread;
  25. class StopWaitingThread;
  26. struct VideoState;
  27. class OpenGLVideoWidget;
  28. class PlayerController : public QObject
  29. {
  30. Q_OBJECT
  31. public:
  32. enum class PlayerState { Idle, Initializing, Playing, Stopping };
  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. public slots:
  55. // 线程生命周期管理
  56. void readPacketStopped();
  57. void decodeVideoStopped();
  58. void decodeAudioStopped();
  59. void decodeSubtitleStopped();
  60. void audioPlayStopped();
  61. void videoPlayStopped();
  62. // 播放状态回调
  63. void playStarted(bool success = true);
  64. void playFailed(const QString& file);
  65. // 线程管理
  66. void setThreads();
  67. void startSendData(bool send = true);
  68. void videoSeek(double pos = 0, double incr = 0);
  69. signals:
  70. // 线程控制信号
  71. void startToPlaySignal();
  72. void stopAudioPlayThread();
  73. void stopVideoPlayThread();
  74. void stopDecodeThread();
  75. void stopReadPacketThread();
  76. void waitStopAudioPlayThread();
  77. void waitStopVideoPlayThread();
  78. void audioStopped(); // 音频结束
  79. void videoStopped(); // 视频结束
  80. void playbackFinished(); // 新增:播放自然结束
  81. // 多媒体数据处理
  82. void frameReady(AVFrame*);
  83. void audioData(const AudioData& data);
  84. void subtitleReady(const QString& text);
  85. // UI更新信号
  86. void setPlayControlWnd(bool set);
  87. void updatePlayControlVolume();
  88. void updatePlayControlStatus();
  89. void updatePlayTime();
  90. void playSeek();
  91. void showMessage(const QString& message, const QString& windowTitle, const QString& styleSheet);
  92. void requestFullscreen(bool fullscreen);
  93. void requestHideStatusBar(bool hide);
  94. void asyncInitFinished(const QString& file, bool success);
  95. protected slots:
  96. void onFrameReady(AVFrame* frame);
  97. private slots:
  98. void onAsyncInitFinished(const QString& file, bool success);
  99. private:
  100. enum class ThreadType {
  101. Video,
  102. Audio,
  103. Read,
  104. DecodeVideo,
  105. DecodeAudio,
  106. DecodeSubtitle,
  107. BeforePlay,
  108. StopWaiting
  109. };
  110. std::unordered_map<ThreadType, std::mutex> m_threadMutexes;
  111. // 核心播放逻辑
  112. bool startPlay();
  113. bool waitStopPlay(const QString& file);
  114. void allThreadStart();
  115. // 线程创建方法
  116. bool createVideoState(const QString& file);
  117. void deleteVideoState();
  118. bool createReadThread();
  119. bool createDecodeVideoThread();
  120. bool createDecodeAudioThread();
  121. bool createDecodeSubtitleThread();
  122. bool createVideoPlayThread();
  123. bool createAudioPlayThread();
  124. bool startPlayThread(); // 避免UI冻结的线程
  125. // 播放辅助方法
  126. void videoSeekInc(double incr);
  127. void printDecodeContext(const AVCodecContext* videoCtx, bool isVideo = true) const;
  128. void displayStatusMessage(const QString& message);
  129. QSize displayVideoSize(AVCodecContext* videoCtx) const;
  130. void setVolumeUpdown(bool increase = true, float unit = 0.05f);
  131. QString strippedName(const QString& fullPath) const;
  132. void hideCursor(bool hide = true);
  133. bool cursorInWindow(QWidget* widget);
  134. // 异步初始化相关
  135. void asyncInit(const QString& file);
  136. std::thread m_initThread;
  137. std::atomic<bool> m_initInProgress{false};
  138. std::atomic<bool> m_initSuccess{false};
  139. std::mutex m_stopMutex;
  140. // 状态机
  141. std::atomic<PlayerState> m_state{PlayerState::Idle};
  142. private:
  143. // 多媒体线程
  144. std::unique_ptr<ReadThread> m_packetReadThread;
  145. std::unique_ptr<VideoDecodeThread> m_decodeVideoThread;
  146. std::unique_ptr<AudioDecodeThread> m_decodeAudioThread;
  147. std::unique_ptr<SubtitleDecodeThread> m_decodeSubtitleThread;
  148. std::unique_ptr<AudioPlayThread> m_audioPlayThread;
  149. std::unique_ptr<VideoPlayThread> m_videoPlayThread;
  150. // 状态管理
  151. std::unique_ptr<VideoStateData> m_videoState;
  152. std::unique_ptr<StartPlayThread> m_beforePlayThread;
  153. std::unique_ptr<StopWaitingThread> m_stopPlayWaitingThread;
  154. std::thread m_eventThread;
  155. QString m_currentFile; // 更清晰的命名
  156. void checkAndResetState();
  157. };
  158. #endif // AVPLAYER2_PLAYERCONTROLLER_H