MP4Muxer.h 3.04 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
xiongziliang committed
9 10 11 12 13
 */

#ifndef ZLMEDIAKIT_MP4MUXER_H
#define ZLMEDIAKIT_MP4MUXER_H

xiongziliang committed
14
#ifdef ENABLE_MP4
xiongziliang committed
15 16 17

#include "Common/MediaSink.h"
#include "Extension/AAC.h"
18
#include "Extension/G711.h"
xiongziliang committed
19 20
#include "Extension/H264.h"
#include "Extension/H265.h"
xiongziliang committed
21
#include "Common/Stamp.h"
xiongziliang committed
22
#include "MP4.h"
xiongziliang committed
23 24 25

namespace mediakit{

26
class MP4MuxerInterface : public MediaSinkInterface {
xiongziliang committed
27
public:
28 29
    MP4MuxerInterface() = default;
    ~MP4MuxerInterface() override = default;
xiongziliang committed
30 31 32 33

    /**
     * 添加已经ready状态的track
     */
34 35
    void addTrack(const Track::Ptr &track) override;

xiongziliang committed
36
    /**
xiongziliang committed
37
     * 输入帧
xiongziliang committed
38
     */
xiongziliang committed
39
    void inputFrame(const Frame::Ptr &frame) override;
xiongziliang committed
40 41

    /**
xiongziliang committed
42
     * 重置所有track
xiongziliang committed
43
     */
44
    void resetTracks() override;
xiongziliang committed
45

46
    /**
47
     * 是否包含视频
48
     */
49
    bool haveVideo() const;
50 51

    /**
52
     * 保存fmp4分片
53
     */
54 55 56 57 58 59 60 61 62
    void saveSegment();

    /**
     * 创建新切片
     */
    void initSegment();

protected:
    virtual MP4FileIO::Writer createWriter() = 0;
63

xiongziliang committed
64
private:
xiongziliang committed
65
    void stampSync();
xiongziliang committed
66

xiongziliang committed
67
private:
68 69 70
    bool _started = false;
    bool _have_video = false;
    MP4FileIO::Writer _mov_writter;
xiongziliang committed
71
    struct track_info {
72
        int track_id = -1;
73
        Stamp stamp;
74
    };
75
    List<Frame::Ptr> _frameCached;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    unordered_map<int, track_info> _codec_to_trackid;
};

class MP4Muxer : public MP4MuxerInterface{
public:
    typedef std::shared_ptr<MP4Muxer> Ptr;

    MP4Muxer();
    ~MP4Muxer() override;

    /**
     * 重置所有track
     */
    void resetTracks() override;

    /**
     * 打开mp4
     * @param file 文件完整路径
     */
    void openMP4(const string &file);

    /**
     * 手动关闭文件(对象析构时会自动关闭)
     */
    void closeMP4();

protected:
    MP4FileIO::Writer createWriter() override;

private:
xiongziliang committed
106
    string _file_name;
107
    MP4FileDisk::Ptr _mp4_file;
xiongziliang committed
108 109
};

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
class MP4MuxerMemory : public MP4MuxerInterface{
public:
    MP4MuxerMemory();
    ~MP4MuxerMemory() override = default;

    /**
     * 重置所有track
     */
    void resetTracks() override;

    /**
     * 输入帧
     */
    void inputFrame(const Frame::Ptr &frame) override;

    /**
     * 获取fmp4 init segment
     */
    const string &getInitSegment();

protected:
    /**
     * 输出fmp4切片回调函数
     * @param string 切片内容
     * @param stamp 切片末尾时间戳
     * @param key_frame 是否有关键帧
     */
    virtual void onSegmentData(const string &string, uint32_t stamp, bool key_frame) = 0;

protected:
    MP4FileIO::Writer createWriter() override;

private:
    bool _key_frame = false;
    Ticker _ticker;
    string _init_segment;
    MP4FileMemory::Ptr _memory_file;
};


xiongziliang committed
150
}//namespace mediakit
xiongziliang committed
151
#endif//#ifdef ENABLE_MP4
xiongziliang committed
152
#endif //ZLMEDIAKIT_MP4MUXER_H