player_core_v2.h 9.4 KB

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