#ifndef ISYNCCONTROLLER_H #define ISYNCCONTROLLER_H #include #include #include #include namespace AVPlayer2 { namespace Core { /** * 同步状态枚举 */ enum class SyncState { Stopped, // 停止状态 Running, // 运行状态 Paused, // 暂停状态 Seeking, // 跳转状态 Buffering // 缓冲状态 }; /** * 同步模式枚举 */ enum class SyncMode { AudioMaster, // 以音频为主时钟 VideoMaster, // 以视频为主时钟 ExternalMaster, // 以外部时钟为主 FreeRun // 自由运行模式 }; /** * 时钟类型枚举 */ enum class ClockType { Audio, // 音频时钟 Video, // 视频时钟 External, // 外部时钟 System // 系统时钟 }; } // namespace Core namespace Types { /** * 时间戳信息 */ struct TimestampInfo { std::chrono::microseconds pts; // 显示时间戳 std::chrono::microseconds dts; // 解码时间戳 std::chrono::microseconds duration; // 持续时间 std::chrono::system_clock::time_point systemTime; // 系统时间 double timeBase; // 时间基准 TimestampInfo() { pts = std::chrono::microseconds(0); dts = std::chrono::microseconds(0); duration = std::chrono::microseconds(0); systemTime = std::chrono::system_clock::now(); timeBase = 1.0; } }; /** * 时钟信息 */ struct ClockInfo { Core::ClockType type; // 时钟类型 std::chrono::microseconds currentTime; // 当前时间 std::chrono::microseconds lastUpdate; // 最后更新时间 double speed; // 播放速度 bool isPaused; // 是否暂停 double drift; // 时钟漂移 ClockInfo() { type = Core::ClockType::System; currentTime = std::chrono::microseconds(0); lastUpdate = std::chrono::microseconds(0); speed = 1.0; isPaused = false; drift = 0.0; } }; /** * 同步统计信息 */ struct SyncStats { double audioVideoDelay; // 音视频延迟(毫秒) double averageDelay; // 平均延迟 double maxDelay; // 最大延迟 double minDelay; // 最小延迟 int droppedFrames; // 丢弃帧数 int duplicatedFrames; // 重复帧数 int syncAdjustments; // 同步调整次数 double clockDrift; // 时钟漂移 SyncStats() { audioVideoDelay = 0.0; averageDelay = 0.0; maxDelay = 0.0; minDelay = 0.0; droppedFrames = 0; duplicatedFrames = 0; syncAdjustments = 0; clockDrift = 0.0; } }; /** * 同步配置 */ struct SyncConfig { Core::SyncMode mode; // 同步模式 double maxAudioDelay; // 最大音频延迟(毫秒) double maxVideoDelay; // 最大视频延迟(毫秒) double syncThreshold; // 同步阈值(毫秒) bool enableFrameDrop; // 是否启用丢帧 bool enableFrameDuplication; // 是否启用重复帧 double speedAdjustmentRange; // 速度调整范围 int maxConsecutiveDrops; // 最大连续丢帧数 SyncConfig() { mode = Core::SyncMode::AudioMaster; maxAudioDelay = 100.0; maxVideoDelay = 100.0; syncThreshold = 40.0; enableFrameDrop = true; enableFrameDuplication = false; speedAdjustmentRange = 0.1; maxConsecutiveDrops = 5; } }; } // namespace Types namespace Core { /** * 同步控制器抽象接口 * 提供音视频同步功能 */ class ISyncController { public: virtual ~ISyncController() = default; /** * 初始化同步控制器 * @param config 同步配置 * @return 是否初始化成功 */ virtual bool initialize(const Types::SyncConfig& config) = 0; /** * 启动同步控制器 * @return 是否启动成功 */ virtual bool start() = 0; /** * 停止同步控制器 */ virtual void stop() = 0; /** * 暂停同步 */ virtual void pause() = 0; /** * 恢复同步 */ virtual void resume() = 0; /** * 重置同步状态 */ virtual void reset() = 0; /** * 设置同步模式 * @param mode 同步模式 */ virtual void setSyncMode(SyncMode mode) = 0; /** * 获取同步模式 * @return 当前同步模式 */ virtual SyncMode getSyncMode() const = 0; /** * 设置播放速度 * @param speed 播放速度(1.0为正常速度) */ virtual void setPlaybackSpeed(double speed) = 0; /** * 获取播放速度 * @return 当前播放速度 */ virtual double getPlaybackSpeed() const = 0; /** * 更新音频时钟 * @param timestamp 时间戳信息 */ virtual void updateAudioClock(const Types::TimestampInfo& timestamp) = 0; /** * 更新视频时钟 * @param timestamp 时间戳信息 */ virtual void updateVideoClock(const Types::TimestampInfo& timestamp) = 0; /** * 更新外部时钟 * @param timestamp 时间戳信息 */ virtual void updateExternalClock(const Types::TimestampInfo& timestamp) = 0; /** * 获取主时钟时间 * @return 主时钟当前时间(微秒) */ virtual std::chrono::microseconds getMasterClockTime() = 0; /** * 获取指定类型时钟信息 * @param type 时钟类型 * @return 时钟信息 */ virtual Types::ClockInfo getClockInfo(ClockType type) = 0; /** * 计算音频延迟 * @param audioTimestamp 音频时间戳 * @return 延迟时间(毫秒) */ virtual double calculateAudioDelay(const Types::TimestampInfo& audioTimestamp) = 0; /** * 计算视频延迟 * @param videoTimestamp 视频时间戳 * @return 延迟时间(毫秒) */ virtual double calculateVideoDelay(const Types::TimestampInfo& videoTimestamp) = 0; /** * 判断是否应该丢弃视频帧 * @param videoTimestamp 视频时间戳 * @return 是否应该丢弃 */ virtual bool shouldDropVideoFrame(const Types::TimestampInfo& videoTimestamp) = 0; /** * 判断是否应该重复视频帧 * @param videoTimestamp 视频时间戳 * @return 是否应该重复 */ virtual bool shouldDuplicateVideoFrame(const Types::TimestampInfo& videoTimestamp) = 0; /** * 计算音频样本调整 * @param audioTimestamp 音频时间戳 * @param sampleCount 样本数 * @return 调整后的样本数 */ virtual int calculateAudioSampleAdjustment(const Types::TimestampInfo& audioTimestamp, int sampleCount) = 0; /** * 跳转到指定时间 * @param seekTime 目标时间(微秒) */ virtual void seekTo(std::chrono::microseconds seekTime) = 0; /** * 获取同步状态 * @return 同步状态 */ virtual SyncState getState() const = 0; /** * 获取同步统计信息 * @return 统计信息 */ virtual Types::SyncStats getStats() const = 0; /** * 重置统计信息 */ virtual void resetStats() = 0; /** * 设置同步状态变化回调 * @param callback 状态变化回调函数 */ virtual void setStateChangedCallback( std::function callback) = 0; /** * 设置时钟更新回调 * @param callback 时钟更新回调函数 */ virtual void setClockUpdateCallback( std::function callback) = 0; /** * 设置同步调整回调 * @param callback 同步调整回调函数 */ virtual void setSyncAdjustmentCallback( std::function callback) = 0; /** * 设置错误回调 * @param callback 错误回调函数 */ virtual void setErrorCallback( std::function callback) = 0; }; /** * 同步控制器工厂接口 */ class ISyncControllerFactory { public: virtual ~ISyncControllerFactory() = default; /** * 创建同步控制器实例 * @param name 控制器名称 * @return 同步控制器智能指针 */ virtual std::unique_ptr createSyncController(const std::string& name = "") = 0; /** * 获取默认同步配置 * @return 默认配置 */ virtual Types::SyncConfig getDefaultConfig() = 0; /** * 获取支持的同步模式列表 * @return 支持的同步模式 */ virtual std::vector getSupportedSyncModes() = 0; }; } // namespace Core } // namespace AVPlayer2 #endif // ISYNCCONTROLLER_H