Merge branch 'FrameDispatcher_addDelegate_refactor' of github.com:mtdxc/ZLMediaKit

This commit is contained in:
ziyue 2022-11-01 16:36:43 +08:00
commit d04761c2c6
9 changed files with 54 additions and 46 deletions

View File

@ -232,18 +232,18 @@ JNI_API(jlong, createMediaPlayer, jstring url, jobject callback){
auto viedoTrack = strongPlayer->getTrack(TrackVideo);
if (viedoTrack) {
viedoTrack->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([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<FrameWriterInterfaceHelper>([globalWeakRef](const Frame::Ptr &frame) {
audioTrack->addDelegate([globalWeakRef](const Frame::Ptr &frame) {
emitEvent((jobject)globalWeakRef,"onData","(L" MediaFrameSign ";)V",makeJavaFrame(env,frame));
return true;
}));
});
}
});

View File

@ -213,9 +213,9 @@ git submodule update --init
WarnL << "none video Track!";
return;
}
viedoTrack->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([](const Frame::Ptr &frame) {
viedoTrack->addDelegate([](const Frame::Ptr &frame) {
//please decode video here
}));
});
});
player->setOnShutdown([](const SockException &ex) {

View File

@ -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<FrameWriterInterfaceHelper>([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) {

View File

@ -83,10 +83,9 @@ int main(int argc, char *argv[]) {
return true;
});
});
auto delegate = std::make_shared<FrameWriterInterfaceHelper>([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<FrameWriterInterfaceHelper>( [decoder](const Frame::Ptr &frame) {
audioTrack->addDelegate([decoder](const Frame::Ptr &frame) {
return decoder->inputFrame(frame, false, true);
});
audioTrack->addDelegate(audio_delegate);
}
});

View File

@ -37,7 +37,7 @@ bool MediaSink::addTrack(const Track::Ptr &track_in) {
};
_ticker.resetTime();
track->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([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<AACTrack>(makeAacConfig(MUTE_ADTS_DATA, ADTS_HEADER_LEN));
_track_map[audio->getTrackType()] = std::make_pair(audio, true);
audio->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) {
audio->addDelegate([this](const Frame::Ptr &frame) {
return onTrackFrame(frame);
}));
});
_mute_audio_maker = std::make_shared<MuteAudioMaker>();
_mute_audio_maker->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([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;

View File

@ -210,5 +210,39 @@ void FrameMerger::flush() {
}
clear();
}
/**
* function
*/
class FrameWriterInterfaceHelper : public FrameWriterInterface {
public:
typedef std::shared_ptr<FrameWriterInterfaceHelper> Ptr;
typedef std::function<bool(const Frame::Ptr &frame)> onWriteFrame;
/**
* inputFrame后触发onWriteFrame回调
*/
FrameWriterInterfaceHelper(const onWriteFrame& cb){
_writeCallback = cb;
}
virtual ~FrameWriterInterfaceHelper(){}
/**
*
*/
bool inputFrame(const Frame::Ptr &frame) override {
return _writeCallback(frame);
}
private:
onWriteFrame _writeCallback;
};
FrameWriterInterface* FrameDispatcher::addDelegate(const std::function<bool(const Frame::Ptr &frame)> &cb) {
auto delegate = std::make_shared<FrameWriterInterfaceHelper>(cb);
std::lock_guard<std::mutex> lck(_mtx);
_delegates.emplace(delegate.get(), delegate);
return delegate.get();
}
}//namespace mediakit

View File

@ -278,29 +278,6 @@ public:
virtual void flush() {};
};
/**
* function
*/
class FrameWriterInterfaceHelper : public FrameWriterInterface {
public:
typedef std::shared_ptr<FrameWriterInterfaceHelper> Ptr;
typedef std::function<bool(const Frame::Ptr &frame)> onWriteFrame;
/**
* inputFrame后触发onWriteFrame回调
*/
FrameWriterInterfaceHelper(const onWriteFrame &cb) { _writeCallback = cb; }
virtual ~FrameWriterInterfaceHelper() = default;
/**
*
*/
bool inputFrame(const Frame::Ptr &frame) override { return _writeCallback(frame); }
private:
onWriteFrame _writeCallback;
};
/**
*
*/
@ -318,6 +295,7 @@ public:
_delegates.emplace(delegate.get(), delegate);
}
FrameWriterInterface* addDelegate(const std::function<bool(const Frame::Ptr &frame)> &cb);
/**
*
*/

View File

@ -112,9 +112,9 @@ bool Demuxer::addTrack(const Track::Ptr &track) {
}
if (_sink->addTrack(track)) {
track->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) {
track->addDelegate([this](const Frame::Ptr &frame) {
return _sink->inputFrame(frame);
}));
});
return true;
}
return false;

View File

@ -136,10 +136,10 @@ bool GB28181Process::inputRtp(bool, const char *data, size_t data_len) {
}
}
// 设置frame回调
_rtp_decoder[pt]->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([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);