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