Commit d04761c2 by ziyue

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

parents bfe118d9 dde35254
......@@ -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;
}));
});
}
});
......
......@@ -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) {
......
......@@ -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) {
......
......@@ -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);
}
});
......
......@@ -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;
......
......@@ -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
......@@ -279,29 +279,6 @@ public:
};
/**
* 写帧接口转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;
};
/**
* 支持代理转发的帧环形缓存
*/
class FrameDispatcher : public FrameWriterInterface {
......@@ -318,6 +295,7 @@ public:
_delegates.emplace(delegate.get(), delegate);
}
FrameWriterInterface* addDelegate(const std::function<bool(const Frame::Ptr &frame)> &cb);
/**
* 删除代理
*/
......
......@@ -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;
......
......@@ -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);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论