mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 19:00:01 +08:00
Refine: 精简复用MediaSink相关代码
This commit is contained in:
parent
84baf920c1
commit
8f60ec9900
@ -158,7 +158,7 @@ void MediaSink::emitAllTrackReady() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
pr.second.for_each([&](const Frame::Ptr &frame) {
|
pr.second.for_each([&](const Frame::Ptr &frame) {
|
||||||
onTrackFrame(frame);
|
inputFrame(frame);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
_frame_unread.clear();
|
_frame_unread.clear();
|
||||||
|
@ -65,90 +65,35 @@ PlayerBase::PlayerBase() {
|
|||||||
|
|
||||||
///////////////////////////DemuxerSink//////////////////////////////
|
///////////////////////////DemuxerSink//////////////////////////////
|
||||||
|
|
||||||
class DemuxerSink : public MediaSink {
|
void MediaSinkDelegate::setTrackListener(TrackListener *listener) {
|
||||||
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) {
|
|
||||||
return inputFrame(frame);
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DemuxerSink::setTrackListener(TrackListener *listener) {
|
|
||||||
_listener = listener;
|
_listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DemuxerSink::onTrackReady(const Track::Ptr &track) {
|
bool MediaSinkDelegate::onTrackReady(const Track::Ptr &track) {
|
||||||
_tracks[track->getTrackType()] = track->clone();
|
if (_listener) {
|
||||||
|
_listener->addTrack(track);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DemuxerSink::onAllTrackReady() {
|
void MediaSinkDelegate::onAllTrackReady() {
|
||||||
if (!_listener) {
|
if (_listener) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (auto &track : _tracks) {
|
|
||||||
if (track) {
|
|
||||||
_listener->addTrack(track);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_listener->addTrackCompleted();
|
_listener->addTrackCompleted();
|
||||||
}
|
|
||||||
|
|
||||||
bool DemuxerSink::onTrackFrame(const Frame::Ptr &frame) {
|
|
||||||
return _tracks[frame->getTrackType()]->inputFrame(frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DemuxerSink::resetTracks() {
|
|
||||||
MediaSink::resetTracks();
|
|
||||||
for (auto &track : _tracks) {
|
|
||||||
track = nullptr;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MediaSinkDelegate::resetTracks() {
|
||||||
|
MediaSink::resetTracks();
|
||||||
if (_listener) {
|
if (_listener) {
|
||||||
_listener->resetTracks();
|
_listener->resetTracks();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<Track::Ptr> DemuxerSink::getTracks(bool ready) const {
|
|
||||||
vector<Track::Ptr> ret;
|
|
||||||
for (auto &track : _tracks) {
|
|
||||||
if (!track || (ready && !track->ready())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
ret.emplace_back(track);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////Demuxer//////////////////////////////
|
///////////////////////////Demuxer//////////////////////////////
|
||||||
|
|
||||||
void Demuxer::setTrackListener(TrackListener *listener, bool wait_track_ready) {
|
void Demuxer::setTrackListener(TrackListener *listener, bool wait_track_ready) {
|
||||||
if (wait_track_ready) {
|
if (wait_track_ready) {
|
||||||
auto sink = std::make_shared<DemuxerSink>();
|
auto sink = std::make_shared<MediaSinkDelegate>();
|
||||||
sink->setTrackListener(listener);
|
sink->setTrackListener(listener);
|
||||||
_sink = std::move(sink);
|
_sink = std::move(sink);
|
||||||
}
|
}
|
||||||
@ -158,8 +103,16 @@ void Demuxer::setTrackListener(TrackListener *listener, bool wait_track_ready) {
|
|||||||
bool Demuxer::addTrack(const Track::Ptr &track) {
|
bool Demuxer::addTrack(const Track::Ptr &track) {
|
||||||
if (!_sink) {
|
if (!_sink) {
|
||||||
_origin_track.emplace_back(track);
|
_origin_track.emplace_back(track);
|
||||||
|
return _listener ? _listener->addTrack(track) : false;
|
||||||
}
|
}
|
||||||
return _sink ? _sink->addTrack(track) : (_listener ? _listener->addTrack(track) : false);
|
|
||||||
|
if (_sink->addTrack(track)) {
|
||||||
|
track->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) {
|
||||||
|
return _sink->inputFrame(frame);
|
||||||
|
}));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Demuxer::addTrackCompleted() {
|
void Demuxer::addTrackCompleted() {
|
||||||
|
@ -238,16 +238,37 @@ protected:
|
|||||||
std::shared_ptr<Delegate> _delegate;
|
std::shared_ptr<Delegate> _delegate;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class MediaSinkDelegate : public MediaSink {
|
||||||
|
public:
|
||||||
|
MediaSinkDelegate() = default;
|
||||||
|
~MediaSinkDelegate() override = default;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置track监听器
|
||||||
|
*/
|
||||||
|
void setTrackListener(TrackListener *listener);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void resetTracks() override;
|
||||||
|
bool onTrackReady(const Track::Ptr & track) override;
|
||||||
|
void onAllTrackReady() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
TrackListener *_listener = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
class Demuxer : protected TrackListener, public TrackSource {
|
class Demuxer : protected TrackListener, public TrackSource {
|
||||||
public:
|
public:
|
||||||
Demuxer() = default;
|
Demuxer() = default;
|
||||||
~Demuxer() override = default;
|
~Demuxer() override = default;
|
||||||
|
|
||||||
void setTrackListener(TrackListener *listener, bool wait_track_ready = false);
|
void setTrackListener(TrackListener *listener, bool wait_track_ready = false);
|
||||||
|
vector<Track::Ptr> getTracks(bool trackReady = true) const override;
|
||||||
|
|
||||||
|
protected:
|
||||||
bool addTrack(const Track::Ptr &track) override;
|
bool addTrack(const Track::Ptr &track) override;
|
||||||
void addTrackCompleted() override;
|
void addTrackCompleted() override;
|
||||||
void resetTracks() override;
|
void resetTracks() override;
|
||||||
vector<Track::Ptr> getTracks(bool trackReady = true) const override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MediaSink::Ptr _sink;
|
MediaSink::Ptr _sink;
|
||||||
|
Loading…
Reference in New Issue
Block a user