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 | 56bae8d | 2018-02-14 16:07:42 -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 | 56bae8d | 2018-02-14 16:07:42 -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 | 56bae8d | 2018-02-14 16:07:42 -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 | 56bae8d | 2018-02-14 16:07:42 -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 | 56bae8d | 2018-02-14 16:07:42 -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 | 56bae8d | 2018-02-14 16:07:42 -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 | 56bae8d | 2018-02-14 16:07:42 -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 | 56bae8d | 2018-02-14 16:07:42 -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 | 56bae8d | 2018-02-14 16:07:42 -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 | 56bae8d | 2018-02-14 16:07:42 -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 | 56bae8d | 2018-02-14 16:07:42 -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 | 56bae8d | 2018-02-14 16:07:42 -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; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 458 | return video_track_stats; |
| 459 | } |
| 460 | |
| 461 | std::unique_ptr<RTCMediaStreamTrackStats> |
| 462 | ProduceMediaStreamTrackStatsFromVideoReceiverInfo( |
| 463 | int64_t timestamp_us, |
| 464 | const VideoTrackInterface& video_track, |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 465 | const cricket::VideoReceiverInfo& video_receiver_info, |
| 466 | int attachment_id) { |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 467 | std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats( |
| 468 | new RTCMediaStreamTrackStats( |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 469 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver, |
| 470 | |
| 471 | attachment_id), |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 472 | timestamp_us, RTCMediaStreamTrackKind::kVideo)); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 473 | SetMediaStreamTrackStatsFromMediaStreamTrackInterface( |
| 474 | video_track, video_track_stats.get()); |
| 475 | video_track_stats->remote_source = true; |
| 476 | video_track_stats->detached = false; |
| 477 | if (video_receiver_info.frame_width > 0 && |
| 478 | video_receiver_info.frame_height > 0) { |
| 479 | video_track_stats->frame_width = static_cast<uint32_t>( |
| 480 | video_receiver_info.frame_width); |
| 481 | video_track_stats->frame_height = static_cast<uint32_t>( |
| 482 | video_receiver_info.frame_height); |
| 483 | } |
hbos | 42f6d2f | 2017-01-20 03:56:50 -0800 | [diff] [blame] | 484 | video_track_stats->frames_received = video_receiver_info.frames_received; |
hbos | f64941f | 2017-01-20 07:39:09 -0800 | [diff] [blame] | 485 | // TODO(hbos): When we support receiving simulcast, this should be the total |
| 486 | // number of frames correctly decoded, independent of which SSRC it was |
| 487 | // 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] | 488 | // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137 |
hbos | f64941f | 2017-01-20 07:39:09 -0800 | [diff] [blame] | 489 | video_track_stats->frames_decoded = video_receiver_info.frames_decoded; |
hbos | 50cfe1f | 2017-01-23 07:21:55 -0800 | [diff] [blame] | 490 | RTC_DCHECK_GE(video_receiver_info.frames_received, |
| 491 | video_receiver_info.frames_rendered); |
| 492 | video_track_stats->frames_dropped = video_receiver_info.frames_received - |
| 493 | video_receiver_info.frames_rendered; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 494 | return video_track_stats; |
| 495 | } |
| 496 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 497 | void ProduceSenderMediaTrackStats( |
| 498 | int64_t timestamp_us, |
| 499 | const TrackMediaInfoMap& track_media_info_map, |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 500 | std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders, |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 501 | RTCStatsReport* report) { |
| 502 | // This function iterates over the senders to generate outgoing track stats. |
| 503 | |
| 504 | // TODO(hbos): Return stats of detached tracks. We have to perform stats |
| 505 | // gathering at the time of detachment to get accurate stats and timestamps. |
| 506 | // https://crbug.com/659137 |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 507 | for (auto sender : senders) { |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 508 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 509 | AudioTrackInterface* track = |
| 510 | static_cast<AudioTrackInterface*>(sender->track().get()); |
| 511 | if (!track) |
| 512 | continue; |
Harald Alvestrand | b8e1201 | 2018-01-23 15:28:16 +0100 | [diff] [blame] | 513 | cricket::VoiceSenderInfo null_sender_info; |
| 514 | const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info; |
| 515 | // TODO(hta): Checking on ssrc is not proper. There should be a way |
| 516 | // to see from a sender whether it's connected or not. |
| 517 | // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none") |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 518 | if (sender->ssrc() != 0) { |
Harald Alvestrand | 76d2952 | 2018-01-30 14:43:29 +0100 | [diff] [blame] | 519 | // When pc.close is called, sender info is discarded, so |
| 520 | // we generate zeroes instead. Bug: It should be retained. |
| 521 | // https://crbug.com/807174 |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 522 | const cricket::VoiceSenderInfo* sender_info = |
Harald Alvestrand | b8e1201 | 2018-01-23 15:28:16 +0100 | [diff] [blame] | 523 | track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc()); |
Harald Alvestrand | 76d2952 | 2018-01-30 14:43:29 +0100 | [diff] [blame] | 524 | if (sender_info) { |
| 525 | voice_sender_info = sender_info; |
| 526 | } else { |
| 527 | RTC_LOG(LS_INFO) |
| 528 | << "RTCStatsCollector: No voice sender info for sender with ssrc " |
| 529 | << sender->ssrc(); |
| 530 | } |
Harald Alvestrand | b8e1201 | 2018-01-23 15:28:16 +0100 | [diff] [blame] | 531 | } |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 532 | std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats = |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 533 | ProduceMediaStreamTrackStatsFromVoiceSenderInfo( |
| 534 | timestamp_us, *track, *voice_sender_info, sender->AttachmentId()); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 535 | report->AddStats(std::move(audio_track_stats)); |
| 536 | } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
| 537 | VideoTrackInterface* track = |
| 538 | static_cast<VideoTrackInterface*>(sender->track().get()); |
| 539 | if (!track) |
| 540 | continue; |
Harald Alvestrand | b8e1201 | 2018-01-23 15:28:16 +0100 | [diff] [blame] | 541 | cricket::VideoSenderInfo null_sender_info; |
| 542 | const cricket::VideoSenderInfo* video_sender_info = &null_sender_info; |
| 543 | // TODO(hta): Check on state not ssrc when state is available |
Harald Alvestrand | 76d2952 | 2018-01-30 14:43:29 +0100 | [diff] [blame] | 544 | // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate |
| 545 | // "none") |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 546 | if (sender->ssrc() != 0) { |
Harald Alvestrand | 76d2952 | 2018-01-30 14:43:29 +0100 | [diff] [blame] | 547 | // When pc.close is called, sender info is discarded, so |
| 548 | // we generate zeroes instead. Bug: It should be retained. |
| 549 | // https://crbug.com/807174 |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 550 | const cricket::VideoSenderInfo* sender_info = |
Harald Alvestrand | b8e1201 | 2018-01-23 15:28:16 +0100 | [diff] [blame] | 551 | track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc()); |
Harald Alvestrand | 76d2952 | 2018-01-30 14:43:29 +0100 | [diff] [blame] | 552 | if (sender_info) { |
| 553 | video_sender_info = sender_info; |
| 554 | } else { |
| 555 | RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc " |
| 556 | << sender->ssrc(); |
| 557 | } |
| 558 | } |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 559 | std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats = |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 560 | ProduceMediaStreamTrackStatsFromVideoSenderInfo( |
| 561 | timestamp_us, *track, *video_sender_info, sender->AttachmentId()); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 562 | report->AddStats(std::move(video_track_stats)); |
| 563 | } else { |
| 564 | RTC_NOTREACHED(); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | void ProduceReceiverMediaTrackStats( |
| 570 | int64_t timestamp_us, |
| 571 | const TrackMediaInfoMap& track_media_info_map, |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 572 | std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers, |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 573 | RTCStatsReport* report) { |
| 574 | // This function iterates over the receivers to find the remote tracks. |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 575 | for (auto receiver : receivers) { |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 576 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 577 | AudioTrackInterface* track = |
| 578 | static_cast<AudioTrackInterface*>(receiver->track().get()); |
| 579 | const cricket::VoiceReceiverInfo* voice_receiver_info = |
| 580 | track_media_info_map.GetVoiceReceiverInfo(*track); |
| 581 | if (!voice_receiver_info) { |
| 582 | continue; |
| 583 | } |
| 584 | std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats = |
| 585 | ProduceMediaStreamTrackStatsFromVoiceReceiverInfo( |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 586 | timestamp_us, *track, *voice_receiver_info, |
| 587 | receiver->AttachmentId()); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 588 | report->AddStats(std::move(audio_track_stats)); |
| 589 | } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
| 590 | VideoTrackInterface* track = |
| 591 | static_cast<VideoTrackInterface*>(receiver->track().get()); |
| 592 | const cricket::VideoReceiverInfo* video_receiver_info = |
| 593 | track_media_info_map.GetVideoReceiverInfo(*track); |
| 594 | if (!video_receiver_info) { |
| 595 | continue; |
| 596 | } |
| 597 | std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats = |
| 598 | ProduceMediaStreamTrackStatsFromVideoReceiverInfo( |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 599 | timestamp_us, *track, *video_receiver_info, |
| 600 | receiver->AttachmentId()); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 601 | report->AddStats(std::move(video_track_stats)); |
| 602 | } else { |
| 603 | RTC_NOTREACHED(); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 608 | } // namespace |
| 609 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 610 | rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create( |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 611 | PeerConnectionInternal* pc, |
| 612 | int64_t cache_lifetime_us) { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 613 | return rtc::scoped_refptr<RTCStatsCollector>( |
| 614 | new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us)); |
| 615 | } |
| 616 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 617 | RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc, |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 618 | int64_t cache_lifetime_us) |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 619 | : pc_(pc), |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 620 | signaling_thread_(pc->signaling_thread()), |
| 621 | worker_thread_(pc->worker_thread()), |
| 622 | network_thread_(pc->network_thread()), |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 623 | num_pending_partial_reports_(0), |
| 624 | partial_report_timestamp_us_(0), |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 625 | cache_timestamp_us_(0), |
| 626 | cache_lifetime_us_(cache_lifetime_us) { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 627 | RTC_DCHECK(pc_); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 628 | RTC_DCHECK(signaling_thread_); |
| 629 | RTC_DCHECK(worker_thread_); |
| 630 | RTC_DCHECK(network_thread_); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 631 | RTC_DCHECK_GE(cache_lifetime_us_, 0); |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 632 | pc_->SignalDataChannelCreated().connect( |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 633 | this, &RTCStatsCollector::OnDataChannelCreated); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 634 | } |
| 635 | |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 636 | RTCStatsCollector::~RTCStatsCollector() { |
| 637 | RTC_DCHECK_EQ(num_pending_partial_reports_, 0); |
| 638 | } |
| 639 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 640 | void RTCStatsCollector::GetStatsReport( |
| 641 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 642 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 643 | RTC_DCHECK(callback); |
| 644 | callbacks_.push_back(callback); |
| 645 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 646 | // "Now" using a monotonically increasing timer. |
| 647 | int64_t cache_now_us = rtc::TimeMicros(); |
| 648 | if (cached_report_ && |
| 649 | cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 650 | // We have a fresh cached report to deliver. |
| 651 | DeliverCachedReport(); |
| 652 | } else if (!num_pending_partial_reports_) { |
| 653 | // Only start gathering stats if we're not already gathering stats. In the |
| 654 | // case of already gathering stats, |callback_| will be invoked when there |
| 655 | // are no more pending partial reports. |
| 656 | |
| 657 | // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970, |
| 658 | // UTC), in microseconds. The system clock could be modified and is not |
| 659 | // necessarily monotonically increasing. |
nisse | cdf37a9 | 2016-09-13 23:41:47 -0700 | [diff] [blame] | 660 | int64_t timestamp_us = rtc::TimeUTCMicros(); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 661 | |
hbos | f415f8a | 2017-01-02 04:28:51 -0800 | [diff] [blame] | 662 | num_pending_partial_reports_ = 2; |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 663 | partial_report_timestamp_us_ = cache_now_us; |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 664 | |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 665 | // Prepare |transceiver_stats_infos_| for use in |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 666 | // |ProducePartialResultsOnNetworkThread| and |
| 667 | // |ProducePartialResultsOnSignalingThread|. |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 668 | transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s(); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 669 | |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 670 | // Prepare |call_stats_| here since GetCallStats() will hop to the worker |
| 671 | // thread. |
| 672 | // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the |
| 673 | // network thread, where it more naturally belongs. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 674 | call_stats_ = pc_->GetCallStats(); |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 675 | |
| 676 | invoker_.AsyncInvoke<void>( |
| 677 | RTC_FROM_HERE, network_thread_, |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 678 | rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread, |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 679 | rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us)); |
hbos | f415f8a | 2017-01-02 04:28:51 -0800 | [diff] [blame] | 680 | ProducePartialResultsOnSignalingThread(timestamp_us); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 681 | } |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | void RTCStatsCollector::ClearCachedStatsReport() { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 685 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 686 | cached_report_ = nullptr; |
| 687 | } |
| 688 | |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 689 | void RTCStatsCollector::WaitForPendingRequest() { |
| 690 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 691 | if (num_pending_partial_reports_) { |
| 692 | rtc::Thread::Current()->ProcessMessages(0); |
| 693 | while (num_pending_partial_reports_) { |
| 694 | rtc::Thread::Current()->SleepMs(1); |
| 695 | rtc::Thread::Current()->ProcessMessages(0); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 700 | void RTCStatsCollector::ProducePartialResultsOnSignalingThread( |
| 701 | int64_t timestamp_us) { |
| 702 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 703 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create( |
| 704 | timestamp_us); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 705 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 706 | ProduceDataChannelStats_s(timestamp_us, report.get()); |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 707 | ProduceMediaStreamStats_s(timestamp_us, report.get()); |
| 708 | ProduceMediaStreamTrackStats_s(timestamp_us, report.get()); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 709 | ProducePeerConnectionStats_s(timestamp_us, report.get()); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 710 | |
| 711 | AddPartialResults(report); |
| 712 | } |
| 713 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 714 | void RTCStatsCollector::ProducePartialResultsOnNetworkThread( |
| 715 | int64_t timestamp_us) { |
| 716 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 717 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create( |
| 718 | timestamp_us); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 719 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 720 | std::set<std::string> transport_names; |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 721 | for (const auto& stats : transceiver_stats_infos_) { |
| 722 | transport_names.insert(stats.transport_name); |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 723 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 724 | std::map<std::string, cricket::TransportStats> transport_stats_by_name = |
| 725 | pc_->GetTransportStatsByNames(transport_names); |
| 726 | |
| 727 | std::map<std::string, CertificateStatsPair> transport_cert_stats = |
| 728 | PrepareTransportCertificateStats_n(transport_stats_by_name); |
| 729 | |
| 730 | ProduceCertificateStats_n(timestamp_us, transport_cert_stats, report.get()); |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 731 | ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, report.get()); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 732 | ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name, |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 733 | call_stats_, report.get()); |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 734 | ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_, report.get()); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 735 | ProduceTransportStats_n(timestamp_us, transport_stats_by_name, |
| 736 | transport_cert_stats, report.get()); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 737 | |
| 738 | AddPartialResults(report); |
| 739 | } |
| 740 | |
| 741 | void RTCStatsCollector::AddPartialResults( |
| 742 | const rtc::scoped_refptr<RTCStatsReport>& partial_report) { |
| 743 | if (!signaling_thread_->IsCurrent()) { |
| 744 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_, |
| 745 | rtc::Bind(&RTCStatsCollector::AddPartialResults_s, |
| 746 | rtc::scoped_refptr<RTCStatsCollector>(this), |
| 747 | partial_report)); |
| 748 | return; |
| 749 | } |
| 750 | AddPartialResults_s(partial_report); |
| 751 | } |
| 752 | |
| 753 | void RTCStatsCollector::AddPartialResults_s( |
| 754 | rtc::scoped_refptr<RTCStatsReport> partial_report) { |
| 755 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 756 | RTC_DCHECK_GT(num_pending_partial_reports_, 0); |
| 757 | if (!partial_report_) |
| 758 | partial_report_ = partial_report; |
| 759 | else |
| 760 | partial_report_->TakeMembersFrom(partial_report); |
| 761 | --num_pending_partial_reports_; |
| 762 | if (!num_pending_partial_reports_) { |
| 763 | cache_timestamp_us_ = partial_report_timestamp_us_; |
| 764 | cached_report_ = partial_report_; |
| 765 | partial_report_ = nullptr; |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 766 | transceiver_stats_infos_.clear(); |
ehmaldonado | a26196b | 2017-07-18 03:30:29 -0700 | [diff] [blame] | 767 | // Trace WebRTC Stats when getStats is called on Javascript. |
| 768 | // This allows access to WebRTC stats from trace logs. To enable them, |
| 769 | // select the "webrtc_stats" category when recording traces. |
ehmaldonado | 8ab0fd8 | 2017-09-04 14:35:04 -0700 | [diff] [blame] | 770 | TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report", |
| 771 | cached_report_->ToJson()); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 772 | DeliverCachedReport(); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | void RTCStatsCollector::DeliverCachedReport() { |
| 777 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 778 | RTC_DCHECK(!callbacks_.empty()); |
| 779 | RTC_DCHECK(cached_report_); |
| 780 | for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback : |
| 781 | callbacks_) { |
| 782 | callback->OnStatsDelivered(cached_report_); |
| 783 | } |
| 784 | callbacks_.clear(); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 785 | } |
| 786 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 787 | void RTCStatsCollector::ProduceCertificateStats_n( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 788 | int64_t timestamp_us, |
| 789 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 790 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 791 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 792 | for (const auto& transport_cert_stats_pair : transport_cert_stats) { |
| 793 | if (transport_cert_stats_pair.second.local) { |
| 794 | ProduceCertificateStatsFromSSLCertificateStats( |
| 795 | timestamp_us, *transport_cert_stats_pair.second.local.get(), report); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 796 | } |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 797 | if (transport_cert_stats_pair.second.remote) { |
| 798 | ProduceCertificateStatsFromSSLCertificateStats( |
| 799 | timestamp_us, *transport_cert_stats_pair.second.remote.get(), report); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 804 | void RTCStatsCollector::ProduceCodecStats_n( |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 805 | int64_t timestamp_us, |
| 806 | const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos, |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 807 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 808 | RTC_DCHECK(network_thread_->IsCurrent()); |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 809 | for (const auto& stats : transceiver_stats_infos) { |
| 810 | const cricket::VoiceMediaInfo* voice_media_info = |
| 811 | stats.track_media_info_map->voice_media_info(); |
| 812 | const cricket::VideoMediaInfo* video_media_info = |
| 813 | stats.track_media_info_map->video_media_info(); |
| 814 | // Audio |
| 815 | if (voice_media_info) { |
| 816 | // Inbound |
| 817 | for (const auto& pair : voice_media_info->receive_codecs) { |
| 818 | report->AddStats(CodecStatsFromRtpCodecParameters( |
| 819 | timestamp_us, stats.mid, true, pair.second)); |
| 820 | } |
| 821 | // Outbound |
| 822 | for (const auto& pair : voice_media_info->send_codecs) { |
| 823 | report->AddStats(CodecStatsFromRtpCodecParameters( |
| 824 | timestamp_us, stats.mid, false, pair.second)); |
| 825 | } |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 826 | } |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 827 | // Video |
| 828 | if (video_media_info) { |
| 829 | // Inbound |
| 830 | for (const auto& pair : video_media_info->receive_codecs) { |
| 831 | report->AddStats(CodecStatsFromRtpCodecParameters( |
| 832 | timestamp_us, stats.mid, true, pair.second)); |
| 833 | } |
| 834 | // Outbound |
| 835 | for (const auto& pair : video_media_info->send_codecs) { |
| 836 | report->AddStats(CodecStatsFromRtpCodecParameters( |
| 837 | timestamp_us, stats.mid, false, pair.second)); |
| 838 | } |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 843 | void RTCStatsCollector::ProduceDataChannelStats_s( |
| 844 | int64_t timestamp_us, RTCStatsReport* report) const { |
| 845 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 846 | for (const rtc::scoped_refptr<DataChannel>& data_channel : |
| 847 | pc_->sctp_data_channels()) { |
| 848 | std::unique_ptr<RTCDataChannelStats> data_channel_stats( |
| 849 | new RTCDataChannelStats( |
| 850 | "RTCDataChannel_" + rtc::ToString<>(data_channel->id()), |
| 851 | timestamp_us)); |
| 852 | data_channel_stats->label = data_channel->label(); |
| 853 | data_channel_stats->protocol = data_channel->protocol(); |
| 854 | data_channel_stats->datachannelid = data_channel->id(); |
| 855 | data_channel_stats->state = |
| 856 | DataStateToRTCDataChannelState(data_channel->state()); |
| 857 | data_channel_stats->messages_sent = data_channel->messages_sent(); |
| 858 | data_channel_stats->bytes_sent = data_channel->bytes_sent(); |
| 859 | data_channel_stats->messages_received = data_channel->messages_received(); |
| 860 | data_channel_stats->bytes_received = data_channel->bytes_received(); |
| 861 | report->AddStats(std::move(data_channel_stats)); |
| 862 | } |
| 863 | } |
| 864 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 865 | void RTCStatsCollector::ProduceIceCandidateAndPairStats_n( |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 866 | int64_t timestamp_us, |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 867 | const std::map<std::string, cricket::TransportStats>& |
| 868 | transport_stats_by_name, |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 869 | const Call::Stats& call_stats, |
| 870 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 871 | RTC_DCHECK(network_thread_->IsCurrent()); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 872 | for (const auto& entry : transport_stats_by_name) { |
| 873 | const std::string& transport_name = entry.first; |
| 874 | const cricket::TransportStats& transport_stats = entry.second; |
| 875 | for (const auto& channel_stats : transport_stats.channel_stats) { |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 876 | std::string transport_id = RTCTransportStatsIDFromTransportChannel( |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 877 | transport_name, channel_stats.component); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 878 | for (const cricket::ConnectionInfo& info : |
| 879 | channel_stats.connection_infos) { |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 880 | std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 881 | new RTCIceCandidatePairStats( |
| 882 | RTCIceCandidatePairStatsIDFromConnectionInfo(info), |
| 883 | timestamp_us)); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 884 | |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 885 | candidate_pair_stats->transport_id = transport_id; |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 886 | // TODO(hbos): There could be other candidates that are not paired with |
| 887 | // anything. We don't have a complete list. Local candidates come from |
| 888 | // Port objects, and prflx candidates (both local and remote) are only |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 889 | // stored in candidate pairs. https://crbug.com/632723 |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 890 | candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats( |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 891 | timestamp_us, info.local_candidate, true, transport_id, report); |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 892 | candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats( |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 893 | timestamp_us, info.remote_candidate, false, transport_id, report); |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 894 | candidate_pair_stats->state = |
| 895 | IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state); |
| 896 | candidate_pair_stats->priority = info.priority; |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 897 | candidate_pair_stats->nominated = info.nominated; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 898 | // TODO(hbos): This writable is different than the spec. It goes to |
| 899 | // false after a certain amount of time without a response passes. |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 900 | // https://crbug.com/633550 |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 901 | candidate_pair_stats->writable = info.writable; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 902 | candidate_pair_stats->bytes_sent = |
| 903 | static_cast<uint64_t>(info.sent_total_bytes); |
| 904 | candidate_pair_stats->bytes_received = |
| 905 | static_cast<uint64_t>(info.recv_total_bytes); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 906 | candidate_pair_stats->total_round_trip_time = |
| 907 | static_cast<double>(info.total_round_trip_time_ms) / |
| 908 | rtc::kNumMillisecsPerSec; |
| 909 | if (info.current_round_trip_time_ms) { |
| 910 | candidate_pair_stats->current_round_trip_time = |
| 911 | static_cast<double>(*info.current_round_trip_time_ms) / |
| 912 | rtc::kNumMillisecsPerSec; |
| 913 | } |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 914 | if (info.best_connection) { |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 915 | // The bandwidth estimations we have are for the selected candidate |
| 916 | // pair ("info.best_connection"). |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 917 | RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0); |
| 918 | RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0); |
| 919 | if (call_stats.send_bandwidth_bps > 0) { |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 920 | candidate_pair_stats->available_outgoing_bitrate = |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 921 | static_cast<double>(call_stats.send_bandwidth_bps); |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 922 | } |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 923 | if (call_stats.recv_bandwidth_bps > 0) { |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 924 | candidate_pair_stats->available_incoming_bitrate = |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 925 | static_cast<double>(call_stats.recv_bandwidth_bps); |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 926 | } |
| 927 | } |
hbos | d82f512 | 2016-12-09 04:12:39 -0800 | [diff] [blame] | 928 | candidate_pair_stats->requests_received = |
| 929 | static_cast<uint64_t>(info.recv_ping_requests); |
hbos | e448dd5 | 2016-12-12 01:22:53 -0800 | [diff] [blame] | 930 | candidate_pair_stats->requests_sent = static_cast<uint64_t>( |
| 931 | info.sent_ping_requests_before_first_response); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 932 | candidate_pair_stats->responses_received = |
| 933 | static_cast<uint64_t>(info.recv_ping_responses); |
| 934 | candidate_pair_stats->responses_sent = |
| 935 | static_cast<uint64_t>(info.sent_ping_responses); |
hbos | e448dd5 | 2016-12-12 01:22:53 -0800 | [diff] [blame] | 936 | RTC_DCHECK_GE(info.sent_ping_requests_total, |
| 937 | info.sent_ping_requests_before_first_response); |
| 938 | candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>( |
| 939 | info.sent_ping_requests_total - |
| 940 | info.sent_ping_requests_before_first_response); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 941 | |
| 942 | report->AddStats(std::move(candidate_pair_stats)); |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 948 | void RTCStatsCollector::ProduceMediaStreamStats_s( |
| 949 | int64_t timestamp_us, |
| 950 | RTCStatsReport* report) const { |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 951 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 952 | |
| 953 | std::map<std::string, std::vector<std::string>> track_ids; |
| 954 | |
| 955 | for (const auto& stats : transceiver_stats_infos_) { |
| 956 | for (auto sender : stats.transceiver->senders()) { |
| 957 | std::string track_id = |
| 958 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 959 | kSender, sender->internal()->AttachmentId()); |
| 960 | for (auto& stream_id : sender->stream_ids()) { |
| 961 | track_ids[stream_id].push_back(track_id); |
| 962 | } |
| 963 | } |
| 964 | for (auto receiver : stats.transceiver->receivers()) { |
| 965 | std::string track_id = |
| 966 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 967 | kReceiver, receiver->internal()->AttachmentId()); |
| 968 | for (auto& stream : receiver->streams()) { |
| 969 | track_ids[stream->label()].push_back(track_id); |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | // Build stats for each stream ID known. |
| 975 | for (auto& it : track_ids) { |
| 976 | std::unique_ptr<RTCMediaStreamStats> stream_stats( |
| 977 | new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us)); |
| 978 | stream_stats->stream_identifier = it.first; |
| 979 | stream_stats->track_ids = it.second; |
| 980 | report->AddStats(std::move(stream_stats)); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | void RTCStatsCollector::ProduceMediaStreamTrackStats_s( |
| 985 | int64_t timestamp_us, |
| 986 | RTCStatsReport* report) const { |
| 987 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 988 | for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) { |
| 989 | std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders; |
| 990 | for (auto sender : stats.transceiver->senders()) { |
| 991 | senders.push_back(sender->internal()); |
| 992 | } |
| 993 | ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map, |
| 994 | senders, report); |
| 995 | |
| 996 | std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers; |
| 997 | for (auto receiver : stats.transceiver->receivers()) { |
| 998 | receivers.push_back(receiver->internal()); |
| 999 | } |
| 1000 | ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map, |
| 1001 | receivers, report); |
| 1002 | } |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 1005 | void RTCStatsCollector::ProducePeerConnectionStats_s( |
| 1006 | int64_t timestamp_us, RTCStatsReport* report) const { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 1007 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1008 | std::unique_ptr<RTCPeerConnectionStats> stats( |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 1009 | new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us)); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1010 | stats->data_channels_opened = internal_record_.data_channels_opened; |
| 1011 | stats->data_channels_closed = internal_record_.data_channels_closed; |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 1012 | report->AddStats(std::move(stats)); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1015 | void RTCStatsCollector::ProduceRTPStreamStats_n( |
Steve Anton | 593e325 | 2017-12-15 11:44:48 -0800 | [diff] [blame] | 1016 | int64_t timestamp_us, |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 1017 | const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos, |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1018 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1019 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1020 | |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 1021 | for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) { |
| 1022 | if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1023 | ProduceAudioRTPStreamStats_n(timestamp_us, stats, report); |
| 1024 | } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1025 | ProduceVideoRTPStreamStats_n(timestamp_us, stats, report); |
| 1026 | } else { |
| 1027 | RTC_NOTREACHED(); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1028 | } |
| 1029 | } |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 1030 | } |
| 1031 | |
| 1032 | void RTCStatsCollector::ProduceAudioRTPStreamStats_n( |
| 1033 | int64_t timestamp_us, |
| 1034 | const RtpTransceiverStatsInfo& stats, |
| 1035 | RTCStatsReport* report) const { |
| 1036 | RTC_DCHECK(stats.track_media_info_map); |
| 1037 | const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map; |
| 1038 | RTC_DCHECK(track_media_info_map.voice_media_info()); |
| 1039 | std::string transport_id = RTCTransportStatsIDFromTransportChannel( |
| 1040 | stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 1041 | // Inbound |
| 1042 | for (const cricket::VoiceReceiverInfo& voice_receiver_info : |
| 1043 | track_media_info_map.voice_media_info()->receivers) { |
| 1044 | if (!voice_receiver_info.connected()) |
| 1045 | continue; |
| 1046 | auto inbound_audio = rtc::MakeUnique<RTCInboundRTPStreamStats>( |
| 1047 | RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()), |
| 1048 | timestamp_us); |
| 1049 | SetInboundRTPStreamStatsFromVoiceReceiverInfo( |
| 1050 | stats.mid, voice_receiver_info, inbound_audio.get()); |
| 1051 | // TODO(hta): This lookup should look for the sender, not the track. |
| 1052 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
| 1053 | track_media_info_map.GetAudioTrack(voice_receiver_info); |
| 1054 | if (audio_track) { |
| 1055 | inbound_audio->track_id = |
| 1056 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 1057 | kReceiver, |
| 1058 | track_media_info_map.GetAttachmentIdByTrack(audio_track).value()); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1059 | } |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 1060 | inbound_audio->transport_id = transport_id; |
| 1061 | report->AddStats(std::move(inbound_audio)); |
| 1062 | } |
| 1063 | // Outbound |
| 1064 | for (const cricket::VoiceSenderInfo& voice_sender_info : |
| 1065 | track_media_info_map.voice_media_info()->senders) { |
| 1066 | if (!voice_sender_info.connected()) |
| 1067 | continue; |
| 1068 | auto outbound_audio = rtc::MakeUnique<RTCOutboundRTPStreamStats>( |
| 1069 | RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()), |
| 1070 | timestamp_us); |
| 1071 | SetOutboundRTPStreamStatsFromVoiceSenderInfo(stats.mid, voice_sender_info, |
| 1072 | outbound_audio.get()); |
| 1073 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
| 1074 | track_media_info_map.GetAudioTrack(voice_sender_info); |
| 1075 | if (audio_track) { |
| 1076 | outbound_audio->track_id = |
| 1077 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 1078 | kSender, |
| 1079 | track_media_info_map.GetAttachmentIdByTrack(audio_track).value()); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1080 | } |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 1081 | outbound_audio->transport_id = transport_id; |
| 1082 | report->AddStats(std::move(outbound_audio)); |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | void RTCStatsCollector::ProduceVideoRTPStreamStats_n( |
| 1087 | int64_t timestamp_us, |
| 1088 | const RtpTransceiverStatsInfo& stats, |
| 1089 | RTCStatsReport* report) const { |
| 1090 | RTC_DCHECK(stats.track_media_info_map); |
| 1091 | const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map; |
| 1092 | RTC_DCHECK(track_media_info_map.video_media_info()); |
| 1093 | std::string transport_id = RTCTransportStatsIDFromTransportChannel( |
| 1094 | stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 1095 | // Inbound |
| 1096 | for (const cricket::VideoReceiverInfo& video_receiver_info : |
| 1097 | track_media_info_map.video_media_info()->receivers) { |
| 1098 | if (!video_receiver_info.connected()) |
| 1099 | continue; |
| 1100 | auto inbound_video = rtc::MakeUnique<RTCInboundRTPStreamStats>( |
| 1101 | RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()), |
| 1102 | timestamp_us); |
| 1103 | SetInboundRTPStreamStatsFromVideoReceiverInfo( |
| 1104 | stats.mid, video_receiver_info, inbound_video.get()); |
| 1105 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
| 1106 | track_media_info_map.GetVideoTrack(video_receiver_info); |
| 1107 | if (video_track) { |
| 1108 | inbound_video->track_id = |
| 1109 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 1110 | kReceiver, |
| 1111 | track_media_info_map.GetAttachmentIdByTrack(video_track).value()); |
| 1112 | } |
| 1113 | inbound_video->transport_id = transport_id; |
| 1114 | report->AddStats(std::move(inbound_video)); |
| 1115 | } |
| 1116 | // Outbound |
| 1117 | for (const cricket::VideoSenderInfo& video_sender_info : |
| 1118 | track_media_info_map.video_media_info()->senders) { |
| 1119 | if (!video_sender_info.connected()) |
| 1120 | continue; |
| 1121 | auto outbound_video = rtc::MakeUnique<RTCOutboundRTPStreamStats>( |
| 1122 | RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()), |
| 1123 | timestamp_us); |
| 1124 | SetOutboundRTPStreamStatsFromVideoSenderInfo(stats.mid, video_sender_info, |
| 1125 | outbound_video.get()); |
| 1126 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
| 1127 | track_media_info_map.GetVideoTrack(video_sender_info); |
| 1128 | if (video_track) { |
| 1129 | outbound_video->track_id = |
| 1130 | RTCMediaStreamTrackStatsIDFromDirectionAndAttachment( |
| 1131 | kSender, |
| 1132 | track_media_info_map.GetAttachmentIdByTrack(video_track).value()); |
| 1133 | } |
| 1134 | outbound_video->transport_id = transport_id; |
| 1135 | report->AddStats(std::move(outbound_video)); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1139 | void RTCStatsCollector::ProduceTransportStats_n( |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1140 | int64_t timestamp_us, |
| 1141 | const std::map<std::string, cricket::TransportStats>& |
| 1142 | transport_stats_by_name, |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1143 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
| 1144 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1145 | RTC_DCHECK(network_thread_->IsCurrent()); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1146 | for (const auto& entry : transport_stats_by_name) { |
| 1147 | const std::string& transport_name = entry.first; |
| 1148 | const cricket::TransportStats& transport_stats = entry.second; |
| 1149 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1150 | // Get reference to RTCP channel, if it exists. |
| 1151 | std::string rtcp_transport_stats_id; |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1152 | for (const cricket::TransportChannelStats& channel_stats : |
| 1153 | transport_stats.channel_stats) { |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1154 | if (channel_stats.component == |
| 1155 | cricket::ICE_CANDIDATE_COMPONENT_RTCP) { |
| 1156 | rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel( |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1157 | transport_name, channel_stats.component); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1158 | break; |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | // Get reference to local and remote certificates of this transport, if they |
| 1163 | // exist. |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1164 | const auto& certificate_stats_it = |
| 1165 | transport_cert_stats.find(transport_name); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1166 | RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend()); |
| 1167 | std::string local_certificate_id; |
| 1168 | if (certificate_stats_it->second.local) { |
| 1169 | local_certificate_id = RTCCertificateIDFromFingerprint( |
| 1170 | certificate_stats_it->second.local->fingerprint); |
| 1171 | } |
| 1172 | std::string remote_certificate_id; |
| 1173 | if (certificate_stats_it->second.remote) { |
| 1174 | remote_certificate_id = RTCCertificateIDFromFingerprint( |
| 1175 | certificate_stats_it->second.remote->fingerprint); |
| 1176 | } |
| 1177 | |
| 1178 | // There is one transport stats for each channel. |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1179 | for (const cricket::TransportChannelStats& channel_stats : |
| 1180 | transport_stats.channel_stats) { |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1181 | std::unique_ptr<RTCTransportStats> transport_stats( |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1182 | new RTCTransportStats(RTCTransportStatsIDFromTransportChannel( |
| 1183 | transport_name, channel_stats.component), |
| 1184 | timestamp_us)); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1185 | transport_stats->bytes_sent = 0; |
| 1186 | transport_stats->bytes_received = 0; |
hbos | 7064d59 | 2017-01-16 07:38:02 -0800 | [diff] [blame] | 1187 | transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState( |
| 1188 | channel_stats.dtls_state); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1189 | for (const cricket::ConnectionInfo& info : |
| 1190 | channel_stats.connection_infos) { |
| 1191 | *transport_stats->bytes_sent += info.sent_total_bytes; |
| 1192 | *transport_stats->bytes_received += info.recv_total_bytes; |
| 1193 | if (info.best_connection) { |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1194 | transport_stats->selected_candidate_pair_id = |
| 1195 | RTCIceCandidatePairStatsIDFromConnectionInfo(info); |
| 1196 | } |
| 1197 | } |
| 1198 | if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP && |
| 1199 | !rtcp_transport_stats_id.empty()) { |
| 1200 | transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id; |
| 1201 | } |
| 1202 | if (!local_certificate_id.empty()) |
| 1203 | transport_stats->local_certificate_id = local_certificate_id; |
| 1204 | if (!remote_certificate_id.empty()) |
| 1205 | transport_stats->remote_certificate_id = remote_certificate_id; |
| 1206 | report->AddStats(std::move(transport_stats)); |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | std::map<std::string, RTCStatsCollector::CertificateStatsPair> |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1212 | RTCStatsCollector::PrepareTransportCertificateStats_n( |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1213 | const std::map<std::string, cricket::TransportStats>& |
| 1214 | transport_stats_by_name) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1215 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1216 | std::map<std::string, CertificateStatsPair> transport_cert_stats; |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1217 | for (const auto& entry : transport_stats_by_name) { |
| 1218 | const std::string& transport_name = entry.first; |
| 1219 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1220 | CertificateStatsPair certificate_stats_pair; |
| 1221 | rtc::scoped_refptr<rtc::RTCCertificate> local_certificate; |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1222 | if (pc_->GetLocalCertificate(transport_name, &local_certificate)) { |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1223 | certificate_stats_pair.local = |
| 1224 | local_certificate->ssl_certificate().GetStats(); |
| 1225 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1226 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1227 | std::unique_ptr<rtc::SSLCertificate> remote_certificate = |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1228 | pc_->GetRemoteSSLCertificate(transport_name); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1229 | if (remote_certificate) { |
| 1230 | certificate_stats_pair.remote = remote_certificate->GetStats(); |
| 1231 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1232 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1233 | transport_cert_stats.insert( |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 1234 | std::make_pair(transport_name, std::move(certificate_stats_pair))); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1235 | } |
| 1236 | return transport_cert_stats; |
| 1237 | } |
| 1238 | |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 1239 | std::vector<RTCStatsCollector::RtpTransceiverStatsInfo> |
| 1240 | RTCStatsCollector::PrepareTransceiverStatsInfos_s() const { |
| 1241 | std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos; |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1242 | |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 1243 | // These are used to invoke GetStats for all the media channels together in |
| 1244 | // one worker thread hop. |
| 1245 | std::map<cricket::VoiceMediaChannel*, |
| 1246 | std::unique_ptr<cricket::VoiceMediaInfo>> |
| 1247 | voice_stats; |
| 1248 | std::map<cricket::VideoMediaChannel*, |
| 1249 | std::unique_ptr<cricket::VideoMediaInfo>> |
| 1250 | video_stats; |
| 1251 | |
| 1252 | for (auto transceiver : pc_->GetTransceiversInternal()) { |
| 1253 | cricket::MediaType media_type = transceiver->internal()->media_type(); |
| 1254 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 1255 | if (!channel) { |
| 1256 | // No BaseChannel, no stats to collect. |
| 1257 | continue; |
| 1258 | } |
| 1259 | |
| 1260 | // Prepare stats entry. The TrackMediaInfoMap will be filled in after the |
| 1261 | // stats have been fetched on the worker thread. |
| 1262 | transceiver_stats_infos.emplace_back(); |
| 1263 | RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back(); |
| 1264 | stats.transceiver = transceiver->internal(); |
| 1265 | stats.media_type = media_type; |
| 1266 | stats.mid = channel->content_name(); |
| 1267 | stats.transport_name = channel->transport_name(); |
| 1268 | |
| 1269 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1270 | auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel); |
| 1271 | RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) == |
| 1272 | voice_stats.end()); |
| 1273 | voice_stats[voice_channel->media_channel()] = |
| 1274 | rtc::MakeUnique<cricket::VoiceMediaInfo>(); |
| 1275 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1276 | auto* video_channel = static_cast<cricket::VideoChannel*>(channel); |
| 1277 | RTC_DCHECK(video_stats.find(video_channel->media_channel()) == |
| 1278 | video_stats.end()); |
| 1279 | video_stats[video_channel->media_channel()] = |
| 1280 | rtc::MakeUnique<cricket::VideoMediaInfo>(); |
| 1281 | } else { |
| 1282 | RTC_NOTREACHED(); |
| 1283 | } |
hbos | 7b0c6fa | 2017-07-12 16:22:34 -0700 | [diff] [blame] | 1284 | } |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 1285 | |
| 1286 | // Call GetStats for all media channels together on the worker thread in one |
| 1287 | // hop. |
| 1288 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 1289 | for (const auto& entry : voice_stats) { |
| 1290 | if (!entry.first->GetStats(entry.second.get())) { |
| 1291 | RTC_LOG(LS_WARNING) << "Failed to get voice stats."; |
| 1292 | } |
| 1293 | } |
| 1294 | for (const auto& entry : video_stats) { |
| 1295 | if (!entry.first->GetStats(entry.second.get())) { |
| 1296 | RTC_LOG(LS_WARNING) << "Failed to get video stats."; |
| 1297 | } |
| 1298 | } |
| 1299 | }); |
| 1300 | |
| 1301 | // Create the TrackMediaInfoMap for each transceiver stats object. |
| 1302 | for (auto& stats : transceiver_stats_infos) { |
| 1303 | auto transceiver = stats.transceiver; |
| 1304 | RTC_DCHECK(transceiver->channel()); |
| 1305 | std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info; |
| 1306 | std::unique_ptr<cricket::VideoMediaInfo> video_media_info; |
| 1307 | cricket::MediaType media_type = transceiver->media_type(); |
| 1308 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1309 | auto* voice_channel = |
| 1310 | static_cast<cricket::VoiceChannel*>(transceiver->channel()); |
| 1311 | RTC_DCHECK(voice_stats[voice_channel->media_channel()]); |
| 1312 | voice_media_info = std::move(voice_stats[voice_channel->media_channel()]); |
| 1313 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1314 | auto* video_channel = |
| 1315 | static_cast<cricket::VideoChannel*>(transceiver->channel()); |
| 1316 | RTC_DCHECK(video_stats[video_channel->media_channel()]); |
| 1317 | video_media_info = std::move(video_stats[video_channel->media_channel()]); |
| 1318 | } |
| 1319 | std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders; |
| 1320 | for (auto sender : transceiver->senders()) { |
| 1321 | senders.push_back(sender->internal()); |
| 1322 | } |
| 1323 | std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers; |
| 1324 | for (auto receiver : transceiver->receivers()) { |
| 1325 | receivers.push_back(receiver->internal()); |
| 1326 | } |
| 1327 | stats.track_media_info_map = rtc::MakeUnique<TrackMediaInfoMap>( |
| 1328 | std::move(voice_media_info), std::move(video_media_info), senders, |
| 1329 | receivers); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1330 | } |
Steve Anton | 56bae8d | 2018-02-14 16:07:42 -0800 | [diff] [blame^] | 1331 | |
| 1332 | return transceiver_stats_infos; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1335 | void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) { |
| 1336 | channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened); |
| 1337 | channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed); |
| 1338 | } |
| 1339 | |
| 1340 | void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) { |
| 1341 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 1342 | bool result = internal_record_.opened_data_channels.insert( |
| 1343 | reinterpret_cast<uintptr_t>(channel)).second; |
| 1344 | ++internal_record_.data_channels_opened; |
| 1345 | RTC_DCHECK(result); |
| 1346 | } |
| 1347 | |
| 1348 | void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) { |
| 1349 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 1350 | // Only channels that have been fully opened (and have increased the |
| 1351 | // |data_channels_opened_| counter) increase the closed counter. |
hbos | 5bf9def | 2017-03-20 03:14:14 -0700 | [diff] [blame] | 1352 | if (internal_record_.opened_data_channels.erase( |
| 1353 | reinterpret_cast<uintptr_t>(channel))) { |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1354 | ++internal_record_.data_channels_closed; |
| 1355 | } |
| 1356 | } |
| 1357 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 1358 | const char* CandidateTypeToRTCIceCandidateTypeForTesting( |
| 1359 | const std::string& type) { |
| 1360 | return CandidateTypeToRTCIceCandidateType(type); |
| 1361 | } |
| 1362 | |
| 1363 | const char* DataStateToRTCDataChannelStateForTesting( |
| 1364 | DataChannelInterface::DataState state) { |
| 1365 | return DataStateToRTCDataChannelState(state); |
| 1366 | } |
| 1367 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1368 | } // namespace webrtc |