playercontroller.h 5.2 KB

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