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;
|
||||
}
|
||||
pr.second.for_each([&](const Frame::Ptr &frame) {
|
||||
onTrackFrame(frame);
|
||||
inputFrame(frame);
|
||||
});
|
||||
}
|
||||
_frame_unread.clear();
|
||||
|
@ -65,90 +65,35 @@ PlayerBase::PlayerBase() {
|
||||
|
||||
///////////////////////////DemuxerSink//////////////////////////////
|
||||
|
||||
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) {
|
||||
return inputFrame(frame);
|
||||
}));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void DemuxerSink::setTrackListener(TrackListener *listener) {
|
||||
void MediaSinkDelegate::setTrackListener(TrackListener *listener) {
|
||||
_listener = listener;
|
||||
}
|
||||
|
||||
bool DemuxerSink::onTrackReady(const Track::Ptr &track) {
|
||||
_tracks[track->getTrackType()] = track->clone();
|
||||
bool MediaSinkDelegate::onTrackReady(const Track::Ptr &track) {
|
||||
if (_listener) {
|
||||
_listener->addTrack(track);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void DemuxerSink::onAllTrackReady() {
|
||||
if (!_listener) {
|
||||
return;
|
||||
}
|
||||
for (auto &track : _tracks) {
|
||||
if (track) {
|
||||
_listener->addTrack(track);
|
||||
}
|
||||
}
|
||||
void MediaSinkDelegate::onAllTrackReady() {
|
||||
if (_listener) {
|
||||
_listener->addTrackCompleted();
|
||||
}
|
||||
|
||||
bool DemuxerSink::onTrackFrame(const Frame::Ptr &frame) {
|
||||
return _tracks[frame->getTrackType()]->inputFrame(frame);
|
||||
}
|
||||
|
||||
void DemuxerSink::resetTracks() {
|
||||
void MediaSinkDelegate::resetTracks() {
|
||||
MediaSink::resetTracks();
|
||||
for (auto &track : _tracks) {
|
||||
track = nullptr;
|
||||
}
|
||||
if (_listener) {
|
||||
_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//////////////////////////////
|
||||
|
||||
void Demuxer::setTrackListener(TrackListener *listener, bool wait_track_ready) {
|
||||
if (wait_track_ready) {
|
||||
auto sink = std::make_shared<DemuxerSink>();
|
||||
auto sink = std::make_shared<MediaSinkDelegate>();
|
||||
sink->setTrackListener(listener);
|
||||
_sink = std::move(sink);
|
||||
}
|
||||
@ -158,8 +103,16 @@ void Demuxer::setTrackListener(TrackListener *listener, bool wait_track_ready) {
|
||||
bool Demuxer::addTrack(const Track::Ptr &track) {
|
||||
if (!_sink) {
|
||||
_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() {
|
||||
|
@ -238,16 +238,37 @@ protected:
|
||||
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 {
|
||||
public:
|
||||
Demuxer() = default;
|
||||
~Demuxer() override = default;
|
||||
|
||||
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;
|
||||
void addTrackCompleted() override;
|
||||
void resetTracks() override;
|
||||
vector<Track::Ptr> getTracks(bool trackReady = true) const override;
|
||||
|
||||
private:
|
||||
MediaSink::Ptr _sink;
|
||||
|
Loading…
Reference in New Issue
Block a user