MultiMediaSourceMuxer默认能处理大部分事件

This commit is contained in:
xiongziliang 2020-08-08 12:20:13 +08:00
parent b3f048f194
commit f23e117751
6 changed files with 43 additions and 38 deletions

View File

@ -106,6 +106,7 @@ void MediaSource::onNoneReader(){
bool MediaSource::setupRecord(Recorder::type type, bool start, const string &custom_path){
auto listener = _listener.lock();
if (!listener) {
WarnL << "未设置MediaSource的事件监听者setupRecord失败:" << getSchema() << "/" << getVhost() << "/" << getApp() << "/" << getId();
return false;
}
return listener->setupRecord(*this, type, start, custom_path);

View File

@ -231,6 +231,7 @@ MultiMediaSourceMuxer::MultiMediaSourceMuxer(const string &vhost,
bool enable_hls,
bool enable_mp4) {
_muxer.reset(new MultiMuxerPrivate(vhost, app, stream, dur_sec, enable_rtsp, enable_rtmp, enable_hls, enable_mp4));
_muxer->setTrackListener(this);
}
void MultiMediaSourceMuxer::setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener) {
@ -238,6 +239,10 @@ void MultiMediaSourceMuxer::setMediaListener(const std::weak_ptr<MediaSourceEven
_listener = listener;
}
void MultiMediaSourceMuxer::setTrackListener(const std::weak_ptr<MultiMuxerPrivate::Listener> &listener) {
_track_listener = listener;
}
int MultiMediaSourceMuxer::totalReaderCount() const {
return _muxer->totalReaderCount();
}
@ -246,10 +251,6 @@ 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);
}
@ -310,6 +311,14 @@ void MultiMediaSourceMuxer::addTrackCompleted() {
_muxer->addTrackCompleted();
}
void MultiMediaSourceMuxer::onAllTrackReady(){
_muxer->setMediaListener(shared_from_this());
auto track_listener = _track_listener.lock();
if(track_listener){
track_listener->onAllTrackReady();
}
}
void MultiMediaSourceMuxer::resetTracks() {
_muxer->resetTracks();
}

View File

@ -27,17 +27,12 @@ public:
virtual ~Listener() = default;
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);
~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);
void resetTracks() override;
void setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener);
int totalReaderCount() const;
@ -46,11 +41,11 @@ private:
bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path);
bool isRecording(MediaSource &sender, Recorder::type type);
bool isEnabled();
private:
void onTrackReady(const Track::Ptr & track) override;
void onTrackFrame(const Frame::Ptr &frame) override;
void onAllTrackReady() override;
MediaSource::Ptr getHlsMediaSource() const;
private:
RtmpMediaSourceMuxer::Ptr _rtmp;
RtspMediaSourceMuxer::Ptr _rtsp;
@ -62,30 +57,29 @@ private:
bool _enable_record = false;
};
class MultiMediaSourceMuxer : public MediaSourceEvent, public MediaSinkInterface, public TrackSource, public std::enable_shared_from_this<MultiMediaSourceMuxer>{
class MultiMediaSourceMuxer : public MediaSourceEvent, public MediaSinkInterface, public TrackSource, public MultiMuxerPrivate::Listener, 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);
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
* @param listener
*/
void setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener);
/**
* Track就绪事件监听器
* @param listener
*/
void setTrackListener(const std::weak_ptr<MultiMuxerPrivate::Listener> &listener);
/**
*
* @return
*/
int totalReaderCount() const;
@ -95,12 +89,6 @@ public:
*/
void setTimeStamp(uint32_t stamp);
/**
* Track就绪事件监听器
* @param listener
*/
void setTrackListener(Listener *listener);
/**
* Track
* @param trackReady track
@ -163,16 +151,20 @@ public:
/**
* trackTrack的clone方法
* sps pps这些信息 Delegate相关关系
* @param track
* @param track
*/
void addTrack(const Track::Ptr & track) override;
/**
* track完毕
* @param track
*/
void addTrackCompleted();
/**
* track全部就绪
*/
void onAllTrackReady() override;
/**
* track
*/
@ -188,9 +180,11 @@ public:
* ()
*/
bool isEnabled();
private:
MultiMuxerPrivate::Ptr _muxer;
std::weak_ptr<MediaSourceEvent> _listener;
std::weak_ptr<MultiMuxerPrivate::Listener> _track_listener;
Stamp _stamp[2];
};

View File

@ -97,10 +97,11 @@ bool Recorder::isRecording(type type, const string &vhost, const string &app, co
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){
if (!src) {
WarnL << "未找到相关的MediaSource,startRecord失败:" << vhost << "/" << app << "/" << stream_id;
return false;
}
return src->setupRecord(type,true,customized_path);
return src->setupRecord(type, true, customized_path);
}
bool Recorder::stopRecord(type type, const string &vhost, const string &app, const string &stream_id){

View File

@ -127,7 +127,7 @@ public:
//不重复生成rtmp
_muxer = std::make_shared<MultiMediaSourceMuxer>(getVhost(), getApp(), getId(), _demuxer->getDuration(), enableRtsp, false, enableHls, enableMP4);
_muxer->setMediaListener(getListener());
_muxer->setTrackListener(this);
_muxer->setTrackListener(static_pointer_cast<RtmpMediaSourceImp>(shared_from_this()));
for(auto &track : _demuxer->getTracks(false)){
_muxer->addTrack(track);
track->addDelegate(_muxer);

View File

@ -114,7 +114,7 @@ public:
//不重复生成rtsp
_muxer = std::make_shared<MultiMediaSourceMuxer>(getVhost(), getApp(), getId(), _demuxer->getDuration(), false, enableRtmp, enableHls, enableMP4);
_muxer->setMediaListener(getListener());
_muxer->setTrackListener(this);
_muxer->setTrackListener(static_pointer_cast<RtspMediaSourceImp>(shared_from_this()));
for(auto &track : _demuxer->getTracks(false)){
_muxer->addTrack(track);
track->addDelegate(_muxer);