From e38f2253fff52ffd7be27049ae2c70d227593713 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Wed, 10 Nov 2021 13:44:29 +0800 Subject: [PATCH] =?UTF-8?q?Demuxer:=20=E8=A7=A3=E5=A4=8D=E7=94=A8=E5=87=8F?= =?UTF-8?q?=E5=B0=91=E4=B8=80=E6=AC=A1MediaSink=E7=9A=84=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Player/PlayerBase.cpp | 88 +++++++++++++++++++++++++++++++++++---- src/Player/PlayerBase.h | 20 ++++----- src/Rtmp/RtmpPlayerImp.h | 2 +- src/Rtsp/RtspPlayerImp.h | 2 +- 4 files changed, 89 insertions(+), 23 deletions(-) diff --git a/src/Player/PlayerBase.cpp b/src/Player/PlayerBase.cpp index 1ee52c98..ffca3fc8 100644 --- a/src/Player/PlayerBase.cpp +++ b/src/Player/PlayerBase.cpp @@ -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 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([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 Demuxer::getTracks(bool ready) const { +vector DemuxerSink::getTracks(bool ready) const { vector ret; for (auto &track : _tracks) { if (!track || (ready && !track->ready())) { @@ -120,4 +143,53 @@ vector 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(); + 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 Demuxer::getTracks(bool ready) const { + if (_sink) { + return _sink->getTracks(ready); + } + + vector ret; + for (auto &track : _origin_track) { + if (ready && !track->ready()) { + continue; + } + ret.emplace_back(track); + } + return ret; +} + } /* namespace mediakit */ diff --git a/src/Player/PlayerBase.h b/src/Player/PlayerBase.h index 8877f2f2..a9730435 100644 --- a/src/Player/PlayerBase.h +++ b/src/Player/PlayerBase.h @@ -238,27 +238,21 @@ protected: std::shared_ptr _delegate; }; -class Demuxer : protected MediaSink { +class Demuxer : protected TrackListener, public TrackSource { public: Demuxer() = default; ~Demuxer() override = default; - /** - * 设置track监听器 - */ - void setTrackListener(TrackListener *listener); - vector getTracks(bool ready = true) const override; - -protected: - bool addTrack(const Track::Ptr & track) override; + 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 getTracks(bool trackReady = true) const override; private: - Track::Ptr _tracks[TrackMax]; + MediaSink::Ptr _sink; TrackListener *_listener = nullptr; + vector _origin_track; }; } /* namespace mediakit */ diff --git a/src/Rtmp/RtmpPlayerImp.h b/src/Rtmp/RtmpPlayerImp.h index f3a106ce..b0056aa8 100644 --- a/src/Rtmp/RtmpPlayerImp.h +++ b/src/Rtmp/RtmpPlayerImp.h @@ -98,7 +98,7 @@ private: _rtmp_src->setMetaData(val); } _demuxer = std::make_shared(); - _demuxer->setTrackListener(this); + _demuxer->setTrackListener(this, true); _demuxer->loadMetaData(val); } diff --git a/src/Rtsp/RtspPlayerImp.h b/src/Rtsp/RtspPlayerImp.h index 103a94c9..51aea0a9 100644 --- a/src/Rtsp/RtspPlayerImp.h +++ b/src/Rtsp/RtspPlayerImp.h @@ -76,7 +76,7 @@ private: _rtsp_media_src->setSdp(sdp); } _demuxer = std::make_shared(); - _demuxer->setTrackListener(this); + _demuxer->setTrackListener(this, true); _demuxer->loadSdp(sdp); return true; }