playercontroller.h 4.8 KB

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