2019-08-08 19:01:45 +08:00
|
|
|
|
/*
|
2019-04-01 12:57:33 +08:00
|
|
|
|
* MIT License
|
|
|
|
|
*
|
2019-05-08 15:40:07 +08:00
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
2019-04-01 12:57:33 +08:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "HlsMakerImp.h"
|
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
#include "Util/uv_errno.h"
|
|
|
|
|
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;
|
|
|
|
|
_file_buf.reset(new char[bufSize],[](char *ptr){
|
|
|
|
|
delete[] ptr;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HlsMakerImp::~HlsMakerImp() {
|
2019-09-26 14:21:20 +08:00
|
|
|
|
//录制完了
|
2019-10-12 10:29:40 +08:00
|
|
|
|
flushLastSegment(true);
|
2019-12-17 09:18:11 +08:00
|
|
|
|
if(isLive()){
|
2019-09-26 14:21:20 +08:00
|
|
|
|
//hls直播才删除文件
|
|
|
|
|
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) {
|
2019-12-12 22:58:31 +08:00
|
|
|
|
string segment_name , segment_path;
|
|
|
|
|
{
|
|
|
|
|
auto strDate = getTimeStr("%Y-%m-%d");
|
|
|
|
|
auto strTime = getTimeStr("%H-%M-%S");
|
|
|
|
|
segment_name = StrPrinter << strDate + "/" + strTime << "_" << index << ".ts";
|
|
|
|
|
segment_path = _path_prefix + "/" + segment_name;
|
2019-12-17 09:18:11 +08:00
|
|
|
|
if(isLive()){
|
|
|
|
|
_segment_file_paths.emplace(index,segment_path);
|
|
|
|
|
}
|
2019-12-12 22:58:31 +08:00
|
|
|
|
}
|
|
|
|
|
_file = makeFile(segment_path, true);
|
2019-04-01 12:57:33 +08:00
|
|
|
|
if(!_file){
|
2019-12-12 22:58:31 +08:00
|
|
|
|
WarnL << "create file falied," << segment_path << " " << get_uv_errmsg();
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}
|
|
|
|
|
if(_params.empty()){
|
2019-12-12 22:58:31 +08:00
|
|
|
|
return std::move(segment_name);
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}
|
2019-12-12 22:58:31 +08:00
|
|
|
|
return std::move(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);
|
|
|
|
|
if(it == _segment_file_paths.end()){
|
|
|
|
|
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);
|
|
|
|
|
if(hls){
|
|
|
|
|
fwrite(data,len,1,hls.get());
|
|
|
|
|
hls.reset();
|
2019-12-28 18:50:56 +08:00
|
|
|
|
if(_media_src){
|
|
|
|
|
_media_src->registHls();
|
|
|
|
|
}
|
2019-04-01 12:57:33 +08:00
|
|
|
|
} else{
|
|
|
|
|
WarnL << "create hls file falied," << _path_hls << " " << get_uv_errmsg();
|
|
|
|
|
}
|
|
|
|
|
//DebugL << "\r\n" << string(data,len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file,bool setbuf) {
|
2019-12-27 15:36:54 +08:00
|
|
|
|
auto file_buf = _file_buf;
|
|
|
|
|
auto ret= shared_ptr<FILE>(File::createfile_file(file.data(), "wb"), [file_buf](FILE *fp) {
|
2019-04-01 12:57:33 +08:00
|
|
|
|
if (fp) {
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if(ret && setbuf){
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 10:49:04 +08:00
|
|
|
|
MediaSource::Ptr HlsMakerImp::getMediaSource() const{
|
|
|
|
|
return _media_src;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}//namespace mediakit
|