mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-04 01:37:33 +08:00
完善按需转协议(包括hls)
This commit is contained in:
@@ -124,4 +124,13 @@ bool HlsMaker::isLive() {
|
||||
return _seg_number != 0;
|
||||
}
|
||||
|
||||
void HlsMaker::clear(){
|
||||
_seg_dur_list.clear();
|
||||
_last_file_name.clear();
|
||||
_ticker_last_data.resetTime();
|
||||
_ticker.resetTime();
|
||||
_file_index = 0;
|
||||
}
|
||||
|
||||
|
||||
}//namespace mediakit
|
||||
@@ -39,6 +39,17 @@ public:
|
||||
* @param is_idr_fast_packet 是否为关键帧第一个包
|
||||
*/
|
||||
void inputData(void *data, uint32_t len, uint32_t timestamp, bool is_idr_fast_packet);
|
||||
|
||||
/**
|
||||
* 是否为直播
|
||||
*/
|
||||
bool isLive();
|
||||
|
||||
/**
|
||||
* 清空记录
|
||||
*/
|
||||
void clear();
|
||||
|
||||
protected:
|
||||
/**
|
||||
* 创建ts切片文件回调
|
||||
@@ -73,10 +84,6 @@ protected:
|
||||
*/
|
||||
void flushLastSegment(bool eof = false);
|
||||
|
||||
/**
|
||||
* 是否为直播
|
||||
*/
|
||||
bool isLive();
|
||||
private:
|
||||
/**
|
||||
* 生成m3u8文件
|
||||
@@ -94,6 +101,7 @@ private:
|
||||
* @param timestamp
|
||||
*/
|
||||
void addNewSegment(uint32_t timestamp);
|
||||
|
||||
private:
|
||||
uint32_t _seg_number = 0;
|
||||
float _seg_duration = 0;
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "HlsMakerImp.h"
|
||||
#include "Util/util.h"
|
||||
#include "Util/uv_errno.h"
|
||||
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
@@ -24,37 +25,44 @@ HlsMakerImp::HlsMakerImp(const string &m3u8_file,
|
||||
_path_hls = m3u8_file;
|
||||
_params = params;
|
||||
_buf_size = bufSize;
|
||||
_file_buf.reset(new char[bufSize],[](char *ptr){
|
||||
_file_buf.reset(new char[bufSize], [](char *ptr) {
|
||||
delete[] ptr;
|
||||
});
|
||||
}
|
||||
|
||||
HlsMakerImp::~HlsMakerImp() {
|
||||
clearCache();
|
||||
}
|
||||
|
||||
void HlsMakerImp::clearCache() {
|
||||
//录制完了
|
||||
flushLastSegment(true);
|
||||
if(isLive()){
|
||||
if (isLive()) {
|
||||
//hls直播才删除文件
|
||||
clear();
|
||||
_file = nullptr;
|
||||
_segment_file_paths.clear();
|
||||
File::delete_file(_path_prefix.data());
|
||||
}
|
||||
}
|
||||
|
||||
string HlsMakerImp::onOpenSegment(int index) {
|
||||
string segment_name , segment_path;
|
||||
string segment_name, segment_path;
|
||||
{
|
||||
auto strDate = getTimeStr("%Y-%m-%d");
|
||||
auto strHour = getTimeStr("%H");
|
||||
auto strTime = getTimeStr("%M-%S");
|
||||
segment_name = StrPrinter << strDate + "/" + strHour + "/" + strTime << "_" << index << ".ts";
|
||||
segment_path = _path_prefix + "/" + segment_name;
|
||||
if(isLive()){
|
||||
_segment_file_paths.emplace(index,segment_path);
|
||||
segment_path = _path_prefix + "/" + segment_name;
|
||||
if (isLive()) {
|
||||
_segment_file_paths.emplace(index, segment_path);
|
||||
}
|
||||
}
|
||||
_file = makeFile(segment_path, true);
|
||||
if(!_file){
|
||||
WarnL << "create file falied," << segment_path << " " << get_uv_errmsg();
|
||||
if (!_file) {
|
||||
WarnL << "create file failed," << segment_path << " " << get_uv_errmsg();
|
||||
}
|
||||
if(_params.empty()){
|
||||
if (_params.empty()) {
|
||||
return std::move(segment_name);
|
||||
}
|
||||
return std::move(segment_name + "?" + _params);
|
||||
@@ -62,7 +70,7 @@ string HlsMakerImp::onOpenSegment(int index) {
|
||||
|
||||
void HlsMakerImp::onDelSegment(int index) {
|
||||
auto it = _segment_file_paths.find(index);
|
||||
if(it == _segment_file_paths.end()){
|
||||
if (it == _segment_file_paths.end()) {
|
||||
return;
|
||||
}
|
||||
File::delete_file(it->second.data());
|
||||
@@ -77,27 +85,27 @@ void HlsMakerImp::onWriteSegment(const char *data, int len) {
|
||||
|
||||
void HlsMakerImp::onWriteHls(const char *data, int len) {
|
||||
auto hls = makeFile(_path_hls);
|
||||
if(hls){
|
||||
fwrite(data,len,1,hls.get());
|
||||
if (hls) {
|
||||
fwrite(data, len, 1, hls.get());
|
||||
hls.reset();
|
||||
if(_media_src){
|
||||
if (_media_src) {
|
||||
_media_src->registHls();
|
||||
}
|
||||
} else{
|
||||
WarnL << "create hls file falied," << _path_hls << " " << get_uv_errmsg();
|
||||
} else {
|
||||
WarnL << "create hls file failed," << _path_hls << " " << get_uv_errmsg();
|
||||
}
|
||||
//DebugL << "\r\n" << string(data,len);
|
||||
}
|
||||
|
||||
|
||||
std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file,bool setbuf) {
|
||||
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) {
|
||||
auto ret = shared_ptr<FILE>(File::create_file(file.data(), "wb"), [file_buf](FILE *fp) {
|
||||
if (fp) {
|
||||
fclose(fp);
|
||||
}
|
||||
});
|
||||
if(ret && setbuf){
|
||||
if (ret && setbuf) {
|
||||
setvbuf(ret.get(), _file_buf.get(), _IOFBF, _buf_size);
|
||||
}
|
||||
return ret;
|
||||
@@ -107,7 +115,7 @@ void HlsMakerImp::setMediaSource(const string &vhost, const string &app, const s
|
||||
_media_src = std::make_shared<HlsMediaSource>(vhost, app, stream_id);
|
||||
}
|
||||
|
||||
MediaSource::Ptr HlsMakerImp::getMediaSource() const{
|
||||
HlsMediaSource::Ptr HlsMakerImp::getMediaSource() const {
|
||||
return _media_src;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,8 @@ public:
|
||||
uint32_t bufSize = 64 * 1024,
|
||||
float seg_duration = 5,
|
||||
uint32_t seg_number = 3);
|
||||
virtual ~HlsMakerImp();
|
||||
|
||||
~HlsMakerImp() override;
|
||||
|
||||
/**
|
||||
* 设置媒体信息
|
||||
@@ -41,23 +42,31 @@ public:
|
||||
* 获取MediaSource
|
||||
* @return
|
||||
*/
|
||||
MediaSource::Ptr getMediaSource() const;
|
||||
HlsMediaSource::Ptr getMediaSource() const;
|
||||
|
||||
/**
|
||||
* 清空缓存
|
||||
*/
|
||||
void clearCache();
|
||||
|
||||
protected:
|
||||
string onOpenSegment(int index) override ;
|
||||
void onDelSegment(int index) override;
|
||||
void onWriteSegment(const char *data, int len) override;
|
||||
void onWriteHls(const char *data, int len) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<FILE> makeFile(const string &file,bool setbuf = false);
|
||||
|
||||
private:
|
||||
HlsMediaSource::Ptr _media_src;
|
||||
map<int /*index*/,string/*file_path*/> _segment_file_paths;
|
||||
int _buf_size;
|
||||
string _params;
|
||||
string _path_hls;
|
||||
string _path_prefix;
|
||||
std::shared_ptr<FILE> _file;
|
||||
std::shared_ptr<char> _file_buf;
|
||||
string _path_prefix;
|
||||
string _path_hls;
|
||||
string _params;
|
||||
int _buf_size;
|
||||
HlsMediaSource::Ptr _media_src;
|
||||
map<int /*index*/,string/*file_path*/> _segment_file_paths;
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
@@ -22,7 +22,7 @@ public:
|
||||
typedef RingBuffer<string> RingType;
|
||||
typedef std::shared_ptr<HlsMediaSource> Ptr;
|
||||
HlsMediaSource(const string &vhost, const string &app, const string &stream_id) : MediaSource(HLS_SCHEMA, vhost, app, stream_id){
|
||||
_readerCount = 0;
|
||||
_reader_count = 0;
|
||||
_ring = std::make_shared<RingType>();
|
||||
}
|
||||
|
||||
@@ -40,18 +40,34 @@ public:
|
||||
* @return
|
||||
*/
|
||||
int readerCount() override {
|
||||
return _readerCount.load();
|
||||
return _reader_count.load();
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册hls
|
||||
* 生成m3u8文件时触发
|
||||
*/
|
||||
void registHls(){
|
||||
if (!_registed) {
|
||||
_registed = true;
|
||||
if (!_is_regist) {
|
||||
_is_regist = true;
|
||||
onReaderChanged(0);
|
||||
regist();
|
||||
}
|
||||
|
||||
//m3u8文件生成,发送给播放器
|
||||
decltype(_list_cb) copy;
|
||||
{
|
||||
lock_guard<mutex> lck(_mtx_cb);
|
||||
copy.swap(_list_cb);
|
||||
}
|
||||
copy.for_each([](const function<void()> &cb) {
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
void waitForHls(function<void()> cb){
|
||||
//等待生成m3u8文件
|
||||
lock_guard<mutex> lck(_mtx_cb);
|
||||
_list_cb.emplace_back(std::move(cb));
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -61,16 +77,19 @@ private:
|
||||
*/
|
||||
void modifyReaderCount(bool add) {
|
||||
if (add) {
|
||||
++_readerCount;
|
||||
++_reader_count;
|
||||
} else {
|
||||
--_readerCount;
|
||||
--_reader_count;
|
||||
}
|
||||
onReaderChanged(_readerCount);
|
||||
onReaderChanged(_reader_count);
|
||||
}
|
||||
|
||||
private:
|
||||
atomic_int _readerCount;
|
||||
bool _registed = false;
|
||||
bool _is_regist = false;
|
||||
atomic_int _reader_count;
|
||||
RingType::Ptr _ring;
|
||||
mutex _mtx_cb;
|
||||
List<function<void()> > _list_cb;
|
||||
};
|
||||
|
||||
class HlsCookieData{
|
||||
|
||||
@@ -15,37 +15,76 @@
|
||||
#include "TsMuxer.h"
|
||||
namespace mediakit {
|
||||
|
||||
class HlsRecorder
|
||||
class HlsRecorder : public MediaSourceEventInterceptor, public std::enable_shared_from_this<HlsRecorder>
|
||||
#if defined(ENABLE_HLS)
|
||||
: public TsMuxer
|
||||
, public TsMuxer
|
||||
#endif
|
||||
{
|
||||
public:
|
||||
typedef std::shared_ptr<HlsRecorder> Ptr;
|
||||
HlsRecorder(const string &m3u8_file, const string ¶ms){
|
||||
GET_CONFIG(uint32_t,hlsNum,Hls::kSegmentNum);
|
||||
GET_CONFIG(uint32_t,hlsBufSize,Hls::kFileBufSize);
|
||||
GET_CONFIG(uint32_t,hlsDuration,Hls::kSegmentDuration);
|
||||
_hls = new HlsMakerImp(m3u8_file,params,hlsBufSize,hlsDuration,hlsNum);
|
||||
GET_CONFIG(uint32_t, hlsNum, Hls::kSegmentNum);
|
||||
GET_CONFIG(uint32_t, hlsBufSize, Hls::kFileBufSize);
|
||||
GET_CONFIG(uint32_t, hlsDuration, Hls::kSegmentDuration);
|
||||
_hls = std::make_shared<HlsMakerImp>(m3u8_file, params, hlsBufSize, hlsDuration, hlsNum);
|
||||
//清空上次的残余文件
|
||||
_hls->clearCache();
|
||||
}
|
||||
~HlsRecorder(){
|
||||
delete _hls;
|
||||
}
|
||||
void setMediaSource(const string &vhost, const string &app, const string &stream_id){
|
||||
|
||||
~HlsRecorder(){}
|
||||
|
||||
void setMediaSource(const string &vhost, const string &app, const string &stream_id) {
|
||||
_hls->setMediaSource(vhost, app, stream_id);
|
||||
}
|
||||
|
||||
MediaSource::Ptr getMediaSource() const{
|
||||
return _hls->getMediaSource();
|
||||
void setListener(const std::weak_ptr<MediaSourceEvent> &listener) {
|
||||
_listener = listener;
|
||||
_hls->getMediaSource()->setListener(shared_from_this());
|
||||
//先注册媒体流,后续可以按需生成
|
||||
_hls->getMediaSource()->registHls();
|
||||
}
|
||||
|
||||
int readerCount() {
|
||||
return _hls->getMediaSource()->readerCount();
|
||||
}
|
||||
|
||||
void onReaderChanged(MediaSource &sender, int size) override {
|
||||
//hls保留切片个数为0时代表为hls录制(不删除切片),那么不管有无观看者都一直生成hls
|
||||
_enabled = _hls->isLive() ? size : true;
|
||||
if (!size && _hls->isLive()) {
|
||||
//hls直播时,如果无人观看就删除视频缓存,目的是为了防止视频跳跃
|
||||
_clear_cache = true;
|
||||
}
|
||||
MediaSourceEventInterceptor::onReaderChanged(sender, size);
|
||||
}
|
||||
|
||||
bool isEnabled() {
|
||||
//缓存尚未清空时,还允许触发inputFrame函数,以便及时清空缓存
|
||||
return _clear_cache ? true : _enabled;
|
||||
}
|
||||
|
||||
#if defined(ENABLE_HLS)
|
||||
protected:
|
||||
void onTs(const void *packet, int bytes,uint32_t timestamp,bool is_idr_fast_packet) override {
|
||||
_hls->inputData((char *)packet,bytes,timestamp, is_idr_fast_packet);
|
||||
};
|
||||
#endif
|
||||
void inputFrame(const Frame::Ptr &frame) override{
|
||||
if (_clear_cache) {
|
||||
_clear_cache = false;
|
||||
_hls->clearCache();
|
||||
}
|
||||
if (_enabled) {
|
||||
TsMuxer::inputFrame(frame);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
HlsMakerImp *_hls;
|
||||
void onTs(const void *packet, int bytes, uint32_t timestamp, bool is_idr_fast_packet) override {
|
||||
_hls->inputData((char *) packet, bytes, timestamp, is_idr_fast_packet);
|
||||
}
|
||||
#endif
|
||||
|
||||
private:
|
||||
//默认不生成hls文件,有播放器时再生成
|
||||
bool _enabled = false;
|
||||
bool _clear_cache = false;
|
||||
std::shared_ptr<HlsMakerImp> _hls;
|
||||
};
|
||||
}//namespace mediakit
|
||||
#endif //HLSRECORDER_H
|
||||
|
||||
Reference in New Issue
Block a user