player_core.h 5.9 KB

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