优化getTrack接口相关代码

This commit is contained in:
xiongziliang 2020-09-06 17:54:52 +08:00
parent 28262b158f
commit ac70597216
9 changed files with 34 additions and 45 deletions

View File

@ -54,15 +54,11 @@ const string& MediaSource::getId() const {
} }
vector<Track::Ptr> MediaSource::getTracks(bool ready) const { vector<Track::Ptr> MediaSource::getTracks(bool ready) const {
auto strongPtr = _track_source.lock(); auto listener = _listener.lock();
if(strongPtr){ if(!listener){
return strongPtr->getTracks(ready);
}
return vector<Track::Ptr>(); return vector<Track::Ptr>();
} }
return listener->getTracks(const_cast<MediaSource &>(*this), ready);
void MediaSource::setTrackSource(const std::weak_ptr<TrackSource> &track_src) {
_track_source = track_src;
} }
void MediaSource::setListener(const std::weak_ptr<MediaSourceEvent> &listener){ void MediaSource::setListener(const std::weak_ptr<MediaSourceEvent> &listener){
@ -549,6 +545,14 @@ bool MediaSourceEventInterceptor::isRecording(MediaSource &sender, Recorder::typ
return listener->isRecording(sender, type); return listener->isRecording(sender, type);
} }
vector<Track::Ptr> MediaSourceEventInterceptor::getTracks(MediaSource &sender, bool trackReady) const {
auto listener = _listener.lock();
if (!listener) {
return vector<Track::Ptr>();
}
return listener->getTracks(sender, trackReady);
}
/////////////////////////////////////FlushPolicy////////////////////////////////////// /////////////////////////////////////FlushPolicy//////////////////////////////////////
static bool isFlushAble_default(bool is_video, uint32_t last_stamp, uint32_t new_stamp, int cache_size) { static bool isFlushAble_default(bool is_video, uint32_t last_stamp, uint32_t new_stamp, int cache_size) {

View File

@ -59,6 +59,8 @@ public:
virtual bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) { return false; }; 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; }; virtual bool isRecording(MediaSource &sender, Recorder::type type) { return false; };
// 获取所有track相关信息
virtual vector<Track::Ptr> getTracks(MediaSource &sender, bool trackReady = true) const { return vector<Track::Ptr>(); };
private: private:
Timer::Ptr _async_close_timer; Timer::Ptr _async_close_timer;
@ -74,9 +76,10 @@ public:
bool close(MediaSource &sender, bool force) override; bool close(MediaSource &sender, bool force) override;
int totalReaderCount(MediaSource &sender) override; int totalReaderCount(MediaSource &sender) override;
void onNoneReader(MediaSource &sender) override; void onNoneReader(MediaSource &sender) override;
void onRegist(MediaSource &sender, bool regist) override;; void onRegist(MediaSource &sender, bool regist) override;
bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) override; bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) override;
bool isRecording(MediaSource &sender, Recorder::type type) override; bool isRecording(MediaSource &sender, Recorder::type type) override;
vector<Track::Ptr> getTracks(MediaSource &sender, bool trackReady = true) const override;
protected: protected:
std::weak_ptr<MediaSourceEvent> _listener; std::weak_ptr<MediaSourceEvent> _listener;
@ -116,6 +119,8 @@ public:
MediaSource(const string &schema, const string &vhost, const string &app, const string &stream_id) ; MediaSource(const string &schema, const string &vhost, const string &app, const string &stream_id) ;
virtual ~MediaSource() ; virtual ~MediaSource() ;
////////////////获取MediaSource相关信息////////////////
// 获取协议类型 // 获取协议类型
const string& getSchema() const; const string& getSchema() const;
// 虚拟主机 // 虚拟主机
@ -125,11 +130,16 @@ public:
// 流id // 流id
const string& getId() const; const string& getId() const;
// 设置TrackSource
void setTrackSource(const std::weak_ptr<TrackSource> &track_src);
// 获取所有Track // 获取所有Track
vector<Track::Ptr> getTracks(bool ready = true) const override; vector<Track::Ptr> getTracks(bool ready = true) const override;
// 获取流当前时间戳
virtual uint32_t getTimeStamp(TrackType type) { return 0; };
// 设置时间戳
virtual void setTimeStamp(uint32_t stamp) {};
////////////////MediaSourceEvent相关接口实现////////////////
// 设置监听者 // 设置监听者
void setListener(const std::weak_ptr<MediaSourceEvent> &listener); void setListener(const std::weak_ptr<MediaSourceEvent> &listener);
// 获取监听者 // 获取监听者
@ -140,11 +150,6 @@ public:
// 观看者个数,包括(hls/rtsp/rtmp) // 观看者个数,包括(hls/rtsp/rtmp)
virtual int totalReaderCount(); virtual int totalReaderCount();
// 获取流当前时间戳
virtual uint32_t getTimeStamp(TrackType type) { return 0; };
// 设置时间戳
virtual void setTimeStamp(uint32_t stamp) {};
// 拖动进度条 // 拖动进度条
bool seekTo(uint32_t stamp); bool seekTo(uint32_t stamp);
// 关闭该流 // 关闭该流
@ -156,6 +161,8 @@ public:
// 获取录制状态 // 获取录制状态
bool isRecording(Recorder::type type); bool isRecording(Recorder::type type);
////////////////static方法查找或生成MediaSource////////////////
// 同步查找流 // 同步查找流
static Ptr find(const string &schema, const string &vhost, const string &app, const string &id); static Ptr find(const string &schema, const string &vhost, const string &app, const string &id);
// 异步查找流 // 异步查找流
@ -180,7 +187,6 @@ private:
string _vhost; string _vhost;
string _app; string _app;
string _stream_id; string _stream_id;
weak_ptr<TrackSource> _track_source;
std::weak_ptr<MediaSourceEvent> _listener; std::weak_ptr<MediaSourceEvent> _listener;
}; };

View File

@ -96,7 +96,6 @@ bool MultiMuxerPrivate::setupRecord(MediaSource &sender, Recorder::type type, bo
if (hls_src) { if (hls_src) {
//设置HlsMediaSource的事件监听器 //设置HlsMediaSource的事件监听器
hls_src->setListener(_listener); hls_src->setListener(_listener);
hls_src->setTrackSource(shared_from_this());
} }
} else if (!start && _hls) { } else if (!start && _hls) {
//停止录制 //停止录制
@ -189,19 +188,12 @@ void MultiMuxerPrivate::onTrackFrame(const Frame::Ptr &frame) {
void MultiMuxerPrivate::onAllTrackReady() { void MultiMuxerPrivate::onAllTrackReady() {
if (_rtmp) { if (_rtmp) {
_rtmp->setTrackSource(shared_from_this());
_rtmp->onAllTrackReady(); _rtmp->onAllTrackReady();
} }
if (_rtsp) { if (_rtsp) {
_rtsp->setTrackSource(shared_from_this());
_rtsp->onAllTrackReady(); _rtsp->onAllTrackReady();
} }
auto hls_src = getHlsMediaSource();
if (hls_src) {
hls_src->setTrackSource(shared_from_this());
}
if (_track_listener) { if (_track_listener) {
_track_listener->onAllTrackReady(); _track_listener->onAllTrackReady();
} }
@ -243,7 +235,7 @@ void MultiMediaSourceMuxer::setTimeStamp(uint32_t stamp) {
_muxer->setTimeStamp(stamp); _muxer->setTimeStamp(stamp);
} }
vector<Track::Ptr> MultiMediaSourceMuxer::getTracks(bool trackReady) const { vector<Track::Ptr> MultiMediaSourceMuxer::getTracks(MediaSource &sender, bool trackReady) const {
return _muxer->getTracks(trackReady); return _muxer->getTracks(trackReady);
} }

View File

@ -57,7 +57,7 @@ private:
std::weak_ptr<MediaSourceEvent> _listener; std::weak_ptr<MediaSourceEvent> _listener;
}; };
class MultiMediaSourceMuxer : public MediaSourceEventInterceptor, public MediaSinkInterface, public TrackSource, public MultiMuxerPrivate::Listener, public std::enable_shared_from_this<MultiMediaSourceMuxer>{ class MultiMediaSourceMuxer : public MediaSourceEventInterceptor, public MediaSinkInterface, 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;
@ -94,16 +94,14 @@ public:
*/ */
void setTimeStamp(uint32_t stamp); void setTimeStamp(uint32_t stamp);
/////////////////////////////////TrackSource override///////////////////////////////// /////////////////////////////////MediaSourceEvent override/////////////////////////////////
/** /**
* Track * Track
* @param trackReady track * @param trackReady track
* @return Track * @return Track
*/ */
vector<Track::Ptr> getTracks(bool trackReady = true) const override; vector<Track::Ptr> getTracks(MediaSource &sender, bool trackReady = true) const override;
/////////////////////////////////MediaSourceEvent override/////////////////////////////////
/** /**
* *

View File

@ -96,7 +96,7 @@ void PlayerProxy::play(const string &strUrlTmp) {
return; return;
} }
if(strongSelf->_muxer) { if(strongSelf->_muxer) {
auto tracks = strongSelf->getTracks(false); auto tracks = strongSelf->MediaPlayer::getTracks(false);
for (auto & track : tracks){ for (auto & track : tracks){
track->delDelegate(strongSelf->_muxer.get()); track->delDelegate(strongSelf->_muxer.get());
} }
@ -271,7 +271,6 @@ void PlayerProxy::onPlaySuccess() {
_muxer->addTrackCompleted(); _muxer->addTrackCompleted();
if (_pMediaSrc) { if (_pMediaSrc) {
_pMediaSrc->setTrackSource(_muxer);
//让_muxer对象拦截一部分事件(比如说录像相关事件) //让_muxer对象拦截一部分事件(比如说录像相关事件)
_pMediaSrc->setListener(_muxer); _pMediaSrc->setListener(_muxer);
} }

View File

@ -112,12 +112,11 @@ public:
* _muxer触发的所有Track就绪的事件 * _muxer触发的所有Track就绪的事件
*/ */
void onAllTrackReady() override{ void onAllTrackReady() override{
setTrackSource(_muxer);
_all_track_ready = true; _all_track_ready = true;
if (_recreate_metadata) { if (_recreate_metadata) {
//更新metadata //更新metadata
for (auto &track : _muxer->getTracks()) { for (auto &track : _muxer->getTracks(*this)) {
Metadata::addTrack(_metadata, track); Metadata::addTrack(_metadata, track);
} }
RtmpMediaSource::updateMetaData(_metadata); RtmpMediaSource::updateMetaData(_metadata);

View File

@ -46,11 +46,6 @@ public:
_media_src->setMetaData(getMetadata()); _media_src->setMetaData(getMetadata());
} }
// 设置TrackSource
void setTrackSource(const std::weak_ptr<TrackSource> &track_src){
_media_src->setTrackSource(track_src);
}
private: private:
RtmpMediaSource::Ptr _media_src; RtmpMediaSource::Ptr _media_src;
}; };

View File

@ -100,9 +100,9 @@ public:
* _muxer触发的所有Track就绪的事件 * _muxer触发的所有Track就绪的事件
*/ */
void onAllTrackReady() override{ void onAllTrackReady() override{
setTrackSource(_muxer);
_all_track_ready = true; _all_track_ready = true;
} }
private: private:
RtspDemuxer::Ptr _demuxer; RtspDemuxer::Ptr _demuxer;
MultiMediaSourceMuxer::Ptr _muxer; MultiMediaSourceMuxer::Ptr _muxer;

View File

@ -45,10 +45,6 @@ public:
_mediaSouce->setSdp(getSdp()); _mediaSouce->setSdp(getSdp());
} }
// 设置TrackSource
void setTrackSource(const std::weak_ptr<TrackSource> &track_src){
_mediaSouce->setTrackSource(track_src);
}
private: private:
RtspMediaSource::Ptr _mediaSouce; RtspMediaSource::Ptr _mediaSouce;
}; };