| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- #ifndef IPLAYERCONTROLLER_H
- #define IPLAYERCONTROLLER_H
- #include <string>
- #include <memory>
- #include <functional>
- #include <chrono>
- #include <vector>
- #include <map>
- namespace AVPlayer2 {
- namespace Core {
- /**
- * 播放器状态枚举
- */
- enum class PlayerState {
- Stopped, // 停止状态
- Loading, // 加载中
- Loaded, // 已加载
- Playing, // 播放中
- Paused, // 暂停
- Seeking, // 跳转中
- Buffering, // 缓冲中
- Error // 错误状态
- };
- /**
- * 播放模式枚举
- */
- enum class PlayMode {
- Normal, // 正常播放
- Loop, // 循环播放
- Shuffle, // 随机播放
- RepeatOne // 单曲循环
- };
- /**
- * 缩放模式枚举
- */
- enum class ScaleMode {
- Fit, // 适应窗口
- Fill, // 填充窗口
- Stretch, // 拉伸
- Original, // 原始大小
- Custom // 自定义
- };
- } // namespace Core
- namespace Types {
- /**
- * 播放器配置
- */
- struct PlayerConfig {
- bool enableHardwareDecoding; // 启用硬件解码
- bool enableAudioOutput; // 启用音频输出
- bool enableVideoOutput; // 启用视频输出
- bool enableSubtitles; // 启用字幕
- int maxVideoFrameBuffer; // 最大视频帧缓冲
- int maxAudioFrameBuffer; // 最大音频帧缓冲
- double seekAccuracy; // 跳转精度(秒)
- bool autoPlay; // 自动播放
- double defaultVolume; // 默认音量(0.0-1.0)
- Core::PlayMode defaultPlayMode; // 默认播放模式
- Core::ScaleMode defaultScaleMode; // 默认缩放模式
- std::string userAgent; // 用户代理
- int networkTimeout; // 网络超时(毫秒)
-
- PlayerConfig() {
- enableHardwareDecoding = true;
- enableAudioOutput = true;
- enableVideoOutput = true;
- enableSubtitles = true;
- maxVideoFrameBuffer = 10;
- maxAudioFrameBuffer = 20;
- seekAccuracy = 1.0;
- autoPlay = false;
- defaultVolume = 1.0;
- defaultPlayMode = Core::PlayMode::Normal;
- defaultScaleMode = Core::ScaleMode::Fit;
- userAgent = "AVPlayer2/1.0";
- networkTimeout = 30000;
- }
- };
- /**
- * 播放器统计信息
- */
- struct PlayerStats {
- std::chrono::microseconds totalDuration; // 总时长
- std::chrono::microseconds currentPosition; // 当前位置
- double playbackSpeed; // 播放速度
- int videoFrameRate; // 视频帧率
- int audioSampleRate; // 音频采样率
- int videoBitrate; // 视频比特率
- int audioBitrate; // 音频比特率
- int droppedFrames; // 丢弃帧数
- int renderedFrames; // 渲染帧数
- double bufferHealth; // 缓冲健康度(0.0-1.0)
- std::string videoCodec; // 视频编解码器
- std::string audioCodec; // 音频编解码器
-
- PlayerStats() {
- totalDuration = std::chrono::microseconds(0);
- currentPosition = std::chrono::microseconds(0);
- playbackSpeed = 1.0;
- videoFrameRate = 0;
- audioSampleRate = 0;
- videoBitrate = 0;
- audioBitrate = 0;
- droppedFrames = 0;
- renderedFrames = 0;
- bufferHealth = 0.0;
- }
- };
- /**
- * 播放列表项
- */
- struct PlaylistItem {
- std::string id; // 项目ID
- std::string url; // 媒体URL
- std::string title; // 标题
- std::string artist; // 艺术家
- std::string album; // 专辑
- std::chrono::microseconds duration; // 时长
- std::map<std::string, std::string> metadata; // 元数据
-
- PlaylistItem() {
- duration = std::chrono::microseconds(0);
- }
- };
- /**
- * 字幕信息
- */
- struct SubtitleTrack {
- int index; // 轨道索引
- std::string language; // 语言
- std::string title; // 标题
- std::string codec; // 编解码器
- bool isDefault; // 是否默认
- bool isForced; // 是否强制
-
- SubtitleTrack() {
- index = -1;
- isDefault = false;
- isForced = false;
- }
- };
- /**
- * 音频轨道信息
- */
- struct AudioTrack {
- int index; // 轨道索引
- std::string language; // 语言
- std::string title; // 标题
- std::string codec; // 编解码器
- int channels; // 声道数
- int sampleRate; // 采样率
- int bitrate; // 比特率
- bool isDefault; // 是否默认
-
- AudioTrack() {
- index = -1;
- channels = 0;
- sampleRate = 0;
- bitrate = 0;
- isDefault = false;
- }
- };
- } // namespace Types
- namespace Core {
- /**
- * 播放器控制器抽象接口
- * 提供完整的媒体播放控制功能
- */
- class IPlayerController {
- public:
- virtual ~IPlayerController() = default;
- /**
- * 初始化播放器
- * @param config 播放器配置
- * @return 是否初始化成功
- */
- virtual bool initialize(const Types::PlayerConfig& config) = 0;
- /**
- * 释放播放器资源
- */
- virtual void release() = 0;
- // ========== 媒体加载 ==========
-
- /**
- * 加载媒体文件
- * @param url 媒体文件URL或路径
- * @return 是否开始加载
- */
- virtual bool loadMedia(const std::string& url) = 0;
- /**
- * 卸载当前媒体
- */
- virtual void unloadMedia() = 0;
- // ========== 播放控制 ==========
-
- /**
- * 开始播放
- * @return 是否开始播放
- */
- virtual bool play() = 0;
- /**
- * 暂停播放
- */
- virtual void pause() = 0;
- /**
- * 停止播放
- */
- virtual void stop() = 0;
- /**
- * 跳转到指定位置
- * @param position 目标位置(微秒)
- * @return 是否开始跳转
- */
- virtual bool seekTo(std::chrono::microseconds position) = 0;
- /**
- * 相对跳转
- * @param offset 偏移量(微秒,正数向前,负数向后)
- * @return 是否开始跳转
- */
- virtual bool seekBy(std::chrono::microseconds offset) = 0;
- // ========== 播放参数 ==========
-
- /**
- * 设置播放速度
- * @param speed 播放速度(1.0为正常速度)
- */
- virtual void setPlaybackSpeed(double speed) = 0;
- /**
- * 获取播放速度
- * @return 当前播放速度
- */
- virtual double getPlaybackSpeed() const = 0;
- /**
- * 设置音量
- * @param volume 音量(0.0-1.0)
- */
- virtual void setVolume(double volume) = 0;
- /**
- * 获取音量
- * @return 当前音量
- */
- virtual double getVolume() const = 0;
- /**
- * 设置静音
- * @param muted 是否静音
- */
- virtual void setMuted(bool muted) = 0;
- /**
- * 获取静音状态
- * @return 是否静音
- */
- virtual bool isMuted() const = 0;
- // ========== 状态查询 ==========
-
- /**
- * 获取播放器状态
- * @return 当前状态
- */
- virtual PlayerState getState() const = 0;
- /**
- * 获取当前播放位置
- * @return 当前位置(微秒)
- */
- virtual std::chrono::microseconds getCurrentPosition() const = 0;
- /**
- * 获取媒体总时长
- * @return 总时长(微秒)
- */
- virtual std::chrono::microseconds getDuration() const = 0;
- /**
- * 获取缓冲进度
- * @return 缓冲进度(0.0-1.0)
- */
- virtual double getBufferProgress() const = 0;
- /**
- * 是否可以播放
- * @return 是否可以播放
- */
- virtual bool canPlay() const = 0;
- /**
- * 是否可以暂停
- * @return 是否可以暂停
- */
- virtual bool canPause() const = 0;
- /**
- * 是否可以跳转
- * @return 是否可以跳转
- */
- virtual bool canSeek() const = 0;
- // ========== 轨道管理 ==========
-
- /**
- * 获取音频轨道列表
- * @return 音频轨道列表
- */
- virtual std::vector<Types::AudioTrack> getAudioTracks() const = 0;
- /**
- * 获取字幕轨道列表
- * @return 字幕轨道列表
- */
- virtual std::vector<Types::SubtitleTrack> getSubtitleTracks() const = 0;
- /**
- * 选择音频轨道
- * @param index 轨道索引
- * @return 是否选择成功
- */
- virtual bool selectAudioTrack(int index) = 0;
- /**
- * 选择字幕轨道
- * @param index 轨道索引(-1表示关闭字幕)
- * @return 是否选择成功
- */
- virtual bool selectSubtitleTrack(int index) = 0;
- /**
- * 获取当前音频轨道索引
- * @return 当前音频轨道索引
- */
- virtual int getCurrentAudioTrack() const = 0;
- /**
- * 获取当前字幕轨道索引
- * @return 当前字幕轨道索引
- */
- virtual int getCurrentSubtitleTrack() const = 0;
- // ========== 播放列表 ==========
-
- /**
- * 设置播放模式
- * @param mode 播放模式
- */
- virtual void setPlayMode(PlayMode mode) = 0;
- /**
- * 获取播放模式
- * @return 当前播放模式
- */
- virtual PlayMode getPlayMode() const = 0;
- /**
- * 添加播放列表项
- * @param item 播放列表项
- * @return 是否添加成功
- */
- virtual bool addPlaylistItem(const Types::PlaylistItem& item) = 0;
- /**
- * 移除播放列表项
- * @param index 项目索引
- * @return 是否移除成功
- */
- virtual bool removePlaylistItem(int index) = 0;
- /**
- * 清空播放列表
- */
- virtual void clearPlaylist() = 0;
- /**
- * 获取播放列表
- * @return 播放列表
- */
- virtual std::vector<Types::PlaylistItem> getPlaylist() const = 0;
- /**
- * 播放指定项目
- * @param index 项目索引
- * @return 是否开始播放
- */
- virtual bool playItem(int index) = 0;
- /**
- * 播放下一项
- * @return 是否开始播放
- */
- virtual bool playNext() = 0;
- /**
- * 播放上一项
- * @return 是否开始播放
- */
- virtual bool playPrevious() = 0;
- /**
- * 获取当前播放项索引
- * @return 当前播放项索引
- */
- virtual int getCurrentPlaylistIndex() const = 0;
- // ========== 视频控制 ==========
-
- /**
- * 设置视频缩放模式
- * @param mode 缩放模式
- */
- virtual void setScaleMode(ScaleMode mode) = 0;
- /**
- * 获取视频缩放模式
- * @return 当前缩放模式
- */
- virtual ScaleMode getScaleMode() const = 0;
- /**
- * 设置视频旋转角度
- * @param angle 旋转角度(度)
- */
- virtual void setRotation(int angle) = 0;
- /**
- * 获取视频旋转角度
- * @return 当前旋转角度
- */
- virtual int getRotation() const = 0;
- // ========== 统计信息 ==========
-
- /**
- * 获取播放器统计信息
- * @return 统计信息
- */
- virtual Types::PlayerStats getStats() const = 0;
- /**
- * 重置统计信息
- */
- virtual void resetStats() = 0;
- // ========== 回调设置 ==========
-
- /**
- * 设置状态变化回调
- * @param callback 状态变化回调函数
- */
- virtual void setStateChangedCallback(
- std::function<void(PlayerState, PlayerState)> callback) = 0;
- /**
- * 设置位置变化回调
- * @param callback 位置变化回调函数
- */
- virtual void setPositionChangedCallback(
- std::function<void(std::chrono::microseconds)> callback) = 0;
- /**
- * 设置时长变化回调
- * @param callback 时长变化回调函数
- */
- virtual void setDurationChangedCallback(
- std::function<void(std::chrono::microseconds)> callback) = 0;
- /**
- * 设置缓冲进度回调
- * @param callback 缓冲进度回调函数
- */
- virtual void setBufferProgressCallback(
- std::function<void(double)> callback) = 0;
- /**
- * 设置媒体加载完成回调
- * @param callback 媒体加载完成回调函数
- */
- virtual void setMediaLoadedCallback(
- std::function<void(const std::string&)> callback) = 0;
- /**
- * 设置播放完成回调
- * @param callback 播放完成回调函数
- */
- virtual void setPlaybackFinishedCallback(
- std::function<void()> callback) = 0;
- /**
- * 设置错误回调
- * @param callback 错误回调函数
- */
- virtual void setErrorCallback(
- std::function<void(const std::string&, const std::string&)> callback) = 0;
- };
- /**
- * 播放器控制器工厂接口
- */
- class IPlayerControllerFactory {
- public:
- virtual ~IPlayerControllerFactory() = default;
- /**
- * 创建播放器控制器实例
- * @param name 控制器名称
- * @return 播放器控制器智能指针
- */
- virtual std::unique_ptr<IPlayerController> createPlayerController(const std::string& name = "") = 0;
- /**
- * 获取默认播放器配置
- * @return 默认配置
- */
- virtual Types::PlayerConfig getDefaultConfig() = 0;
- /**
- * 获取支持的媒体格式列表
- * @return 支持的格式列表
- */
- virtual std::vector<std::string> getSupportedFormats() = 0;
- /**
- * 检查是否支持指定格式
- * @param format 媒体格式
- * @return 是否支持
- */
- virtual bool isFormatSupported(const std::string& format) = 0;
- };
- } // namespace Core
- } // namespace AVPlayer2
- #endif // IPLAYERCONTROLLER_H
|