player_core_v2.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. // 安全帧获取接口
  159. AVFramePtr getNextVideoFrame(); // 获取下一个视频帧
  160. AVFramePtr getNextAudioFrame(); // 获取下一个音频帧
  161. // 线程安全的更新接口
  162. void update(); // 定期调用以更新播放状态
  163. // 调试接口
  164. std::string getDebugInfo() const;
  165. void dumpStats() const;
  166. private:
  167. // 初始化和清理
  168. bool initializeFFmpeg();
  169. void cleanup();
  170. bool openMediaFile(const std::string& filename);
  171. void closeMediaFile();
  172. // 线程管理
  173. bool startReadThread();
  174. bool startDecodeThreads();
  175. bool startVideoPlayThread();
  176. bool startAudioPlayThread();
  177. void stopAllThreads();
  178. // 解码器管理
  179. bool setupVideoDecoder();
  180. bool setupAudioDecoder();
  181. void resetDecoders();
  182. // 同步控制
  183. void updateSynchronization();
  184. int64_t getCurrentPlayTime();
  185. void handleSyncError(double error, const std::string& reason);
  186. // 状态管理
  187. void setState(PlayerState newState);
  188. void updateStats();
  189. void notifyStateChanged(PlayerState newState);
  190. void notifyError(const std::string& error);
  191. void notifyPositionChanged();
  192. // 线程函数
  193. void readThreadFunc();
  194. void videoDecodeThreadFunc();
  195. void audioDecodeThreadFunc();
  196. void videoPlayThreadFunc();
  197. void audioPlayThreadFunc();
  198. // 帧处理
  199. bool processVideoFrame();
  200. bool processAudioFrame();
  201. void handleVideoFrameDecision(const FrameDecision& decision, AVFrame* frame);
  202. void handleAudioFrameDecision(const FrameDecision& decision, AVFrame* frame);
  203. // Seek处理
  204. void performSeek();
  205. void flushBuffers();
  206. // 错误处理
  207. void handleError(const std::string& error);
  208. void attemptRecovery();
  209. // 性能监控
  210. void updatePerformanceStats();
  211. double calculateCpuUsage();
  212. double calculateMemoryUsage();
  213. private:
  214. // 状态变量
  215. std::atomic<PlayerState> m_state{PlayerState::Idle};
  216. mutable std::mutex m_mutex;
  217. // 事件回调
  218. PlayerEventCallback* m_eventCallback = nullptr;
  219. // 媒体信息
  220. MediaInfo m_mediaInfo;
  221. PlaybackStats m_stats;
  222. // FFmpeg相关
  223. AVFormatContext* m_formatContext = nullptr;
  224. // 解码器
  225. std::unique_ptr<VideoDecoder> m_videoDecoder;
  226. std::unique_ptr<AudioDecoder> m_audioDecoder;
  227. // 队列管理 - 分离视频和音频包队列以提高效率
  228. std::unique_ptr<av::utils::PacketQueue> m_videoPacketQueue;
  229. std::unique_ptr<av::utils::PacketQueue> m_audioPacketQueue;
  230. std::unique_ptr<av::utils::FrameQueue> m_videoFrameQueue;
  231. std::unique_ptr<av::utils::FrameQueue> m_audioFrameQueue;
  232. // 同步器
  233. std::unique_ptr<SynchronizerV2> m_synchronizer;
  234. // 音频输出
  235. std::unique_ptr<AudioOutput> m_audioOutput;
  236. // 视频渲染(已移除直接依赖,改为使用回调)
  237. // OpenGLVideoWidget* m_openGLVideoRenderer = nullptr;
  238. // 播放控制
  239. std::atomic<double> m_volume{1.0};
  240. std::atomic<double> m_playbackSpeed{1.0};
  241. // Seek控制
  242. std::atomic<int64_t> m_seekTarget{-1};
  243. std::atomic<int64_t> m_seekMinTime{INT64_MIN};
  244. std::atomic<int64_t> m_seekMaxTime{INT64_MAX};
  245. std::atomic<int> m_seekFlags{AVSEEK_FLAG_BACKWARD};
  246. std::atomic<bool> m_seeking{false};
  247. std::mutex m_seekMutex;
  248. std::condition_variable m_seekCondition;
  249. // 流控制
  250. std::atomic<bool> m_videoStreamEnabled{true};
  251. std::atomic<bool> m_audioStreamEnabled{true};
  252. // 时间管理
  253. std::chrono::steady_clock::time_point m_playStartTime;
  254. std::atomic<int64_t> m_baseTime{0};
  255. std::atomic<int64_t> m_lastUpdateTime{0};
  256. // 线程
  257. std::thread m_readThread;
  258. std::thread m_videoDecodeThread;
  259. std::thread m_audioDecodeThread;
  260. std::thread m_videoPlayThread;
  261. std::thread m_audioPlayThread;
  262. // 线程控制
  263. std::atomic<bool> m_threadsShouldStop{false};
  264. std::atomic<bool> m_threadsRunning{false};
  265. std::atomic<bool> m_paused{false}; // 暂停标志,类似ffplay.c中的paused
  266. std::condition_variable m_pauseCondition; // 暂停条件变量,类似ffplay.c中的continue_read_thread
  267. std::mutex m_pauseMutex; // 暂停互斥锁
  268. // 初始化标志
  269. std::atomic<bool> m_initialized{false};
  270. // 帧计数
  271. std::atomic<int64_t> m_frameCount{0};
  272. std::atomic<int64_t> m_lastFrameCount{0};
  273. // 性能监控
  274. std::chrono::steady_clock::time_point m_lastStatsUpdate;
  275. std::chrono::steady_clock::time_point m_lastCpuMeasure;
  276. clock_t m_lastCpuTime;
  277. // 错误恢复
  278. std::atomic<int> m_errorCount{0};
  279. std::chrono::steady_clock::time_point m_lastErrorTime;
  280. // 缓冲控制
  281. std::atomic<bool> m_buffering{false};
  282. std::atomic<double> m_bufferHealth{1.0};
  283. };
  284. } // namespace player
  285. } // namespace av
  286. #endif // AV_PLAYER_CORE_V2_H