player_core_v2.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #ifndef AV_PLAYER_CORE_V2_H
  2. #define AV_PLAYER_CORE_V2_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_v2.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. #include <thread>
  21. #include <condition_variable>
  22. extern "C" {
  23. #include <libavformat/avformat.h>
  24. #include <libavcodec/avcodec.h>
  25. #include <libavutil/avutil.h>
  26. #include <libavutil/time.h>
  27. }
  28. namespace av {
  29. namespace player {
  30. using VideoDecoder = av::codec::VideoDecoder;
  31. using AudioDecoder = av::codec::AudioDecoder;
  32. using VideoDecoderParams = av::codec::VideoDecoderParams;
  33. using AudioDecoderParams = av::codec::AudioDecoderParams;
  34. using SynchronizerV2 = av::utils::SynchronizerV2;
  35. using SyncConfigV2 = av::utils::SyncConfigV2;
  36. using FrameDecision = av::utils::FrameDecision;
  37. using FrameAction = av::utils::FrameAction;
  38. /**
  39. * 播放器状态枚举
  40. */
  41. enum class PlayerState {
  42. Idle, // 空闲
  43. Opening, // 正在打开文件
  44. Playing, // 播放中
  45. Paused, // 暂停
  46. Seeking, // 跳转中
  47. Stopped, // 已停止
  48. Error // 错误状态
  49. };
  50. /**
  51. * 媒体信息结构
  52. */
  53. struct MediaInfo {
  54. std::string filename;
  55. int64_t duration = 0; // 总时长(微秒)
  56. int width = 0;
  57. int height = 0;
  58. double fps = 0.0;
  59. int audioSampleRate = 0;
  60. int audioChannels = 0;
  61. bool hasVideo = false;
  62. bool hasAudio = false;
  63. bool hasSubtitle = false;
  64. // 流索引
  65. int videoStreamIndex = -1;
  66. int audioStreamIndex = -1;
  67. int subtitleStreamIndex = -1;
  68. // 时间基
  69. AVRational videoTimeBase;
  70. AVRational audioTimeBase;
  71. // 兼容性字段 (与编译错误中的字段名匹配)
  72. int sampleRate = 0; // 音频采样率 (与audioSampleRate相同)
  73. int channels = 0; // 音频通道数 (与audioChannels相同)
  74. double bitrate = 0.0; // 比特率
  75. };
  76. /**
  77. * 播放统计信息
  78. */
  79. struct PlaybackStats {
  80. int64_t currentTime = 0; // 当前播放时间(微秒)
  81. int64_t totalFrames = 0; // 总帧数
  82. int64_t droppedFrames = 0; // 丢帧数
  83. int64_t duplicatedFrames = 0; // 重复帧数
  84. double playbackSpeed = 1.0; // 播放速度
  85. int queuedVideoFrames = 0; // 视频帧队列大小
  86. int queuedAudioFrames = 0; // 音频帧队列大小
  87. int queuedPackets = 0; // 数据包队列大小
  88. // 同步统计
  89. double syncError = 0.0; // 当前同步误差
  90. double avgSyncError = 0.0; // 平均同步误差
  91. double maxSyncError = 0.0; // 最大同步误差
  92. // 性能统计
  93. double cpuUsage = 0.0;
  94. double memoryUsage = 0.0;
  95. int64_t bytesRead = 0;
  96. double bitrate = 0.0;
  97. };
  98. /**
  99. * 播放器事件回调接口
  100. */
  101. class PlayerEventCallback {
  102. public:
  103. virtual ~PlayerEventCallback() = default;
  104. virtual void onStateChanged(PlayerState newState) = 0;
  105. virtual void onMediaInfoChanged(const MediaInfo& info) = 0;
  106. virtual void onPositionChanged(int64_t position) = 0; // 微秒
  107. virtual void onErrorOccurred(const std::string& error) = 0;
  108. virtual void onEndOfFile() = 0;
  109. virtual void onSyncError(double error, const std::string& reason) = 0;
  110. virtual void onFrameDropped(int64_t count) = 0;
  111. };
  112. /**
  113. * 改进的播放器核心类 - 基于AVPlayer2的经验重新设计
  114. *
  115. * 主要改进:
  116. * 1. 使用新的SynchronizerV2进行精确时间同步
  117. * 2. 改进的线程管理和错误处理
  118. * 3. 更好的seek操作和状态管理
  119. * 4. 详细的性能监控和统计
  120. * 5. 自适应播放策略
  121. */
  122. class PlayerCoreV2
  123. {
  124. public:
  125. explicit PlayerCoreV2(const SyncConfigV2& syncConfig = SyncConfigV2());
  126. ~PlayerCoreV2();
  127. // 事件回调设置
  128. void setEventCallback(PlayerEventCallback* callback);
  129. // 播放控制接口
  130. ErrorCode openFile(const std::string& filename);
  131. ErrorCode play();
  132. ErrorCode pause();
  133. ErrorCode stop();
  134. ErrorCode seek(int64_t timestamp); // 微秒
  135. ErrorCode setPlaybackSpeed(double speed);
  136. // 状态查询
  137. PlayerState getState() const { return m_state.load(); }
  138. MediaInfo getMediaInfo() const;
  139. PlaybackStats getStats() const;
  140. int64_t getCurrentTime() const;
  141. double getPlaybackSpeed() const;
  142. // 音量控制
  143. void setVolume(double volume); // 0.0 - 1.0
  144. double getVolume() const { return m_volume; }
  145. // 同步控制
  146. void setSyncConfig(const SyncConfigV2& config);
  147. SyncConfigV2 getSyncConfig() const;
  148. // OpenGL渲染器设置
  149. void setOpenGLVideoRenderer(OpenGLVideoRenderer* renderer);
  150. OpenGLVideoRenderer* getOpenGLVideoRenderer() const { return m_openGLVideoRenderer; }
  151. // 帧获取接口(供UI渲染使用)
  152. AVFrame* getNextVideoFrame(); // 获取下一个视频帧
  153. AVFrame* getNextAudioFrame(); // 获取下一个音频帧
  154. void releaseVideoFrame(AVFrame* frame); // 释放视频帧
  155. void releaseAudioFrame(AVFrame* frame); // 释放音频帧
  156. // 线程安全的更新接口
  157. void update(); // 定期调用以更新播放状态
  158. // 调试接口
  159. std::string getDebugInfo() const;
  160. void dumpStats() const;
  161. private:
  162. // 初始化和清理
  163. bool initializeFFmpeg();
  164. void cleanup();
  165. bool openMediaFile(const std::string& filename);
  166. void closeMediaFile();
  167. // 线程管理
  168. bool startReadThread();
  169. bool startDecodeThreads();
  170. bool startVideoPlayThread();
  171. bool startAudioPlayThread();
  172. void stopAllThreads();
  173. // 解码器管理
  174. bool setupVideoDecoder();
  175. bool setupAudioDecoder();
  176. void resetDecoders();
  177. // 同步控制
  178. void updateSynchronization();
  179. int64_t getCurrentPlayTime();
  180. void handleSyncError(double error, const std::string& reason);
  181. // 状态管理
  182. void setState(PlayerState newState);
  183. void updateStats();
  184. void notifyStateChanged(PlayerState newState);
  185. void notifyError(const std::string& error);
  186. void notifyPositionChanged();
  187. // 线程函数
  188. void readThreadFunc();
  189. void videoDecodeThreadFunc();
  190. void audioDecodeThreadFunc();
  191. void videoPlayThreadFunc();
  192. void audioPlayThreadFunc();
  193. // 帧处理
  194. bool processVideoFrame();
  195. bool processAudioFrame();
  196. void handleVideoFrameDecision(const FrameDecision& decision, AVFrame* frame);
  197. void handleAudioFrameDecision(const FrameDecision& decision, AVFrame* frame);
  198. // Seek处理
  199. void performSeek();
  200. void flushBuffers();
  201. // 错误处理
  202. void handleError(const std::string& error);
  203. void attemptRecovery();
  204. // 性能监控
  205. void updatePerformanceStats();
  206. double calculateCpuUsage();
  207. double calculateMemoryUsage();
  208. private:
  209. // 状态变量
  210. std::atomic<PlayerState> m_state{PlayerState::Idle};
  211. mutable std::mutex m_mutex;
  212. // 事件回调
  213. PlayerEventCallback* m_eventCallback = nullptr;
  214. // 媒体信息
  215. MediaInfo m_mediaInfo;
  216. PlaybackStats m_stats;
  217. // FFmpeg相关
  218. AVFormatContext* m_formatContext = nullptr;
  219. // 解码器
  220. std::unique_ptr<VideoDecoder> m_videoDecoder;
  221. std::unique_ptr<AudioDecoder> m_audioDecoder;
  222. // 队列管理
  223. std::unique_ptr<av::utils::PacketQueue> m_packetQueue;
  224. std::unique_ptr<av::utils::FrameQueue> m_videoFrameQueue;
  225. std::unique_ptr<av::utils::FrameQueue> m_audioFrameQueue;
  226. // 改进的同步器
  227. std::unique_ptr<SynchronizerV2> m_synchronizer;
  228. // 线程管理
  229. std::unique_ptr<ThreadManager> m_threadManager;
  230. // 音频输出
  231. std::unique_ptr<AudioOutput> m_audioOutput;
  232. // 视频渲染
  233. OpenGLVideoRenderer* m_openGLVideoRenderer = nullptr;
  234. // 播放控制
  235. std::atomic<double> m_volume{1.0};
  236. std::atomic<double> m_playbackSpeed{1.0};
  237. // Seek控制
  238. std::atomic<int64_t> m_seekTarget{-1};
  239. std::atomic<bool> m_seeking{false};
  240. std::mutex m_seekMutex;
  241. std::condition_variable m_seekCondition;
  242. // 时间管理
  243. std::chrono::steady_clock::time_point m_playStartTime;
  244. std::atomic<int64_t> m_baseTime{0};
  245. std::atomic<int64_t> m_lastUpdateTime{0};
  246. // 线程
  247. std::thread m_readThread;
  248. std::thread m_videoDecodeThread;
  249. std::thread m_audioDecodeThread;
  250. std::thread m_videoPlayThread;
  251. std::thread m_audioPlayThread;
  252. // 线程控制
  253. std::atomic<bool> m_threadsShouldStop{false};
  254. std::atomic<bool> m_threadsRunning{false};
  255. // 初始化标志
  256. std::atomic<bool> m_initialized{false};
  257. // 帧计数
  258. std::atomic<int64_t> m_frameCount{0};
  259. std::atomic<int64_t> m_lastFrameCount{0};
  260. // 性能监控
  261. std::chrono::steady_clock::time_point m_lastStatsUpdate;
  262. std::chrono::steady_clock::time_point m_lastCpuMeasure;
  263. clock_t m_lastCpuTime;
  264. // 错误恢复
  265. std::atomic<int> m_errorCount{0};
  266. std::chrono::steady_clock::time_point m_lastErrorTime;
  267. // 缓冲控制
  268. std::atomic<bool> m_buffering{false};
  269. std::atomic<double> m_bufferHealth{1.0};
  270. };
  271. } // namespace player
  272. } // namespace av
  273. #endif // AV_PLAYER_CORE_V2_H