| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- #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 "opengl_video_renderer.h"
- #include <memory>
- #include <atomic>
- #include <mutex>
- #include <string>
- #include <functional>
- #include <chrono>
- extern "C" {
- #include <libavformat/avformat.h>
- #include <libavcodec/avcodec.h>
- #include <libavutil/avutil.h>
- }
- 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; }
-
- // OpenGL渲染器设置
- void setOpenGLVideoRenderer(OpenGLVideoRenderer* renderer);
- OpenGLVideoRenderer* getOpenGLVideoRenderer() const { return m_openGLVideoRenderer; }
-
- // 帧获取接口(供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();
-
- 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_packetQueue;
- 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<ThreadManager> m_threadManager;
-
- // 输出设备
- std::unique_ptr<AudioOutput> m_audioOutput;
- OpenGLVideoRenderer* m_openGLVideoRenderer = nullptr; // OpenGL视频渲染器
-
- // 播放控制
- double m_volume = 1.0;
- double m_playbackSpeed = 1.0;
- int64_t m_seekTarget = -1;
- std::atomic<bool> m_seeking{false};
-
- // 性能统计
- int64_t m_lastUpdateTime = 0;
- int64_t m_frameCount = 0;
-
- // 播放时间计算
- std::chrono::steady_clock::time_point m_playStartTime;
- int64_t m_baseTime = 0; // 基准时间(微秒)
-
- // 初始化标志
- bool m_initialized = false;
- };
- } // namespace player
- } // namespace av
- #endif // AV_PLAYER_CORE_H
|