| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- #ifndef AV_MUXER_FILE_MUXER_H
- #define AV_MUXER_FILE_MUXER_H
- #include "muxer_abstract_muxer.h"
- #include <string>
- #include <memory>
- #include <thread>
- #include <queue>
- #include <condition_variable>
- #include <atomic>
- extern "C" {
- #include <libavformat/avformat.h>
- #include <libavcodec/avcodec.h>
- #include <libavutil/avutil.h>
- }
- namespace av {
- namespace muxer {
- // 文件复用器参数
- struct FileMuxerParams : public MuxerParams {
- std::string outputFile; // 输出文件路径
- bool overwrite = true; // 是否覆盖已存在文件
-
- // 文件特定选项
- int64_t maxFileSize = 0; // 最大文件大小(字节,0表示无限制)
- int maxDuration = 0; // 最大录制时长(秒,0表示无限制)
- bool enableSegmentation = false; // 是否启用分段
- int segmentDuration = 600; // 分段时长(秒)
- std::string segmentPattern; // 分段文件名模式
-
- // 质量控制
- bool enableFastStart = true; // 启用快速开始(moov atom前置)
- int movFlags = 0; // MOV标志
-
- // 同步选项
- bool syncMode = false; // 同步写入模式
- int flushInterval = 5; // 刷新间隔(秒)
-
- FileMuxerParams() : MuxerParams(MuxerType::FILE_MUXER) {}
- };
- // 包队列项
- struct PacketQueueItem {
- AVPacket* packet = nullptr;
- int streamIndex = -1;
- std::chrono::steady_clock::time_point timestamp;
-
- PacketQueueItem() = default;
- PacketQueueItem(AVPacket* pkt, int index)
- : packet(pkt), streamIndex(index), timestamp(std::chrono::steady_clock::now()) {}
-
- ~PacketQueueItem() {
- if (packet) {
- av_packet_free(&packet);
- }
- }
-
- // 禁用拷贝,只允许移动
- PacketQueueItem(const PacketQueueItem&) = delete;
- PacketQueueItem& operator=(const PacketQueueItem&) = delete;
-
- PacketQueueItem(PacketQueueItem&& other) noexcept
- : packet(other.packet), streamIndex(other.streamIndex), timestamp(other.timestamp) {
- other.packet = nullptr;
- }
-
- PacketQueueItem& operator=(PacketQueueItem&& other) noexcept {
- if (this != &other) {
- if (packet) {
- av_packet_free(&packet);
- }
- packet = other.packet;
- streamIndex = other.streamIndex;
- timestamp = other.timestamp;
- other.packet = nullptr;
- }
- return *this;
- }
- };
- // 文件复用器类
- class FileMuxer : public AbstractMuxer {
- public:
- FileMuxer();
- ~FileMuxer() override;
-
- // 基础接口实现
- ErrorCode initialize(const MuxerParams& params) override;
- ErrorCode start() override;
- ErrorCode stop() override;
- ErrorCode close() override;
-
- // 写入接口实现
- ErrorCode writePacket(AVPacket* packet) override;
- ErrorCode writeFrame(AVFrame* frame, int streamIndex) override;
- ErrorCode flush() override;
-
- // 流管理实现
- ErrorCode addStream(const StreamInfo& streamInfo) override;
-
- // 文件特定接口
- ErrorCode setOutputFile(const std::string& filename);
- std::string getOutputFile() const;
- int64_t getCurrentFileSize() const;
- double getCurrentDuration() const;
-
- // 分段控制
- ErrorCode enableSegmentation(bool enable, int duration = 600);
- ErrorCode forceNewSegment();
- std::vector<std::string> getSegmentFiles() const;
-
- // 质量控制
- ErrorCode setFastStart(bool enable);
- ErrorCode setMovFlags(int flags);
-
- // 工厂类
- class FileMuxerFactory : public MuxerFactory {
- public:
- std::unique_ptr<AbstractMuxer> createMuxer(MuxerType type) override;
- bool isTypeSupported(MuxerType type) const override;
- std::vector<MuxerType> getSupportedTypes() const override;
-
- // 文件复用器特定方法
- static std::unique_ptr<FileMuxer> createFileMuxer(const std::string& filename);
- static std::unique_ptr<FileMuxer> createSegmentedMuxer(const std::string& pattern, int duration);
- };
-
- protected:
- // 内部实现方法
- ErrorCode setupOutput() override;
- ErrorCode writeHeader() override;
- ErrorCode writeTrailer() override;
-
- // 文件操作
- ErrorCode openOutputFile();
- ErrorCode closeOutputFile();
- ErrorCode createNewSegment();
-
- // 流设置
- ErrorCode setupStreams();
- AVStream* createAVStream(const StreamInfo& streamInfo);
-
- // 内部方法
- ErrorCode processPacket(AVPacket* packet);
- ErrorCode writePacketInternal(AVPacket* packet);
- ErrorCode flushInternal(); // 内部刷新方法,假设已持有锁
-
- // 线程函数
- void writeThreadFunc();
-
- // 工具方法
- std::string generateSegmentFilename(int segmentIndex);
- bool shouldCreateNewSegment() const;
- ErrorCode validateFilePath(const std::string& path);
-
- // 参数验证
- bool validateParams(const MuxerParams& params) override;
-
- private:
- FileMuxerParams fileMuxerParams_;
-
- // 文件信息
- std::string currentOutputFile_;
- int64_t currentFileSize_ = 0;
- std::chrono::steady_clock::time_point fileStartTime_;
-
- // 分段相关
- bool segmentationEnabled_ = false;
- int currentSegmentIndex_ = 0;
- std::vector<std::string> segmentFiles_;
-
- // 写入线程
- std::thread writeThread_;
- std::atomic<bool> shouldStopWriting_{false};
-
- // 包队列
- std::queue<std::unique_ptr<PacketQueueItem>> packetQueue_;
- mutable std::mutex queueMutex_;
- std::condition_variable queueCondition_;
- static constexpr size_t MAX_QUEUE_SIZE = 100;
-
- // 同步控制
- mutable std::mutex fileMutex_;
- std::chrono::steady_clock::time_point lastFlushTime_;
-
- // 性能监控
- std::atomic<uint64_t> queuedPackets_{0};
- std::atomic<uint64_t> writtenPackets_{0};
- std::atomic<double> averageWriteTime_{0.0};
- };
- } // namespace muxer
- } // namespace av
- #endif // AV_MUXER_FILE_MUXER_H
|