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

View File

@ -231,6 +231,7 @@ MultiMediaSourceMuxer::MultiMediaSourceMuxer(const string &vhost,
bool enable_hls, bool enable_hls,
bool enable_mp4) { bool enable_mp4) {
_muxer.reset(new MultiMuxerPrivate(vhost, app, stream, dur_sec, enable_rtsp, enable_rtmp, enable_hls, 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) { void MultiMediaSourceMuxer::setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener) {
@ -238,6 +239,10 @@ void MultiMediaSourceMuxer::setMediaListener(const std::weak_ptr<MediaSourceEven
_listener = listener; _listener = listener;
} }
void MultiMediaSourceMuxer::setTrackListener(const std::weak_ptr<MultiMuxerPrivate::Listener> &listener) {
_track_listener = listener;
}
int MultiMediaSourceMuxer::totalReaderCount() const { int MultiMediaSourceMuxer::totalReaderCount() const {
return _muxer->totalReaderCount(); return _muxer->totalReaderCount();
} }
@ -246,10 +251,6 @@ void MultiMediaSourceMuxer::setTimeStamp(uint32_t stamp) {
_muxer->setTimeStamp(stamp); _muxer->setTimeStamp(stamp);
} }
void MultiMediaSourceMuxer::setTrackListener(Listener *listener) {
_muxer->setTrackListener(listener);
}
vector<Track::Ptr> MultiMediaSourceMuxer::getTracks(bool trackReady) const { vector<Track::Ptr> MultiMediaSourceMuxer::getTracks(bool trackReady) const {
return _muxer->getTracks(trackReady); return _muxer->getTracks(trackReady);
} }
@ -310,6 +311,14 @@ void MultiMediaSourceMuxer::addTrackCompleted() {
_muxer->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() { void MultiMediaSourceMuxer::resetTracks() {
_muxer->resetTracks(); _muxer->resetTracks();
} }

View File

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

View File

@ -98,6 +98,7 @@ 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){ 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); auto src = getMediaSource(vhost, app, stream_id);
if (!src) { if (!src) {
WarnL << "未找到相关的MediaSource,startRecord失败:" << vhost << "/" << app << "/" << stream_id;
return false; return false;
} }
return src->setupRecord(type, true, customized_path); return src->setupRecord(type, true, customized_path);

View File

@ -127,7 +127,7 @@ public:
//不重复生成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->setMediaListener(getListener()); _muxer->setMediaListener(getListener());
_muxer->setTrackListener(this); _muxer->setTrackListener(static_pointer_cast<RtmpMediaSourceImp>(shared_from_this()));
for(auto &track : _demuxer->getTracks(false)){ for(auto &track : _demuxer->getTracks(false)){
_muxer->addTrack(track); _muxer->addTrack(track);
track->addDelegate(_muxer); track->addDelegate(_muxer);

View File

@ -114,7 +114,7 @@ public:
//不重复生成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->setMediaListener(getListener()); _muxer->setMediaListener(getListener());
_muxer->setTrackListener(this); _muxer->setTrackListener(static_pointer_cast<RtspMediaSourceImp>(shared_from_this()));
for(auto &track : _demuxer->getTracks(false)){ for(auto &track : _demuxer->getTracks(false)){
_muxer->addTrack(track); _muxer->addTrack(track);
track->addDelegate(_muxer); track->addDelegate(_muxer);