playercontroller.h 5.2 KB

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