HlsMaker.cpp 3.35 KB
Newer Older
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 31 32 33 34
HlsMaker::HlsMaker(float seg_duration, uint32_t seg_number) {
    seg_number = MAX(1,seg_number);
    seg_duration = MAX(1,seg_duration);
    _seg_number = seg_number;
    _seg_duration = seg_duration;
xzl committed
35 36
}

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

40 41 42 43 44
#define PRINT(...)  file_size += snprintf(file_content + file_size,sizeof(file_content) - file_size, ##__VA_ARGS__)

void HlsMaker::makeIndexFile(bool eof) {
    char file_content[4 * 1024];
    int file_size = 0;
xiongziliang committed
45

xiongziliang committed
46
    int maxSegmentDuration = 0;
47 48 49
    for (auto &tp : _seg_dur_list) {
        int dur = std::get<0>(tp);
        if (dur > maxSegmentDuration) {
xiongziliang committed
50 51 52
            maxSegmentDuration = dur;
        }
    }
53 54 55 56 57 58 59 60 61 62
    PRINT("#EXTM3U\n"
          "#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,
          _file_index);

    for (auto &tp : _seg_dur_list) {
        PRINT("#EXTINF:%.3f,\n%s\n", std::get<0>(tp) / 1000.0, std::get<1>(tp).data());
xiongziliang committed
63
    }
64

65 66 67 68
    if (eof) {
        PRINT("#EXT-X-ENDLIST\n");
    }
    onWriteHls(file_content, file_size);
xzl committed
69 70 71
}


72 73 74
void HlsMaker::inputData(void *data, uint32_t len, uint32_t timestamp) {
    addNewFile(timestamp);
    onWriteFile((char *) data, len);
xzl committed
75 76
}

77 78 79 80 81
void HlsMaker::delOldFile() {
    //在hls m3u8索引文件中,我们保存的切片个数跟_seg_number相关设置一致
    if (_file_index >= _seg_number + 2) {
        _seg_dur_list.pop_front();
    }
82

83 84 85 86
    //但是实际保存的切片个数比m3u8所述多两个,这样做的目的是防止播放器在切片删除前能下载完毕
    if (_file_index >= _seg_number + 4) {
        onDelFile(_file_index - _seg_number - 4);
    }
87 88
}

89 90
void HlsMaker::addNewFile(uint32_t) {
    int stampInc = _ticker.elapsedTime();
91
    if (stampInc >= _seg_duration * 1000) {
92
        _ticker.resetTime();
93 94 95 96 97 98 99 100 101
        auto file_name = onOpenFile(_file_index);
        if (_file_index++ > 0) {
            _seg_dur_list.push_back(std::make_tuple(stampInc, _last_file_name));
            delOldFile();
            makeIndexFile();
        }
        _last_file_name = file_name;
    }
}
xzl committed
102

103
}//namespace mediakit