合并pr(添加录制ts完成通知): #479

This commit is contained in:
xiongziliang
2020-09-20 11:40:42 +08:00
parent 0fce108de5
commit f84981dc75
11 changed files with 94 additions and 145 deletions

View File

@@ -118,16 +118,12 @@ void HlsMaker::flushLastSegment(bool eof){
seg_dur = 100;
}
_seg_dur_list.push_back(std::make_tuple(seg_dur, std::move(_last_file_name)));
_last_file_name.clear();
delOldSegment();
makeIndexFile(eof);
_last_file_name.clear();
onFlushLastSegment(seg_dur);
}
void HlsMaker::onFlushLastSegment(uint32_t) {
}
bool HlsMaker::isLive() {
return _seg_number != 0;
}

View File

@@ -22,20 +22,6 @@ using namespace toolkit;
namespace mediakit {
class TsInfo {
public:
time_t ui64StartedTime; // GMT 标准时间,单位秒
time_t ui64TimeLen; // 录像长度,单位毫秒
off_t ui64FileSize; // 文件大小,单位 BYTE
string strFilePath; // 文件路径
string strFileName; // 文件名称
string strFolder; // 文件夹路径
string strUrl; // 播放路径
string strAppName; // 应用名称
string strStreamId; // 流 ID
string strVhost; // vhost
};
class HlsMaker {
public:
/**
@@ -92,18 +78,18 @@ protected:
*/
virtual void onWriteHls(const char *data, int len) = 0;
/**
* 上一个 ts 切片写入完成, 可在这里进行通知处理
* @param duration_ms 上一个 ts 切片的时长, 单位为毫秒
*/
virtual void onFlushLastSegment(uint32_t duration_ms) {};
/**
* 关闭上个ts切片并且写入m3u8索引
* @param eof HLS直播是否已结束
*/
void flushLastSegment(bool eof);
/**
* 上一个 ts 切片写入完成, 可在这里进行通知处理
* @param duration 上一个 ts 切片的时长, 单位为毫秒
*/
virtual void onFlushLastSegment(uint32_t duration);
private:
/**
* 生成m3u8文件

View File

@@ -11,7 +11,6 @@
#include <ctime>
#include <sys/stat.h>
#include "HlsMakerImp.h"
#include "Thread/WorkThreadPool.h"
#include "Util/util.h"
#include "Util/uv_errno.h"
@@ -32,7 +31,7 @@ HlsMakerImp::HlsMakerImp(const string &m3u8_file,
delete[] ptr;
});
_info.strFolder = _path_prefix;
_info.folder = _path_prefix;
}
HlsMakerImp::~HlsMakerImp() {
@@ -65,10 +64,11 @@ string HlsMakerImp::onOpenSegment(int index) {
}
_file = makeFile(segment_path, true);
_info.ui64StartedTime = ::time(NULL);
_info.strFileName = segment_name;
_info.strFilePath = segment_path;
_info.strUrl = _info.strAppName + "/" + _info.strStreamId + "/" + segment_name;
//保存本切片的元数据
_info.start_time = ::time(NULL);
_info.file_name = segment_name;
_info.file_path = segment_path;
_info.url = _info.app + "/" + _info.stream + "/" + segment_name;
if (!_file) {
WarnL << "create file failed," << segment_path << " " << get_uv_errmsg();
@@ -108,22 +108,19 @@ void HlsMakerImp::onWriteHls(const char *data, int len) {
//DebugL << "\r\n" << string(data,len);
}
void HlsMakerImp::onFlushLastSegment(uint32_t duration) {
void HlsMakerImp::onFlushLastSegment(uint32_t duration_ms) {
GET_CONFIG(bool, broadcastRecordTs, Hls::kBroadcastRecordTs);
if (broadcastRecordTs) {
auto info = _info;
info.ui64TimeLen = duration;
WorkThreadPool::Instance().getExecutor()->async([info]() {
struct stat fileData;
stat(info.strFilePath.data(), &fileData);
const_cast<RecordInfo&>(info).ui64FileSize = fileData.st_size;
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordTs, info);
});
//关闭ts文件以便获取正确的文件大小
_file = nullptr;
_info.time_len = duration_ms / 1000.0;
struct stat fileData;
stat(_info.file_path.data(), &fileData);
_info.file_size = fileData.st_size;
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordTs, _info);
}
}
std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file, bool setbuf) {
auto file_buf = _file_buf;
auto ret = shared_ptr<FILE>(File::create_file(file.data(), "wb"), [file_buf](FILE *fp) {
@@ -139,9 +136,9 @@ std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file, bool setbuf) {
void HlsMakerImp::setMediaSource(const string &vhost, const string &app, const string &stream_id) {
_media_src = std::make_shared<HlsMediaSource>(vhost, app, stream_id);
_info.strAppName = app;
_info.strStreamId = stream_id;
_info.strVhost = vhost;
_info.app = app;
_info.stream = stream_id;
_info.vhost = vhost;
}
HlsMediaSource::Ptr HlsMakerImp::getMediaSource() const {

View File

@@ -16,7 +16,6 @@
#include <stdlib.h>
#include "HlsMaker.h"
#include "HlsMediaSource.h"
#include "RecordInfo.h"
using namespace std;
@@ -56,7 +55,7 @@ protected:
void onDelSegment(int index) override;
void onWriteSegment(const char *data, int len) override;
void onWriteHls(const char *data, int len) override;
void onFlushLastSegment(uint32_t duration) override;
void onFlushLastSegment(uint32_t duration_ms) override;
private:
std::shared_ptr<FILE> makeFile(const string &file,bool setbuf = false);
@@ -66,10 +65,10 @@ private:
string _params;
string _path_hls;
string _path_prefix;
RecordInfo _info;
std::shared_ptr<FILE> _file;
std::shared_ptr<char> _file_buf;
HlsMediaSource::Ptr _media_src;
RecordInfo _info;
map<int /*index*/,string/*file_path*/> _segment_file_paths;
};

View File

@@ -25,10 +25,10 @@ MP4Recorder::MP4Recorder(const string& strPath,
const string &strStreamId) {
_strPath = strPath;
/////record 业务逻辑//////
_info.strAppName = strApp;
_info.strStreamId = strStreamId;
_info.strVhost = strVhost;
_info.strFolder = strPath;
_info.app = strApp;
_info.stream = strStreamId;
_info.vhost = strVhost;
_info.folder = strPath;
}
MP4Recorder::~MP4Recorder() {
closeFile();
@@ -42,15 +42,15 @@ void MP4Recorder::createFile() {
auto strFile = _strPath + strDate + "/" + strTime + ".mp4";
/////record 业务逻辑//////
_info.ui64StartedTime = ::time(NULL);
_info.strFileName = strTime + ".mp4";
_info.strFilePath = strFile;
_info.start_time = ::time(NULL);
_info.file_name = strTime + ".mp4";
_info.file_path = strFile;
GET_CONFIG(string,appName,Record::kAppName);
_info.strUrl = appName + "/"
+ _info.strAppName + "/"
+ _info.strStreamId + "/"
+ strDate + "/"
+ strTime + ".mp4";
_info.url = appName + "/"
+ _info.app + "/"
+ _info.stream + "/"
+ strDate + "/"
+ strTime + ".mp4";
try {
_muxer = std::make_shared<MP4Muxer>(strFileTmp.data());
@@ -73,7 +73,7 @@ void MP4Recorder::asyncClose() {
auto info = _info;
WorkThreadPool::Instance().getExecutor()->async([muxer,strFileTmp,strFile,info]() {
//获取文件录制时间放在关闭mp4之前是为了忽略关闭mp4执行时间
const_cast<RecordInfo&>(info).ui64TimeLen = ::time(NULL) - info.ui64StartedTime;
const_cast<RecordInfo&>(info).time_len = ::time(NULL) - info.start_time;
//关闭mp4非常耗时所以要放在后台线程执行
muxer->closeMP4();
//临时文件名改成正式文件名防止mp4未完成时被访问
@@ -81,7 +81,7 @@ void MP4Recorder::asyncClose() {
//获取文件大小
struct stat fileData;
stat(strFile.data(), &fileData);
const_cast<RecordInfo&>(info).ui64FileSize = fileData.st_size;
const_cast<RecordInfo&>(info).file_size = fileData.st_size;
/////record 业务逻辑//////
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordMP4,info);
});

View File

@@ -20,7 +20,6 @@
#include "Util/TimeTicker.h"
#include "Common/MediaSink.h"
#include "MP4Muxer.h"
#include "RecordInfo.h"
using namespace toolkit;

View File

@@ -1,34 +0,0 @@
/*
* Copyright (c) 2020 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* 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.
*/
#ifndef RECORDINFO_H_
#define RECORDINFO_H_
#include "Common/config.h"
namespace mediakit {
class RecordInfo {
public:
time_t ui64StartedTime; // GMT 标准时间,单位秒
time_t ui64TimeLen; // 录像长度,需要注意 mp4 单位是秒,而 hls ts 单位是毫秒
off_t ui64FileSize; // 文件大小,单位 BYTE
string strFilePath; // 文件路径
string strFileName; // 文件名称
string strFolder; // 文件夹路径
string strUrl; // 播放路径
string strAppName; // 应用名称
string strStreamId; // 流 ID
string strVhost; // vhost
};
} // namespace mediakit
#endif // RECORDINFO_H_

View File

@@ -16,6 +16,20 @@ using namespace std;
namespace mediakit {
class MediaSinkInterface;
class RecordInfo {
public:
time_t start_time; // GMT 标准时间,单位秒
float time_len; // 录像长度,单位秒
off_t file_size; // 文件大小,单位 BYTE
string file_path; // 文件路径
string file_name; // 文件名称
string folder; // 文件夹路径
string url; // 播放路径
string app; // 应用名称
string stream; // 流 ID
string vhost; // 虚拟主机
};
class Recorder{
public:
typedef enum {