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