Demuxer: 解复用减少一次MediaSink的数据过滤

This commit is contained in:
ziyue 2021-11-10 13:44:29 +08:00
parent 2a8a6cac27
commit e38f2253ff
4 changed files with 89 additions and 23 deletions

View File

@ -62,9 +62,32 @@ PlayerBase::PlayerBase() {
this->mINI::operator[](kBeatIntervalMS) = 5000;
}
///////////////////////////Demuxer//////////////////////////////
///////////////////////////DemuxerSink//////////////////////////////
bool Demuxer::addTrack(const Track::Ptr &track) {
class DemuxerSink : public MediaSink {
public:
DemuxerSink() = default;
~DemuxerSink() override = default;
/**
* track监听器
*/
void setTrackListener(TrackListener *listener);
vector<Track::Ptr> getTracks(bool ready = true) const override;
protected:
bool addTrack(const Track::Ptr & track) override;
void resetTracks() override;
bool onTrackReady(const Track::Ptr & track) override;
void onAllTrackReady() override;
bool onTrackFrame(const Frame::Ptr &frame) override;
private:
Track::Ptr _tracks[TrackMax];
TrackListener *_listener = nullptr;
};
bool DemuxerSink::addTrack(const Track::Ptr &track) {
auto ret = MediaSink::addTrack(track);
if (ret) {
track->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) {
@ -74,16 +97,16 @@ bool Demuxer::addTrack(const Track::Ptr &track) {
return ret;
}
void Demuxer::setTrackListener(TrackListener *listener) {
void DemuxerSink::setTrackListener(TrackListener *listener) {
_listener = listener;
}
bool Demuxer::onTrackReady(const Track::Ptr &track) {
bool DemuxerSink::onTrackReady(const Track::Ptr &track) {
_tracks[track->getTrackType()] = track->clone();
return true;
}
void Demuxer::onAllTrackReady() {
void DemuxerSink::onAllTrackReady() {
if (!_listener) {
return;
}
@ -95,11 +118,11 @@ void Demuxer::onAllTrackReady() {
_listener->addTrackCompleted();
}
bool Demuxer::onTrackFrame(const Frame::Ptr &frame) {
bool DemuxerSink::onTrackFrame(const Frame::Ptr &frame) {
return _tracks[frame->getTrackType()]->inputFrame(frame);
}
void Demuxer::resetTracks() {
void DemuxerSink::resetTracks() {
MediaSink::resetTracks();
for (auto &track : _tracks) {
track = nullptr;
@ -109,7 +132,7 @@ void Demuxer::resetTracks() {
}
}
vector<Track::Ptr> Demuxer::getTracks(bool ready) const {
vector<Track::Ptr> DemuxerSink::getTracks(bool ready) const {
vector<Track::Ptr> ret;
for (auto &track : _tracks) {
if (!track || (ready && !track->ready())) {
@ -120,4 +143,53 @@ vector<Track::Ptr> Demuxer::getTracks(bool ready) const {
return ret;
}
///////////////////////////Demuxer//////////////////////////////
void Demuxer::setTrackListener(TrackListener *listener, bool wait_track_ready) {
if (wait_track_ready) {
auto sink = std::make_shared<DemuxerSink>();
sink->setTrackListener(listener);
_sink = std::move(sink);
}
_listener = listener;
}
bool Demuxer::addTrack(const Track::Ptr &track) {
if (!_sink) {
_origin_track.emplace_back(track);
}
return _sink ? _sink->addTrack(track) : (_listener ? _listener->addTrack(track) : false);
}
void Demuxer::addTrackCompleted() {
if (_sink) {
_sink->addTrackCompleted();
} else if (_listener) {
_listener->addTrackCompleted();
}
}
void Demuxer::resetTracks() {
if (_sink) {
_sink->resetTracks();
} else if (_listener) {
_listener->resetTracks();
}
}
vector<Track::Ptr> Demuxer::getTracks(bool ready) const {
if (_sink) {
return _sink->getTracks(ready);
}
vector<Track::Ptr> ret;
for (auto &track : _origin_track) {
if (ready && !track->ready()) {
continue;
}
ret.emplace_back(track);
}
return ret;
}
} /* namespace mediakit */

View File

@ -238,27 +238,21 @@ protected:
std::shared_ptr<Delegate> _delegate;
};
class Demuxer : protected MediaSink {
class Demuxer : protected TrackListener, public TrackSource {
public:
Demuxer() = default;
~Demuxer() override = default;
/**
* track监听器
*/
void setTrackListener(TrackListener *listener);
vector<Track::Ptr> getTracks(bool ready = true) const override;
protected:
void setTrackListener(TrackListener *listener, bool wait_track_ready = false);
bool addTrack(const Track::Ptr &track) override;
void addTrackCompleted() override;
void resetTracks() override;
bool onTrackReady(const Track::Ptr & track) override;
void onAllTrackReady() override;
bool onTrackFrame(const Frame::Ptr &frame) override;
vector<Track::Ptr> getTracks(bool trackReady = true) const override;
private:
Track::Ptr _tracks[TrackMax];
MediaSink::Ptr _sink;
TrackListener *_listener = nullptr;
vector<Track::Ptr> _origin_track;
};
} /* namespace mediakit */

View File

@ -98,7 +98,7 @@ private:
_rtmp_src->setMetaData(val);
}
_demuxer = std::make_shared<RtmpDemuxer>();
_demuxer->setTrackListener(this);
_demuxer->setTrackListener(this, true);
_demuxer->loadMetaData(val);
}

View File

@ -76,7 +76,7 @@ private:
_rtsp_media_src->setSdp(sdp);
}
_demuxer = std::make_shared<RtspDemuxer>();
_demuxer->setTrackListener(this);
_demuxer->setTrackListener(this, true);
_demuxer->loadSdp(sdp);
return true;
}