muxer_abstract_muxer.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #ifndef AV_MUXER_ABSTRACT_MUXER_H
  2. #define AV_MUXER_ABSTRACT_MUXER_H
  3. #include "../base/media_common.h"
  4. #include "../base/logger.h"
  5. #include <string>
  6. #include <vector>
  7. #include <memory>
  8. #include <functional>
  9. #include <atomic>
  10. #include <mutex>
  11. #include <chrono>
  12. #include <map>
  13. extern "C" {
  14. #include <libavformat/avformat.h>
  15. #include <libavcodec/avcodec.h>
  16. #include <libavutil/avutil.h>
  17. }
  18. namespace av {
  19. namespace muxer {
  20. // 复用器状态
  21. enum class MuxerState {
  22. IDLE = 0, // 空闲状态
  23. INITIALIZED, // 已初始化
  24. STARTED, // 已启动
  25. PAUSED, // 已暂停
  26. STOPPED, // 已停止
  27. ERROR_STATE // 错误状态
  28. };
  29. // 复用器类型
  30. enum class MuxerType {
  31. FILE_MUXER = 0, // 文件复用器
  32. STREAM_MUXER, // 流复用器
  33. RTMP_MUXER, // RTMP推流
  34. UDP_MUXER, // UDP推流
  35. TCP_MUXER, // TCP推流
  36. HLS_MUXER, // HLS分片
  37. DASH_MUXER // DASH分片
  38. };
  39. // 流类型
  40. enum class StreamType {
  41. VIDEO = 0,
  42. AUDIO,
  43. SUBTITLE,
  44. DATA
  45. };
  46. // 流信息
  47. struct StreamInfo {
  48. int index = -1; // 流索引
  49. StreamType type = StreamType::VIDEO; // 流类型
  50. AVCodecID codecId = AV_CODEC_ID_NONE; // 编解码器ID
  51. std::string codecName; // 编解码器名称
  52. // 视频流参数
  53. int width = 0;
  54. int height = 0;
  55. AVRational frameRate = {0, 1};
  56. AVPixelFormat pixelFormat = AV_PIX_FMT_NONE;
  57. int64_t bitrate = 0;
  58. // 音频流参数
  59. int sampleRate = 0;
  60. int channels = 0;
  61. AVSampleFormat sampleFormat = AV_SAMPLE_FMT_NONE;
  62. uint64_t channelLayout = 0;
  63. // 通用参数
  64. AVRational timeBase = {1, 1000};
  65. std::string metadata; // 元数据
  66. bool isDefault = false; // 是否为默认流
  67. };
  68. // 复用器参数基类
  69. struct MuxerParams {
  70. MuxerType type = MuxerType::FILE_MUXER;
  71. MediaType mediaType = MediaType::VIDEO;
  72. std::string outputPath; // 输出路径或URL
  73. std::string format; // 输出格式 (mp4, flv, ts等)
  74. // 流配置
  75. std::vector<StreamInfo> streams; // 流信息列表
  76. // 通用选项
  77. std::map<std::string, std::string> options; // 格式选项
  78. std::map<std::string, std::string> metadata; // 元数据
  79. // 性能参数
  80. int bufferSize = 1024 * 1024; // 缓冲区大小
  81. int maxDelay = 500000; // 最大延迟(微秒)
  82. bool lowLatency = false; // 低延迟模式
  83. // 错误处理
  84. int maxRetries = 3; // 最大重试次数
  85. int retryDelay = 1000; // 重试延迟(毫秒)
  86. MuxerParams(MuxerType t) : type(t) {}
  87. virtual ~MuxerParams() = default;
  88. };
  89. // 复用器统计信息
  90. struct MuxerStats {
  91. // 基础统计
  92. uint64_t totalPackets = 0; // 总包数
  93. uint64_t totalBytes = 0; // 总字节数
  94. uint64_t droppedPackets = 0; // 丢弃包数
  95. uint64_t errorCount = 0; // 错误计数
  96. // 性能统计
  97. double averageWriteTime = 0.0; // 平均写入时间(毫秒)
  98. double currentBitrate = 0.0; // 当前码率(bps)
  99. double averageBitrate = 0.0; // 平均码率(bps)
  100. // 时间统计
  101. std::chrono::steady_clock::time_point startTime;
  102. std::chrono::steady_clock::time_point lastPacketTime;
  103. double duration = 0.0; // 持续时间(秒)
  104. // 流统计
  105. std::map<int, uint64_t> streamPackets; // 各流包数
  106. std::map<int, uint64_t> streamBytes; // 各流字节数
  107. };
  108. // 抽象复用器基类
  109. class AbstractMuxer {
  110. public:
  111. AbstractMuxer();
  112. virtual ~AbstractMuxer();
  113. // 基础接口
  114. virtual ErrorCode initialize(const MuxerParams& params);
  115. virtual ErrorCode start();
  116. virtual ErrorCode stop();
  117. virtual ErrorCode pause();
  118. virtual ErrorCode resume();
  119. virtual ErrorCode reset();
  120. virtual ErrorCode close();
  121. // 写入接口
  122. virtual ErrorCode writePacket(AVPacket* packet);
  123. virtual ErrorCode writeFrame(AVFrame* frame, int streamIndex);
  124. virtual ErrorCode flush();
  125. // 流管理
  126. virtual ErrorCode addStream(const StreamInfo& streamInfo);
  127. virtual ErrorCode removeStream(int streamIndex);
  128. virtual std::vector<StreamInfo> getStreams() const;
  129. virtual StreamInfo getStreamInfo(int streamIndex) const;
  130. // 状态查询
  131. MuxerState getState() const { return state_.load(); }
  132. bool isRunning() const { return getState() == MuxerState::STARTED; }
  133. bool isPaused() const { return getState() == MuxerState::PAUSED; }
  134. // 统计信息
  135. MuxerStats getStats() const;
  136. void resetStats();
  137. // 错误处理
  138. std::string getLastError() const;
  139. // 回调设置
  140. using ErrorCallback = std::function<void(ErrorCode, const std::string&)>;
  141. using ProgressCallback = std::function<void(double)>; // 进度百分比
  142. using StatsCallback = std::function<void(const MuxerStats&)>;
  143. void setErrorCallback(ErrorCallback callback) { errorCallback_ = callback; }
  144. void setProgressCallback(ProgressCallback callback) { progressCallback_ = callback; }
  145. void setStatsCallback(StatsCallback callback) { statsCallback_ = callback; }
  146. // 静态工具方法
  147. static std::vector<std::string> getSupportedFormats();
  148. static std::vector<std::string> getSupportedCodecs(const std::string& format);
  149. static bool isFormatSupported(const std::string& format);
  150. static std::string getFormatFromExtension(const std::string& filename);
  151. static std::string getDefaultExtension(const std::string& format);
  152. protected:
  153. // 状态管理
  154. void setState(MuxerState state);
  155. // 错误处理
  156. void onError(ErrorCode code, const std::string& message);
  157. // 统计更新
  158. void updateStats(AVPacket* packet);
  159. void updateBitrate();
  160. // 参数验证
  161. virtual bool validateParams(const MuxerParams& params);
  162. // 内部实现方法
  163. virtual ErrorCode setupOutput();
  164. virtual ErrorCode writeHeader();
  165. virtual ErrorCode writeTrailer();
  166. // 成员变量
  167. std::atomic<MuxerState> state_{MuxerState::IDLE};
  168. mutable std::mutex statsMutex_;
  169. mutable std::mutex streamsMutex_;
  170. MuxerStats stats_;
  171. std::vector<StreamInfo> streams_;
  172. std::string lastError_;
  173. // 回调函数
  174. ErrorCallback errorCallback_;
  175. ProgressCallback progressCallback_;
  176. StatsCallback statsCallback_;
  177. // FFmpeg相关
  178. AVFormatContext* formatCtx_ = nullptr;
  179. std::map<int, AVStream*> streamMap_; // 流索引映射
  180. // 性能监控
  181. std::chrono::steady_clock::time_point lastStatsUpdate_;
  182. static constexpr double STATS_UPDATE_INTERVAL = 1.0; // 统计更新间隔(秒)
  183. };
  184. // 复用器工厂抽象基类
  185. class MuxerFactory {
  186. public:
  187. virtual ~MuxerFactory() = default;
  188. // 工厂方法
  189. virtual std::unique_ptr<AbstractMuxer> createMuxer(MuxerType type) = 0;
  190. // 能力查询
  191. virtual bool isTypeSupported(MuxerType type) const = 0;
  192. virtual std::vector<MuxerType> getSupportedTypes() const = 0;
  193. virtual std::string getTypeName(MuxerType type) const;
  194. // 静态工厂方法
  195. static std::unique_ptr<MuxerFactory> createFactory();
  196. };
  197. } // namespace muxer
  198. } // namespace av
  199. #endif // AV_MUXER_ABSTRACT_MUXER_H