HlsMaker.h 2.74 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.
xzl committed
9 10
 */

11 12
#ifndef HLSMAKER_H
#define HLSMAKER_H
xzl committed
13

14
#include <deque>
15 16
#include <tuple>
#include "Common/config.h"
xzl committed
17 18 19 20
#include "Util/TimeTicker.h"
#include "Util/File.h"
#include "Util/util.h"
#include "Util/logger.h"
xiongziliang committed
21
using namespace toolkit;
xiongzilaing committed
22

xiongziliang committed
23
namespace mediakit {
xzl committed
24

25
class HlsMaker {
xzl committed
26
public:
27 28 29 30 31 32
    /**
     * @param seg_duration 切片文件长度
     * @param seg_number 切片个数
     */
    HlsMaker(float seg_duration = 5, uint32_t seg_number = 3);
    virtual ~HlsMaker();
xzl committed
33

34 35 36 37 38
    /**
     * 写入ts数据
     * @param data 数据
     * @param len 数据长度
     * @param timestamp 毫秒时间戳
39
     * @param is_idr_fast_packet 是否为关键帧第一个包
40
     */
41
    void inputData(void *data, size_t len, uint32_t timestamp, bool is_idr_fast_packet);
42 43 44 45 46 47 48 49 50 51 52

    /**
     * 是否为直播
     */
    bool isLive();

    /**
     * 清空记录
     */
    void clear();

53
protected:
54 55 56 57
    /**
     * 创建ts切片文件回调
     * @param index
     * @return
58
     */
59
    virtual string onOpenSegment(uint64_t index) = 0;
60

61 62 63 64
    /**
     * 删除ts切片文件回调
     * @param index
     */
65
    virtual void onDelSegment(uint64_t index) = 0;
xzl committed
66

67 68 69 70 71
    /**
     * 写ts切片文件回调
     * @param data
     * @param len
     */
72
    virtual void onWriteSegment(const char *data, size_t len) = 0;
73

74 75 76 77 78
    /**
     * 写m3u8文件回调
     * @param data
     * @param len
     */
79
    virtual void onWriteHls(const char *data, size_t len) = 0;
xiongziliang committed
80 81

    /**
82 83
     * 上一个 ts 切片写入完成, 可在这里进行通知处理
     * @param duration_ms 上一个 ts 切片的时长, 单位为毫秒
xiongziliang committed
84
     */
85
    virtual void onFlushLastSegment(uint32_t duration_ms) {};
xiongziliang committed
86

87
    /**
88 89
     * 关闭上个ts切片并且写入m3u8索引
     * @param eof HLS直播是否已结束
90
     */
91
    void flushLastSegment(bool eof);
92

xiongziliang committed
93 94
private:
    /**
xiongziliang committed
95 96 97 98
     * 生成m3u8文件
     * @param eof true代表点播
     */
    void makeIndexFile(bool eof = false);
xiongziliang committed
99 100 101 102 103 104 105 106 107 108 109

    /**
     * 删除旧的ts切片
     */
    void delOldSegment();

    /**
     * 添加新的ts切片
     * @param timestamp
     */
    void addNewSegment(uint32_t timestamp);
110

111 112
private:
    float _seg_duration = 0;
113
    uint32_t _seg_number = 0;
114
    uint32_t _last_timestamp = 0;
115
    uint32_t _last_seg_timestamp = 0;
116 117 118
    uint64_t _file_index = 0;
    string _last_file_name;
    std::deque<tuple<int,string> > _seg_dur_list;
xzl committed
119 120
};

121 122
}//namespace mediakit
#endif //HLSMAKER_H