playercontroller.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // 多媒体数据处理
  80. void frameReady(AVFrame*);
  81. void audioData(const AudioData& data);
  82. void subtitleReady(const QString& text);
  83. // UI更新信号
  84. void setPlayControlWnd(bool set);
  85. void updatePlayControlVolume();
  86. void updatePlayControlStatus();
  87. void updatePlayTime();
  88. void playSeek();
  89. void showMessage(const QString& message, const QString& windowTitle, const QString& styleSheet);
  90. void requestFullscreen(bool fullscreen);
  91. void requestHideStatusBar(bool hide);
  92. private:
  93. // 核心播放逻辑
  94. bool startPlay();
  95. void waitStopPlay(const QString& file);
  96. void allThreadStart();
  97. // 线程创建方法
  98. bool createVideoState(const QString& file);
  99. void deleteVideoState();
  100. bool createReadThread();
  101. bool createDecodeVideoThread();
  102. bool createDecodeAudioThread();
  103. bool createDecodeSubtitleThread();
  104. bool createVideoPlayThread();
  105. bool createAudioPlayThread();
  106. bool startPlayThread(); // 避免UI冻结的线程
  107. // 播放辅助方法
  108. void videoSeekInc(double incr);
  109. void printDecodeContext(const AVCodecContext* videoCtx, bool isVideo = true) const;
  110. void displayStatusMessage(const QString& message);
  111. QSize displayVideoSize(AVCodecContext* videoCtx) const;
  112. void setVolumeUpdown(bool increase = true, float unit = 0.05f);
  113. QString strippedName(const QString& fullPath) const;
  114. void hideCursor(bool hide = true);
  115. bool cursorInWindow(QWidget* widget);
  116. private:
  117. // 多媒体线程
  118. std::unique_ptr<ReadThread> m_packetReadThread;
  119. std::unique_ptr<VideoDecodeThread> m_decodeVideoThread;
  120. std::unique_ptr<AudioDecodeThread> m_decodeAudioThread;
  121. std::unique_ptr<SubtitleDecodeThread> m_decodeSubtitleThread;
  122. std::unique_ptr<AudioPlayThread> m_audioPlayThread;
  123. std::unique_ptr<VideoPlayThread> m_videoPlayThread;
  124. // 状态管理
  125. std::unique_ptr<VideoStateData> m_videoState;
  126. std::unique_ptr<StartPlayThread> m_beforePlayThread;
  127. std::unique_ptr<StopWaitingThread> m_stopPlayWaitingThread;
  128. QString m_currentFile; // 更清晰的命名
  129. };