player_core_v2.h 10 KB

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