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