From ac3d7bd21f4c44a9133f50a353394193bad306de Mon Sep 17 00:00:00 2001 From: cqm Date: Tue, 1 Nov 2022 08:54:56 +0800 Subject: [PATCH] add addDelegate with std::function, remove FrameWriterInterfaceHelper --- Android/app/src/main/cpp/native-lib.cpp | 8 ++++---- README_en.md | 4 ++-- api/source/mk_track.cpp | 4 +--- player/test_player.cpp | 6 ++---- src/Common/MediaSink.cpp | 12 ++++++------ src/Extension/Frame.h | 6 ++++++ src/Player/PlayerBase.cpp | 4 ++-- src/Rtp/GB28181Process.cpp | 4 ++-- 8 files changed, 25 insertions(+), 23 deletions(-) diff --git a/Android/app/src/main/cpp/native-lib.cpp b/Android/app/src/main/cpp/native-lib.cpp index 5713b11e..5567fe98 100644 --- a/Android/app/src/main/cpp/native-lib.cpp +++ b/Android/app/src/main/cpp/native-lib.cpp @@ -232,18 +232,18 @@ JNI_API(jlong, createMediaPlayer, jstring url, jobject callback){ auto viedoTrack = strongPlayer->getTrack(TrackVideo); if (viedoTrack) { - viedoTrack->addDelegate(std::make_shared([globalWeakRef](const Frame::Ptr &frame) { + viedoTrack->addDelegate([globalWeakRef](const Frame::Ptr &frame) { emitEvent((jobject)globalWeakRef,"onData","(L" MediaFrameSign ";)V",makeJavaFrame(env,frame)); return true; - })); + }); } auto audioTrack = strongPlayer->getTrack(TrackAudio); if (audioTrack) { - audioTrack->addDelegate(std::make_shared([globalWeakRef](const Frame::Ptr &frame) { + audioTrack->addDelegate([globalWeakRef](const Frame::Ptr &frame) { emitEvent((jobject)globalWeakRef,"onData","(L" MediaFrameSign ";)V",makeJavaFrame(env,frame)); return true; - })); + }); } }); diff --git a/README_en.md b/README_en.md index f9c56da9..dd031c10 100644 --- a/README_en.md +++ b/README_en.md @@ -213,9 +213,9 @@ git submodule update --init WarnL << "none video Track!"; return; } - viedoTrack->addDelegate(std::make_shared([](const Frame::Ptr &frame) { + viedoTrack->addDelegate([](const Frame::Ptr &frame) { //please decode video here - })); + }); }); player->setOnShutdown([](const SockException &ex) { diff --git a/api/source/mk_track.cpp b/api/source/mk_track.cpp index b4df453d..3190b936 100644 --- a/api/source/mk_track.cpp +++ b/api/source/mk_track.cpp @@ -115,12 +115,10 @@ API_EXPORT int API_CALL mk_track_bit_rate(mk_track track) { API_EXPORT void *API_CALL mk_track_add_delegate(mk_track track, on_mk_frame_out cb, void *user_data) { assert(track && cb); - auto delegate = std::make_shared([cb, user_data](const Frame::Ptr &frame) { + return (*((Track::Ptr *) track))->addDelegate([cb, user_data](const Frame::Ptr &frame) { cb(user_data, (mk_frame) &frame); return true; }); - (*((Track::Ptr *) track))->addDelegate(delegate); - return delegate.get(); } API_EXPORT void API_CALL mk_track_del_delegate(mk_track track, void *tag) { diff --git a/player/test_player.cpp b/player/test_player.cpp index b0bba649..5bbedf23 100644 --- a/player/test_player.cpp +++ b/player/test_player.cpp @@ -83,10 +83,9 @@ int main(int argc, char *argv[]) { return true; }); }); - auto delegate = std::make_shared([decoder](const Frame::Ptr &frame) { + videoTrack->addDelegate([decoder](const Frame::Ptr &frame) { return decoder->inputFrame(frame, false, true); }); - videoTrack->addDelegate(delegate); } if (audioTrack) { @@ -105,10 +104,9 @@ int main(int argc, char *argv[]) { auto len = pcm->get()->nb_samples * pcm->get()->channels * av_get_bytes_per_sample((enum AVSampleFormat)pcm->get()->format); audio_player->playPCM((const char *) (pcm->get()->data[0]), MIN(len, frame->get()->linesize[0])); }); - auto audio_delegate = std::make_shared( [decoder](const Frame::Ptr &frame) { + audioTrack->addDelegate([decoder](const Frame::Ptr &frame) { return decoder->inputFrame(frame, false, true); }); - audioTrack->addDelegate(audio_delegate); } }); diff --git a/src/Common/MediaSink.cpp b/src/Common/MediaSink.cpp index 06d577d3..2cd3749a 100644 --- a/src/Common/MediaSink.cpp +++ b/src/Common/MediaSink.cpp @@ -37,7 +37,7 @@ bool MediaSink::addTrack(const Track::Ptr &track_in) { }; _ticker.resetTime(); - track->addDelegate(std::make_shared([this](const Frame::Ptr &frame) { + track->addDelegate([this](const Frame::Ptr &frame) { if (_all_track_ready) { return onTrackFrame(frame); } @@ -52,7 +52,7 @@ bool MediaSink::addTrack(const Track::Ptr &track_in) { //还有Track未就绪,先缓存之 frame_unread.emplace_back(Frame::getCacheAbleFrame(frame)); return true; - })); + }); return true; } @@ -247,13 +247,13 @@ bool MediaSink::addMuteAudioTrack() { } auto audio = std::make_shared(makeAacConfig(MUTE_ADTS_DATA, ADTS_HEADER_LEN)); _track_map[audio->getTrackType()] = std::make_pair(audio, true); - audio->addDelegate(std::make_shared([this](const Frame::Ptr &frame) { + audio->addDelegate([this](const Frame::Ptr &frame) { return onTrackFrame(frame); - })); + }); _mute_audio_maker = std::make_shared(); - _mute_audio_maker->addDelegate(std::make_shared([audio](const Frame::Ptr &frame) { + _mute_audio_maker->addDelegate([audio](const Frame::Ptr &frame) { return audio->inputFrame(frame); - })); + }); onTrackReady(audio); TraceL << "mute aac track added"; return true; diff --git a/src/Extension/Frame.h b/src/Extension/Frame.h index 0c118bbe..10a046a8 100644 --- a/src/Extension/Frame.h +++ b/src/Extension/Frame.h @@ -318,6 +318,12 @@ public: _delegates.emplace(delegate.get(), delegate); } + FrameWriterInterface* addDelegate(const std::function &cb) { + auto delegate = std::make_shared(cb); + std::lock_guard lck(_mtx); + _delegates.emplace(delegate.get(), delegate); + return delegate.get(); + } /** * 删除代理 */ diff --git a/src/Player/PlayerBase.cpp b/src/Player/PlayerBase.cpp index 9fb6843d..68dc6159 100644 --- a/src/Player/PlayerBase.cpp +++ b/src/Player/PlayerBase.cpp @@ -112,9 +112,9 @@ bool Demuxer::addTrack(const Track::Ptr &track) { } if (_sink->addTrack(track)) { - track->addDelegate(std::make_shared([this](const Frame::Ptr &frame) { + track->addDelegate([this](const Frame::Ptr &frame) { return _sink->inputFrame(frame); - })); + }); return true; } return false; diff --git a/src/Rtp/GB28181Process.cpp b/src/Rtp/GB28181Process.cpp index 6e8e1544..485fed6a 100644 --- a/src/Rtp/GB28181Process.cpp +++ b/src/Rtp/GB28181Process.cpp @@ -136,10 +136,10 @@ bool GB28181Process::inputRtp(bool, const char *data, size_t data_len) { } } // 设置frame回调 - _rtp_decoder[pt]->addDelegate(std::make_shared([this](const Frame::Ptr &frame) { + _rtp_decoder[pt]->addDelegate([this](const Frame::Ptr &frame) { onRtpDecode(frame); return true; - })); + }); } return ref->inputRtp(TrackVideo, (unsigned char *)data, data_len);