hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/rtc_stats_collector.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 14 | #include <string> |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 15 | #include <utility> |
| 16 | #include <vector> |
| 17 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 18 | #include "absl/memory/memory.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 19 | #include "api/candidate.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "api/media_stream_interface.h" |
| 21 | #include "api/peer_connection_interface.h" |
| 22 | #include "media/base/media_channel.h" |
| 23 | #include "p2p/base/p2p_constants.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "p2p/base/port.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "pc/peer_connection.h" |
| 26 | #include "pc/rtc_stats_traversal.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/checks.h" |
Jonas Olsson | 43568dd | 2018-06-11 16:25:54 +0200 | [diff] [blame] | 28 | #include "rtc_base/strings/string_builder.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 29 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "rtc_base/trace_event.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
| 33 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 34 | namespace { |
| 35 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 36 | std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) { |
| 37 | return "RTCCertificate_" + fingerprint; |
| 38 | } |
| 39 | |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 40 | std::string RTCCodecStatsIDFromMidDirectionAndPayload(const std::string& mid, |
| 41 | bool inbound, |
| 42 | uint32_t payload_type) { |
Jonas Olsson | 43568dd | 2018-06-11 16:25:54 +0200 | [diff] [blame] | 43 | char buf[1024]; |
| 44 | rtc::SimpleStringBuilder sb(buf); |
| 45 | sb << "RTCCodec_" << mid << (inbound ? "_Inbound_" : "_Outbound_") |
| 46 | << payload_type; |
| 47 | return sb.str(); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 48 | } |
| 49 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 50 | std::string RTCIceCandidatePairStatsIDFromConnectionInfo( |
| 51 | const cricket::ConnectionInfo& info) { |
Jonas Olsson | 43568dd | 2018-06-11 16:25:54 +0200 | [diff] [blame] | 52 | char buf[4096]; |
| 53 | rtc::SimpleStringBuilder sb(buf); |
| 54 | sb << "RTCIceCandidatePair_" << info.local_candidate.id() << "_" |
| 55 | << info.remote_candidate.id(); |
| 56 | return sb.str(); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 59 | const char kSender[] = "sender"; |
| 60 | const char kReceiver[] = "receiver"; |
| 61 | |
| 62 | std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 63 | const char* direction, |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 64 | int attachment_id) { |
Jonas Olsson | 43568dd | 2018-06-11 16:25:54 +0200 | [diff] [blame] | 65 | char buf[1024]; |
| 66 | rtc::SimpleStringBuilder sb(buf); |
| 67 | sb << "RTCMediaStreamTrack_" << direction << "_" << attachment_id; |
| 68 | return sb.str(); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 69 | } |
| 70 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 71 | std::string RTCTransportStatsIDFromTransportChannel( |
| 72 | const std::string& transport_name, int channel_component) { |
Jonas Olsson | 43568dd | 2018-06-11 16:25:54 +0200 | [diff] [blame] | 73 | char buf[1024]; |
| 74 | rtc::SimpleStringBuilder sb(buf); |
| 75 | sb << "RTCTransport_" << transport_name << "_" << channel_component; |
| 76 | return sb.str(); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 77 | } |
| 78 | |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 79 | std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) { |
Jonas Olsson | 43568dd | 2018-06-11 16:25:54 +0200 | [diff] [blame] | 80 | char buf[1024]; |
| 81 | rtc::SimpleStringBuilder sb(buf); |
| 82 | sb << "RTCInboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc; |
| 83 | return sb.str(); |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 84 | } |
| 85 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 86 | std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) { |
Jonas Olsson | 43568dd | 2018-06-11 16:25:54 +0200 | [diff] [blame] | 87 | char buf[1024]; |
| 88 | rtc::SimpleStringBuilder sb(buf); |
| 89 | sb << "RTCOutboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc; |
| 90 | return sb.str(); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 91 | } |
| 92 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 93 | const char* CandidateTypeToRTCIceCandidateType(const std::string& type) { |
| 94 | if (type == cricket::LOCAL_PORT_TYPE) |
| 95 | return RTCIceCandidateType::kHost; |
| 96 | if (type == cricket::STUN_PORT_TYPE) |
| 97 | return RTCIceCandidateType::kSrflx; |
| 98 | if (type == cricket::PRFLX_PORT_TYPE) |
| 99 | return RTCIceCandidateType::kPrflx; |
| 100 | if (type == cricket::RELAY_PORT_TYPE) |
| 101 | return RTCIceCandidateType::kRelay; |
| 102 | RTC_NOTREACHED(); |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 106 | const char* DataStateToRTCDataChannelState( |
| 107 | DataChannelInterface::DataState state) { |
| 108 | switch (state) { |
| 109 | case DataChannelInterface::kConnecting: |
| 110 | return RTCDataChannelState::kConnecting; |
| 111 | case DataChannelInterface::kOpen: |
| 112 | return RTCDataChannelState::kOpen; |
| 113 | case DataChannelInterface::kClosing: |
| 114 | return RTCDataChannelState::kClosing; |
| 115 | case DataChannelInterface::kClosed: |
| 116 | return RTCDataChannelState::kClosed; |
| 117 | default: |
| 118 | RTC_NOTREACHED(); |
| 119 | return nullptr; |
| 120 | } |
| 121 | } |
| 122 | |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 123 | const char* IceCandidatePairStateToRTCStatsIceCandidatePairState( |
| 124 | cricket::IceCandidatePairState state) { |
| 125 | switch (state) { |
| 126 | case cricket::IceCandidatePairState::WAITING: |
| 127 | return RTCStatsIceCandidatePairState::kWaiting; |
| 128 | case cricket::IceCandidatePairState::IN_PROGRESS: |
| 129 | return RTCStatsIceCandidatePairState::kInProgress; |
| 130 | case cricket::IceCandidatePairState::SUCCEEDED: |
| 131 | return RTCStatsIceCandidatePairState::kSucceeded; |
| 132 | case cricket::IceCandidatePairState::FAILED: |
| 133 | return RTCStatsIceCandidatePairState::kFailed; |
| 134 | default: |
| 135 | RTC_NOTREACHED(); |
| 136 | return nullptr; |
| 137 | } |
| 138 | } |
| 139 | |
hbos | 7064d59 | 2017-01-16 07:38:02 -0800 | [diff] [blame] | 140 | const char* DtlsTransportStateToRTCDtlsTransportState( |
| 141 | cricket::DtlsTransportState state) { |
| 142 | switch (state) { |
| 143 | case cricket::DTLS_TRANSPORT_NEW: |
| 144 | return RTCDtlsTransportState::kNew; |
| 145 | case cricket::DTLS_TRANSPORT_CONNECTING: |
| 146 | return RTCDtlsTransportState::kConnecting; |
| 147 | case cricket::DTLS_TRANSPORT_CONNECTED: |
| 148 | return RTCDtlsTransportState::kConnected; |
| 149 | case cricket::DTLS_TRANSPORT_CLOSED: |
| 150 | return RTCDtlsTransportState::kClosed; |
| 151 | case cricket::DTLS_TRANSPORT_FAILED: |
| 152 | return RTCDtlsTransportState::kFailed; |
| 153 | default: |
| 154 | RTC_NOTREACHED(); |
| 155 | return nullptr; |
| 156 | } |
| 157 | } |
| 158 | |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 159 | const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) { |
| 160 | switch (type) { |
| 161 | case rtc::ADAPTER_TYPE_CELLULAR: |
| 162 | return RTCNetworkType::kCellular; |
| 163 | case rtc::ADAPTER_TYPE_ETHERNET: |
| 164 | return RTCNetworkType::kEthernet; |
| 165 | case rtc::ADAPTER_TYPE_WIFI: |
| 166 | return RTCNetworkType::kWifi; |
| 167 | case rtc::ADAPTER_TYPE_VPN: |
| 168 | return RTCNetworkType::kVpn; |
| 169 | case rtc::ADAPTER_TYPE_UNKNOWN: |
| 170 | case rtc::ADAPTER_TYPE_LOOPBACK: |
Qingsi Wang | 9f1de69 | 2018-06-28 15:38:09 -0700 | [diff] [blame] | 171 | case rtc::ADAPTER_TYPE_ANY: |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 172 | return RTCNetworkType::kUnknown; |
| 173 | } |
| 174 | RTC_NOTREACHED(); |
| 175 | return nullptr; |
| 176 | } |
| 177 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 178 | double DoubleAudioLevelFromIntAudioLevel(int audio_level) { |
| 179 | RTC_DCHECK_GE(audio_level, 0); |
| 180 | RTC_DCHECK_LE(audio_level, 32767); |
| 181 | return audio_level / 32767.0; |
| 182 | } |
| 183 | |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 184 | std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters( |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 185 | uint64_t timestamp_us, |
| 186 | const std::string& mid, |
| 187 | bool inbound, |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 188 | const RtpCodecParameters& codec_params) { |
| 189 | RTC_DCHECK_GE(codec_params.payload_type, 0); |
| 190 | RTC_DCHECK_LE(codec_params.payload_type, 127); |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 191 | RTC_DCHECK(codec_params.clock_rate); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 192 | uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type); |
| 193 | std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats( |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 194 | RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type), |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 195 | timestamp_us)); |
| 196 | codec_stats->payload_type = payload_type; |
hbos | 13f54b2 | 2017-02-28 06:56:04 -0800 | [diff] [blame] | 197 | codec_stats->mime_type = codec_params.mime_type(); |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 198 | if (codec_params.clock_rate) { |
| 199 | codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate); |
| 200 | } |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 201 | return codec_stats; |
| 202 | } |
| 203 | |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 204 | void SetMediaStreamTrackStatsFromMediaStreamTrackInterface( |
| 205 | const MediaStreamTrackInterface& track, |
| 206 | RTCMediaStreamTrackStats* track_stats) { |
| 207 | track_stats->track_identifier = track.id(); |
| 208 | track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded); |
| 209 | } |
| 210 | |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 211 | // Provides the media independent counters (both audio and video). |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 212 | void SetInboundRTPStreamStatsFromMediaReceiverInfo( |
| 213 | const cricket::MediaReceiverInfo& media_receiver_info, |
| 214 | RTCInboundRTPStreamStats* inbound_stats) { |
| 215 | RTC_DCHECK(inbound_stats); |
hbos | 3443bb7 | 2017-02-07 06:28:11 -0800 | [diff] [blame] | 216 | inbound_stats->ssrc = media_receiver_info.ssrc(); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 217 | // TODO(hbos): Support the remote case. https://crbug.com/657855 |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 218 | inbound_stats->is_remote = false; |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 219 | inbound_stats->packets_received = |
| 220 | static_cast<uint32_t>(media_receiver_info.packets_rcvd); |
| 221 | inbound_stats->bytes_received = |
| 222 | static_cast<uint64_t>(media_receiver_info.bytes_rcvd); |
hbos | 02cd4d6 | 2016-12-09 04:19:44 -0800 | [diff] [blame] | 223 | inbound_stats->packets_lost = |
Harald Alvestrand | 719487e | 2017-12-13 12:26:04 +0100 | [diff] [blame] | 224 | static_cast<int32_t>(media_receiver_info.packets_lost); |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 225 | inbound_stats->fraction_lost = |
| 226 | static_cast<double>(media_receiver_info.fraction_lost); |
| 227 | } |
| 228 | |
| 229 | void SetInboundRTPStreamStatsFromVoiceReceiverInfo( |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 230 | const std::string& mid, |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 231 | const cricket::VoiceReceiverInfo& voice_receiver_info, |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 232 | RTCInboundRTPStreamStats* inbound_audio) { |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 233 | SetInboundRTPStreamStatsFromMediaReceiverInfo( |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 234 | voice_receiver_info, inbound_audio); |
| 235 | inbound_audio->media_type = "audio"; |
Philipp Hancke | 3bc0166 | 2018-08-28 14:55:03 +0200 | [diff] [blame] | 236 | inbound_audio->kind = "audio"; |
hbos | 585a9b1 | 2017-02-07 04:59:16 -0800 | [diff] [blame] | 237 | if (voice_receiver_info.codec_payload_type) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 238 | inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload( |
| 239 | mid, true, *voice_receiver_info.codec_payload_type); |
hbos | 585a9b1 | 2017-02-07 04:59:16 -0800 | [diff] [blame] | 240 | } |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 241 | inbound_audio->jitter = |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 242 | static_cast<double>(voice_receiver_info.jitter_ms) / |
| 243 | rtc::kNumMillisecsPerSec; |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 244 | // |fir_count|, |pli_count| and |sli_count| are only valid for video and are |
| 245 | // purposefully left undefined for audio. |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void SetInboundRTPStreamStatsFromVideoReceiverInfo( |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 249 | const std::string& mid, |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 250 | const cricket::VideoReceiverInfo& video_receiver_info, |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 251 | RTCInboundRTPStreamStats* inbound_video) { |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 252 | SetInboundRTPStreamStatsFromMediaReceiverInfo( |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 253 | video_receiver_info, inbound_video); |
| 254 | inbound_video->media_type = "video"; |
Philipp Hancke | 3bc0166 | 2018-08-28 14:55:03 +0200 | [diff] [blame] | 255 | inbound_video->kind = "video"; |
hbos | 585a9b1 | 2017-02-07 04:59:16 -0800 | [diff] [blame] | 256 | if (video_receiver_info.codec_payload_type) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 257 | inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload( |
| 258 | mid, true, *video_receiver_info.codec_payload_type); |
hbos | 585a9b1 | 2017-02-07 04:59:16 -0800 | [diff] [blame] | 259 | } |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 260 | inbound_video->fir_count = |
| 261 | static_cast<uint32_t>(video_receiver_info.firs_sent); |
| 262 | inbound_video->pli_count = |
| 263 | static_cast<uint32_t>(video_receiver_info.plis_sent); |
| 264 | inbound_video->nack_count = |
| 265 | static_cast<uint32_t>(video_receiver_info.nacks_sent); |
hbos | 6769c49 | 2017-01-02 08:35:13 -0800 | [diff] [blame] | 266 | inbound_video->frames_decoded = video_receiver_info.frames_decoded; |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 267 | if (video_receiver_info.qp_sum) |
| 268 | inbound_video->qp_sum = *video_receiver_info.qp_sum; |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 269 | } |
| 270 | |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 271 | // Provides the media independent counters (both audio and video). |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 272 | void SetOutboundRTPStreamStatsFromMediaSenderInfo( |
| 273 | const cricket::MediaSenderInfo& media_sender_info, |
| 274 | RTCOutboundRTPStreamStats* outbound_stats) { |
| 275 | RTC_DCHECK(outbound_stats); |
hbos | 3443bb7 | 2017-02-07 06:28:11 -0800 | [diff] [blame] | 276 | outbound_stats->ssrc = media_sender_info.ssrc(); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 277 | // TODO(hbos): Support the remote case. https://crbug.com/657856 |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 278 | outbound_stats->is_remote = false; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 279 | outbound_stats->packets_sent = |
| 280 | static_cast<uint32_t>(media_sender_info.packets_sent); |
| 281 | outbound_stats->bytes_sent = |
| 282 | static_cast<uint64_t>(media_sender_info.bytes_sent); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void SetOutboundRTPStreamStatsFromVoiceSenderInfo( |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 286 | const std::string& mid, |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 287 | const cricket::VoiceSenderInfo& voice_sender_info, |
| 288 | RTCOutboundRTPStreamStats* outbound_audio) { |
| 289 | SetOutboundRTPStreamStatsFromMediaSenderInfo( |
| 290 | voice_sender_info, outbound_audio); |
| 291 | outbound_audio->media_type = "audio"; |
Philipp Hancke | 3bc0166 | 2018-08-28 14:55:03 +0200 | [diff] [blame] | 292 | outbound_audio->kind = "audio"; |
hbos | 585a9b1 | 2017-02-07 04:59:16 -0800 | [diff] [blame] | 293 | if (voice_sender_info.codec_payload_type) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 294 | outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload( |
| 295 | mid, false, *voice_sender_info.codec_payload_type); |
hbos | 585a9b1 | 2017-02-07 04:59:16 -0800 | [diff] [blame] | 296 | } |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 297 | // |fir_count|, |pli_count| and |sli_count| are only valid for video and are |
| 298 | // purposefully left undefined for audio. |
| 299 | } |
| 300 | |
| 301 | void SetOutboundRTPStreamStatsFromVideoSenderInfo( |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 302 | const std::string& mid, |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 303 | const cricket::VideoSenderInfo& video_sender_info, |
| 304 | RTCOutboundRTPStreamStats* outbound_video) { |
| 305 | SetOutboundRTPStreamStatsFromMediaSenderInfo( |
| 306 | video_sender_info, outbound_video); |
| 307 | outbound_video->media_type = "video"; |
Philipp Hancke | 3bc0166 | 2018-08-28 14:55:03 +0200 | [diff] [blame] | 308 | outbound_video->kind = "video"; |
hbos | 585a9b1 | 2017-02-07 04:59:16 -0800 | [diff] [blame] | 309 | if (video_sender_info.codec_payload_type) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 310 | outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload( |
| 311 | mid, false, *video_sender_info.codec_payload_type); |
hbos | 585a9b1 | 2017-02-07 04:59:16 -0800 | [diff] [blame] | 312 | } |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 313 | outbound_video->fir_count = |
| 314 | static_cast<uint32_t>(video_sender_info.firs_rcvd); |
| 315 | outbound_video->pli_count = |
| 316 | static_cast<uint32_t>(video_sender_info.plis_rcvd); |
| 317 | outbound_video->nack_count = |
| 318 | static_cast<uint32_t>(video_sender_info.nacks_rcvd); |
hbos | 6769c49 | 2017-01-02 08:35:13 -0800 | [diff] [blame] | 319 | if (video_sender_info.qp_sum) |
| 320 | outbound_video->qp_sum = *video_sender_info.qp_sum; |
| 321 | outbound_video->frames_encoded = video_sender_info.frames_encoded; |
Henrik Boström | f71362f | 2019-04-08 16:14:23 +0200 | [diff] [blame^] | 322 | outbound_video->total_encode_time = |
| 323 | static_cast<double>(video_sender_info.total_encode_time_ms) / |
| 324 | rtc::kNumMillisecsPerSec; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 325 | } |
| 326 | |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 327 | void ProduceCertificateStatsFromSSLCertificateStats( |
| 328 | int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats, |
| 329 | RTCStatsReport* report) { |
| 330 | RTCCertificateStats* prev_certificate_stats = nullptr; |
| 331 | for (const rtc::SSLCertificateStats* s = &certificate_stats; s; |
| 332 | s = s->issuer.get()) { |
hbos | 02d2a92 | 2016-12-21 01:29:05 -0800 | [diff] [blame] | 333 | std::string certificate_stats_id = |
| 334 | RTCCertificateIDFromFingerprint(s->fingerprint); |
| 335 | // It is possible for the same certificate to show up multiple times, e.g. |
| 336 | // if local and remote side use the same certificate in a loopback call. |
| 337 | // If the report already contains stats for this certificate, skip it. |
| 338 | if (report->Get(certificate_stats_id)) { |
| 339 | RTC_DCHECK_EQ(s, &certificate_stats); |
| 340 | break; |
| 341 | } |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 342 | RTCCertificateStats* certificate_stats = new RTCCertificateStats( |
hbos | 02d2a92 | 2016-12-21 01:29:05 -0800 | [diff] [blame] | 343 | certificate_stats_id, timestamp_us); |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 344 | certificate_stats->fingerprint = s->fingerprint; |
| 345 | certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm; |
| 346 | certificate_stats->base64_certificate = s->base64_certificate; |
| 347 | if (prev_certificate_stats) |
| 348 | prev_certificate_stats->issuer_certificate_id = certificate_stats->id(); |
| 349 | report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats)); |
| 350 | prev_certificate_stats = certificate_stats; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | const std::string& ProduceIceCandidateStats( |
| 355 | int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local, |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 356 | const std::string& transport_id, RTCStatsReport* report) { |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 357 | const std::string& id = "RTCIceCandidate_" + candidate.id(); |
| 358 | const RTCStats* stats = report->Get(id); |
| 359 | if (!stats) { |
| 360 | std::unique_ptr<RTCIceCandidateStats> candidate_stats; |
| 361 | if (is_local) |
| 362 | candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us)); |
| 363 | else |
| 364 | candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us)); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 365 | candidate_stats->transport_id = transport_id; |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 366 | if (is_local) { |
| 367 | candidate_stats->network_type = |
| 368 | NetworkAdapterTypeToStatsType(candidate.network_type()); |
Philipp Hancke | 9551375 | 2018-09-27 14:40:08 +0200 | [diff] [blame] | 369 | if (candidate.type() == cricket::RELAY_PORT_TYPE) { |
| 370 | std::string relay_protocol = candidate.relay_protocol(); |
| 371 | RTC_DCHECK(relay_protocol.compare("udp") == 0 || |
| 372 | relay_protocol.compare("tcp") == 0 || |
| 373 | relay_protocol.compare("tls") == 0); |
| 374 | candidate_stats->relay_protocol = relay_protocol; |
| 375 | } |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 376 | } else { |
| 377 | // We don't expect to know the adapter type of remote candidates. |
| 378 | RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type()); |
| 379 | } |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 380 | candidate_stats->ip = candidate.address().ipaddr().ToString(); |
| 381 | candidate_stats->port = static_cast<int32_t>(candidate.address().port()); |
| 382 | candidate_stats->protocol = candidate.protocol(); |
| 383 | candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType( |
| 384 | candidate.type()); |
| 385 | candidate_stats->priority = static_cast<int32_t>(candidate.priority()); |
| 386 | |
| 387 | stats = candidate_stats.get(); |
| 388 | report->AddStats(std::move(candidate_stats)); |
| 389 | } |
| 390 | RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType |
| 391 | : RTCRemoteIceCandidateStats::kType); |
| 392 | return stats->id(); |
| 393 | } |
| 394 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 395 | std::unique_ptr<RTCMediaStreamTrackStats> |
| 396 | ProduceMediaStreamTrackStatsFromVoiceSenderInfo( |
| 397 | int64_t timestamp_us, |
| 398 | const AudioTrackInterface& audio_track, |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 399 | const cricket::VoiceSenderInfo& voice_sender_info, |
| 400 | int attachment_id) { |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 401 | std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats( |
| 402 | new RTCMediaStreamTrackStats( |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 403 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender, |
| 404 | attachment_id), |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 405 | timestamp_us, RTCMediaStreamTrackKind::kAudio)); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 406 | SetMediaStreamTrackStatsFromMediaStreamTrackInterface( |
| 407 | audio_track, audio_track_stats.get()); |
| 408 | audio_track_stats->remote_source = false; |
| 409 | audio_track_stats->detached = false; |
| 410 | if (voice_sender_info.audio_level >= 0) { |
| 411 | audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel( |
| 412 | voice_sender_info.audio_level); |
| 413 | } |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 414 | audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy; |
| 415 | audio_track_stats->total_samples_duration = |
| 416 | voice_sender_info.total_input_duration; |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 417 | if (voice_sender_info.apm_statistics.echo_return_loss) { |
| 418 | audio_track_stats->echo_return_loss = |
| 419 | *voice_sender_info.apm_statistics.echo_return_loss; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 420 | } |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 421 | if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) { |
| 422 | audio_track_stats->echo_return_loss_enhancement = |
| 423 | *voice_sender_info.apm_statistics.echo_return_loss_enhancement; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 424 | } |
| 425 | return audio_track_stats; |
| 426 | } |
| 427 | |
| 428 | std::unique_ptr<RTCMediaStreamTrackStats> |
| 429 | ProduceMediaStreamTrackStatsFromVoiceReceiverInfo( |
| 430 | int64_t timestamp_us, |
| 431 | const AudioTrackInterface& audio_track, |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 432 | const cricket::VoiceReceiverInfo& voice_receiver_info, |
| 433 | int attachment_id) { |
| 434 | // Since receiver tracks can't be reattached, we use the SSRC as |
| 435 | // an attachment identifier. |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 436 | std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats( |
| 437 | new RTCMediaStreamTrackStats( |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 438 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver, |
| 439 | attachment_id), |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 440 | timestamp_us, RTCMediaStreamTrackKind::kAudio)); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 441 | SetMediaStreamTrackStatsFromMediaStreamTrackInterface( |
| 442 | audio_track, audio_track_stats.get()); |
| 443 | audio_track_stats->remote_source = true; |
| 444 | audio_track_stats->detached = false; |
| 445 | if (voice_receiver_info.audio_level >= 0) { |
| 446 | audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel( |
| 447 | voice_receiver_info.audio_level); |
| 448 | } |
Gustaf Ullberg | b0a0207 | 2017-10-02 12:00:34 +0200 | [diff] [blame] | 449 | audio_track_stats->jitter_buffer_delay = |
| 450 | voice_receiver_info.jitter_buffer_delay_seconds; |
Chen Xing | 0acffb5 | 2019-01-15 15:46:29 +0100 | [diff] [blame] | 451 | audio_track_stats->jitter_buffer_emitted_count = |
| 452 | voice_receiver_info.jitter_buffer_emitted_count; |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 453 | audio_track_stats->total_audio_energy = |
| 454 | voice_receiver_info.total_output_energy; |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 455 | audio_track_stats->total_samples_received = |
| 456 | voice_receiver_info.total_samples_received; |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 457 | audio_track_stats->total_samples_duration = |
| 458 | voice_receiver_info.total_output_duration; |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 459 | audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples; |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 460 | audio_track_stats->concealment_events = |
| 461 | voice_receiver_info.concealment_events; |
Ruslan Burakov | 8af8896 | 2018-11-22 17:21:10 +0100 | [diff] [blame] | 462 | audio_track_stats->jitter_buffer_flushes = |
| 463 | voice_receiver_info.jitter_buffer_flushes; |
Jakob Ivarsson | 352ce5c | 2018-11-27 12:52:16 +0100 | [diff] [blame] | 464 | audio_track_stats->delayed_packet_outage_samples = |
| 465 | voice_receiver_info.delayed_packet_outage_samples; |
Jakob Ivarsson | 232b3fd | 2019-03-06 09:18:40 +0100 | [diff] [blame] | 466 | audio_track_stats->relative_packet_arrival_delay = |
| 467 | voice_receiver_info.relative_packet_arrival_delay_seconds; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 468 | return audio_track_stats; |
| 469 | } |
| 470 | |
| 471 | std::unique_ptr<RTCMediaStreamTrackStats> |
| 472 | ProduceMediaStreamTrackStatsFromVideoSenderInfo( |
| 473 | int64_t timestamp_us, |
| 474 | const VideoTrackInterface& video_track, |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 475 | const cricket::VideoSenderInfo& video_sender_info, |
| 476 | int attachment_id) { |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 477 | std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats( |
| 478 | new RTCMediaStreamTrackStats( |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 479 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender, |
| 480 | |
| 481 | attachment_id), |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 482 | timestamp_us, RTCMediaStreamTrackKind::kVideo)); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 483 | SetMediaStreamTrackStatsFromMediaStreamTrackInterface( |
| 484 | video_track, video_track_stats.get()); |
| 485 | video_track_stats->remote_source = false; |
| 486 | video_track_stats->detached = false; |
| 487 | video_track_stats->frame_width = static_cast<uint32_t>( |
| 488 | video_sender_info.send_frame_width); |
| 489 | video_track_stats->frame_height = static_cast<uint32_t>( |
| 490 | video_sender_info.send_frame_height); |
hbos | fefe076 | 2017-01-20 06:14:25 -0800 | [diff] [blame] | 491 | // TODO(hbos): Will reduce this by frames dropped due to congestion control |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 492 | // when available. https://crbug.com/659137 |
hbos | fefe076 | 2017-01-20 06:14:25 -0800 | [diff] [blame] | 493 | video_track_stats->frames_sent = video_sender_info.frames_encoded; |
Ilya Nikolaevskiy | 70473fc | 2018-02-28 16:35:03 +0100 | [diff] [blame] | 494 | video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 495 | return video_track_stats; |
| 496 | } |
| 497 | |
| 498 | std::unique_ptr<RTCMediaStreamTrackStats> |
| 499 | ProduceMediaStreamTrackStatsFromVideoReceiverInfo( |
| 500 | int64_t timestamp_us, |
| 501 | const VideoTrackInterface& video_track, |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 502 | const cricket::VideoReceiverInfo& video_receiver_info, |
| 503 | int attachment_id) { |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 504 | std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats( |
| 505 | new RTCMediaStreamTrackStats( |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 506 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver, |
| 507 | |
| 508 | attachment_id), |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 509 | timestamp_us, RTCMediaStreamTrackKind::kVideo)); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 510 | SetMediaStreamTrackStatsFromMediaStreamTrackInterface( |
| 511 | video_track, video_track_stats.get()); |
| 512 | video_track_stats->remote_source = true; |
| 513 | video_track_stats->detached = false; |
| 514 | if (video_receiver_info.frame_width > 0 && |
| 515 | video_receiver_info.frame_height > 0) { |
| 516 | video_track_stats->frame_width = static_cast<uint32_t>( |
| 517 | video_receiver_info.frame_width); |
| 518 | video_track_stats->frame_height = static_cast<uint32_t>( |
| 519 | video_receiver_info.frame_height); |
| 520 | } |
hbos | 42f6d2f | 2017-01-20 03:56:50 -0800 | [diff] [blame] | 521 | video_track_stats->frames_received = video_receiver_info.frames_received; |
hbos | f64941f | 2017-01-20 07:39:09 -0800 | [diff] [blame] | 522 | // TODO(hbos): When we support receiving simulcast, this should be the total |
| 523 | // number of frames correctly decoded, independent of which SSRC it was |
| 524 | // received from. Since we don't support that, this is correct and is the same |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 525 | // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137 |
hbos | f64941f | 2017-01-20 07:39:09 -0800 | [diff] [blame] | 526 | video_track_stats->frames_decoded = video_receiver_info.frames_decoded; |
hbos | 50cfe1f | 2017-01-23 07:21:55 -0800 | [diff] [blame] | 527 | RTC_DCHECK_GE(video_receiver_info.frames_received, |
| 528 | video_receiver_info.frames_rendered); |
| 529 | video_track_stats->frames_dropped = video_receiver_info.frames_received - |
| 530 | video_receiver_info.frames_rendered; |
Sergey Silkin | 0237106 | 2019-01-31 16:45:42 +0100 | [diff] [blame] | 531 | video_track_stats->freeze_count = video_receiver_info.freeze_count; |
| 532 | video_track_stats->pause_count = video_receiver_info.pause_count; |
| 533 | video_track_stats->total_freezes_duration = |
| 534 | static_cast<double>(video_receiver_info.total_freezes_duration_ms) / |
| 535 | rtc::kNumMillisecsPerSec; |
| 536 | video_track_stats->total_pauses_duration = |
| 537 | static_cast<double>(video_receiver_info.total_pauses_duration_ms) / |
| 538 | rtc::kNumMillisecsPerSec; |
| 539 | video_track_stats->total_frames_duration = |
| 540 | static_cast<double>(video_receiver_info.total_frames_duration_ms) / |
| 541 | rtc::kNumMillisecsPerSec; |
| 542 | video_track_stats->sum_squared_frame_durations = |
| 543 | video_receiver_info.sum_squared_frame_durations; |
| 544 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 545 | return video_track_stats; |
| 546 | } |
| 547 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 548 | void ProduceSenderMediaTrackStats( |
| 549 | int64_t timestamp_us, |
| 550 | const TrackMediaInfoMap& track_media_info_map, |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 551 | std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders, |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 552 | RTCStatsReport* report) { |
| 553 | // This function iterates over the senders to generate outgoing track stats. |
| 554 | |
| 555 | // TODO(hbos): Return stats of detached tracks. We have to perform stats |
| 556 | // gathering at the time of detachment to get accurate stats and timestamps. |
| 557 | // https://crbug.com/659137 |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 558 | for (const auto& sender : senders) { |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 559 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 560 | AudioTrackInterface* track = |
| 561 | static_cast<AudioTrackInterface*>(sender->track().get()); |
| 562 | if (!track) |
| 563 | continue; |
Harald Alvestrand | b8e1201 | 2018-01-23 15:28:16 +0100 | [diff] [blame] | 564 | cricket::VoiceSenderInfo null_sender_info; |
| 565 | const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info; |
| 566 | // TODO(hta): Checking on ssrc is not proper. There should be a way |
| 567 | // to see from a sender whether it's connected or not. |
| 568 | // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none") |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 569 | if (sender->ssrc() != 0) { |
Harald Alvestrand | 76d2952 | 2018-01-30 14:43:29 +0100 | [diff] [blame] | 570 | // When pc.close is called, sender info is discarded, so |
| 571 | // we generate zeroes instead. Bug: It should be retained. |
| 572 | // https://crbug.com/807174 |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 573 | const cricket::VoiceSenderInfo* sender_info = |
Harald Alvestrand | b8e1201 | 2018-01-23 15:28:16 +0100 | [diff] [blame] | 574 | track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc()); |
Harald Alvestrand | 76d2952 | 2018-01-30 14:43:29 +0100 | [diff] [blame] | 575 | if (sender_info) { |
| 576 | voice_sender_info = sender_info; |
| 577 | } else { |
| 578 | RTC_LOG(LS_INFO) |
| 579 | << "RTCStatsCollector: No voice sender info for sender with ssrc " |
| 580 | << sender->ssrc(); |
| 581 | } |
Harald Alvestrand | b8e1201 | 2018-01-23 15:28:16 +0100 | [diff] [blame] | 582 | } |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 583 | std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats = |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 584 | ProduceMediaStreamTrackStatsFromVoiceSenderInfo( |
| 585 | timestamp_us, *track, *voice_sender_info, sender->AttachmentId()); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 586 | report->AddStats(std::move(audio_track_stats)); |
| 587 | } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
| 588 | VideoTrackInterface* track = |
| 589 | static_cast<VideoTrackInterface*>(sender->track().get()); |
| 590 | if (!track) |
| 591 | continue; |
Harald Alvestrand | b8e1201 | 2018-01-23 15:28:16 +0100 | [diff] [blame] | 592 | cricket::VideoSenderInfo null_sender_info; |
| 593 | const cricket::VideoSenderInfo* video_sender_info = &null_sender_info; |
| 594 | // TODO(hta): Check on state not ssrc when state is available |
Harald Alvestrand | 76d2952 | 2018-01-30 14:43:29 +0100 | [diff] [blame] | 595 | // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate |
| 596 | // "none") |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 597 | if (sender->ssrc() != 0) { |
Harald Alvestrand | 76d2952 | 2018-01-30 14:43:29 +0100 | [diff] [blame] | 598 | // When pc.close is called, sender info is discarded, so |
| 599 | // we generate zeroes instead. Bug: It should be retained. |
| 600 | // https://crbug.com/807174 |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 601 | const cricket::VideoSenderInfo* sender_info = |
Harald Alvestrand | b8e1201 | 2018-01-23 15:28:16 +0100 | [diff] [blame] | 602 | track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc()); |
Harald Alvestrand | 76d2952 | 2018-01-30 14:43:29 +0100 | [diff] [blame] | 603 | if (sender_info) { |
| 604 | video_sender_info = sender_info; |
| 605 | } else { |
| 606 | RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc " |
| 607 | << sender->ssrc(); |
| 608 | } |
| 609 | } |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 610 | std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats = |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 611 | ProduceMediaStreamTrackStatsFromVideoSenderInfo( |
| 612 | timestamp_us, *track, *video_sender_info, sender->AttachmentId()); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 613 | report->AddStats(std::move(video_track_stats)); |
| 614 | } else { |
| 615 | RTC_NOTREACHED(); |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | void ProduceReceiverMediaTrackStats( |
| 621 | int64_t timestamp_us, |
| 622 | const TrackMediaInfoMap& track_media_info_map, |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 623 | std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers, |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 624 | RTCStatsReport* report) { |
| 625 | // This function iterates over the receivers to find the remote tracks. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 626 | for (const auto& receiver : receivers) { |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 627 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 628 | AudioTrackInterface* track = |
| 629 | static_cast<AudioTrackInterface*>(receiver->track().get()); |
| 630 | const cricket::VoiceReceiverInfo* voice_receiver_info = |
| 631 | track_media_info_map.GetVoiceReceiverInfo(*track); |
| 632 | if (!voice_receiver_info) { |
| 633 | continue; |
| 634 | } |
| 635 | std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats = |
| 636 | ProduceMediaStreamTrackStatsFromVoiceReceiverInfo( |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 637 | timestamp_us, *track, *voice_receiver_info, |
| 638 | receiver->AttachmentId()); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 639 | report->AddStats(std::move(audio_track_stats)); |
| 640 | } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
| 641 | VideoTrackInterface* track = |
| 642 | static_cast<VideoTrackInterface*>(receiver->track().get()); |
| 643 | const cricket::VideoReceiverInfo* video_receiver_info = |
| 644 | track_media_info_map.GetVideoReceiverInfo(*track); |
| 645 | if (!video_receiver_info) { |
| 646 | continue; |
| 647 | } |
| 648 | std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats = |
| 649 | ProduceMediaStreamTrackStatsFromVideoReceiverInfo( |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 650 | timestamp_us, *track, *video_receiver_info, |
| 651 | receiver->AttachmentId()); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 652 | report->AddStats(std::move(video_track_stats)); |
| 653 | } else { |
| 654 | RTC_NOTREACHED(); |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
Henrik Boström | 5b3541f | 2018-03-19 13:52:56 +0100 | [diff] [blame] | 659 | rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector( |
| 660 | bool filter_by_sender_selector, |
| 661 | rtc::scoped_refptr<const RTCStatsReport> report, |
| 662 | rtc::scoped_refptr<RtpSenderInternal> sender_selector, |
| 663 | rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) { |
| 664 | std::vector<std::string> rtpstream_ids; |
| 665 | if (filter_by_sender_selector) { |
| 666 | // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector |
| 667 | if (sender_selector) { |
| 668 | // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that |
| 669 | // reference the sender stats. |
| 670 | // Because we do not implement sender stats, we look at outbound-rtp(s) |
| 671 | // that reference the track attachment stats for the sender instead. |
| 672 | std::string track_id = |
| 673 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 674 | kSender, sender_selector->AttachmentId()); |
| 675 | for (const auto& stats : *report) { |
| 676 | if (stats.type() != RTCOutboundRTPStreamStats::kType) |
| 677 | continue; |
| 678 | const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>(); |
| 679 | if (outbound_rtp.track_id.is_defined() && |
| 680 | *outbound_rtp.track_id == track_id) { |
| 681 | rtpstream_ids.push_back(outbound_rtp.id()); |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | } else { |
| 686 | // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector |
| 687 | if (receiver_selector) { |
| 688 | // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that |
| 689 | // reference the receiver stats. |
| 690 | // Because we do not implement receiver stats, we look at inbound-rtp(s) |
| 691 | // that reference the track attachment stats for the receiver instead. |
| 692 | std::string track_id = |
| 693 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 694 | kReceiver, receiver_selector->AttachmentId()); |
| 695 | for (const auto& stats : *report) { |
| 696 | if (stats.type() != RTCInboundRTPStreamStats::kType) |
| 697 | continue; |
| 698 | const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>(); |
| 699 | if (inbound_rtp.track_id.is_defined() && |
| 700 | *inbound_rtp.track_id == track_id) { |
| 701 | rtpstream_ids.push_back(inbound_rtp.id()); |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | if (rtpstream_ids.empty()) |
| 707 | return RTCStatsReport::Create(report->timestamp_us()); |
| 708 | return TakeReferencedStats(report->Copy(), rtpstream_ids); |
| 709 | } |
| 710 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 711 | } // namespace |
| 712 | |
Henrik Boström | 5b3541f | 2018-03-19 13:52:56 +0100 | [diff] [blame] | 713 | RTCStatsCollector::RequestInfo::RequestInfo( |
| 714 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) |
| 715 | : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {} |
| 716 | |
| 717 | RTCStatsCollector::RequestInfo::RequestInfo( |
| 718 | rtc::scoped_refptr<RtpSenderInternal> selector, |
| 719 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) |
| 720 | : RequestInfo(FilterMode::kSenderSelector, |
| 721 | std::move(callback), |
| 722 | std::move(selector), |
| 723 | nullptr) {} |
| 724 | |
| 725 | RTCStatsCollector::RequestInfo::RequestInfo( |
| 726 | rtc::scoped_refptr<RtpReceiverInternal> selector, |
| 727 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) |
| 728 | : RequestInfo(FilterMode::kReceiverSelector, |
| 729 | std::move(callback), |
| 730 | nullptr, |
| 731 | std::move(selector)) {} |
| 732 | |
| 733 | RTCStatsCollector::RequestInfo::RequestInfo( |
| 734 | RTCStatsCollector::RequestInfo::FilterMode filter_mode, |
| 735 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback, |
| 736 | rtc::scoped_refptr<RtpSenderInternal> sender_selector, |
| 737 | rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) |
| 738 | : filter_mode_(filter_mode), |
| 739 | callback_(std::move(callback)), |
| 740 | sender_selector_(std::move(sender_selector)), |
| 741 | receiver_selector_(std::move(receiver_selector)) { |
| 742 | RTC_DCHECK(callback_); |
| 743 | RTC_DCHECK(!sender_selector_ || !receiver_selector_); |
| 744 | } |
| 745 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 746 | rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create( |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 747 | PeerConnectionInternal* pc, |
| 748 | int64_t cache_lifetime_us) { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 749 | return rtc::scoped_refptr<RTCStatsCollector>( |
| 750 | new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us)); |
| 751 | } |
| 752 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 753 | RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc, |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 754 | int64_t cache_lifetime_us) |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 755 | : pc_(pc), |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 756 | signaling_thread_(pc->signaling_thread()), |
| 757 | worker_thread_(pc->worker_thread()), |
| 758 | network_thread_(pc->network_thread()), |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 759 | num_pending_partial_reports_(0), |
| 760 | partial_report_timestamp_us_(0), |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 761 | network_report_event_(true /* manual_reset */, |
| 762 | true /* initially_signaled */), |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 763 | cache_timestamp_us_(0), |
| 764 | cache_lifetime_us_(cache_lifetime_us) { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 765 | RTC_DCHECK(pc_); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 766 | RTC_DCHECK(signaling_thread_); |
| 767 | RTC_DCHECK(worker_thread_); |
| 768 | RTC_DCHECK(network_thread_); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 769 | RTC_DCHECK_GE(cache_lifetime_us_, 0); |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 770 | pc_->SignalDataChannelCreated().connect( |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 771 | this, &RTCStatsCollector::OnDataChannelCreated); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 772 | } |
| 773 | |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 774 | RTCStatsCollector::~RTCStatsCollector() { |
| 775 | RTC_DCHECK_EQ(num_pending_partial_reports_, 0); |
| 776 | } |
| 777 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 778 | void RTCStatsCollector::GetStatsReport( |
| 779 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
Henrik Boström | 5b3541f | 2018-03-19 13:52:56 +0100 | [diff] [blame] | 780 | GetStatsReportInternal(RequestInfo(std::move(callback))); |
| 781 | } |
| 782 | |
| 783 | void RTCStatsCollector::GetStatsReport( |
| 784 | rtc::scoped_refptr<RtpSenderInternal> selector, |
| 785 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 786 | GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback))); |
| 787 | } |
| 788 | |
| 789 | void RTCStatsCollector::GetStatsReport( |
| 790 | rtc::scoped_refptr<RtpReceiverInternal> selector, |
| 791 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 792 | GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback))); |
| 793 | } |
| 794 | |
| 795 | void RTCStatsCollector::GetStatsReportInternal( |
| 796 | RTCStatsCollector::RequestInfo request) { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 797 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
Henrik Boström | 5b3541f | 2018-03-19 13:52:56 +0100 | [diff] [blame] | 798 | requests_.push_back(std::move(request)); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 799 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 800 | // "Now" using a monotonically increasing timer. |
| 801 | int64_t cache_now_us = rtc::TimeMicros(); |
| 802 | if (cached_report_ && |
| 803 | cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) { |
Taylor Brandstetter | 25e022f | 2018-03-08 09:53:47 -0800 | [diff] [blame] | 804 | // We have a fresh cached report to deliver. Deliver asynchronously, since |
| 805 | // the caller may not be expecting a synchronous callback, and it avoids |
| 806 | // reentrancy problems. |
Henrik Boström | 5b3541f | 2018-03-19 13:52:56 +0100 | [diff] [blame] | 807 | std::vector<RequestInfo> requests; |
| 808 | requests.swap(requests_); |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 809 | signaling_thread_->PostTask( |
| 810 | RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this, |
| 811 | cached_report_, std::move(requests))); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 812 | } else if (!num_pending_partial_reports_) { |
| 813 | // Only start gathering stats if we're not already gathering stats. In the |
| 814 | // case of already gathering stats, |callback_| will be invoked when there |
| 815 | // are no more pending partial reports. |
| 816 | |
| 817 | // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970, |
| 818 | // UTC), in microseconds. The system clock could be modified and is not |
| 819 | // necessarily monotonically increasing. |
nisse | cdf37a9 | 2016-09-13 23:41:47 -0700 | [diff] [blame] | 820 | int64_t timestamp_us = rtc::TimeUTCMicros(); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 821 | |
hbos | f415f8a | 2017-01-02 04:28:51 -0800 | [diff] [blame] | 822 | num_pending_partial_reports_ = 2; |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 823 | partial_report_timestamp_us_ = cache_now_us; |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 824 | |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 825 | // Prepare |transceiver_stats_infos_| for use in |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 826 | // |ProducePartialResultsOnNetworkThread| and |
| 827 | // |ProducePartialResultsOnSignalingThread|. |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 828 | transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s(); |
Steve Anton | 7eca093 | 2018-03-30 15:18:41 -0700 | [diff] [blame] | 829 | // Prepare |transport_names_| for use in |
| 830 | // |ProducePartialResultsOnNetworkThread|. |
| 831 | transport_names_ = PrepareTransportNames_s(); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 832 | |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 833 | // Prepare |call_stats_| here since GetCallStats() will hop to the worker |
| 834 | // thread. |
| 835 | // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the |
| 836 | // network thread, where it more naturally belongs. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 837 | call_stats_ = pc_->GetCallStats(); |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 838 | |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 839 | // Don't touch |network_report_| on the signaling thread until |
| 840 | // ProducePartialResultsOnNetworkThread() has signaled the |
| 841 | // |network_report_event_|. |
| 842 | network_report_event_.Reset(); |
| 843 | network_thread_->PostTask( |
| 844 | RTC_FROM_HERE, |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 845 | rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread, |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 846 | this, timestamp_us)); |
hbos | f415f8a | 2017-01-02 04:28:51 -0800 | [diff] [blame] | 847 | ProducePartialResultsOnSignalingThread(timestamp_us); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 848 | } |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | void RTCStatsCollector::ClearCachedStatsReport() { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 852 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 853 | cached_report_ = nullptr; |
| 854 | } |
| 855 | |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 856 | void RTCStatsCollector::WaitForPendingRequest() { |
| 857 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 858 | // If a request is pending, blocks until the |network_report_event_| is |
| 859 | // signaled and then delivers the result. Otherwise this is a NO-OP. |
| 860 | MergeNetworkReport_s(); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 861 | } |
| 862 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 863 | void RTCStatsCollector::ProducePartialResultsOnSignalingThread( |
| 864 | int64_t timestamp_us) { |
| 865 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 866 | partial_report_ = RTCStatsReport::Create(timestamp_us); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 867 | |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 868 | ProducePartialResultsOnSignalingThreadImpl(timestamp_us, |
| 869 | partial_report_.get()); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 870 | |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 871 | // ProducePartialResultsOnSignalingThread() is running synchronously on the |
| 872 | // signaling thread, so it is always the first partial result delivered on the |
| 873 | // signaling thread. The request is not complete until MergeNetworkReport_s() |
| 874 | // happens; we don't have to do anything here. |
| 875 | RTC_DCHECK_GT(num_pending_partial_reports_, 1); |
| 876 | --num_pending_partial_reports_; |
| 877 | } |
| 878 | |
| 879 | void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl( |
| 880 | int64_t timestamp_us, |
| 881 | RTCStatsReport* partial_report) { |
| 882 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 883 | ProduceDataChannelStats_s(timestamp_us, partial_report); |
| 884 | ProduceMediaStreamStats_s(timestamp_us, partial_report); |
| 885 | ProduceMediaStreamTrackStats_s(timestamp_us, partial_report); |
| 886 | ProducePeerConnectionStats_s(timestamp_us, partial_report); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 887 | } |
| 888 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 889 | void RTCStatsCollector::ProducePartialResultsOnNetworkThread( |
| 890 | int64_t timestamp_us) { |
| 891 | RTC_DCHECK(network_thread_->IsCurrent()); |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 892 | // Touching |network_report_| on this thread is safe by this method because |
| 893 | // |network_report_event_| is reset before this method is invoked. |
| 894 | network_report_ = RTCStatsReport::Create(timestamp_us); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 895 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 896 | std::map<std::string, cricket::TransportStats> transport_stats_by_name = |
Steve Anton | 7eca093 | 2018-03-30 15:18:41 -0700 | [diff] [blame] | 897 | pc_->GetTransportStatsByNames(transport_names_); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 898 | std::map<std::string, CertificateStatsPair> transport_cert_stats = |
| 899 | PrepareTransportCertificateStats_n(transport_stats_by_name); |
| 900 | |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 901 | ProducePartialResultsOnNetworkThreadImpl( |
| 902 | timestamp_us, transport_stats_by_name, transport_cert_stats, |
| 903 | network_report_.get()); |
Mirko Bonadei | ca890ee | 2019-02-15 21:10:40 +0000 | [diff] [blame] | 904 | |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 905 | // Signal that it is now safe to touch |network_report_| on the signaling |
| 906 | // thread, and post a task to merge it into the final results. |
| 907 | network_report_event_.Set(); |
| 908 | signaling_thread_->PostTask( |
| 909 | RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this)); |
Henrik Boström | 05d43c6 | 2019-02-15 10:23:08 +0100 | [diff] [blame] | 910 | } |
| 911 | |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 912 | void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl( |
| 913 | int64_t timestamp_us, |
| 914 | const std::map<std::string, cricket::TransportStats>& |
| 915 | transport_stats_by_name, |
| 916 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
| 917 | RTCStatsReport* partial_report) { |
| 918 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 919 | ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report); |
| 920 | ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report); |
| 921 | ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name, |
| 922 | call_stats_, partial_report); |
| 923 | ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_, |
| 924 | partial_report); |
| 925 | ProduceTransportStats_n(timestamp_us, transport_stats_by_name, |
| 926 | transport_cert_stats, partial_report); |
| 927 | } |
| 928 | |
| 929 | void RTCStatsCollector::MergeNetworkReport_s() { |
| 930 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 931 | // The |network_report_event_| must be signaled for it to be safe to touch |
| 932 | // |network_report_|. This is normally not blocking, but if |
| 933 | // WaitForPendingRequest() is called while a request is pending, we might have |
| 934 | // to wait until the network thread is done touching |network_report_|. |
| 935 | network_report_event_.Wait(rtc::Event::kForever); |
| 936 | if (!network_report_) { |
| 937 | // Normally, MergeNetworkReport_s() is executed because it is posted from |
| 938 | // the network thread. But if WaitForPendingRequest() is called while a |
| 939 | // request is pending, an early call to MergeNetworkReport_s() is made, |
| 940 | // merging the report and setting |network_report_| to null. If so, when the |
| 941 | // previously posted MergeNetworkReport_s() is later executed, the report is |
| 942 | // already null and nothing needs to be done here. |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 943 | return; |
| 944 | } |
Mirko Bonadei | ca890ee | 2019-02-15 21:10:40 +0000 | [diff] [blame] | 945 | RTC_DCHECK_GT(num_pending_partial_reports_, 0); |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 946 | RTC_DCHECK(partial_report_); |
| 947 | partial_report_->TakeMembersFrom(network_report_); |
| 948 | network_report_ = nullptr; |
Mirko Bonadei | ca890ee | 2019-02-15 21:10:40 +0000 | [diff] [blame] | 949 | --num_pending_partial_reports_; |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 950 | // |network_report_| is currently the only partial report collected |
| 951 | // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are |
| 952 | // ready to deliver the result. |
| 953 | RTC_DCHECK_EQ(num_pending_partial_reports_, 0); |
| 954 | cache_timestamp_us_ = partial_report_timestamp_us_; |
| 955 | cached_report_ = partial_report_; |
| 956 | partial_report_ = nullptr; |
| 957 | transceiver_stats_infos_.clear(); |
| 958 | // Trace WebRTC Stats when getStats is called on Javascript. |
| 959 | // This allows access to WebRTC stats from trace logs. To enable them, |
| 960 | // select the "webrtc_stats" category when recording traces. |
| 961 | TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report", |
| 962 | cached_report_->ToJson()); |
Mirko Bonadei | ca890ee | 2019-02-15 21:10:40 +0000 | [diff] [blame] | 963 | |
Henrik Boström | 40b030e | 2019-02-28 09:49:31 +0100 | [diff] [blame] | 964 | // Deliver report and clear |requests_|. |
| 965 | std::vector<RequestInfo> requests; |
| 966 | requests.swap(requests_); |
| 967 | DeliverCachedReport(cached_report_, std::move(requests)); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Taylor Brandstetter | 25e022f | 2018-03-08 09:53:47 -0800 | [diff] [blame] | 970 | void RTCStatsCollector::DeliverCachedReport( |
| 971 | rtc::scoped_refptr<const RTCStatsReport> cached_report, |
Henrik Boström | 5b3541f | 2018-03-19 13:52:56 +0100 | [diff] [blame] | 972 | std::vector<RTCStatsCollector::RequestInfo> requests) { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 973 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
Henrik Boström | 5b3541f | 2018-03-19 13:52:56 +0100 | [diff] [blame] | 974 | RTC_DCHECK(!requests.empty()); |
Taylor Brandstetter | 25e022f | 2018-03-08 09:53:47 -0800 | [diff] [blame] | 975 | RTC_DCHECK(cached_report); |
Taylor Brandstetter | 87d5a74 | 2018-03-06 09:42:25 -0800 | [diff] [blame] | 976 | |
Henrik Boström | 5b3541f | 2018-03-19 13:52:56 +0100 | [diff] [blame] | 977 | for (const RequestInfo& request : requests) { |
| 978 | if (request.filter_mode() == RequestInfo::FilterMode::kAll) { |
| 979 | request.callback()->OnStatsDelivered(cached_report); |
| 980 | } else { |
| 981 | bool filter_by_sender_selector; |
| 982 | rtc::scoped_refptr<RtpSenderInternal> sender_selector; |
| 983 | rtc::scoped_refptr<RtpReceiverInternal> receiver_selector; |
| 984 | if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) { |
| 985 | filter_by_sender_selector = true; |
| 986 | sender_selector = request.sender_selector(); |
| 987 | } else { |
| 988 | RTC_DCHECK(request.filter_mode() == |
| 989 | RequestInfo::FilterMode::kReceiverSelector); |
| 990 | filter_by_sender_selector = false; |
| 991 | receiver_selector = request.receiver_selector(); |
| 992 | } |
| 993 | request.callback()->OnStatsDelivered(CreateReportFilteredBySelector( |
| 994 | filter_by_sender_selector, cached_report, sender_selector, |
| 995 | receiver_selector)); |
| 996 | } |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 997 | } |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 998 | } |
| 999 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1000 | void RTCStatsCollector::ProduceCertificateStats_n( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1001 | int64_t timestamp_us, |
| 1002 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 1003 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1004 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 1005 | for (const auto& transport_cert_stats_pair : transport_cert_stats) { |
| 1006 | if (transport_cert_stats_pair.second.local) { |
| 1007 | ProduceCertificateStatsFromSSLCertificateStats( |
| 1008 | timestamp_us, *transport_cert_stats_pair.second.local.get(), report); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 1009 | } |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 1010 | if (transport_cert_stats_pair.second.remote) { |
| 1011 | ProduceCertificateStatsFromSSLCertificateStats( |
| 1012 | timestamp_us, *transport_cert_stats_pair.second.remote.get(), report); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1017 | void RTCStatsCollector::ProduceCodecStats_n( |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1018 | int64_t timestamp_us, |
| 1019 | const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos, |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1020 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1021 | RTC_DCHECK(network_thread_->IsCurrent()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1022 | for (const auto& stats : transceiver_stats_infos) { |
| 1023 | if (!stats.mid) { |
| 1024 | continue; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1025 | } |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1026 | const cricket::VoiceMediaInfo* voice_media_info = |
| 1027 | stats.track_media_info_map->voice_media_info(); |
| 1028 | const cricket::VideoMediaInfo* video_media_info = |
| 1029 | stats.track_media_info_map->video_media_info(); |
| 1030 | // Audio |
| 1031 | if (voice_media_info) { |
| 1032 | // Inbound |
| 1033 | for (const auto& pair : voice_media_info->receive_codecs) { |
| 1034 | report->AddStats(CodecStatsFromRtpCodecParameters( |
| 1035 | timestamp_us, *stats.mid, true, pair.second)); |
| 1036 | } |
| 1037 | // Outbound |
| 1038 | for (const auto& pair : voice_media_info->send_codecs) { |
| 1039 | report->AddStats(CodecStatsFromRtpCodecParameters( |
| 1040 | timestamp_us, *stats.mid, false, pair.second)); |
| 1041 | } |
Guido Urdaneta | ee2388f | 2018-02-15 16:36:19 +0000 | [diff] [blame] | 1042 | } |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1043 | // Video |
| 1044 | if (video_media_info) { |
| 1045 | // Inbound |
| 1046 | for (const auto& pair : video_media_info->receive_codecs) { |
| 1047 | report->AddStats(CodecStatsFromRtpCodecParameters( |
| 1048 | timestamp_us, *stats.mid, true, pair.second)); |
| 1049 | } |
| 1050 | // Outbound |
| 1051 | for (const auto& pair : video_media_info->send_codecs) { |
| 1052 | report->AddStats(CodecStatsFromRtpCodecParameters( |
| 1053 | timestamp_us, *stats.mid, false, pair.second)); |
| 1054 | } |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 1059 | void RTCStatsCollector::ProduceDataChannelStats_s( |
| 1060 | int64_t timestamp_us, RTCStatsReport* report) const { |
| 1061 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 1062 | for (const rtc::scoped_refptr<DataChannel>& data_channel : |
| 1063 | pc_->sctp_data_channels()) { |
| 1064 | std::unique_ptr<RTCDataChannelStats> data_channel_stats( |
| 1065 | new RTCDataChannelStats( |
Jonas Olsson | 6b1985d | 2018-07-05 11:59:48 +0200 | [diff] [blame] | 1066 | "RTCDataChannel_" + rtc::ToString(data_channel->id()), |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 1067 | timestamp_us)); |
| 1068 | data_channel_stats->label = data_channel->label(); |
| 1069 | data_channel_stats->protocol = data_channel->protocol(); |
| 1070 | data_channel_stats->datachannelid = data_channel->id(); |
| 1071 | data_channel_stats->state = |
| 1072 | DataStateToRTCDataChannelState(data_channel->state()); |
| 1073 | data_channel_stats->messages_sent = data_channel->messages_sent(); |
| 1074 | data_channel_stats->bytes_sent = data_channel->bytes_sent(); |
| 1075 | data_channel_stats->messages_received = data_channel->messages_received(); |
| 1076 | data_channel_stats->bytes_received = data_channel->bytes_received(); |
| 1077 | report->AddStats(std::move(data_channel_stats)); |
| 1078 | } |
| 1079 | } |
| 1080 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1081 | void RTCStatsCollector::ProduceIceCandidateAndPairStats_n( |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 1082 | int64_t timestamp_us, |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1083 | const std::map<std::string, cricket::TransportStats>& |
| 1084 | transport_stats_by_name, |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 1085 | const Call::Stats& call_stats, |
| 1086 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1087 | RTC_DCHECK(network_thread_->IsCurrent()); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1088 | for (const auto& entry : transport_stats_by_name) { |
| 1089 | const std::string& transport_name = entry.first; |
| 1090 | const cricket::TransportStats& transport_stats = entry.second; |
| 1091 | for (const auto& channel_stats : transport_stats.channel_stats) { |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1092 | std::string transport_id = RTCTransportStatsIDFromTransportChannel( |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1093 | transport_name, channel_stats.component); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 1094 | for (const cricket::ConnectionInfo& info : |
| 1095 | channel_stats.connection_infos) { |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 1096 | std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1097 | new RTCIceCandidatePairStats( |
| 1098 | RTCIceCandidatePairStatsIDFromConnectionInfo(info), |
| 1099 | timestamp_us)); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 1100 | |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1101 | candidate_pair_stats->transport_id = transport_id; |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 1102 | // TODO(hbos): There could be other candidates that are not paired with |
| 1103 | // anything. We don't have a complete list. Local candidates come from |
| 1104 | // Port objects, and prflx candidates (both local and remote) are only |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 1105 | // stored in candidate pairs. https://crbug.com/632723 |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 1106 | candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats( |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 1107 | timestamp_us, info.local_candidate, true, transport_id, report); |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 1108 | candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats( |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 1109 | timestamp_us, info.remote_candidate, false, transport_id, report); |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 1110 | candidate_pair_stats->state = |
| 1111 | IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state); |
| 1112 | candidate_pair_stats->priority = info.priority; |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1113 | candidate_pair_stats->nominated = info.nominated; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 1114 | // TODO(hbos): This writable is different than the spec. It goes to |
| 1115 | // false after a certain amount of time without a response passes. |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 1116 | // https://crbug.com/633550 |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 1117 | candidate_pair_stats->writable = info.writable; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 1118 | candidate_pair_stats->bytes_sent = |
| 1119 | static_cast<uint64_t>(info.sent_total_bytes); |
| 1120 | candidate_pair_stats->bytes_received = |
| 1121 | static_cast<uint64_t>(info.recv_total_bytes); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1122 | candidate_pair_stats->total_round_trip_time = |
| 1123 | static_cast<double>(info.total_round_trip_time_ms) / |
| 1124 | rtc::kNumMillisecsPerSec; |
| 1125 | if (info.current_round_trip_time_ms) { |
| 1126 | candidate_pair_stats->current_round_trip_time = |
| 1127 | static_cast<double>(*info.current_round_trip_time_ms) / |
| 1128 | rtc::kNumMillisecsPerSec; |
| 1129 | } |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 1130 | if (info.best_connection) { |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 1131 | // The bandwidth estimations we have are for the selected candidate |
| 1132 | // pair ("info.best_connection"). |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 1133 | RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0); |
| 1134 | RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0); |
| 1135 | if (call_stats.send_bandwidth_bps > 0) { |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 1136 | candidate_pair_stats->available_outgoing_bitrate = |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 1137 | static_cast<double>(call_stats.send_bandwidth_bps); |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 1138 | } |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 1139 | if (call_stats.recv_bandwidth_bps > 0) { |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 1140 | candidate_pair_stats->available_incoming_bitrate = |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 1141 | static_cast<double>(call_stats.recv_bandwidth_bps); |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 1142 | } |
| 1143 | } |
hbos | d82f512 | 2016-12-09 04:12:39 -0800 | [diff] [blame] | 1144 | candidate_pair_stats->requests_received = |
| 1145 | static_cast<uint64_t>(info.recv_ping_requests); |
hbos | e448dd5 | 2016-12-12 01:22:53 -0800 | [diff] [blame] | 1146 | candidate_pair_stats->requests_sent = static_cast<uint64_t>( |
| 1147 | info.sent_ping_requests_before_first_response); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 1148 | candidate_pair_stats->responses_received = |
| 1149 | static_cast<uint64_t>(info.recv_ping_responses); |
| 1150 | candidate_pair_stats->responses_sent = |
| 1151 | static_cast<uint64_t>(info.sent_ping_responses); |
hbos | e448dd5 | 2016-12-12 01:22:53 -0800 | [diff] [blame] | 1152 | RTC_DCHECK_GE(info.sent_ping_requests_total, |
| 1153 | info.sent_ping_requests_before_first_response); |
| 1154 | candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>( |
| 1155 | info.sent_ping_requests_total - |
| 1156 | info.sent_ping_requests_before_first_response); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 1157 | |
| 1158 | report->AddStats(std::move(candidate_pair_stats)); |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1164 | void RTCStatsCollector::ProduceMediaStreamStats_s( |
| 1165 | int64_t timestamp_us, |
| 1166 | RTCStatsReport* report) const { |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1167 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1168 | |
| 1169 | std::map<std::string, std::vector<std::string>> track_ids; |
| 1170 | |
| 1171 | for (const auto& stats : transceiver_stats_infos_) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1172 | for (const auto& sender : stats.transceiver->senders()) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1173 | std::string track_id = |
| 1174 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 1175 | kSender, sender->internal()->AttachmentId()); |
| 1176 | for (auto& stream_id : sender->stream_ids()) { |
| 1177 | track_ids[stream_id].push_back(track_id); |
| 1178 | } |
| 1179 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1180 | for (const auto& receiver : stats.transceiver->receivers()) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1181 | std::string track_id = |
| 1182 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 1183 | kReceiver, receiver->internal()->AttachmentId()); |
| 1184 | for (auto& stream : receiver->streams()) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1185 | track_ids[stream->id()].push_back(track_id); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1186 | } |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | // Build stats for each stream ID known. |
| 1191 | for (auto& it : track_ids) { |
| 1192 | std::unique_ptr<RTCMediaStreamStats> stream_stats( |
| 1193 | new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us)); |
| 1194 | stream_stats->stream_identifier = it.first; |
| 1195 | stream_stats->track_ids = it.second; |
| 1196 | report->AddStats(std::move(stream_stats)); |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | void RTCStatsCollector::ProduceMediaStreamTrackStats_s( |
| 1201 | int64_t timestamp_us, |
| 1202 | RTCStatsReport* report) const { |
| 1203 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 1204 | for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) { |
| 1205 | std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1206 | for (const auto& sender : stats.transceiver->senders()) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1207 | senders.push_back(sender->internal()); |
| 1208 | } |
| 1209 | ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map, |
| 1210 | senders, report); |
| 1211 | |
| 1212 | std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1213 | for (const auto& receiver : stats.transceiver->receivers()) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1214 | receivers.push_back(receiver->internal()); |
| 1215 | } |
| 1216 | ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map, |
| 1217 | receivers, report); |
| 1218 | } |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1219 | } |
| 1220 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 1221 | void RTCStatsCollector::ProducePeerConnectionStats_s( |
| 1222 | int64_t timestamp_us, RTCStatsReport* report) const { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 1223 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1224 | std::unique_ptr<RTCPeerConnectionStats> stats( |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 1225 | new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us)); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1226 | stats->data_channels_opened = internal_record_.data_channels_opened; |
| 1227 | stats->data_channels_closed = internal_record_.data_channels_closed; |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 1228 | report->AddStats(std::move(stats)); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1231 | void RTCStatsCollector::ProduceRTPStreamStats_n( |
Steve Anton | 593e325 | 2017-12-15 11:44:48 -0800 | [diff] [blame] | 1232 | int64_t timestamp_us, |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1233 | const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos, |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1234 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1235 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1236 | |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1237 | for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) { |
| 1238 | if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1239 | ProduceAudioRTPStreamStats_n(timestamp_us, stats, report); |
| 1240 | } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1241 | ProduceVideoRTPStreamStats_n(timestamp_us, stats, report); |
| 1242 | } else { |
| 1243 | RTC_NOTREACHED(); |
Guido Urdaneta | ee2388f | 2018-02-15 16:36:19 +0000 | [diff] [blame] | 1244 | } |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame] | 1245 | } |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | void RTCStatsCollector::ProduceAudioRTPStreamStats_n( |
| 1249 | int64_t timestamp_us, |
| 1250 | const RtpTransceiverStatsInfo& stats, |
| 1251 | RTCStatsReport* report) const { |
| 1252 | if (!stats.mid || !stats.transport_name) { |
| 1253 | return; |
| 1254 | } |
| 1255 | RTC_DCHECK(stats.track_media_info_map); |
| 1256 | const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map; |
| 1257 | RTC_DCHECK(track_media_info_map.voice_media_info()); |
| 1258 | std::string mid = *stats.mid; |
| 1259 | std::string transport_id = RTCTransportStatsIDFromTransportChannel( |
| 1260 | *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 1261 | // Inbound |
| 1262 | for (const cricket::VoiceReceiverInfo& voice_receiver_info : |
| 1263 | track_media_info_map.voice_media_info()->receivers) { |
| 1264 | if (!voice_receiver_info.connected()) |
| 1265 | continue; |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 1266 | auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>( |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1267 | RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()), |
| 1268 | timestamp_us); |
| 1269 | SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info, |
| 1270 | inbound_audio.get()); |
| 1271 | // TODO(hta): This lookup should look for the sender, not the track. |
| 1272 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
| 1273 | track_media_info_map.GetAudioTrack(voice_receiver_info); |
| 1274 | if (audio_track) { |
| 1275 | inbound_audio->track_id = |
| 1276 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 1277 | kReceiver, |
| 1278 | track_media_info_map.GetAttachmentIdByTrack(audio_track).value()); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1279 | } |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1280 | inbound_audio->transport_id = transport_id; |
| 1281 | report->AddStats(std::move(inbound_audio)); |
| 1282 | } |
| 1283 | // Outbound |
| 1284 | for (const cricket::VoiceSenderInfo& voice_sender_info : |
| 1285 | track_media_info_map.voice_media_info()->senders) { |
| 1286 | if (!voice_sender_info.connected()) |
| 1287 | continue; |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 1288 | auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>( |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1289 | RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()), |
| 1290 | timestamp_us); |
| 1291 | SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info, |
| 1292 | outbound_audio.get()); |
| 1293 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
| 1294 | track_media_info_map.GetAudioTrack(voice_sender_info); |
| 1295 | if (audio_track) { |
| 1296 | outbound_audio->track_id = |
| 1297 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 1298 | kSender, |
| 1299 | track_media_info_map.GetAttachmentIdByTrack(audio_track).value()); |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame] | 1300 | } |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1301 | outbound_audio->transport_id = transport_id; |
| 1302 | report->AddStats(std::move(outbound_audio)); |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | void RTCStatsCollector::ProduceVideoRTPStreamStats_n( |
| 1307 | int64_t timestamp_us, |
| 1308 | const RtpTransceiverStatsInfo& stats, |
| 1309 | RTCStatsReport* report) const { |
| 1310 | if (!stats.mid || !stats.transport_name) { |
| 1311 | return; |
| 1312 | } |
| 1313 | RTC_DCHECK(stats.track_media_info_map); |
| 1314 | const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map; |
| 1315 | RTC_DCHECK(track_media_info_map.video_media_info()); |
| 1316 | std::string mid = *stats.mid; |
| 1317 | std::string transport_id = RTCTransportStatsIDFromTransportChannel( |
| 1318 | *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 1319 | // Inbound |
| 1320 | for (const cricket::VideoReceiverInfo& video_receiver_info : |
| 1321 | track_media_info_map.video_media_info()->receivers) { |
| 1322 | if (!video_receiver_info.connected()) |
| 1323 | continue; |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 1324 | auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>( |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1325 | RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()), |
| 1326 | timestamp_us); |
| 1327 | SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info, |
| 1328 | inbound_video.get()); |
| 1329 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
| 1330 | track_media_info_map.GetVideoTrack(video_receiver_info); |
| 1331 | if (video_track) { |
| 1332 | inbound_video->track_id = |
| 1333 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 1334 | kReceiver, |
| 1335 | track_media_info_map.GetAttachmentIdByTrack(video_track).value()); |
| 1336 | } |
| 1337 | inbound_video->transport_id = transport_id; |
| 1338 | report->AddStats(std::move(inbound_video)); |
| 1339 | } |
| 1340 | // Outbound |
| 1341 | for (const cricket::VideoSenderInfo& video_sender_info : |
| 1342 | track_media_info_map.video_media_info()->senders) { |
| 1343 | if (!video_sender_info.connected()) |
| 1344 | continue; |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 1345 | auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>( |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1346 | RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()), |
| 1347 | timestamp_us); |
| 1348 | SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info, |
| 1349 | outbound_video.get()); |
| 1350 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
| 1351 | track_media_info_map.GetVideoTrack(video_sender_info); |
| 1352 | if (video_track) { |
| 1353 | outbound_video->track_id = |
| 1354 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 1355 | kSender, |
| 1356 | track_media_info_map.GetAttachmentIdByTrack(video_track).value()); |
| 1357 | } |
| 1358 | outbound_video->transport_id = transport_id; |
| 1359 | report->AddStats(std::move(outbound_video)); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1363 | void RTCStatsCollector::ProduceTransportStats_n( |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1364 | int64_t timestamp_us, |
| 1365 | const std::map<std::string, cricket::TransportStats>& |
| 1366 | transport_stats_by_name, |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1367 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
| 1368 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1369 | RTC_DCHECK(network_thread_->IsCurrent()); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1370 | for (const auto& entry : transport_stats_by_name) { |
| 1371 | const std::string& transport_name = entry.first; |
| 1372 | const cricket::TransportStats& transport_stats = entry.second; |
| 1373 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1374 | // Get reference to RTCP channel, if it exists. |
| 1375 | std::string rtcp_transport_stats_id; |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1376 | for (const cricket::TransportChannelStats& channel_stats : |
| 1377 | transport_stats.channel_stats) { |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1378 | if (channel_stats.component == |
| 1379 | cricket::ICE_CANDIDATE_COMPONENT_RTCP) { |
| 1380 | rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel( |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1381 | transport_name, channel_stats.component); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1382 | break; |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | // Get reference to local and remote certificates of this transport, if they |
| 1387 | // exist. |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1388 | const auto& certificate_stats_it = |
| 1389 | transport_cert_stats.find(transport_name); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1390 | RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend()); |
| 1391 | std::string local_certificate_id; |
| 1392 | if (certificate_stats_it->second.local) { |
| 1393 | local_certificate_id = RTCCertificateIDFromFingerprint( |
| 1394 | certificate_stats_it->second.local->fingerprint); |
| 1395 | } |
| 1396 | std::string remote_certificate_id; |
| 1397 | if (certificate_stats_it->second.remote) { |
| 1398 | remote_certificate_id = RTCCertificateIDFromFingerprint( |
| 1399 | certificate_stats_it->second.remote->fingerprint); |
| 1400 | } |
| 1401 | |
| 1402 | // There is one transport stats for each channel. |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1403 | for (const cricket::TransportChannelStats& channel_stats : |
| 1404 | transport_stats.channel_stats) { |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1405 | std::unique_ptr<RTCTransportStats> transport_stats( |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1406 | new RTCTransportStats(RTCTransportStatsIDFromTransportChannel( |
| 1407 | transport_name, channel_stats.component), |
| 1408 | timestamp_us)); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1409 | transport_stats->bytes_sent = 0; |
| 1410 | transport_stats->bytes_received = 0; |
hbos | 7064d59 | 2017-01-16 07:38:02 -0800 | [diff] [blame] | 1411 | transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState( |
| 1412 | channel_stats.dtls_state); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1413 | for (const cricket::ConnectionInfo& info : |
| 1414 | channel_stats.connection_infos) { |
| 1415 | *transport_stats->bytes_sent += info.sent_total_bytes; |
| 1416 | *transport_stats->bytes_received += info.recv_total_bytes; |
| 1417 | if (info.best_connection) { |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1418 | transport_stats->selected_candidate_pair_id = |
| 1419 | RTCIceCandidatePairStatsIDFromConnectionInfo(info); |
| 1420 | } |
| 1421 | } |
| 1422 | if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP && |
| 1423 | !rtcp_transport_stats_id.empty()) { |
| 1424 | transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id; |
| 1425 | } |
| 1426 | if (!local_certificate_id.empty()) |
| 1427 | transport_stats->local_certificate_id = local_certificate_id; |
| 1428 | if (!remote_certificate_id.empty()) |
| 1429 | transport_stats->remote_certificate_id = remote_certificate_id; |
| 1430 | report->AddStats(std::move(transport_stats)); |
| 1431 | } |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | std::map<std::string, RTCStatsCollector::CertificateStatsPair> |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1436 | RTCStatsCollector::PrepareTransportCertificateStats_n( |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1437 | const std::map<std::string, cricket::TransportStats>& |
| 1438 | transport_stats_by_name) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1439 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1440 | std::map<std::string, CertificateStatsPair> transport_cert_stats; |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1441 | for (const auto& entry : transport_stats_by_name) { |
| 1442 | const std::string& transport_name = entry.first; |
| 1443 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1444 | CertificateStatsPair certificate_stats_pair; |
| 1445 | rtc::scoped_refptr<rtc::RTCCertificate> local_certificate; |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1446 | if (pc_->GetLocalCertificate(transport_name, &local_certificate)) { |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1447 | certificate_stats_pair.local = |
Benjamin Wright | 6c6c9df | 2018-10-25 01:16:26 -0700 | [diff] [blame] | 1448 | local_certificate->GetSSLCertificateChain().GetStats(); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1449 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1450 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 1451 | std::unique_ptr<rtc::SSLCertChain> remote_cert_chain = |
| 1452 | pc_->GetRemoteSSLCertChain(transport_name); |
| 1453 | if (remote_cert_chain) { |
| 1454 | certificate_stats_pair.remote = remote_cert_chain->GetStats(); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1455 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1456 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1457 | transport_cert_stats.insert( |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1458 | std::make_pair(transport_name, std::move(certificate_stats_pair))); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1459 | } |
| 1460 | return transport_cert_stats; |
| 1461 | } |
| 1462 | |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1463 | std::vector<RTCStatsCollector::RtpTransceiverStatsInfo> |
| 1464 | RTCStatsCollector::PrepareTransceiverStatsInfos_s() const { |
| 1465 | std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos; |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame] | 1466 | |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1467 | // These are used to invoke GetStats for all the media channels together in |
| 1468 | // one worker thread hop. |
| 1469 | std::map<cricket::VoiceMediaChannel*, |
| 1470 | std::unique_ptr<cricket::VoiceMediaInfo>> |
| 1471 | voice_stats; |
| 1472 | std::map<cricket::VideoMediaChannel*, |
| 1473 | std::unique_ptr<cricket::VideoMediaInfo>> |
| 1474 | video_stats; |
| 1475 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1476 | for (const auto& transceiver : pc_->GetTransceiversInternal()) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1477 | cricket::MediaType media_type = transceiver->media_type(); |
| 1478 | |
| 1479 | // Prepare stats entry. The TrackMediaInfoMap will be filled in after the |
| 1480 | // stats have been fetched on the worker thread. |
| 1481 | transceiver_stats_infos.emplace_back(); |
| 1482 | RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back(); |
| 1483 | stats.transceiver = transceiver->internal(); |
| 1484 | stats.media_type = media_type; |
| 1485 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1486 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1487 | if (!channel) { |
| 1488 | // The remaining fields require a BaseChannel. |
| 1489 | continue; |
| 1490 | } |
| 1491 | |
| 1492 | stats.mid = channel->content_name(); |
| 1493 | stats.transport_name = channel->transport_name(); |
| 1494 | |
| 1495 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1496 | auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel); |
| 1497 | RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) == |
| 1498 | voice_stats.end()); |
| 1499 | voice_stats[voice_channel->media_channel()] = |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 1500 | absl::make_unique<cricket::VoiceMediaInfo>(); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1501 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1502 | auto* video_channel = static_cast<cricket::VideoChannel*>(channel); |
| 1503 | RTC_DCHECK(video_stats.find(video_channel->media_channel()) == |
| 1504 | video_stats.end()); |
| 1505 | video_stats[video_channel->media_channel()] = |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 1506 | absl::make_unique<cricket::VideoMediaInfo>(); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1507 | } else { |
| 1508 | RTC_NOTREACHED(); |
| 1509 | } |
Guido Urdaneta | ee2388f | 2018-02-15 16:36:19 +0000 | [diff] [blame] | 1510 | } |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1511 | |
| 1512 | // Call GetStats for all media channels together on the worker thread in one |
| 1513 | // hop. |
| 1514 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 1515 | for (const auto& entry : voice_stats) { |
| 1516 | if (!entry.first->GetStats(entry.second.get())) { |
| 1517 | RTC_LOG(LS_WARNING) << "Failed to get voice stats."; |
| 1518 | } |
| 1519 | } |
| 1520 | for (const auto& entry : video_stats) { |
| 1521 | if (!entry.first->GetStats(entry.second.get())) { |
| 1522 | RTC_LOG(LS_WARNING) << "Failed to get video stats."; |
| 1523 | } |
| 1524 | } |
| 1525 | }); |
| 1526 | |
| 1527 | // Create the TrackMediaInfoMap for each transceiver stats object. |
| 1528 | for (auto& stats : transceiver_stats_infos) { |
| 1529 | auto transceiver = stats.transceiver; |
| 1530 | std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info; |
| 1531 | std::unique_ptr<cricket::VideoMediaInfo> video_media_info; |
| 1532 | if (transceiver->channel()) { |
| 1533 | cricket::MediaType media_type = transceiver->media_type(); |
| 1534 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1535 | auto* voice_channel = |
| 1536 | static_cast<cricket::VoiceChannel*>(transceiver->channel()); |
| 1537 | RTC_DCHECK(voice_stats[voice_channel->media_channel()]); |
| 1538 | voice_media_info = |
| 1539 | std::move(voice_stats[voice_channel->media_channel()]); |
| 1540 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1541 | auto* video_channel = |
| 1542 | static_cast<cricket::VideoChannel*>(transceiver->channel()); |
| 1543 | RTC_DCHECK(video_stats[video_channel->media_channel()]); |
| 1544 | video_media_info = |
| 1545 | std::move(video_stats[video_channel->media_channel()]); |
| 1546 | } |
| 1547 | } |
| 1548 | std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1549 | for (const auto& sender : transceiver->senders()) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1550 | senders.push_back(sender->internal()); |
| 1551 | } |
| 1552 | std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1553 | for (const auto& receiver : transceiver->receivers()) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1554 | receivers.push_back(receiver->internal()); |
| 1555 | } |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 1556 | stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>( |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1557 | std::move(voice_media_info), std::move(video_media_info), senders, |
| 1558 | receivers); |
Guido Urdaneta | ee2388f | 2018-02-15 16:36:19 +0000 | [diff] [blame] | 1559 | } |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1560 | |
| 1561 | return transceiver_stats_infos; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1562 | } |
| 1563 | |
Steve Anton | 7eca093 | 2018-03-30 15:18:41 -0700 | [diff] [blame] | 1564 | std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const { |
| 1565 | std::set<std::string> transport_names; |
| 1566 | for (const auto& transceiver : pc_->GetTransceiversInternal()) { |
| 1567 | if (transceiver->internal()->channel()) { |
| 1568 | transport_names.insert( |
| 1569 | transceiver->internal()->channel()->transport_name()); |
| 1570 | } |
| 1571 | } |
| 1572 | if (pc_->rtp_data_channel()) { |
| 1573 | transport_names.insert(pc_->rtp_data_channel()->transport_name()); |
| 1574 | } |
| 1575 | if (pc_->sctp_transport_name()) { |
| 1576 | transport_names.insert(*pc_->sctp_transport_name()); |
| 1577 | } |
| 1578 | return transport_names; |
| 1579 | } |
| 1580 | |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1581 | void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) { |
| 1582 | channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened); |
| 1583 | channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed); |
| 1584 | } |
| 1585 | |
| 1586 | void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) { |
| 1587 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 1588 | bool result = internal_record_.opened_data_channels.insert( |
| 1589 | reinterpret_cast<uintptr_t>(channel)).second; |
| 1590 | ++internal_record_.data_channels_opened; |
| 1591 | RTC_DCHECK(result); |
| 1592 | } |
| 1593 | |
| 1594 | void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) { |
| 1595 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 1596 | // Only channels that have been fully opened (and have increased the |
| 1597 | // |data_channels_opened_| counter) increase the closed counter. |
hbos | 5bf9def | 2017-03-20 03:14:14 -0700 | [diff] [blame] | 1598 | if (internal_record_.opened_data_channels.erase( |
| 1599 | reinterpret_cast<uintptr_t>(channel))) { |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1600 | ++internal_record_.data_channels_closed; |
| 1601 | } |
| 1602 | } |
| 1603 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 1604 | const char* CandidateTypeToRTCIceCandidateTypeForTesting( |
| 1605 | const std::string& type) { |
| 1606 | return CandidateTypeToRTCIceCandidateType(type); |
| 1607 | } |
| 1608 | |
| 1609 | const char* DataStateToRTCDataChannelStateForTesting( |
| 1610 | DataChannelInterface::DataState state) { |
| 1611 | return DataStateToRTCDataChannelState(state); |
| 1612 | } |
| 1613 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1614 | } // namespace webrtc |