#ifndef AV_MUXER_ABSTRACT_MUXER_H #define AV_MUXER_ABSTRACT_MUXER_H #include "../base/media_common.h" #include "../base/logger.h" #include #include #include #include #include #include #include #include extern "C" { #include #include #include #include } namespace av { namespace muxer { // 复用器状态 enum class MuxerState { IDLE = 0, // 空闲状态 INITIALIZED, // 已初始化 STARTED, // 已启动 PAUSED, // 已暂停 STOPPED, // 已停止 ERROR_STATE // 错误状态 }; // 复用器类型 enum class MuxerType { FILE_MUXER = 0, // 文件复用器 STREAM_MUXER, // 流复用器 RTMP_MUXER, // RTMP推流 UDP_MUXER, // UDP推流 TCP_MUXER, // TCP推流 HLS_MUXER, // HLS分片 DASH_MUXER // DASH分片 }; // 流类型 enum class StreamType { VIDEO = 0, AUDIO, SUBTITLE, DATA }; // 流信息 struct StreamInfo { int index = -1; // 流索引 StreamType type = StreamType::VIDEO; // 流类型 AVCodecID codecId = AV_CODEC_ID_NONE; // 编解码器ID std::string codecName; // 编解码器名称 // 视频流参数 int width = 0; int height = 0; AVRational frameRate = {0, 1}; AVPixelFormat pixelFormat = AV_PIX_FMT_NONE; int64_t bitrate = 0; // 音频流参数 int sampleRate = 0; int channels = 0; AVSampleFormat sampleFormat = AV_SAMPLE_FMT_NONE; AVChannelLayout channelLayout = AV_CHANNEL_LAYOUT_MASK(0, 0); // 通用参数 AVRational timeBase = {1, 1000}; std::string metadata; // 元数据 bool isDefault = false; // 是否为默认流 }; // 复用器参数基类 struct MuxerParams { MuxerType type = MuxerType::FILE_MUXER; MediaType mediaType = MediaType::VIDEO; std::string outputPath; // 输出路径或URL std::string format; // 输出格式 (mp4, flv, ts等) // 流配置 std::vector streams; // 流信息列表 // 通用选项 std::map options; // 格式选项 std::map metadata; // 元数据 // 性能参数 int bufferSize = 1024 * 1024; // 缓冲区大小 int maxDelay = 500000; // 最大延迟(微秒) bool lowLatency = false; // 低延迟模式 // 错误处理 int maxRetries = 3; // 最大重试次数 int retryDelay = 1000; // 重试延迟(毫秒) MuxerParams(MuxerType t) : type(t) {} virtual ~MuxerParams() = default; }; // 复用器统计信息 struct MuxerStats { // 基础统计 uint64_t totalPackets = 0; // 总包数 uint64_t totalBytes = 0; // 总字节数 uint64_t droppedPackets = 0; // 丢弃包数 uint64_t errorCount = 0; // 错误计数 // 性能统计 double averageWriteTime = 0.0; // 平均写入时间(毫秒) double currentBitrate = 0.0; // 当前码率(bps) double averageBitrate = 0.0; // 平均码率(bps) // 时间统计 std::chrono::steady_clock::time_point startTime; std::chrono::steady_clock::time_point lastPacketTime; double duration = 0.0; // 持续时间(秒) // 流统计 std::map streamPackets; // 各流包数 std::map streamBytes; // 各流字节数 }; // 抽象复用器基类 class AbstractMuxer { public: AbstractMuxer(); virtual ~AbstractMuxer(); // 基础接口 virtual ErrorCode initialize(const MuxerParams& params); virtual ErrorCode start(); virtual ErrorCode stop(); virtual ErrorCode pause(); virtual ErrorCode resume(); virtual ErrorCode reset(); virtual ErrorCode close(); // 写入接口 virtual ErrorCode writePacket(AVPacket* packet); virtual ErrorCode writeFrame(AVFrame* frame, int streamIndex); virtual ErrorCode flush(); // 流管理 virtual ErrorCode addStream(const StreamInfo& streamInfo); virtual ErrorCode removeStream(int streamIndex); virtual std::vector getStreams() const; virtual StreamInfo getStreamInfo(int streamIndex) const; // 状态查询 MuxerState getState() const { return state_.load(); } bool isRunning() const { return getState() == MuxerState::STARTED; } bool isPaused() const { return getState() == MuxerState::PAUSED; } // 统计信息 MuxerStats getStats() const; void resetStats(); // 错误处理 std::string getLastError() const; // 回调设置 using ErrorCallback = std::function; using ProgressCallback = std::function; // 进度百分比 using StatsCallback = std::function; void setErrorCallback(ErrorCallback callback) { errorCallback_ = callback; } void setProgressCallback(ProgressCallback callback) { progressCallback_ = callback; } void setStatsCallback(StatsCallback callback) { statsCallback_ = callback; } // 静态工具方法 static std::vector getSupportedFormats(); static std::vector getSupportedCodecs(const std::string& format); static bool isFormatSupported(const std::string& format); static std::string getFormatFromExtension(const std::string& filename); static std::string getDefaultExtension(const std::string& format); protected: // 状态管理 void setState(MuxerState state); // 错误处理 void onError(ErrorCode code, const std::string& message); // 统计更新 void updateStats(AVPacket* packet); void updateBitrate(); // 参数验证 virtual bool validateParams(const MuxerParams& params); // 内部实现方法 virtual ErrorCode setupOutput(); virtual ErrorCode writeHeader(); virtual ErrorCode writeTrailer(); // 成员变量 std::atomic state_{MuxerState::IDLE}; mutable std::mutex statsMutex_; mutable std::mutex streamsMutex_; MuxerStats stats_; std::vector streams_; std::string lastError_; // 回调函数 ErrorCallback errorCallback_; ProgressCallback progressCallback_; StatsCallback statsCallback_; // FFmpeg相关 AVFormatContext* formatCtx_ = nullptr; std::map streamMap_; // 流索引映射 // 性能监控 std::chrono::steady_clock::time_point lastStatsUpdate_; static constexpr double STATS_UPDATE_INTERVAL = 1.0; // 统计更新间隔(秒) }; // 复用器工厂抽象基类 class MuxerFactory { public: virtual ~MuxerFactory() = default; // 工厂方法 virtual std::unique_ptr createMuxer(MuxerType type) = 0; // 能力查询 virtual bool isTypeSupported(MuxerType type) const = 0; virtual std::vector getSupportedTypes() const = 0; virtual std::string getTypeName(MuxerType type) const; // 静态工厂方法 static std::unique_ptr createFactory(); }; } // namespace muxer } // namespace av #endif // AV_MUXER_ABSTRACT_MUXER_H