2019-08-08 19:01:45 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2019-04-01 12:57:33 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* 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.
|
2019-04-01 12:57:33 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2020-09-13 14:08:05 +08:00
|
|
|
|
#include <ctime>
|
|
|
|
|
#include <sys/stat.h>
|
2019-04-01 12:57:33 +08:00
|
|
|
|
#include "HlsMakerImp.h"
|
2020-09-13 14:08:05 +08:00
|
|
|
|
#include "Thread/WorkThreadPool.h"
|
2019-04-01 12:57:33 +08:00
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
#include "Util/uv_errno.h"
|
2020-09-12 19:20:18 +08:00
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
|
|
|
HlsMakerImp::HlsMakerImp(const string &m3u8_file,
|
|
|
|
|
const string ¶ms,
|
|
|
|
|
uint32_t bufSize,
|
|
|
|
|
float seg_duration,
|
|
|
|
|
uint32_t seg_number) : HlsMaker(seg_duration, seg_number) {
|
|
|
|
|
_path_prefix = m3u8_file.substr(0, m3u8_file.rfind('/'));
|
|
|
|
|
_path_hls = m3u8_file;
|
|
|
|
|
_params = params;
|
|
|
|
|
_buf_size = bufSize;
|
2020-09-12 19:20:18 +08:00
|
|
|
|
_file_buf.reset(new char[bufSize], [](char *ptr) {
|
2019-04-01 12:57:33 +08:00
|
|
|
|
delete[] ptr;
|
|
|
|
|
});
|
2020-09-13 14:08:05 +08:00
|
|
|
|
|
|
|
|
|
_info.strFolder = _path_prefix;
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HlsMakerImp::~HlsMakerImp() {
|
2020-09-12 19:20:18 +08:00
|
|
|
|
clearCache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsMakerImp::clearCache() {
|
2019-09-26 14:21:20 +08:00
|
|
|
|
//录制完了
|
2019-10-12 10:29:40 +08:00
|
|
|
|
flushLastSegment(true);
|
2020-09-12 19:20:18 +08:00
|
|
|
|
if (isLive()) {
|
2019-09-26 14:21:20 +08:00
|
|
|
|
//hls直播才删除文件
|
2020-09-12 19:20:18 +08:00
|
|
|
|
clear();
|
|
|
|
|
_file = nullptr;
|
|
|
|
|
_segment_file_paths.clear();
|
2019-09-26 14:21:20 +08:00
|
|
|
|
File::delete_file(_path_prefix.data());
|
|
|
|
|
}
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 10:29:40 +08:00
|
|
|
|
string HlsMakerImp::onOpenSegment(int index) {
|
2020-09-12 19:20:18 +08:00
|
|
|
|
string segment_name, segment_path;
|
2019-12-12 22:58:31 +08:00
|
|
|
|
{
|
|
|
|
|
auto strDate = getTimeStr("%Y-%m-%d");
|
2020-01-02 12:47:12 +08:00
|
|
|
|
auto strHour = getTimeStr("%H");
|
|
|
|
|
auto strTime = getTimeStr("%M-%S");
|
|
|
|
|
segment_name = StrPrinter << strDate + "/" + strHour + "/" + strTime << "_" << index << ".ts";
|
2020-09-12 19:20:18 +08:00
|
|
|
|
segment_path = _path_prefix + "/" + segment_name;
|
|
|
|
|
if (isLive()) {
|
|
|
|
|
_segment_file_paths.emplace(index, segment_path);
|
2019-12-17 09:18:11 +08:00
|
|
|
|
}
|
2019-12-12 22:58:31 +08:00
|
|
|
|
}
|
|
|
|
|
_file = makeFile(segment_path, true);
|
2020-09-13 14:08:05 +08:00
|
|
|
|
|
|
|
|
|
_info.ui64StartedTime = ::time(NULL);
|
|
|
|
|
_info.strFileName = segment_name;
|
|
|
|
|
_info.strFilePath = segment_path;
|
|
|
|
|
_info.strUrl = _info.strAppName + "/" + _info.strStreamId + "/" + segment_name;
|
|
|
|
|
|
2020-09-12 19:20:18 +08:00
|
|
|
|
if (!_file) {
|
|
|
|
|
WarnL << "create file failed," << segment_path << " " << get_uv_errmsg();
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
if (_params.empty()) {
|
2020-09-13 11:07:19 +08:00
|
|
|
|
return segment_name;
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}
|
2020-09-13 11:07:19 +08:00
|
|
|
|
return segment_name + "?" + _params;
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 10:29:40 +08:00
|
|
|
|
void HlsMakerImp::onDelSegment(int index) {
|
2019-12-12 22:58:31 +08:00
|
|
|
|
auto it = _segment_file_paths.find(index);
|
2020-09-12 19:20:18 +08:00
|
|
|
|
if (it == _segment_file_paths.end()) {
|
2019-12-12 22:58:31 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
File::delete_file(it->second.data());
|
|
|
|
|
_segment_file_paths.erase(it);
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 10:29:40 +08:00
|
|
|
|
void HlsMakerImp::onWriteSegment(const char *data, int len) {
|
2019-04-01 12:57:33 +08:00
|
|
|
|
if (_file) {
|
|
|
|
|
fwrite(data, len, 1, _file.get());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsMakerImp::onWriteHls(const char *data, int len) {
|
|
|
|
|
auto hls = makeFile(_path_hls);
|
2020-09-12 19:20:18 +08:00
|
|
|
|
if (hls) {
|
|
|
|
|
fwrite(data, len, 1, hls.get());
|
2019-04-01 12:57:33 +08:00
|
|
|
|
hls.reset();
|
2020-09-12 19:20:18 +08:00
|
|
|
|
if (_media_src) {
|
2020-09-12 20:42:58 +08:00
|
|
|
|
_media_src->registHls(true);
|
2019-12-28 18:50:56 +08:00
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
} else {
|
|
|
|
|
WarnL << "create hls file failed," << _path_hls << " " << get_uv_errmsg();
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}
|
|
|
|
|
//DebugL << "\r\n" << string(data,len);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 14:08:05 +08:00
|
|
|
|
void HlsMakerImp::onFlushLastSegment(uint32_t duration) {
|
|
|
|
|
auto info = _info;
|
|
|
|
|
info.ui64TimeLen = duration;
|
|
|
|
|
WorkThreadPool::Instance().getExecutor()->async([info]() {
|
|
|
|
|
struct stat fileData;
|
|
|
|
|
stat(info.strFilePath.data(), &fileData);
|
|
|
|
|
const_cast<TsInfo&>(info).ui64FileSize = fileData.st_size;
|
|
|
|
|
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordTs, info);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
|
2020-09-12 19:20:18 +08:00
|
|
|
|
std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file, bool setbuf) {
|
2019-12-27 15:36:54 +08:00
|
|
|
|
auto file_buf = _file_buf;
|
2020-09-12 19:20:18 +08:00
|
|
|
|
auto ret = shared_ptr<FILE>(File::create_file(file.data(), "wb"), [file_buf](FILE *fp) {
|
2019-04-01 12:57:33 +08:00
|
|
|
|
if (fp) {
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-09-12 19:20:18 +08:00
|
|
|
|
if (ret && setbuf) {
|
2019-04-01 12:57:33 +08:00
|
|
|
|
setvbuf(ret.get(), _file_buf.get(), _IOFBF, _buf_size);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 10:49:04 +08:00
|
|
|
|
void HlsMakerImp::setMediaSource(const string &vhost, const string &app, const string &stream_id) {
|
2019-12-28 18:50:56 +08:00
|
|
|
|
_media_src = std::make_shared<HlsMediaSource>(vhost, app, stream_id);
|
2020-09-13 14:08:05 +08:00
|
|
|
|
_info.strAppName = app;
|
|
|
|
|
_info.strStreamId = stream_id;
|
|
|
|
|
_info.strVhost = vhost;
|
2019-12-28 18:50:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 19:20:18 +08:00
|
|
|
|
HlsMediaSource::Ptr HlsMakerImp::getMediaSource() const {
|
2019-12-29 10:49:04 +08:00
|
|
|
|
return _media_src;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}//namespace mediakit
|