muxer_abstract_muxer.h 7.4 KB

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