HlsMakerImp.cpp 4.97 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
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.
9 10
 */

11 12
#include <ctime>
#include <sys/stat.h>
13 14 15
#include "HlsMakerImp.h"
#include "Util/util.h"
#include "Util/uv_errno.h"
mtdxc committed
16 17
#include "Util/File.h"
#include "Common/config.h"
18

夏楚 committed
19
using namespace std;
20 21 22 23 24 25 26 27
using namespace toolkit;

namespace mediakit {

HlsMakerImp::HlsMakerImp(const string &m3u8_file,
                         const string &params,
                         uint32_t bufSize,
                         float seg_duration,
28 29
                         uint32_t seg_number,
                         bool seg_keep):HlsMaker(seg_duration, seg_number, seg_keep) {
30
    _poller = EventPollerPool::Instance().getPoller();
31 32 33 34
    _path_prefix = m3u8_file.substr(0, m3u8_file.rfind('/'));
    _path_hls = m3u8_file;
    _params = params;
    _buf_size = bufSize;
35
    _file_buf.reset(new char[bufSize], [](char *ptr) {
36 37
        delete[] ptr;
    });
38

39
    _info.folder = _path_prefix;
40 41 42
}

HlsMakerImp::~HlsMakerImp() {
43 44 45 46 47 48
    try {
        // 可能hls注册时导致抛异常
        clearCache(false, true);
    } catch (std::exception &ex) {
        WarnL << ex.what();
    }
49 50
}

51 52 53 54 55
void HlsMakerImp::clearCache() {
    clearCache(true, false);
}

void HlsMakerImp::clearCache(bool immediately, bool eof) {
xiongziliang committed
56
    //录制完了
57
    flushLastSegment(eof);
58
    if (!isLive()||isKeep()) {
59 60 61 62 63 64 65 66 67 68
        return;
    }

    clear();
    _file = nullptr;
    _segment_file_paths.clear();

    //hls直播才删除文件
    GET_CONFIG(uint32_t, delay, Hls::kDeleteDelaySec);
    if (!delay || immediately) {
xiongziliang committed
69
        File::delete_file(_path_prefix.data());
70 71 72 73 74 75
    } else {
        auto path_prefix = _path_prefix;
        _poller->doDelayTask(delay * 1000, [path_prefix]() {
            File::delete_file(path_prefix.data());
            return 0;
        });
xiongziliang committed
76
    }
77 78
}

79
string HlsMakerImp::onOpenSegment(uint64_t index) {
80
    string segment_name, segment_path;
81 82
    {
        auto strDate = getTimeStr("%Y-%m-%d");
83 84 85
        auto strHour = getTimeStr("%H");
        auto strTime = getTimeStr("%M-%S");
        segment_name = StrPrinter << strDate + "/" + strHour + "/" + strTime << "_" << index << ".ts";
86 87 88
        segment_path = _path_prefix + "/" + segment_name;
        if (isLive()) {
            _segment_file_paths.emplace(index, segment_path);
xiongziliang committed
89
        }
90 91
    }
    _file = makeFile(segment_path, true);
92

93 94 95 96 97
    //保存本切片的元数据
    _info.start_time = ::time(NULL);
    _info.file_name = segment_name;
    _info.file_path = segment_path;
    _info.url = _info.app + "/" + _info.stream + "/" + segment_name;
98

99 100
    if (!_file) {
        WarnL << "create file failed," << segment_path << " " << get_uv_errmsg();
101
    }
102
    if (_params.empty()) {
103
        return segment_name;
104
    }
105
    return segment_name + "?" + _params;
106 107
}

108
void HlsMakerImp::onDelSegment(uint64_t index) {
109
    auto it = _segment_file_paths.find(index);
110
    if (it == _segment_file_paths.end()) {
111 112 113 114
        return;
    }
    File::delete_file(it->second.data());
    _segment_file_paths.erase(it);
115 116
}

117
void HlsMakerImp::onWriteSegment(const char *data, size_t len) {
118 119 120
    if (_file) {
        fwrite(data, len, 1, _file.get());
    }
121 122 123
    if (_media_src) {
        _media_src->onSegmentSize(len);
    }
124 125
}

126
void HlsMakerImp::onWriteHls(const std::string &data) {
127
    auto hls = makeFile(_path_hls);
128
    if (hls) {
129
        fwrite(data.data(), data.size(), 1, hls.get());
130
        hls.reset();
131
        if (_media_src) {
132
            _media_src->setIndexFile(data);
133
        }
134 135
    } else {
        WarnL << "create hls file failed," << _path_hls << " " << get_uv_errmsg();
136 137 138 139
    }
    //DebugL << "\r\n"  << string(data,len);
}

ziyue committed
140
void HlsMakerImp::onFlushLastSegment(uint64_t duration_ms) {
141 142 143
    //关闭并flush文件到磁盘
    _file = nullptr;

144 145
    GET_CONFIG(bool, broadcastRecordTs, Hls::kBroadcastRecordTs);
    if (broadcastRecordTs) {
146
        _info.time_len = duration_ms / 1000.0f;
147
        _info.file_size = File::fileSize(_info.file_path.data());
148
        NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordTs, _info);
149
    }
150 151
}

152
std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file, bool setbuf) {
153
    auto file_buf = _file_buf;
154
    auto ret = shared_ptr<FILE>(File::create_file(file.data(), "wb"), [file_buf](FILE *fp) {
155 156 157 158
        if (fp) {
            fclose(fp);
        }
    });
159
    if (ret && setbuf) {
160 161 162 163 164
        setvbuf(ret.get(), _file_buf.get(), _IOFBF, _buf_size);
    }
    return ret;
}

165
void HlsMakerImp::setMediaSource(const string &vhost, const string &app, const string &stream_id) {
166
    _media_src = std::make_shared<HlsMediaSource>(vhost, app, stream_id);
167 168 169
    _info.app = app;
    _info.stream = stream_id;
    _info.vhost = vhost;
170 171
}

172
HlsMediaSource::Ptr HlsMakerImp::getMediaSource() const {
173 174 175
    return _media_src;
}

176
}//namespace mediakit