TsMuxer.h 2.35 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
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.
9 10 11 12 13
 */

#ifndef TSMUXER_H
#define TSMUXER_H

14
#if defined(ENABLE_HLS)
15 16 17 18
#include <unordered_map>
#include "Extension/Frame.h"
#include "Extension/Track.h"
#include "Util/File.h"
xiongziliang committed
19
#include "Common/MediaSink.h"
xiongziliang committed
20
#include "Common/Stamp.h"
21 22 23
using namespace toolkit;
namespace mediakit {

xiongziliang committed
24
//该类用于产生MPEG-TS
xiongziliang committed
25
class TsMuxer : public MediaSinkInterface {
26 27 28
public:
    TsMuxer();
    virtual ~TsMuxer();
xiongziliang committed
29 30 31 32

    /**
     * 添加音视频轨道
     */
33
    void addTrack(const Track::Ptr &track) override;
xiongziliang committed
34 35 36 37

    /**
     * 重置音视频轨道
     */
38
    void resetTracks() override;
xiongziliang committed
39 40 41 42

    /**
     * 输入帧数据
     */
43
    void inputFrame(const Frame::Ptr &frame) override;
xiongziliang committed
44

45
protected:
xiongziliang committed
46 47 48 49 50 51 52
    /**
     * 输出mpegts数据回调
     * @param packet mpegts数据
     * @param bytes mpegts数据长度
     * @param timestamp 时间戳,单位毫秒
     * @param is_idr_fast_packet 是否为关键帧的第一个TS包,用于确保ts切片第一帧为关键帧
     */
53
    virtual void onTs(const void *packet, int bytes,uint32_t timestamp,bool is_idr_fast_packet) = 0;
xiongziliang committed
54

55 56 57
private:
    void init();
    void uninit();
xiongziliang committed
58 59 60
    //音视频时间戳同步用
    void stampSync();

61
private:
xiongziliang committed
62 63
    void *_context = nullptr;
    char _tsbuf[188];
64
    uint32_t _timestamp = 0;
xiongziliang committed
65
    struct track_info {
66 67 68
        int track_id = -1;
        Stamp stamp;
    };
xiongziliang committed
69
    unordered_map<int, track_info> _codec_to_trackid;
70
    List<Frame::Ptr> _frameCached;
71 72
    bool _is_idr_fast_packet = false;
    bool _have_video = false;
73 74 75
};

}//namespace mediakit
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

#else

#include "Common/MediaSink.h"

namespace mediakit {
class TsMuxer : public MediaSinkInterface {
public:
    TsMuxer() {}
    ~TsMuxer() override {}
    void addTrack(const Track::Ptr &track) override {}
    void resetTracks() override {}
    void inputFrame(const Frame::Ptr &frame) override {}

protected:
    virtual void onTs(const void *packet, int bytes,uint32_t timestamp,bool is_idr_fast_packet) = 0;
};
}//namespace mediakit

#endif// defined(ENABLE_HLS)

xiongziliang committed
97
#endif //TSMUXER_H