去除rtp_sender_mtx (#1722)

* 去除rtp_sender_mtx
* 无人观看定时器指定为MediaSource对象所属poller以确保读取观看数是线程安全的

Co-authored-by: xiongziliang <771730766@qq.com>
This commit is contained in:
custompal 2022-06-18 13:22:57 +08:00 committed by GitHub
parent fda4e83ae4
commit 771844d065
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 10 deletions

View File

@ -233,10 +233,17 @@ toolkit::EventPoller::Ptr MediaSource::getOwnerPoller() {
} }
void MediaSource::onReaderChanged(int size) { void MediaSource::onReaderChanged(int size) {
auto listener = _listener.lock(); weak_ptr<MediaSource> weak_self = shared_from_this();
if (listener) { getOwnerPoller()->async([weak_self, size]() {
listener->onReaderChanged(*this, size); auto strong_self = weak_self.lock();
if (!strong_self) {
return;
} }
auto listener = strong_self->_listener.lock();
if (listener) {
listener->onReaderChanged(*strong_self, size);
}
});
} }
bool MediaSource::setupRecord(Recorder::type type, bool start, const string &custom_path, size_t max_second){ bool MediaSource::setupRecord(Recorder::type type, bool start, const string &custom_path, size_t max_second){

View File

@ -230,7 +230,6 @@ void MultiMediaSourceMuxer::startSendRtp(MediaSource &, const MediaSourceEvent::
rtp_sender->addTrack(track); rtp_sender->addTrack(track);
} }
rtp_sender->addTrackCompleted(); rtp_sender->addTrackCompleted();
lock_guard<mutex> lck(strong_self->_rtp_sender_mtx);
strong_self->_rtp_sender[args.ssrc] = rtp_sender; strong_self->_rtp_sender[args.ssrc] = rtp_sender;
}); });
#else #else
@ -249,13 +248,11 @@ bool MultiMediaSourceMuxer::stopSendRtp(MediaSource &sender, const string &ssrc)
} }
if (ssrc.empty()) { if (ssrc.empty()) {
//关闭全部 //关闭全部
lock_guard<mutex> lck(_rtp_sender_mtx);
auto size = _rtp_sender.size(); auto size = _rtp_sender.size();
_rtp_sender.clear(); _rtp_sender.clear();
return size; return size;
} }
//关闭特定的 //关闭特定的
lock_guard<mutex> lck(_rtp_sender_mtx);
return _rtp_sender.erase(ssrc); return _rtp_sender.erase(ssrc);
#else #else
return false; return false;
@ -340,7 +337,6 @@ void MultiMediaSourceMuxer::resetTracks() {
#endif #endif
#if defined(ENABLE_RTPPROXY) #if defined(ENABLE_RTPPROXY)
lock_guard<mutex> lck(_rtp_sender_mtx);
for (auto &pr : _rtp_sender) { for (auto &pr : _rtp_sender) {
pr.second->resetTracks(); pr.second->resetTracks();
} }
@ -447,7 +443,6 @@ bool MultiMediaSourceMuxer::onTrackFrame(const Frame::Ptr &frame_in) {
#endif #endif
#if defined(ENABLE_RTPPROXY) #if defined(ENABLE_RTPPROXY)
lock_guard<mutex> lck(_rtp_sender_mtx);
for (auto &pr : _rtp_sender) { for (auto &pr : _rtp_sender) {
ret = pr.second->inputFrame(frame) ? true : ret; ret = pr.second->inputFrame(frame) ? true : ret;
} }

View File

@ -180,7 +180,6 @@ private:
std::weak_ptr<Listener> _track_listener; std::weak_ptr<Listener> _track_listener;
std::function<std::string()> _get_origin_url; std::function<std::string()> _get_origin_url;
#if defined(ENABLE_RTPPROXY) #if defined(ENABLE_RTPPROXY)
std::mutex _rtp_sender_mtx;
std::unordered_map<std::string, RtpSender::Ptr> _rtp_sender; std::unordered_map<std::string, RtpSender::Ptr> _rtp_sender;
#endif //ENABLE_RTPPROXY #endif //ENABLE_RTPPROXY

View File

@ -277,5 +277,9 @@ std::shared_ptr<SockInfo> RtpProcess::getOriginSock(MediaSource &sender) const {
return const_cast<RtpProcess *>(this)->shared_from_this(); return const_cast<RtpProcess *>(this)->shared_from_this();
} }
toolkit::EventPoller::Ptr RtpProcess::getOwnerPoller(MediaSource &sender) {
return _sock ? _sock->getPoller() : nullptr;
}
}//namespace mediakit }//namespace mediakit
#endif//defined(ENABLE_RTPPROXY) #endif//defined(ENABLE_RTPPROXY)

View File

@ -76,6 +76,7 @@ protected:
MediaOriginType getOriginType(MediaSource &sender) const override; MediaOriginType getOriginType(MediaSource &sender) const override;
std::string getOriginUrl(MediaSource &sender) const override; std::string getOriginUrl(MediaSource &sender) const override;
std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const override; std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const override;
toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override;
private: private:
void emitOnPublish(); void emitOnPublish();