playercontroller.h 4.4 KB

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