#ifndef AV_PLAYER_CORE_H #define AV_PLAYER_CORE_H #pragma once #include "../base/logger.h" #include "../base/types.h" #include "../codec/codec_audio_decoder.h" #include "../codec/codec_video_decoder.h" #include "../utils/utils_packet_queue.h" #include "../utils/utils_frame_queue.h" #include "../utils/utils_synchronizer.h" #include "thread_manager.h" #include "audio_output.h" #include "video_renderer.h" #include #include #include #include #include extern "C" { #include #include #include } namespace av { namespace player { using VideoDecoder = av::codec::VideoDecoder; using AudioDecoder = av::codec::AudioDecoder; using Synchronizer = av::utils::Synchronizer; /** * 播放器状态枚举 */ enum class PlayerState { Idle, // 空闲 Opening, // 正在打开文件 Playing, // 播放中 Paused, // 暂停 Seeking, // 跳转中 Stopped, // 已停止 Error // 错误状态 }; /** * 媒体信息结构 */ struct MediaInfo { std::string filename; int64_t duration = 0; // 总时长(微秒) int width = 0; int height = 0; double fps = 0.0; int audioSampleRate = 0; int audioChannels = 0; bool hasVideo = false; bool hasAudio = false; bool hasSubtitle = false; // 流索引 int videoStreamIndex = -1; int audioStreamIndex = -1; int subtitleStreamIndex = -1; }; /** * 播放统计信息 */ struct PlaybackStats { int64_t currentTime = 0; // 当前播放时间(微秒) int64_t totalFrames = 0; // 总帧数 int64_t droppedFrames = 0; // 丢帧数 double playbackSpeed = 1.0; // 播放速度 int queuedVideoFrames = 0; // 视频帧队列大小 int queuedAudioFrames = 0; // 音频帧队列大小 int queuedPackets = 0; // 数据包队列大小 }; /** * 播放器事件回调接口 */ class PlayerEventCallback { public: virtual ~PlayerEventCallback() = default; virtual void onStateChanged(PlayerState newState) = 0; virtual void onMediaInfoChanged(const MediaInfo& info) = 0; virtual void onPositionChanged(int64_t position) = 0; // 微秒 virtual void onErrorOccurred(const std::string& error) = 0; virtual void onEndOfFile() = 0; }; /** * 播放器核心类 - 使用新的AV核心库 * 负责媒体文件的解码和播放控制,但不涉及UI渲染 * 这是一个纯C++实现,不依赖Qt */ class PlayerCore { public: explicit PlayerCore(); ~PlayerCore(); // 事件回调设置 void setEventCallback(PlayerEventCallback* callback); // 播放控制接口 ErrorCode openFile(const std::string& filename); ErrorCode play(); ErrorCode pause(); ErrorCode stop(); ErrorCode seek(int64_t timestamp); // 微秒 ErrorCode setPlaybackSpeed(double speed); // 状态查询 PlayerState getState() const { return m_state.load(); } MediaInfo getMediaInfo() const; PlaybackStats getStats() const; int64_t getCurrentTime() const; double getPlaybackSpeed() const; // 音量控制 void setVolume(double volume); // 0.0 - 1.0 double getVolume() const { return m_volume; } // 输出设备设置 void setVideoRenderer(VideoRenderer* renderer); VideoRenderer* getVideoRenderer() const { return m_videoRenderer; } // 帧获取接口(供UI渲染使用) AVFrame* getNextVideoFrame(); // 获取下一个视频帧 AVFrame* getNextAudioFrame(); // 获取下一个音频帧 void releaseVideoFrame(AVFrame* frame); // 释放视频帧 void releaseAudioFrame(AVFrame* frame); // 释放音频帧 // 线程安全的更新接口 void update(); // 定期调用以更新播放状态 private: // 初始化和清理 bool initializeFFmpeg(); void cleanup(); bool openMediaFile(const std::string& filename); void closeMediaFile(); // 线程管理 bool startReadThread(); bool startDecodeThreads(); void stopAllThreads(); // 解码器管理 bool setupVideoDecoder(); bool setupAudioDecoder(); void resetDecoders(); // 同步控制 void updateSynchronization(); int64_t getCurrentPlayTime(); // 状态管理 void setState(PlayerState newState); void updateStats(); void notifyStateChanged(PlayerState newState); void notifyError(const std::string& error); // 线程函数 void readThreadFunc(); void videoDecodeThreadFunc(); void audioDecodeThreadFunc(); private: // 状态变量 std::atomic m_state{PlayerState::Idle}; mutable std::mutex m_mutex; // 事件回调 PlayerEventCallback* m_eventCallback = nullptr; // 媒体信息 MediaInfo m_mediaInfo; PlaybackStats m_stats; // FFmpeg相关 AVFormatContext* m_formatContext = nullptr; // 解码器 std::unique_ptr m_videoDecoder; std::unique_ptr m_audioDecoder; // 队列管理 std::unique_ptr m_packetQueue; std::unique_ptr m_videoFrameQueue; std::unique_ptr m_audioFrameQueue; // 同步器 std::unique_ptr m_synchronizer; // 线程管理器 std::unique_ptr m_threadManager; // 输出设备 std::unique_ptr m_audioOutput; VideoRenderer* m_videoRenderer = nullptr; // 外部提供的视频渲染器 // 播放控制 double m_volume = 1.0; double m_playbackSpeed = 1.0; int64_t m_seekTarget = -1; std::atomic m_seeking{false}; // 性能统计 int64_t m_lastUpdateTime = 0; int64_t m_frameCount = 0; // 初始化标志 bool m_initialized = false; }; } // namespace player } // namespace av #endif // AV_PLAYER_CORE_H