From b6d3ec5251e722c397f34858559ec17284b8eacf Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 11 Jun 2022 12:56:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=8E=B7=E5=8F=96=E4=B8=A2?= =?UTF-8?q?=E5=8C=85=E7=8E=87pull=20request;=20=E6=96=B0=E5=A2=9EMediaSour?= =?UTF-8?q?ce::getOwnerPoller=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/MediaSource.cpp | 19 ++++++++++++++++++- src/Common/MediaSource.h | 14 +++++++++++--- webrtc/WebRtcPusher.cpp | 6 +++++- webrtc/WebRtcPusher.h | 4 +++- webrtc/WebRtcTransport.cpp | 27 +++++++++++++-------------- 5 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index d2de4b9a..d34913a3 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. * * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit). @@ -221,6 +221,14 @@ int MediaSource::getLossRate(mediakit::TrackType type) { return listener->getLossRate(*this, type); } +toolkit::EventPoller::Ptr MediaSource::getOwnerPoller() { + auto listener = _listener.lock(); + if (!listener) { + return nullptr; + } + return listener->getOwnerPoller(*this); +} + void MediaSource::onReaderChanged(int size) { auto listener = _listener.lock(); if (listener) { @@ -706,6 +714,15 @@ int MediaSourceEventInterceptor::getLossRate(MediaSource &sender, TrackType type return -1; //异常返回-1 } +toolkit::EventPoller::Ptr MediaSourceEventInterceptor::getOwnerPoller(MediaSource &sender) { + auto listener = _listener.lock(); + if (listener) { + return listener->getOwnerPoller(sender); + } + return nullptr; +} + + bool MediaSourceEventInterceptor::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second) { auto listener = _listener.lock(); if (!listener) { diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index d711b4dc..4b0b1f34 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. * * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit). @@ -77,8 +77,10 @@ public: virtual void onReaderChanged(MediaSource &sender, int size); //流注册或注销事件 virtual void onRegist(MediaSource &sender, bool regist) {}; - - virtual int getLossRate(MediaSource &sender, TrackType type) {return -1;}; + // 获取丢包率 + virtual int getLossRate(MediaSource &sender, TrackType type) { return -1; } + // 获取所在线程 + virtual toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) { return nullptr; } ////////////////////////仅供MultiMediaSourceMuxer对象继承//////////////////////// // 开启或关闭录制 @@ -145,6 +147,8 @@ public: void startSendRtp(MediaSource &sender, const SendRtpArgs &args, const std::function cb) override; bool stopSendRtp(MediaSource &sender, const std::string &ssrc) override; int getLossRate(MediaSource &sender, TrackType type) override; + toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override; + private: std::weak_ptr _listener; }; @@ -297,6 +301,10 @@ public: void startSendRtp(const MediaSourceEvent::SendRtpArgs &args, const std::function cb); // 停止发送ps-rtp bool stopSendRtp(const std::string &ssrc); + // 获取丢包率 + int getLossRate(mediakit::TrackType type); + // 获取所在线程 + toolkit::EventPoller::Ptr getOwnerPoller(); ////////////////static方法,查找或生成MediaSource//////////////// diff --git a/webrtc/WebRtcPusher.cpp b/webrtc/WebRtcPusher.cpp index 9659e101..1f27f6d2 100644 --- a/webrtc/WebRtcPusher.cpp +++ b/webrtc/WebRtcPusher.cpp @@ -150,4 +150,8 @@ void WebRtcPusher::onRtcConfigure(RtcConfigure &configure) const { int WebRtcPusher::getLossRate(MediaSource &sender,mediakit::TrackType type){ return WebRtcTransportImp::getLossRate(type); -} \ No newline at end of file +} + +toolkit::EventPoller::Ptr WebRtcPusher::getOwnerPoller(mediakit::MediaSource &sender) { + return getPoller(); +} \ No newline at end of file diff --git a/webrtc/WebRtcPusher.h b/webrtc/WebRtcPusher.h index 4b92961b..4994f214 100644 --- a/webrtc/WebRtcPusher.h +++ b/webrtc/WebRtcPusher.h @@ -39,8 +39,10 @@ protected: std::string getOriginUrl(mediakit::MediaSource &sender) const override; // 获取媒体源客户端相关信息 std::shared_ptr getOriginSock(mediakit::MediaSource &sender) const override; - + // 获取丢包率 int getLossRate(mediakit::MediaSource &sender,mediakit::TrackType type) override; + // 获取MediaSource归属线程 + toolkit::EventPoller::Ptr getOwnerPoller(mediakit::MediaSource &sender) override; private: WebRtcPusher(const EventPoller::Ptr &poller, const mediakit::RtspMediaSourceImp::Ptr &src, diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index fa9840c9..fb02eaba 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -612,10 +612,10 @@ public: } int getLossRate() { - if (!_rtcp_context.getExpectedPacketsInterval()) //_rtcp_context.getExpectedPacketsInterval()取值总为零? - { - return 0; - } + if (!_rtcp_context.getExpectedPacketsInterval()) { + //_rtcp_context.getExpectedPacketsInterval()取值总为零? + return 0; + } return _rtcp_context.geLostInterval() * 100 / _rtcp_context.getExpectedPacketsInterval(); } @@ -659,19 +659,18 @@ std::shared_ptr MediaTrack::getRtpChannel(uint32_t ssrc) const{ return it_chn->second; } -int WebRtcTransportImp::getLossRate(mediakit::TrackType type){ - for(auto it : _ssrc_to_track){ - auto ssrc = it.first; - auto track = it.second; - auto rtp_chn = track->getRtpChannel(ssrc); - if(rtp_chn){ - InfoL << "-----------接收丢包率,ssrc------------- :" << ssrc << ",loss rate(%):" << rtp_chn->getLossRate() ; - if (track->media && type==track->media->type){ +int WebRtcTransportImp::getLossRate(mediakit::TrackType type) { + for (auto &pr : _ssrc_to_track) { + auto ssrc = pr.first; + auto &track = pr.second; + auto rtp_chn = track->getRtpChannel(ssrc); + if (rtp_chn) { + if (track->media && type == track->media->type) { return rtp_chn->getLossRate(); } - } + } } - return -1; + return -1; } void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {