From be995f9cd2f262040ee1f6ecbcbf79617125b291 Mon Sep 17 00:00:00 2001 From: Leon <8544842+leon14631@user.noreply.gitee.com> Date: Sat, 11 Jun 2022 04:31:06 +0000 Subject: [PATCH] =?UTF-8?q?!17=20=E3=80=90=E5=8A=9F=E8=83=BD=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E3=80=91=20/index/api/getMediaList=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0LOSS=E5=AD=97=E6=AE=B5=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E6=AF=8F=E4=B8=AA=E8=BD=A8=E9=81=93=E4=B8=A2?= =?UTF-8?q?=E5=8C=85=E7=8E=87=E5=8F=8D=E9=A6=88=20*=20update=20webrtc/WebR?= =?UTF-8?q?tcPusher.h.=20*=20update=20webrtc/WebRtcPusher.cpp.=20*=20updat?= =?UTF-8?q?e=20webrtc/WebRtcTransport.h.=20*=20update=20webrtc/WebRtcTrans?= =?UTF-8?q?port.cpp.=20*=20update=20src/Common/MediaSource.h.=20*=20update?= =?UTF-8?q?=20src/Common/MediaSource.cpp.=20*=20update=20server/WebApi.cpp?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 4 +++- src/Common/MediaSource.cpp | 18 +++++++++++++++++- src/Common/MediaSource.h | 6 ++++-- webrtc/WebRtcPusher.cpp | 8 ++++++-- webrtc/WebRtcPusher.h | 4 +++- webrtc/WebRtcTransport.cpp | 21 ++++++++++++++++++++- webrtc/WebRtcTransport.h | 3 ++- 7 files changed, 55 insertions(+), 9 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 9992cfa6..282c2fba 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.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). @@ -343,6 +343,7 @@ Value makeMediaSourceJson(MediaSource &media){ switch(codec_type){ case TrackAudio : { auto audio_track = dynamic_pointer_cast(track); + obj["loss"] = media.getLossRate(TrackAudio); obj["sample_rate"] = audio_track->getAudioSampleRate(); obj["channels"] = audio_track->getAudioChannel(); obj["sample_bit"] = audio_track->getAudioSampleBit(); @@ -350,6 +351,7 @@ Value makeMediaSourceJson(MediaSource &media){ } case TrackVideo : { auto video_track = dynamic_pointer_cast(track); + obj["loss"] = media.getLossRate(TrackVideo); obj["width"] = video_track->getVideoWidth(); obj["height"] = video_track->getVideoHeight(); obj["fps"] = round(video_track->getVideoFps()); diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 6b395c18..d2de4b9a 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). @@ -213,6 +213,14 @@ bool MediaSource::close(bool force) { return listener->close(*this,force); } +int MediaSource::getLossRate(mediakit::TrackType type) { + auto listener = _listener.lock(); + if (!listener) { + return -1; + } + return listener->getLossRate(*this, type); +} + void MediaSource::onReaderChanged(int size) { auto listener = _listener.lock(); if (listener) { @@ -690,6 +698,14 @@ void MediaSourceEventInterceptor::onRegist(MediaSource &sender, bool regist) { } } +int MediaSourceEventInterceptor::getLossRate(MediaSource &sender, TrackType type){ + auto listener = _listener.lock(); + if (listener) { + return listener->getLossRate(sender, type); + } + return -1; //异常返回-1 +} + 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 dd6d3572..d711b4dc 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). @@ -78,6 +78,8 @@ public: //流注册或注销事件 virtual void onRegist(MediaSource &sender, bool regist) {}; + virtual int getLossRate(MediaSource &sender, TrackType type) {return -1;}; + ////////////////////////仅供MultiMediaSourceMuxer对象继承//////////////////////// // 开启或关闭录制 virtual bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const std::string &custom_path, size_t max_second) { return false; }; @@ -142,7 +144,7 @@ public: std::vector getMediaTracks(MediaSource &sender, bool trackReady = true) const override; 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; private: std::weak_ptr _listener; }; diff --git a/webrtc/WebRtcPusher.cpp b/webrtc/WebRtcPusher.cpp index 111cda38..9659e101 100644 --- a/webrtc/WebRtcPusher.cpp +++ b/webrtc/WebRtcPusher.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). @@ -146,4 +146,8 @@ void WebRtcPusher::onRtcConfigure(RtcConfigure &configure) const { WebRtcTransportImp::onRtcConfigure(configure); //这只是推流 configure.audio.direction = configure.video.direction = RtpDirection::recvonly; -} \ No newline at end of file +} + +int WebRtcPusher::getLossRate(MediaSource &sender,mediakit::TrackType type){ + return WebRtcTransportImp::getLossRate(type); +} \ No newline at end of file diff --git a/webrtc/WebRtcPusher.h b/webrtc/WebRtcPusher.h index 865c5672..4b92961b 100644 --- a/webrtc/WebRtcPusher.h +++ b/webrtc/WebRtcPusher.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). @@ -40,6 +40,8 @@ protected: // 获取媒体源客户端相关信息 std::shared_ptr getOriginSock(mediakit::MediaSource &sender) const override; + int getLossRate(mediakit::MediaSource &sender,mediakit::TrackType type) override; + private: WebRtcPusher(const EventPoller::Ptr &poller, const mediakit::RtspMediaSourceImp::Ptr &src, const std::shared_ptr &ownership, const mediakit::MediaInfo &info, const mediakit::ProtocolOption &option); diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 42e97e92..fa9840c9 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.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). @@ -612,6 +612,10 @@ public: } int getLossRate() { + if (!_rtcp_context.getExpectedPacketsInterval()) //_rtcp_context.getExpectedPacketsInterval()取值总为零? + { + return 0; + } return _rtcp_context.geLostInterval() * 100 / _rtcp_context.getExpectedPacketsInterval(); } @@ -655,6 +659,21 @@ 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){ + return rtp_chn->getLossRate(); + } + } + } + return -1; +} + void WebRtcTransportImp::onRtcp(const char *buf, size_t len) { _bytes_usage += len; auto rtcps = RtcpHeader::loadFromBytes((char *) buf, len); diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index a5bd4147..7a1cc63e 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.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). @@ -262,6 +262,7 @@ protected: void onShutdown(const SockException &ex) override; virtual void onRecvRtp(MediaTrack &track, const std::string &rid, mediakit::RtpPacket::Ptr rtp) = 0; void updateTicker(); + int getLossRate(mediakit::TrackType type); private: void onSortedRtp(MediaTrack &track, const std::string &rid, mediakit::RtpPacket::Ptr rtp);