Commit c2a8d46a by xia-chu

提高c sdk mk_media对象线程安全

parent ba4296e1
......@@ -20,8 +20,10 @@ class MediaHelper : public MediaSourceEvent , public std::enable_shared_from_thi
public:
using Ptr = std::shared_ptr<MediaHelper>;
template<typename ...ArgsType>
MediaHelper(ArgsType &&...args){
_channel = std::make_shared<DevChannel>(std::forward<ArgsType>(args)...);
MediaHelper(ArgsType &&...args) {
_poller = EventPollerPool::Instance().getPoller();
// 在poller线程中创建DevChannel(MultiMediaSourceMuxer)对象,确保严格的线程安全限制
_poller->sync([&]() { _channel = std::make_shared<DevChannel>(std::forward<ArgsType>(args)...); });
}
~MediaHelper() = default;
......@@ -102,7 +104,12 @@ protected:
}
}
toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) {
return _poller;
}
private:
EventPoller::Ptr _poller;
DevChannel::Ptr _channel;
on_mk_media_close _on_close = nullptr;
on_mk_media_seek _on_seek = nullptr;
......
......@@ -179,6 +179,27 @@ bool DevChannel::initAudio(const AudioInfo &info) {
}
}
bool DevChannel::inputFrame(const Frame::Ptr &frame) {
auto cached_frame = Frame::getCacheAbleFrame(frame);
weak_ptr<MultiMediaSourceMuxer> weak_self = shared_from_this();
getOwnerPoller(MediaSource::NullMediaSource())->async([weak_self, cached_frame]() {
if (auto strong_self = weak_self.lock()) {
strong_self->MultiMediaSourceMuxer::inputFrame(cached_frame);
}
});
return true;
}
bool DevChannel::addTrack(const Track::Ptr &track) {
bool ret;
getOwnerPoller(MediaSource::NullMediaSource())->sync([&]() { ret = MultiMediaSourceMuxer::addTrack(track); });
return ret;
}
void DevChannel::addTrackCompleted() {
getOwnerPoller(MediaSource::NullMediaSource())->sync([&]() { MultiMediaSourceMuxer::addTrackCompleted(); });
}
MediaOriginType DevChannel::getOriginType(MediaSource &sender) const {
return MediaOriginType::device_chn;
}
......
......@@ -118,6 +118,11 @@ public:
*/
bool inputPCM(char *data, int len, uint64_t cts);
//// 重载基类方法,确保线程安全 ////
bool inputFrame(const Frame::Ptr &frame) override;
bool addTrack(const Track::Ptr & track) override;
void addTrackCompleted() override;
private:
MediaOriginType getOriginType(MediaSource &sender) const override;
......
......@@ -172,7 +172,7 @@ void MediaSink::emitAllTrackReady() {
continue;
}
pr.second.for_each([&](const Frame::Ptr &frame) {
inputFrame(frame);
MediaSink::inputFrame(frame);
});
}
_frame_unread.clear();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论