HlsMaker.cpp 4.46 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
xzl committed
25 26
 */

27
#include "HlsMaker.h"
xiongziliang committed
28
namespace mediakit {
xzl committed
29

30
HlsMaker::HlsMaker(float seg_duration, uint32_t seg_number) {
xiongziliang committed
31 32
    //最小允许设置为0,0个切片代表点播
    seg_number = MAX(0,seg_number);
33 34 35
    seg_duration = MAX(1,seg_duration);
    _seg_number = seg_number;
    _seg_duration = seg_duration;
xzl committed
36 37
}

38
HlsMaker::~HlsMaker() {
xzl committed
39 40
}

41 42

void HlsMaker::makeIndexFile(bool eof) {
xiongziliang committed
43
    char file_content[1024];
xiongziliang committed
44
    int maxSegmentDuration = 0;
45

46 47 48
    for (auto &tp : _seg_dur_list) {
        int dur = std::get<0>(tp);
        if (dur > maxSegmentDuration) {
xiongziliang committed
49 50 51
            maxSegmentDuration = dur;
        }
    }
xiongziliang committed
52 53 54 55

    string m3u8;
    snprintf(file_content,sizeof(file_content),
          "#EXTM3U\n"
56 57 58 59 60
          "#EXT-X-VERSION:3\n"
          "#EXT-X-ALLOW-CACHE:NO\n"
          "#EXT-X-TARGETDURATION:%u\n"
          "#EXT-X-MEDIA-SEQUENCE:%llu\n",
          (maxSegmentDuration + 999) / 1000,
61
          _seg_number ? _file_index : 0);
62

xiongziliang committed
63 64
    m3u8.assign(file_content);

65
    for (auto &tp : _seg_dur_list) {
xiongziliang committed
66 67
        snprintf(file_content,sizeof(file_content), "#EXTINF:%.3f,\n%s\n", std::get<0>(tp) / 1000.0, std::get<1>(tp).data());
        m3u8.append(file_content);
xiongziliang committed
68
    }
69

70
    if (eof) {
xiongziliang committed
71 72
        snprintf(file_content,sizeof(file_content),"#EXT-X-ENDLIST\n");
        m3u8.append(file_content);
73
    }
xiongziliang committed
74
    onWriteHls(m3u8.data(), m3u8.size());
xzl committed
75 76 77
}


78
void HlsMaker::inputData(void *data, uint32_t len, uint32_t timestamp) {
79 80
    //分片数据中断结束
    if (data && len) {
xiongziliang committed
81 82 83 84
        addNewSegment(timestamp);
        onWriteSegment((char *) data, len);
        //记录上次写入数据时间
        _ticker_last_data.resetTime();
xiongziliang committed
85
    } else {
xiongziliang committed
86
        flushLastSegment(true);
87
    }
xzl committed
88 89
}

xiongziliang committed
90
void HlsMaker::delOldSegment() {
xiongziliang committed
91 92 93 94
    if(_seg_number == 0){
        //如果设置为保留0个切片,则认为是保存为点播
        return;
    }
95
    //在hls m3u8索引文件中,我们保存的切片个数跟_seg_number相关设置一致
96
    if (_file_index > _seg_number) {
97 98
        _seg_dur_list.pop_front();
    }
99

100 101 102 103
    GET_CONFIG(uint32_t,segRetain,Hls::kSegmentRetain);
    //但是实际保存的切片个数比m3u8所述多若干个,这样做的目的是防止播放器在切片删除前能下载完毕
    if (_file_index > _seg_number + segRetain) {
        onDelSegment(_file_index - _seg_number - segRetain - 1);
104
    }
105 106
}

xiongziliang committed
107 108 109 110
void HlsMaker::addNewSegment(uint32_t) {
    if(!_last_file_name.empty() && _ticker.elapsedTime() < _seg_duration * 1000){
        //存在上个切片,并且未到分片时间
        return;
111
    }
xiongziliang committed
112

113 114
    //关闭并保存上一个切片,如果_seg_number==0,那么是点播。
    flushLastSegment(_seg_number == 0);
xiongziliang committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    //新增切片
    _last_file_name = onOpenSegment(_file_index++);
    //重置切片计时器
    _ticker.resetTime();
}

void HlsMaker::flushLastSegment(bool eof){
    if(_last_file_name.empty()){
        //不存在上个切片
        return;
    }
    //文件创建到最后一次数据写入的时间即为切片长度
    auto seg_dur = _ticker.elapsedTime() - _ticker_last_data.elapsedTime();
    if(seg_dur <= 0){
        seg_dur = 100;
130
    }
xiongziliang committed
131 132 133 134
    _seg_dur_list.push_back(std::make_tuple(seg_dur, _last_file_name));
    delOldSegment();
    makeIndexFile(eof);
    _last_file_name.clear();
135
}
xzl committed
136

xiongziliang committed
137 138 139 140
bool HlsMaker::isLive() {
    return _seg_number != 0;
}

141
}//namespace mediakit