部分恢复录制相关接口

This commit is contained in:
xiongziliang 2020-04-05 09:26:29 +08:00
parent c040f472fa
commit e5d5cabf89
15 changed files with 612 additions and 165 deletions

View File

@ -44,6 +44,39 @@ API_EXPORT void API_CALL mk_flv_recorder_release(mk_flv_recorder ctx);
*/ */
API_EXPORT int API_CALL mk_flv_recorder_start(mk_flv_recorder ctx, const char *vhost, const char *app, const char *stream, const char *file_path); API_EXPORT int API_CALL mk_flv_recorder_start(mk_flv_recorder ctx, const char *vhost, const char *app, const char *stream, const char *file_path);
///////////////////////////////////////////hls/mp4录制/////////////////////////////////////////////
/**
*
* @param type 0:hls,1:MP4
* @param vhost
* @param app
* @param stream id
* @return ,0:, 1:
*/
API_EXPORT int API_CALL mk_recorder_is_recording(int type, const char *vhost, const char *app, const char *stream);
/**
*
* @param type 0:hls,1:MP4
* @param vhost
* @param app
* @param stream id
* @param customized_path null则自动生成
* @return 10
*/
API_EXPORT int API_CALL mk_recorder_start(int type, const char *vhost, const char *app, const char *stream, const char *customized_path);
/**
*
* @param type 0:hls,1:MP4
* @param vhost
* @param app
* @param stream id
* @return 1:0
*/
API_EXPORT int API_CALL mk_recorder_stop(int type, const char *vhost, const char *app, const char *stream);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -26,7 +26,7 @@ public:
~MediaHelper(){} ~MediaHelper(){}
void attachEvent(){ void attachEvent(){
_channel->setListener(shared_from_this()); _channel->setMediaListener(shared_from_this());
} }
DevChannel::Ptr &getChannel(){ DevChannel::Ptr &getChannel(){

View File

@ -34,3 +34,19 @@ API_EXPORT int API_CALL mk_flv_recorder_start(mk_flv_recorder ctx, const char *v
return -1; return -1;
} }
} }
///////////////////////////////////////////hls/mp4录制/////////////////////////////////////////////
API_EXPORT int API_CALL mk_recorder_is_recording(int type, const char *vhost, const char *app, const char *stream){
assert(vhost && app && stream);
return Recorder::isRecording((Recorder::type)type,vhost,app,stream);
}
API_EXPORT int API_CALL mk_recorder_start(int type, const char *vhost, const char *app, const char *stream,const char *customized_path){
assert(vhost && app && stream);
return Recorder::startRecord((Recorder::type)type,vhost,app,stream,customized_path ? customized_path : "");
}
API_EXPORT int API_CALL mk_recorder_stop(int type, const char *vhost, const char *app, const char *stream){
assert(vhost && app && stream);
return Recorder::stopRecord((Recorder::type)type,vhost,app,stream);
}

View File

@ -721,6 +721,37 @@ void installWebApi() {
}); });
#endif//ENABLE_RTPPROXY #endif//ENABLE_RTPPROXY
// 开始录制hls或MP4
api_regist1("/index/api/startRecord",[](API_ARGS1){
CHECK_SECRET();
CHECK_ARGS("type","vhost","app","stream");
val["result"] = Recorder::startRecord((Recorder::type) allArgs["type"].as<int>(),
allArgs["vhost"],
allArgs["app"],
allArgs["stream"],
allArgs["customized_path"]);
});
// 停止录制hls或MP4
api_regist1("/index/api/stopRecord",[](API_ARGS1){
CHECK_SECRET();
CHECK_ARGS("type","vhost","app","stream");
val["result"] = Recorder::stopRecord((Recorder::type) allArgs["type"].as<int>(),
allArgs["vhost"],
allArgs["app"],
allArgs["stream"]);
});
// 获取hls或MP4录制状态
api_regist1("/index/api/isRecording",[](API_ARGS1){
CHECK_SECRET();
CHECK_ARGS("type","vhost","app","stream");
val["status"] = Recorder::isRecording((Recorder::type) allArgs["type"].as<int>(),
allArgs["vhost"],
allArgs["app"],
allArgs["stream"]);
});
//获取录像文件夹列表或mp4文件列表 //获取录像文件夹列表或mp4文件列表
//http://127.0.0.1/index/api/getMp4RecordFile?vhost=__defaultVhost__&app=live&stream=ss&period=2020-01 //http://127.0.0.1/index/api/getMp4RecordFile?vhost=__defaultVhost__&app=live&stream=ss&period=2020-01
api_regist1("/index/api/getMp4RecordFile", [](API_ARGS1){ api_regist1("/index/api/getMp4RecordFile", [](API_ARGS1){

View File

@ -13,9 +13,7 @@
#include "Util/util.h" #include "Util/util.h"
#include "Network/sockutil.h" #include "Network/sockutil.h"
#include "Network/TcpSession.h" #include "Network/TcpSession.h"
using namespace toolkit; using namespace toolkit;
namespace mediakit { namespace mediakit {
recursive_mutex MediaSource::g_mtxMediaSrc; recursive_mutex MediaSource::g_mtxMediaSrc;
@ -104,6 +102,22 @@ void MediaSource::onNoneReader(){
} }
} }
bool MediaSource::setupRecord(Recorder::type type, bool start, const string &custom_path){
auto listener = _listener.lock();
if (!listener) {
return false;
}
return listener->setupRecord(*this, type, start, custom_path);
}
bool MediaSource::isRecording(Recorder::type type){
auto listener = _listener.lock();
if(!listener){
return false;
}
return listener->isRecording(*this, type);
}
void MediaSource::for_each_media(const function<void(const MediaSource::Ptr &src)> &cb) { void MediaSource::for_each_media(const function<void(const MediaSource::Ptr &src)> &cb) {
lock_guard<recursive_mutex> lock(g_mtxMediaSrc); lock_guard<recursive_mutex> lock(g_mtxMediaSrc);
for (auto &pr0 : g_mapMediaSrc) { for (auto &pr0 : g_mapMediaSrc) {

View File

@ -22,6 +22,7 @@
#include "Util/TimeTicker.h" #include "Util/TimeTicker.h"
#include "Util/NoticeCenter.h" #include "Util/NoticeCenter.h"
#include "Extension/Track.h" #include "Extension/Track.h"
#include "Record/Recorder.h"
using namespace std; using namespace std;
using namespace toolkit; using namespace toolkit;
@ -45,6 +46,10 @@ public:
virtual bool close(MediaSource &sender,bool force) { return false;} virtual bool close(MediaSource &sender,bool force) { return false;}
// 观看总人数 // 观看总人数
virtual int totalReaderCount(MediaSource &sender) = 0; virtual int totalReaderCount(MediaSource &sender) = 0;
// 开启或关闭录制
virtual bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) { return false; };
// 获取录制状态
virtual bool isRecording(MediaSource &sender, Recorder::type type) { return false; };
private: private:
// 通知无人观看 // 通知无人观看
void onNoneReader(MediaSource &sender); void onNoneReader(MediaSource &sender);
@ -104,7 +109,6 @@ public:
// 获取监听者 // 获取监听者
const std::weak_ptr<MediaSourceEvent>& getListener() const; const std::weak_ptr<MediaSourceEvent>& getListener() const;
// 本协议获取观看者个数,可能返回本协议的观看人数,也可能返回总人数 // 本协议获取观看者个数,可能返回本协议的观看人数,也可能返回总人数
virtual int readerCount() = 0; virtual int readerCount() = 0;
// 观看者个数,包括(hls/rtsp/rtmp) // 观看者个数,包括(hls/rtsp/rtmp)
@ -121,6 +125,10 @@ public:
bool close(bool force); bool close(bool force);
// 该流无人观看 // 该流无人观看
void onNoneReader(); void onNoneReader();
// 开启或关闭录制
bool setupRecord(Recorder::type type, bool start, const string &custom_path);
// 获取录制状态
bool isRecording(Recorder::type type);
// 同步查找流 // 同步查找流
static Ptr find(const string &schema, const string &vhost, const string &app, const string &id, bool bMake = true) ; static Ptr find(const string &schema, const string &vhost, const string &app, const string &id, bool bMake = true) ;

View File

@ -0,0 +1,295 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/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.
*/
#include "MultiMediaSourceMuxer.h"
namespace mediakit {
MultiMuxerPrivate::~MultiMuxerPrivate() {}
MultiMuxerPrivate::MultiMuxerPrivate(const string &vhost,
const string &app,
const string &stream,
float dur_sec,
bool enable_rtsp,
bool enable_rtmp,
bool enable_hls,
bool enable_mp4) {
if (enable_rtmp) {
_rtmp = std::make_shared<RtmpMediaSourceMuxer>(vhost, app, stream, std::make_shared<TitleMeta>(dur_sec));
}
if (enable_rtsp) {
_rtsp = std::make_shared<RtspMediaSourceMuxer>(vhost, app, stream, std::make_shared<TitleSdp>(dur_sec));
}
if (enable_hls) {
_hls = Recorder::createRecorder(Recorder::type_hls, vhost, app, stream);
}
if (enable_mp4) {
_mp4 = Recorder::createRecorder(Recorder::type_mp4, vhost, app, stream);
}
}
void MultiMuxerPrivate::resetTracks() {
if (_rtmp) {
_rtmp->resetTracks();
}
if (_rtsp) {
_rtsp->resetTracks();
}
//拷贝智能指针目的是为了防止跨线程调用设置录像相关api导致的线程竞争问题
auto hls = _hls;
if (hls) {
hls->resetTracks();
}
auto mp4 = _mp4;
if (mp4) {
mp4->resetTracks();
}
}
void MultiMuxerPrivate::setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener) {
if (_rtmp) {
_rtmp->setListener(listener);
}
if (_rtsp) {
_rtsp->setListener(listener);
}
auto hls_src = getHlsMediaSource();
if (hls_src) {
hls_src->setListener(listener);
}
_meida_listener = listener;
}
int MultiMuxerPrivate::totalReaderCount() const {
auto hls_src = getHlsMediaSource();
return (_rtsp ? _rtsp->readerCount() : 0) + (_rtmp ? _rtmp->readerCount() : 0) + (hls_src ? hls_src->readerCount() : 0);
}
static std::shared_ptr<MediaSinkInterface> makeRecorder(const vector<Track::Ptr> &tracks, Recorder::type type, MediaSource &sender){
auto recorder = Recorder::createRecorder(type, sender.getVhost(), sender.getApp(), sender.getId());
for (auto &track : tracks) {
recorder->addTrack(track);
}
return recorder;
}
//此函数可能跨线程调用
bool MultiMuxerPrivate::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path){
switch (type) {
case Recorder::type_hls : {
if (start && !_hls) {
//开始录制
_hls = makeRecorder(getTracks(true), type, sender);
auto hls_src = getHlsMediaSource();
if (hls_src) {
//设置HlsMediaSource的事件监听器
hls_src->setListener(_meida_listener);
}
} else if (!start && _hls) {
//停止录制
_hls = nullptr;
}
return true;
}
case Recorder::type_mp4 : {
if (start && !_mp4) {
//开始录制
_mp4 = makeRecorder(getTracks(true), type, sender);;
} else if (!start && _mp4) {
//停止录制
_mp4 = nullptr;
}
return true;
}
default:
return false;
}
}
//此函数可能跨线程调用
bool MultiMuxerPrivate::isRecording(MediaSource &sender, Recorder::type type){
switch (type){
case Recorder::type_hls :
return _hls ? true : false;
case Recorder::type_mp4 :
return _mp4 ? true : false;
default:
return false;
}
}
void MultiMuxerPrivate::setTimeStamp(uint32_t stamp) {
if (_rtmp) {
_rtmp->setTimeStamp(stamp);
}
if (_rtsp) {
_rtsp->setTimeStamp(stamp);
}
}
void MultiMuxerPrivate::setTrackListener(Listener *listener) {
_listener = listener;
}
void MultiMuxerPrivate::onTrackReady(const Track::Ptr &track) {
if (_rtmp) {
_rtmp->addTrack(track);
}
if (_rtsp) {
_rtsp->addTrack(track);
}
//拷贝智能指针目的是为了防止跨线程调用设置录像相关api导致的线程竞争问题
auto hls = _hls;
if (hls) {
hls->addTrack(track);
}
auto mp4 = _mp4;
if (mp4) {
mp4->addTrack(track);
}
}
void MultiMuxerPrivate::onTrackFrame(const Frame::Ptr &frame) {
if (_rtmp) {
_rtmp->inputFrame(frame);
}
if (_rtsp) {
_rtsp->inputFrame(frame);
}
//拷贝智能指针目的是为了防止跨线程调用设置录像相关api导致的线程竞争问题
//此处使用智能指针拷贝来确保线程安全,比互斥锁性能更优
auto hls = _hls;
if (hls) {
hls->inputFrame(frame);
}
auto mp4 = _mp4;
if (mp4) {
mp4->inputFrame(frame);
}
}
void MultiMuxerPrivate::onAllTrackReady() {
if (_rtmp) {
_rtmp->setTrackSource(shared_from_this());
_rtmp->onAllTrackReady();
}
if (_rtsp) {
_rtsp->setTrackSource(shared_from_this());
_rtsp->onAllTrackReady();
}
auto hls_src = getHlsMediaSource();
if (hls_src) {
hls_src->setTrackSource(shared_from_this());
}
if (_listener) {
_listener->onAllTrackReady();
}
}
MediaSource::Ptr MultiMuxerPrivate::getHlsMediaSource() const {
auto recorder = dynamic_pointer_cast<HlsRecorder>(_hls);
if (recorder) {
return recorder->getMediaSource();
}
return nullptr;
}
/////////////////////////////////////////////////////////////////
MultiMediaSourceMuxer::~MultiMediaSourceMuxer() {}
MultiMediaSourceMuxer::MultiMediaSourceMuxer(const string &vhost,
const string &app,
const string &stream,
float dur_sec,
bool enable_rtsp,
bool enable_rtmp,
bool enable_hls,
bool enable_mp4) {
_muxer.reset(new MultiMuxerPrivate(vhost, app, stream, dur_sec, enable_rtsp, enable_rtmp, enable_hls, enable_mp4));
}
void MultiMediaSourceMuxer::setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener) {
_muxer->setMediaListener(shared_from_this());
_listener = listener;
}
int MultiMediaSourceMuxer::totalReaderCount() const {
return _muxer->totalReaderCount();
}
void MultiMediaSourceMuxer::setTimeStamp(uint32_t stamp) {
_muxer->setTimeStamp(stamp);
}
void MultiMediaSourceMuxer::setTrackListener(Listener *listener) {
_muxer->setTrackListener(listener);
}
vector<Track::Ptr> MultiMediaSourceMuxer::getTracks(bool trackReady) const {
return _muxer->getTracks(trackReady);
}
bool MultiMediaSourceMuxer::seekTo(MediaSource &sender, uint32_t ui32Stamp) {
auto listener = _listener.lock();
if (!listener) {
return false;
}
return listener->seekTo(sender, ui32Stamp);
}
bool MultiMediaSourceMuxer::close(MediaSource &sender, bool force) {
auto listener = _listener.lock();
if (!listener) {
return false;
}
return listener->close(sender, force);
}
int MultiMediaSourceMuxer::totalReaderCount(MediaSource &sender) {
auto listener = _listener.lock();
if (!listener) {
return _muxer->totalReaderCount();
}
return listener->totalReaderCount(sender);
}
bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) {
return _muxer->setupRecord(sender,type,start,custom_path);
}
bool MultiMediaSourceMuxer::isRecording(MediaSource &sender, Recorder::type type) {
return _muxer->isRecording(sender,type);
}
void MultiMediaSourceMuxer::addTrack(const Track::Ptr &track) {
_muxer->addTrack(track);
}
void MultiMediaSourceMuxer::addTrackCompleted() {
_muxer->addTrackCompleted();
}
void MultiMediaSourceMuxer::resetTracks() {
_muxer->resetTracks();
}
void MultiMediaSourceMuxer::inputFrame(const Frame::Ptr &frame) {
_muxer->inputFrame(frame);
}
}//namespace mediakit

View File

@ -10,180 +10,167 @@
#ifndef ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H #ifndef ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H
#define ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H #define ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H
#include "Rtsp/RtspMediaSourceMuxer.h" #include "Rtsp/RtspMediaSourceMuxer.h"
#include "Rtmp/RtmpMediaSourceMuxer.h" #include "Rtmp/RtmpMediaSourceMuxer.h"
#include "Record/Recorder.h" #include "Record/Recorder.h"
#include "Record/HlsMediaSource.h" #include "Record/HlsMediaSource.h"
#include "Record/HlsRecorder.h" #include "Record/HlsRecorder.h"
namespace mediakit{
/** class MultiMuxerPrivate : public MediaSink , public std::enable_shared_from_this<MultiMuxerPrivate>{
* 使使setListener方法来绑定MediaSource相关的事件
* MediaSource(rtsp/rtmp/hls)
*/
class MultiMediaSourceMuxer : public MediaSink , public std::enable_shared_from_this<MultiMediaSourceMuxer>{
public: public:
friend class MultiMediaSourceMuxer;
typedef std::shared_ptr<MultiMuxerPrivate> Ptr;
class Listener{ class Listener{
public: public:
Listener() = default; Listener() = default;
virtual ~Listener() = default; virtual ~Listener() = default;
virtual void onAllTrackReady() = 0; virtual void onAllTrackReady() = 0;
}; };
~MultiMuxerPrivate() override ;
private:
MultiMuxerPrivate(const string &vhost,
const string &app,
const string &stream,
float dur_sec,
bool enable_rtsp,
bool enable_rtmp,
bool enable_hls,
bool enable_mp4);
typedef std::shared_ptr<MultiMediaSourceMuxer> Ptr; void resetTracks() override;
MultiMediaSourceMuxer(const string &vhost, const string &app, const string &stream, float dur_sec = 0.0, void setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener);
bool enable_rtsp = true, bool enable_rtmp = true, bool enable_hls = true, bool enable_mp4 = false){ int totalReaderCount() const;
if (enable_rtmp) { void setTimeStamp(uint32_t stamp);
_rtmp = std::make_shared<RtmpMediaSourceMuxer>(vhost, app, stream, std::make_shared<TitleMeta>(dur_sec)); void setTrackListener(Listener *listener);
} bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path);
if (enable_rtsp) { bool isRecording(MediaSource &sender, Recorder::type type);
_rtsp = std::make_shared<RtspMediaSourceMuxer>(vhost, app, stream, std::make_shared<TitleSdp>(dur_sec)); private:
} void onTrackReady(const Track::Ptr & track) override;
void onTrackFrame(const Frame::Ptr &frame) override;
if(enable_hls){ void onAllTrackReady() override;
_hls = Recorder::createRecorder(Recorder::type_hls,vhost, app, stream); MediaSource::Ptr getHlsMediaSource() const;
}
if(enable_mp4){
_mp4 = Recorder::createRecorder(Recorder::type_mp4,vhost, app, stream);
}
}
virtual ~MultiMediaSourceMuxer(){}
/**
*
*/
void resetTracks() override{
if(_rtmp){
_rtmp->resetTracks();
}
if(_rtsp){
_rtsp->resetTracks();
}
if(_hls){
_hls->resetTracks();
}
if(_mp4){
_mp4->resetTracks();
}
}
/**
*
* @param listener
*/
void setListener(const std::weak_ptr<MediaSourceEvent> &listener){
if(_rtmp) {
_rtmp->setListener(listener);
}
if(_rtsp) {
_rtsp->setListener(listener);
}
auto hls_src = getHlsMediaSource();
if(hls_src){
hls_src->setListener(listener);
}
}
/**
*
* @return
*/
int totalReaderCount() const{
auto hls_src = getHlsMediaSource();
return (_rtsp ? _rtsp->readerCount() : 0) + (_rtmp ? _rtmp->readerCount() : 0) + (hls_src ? hls_src->readerCount() : 0);
}
void setTimeStamp(uint32_t stamp){
if(_rtmp){
_rtmp->setTimeStamp(stamp);
}
if(_rtsp){
_rtsp->setTimeStamp(stamp);
}
}
void setTrackListener(Listener *listener){
_listener = listener;
}
protected:
/**
*
* @param track
*/
void onTrackReady(const Track::Ptr & track) override {
if(_rtmp){
_rtmp->addTrack(track);
}
if(_rtsp){
_rtsp->addTrack(track);
}
if(_hls){
_hls->addTrack(track);
}
if(_mp4){
_mp4->addTrack(track);
}
}
/**
* rtmp
* @param frame
*/
void onTrackFrame(const Frame::Ptr &frame) override {
if(_rtmp) {
_rtmp->inputFrame(frame);
}
if(_rtsp) {
_rtsp->inputFrame(frame);
}
if(_hls){
_hls->inputFrame(frame);
}
if(_mp4){
_mp4->inputFrame(frame);
}
}
/**
* Track都准备就绪
*/
void onAllTrackReady() override{
if(_rtmp) {
_rtmp->setTrackSource(shared_from_this());
_rtmp->onAllTrackReady();
}
if(_rtsp) {
_rtsp->setTrackSource(shared_from_this());
_rtsp->onAllTrackReady();
}
auto hls_src = getHlsMediaSource();
if(hls_src){
hls_src->setTrackSource(shared_from_this());
}
if(_listener){
_listener->onAllTrackReady();
}
}
MediaSource::Ptr getHlsMediaSource() const{
auto recorder = dynamic_pointer_cast<HlsRecorder>(_hls);
if(recorder){
return recorder->getMediaSource();
}
return nullptr;
}
private: private:
RtmpMediaSourceMuxer::Ptr _rtmp; RtmpMediaSourceMuxer::Ptr _rtmp;
RtspMediaSourceMuxer::Ptr _rtsp; RtspMediaSourceMuxer::Ptr _rtsp;
MediaSinkInterface::Ptr _hls; MediaSinkInterface::Ptr _hls;
MediaSinkInterface::Ptr _mp4; MediaSinkInterface::Ptr _mp4;
Listener *_listener = nullptr; Listener *_listener = nullptr;
std::weak_ptr<MediaSourceEvent> _meida_listener;
}; };
class MultiMediaSourceMuxer : public MediaSourceEvent, public MediaSinkInterface, public TrackSource, public std::enable_shared_from_this<MultiMediaSourceMuxer>{
public:
typedef MultiMuxerPrivate::Listener Listener;
typedef std::shared_ptr<MultiMediaSourceMuxer> Ptr;
~MultiMediaSourceMuxer() override;
MultiMediaSourceMuxer(const string &vhost,
const string &app,
const string &stream,
float dur_sec = 0.0,
bool enable_rtsp = true,
bool enable_rtmp = true,
bool enable_hls = true,
bool enable_mp4 = false);
/**
*
* @param listener
*/
void setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener);
/**
*
* @return
*/
int totalReaderCount() const;
/**
* MediaSource时间戳
* @param stamp
*/
void setTimeStamp(uint32_t stamp);
/**
* Track就绪事件监听器
* @param listener
*/
void setTrackListener(Listener *listener);
/**
* Track
* @param trackReady track
* @return Track
*/
vector<Track::Ptr> getTracks(bool trackReady = true) const override;
/**
*
* @param sender
* @param ui32Stamp
* @return
*/
bool seekTo(MediaSource &sender,uint32_t ui32Stamp) override;
/**
*
* @param sender
* @param force
* @return
*/
bool close(MediaSource &sender,bool force) override;
/**
*
* @param sender
* @return
*/
int totalReaderCount(MediaSource &sender) override;
/**
*
* @param type
* @param start
* @param custom_path
* @return
*/
bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) override;
/**
*
* @param type
* @return
*/
bool isRecording(MediaSource &sender, Recorder::type type) override;
/**
* trackTrack的clone方法
* sps pps这些信息 Delegate相关关系
* @param track
*/
void addTrack(const Track::Ptr & track) override;
/**
* track完毕
* @param track
*/
void addTrackCompleted();
/**
* track
*/
void resetTracks() override;
/**
*
* @param frame
*/
void inputFrame(const Frame::Ptr &frame) override;
private:
MultiMuxerPrivate::Ptr _muxer;
std::weak_ptr<MediaSourceEvent> _listener;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H #endif //ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H

View File

@ -238,7 +238,7 @@ void PlayerProxy::onPlaySuccess() {
_mediaMuxer.reset(new MultiMediaSourceMuxer(_strVhost, _strApp, _strSrc, getDuration(), _bEnableRtsp, _bEnableRtmp, _bEnableHls, _bEnableMp4)); _mediaMuxer.reset(new MultiMediaSourceMuxer(_strVhost, _strApp, _strSrc, getDuration(), _bEnableRtsp, _bEnableRtmp, _bEnableHls, _bEnableMp4));
} }
} }
_mediaMuxer->setListener(shared_from_this()); _mediaMuxer->setMediaListener(shared_from_this());
auto videoTrack = getTrack(TrackVideo,false); auto videoTrack = getTrack(TrackVideo,false);
if(videoTrack){ if(videoTrack){

View File

@ -73,7 +73,7 @@ bool MP4Reader::readSample() {
void MP4Reader::startReadMP4() { void MP4Reader::startReadMP4() {
GET_CONFIG(uint32_t, sampleMS, Record::kSampleMS); GET_CONFIG(uint32_t, sampleMS, Record::kSampleMS);
auto strongSelf = shared_from_this(); auto strongSelf = shared_from_this();
_mediaMuxer->setListener(strongSelf); _mediaMuxer->setMediaListener(strongSelf);
//先获取关键帧 //先获取关键帧
seekTo(0); seekTo(0);

View File

@ -79,4 +79,36 @@ std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const st
} }
} }
static MediaSource::Ptr getMediaSource(const string &vhost, const string &app, const string &stream_id){
auto src = MediaSource::find(RTMP_SCHEMA, vhost, app, stream_id, false);
if(src){
return src;
}
return MediaSource::find(RTSP_SCHEMA, vhost, app, stream_id, false);
}
bool Recorder::isRecording(type type, const string &vhost, const string &app, const string &stream_id){
auto src = getMediaSource(vhost, app, stream_id);
if(!src){
return false;
}
return src->isRecording(type);
}
bool Recorder::startRecord(type type, const string &vhost, const string &app, const string &stream_id,const string &customized_path){
auto src = getMediaSource(vhost, app, stream_id);
if(!src){
return false;
}
return src->setupRecord(type,true,customized_path);
}
bool Recorder::stopRecord(type type, const string &vhost, const string &app, const string &stream_id){
auto src = getMediaSource(vhost, app, stream_id);
if(!src){
return false;
}
return src->setupRecord(type, false, "");
}
} /* namespace mediakit */ } /* namespace mediakit */

View File

@ -46,6 +46,37 @@ public:
* @return nullptr * @return nullptr
*/ */
static std::shared_ptr<MediaSinkInterface> createRecorder(type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path = ""); static std::shared_ptr<MediaSinkInterface> createRecorder(type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path = "");
/**
*
* @param type hls还是MP4录制
* @param vhost
* @param app
* @param stream_id id
* @return
*/
static bool isRecording(type type, const string &vhost, const string &app, const string &stream_id);
/**
*
* @param type hls还是MP4录制
* @param vhost
* @param app
* @param stream_id id
* @param customized_path
* @return
*/
static bool startRecord(type type, const string &vhost, const string &app, const string &stream_id,const string &customized_path);
/**
*
* @param type hls还是MP4录制
* @param vhost
* @param app
* @param stream_id id
*/
static bool stopRecord(type type, const string &vhost, const string &app, const string &stream_id);
private: private:
Recorder() = delete; Recorder() = delete;
~Recorder() = delete; ~Recorder() = delete;

View File

@ -68,7 +68,7 @@ public:
void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override { void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override {
RtmpMediaSource::setListener(listener); RtmpMediaSource::setListener(listener);
if(_muxer){ if(_muxer){
_muxer->setListener(listener); _muxer->setMediaListener(listener);
} }
} }
@ -88,7 +88,7 @@ public:
void setProtocolTranslation(bool enableRtsp, bool enableHls, bool enableMP4) { void setProtocolTranslation(bool enableRtsp, bool enableHls, bool enableMP4) {
//不重复生成rtmp //不重复生成rtmp
_muxer = std::make_shared<MultiMediaSourceMuxer>(getVhost(), getApp(), getId(), _demuxer->getDuration(), enableRtsp, false, enableHls, enableMP4); _muxer = std::make_shared<MultiMediaSourceMuxer>(getVhost(), getApp(), getId(), _demuxer->getDuration(), enableRtsp, false, enableHls, enableMP4);
_muxer->setListener(getListener()); _muxer->setMediaListener(getListener());
_muxer->setTrackListener(this); _muxer->setTrackListener(this);
for(auto &track : _demuxer->getTracks(false)){ for(auto &track : _demuxer->getTracks(false)){
_muxer->addTrack(track); _muxer->addTrack(track);

View File

@ -298,7 +298,7 @@ int RtpProcess::totalReaderCount(){
} }
void RtpProcess::setListener(const std::weak_ptr<MediaSourceEvent> &listener){ void RtpProcess::setListener(const std::weak_ptr<MediaSourceEvent> &listener){
_muxer->setListener(listener); _muxer->setMediaListener(listener);
} }

View File

@ -59,7 +59,7 @@ public:
void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override { void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override {
RtspMediaSource::setListener(listener); RtspMediaSource::setListener(listener);
if(_muxer){ if(_muxer){
_muxer->setListener(listener); _muxer->setMediaListener(listener);
} }
} }
@ -79,7 +79,7 @@ public:
void setProtocolTranslation(bool enableRtmp,bool enableHls,bool enableMP4){ void setProtocolTranslation(bool enableRtmp,bool enableHls,bool enableMP4){
//不重复生成rtsp //不重复生成rtsp
_muxer = std::make_shared<MultiMediaSourceMuxer>(getVhost(), getApp(), getId(), _demuxer->getDuration(), false, enableRtmp, enableHls, enableMP4); _muxer = std::make_shared<MultiMediaSourceMuxer>(getVhost(), getApp(), getId(), _demuxer->getDuration(), false, enableRtmp, enableHls, enableMP4);
_muxer->setListener(getListener()); _muxer->setMediaListener(getListener());
_muxer->setTrackListener(this); _muxer->setTrackListener(this);
for(auto &track : _demuxer->getTracks(false)){ for(auto &track : _demuxer->getTracks(false)){
_muxer->addTrack(track); _muxer->addTrack(track);