player_core.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #ifndef AV_PLAYER_CORE_H
  2. #define AV_PLAYER_CORE_H
  3. #pragma once
  4. #include "../base/logger.h"
  5. #include "../base/types.h"
  6. #include "../codec/codec_audio_decoder.h"
  7. #include "../codec/codec_video_decoder.h"
  8. #include "../utils/utils_packet_queue.h"
  9. #include "../utils/utils_frame_queue.h"
  10. #include "../utils/utils_synchronizer.h"
  11. #include "thread_manager.h"
  12. #include "audio_output.h"
  13. #include "opengl_video_renderer.h"
  14. #include <memory>
  15. #include <atomic>
  16. #include <mutex>
  17. #include <string>
  18. #include <functional>
  19. #include <chrono>
  20. extern "C" {
  21. #include <libavformat/avformat.h>
  22. #include <libavcodec/avcodec.h>
  23. #include <libavutil/avutil.h>
  24. }
  25. namespace av {
  26. namespace player {
  27. using VideoDecoder = av::codec::VideoDecoder;
  28. using AudioDecoder = av::codec::AudioDecoder;
  29. using Synchronizer = av::utils::Synchronizer;
  30. /**
  31. * 播放器状态枚举
  32. */
  33. enum class PlayerState {
  34. Idle, // 空闲
  35. Opening, // 正在打开文件
  36. Playing, // 播放中
  37. Paused, // 暂停
  38. Seeking, // 跳转中
  39. Stopped, // 已停止
  40. Error // 错误状态
  41. };
  42. /**
  43. * 媒体信息结构
  44. */
  45. struct MediaInfo {
  46. std::string filename;
  47. int64_t duration = 0; // 总时长(微秒)
  48. int width = 0;
  49. int height = 0;
  50. double fps = 0.0;
  51. int audioSampleRate = 0;
  52. int audioChannels = 0;
  53. bool hasVideo = false;
  54. bool hasAudio = false;
  55. bool hasSubtitle = false;
  56. // 流索引
  57. int videoStreamIndex = -1;
  58. int audioStreamIndex = -1;
  59. int subtitleStreamIndex = -1;
  60. };
  61. /**
  62. * 播放统计信息
  63. */
  64. struct PlaybackStats {
  65. int64_t currentTime = 0; // 当前播放时间(微秒)
  66. int64_t totalFrames = 0; // 总帧数
  67. int64_t droppedFrames = 0; // 丢帧数
  68. double playbackSpeed = 1.0; // 播放速度
  69. int queuedVideoFrames = 0; // 视频帧队列大小
  70. int queuedAudioFrames = 0; // 音频帧队列大小
  71. int queuedPackets = 0; // 数据包队列大小
  72. };
  73. /**
  74. * 播放器事件回调接口
  75. */
  76. class PlayerEventCallback {
  77. public:
  78. virtual ~PlayerEventCallback() = default;
  79. virtual void onStateChanged(PlayerState newState) = 0;
  80. virtual void onMediaInfoChanged(const MediaInfo& info) = 0;
  81. virtual void onPositionChanged(int64_t position) = 0; // 微秒
  82. virtual void onErrorOccurred(const std::string& error) = 0;
  83. virtual void onEndOfFile() = 0;
  84. };
  85. /**
  86. * 播放器核心类 - 使用新的AV核心库
  87. * 负责媒体文件的解码和播放控制,但不涉及UI渲染
  88. * 这是一个纯C++实现,不依赖Qt
  89. */
  90. class PlayerCore
  91. {
  92. public:
  93. explicit PlayerCore();
  94. ~PlayerCore();
  95. // 事件回调设置
  96. void setEventCallback(PlayerEventCallback* callback);
  97. // 播放控制接口
  98. ErrorCode openFile(const std::string& filename);
  99. ErrorCode play();
  100. ErrorCode pause();
  101. ErrorCode stop();
  102. ErrorCode seek(int64_t timestamp); // 微秒
  103. ErrorCode setPlaybackSpeed(double speed);
  104. // 状态查询
  105. PlayerState getState() const { return m_state.load(); }
  106. MediaInfo getMediaInfo() const;
  107. PlaybackStats getStats() const;
  108. int64_t getCurrentTime() const;
  109. double getPlaybackSpeed() const;
  110. // 音量控制
  111. void setVolume(double volume); // 0.0 - 1.0
  112. double getVolume() const { return m_volume; }
  113. // OpenGL渲染器设置
  114. void setOpenGLVideoRenderer(OpenGLVideoRenderer* renderer);
  115. OpenGLVideoRenderer* getOpenGLVideoRenderer() const { return m_openGLVideoRenderer; }
  116. // 帧获取接口(供UI渲染使用)
  117. AVFrame* getNextVideoFrame(); // 获取下一个视频帧
  118. AVFrame* getNextAudioFrame(); // 获取下一个音频帧
  119. void releaseVideoFrame(AVFrame* frame); // 释放视频帧
  120. void releaseAudioFrame(AVFrame* frame); // 释放音频帧
  121. // 线程安全的更新接口
  122. void update(); // 定期调用以更新播放状态
  123. private:
  124. // 初始化和清理
  125. bool initializeFFmpeg();
  126. void cleanup();
  127. bool openMediaFile(const std::string& filename);
  128. void closeMediaFile();
  129. // 线程管理
  130. bool startReadThread();
  131. bool startDecodeThreads();
  132. void stopAllThreads();
  133. // 解码器管理
  134. bool setupVideoDecoder();
  135. bool setupAudioDecoder();
  136. void resetDecoders();
  137. // 同步控制
  138. void updateSynchronization();
  139. int64_t getCurrentPlayTime();
  140. // 状态管理
  141. void setState(PlayerState newState);
  142. void updateStats();
  143. void notifyStateChanged(PlayerState newState);
  144. void notifyError(const std::string& error);
  145. // 线程函数
  146. void readThreadFunc();
  147. private:
  148. // 状态变量
  149. std::atomic<PlayerState> m_state{PlayerState::Idle};
  150. mutable std::mutex m_mutex;
  151. // 事件回调
  152. PlayerEventCallback* m_eventCallback = nullptr;
  153. // 媒体信息
  154. MediaInfo m_mediaInfo;
  155. PlaybackStats m_stats;
  156. // FFmpeg相关
  157. AVFormatContext* m_formatContext = nullptr;
  158. // 解码器
  159. std::unique_ptr<VideoDecoder> m_videoDecoder;
  160. std::unique_ptr<AudioDecoder> m_audioDecoder;
  161. // 队列管理
  162. std::unique_ptr<av::utils::PacketQueue> m_packetQueue;
  163. std::unique_ptr<av::utils::FrameQueue> m_videoFrameQueue;
  164. std::unique_ptr<av::utils::FrameQueue> m_audioFrameQueue;
  165. // 同步器
  166. std::unique_ptr<Synchronizer> m_synchronizer;
  167. // 线程管理器
  168. std::unique_ptr<ThreadManager> m_threadManager;
  169. // 输出设备
  170. std::unique_ptr<AudioOutput> m_audioOutput;
  171. OpenGLVideoRenderer* m_openGLVideoRenderer = nullptr; // OpenGL视频渲染器
  172. // 播放控制
  173. double m_volume = 1.0;
  174. double m_playbackSpeed = 1.0;
  175. int64_t m_seekTarget = -1;
  176. std::atomic<bool> m_seeking{false};
  177. // 性能统计
  178. int64_t m_lastUpdateTime = 0;
  179. int64_t m_frameCount = 0;
  180. // 播放时间计算
  181. std::chrono::steady_clock::time_point m_playStartTime;
  182. int64_t m_baseTime = 0; // 基准时间(微秒)
  183. // 初始化标志
  184. bool m_initialized = false;
  185. };
  186. } // namespace player
  187. } // namespace av
  188. #endif // AV_PLAYER_CORE_H