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

View File

@ -96,7 +96,6 @@ bool MultiMuxerPrivate::setupRecord(MediaSource &sender, Recorder::type type, bo
if (hls_src) {
//设置HlsMediaSource的事件监听器
hls_src->setListener(_listener);
hls_src->setTrackSource(shared_from_this());
}
} else if (!start && _hls) {
//停止录制
@ -189,19 +188,12 @@ void MultiMuxerPrivate::onTrackFrame(const Frame::Ptr &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 (_track_listener) {
_track_listener->onAllTrackReady();
}
@ -243,7 +235,7 @@ void MultiMediaSourceMuxer::setTimeStamp(uint32_t 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);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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