提高c sdk mk_media对象线程安全

This commit is contained in:
xia-chu 2023-03-11 11:02:20 +08:00
parent ba4296e180
commit c2a8d46a64
4 changed files with 36 additions and 3 deletions

View File

@ -21,7 +21,9 @@ public:
using Ptr = std::shared_ptr<MediaHelper>;
template<typename ...ArgsType>
MediaHelper(ArgsType &&...args) {
_channel = std::make_shared<DevChannel>(std::forward<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;

View File

@ -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;
}

View File

@ -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;

View File

@ -172,7 +172,7 @@ void MediaSink::emitAllTrackReady() {
continue;
}
pr.second.for_each([&](const Frame::Ptr &frame) {
inputFrame(frame);
MediaSink::inputFrame(frame);
});
}
_frame_unread.clear();