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