| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- #ifndef AV_PLAYER_CORE_V2_H
- #define AV_PLAYER_CORE_V2_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_frame_queue.h"
- #include "../utils/utils_packet_queue.h"
- #include "../utils/utils_synchronizer_v2.h"
- #include "audio_output.h"
- #include "../../AvRecorder/ui/opengl_video_widget.h"
- #include <memory>
- #include <atomic>
- #include <mutex>
- #include <string>
- #include <functional>
- #include <chrono>
- #include <thread>
- #include <condition_variable>
- extern "C" {
- #include <libavformat/avformat.h>
- #include <libavcodec/avcodec.h>
- #include <libavutil/avutil.h>
- #include <libavutil/time.h>
- }
- namespace av {
- namespace player {
- using VideoDecoder = av::codec::VideoDecoder;
- using AudioDecoder = av::codec::AudioDecoder;
- using VideoDecoderParams = av::codec::VideoDecoderParams;
- using AudioDecoderParams = av::codec::AudioDecoderParams;
- using SynchronizerV2 = av::utils::SynchronizerV2;
- using SyncConfigV2 = av::utils::SyncConfigV2;
- using FrameDecision = av::utils::FrameDecision;
- using FrameAction = av::utils::FrameAction;
- /**
- * 播放器状态枚举
- */
- 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;
-
- // 时间基
- AVRational videoTimeBase;
- AVRational audioTimeBase;
-
- // 兼容性字段 (与编译错误中的字段名匹配)
- int sampleRate = 0; // 音频采样率 (与audioSampleRate相同)
- int channels = 0; // 音频通道数 (与audioChannels相同)
- double bitrate = 0.0; // 比特率
- };
- /**
- * 播放统计信息
- */
- struct PlaybackStats {
- int64_t currentTime = 0; // 当前播放时间(微秒)
- int64_t totalFrames = 0; // 总帧数
- int64_t droppedFrames = 0; // 丢帧数
- int64_t duplicatedFrames = 0; // 重复帧数
- double playbackSpeed = 1.0; // 播放速度
- int queuedVideoFrames = 0; // 视频帧队列大小
- int queuedAudioFrames = 0; // 音频帧队列大小
- int queuedPackets = 0; // 数据包队列大小
-
- // 同步统计
- double syncError = 0.0; // 当前同步误差
- double avgSyncError = 0.0; // 平均同步误差
- double maxSyncError = 0.0; // 最大同步误差
-
- // 性能统计
- double cpuUsage = 0.0;
- double memoryUsage = 0.0;
- int64_t bytesRead = 0;
- double bitrate = 0.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;
- virtual void onSyncError(double error, const std::string& reason) = 0;
- virtual void onFrameDropped(int64_t count) = 0;
- };
- /**
- * 改进的播放器核心类 - 基于AVPlayer2的经验重新设计
- *
- * 主要改进:
- * 1. 使用新的SynchronizerV2进行精确时间同步
- * 2. 改进的线程管理和错误处理
- * 3. 更好的seek操作和状态管理
- * 4. 详细的性能监控和统计
- * 5. 自适应播放策略
- */
- class PlayerCoreV2
- {
- public:
- explicit PlayerCoreV2(const SyncConfigV2& syncConfig = SyncConfigV2());
- ~PlayerCoreV2();
- // 事件回调设置
- 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 setSyncConfig(const SyncConfig& config);
- // SyncConfig getSyncConfig() const;
- // OpenGL渲染器设置
- void setOpenGLVideoRenderer(OpenGLVideoWidget* renderer);
- OpenGLVideoWidget* getOpenGLVideoRenderer() const { return m_openGLVideoRenderer; }
-
- // 帧获取接口(供UI渲染使用)
- AVFrame* getNextVideoFrame(); // 获取下一个视频帧
- AVFrame* getNextAudioFrame(); // 获取下一个音频帧
- void releaseVideoFrame(AVFrame* frame); // 释放视频帧
- void releaseAudioFrame(AVFrame* frame); // 释放音频帧
-
- // 线程安全的更新接口
- void update(); // 定期调用以更新播放状态
-
- // 调试接口
- std::string getDebugInfo() const;
- void dumpStats() const;
- private:
- // 初始化和清理
- bool initializeFFmpeg();
- void cleanup();
- bool openMediaFile(const std::string& filename);
- void closeMediaFile();
-
- // 线程管理
- bool startReadThread();
- bool startDecodeThreads();
- bool startVideoPlayThread();
- bool startAudioPlayThread();
- void stopAllThreads();
-
- // 解码器管理
- bool setupVideoDecoder();
- bool setupAudioDecoder();
- void resetDecoders();
-
- // 同步控制
- void updateSynchronization();
- int64_t getCurrentPlayTime();
- void handleSyncError(double error, const std::string& reason);
-
- // 状态管理
- void setState(PlayerState newState);
- void updateStats();
- void notifyStateChanged(PlayerState newState);
- void notifyError(const std::string& error);
- void notifyPositionChanged();
-
- // 线程函数
- void readThreadFunc();
- void videoDecodeThreadFunc();
- void audioDecodeThreadFunc();
- void videoPlayThreadFunc();
- void audioPlayThreadFunc();
-
- // 帧处理
- bool processVideoFrame();
- bool processAudioFrame();
- void handleVideoFrameDecision(const FrameDecision& decision, AVFrame* frame);
- void handleAudioFrameDecision(const FrameDecision& decision, AVFrame* frame);
-
- // Seek处理
- void performSeek();
- void flushBuffers();
-
- // 错误处理
- void handleError(const std::string& error);
- void attemptRecovery();
-
- // 性能监控
- void updatePerformanceStats();
- double calculateCpuUsage();
- double calculateMemoryUsage();
-
- private:
- // 状态变量
- std::atomic<PlayerState> 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<VideoDecoder> m_videoDecoder;
- std::unique_ptr<AudioDecoder> m_audioDecoder;
-
- // 队列管理 - 分离视频和音频包队列以提高效率
- std::unique_ptr<av::utils::PacketQueue> m_videoPacketQueue;
- std::unique_ptr<av::utils::PacketQueue> m_audioPacketQueue;
- std::unique_ptr<av::utils::FrameQueue> m_videoFrameQueue;
- std::unique_ptr<av::utils::FrameQueue> m_audioFrameQueue;
-
- // 同步器
- // std::unique_ptr<Synchronizer> m_synchronizer;
- // 音频输出
- std::unique_ptr<AudioOutput> m_audioOutput;
-
- // 视频渲染
- OpenGLVideoWidget* m_openGLVideoRenderer = nullptr;
-
- // 播放控制
- std::atomic<double> m_volume{1.0};
- std::atomic<double> m_playbackSpeed{1.0};
-
- // Seek控制
- std::atomic<int64_t> m_seekTarget{-1};
- std::atomic<bool> m_seeking{false};
- std::mutex m_seekMutex;
- std::condition_variable m_seekCondition;
-
- // 时间管理
- std::chrono::steady_clock::time_point m_playStartTime;
- std::atomic<int64_t> m_baseTime{0};
- std::atomic<int64_t> m_lastUpdateTime{0};
-
- // 线程
- std::thread m_readThread;
- std::thread m_videoDecodeThread;
- std::thread m_audioDecodeThread;
- std::thread m_videoPlayThread;
- std::thread m_audioPlayThread;
-
- // 线程控制
- std::atomic<bool> m_threadsShouldStop{false};
- std::atomic<bool> m_threadsRunning{false};
-
- // 初始化标志
- std::atomic<bool> m_initialized{false};
-
- // 帧计数
- std::atomic<int64_t> m_frameCount{0};
- std::atomic<int64_t> m_lastFrameCount{0};
-
- // 性能监控
- std::chrono::steady_clock::time_point m_lastStatsUpdate;
- std::chrono::steady_clock::time_point m_lastCpuMeasure;
- clock_t m_lastCpuTime;
-
- // 错误恢复
- std::atomic<int> m_errorCount{0};
- std::chrono::steady_clock::time_point m_lastErrorTime;
-
- // 缓冲控制
- std::atomic<bool> m_buffering{false};
- std::atomic<double> m_bufferHealth{1.0};
- };
- } // namespace player
- } // namespace av
- #endif // AV_PLAYER_CORE_V2_H
|