| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- #ifndef AV_MUXER_ABSTRACT_MUXER_H
- #define AV_MUXER_ABSTRACT_MUXER_H
- #include "../base/media_common.h"
- #include "../base/logger.h"
- #include <string>
- #include <vector>
- #include <memory>
- #include <functional>
- #include <atomic>
- #include <mutex>
- #include <chrono>
- #include <map>
- extern "C" {
- #include <libavformat/avformat.h>
- #include <libavcodec/avcodec.h>
- #include <libavutil/avutil.h>
- #include <libavutil/channel_layout.h>
- }
- 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<StreamInfo> streams; // 流信息列表
-
- // 通用选项
- std::map<std::string, std::string> options; // 格式选项
- std::map<std::string, std::string> 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<int, uint64_t> streamPackets; // 各流包数
- std::map<int, uint64_t> 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<StreamInfo> 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<void(ErrorCode, const std::string&)>;
- using ProgressCallback = std::function<void(double)>; // 进度百分比
- using StatsCallback = std::function<void(const MuxerStats&)>;
-
- void setErrorCallback(ErrorCallback callback) { errorCallback_ = callback; }
- void setProgressCallback(ProgressCallback callback) { progressCallback_ = callback; }
- void setStatsCallback(StatsCallback callback) { statsCallback_ = callback; }
-
- // 静态工具方法
- static std::vector<std::string> getSupportedFormats();
- static std::vector<std::string> 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<MuxerState> state_{MuxerState::IDLE};
- mutable std::mutex statsMutex_;
- mutable std::mutex streamsMutex_;
-
- MuxerStats stats_;
- std::vector<StreamInfo> streams_;
- std::string lastError_;
-
- // 回调函数
- ErrorCallback errorCallback_;
- ProgressCallback progressCallback_;
- StatsCallback statsCallback_;
-
- // FFmpeg相关
- AVFormatContext* formatCtx_ = nullptr;
- std::map<int, AVStream*> 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<AbstractMuxer> createMuxer(MuxerType type) = 0;
-
- // 能力查询
- virtual bool isTypeSupported(MuxerType type) const = 0;
- virtual std::vector<MuxerType> getSupportedTypes() const = 0;
- virtual std::string getTypeName(MuxerType type) const;
-
- // 静态工厂方法
- static std::unique_ptr<MuxerFactory> createFactory();
- };
- } // namespace muxer
- } // namespace av
- #endif // AV_MUXER_ABSTRACT_MUXER_H
|