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 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 11 | #include "webrtc/pc/rtcstatscollector.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 14 | #include <sstream> |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 15 | #include <utility> |
| 16 | #include <vector> |
| 17 | |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 18 | #include "webrtc/api/mediastreaminterface.h" |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 19 | #include "webrtc/api/peerconnectioninterface.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 20 | #include "webrtc/base/checks.h" |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 21 | #include "webrtc/base/timeutils.h" |
| 22 | #include "webrtc/media/base/mediachannel.h" |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 23 | #include "webrtc/p2p/base/candidate.h" |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 24 | #include "webrtc/p2p/base/p2pconstants.h" |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 25 | #include "webrtc/p2p/base/port.h" |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 26 | #include "webrtc/pc/peerconnection.h" |
| 27 | #include "webrtc/pc/webrtcsession.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 31 | namespace { |
| 32 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 33 | std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) { |
| 34 | return "RTCCertificate_" + fingerprint; |
| 35 | } |
| 36 | |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 37 | std::string RTCCodecStatsIDFromDirectionMediaAndPayload( |
| 38 | bool inbound, bool audio, uint32_t payload_type) { |
| 39 | // TODO(hbos): When we are able to handle multiple m= lines of the same media |
| 40 | // type (and multiple BaseChannels for the same type is possible?) this needs |
| 41 | // to be updated to differentiate the transport being used, and stats need to |
| 42 | // be collected for all of them. crbug.com/659117 |
| 43 | if (inbound) { |
| 44 | return audio ? "RTCCodec_InboundAudio_" + rtc::ToString<>(payload_type) |
| 45 | : "RTCCodec_InboundVideo_" + rtc::ToString<>(payload_type); |
| 46 | } |
| 47 | return audio ? "RTCCodec_OutboundAudio_" + rtc::ToString<>(payload_type) |
| 48 | : "RTCCodec_OutboundVideo_" + rtc::ToString<>(payload_type); |
| 49 | } |
| 50 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 51 | std::string RTCIceCandidatePairStatsIDFromConnectionInfo( |
| 52 | const cricket::ConnectionInfo& info) { |
| 53 | return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" + |
| 54 | info.remote_candidate.id(); |
| 55 | } |
| 56 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 57 | std::string RTCMediaStreamTrackStatsIDFromTrackKindIDAndSsrc( |
| 58 | bool is_local, const char* kind, const std::string& id, uint32_t ssrc) { |
| 59 | RTC_DCHECK(kind == MediaStreamTrackInterface::kAudioKind || |
| 60 | kind == MediaStreamTrackInterface::kVideoKind); |
| 61 | std::ostringstream oss; |
| 62 | oss << (is_local ? "RTCMediaStreamTrack_local_" |
| 63 | : "RTCMediaStreamTrack_remote_"); |
| 64 | oss << kind << "_"; |
| 65 | oss << id << "_"; |
| 66 | oss << ssrc; |
| 67 | return oss.str(); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 68 | } |
| 69 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 70 | std::string RTCTransportStatsIDFromTransportChannel( |
| 71 | const std::string& transport_name, int channel_component) { |
| 72 | return "RTCTransport_" + transport_name + "_" + |
| 73 | rtc::ToString<>(channel_component); |
| 74 | } |
| 75 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 76 | std::string RTCTransportStatsIDFromBaseChannel( |
| 77 | const ProxyTransportMap& proxy_to_transport, |
| 78 | const cricket::BaseChannel& base_channel) { |
| 79 | auto proxy_it = proxy_to_transport.find(base_channel.content_name()); |
| 80 | if (proxy_it == proxy_to_transport.cend()) |
| 81 | return ""; |
| 82 | return RTCTransportStatsIDFromTransportChannel( |
| 83 | proxy_it->second, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 84 | } |
| 85 | |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 86 | std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) { |
| 87 | return audio ? "RTCInboundRTPAudioStream_" + rtc::ToString<>(ssrc) |
| 88 | : "RTCInboundRTPVideoStream_" + rtc::ToString<>(ssrc); |
| 89 | } |
| 90 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 91 | std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) { |
| 92 | return audio ? "RTCOutboundRTPAudioStream_" + rtc::ToString<>(ssrc) |
| 93 | : "RTCOutboundRTPVideoStream_" + rtc::ToString<>(ssrc); |
| 94 | } |
| 95 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 96 | const char* CandidateTypeToRTCIceCandidateType(const std::string& type) { |
| 97 | if (type == cricket::LOCAL_PORT_TYPE) |
| 98 | return RTCIceCandidateType::kHost; |
| 99 | if (type == cricket::STUN_PORT_TYPE) |
| 100 | return RTCIceCandidateType::kSrflx; |
| 101 | if (type == cricket::PRFLX_PORT_TYPE) |
| 102 | return RTCIceCandidateType::kPrflx; |
| 103 | if (type == cricket::RELAY_PORT_TYPE) |
| 104 | return RTCIceCandidateType::kRelay; |
| 105 | RTC_NOTREACHED(); |
| 106 | return nullptr; |
| 107 | } |
| 108 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 109 | const char* DataStateToRTCDataChannelState( |
| 110 | DataChannelInterface::DataState state) { |
| 111 | switch (state) { |
| 112 | case DataChannelInterface::kConnecting: |
| 113 | return RTCDataChannelState::kConnecting; |
| 114 | case DataChannelInterface::kOpen: |
| 115 | return RTCDataChannelState::kOpen; |
| 116 | case DataChannelInterface::kClosing: |
| 117 | return RTCDataChannelState::kClosing; |
| 118 | case DataChannelInterface::kClosed: |
| 119 | return RTCDataChannelState::kClosed; |
| 120 | default: |
| 121 | RTC_NOTREACHED(); |
| 122 | return nullptr; |
| 123 | } |
| 124 | } |
| 125 | |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 126 | const char* IceCandidatePairStateToRTCStatsIceCandidatePairState( |
| 127 | cricket::IceCandidatePairState state) { |
| 128 | switch (state) { |
| 129 | case cricket::IceCandidatePairState::WAITING: |
| 130 | return RTCStatsIceCandidatePairState::kWaiting; |
| 131 | case cricket::IceCandidatePairState::IN_PROGRESS: |
| 132 | return RTCStatsIceCandidatePairState::kInProgress; |
| 133 | case cricket::IceCandidatePairState::SUCCEEDED: |
| 134 | return RTCStatsIceCandidatePairState::kSucceeded; |
| 135 | case cricket::IceCandidatePairState::FAILED: |
| 136 | return RTCStatsIceCandidatePairState::kFailed; |
| 137 | default: |
| 138 | RTC_NOTREACHED(); |
| 139 | return nullptr; |
| 140 | } |
| 141 | } |
| 142 | |
hbos | 7064d59 | 2017-01-16 07:38:02 -0800 | [diff] [blame] | 143 | const char* DtlsTransportStateToRTCDtlsTransportState( |
| 144 | cricket::DtlsTransportState state) { |
| 145 | switch (state) { |
| 146 | case cricket::DTLS_TRANSPORT_NEW: |
| 147 | return RTCDtlsTransportState::kNew; |
| 148 | case cricket::DTLS_TRANSPORT_CONNECTING: |
| 149 | return RTCDtlsTransportState::kConnecting; |
| 150 | case cricket::DTLS_TRANSPORT_CONNECTED: |
| 151 | return RTCDtlsTransportState::kConnected; |
| 152 | case cricket::DTLS_TRANSPORT_CLOSED: |
| 153 | return RTCDtlsTransportState::kClosed; |
| 154 | case cricket::DTLS_TRANSPORT_FAILED: |
| 155 | return RTCDtlsTransportState::kFailed; |
| 156 | default: |
| 157 | RTC_NOTREACHED(); |
| 158 | return nullptr; |
| 159 | } |
| 160 | } |
| 161 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 162 | double DoubleAudioLevelFromIntAudioLevel(int audio_level) { |
| 163 | RTC_DCHECK_GE(audio_level, 0); |
| 164 | RTC_DCHECK_LE(audio_level, 32767); |
| 165 | return audio_level / 32767.0; |
| 166 | } |
| 167 | |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 168 | std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters( |
| 169 | uint64_t timestamp_us, bool inbound, bool audio, |
| 170 | const RtpCodecParameters& codec_params) { |
| 171 | RTC_DCHECK_GE(codec_params.payload_type, 0); |
| 172 | RTC_DCHECK_LE(codec_params.payload_type, 127); |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame^] | 173 | RTC_DCHECK(codec_params.clock_rate); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 174 | uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type); |
| 175 | std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats( |
| 176 | RTCCodecStatsIDFromDirectionMediaAndPayload(inbound, audio, payload_type), |
| 177 | timestamp_us)); |
| 178 | codec_stats->payload_type = payload_type; |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame^] | 179 | codec_stats->codec = codec_params.mime_type(); |
| 180 | if (codec_params.clock_rate) { |
| 181 | codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate); |
| 182 | } |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 183 | return codec_stats; |
| 184 | } |
| 185 | |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 186 | void SetMediaStreamTrackStatsFromMediaStreamTrackInterface( |
| 187 | const MediaStreamTrackInterface& track, |
| 188 | RTCMediaStreamTrackStats* track_stats) { |
| 189 | track_stats->track_identifier = track.id(); |
| 190 | track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded); |
| 191 | } |
| 192 | |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 193 | // Provides the media independent counters (both audio and video). |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 194 | void SetInboundRTPStreamStatsFromMediaReceiverInfo( |
| 195 | const cricket::MediaReceiverInfo& media_receiver_info, |
| 196 | RTCInboundRTPStreamStats* inbound_stats) { |
| 197 | RTC_DCHECK(inbound_stats); |
| 198 | inbound_stats->ssrc = rtc::ToString<>(media_receiver_info.ssrc()); |
| 199 | // TODO(hbos): Support the remote case. crbug.com/657855 |
| 200 | inbound_stats->is_remote = false; |
| 201 | // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant: |
| 202 | // |media_receiver_info.codec_name|. crbug.com/657854, 657855, 659117 |
| 203 | inbound_stats->packets_received = |
| 204 | static_cast<uint32_t>(media_receiver_info.packets_rcvd); |
| 205 | inbound_stats->bytes_received = |
| 206 | static_cast<uint64_t>(media_receiver_info.bytes_rcvd); |
hbos | 02cd4d6 | 2016-12-09 04:19:44 -0800 | [diff] [blame] | 207 | inbound_stats->packets_lost = |
| 208 | static_cast<uint32_t>(media_receiver_info.packets_lost); |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 209 | inbound_stats->fraction_lost = |
| 210 | static_cast<double>(media_receiver_info.fraction_lost); |
| 211 | } |
| 212 | |
| 213 | void SetInboundRTPStreamStatsFromVoiceReceiverInfo( |
| 214 | const cricket::VoiceReceiverInfo& voice_receiver_info, |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 215 | RTCInboundRTPStreamStats* inbound_audio) { |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 216 | SetInboundRTPStreamStatsFromMediaReceiverInfo( |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 217 | voice_receiver_info, inbound_audio); |
| 218 | inbound_audio->media_type = "audio"; |
| 219 | inbound_audio->jitter = |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 220 | static_cast<double>(voice_receiver_info.jitter_ms) / |
| 221 | rtc::kNumMillisecsPerSec; |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 222 | // |fir_count|, |pli_count| and |sli_count| are only valid for video and are |
| 223 | // purposefully left undefined for audio. |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void SetInboundRTPStreamStatsFromVideoReceiverInfo( |
| 227 | const cricket::VideoReceiverInfo& video_receiver_info, |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 228 | RTCInboundRTPStreamStats* inbound_video) { |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 229 | SetInboundRTPStreamStatsFromMediaReceiverInfo( |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 230 | video_receiver_info, inbound_video); |
| 231 | inbound_video->media_type = "video"; |
| 232 | inbound_video->fir_count = |
| 233 | static_cast<uint32_t>(video_receiver_info.firs_sent); |
| 234 | inbound_video->pli_count = |
| 235 | static_cast<uint32_t>(video_receiver_info.plis_sent); |
| 236 | inbound_video->nack_count = |
| 237 | static_cast<uint32_t>(video_receiver_info.nacks_sent); |
hbos | 6769c49 | 2017-01-02 08:35:13 -0800 | [diff] [blame] | 238 | inbound_video->frames_decoded = video_receiver_info.frames_decoded; |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 239 | } |
| 240 | |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 241 | // Provides the media independent counters (both audio and video). |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 242 | void SetOutboundRTPStreamStatsFromMediaSenderInfo( |
| 243 | const cricket::MediaSenderInfo& media_sender_info, |
| 244 | RTCOutboundRTPStreamStats* outbound_stats) { |
| 245 | RTC_DCHECK(outbound_stats); |
| 246 | outbound_stats->ssrc = rtc::ToString<>(media_sender_info.ssrc()); |
| 247 | // TODO(hbos): Support the remote case. crbug.com/657856 |
| 248 | outbound_stats->is_remote = false; |
| 249 | // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant: |
| 250 | // |media_sender_info.codec_name|. crbug.com/657854, 657856, 659117 |
| 251 | outbound_stats->packets_sent = |
| 252 | static_cast<uint32_t>(media_sender_info.packets_sent); |
| 253 | outbound_stats->bytes_sent = |
| 254 | static_cast<uint64_t>(media_sender_info.bytes_sent); |
hbos | e10e6d1 | 2016-12-15 01:54:29 -0800 | [diff] [blame] | 255 | if (media_sender_info.rtt_ms >= 0) { |
| 256 | outbound_stats->round_trip_time = static_cast<double>( |
| 257 | media_sender_info.rtt_ms) / rtc::kNumMillisecsPerSec; |
| 258 | } |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void SetOutboundRTPStreamStatsFromVoiceSenderInfo( |
| 262 | const cricket::VoiceSenderInfo& voice_sender_info, |
| 263 | RTCOutboundRTPStreamStats* outbound_audio) { |
| 264 | SetOutboundRTPStreamStatsFromMediaSenderInfo( |
| 265 | voice_sender_info, outbound_audio); |
| 266 | outbound_audio->media_type = "audio"; |
| 267 | // |fir_count|, |pli_count| and |sli_count| are only valid for video and are |
| 268 | // purposefully left undefined for audio. |
| 269 | } |
| 270 | |
| 271 | void SetOutboundRTPStreamStatsFromVideoSenderInfo( |
| 272 | const cricket::VideoSenderInfo& video_sender_info, |
| 273 | RTCOutboundRTPStreamStats* outbound_video) { |
| 274 | SetOutboundRTPStreamStatsFromMediaSenderInfo( |
| 275 | video_sender_info, outbound_video); |
| 276 | outbound_video->media_type = "video"; |
| 277 | outbound_video->fir_count = |
| 278 | static_cast<uint32_t>(video_sender_info.firs_rcvd); |
| 279 | outbound_video->pli_count = |
| 280 | static_cast<uint32_t>(video_sender_info.plis_rcvd); |
| 281 | outbound_video->nack_count = |
| 282 | static_cast<uint32_t>(video_sender_info.nacks_rcvd); |
hbos | 6769c49 | 2017-01-02 08:35:13 -0800 | [diff] [blame] | 283 | if (video_sender_info.qp_sum) |
| 284 | outbound_video->qp_sum = *video_sender_info.qp_sum; |
| 285 | outbound_video->frames_encoded = video_sender_info.frames_encoded; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 286 | } |
| 287 | |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 288 | void ProduceCertificateStatsFromSSLCertificateStats( |
| 289 | int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats, |
| 290 | RTCStatsReport* report) { |
| 291 | RTCCertificateStats* prev_certificate_stats = nullptr; |
| 292 | for (const rtc::SSLCertificateStats* s = &certificate_stats; s; |
| 293 | s = s->issuer.get()) { |
hbos | 02d2a92 | 2016-12-21 01:29:05 -0800 | [diff] [blame] | 294 | std::string certificate_stats_id = |
| 295 | RTCCertificateIDFromFingerprint(s->fingerprint); |
| 296 | // It is possible for the same certificate to show up multiple times, e.g. |
| 297 | // if local and remote side use the same certificate in a loopback call. |
| 298 | // If the report already contains stats for this certificate, skip it. |
| 299 | if (report->Get(certificate_stats_id)) { |
| 300 | RTC_DCHECK_EQ(s, &certificate_stats); |
| 301 | break; |
| 302 | } |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 303 | RTCCertificateStats* certificate_stats = new RTCCertificateStats( |
hbos | 02d2a92 | 2016-12-21 01:29:05 -0800 | [diff] [blame] | 304 | certificate_stats_id, timestamp_us); |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 305 | certificate_stats->fingerprint = s->fingerprint; |
| 306 | certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm; |
| 307 | certificate_stats->base64_certificate = s->base64_certificate; |
| 308 | if (prev_certificate_stats) |
| 309 | prev_certificate_stats->issuer_certificate_id = certificate_stats->id(); |
| 310 | report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats)); |
| 311 | prev_certificate_stats = certificate_stats; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | const std::string& ProduceIceCandidateStats( |
| 316 | int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local, |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 317 | const std::string& transport_id, RTCStatsReport* report) { |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 318 | const std::string& id = "RTCIceCandidate_" + candidate.id(); |
| 319 | const RTCStats* stats = report->Get(id); |
| 320 | if (!stats) { |
| 321 | std::unique_ptr<RTCIceCandidateStats> candidate_stats; |
| 322 | if (is_local) |
| 323 | candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us)); |
| 324 | else |
| 325 | candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us)); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 326 | candidate_stats->transport_id = transport_id; |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 327 | candidate_stats->ip = candidate.address().ipaddr().ToString(); |
| 328 | candidate_stats->port = static_cast<int32_t>(candidate.address().port()); |
| 329 | candidate_stats->protocol = candidate.protocol(); |
| 330 | candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType( |
| 331 | candidate.type()); |
| 332 | candidate_stats->priority = static_cast<int32_t>(candidate.priority()); |
| 333 | |
| 334 | stats = candidate_stats.get(); |
| 335 | report->AddStats(std::move(candidate_stats)); |
| 336 | } |
| 337 | RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType |
| 338 | : RTCRemoteIceCandidateStats::kType); |
| 339 | return stats->id(); |
| 340 | } |
| 341 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 342 | std::unique_ptr<RTCMediaStreamTrackStats> |
| 343 | ProduceMediaStreamTrackStatsFromVoiceSenderInfo( |
| 344 | int64_t timestamp_us, |
| 345 | const AudioTrackInterface& audio_track, |
| 346 | const cricket::VoiceSenderInfo& voice_sender_info) { |
| 347 | std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats( |
| 348 | new RTCMediaStreamTrackStats( |
| 349 | RTCMediaStreamTrackStatsIDFromTrackKindIDAndSsrc( |
| 350 | true, MediaStreamTrackInterface::kAudioKind, audio_track.id(), |
| 351 | voice_sender_info.ssrc()), |
| 352 | timestamp_us, |
| 353 | RTCMediaStreamTrackKind::kAudio)); |
| 354 | SetMediaStreamTrackStatsFromMediaStreamTrackInterface( |
| 355 | audio_track, audio_track_stats.get()); |
| 356 | audio_track_stats->remote_source = false; |
| 357 | audio_track_stats->detached = false; |
| 358 | if (voice_sender_info.audio_level >= 0) { |
| 359 | audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel( |
| 360 | voice_sender_info.audio_level); |
| 361 | } |
| 362 | if (voice_sender_info.echo_return_loss != -100) { |
| 363 | audio_track_stats->echo_return_loss = static_cast<double>( |
| 364 | voice_sender_info.echo_return_loss); |
| 365 | } |
| 366 | if (voice_sender_info.echo_return_loss_enhancement != -100) { |
| 367 | audio_track_stats->echo_return_loss_enhancement = static_cast<double>( |
| 368 | voice_sender_info.echo_return_loss_enhancement); |
| 369 | } |
| 370 | return audio_track_stats; |
| 371 | } |
| 372 | |
| 373 | std::unique_ptr<RTCMediaStreamTrackStats> |
| 374 | ProduceMediaStreamTrackStatsFromVoiceReceiverInfo( |
| 375 | int64_t timestamp_us, |
| 376 | const AudioTrackInterface& audio_track, |
| 377 | const cricket::VoiceReceiverInfo& voice_receiver_info) { |
| 378 | std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats( |
| 379 | new RTCMediaStreamTrackStats( |
| 380 | RTCMediaStreamTrackStatsIDFromTrackKindIDAndSsrc( |
| 381 | false, MediaStreamTrackInterface::kAudioKind, audio_track.id(), |
| 382 | voice_receiver_info.ssrc()), |
| 383 | timestamp_us, |
| 384 | RTCMediaStreamTrackKind::kAudio)); |
| 385 | SetMediaStreamTrackStatsFromMediaStreamTrackInterface( |
| 386 | audio_track, audio_track_stats.get()); |
| 387 | audio_track_stats->remote_source = true; |
| 388 | audio_track_stats->detached = false; |
| 389 | if (voice_receiver_info.audio_level >= 0) { |
| 390 | audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel( |
| 391 | voice_receiver_info.audio_level); |
| 392 | } |
| 393 | return audio_track_stats; |
| 394 | } |
| 395 | |
| 396 | std::unique_ptr<RTCMediaStreamTrackStats> |
| 397 | ProduceMediaStreamTrackStatsFromVideoSenderInfo( |
| 398 | int64_t timestamp_us, |
| 399 | const VideoTrackInterface& video_track, |
| 400 | const cricket::VideoSenderInfo& video_sender_info) { |
| 401 | std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats( |
| 402 | new RTCMediaStreamTrackStats( |
| 403 | RTCMediaStreamTrackStatsIDFromTrackKindIDAndSsrc( |
| 404 | true, MediaStreamTrackInterface::kVideoKind, video_track.id(), |
| 405 | video_sender_info.ssrc()), |
| 406 | timestamp_us, |
| 407 | RTCMediaStreamTrackKind::kVideo)); |
| 408 | SetMediaStreamTrackStatsFromMediaStreamTrackInterface( |
| 409 | video_track, video_track_stats.get()); |
| 410 | video_track_stats->remote_source = false; |
| 411 | video_track_stats->detached = false; |
| 412 | video_track_stats->frame_width = static_cast<uint32_t>( |
| 413 | video_sender_info.send_frame_width); |
| 414 | video_track_stats->frame_height = static_cast<uint32_t>( |
| 415 | video_sender_info.send_frame_height); |
hbos | fefe076 | 2017-01-20 06:14:25 -0800 | [diff] [blame] | 416 | // TODO(hbos): Will reduce this by frames dropped due to congestion control |
| 417 | // when available. crbug.com/659137 |
| 418 | video_track_stats->frames_sent = video_sender_info.frames_encoded; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 419 | return video_track_stats; |
| 420 | } |
| 421 | |
| 422 | std::unique_ptr<RTCMediaStreamTrackStats> |
| 423 | ProduceMediaStreamTrackStatsFromVideoReceiverInfo( |
| 424 | int64_t timestamp_us, |
| 425 | const VideoTrackInterface& video_track, |
| 426 | const cricket::VideoReceiverInfo& video_receiver_info) { |
| 427 | std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats( |
| 428 | new RTCMediaStreamTrackStats( |
| 429 | RTCMediaStreamTrackStatsIDFromTrackKindIDAndSsrc( |
| 430 | false, MediaStreamTrackInterface::kVideoKind, video_track.id(), |
| 431 | video_receiver_info.ssrc()), |
| 432 | timestamp_us, |
| 433 | RTCMediaStreamTrackKind::kVideo)); |
| 434 | SetMediaStreamTrackStatsFromMediaStreamTrackInterface( |
| 435 | video_track, video_track_stats.get()); |
| 436 | video_track_stats->remote_source = true; |
| 437 | video_track_stats->detached = false; |
| 438 | if (video_receiver_info.frame_width > 0 && |
| 439 | video_receiver_info.frame_height > 0) { |
| 440 | video_track_stats->frame_width = static_cast<uint32_t>( |
| 441 | video_receiver_info.frame_width); |
| 442 | video_track_stats->frame_height = static_cast<uint32_t>( |
| 443 | video_receiver_info.frame_height); |
| 444 | } |
hbos | 42f6d2f | 2017-01-20 03:56:50 -0800 | [diff] [blame] | 445 | video_track_stats->frames_received = video_receiver_info.frames_received; |
hbos | f64941f | 2017-01-20 07:39:09 -0800 | [diff] [blame] | 446 | // TODO(hbos): When we support receiving simulcast, this should be the total |
| 447 | // number of frames correctly decoded, independent of which SSRC it was |
| 448 | // received from. Since we don't support that, this is correct and is the same |
| 449 | // value as "RTCInboundRTPStreamStats.framesDecoded". crbug.com/659137 |
| 450 | video_track_stats->frames_decoded = video_receiver_info.frames_decoded; |
hbos | 50cfe1f | 2017-01-23 07:21:55 -0800 | [diff] [blame] | 451 | RTC_DCHECK_GE(video_receiver_info.frames_received, |
| 452 | video_receiver_info.frames_rendered); |
| 453 | video_track_stats->frames_dropped = video_receiver_info.frames_received - |
| 454 | video_receiver_info.frames_rendered; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 455 | return video_track_stats; |
| 456 | } |
| 457 | |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 458 | void ProduceMediaStreamAndTrackStats( |
| 459 | int64_t timestamp_us, |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 460 | const TrackMediaInfoMap& track_media_info_map, |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 461 | rtc::scoped_refptr<StreamCollectionInterface> streams, |
| 462 | bool is_local, |
| 463 | RTCStatsReport* report) { |
| 464 | // TODO(hbos): When "AddTrack" is implemented we should iterate tracks to |
| 465 | // find which streams exist, not iterate streams to find tracks. |
| 466 | // crbug.com/659137 |
| 467 | // TODO(hbos): Return stats of detached tracks. We have to perform stats |
| 468 | // gathering at the time of detachment to get accurate stats and timestamps. |
| 469 | // crbug.com/659137 |
| 470 | if (!streams) |
| 471 | return; |
| 472 | for (size_t i = 0; i < streams->count(); ++i) { |
| 473 | MediaStreamInterface* stream = streams->at(i); |
| 474 | |
| 475 | std::unique_ptr<RTCMediaStreamStats> stream_stats( |
hbos | 02d2a92 | 2016-12-21 01:29:05 -0800 | [diff] [blame] | 476 | new RTCMediaStreamStats( |
| 477 | (is_local ? "RTCMediaStream_local_" : "RTCMediaStream_remote_") + |
| 478 | stream->label(), timestamp_us)); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 479 | stream_stats->stream_identifier = stream->label(); |
| 480 | stream_stats->track_ids = std::vector<std::string>(); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 481 | // The track stats are per-attachment to the connection. There can be one |
| 482 | // for receiving (remote) tracks and multiple attachments for sending |
| 483 | // (local) tracks. |
| 484 | if (is_local) { |
| 485 | // Local Audio Tracks |
| 486 | for (const rtc::scoped_refptr<AudioTrackInterface>& audio_track : |
| 487 | stream->GetAudioTracks()) { |
| 488 | const std::vector<cricket::VoiceSenderInfo*>* voice_sender_infos = |
| 489 | track_media_info_map.GetVoiceSenderInfos(*audio_track); |
| 490 | if (!voice_sender_infos) { |
| 491 | continue; |
hbos | 9a394f0 | 2016-12-14 07:58:22 -0800 | [diff] [blame] | 492 | } |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 493 | for (const auto& voice_sender_info : *voice_sender_infos) { |
| 494 | std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats = |
| 495 | ProduceMediaStreamTrackStatsFromVoiceSenderInfo( |
| 496 | timestamp_us, *audio_track, *voice_sender_info); |
| 497 | stream_stats->track_ids->push_back(audio_track_stats->id()); |
| 498 | report->AddStats(std::move(audio_track_stats)); |
hbos | 9a394f0 | 2016-12-14 07:58:22 -0800 | [diff] [blame] | 499 | } |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 500 | } |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 501 | // Local Video Tracks |
| 502 | for (const rtc::scoped_refptr<VideoTrackInterface>& video_track : |
| 503 | stream->GetVideoTracks()) { |
| 504 | const std::vector<cricket::VideoSenderInfo*>* video_sender_infos = |
| 505 | track_media_info_map.GetVideoSenderInfos(*video_track); |
| 506 | if (!video_sender_infos) { |
| 507 | continue; |
| 508 | } |
| 509 | for (const auto& video_sender_info : *video_sender_infos) { |
| 510 | std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats = |
| 511 | ProduceMediaStreamTrackStatsFromVideoSenderInfo( |
| 512 | timestamp_us, *video_track, *video_sender_info); |
| 513 | stream_stats->track_ids->push_back(video_track_stats->id()); |
| 514 | report->AddStats(std::move(video_track_stats)); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 515 | } |
| 516 | } |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 517 | } else { |
| 518 | // Remote Audio Tracks |
| 519 | for (const rtc::scoped_refptr<AudioTrackInterface>& audio_track : |
| 520 | stream->GetAudioTracks()) { |
| 521 | const cricket::VoiceReceiverInfo* voice_receiver_info = |
| 522 | track_media_info_map.GetVoiceReceiverInfo(*audio_track); |
| 523 | if (!voice_receiver_info) { |
| 524 | continue; |
| 525 | } |
| 526 | std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats = |
| 527 | ProduceMediaStreamTrackStatsFromVoiceReceiverInfo( |
| 528 | timestamp_us, *audio_track, *voice_receiver_info); |
| 529 | stream_stats->track_ids->push_back(audio_track_stats->id()); |
| 530 | report->AddStats(std::move(audio_track_stats)); |
| 531 | } |
| 532 | // Remote Video Tracks |
| 533 | for (const rtc::scoped_refptr<VideoTrackInterface>& video_track : |
| 534 | stream->GetVideoTracks()) { |
| 535 | const cricket::VideoReceiverInfo* video_receiver_info = |
| 536 | track_media_info_map.GetVideoReceiverInfo(*video_track); |
| 537 | if (!video_receiver_info) { |
| 538 | continue; |
| 539 | } |
| 540 | std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats = |
| 541 | ProduceMediaStreamTrackStatsFromVideoReceiverInfo( |
| 542 | timestamp_us, *video_track, *video_receiver_info); |
| 543 | stream_stats->track_ids->push_back(video_track_stats->id()); |
| 544 | report->AddStats(std::move(video_track_stats)); |
| 545 | } |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 546 | } |
| 547 | report->AddStats(std::move(stream_stats)); |
| 548 | } |
| 549 | } |
| 550 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 551 | } // namespace |
| 552 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 553 | rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create( |
| 554 | PeerConnection* pc, int64_t cache_lifetime_us) { |
| 555 | return rtc::scoped_refptr<RTCStatsCollector>( |
| 556 | new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us)); |
| 557 | } |
| 558 | |
| 559 | RTCStatsCollector::RTCStatsCollector(PeerConnection* pc, |
| 560 | int64_t cache_lifetime_us) |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 561 | : pc_(pc), |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 562 | signaling_thread_(pc->session()->signaling_thread()), |
| 563 | worker_thread_(pc->session()->worker_thread()), |
| 564 | network_thread_(pc->session()->network_thread()), |
| 565 | num_pending_partial_reports_(0), |
| 566 | partial_report_timestamp_us_(0), |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 567 | cache_timestamp_us_(0), |
| 568 | cache_lifetime_us_(cache_lifetime_us) { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 569 | RTC_DCHECK(pc_); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 570 | RTC_DCHECK(signaling_thread_); |
| 571 | RTC_DCHECK(worker_thread_); |
| 572 | RTC_DCHECK(network_thread_); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 573 | RTC_DCHECK_GE(cache_lifetime_us_, 0); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 574 | pc_->SignalDataChannelCreated.connect( |
| 575 | this, &RTCStatsCollector::OnDataChannelCreated); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 576 | } |
| 577 | |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 578 | RTCStatsCollector::~RTCStatsCollector() { |
| 579 | RTC_DCHECK_EQ(num_pending_partial_reports_, 0); |
| 580 | } |
| 581 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 582 | void RTCStatsCollector::GetStatsReport( |
| 583 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 584 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 585 | RTC_DCHECK(callback); |
| 586 | callbacks_.push_back(callback); |
| 587 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 588 | // "Now" using a monotonically increasing timer. |
| 589 | int64_t cache_now_us = rtc::TimeMicros(); |
| 590 | if (cached_report_ && |
| 591 | cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 592 | // We have a fresh cached report to deliver. |
| 593 | DeliverCachedReport(); |
| 594 | } else if (!num_pending_partial_reports_) { |
| 595 | // Only start gathering stats if we're not already gathering stats. In the |
| 596 | // case of already gathering stats, |callback_| will be invoked when there |
| 597 | // are no more pending partial reports. |
| 598 | |
| 599 | // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970, |
| 600 | // UTC), in microseconds. The system clock could be modified and is not |
| 601 | // necessarily monotonically increasing. |
nisse | cdf37a9 | 2016-09-13 23:41:47 -0700 | [diff] [blame] | 602 | int64_t timestamp_us = rtc::TimeUTCMicros(); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 603 | |
hbos | f415f8a | 2017-01-02 04:28:51 -0800 | [diff] [blame] | 604 | num_pending_partial_reports_ = 2; |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 605 | partial_report_timestamp_us_ = cache_now_us; |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 606 | |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 607 | // Prepare |channel_name_pairs_| for use in |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 608 | // |ProducePartialResultsOnNetworkThread|. |
| 609 | channel_name_pairs_.reset(new ChannelNamePairs()); |
| 610 | if (pc_->session()->voice_channel()) { |
| 611 | channel_name_pairs_->voice = rtc::Optional<ChannelNamePair>( |
| 612 | ChannelNamePair(pc_->session()->voice_channel()->content_name(), |
| 613 | pc_->session()->voice_channel()->transport_name())); |
| 614 | } |
| 615 | if (pc_->session()->video_channel()) { |
| 616 | channel_name_pairs_->video = rtc::Optional<ChannelNamePair>( |
| 617 | ChannelNamePair(pc_->session()->video_channel()->content_name(), |
| 618 | pc_->session()->video_channel()->transport_name())); |
| 619 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 620 | if (pc_->session()->rtp_data_channel()) { |
| 621 | channel_name_pairs_->data = |
| 622 | rtc::Optional<ChannelNamePair>(ChannelNamePair( |
| 623 | pc_->session()->rtp_data_channel()->content_name(), |
| 624 | pc_->session()->rtp_data_channel()->transport_name())); |
| 625 | } |
| 626 | if (pc_->session()->sctp_content_name()) { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 627 | channel_name_pairs_->data = rtc::Optional<ChannelNamePair>( |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 628 | ChannelNamePair(*pc_->session()->sctp_content_name(), |
| 629 | *pc_->session()->sctp_transport_name())); |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 630 | } |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 631 | // Prepare |track_media_info_map_| for use in |
| 632 | // |ProducePartialResultsOnNetworkThread| and |
| 633 | // |ProducePartialResultsOnSignalingThread|. |
| 634 | track_media_info_map_.reset(PrepareTrackMediaInfoMap_s().release()); |
| 635 | // Prepare |track_to_id_| for use in |ProducePartialResultsOnNetworkThread|. |
| 636 | // This avoids a possible deadlock if |MediaStreamTrackInterface::id| is |
| 637 | // implemented to invoke on the signaling thread. |
| 638 | track_to_id_ = PrepareTrackToID_s(); |
hbos | f415f8a | 2017-01-02 04:28:51 -0800 | [diff] [blame] | 639 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 640 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_, |
| 641 | rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread, |
| 642 | rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us)); |
hbos | f415f8a | 2017-01-02 04:28:51 -0800 | [diff] [blame] | 643 | ProducePartialResultsOnSignalingThread(timestamp_us); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 644 | } |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void RTCStatsCollector::ClearCachedStatsReport() { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 648 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 649 | cached_report_ = nullptr; |
| 650 | } |
| 651 | |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 652 | void RTCStatsCollector::WaitForPendingRequest() { |
| 653 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 654 | if (num_pending_partial_reports_) { |
| 655 | rtc::Thread::Current()->ProcessMessages(0); |
| 656 | while (num_pending_partial_reports_) { |
| 657 | rtc::Thread::Current()->SleepMs(1); |
| 658 | rtc::Thread::Current()->ProcessMessages(0); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 663 | void RTCStatsCollector::ProducePartialResultsOnSignalingThread( |
| 664 | int64_t timestamp_us) { |
| 665 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 666 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create( |
| 667 | timestamp_us); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 668 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 669 | ProduceDataChannelStats_s(timestamp_us, report.get()); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 670 | ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get()); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 671 | ProducePeerConnectionStats_s(timestamp_us, report.get()); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 672 | |
| 673 | AddPartialResults(report); |
| 674 | } |
| 675 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 676 | void RTCStatsCollector::ProducePartialResultsOnNetworkThread( |
| 677 | int64_t timestamp_us) { |
| 678 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 679 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create( |
| 680 | timestamp_us); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 681 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 682 | std::unique_ptr<SessionStats> session_stats = |
| 683 | pc_->session()->GetStats(*channel_name_pairs_); |
| 684 | if (session_stats) { |
| 685 | std::map<std::string, CertificateStatsPair> transport_cert_stats = |
| 686 | PrepareTransportCertificateStats_n(*session_stats); |
| 687 | |
| 688 | ProduceCertificateStats_n( |
| 689 | timestamp_us, transport_cert_stats, report.get()); |
| 690 | ProduceCodecStats_n( |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 691 | timestamp_us, *track_media_info_map_, report.get()); |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 692 | ProduceIceCandidateAndPairStats_n( |
| 693 | timestamp_us, *session_stats, report.get()); |
| 694 | ProduceRTPStreamStats_n( |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 695 | timestamp_us, *session_stats, *track_media_info_map_, report.get()); |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 696 | ProduceTransportStats_n( |
| 697 | timestamp_us, *session_stats, transport_cert_stats, report.get()); |
| 698 | } |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 699 | |
| 700 | AddPartialResults(report); |
| 701 | } |
| 702 | |
| 703 | void RTCStatsCollector::AddPartialResults( |
| 704 | const rtc::scoped_refptr<RTCStatsReport>& partial_report) { |
| 705 | if (!signaling_thread_->IsCurrent()) { |
| 706 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_, |
| 707 | rtc::Bind(&RTCStatsCollector::AddPartialResults_s, |
| 708 | rtc::scoped_refptr<RTCStatsCollector>(this), |
| 709 | partial_report)); |
| 710 | return; |
| 711 | } |
| 712 | AddPartialResults_s(partial_report); |
| 713 | } |
| 714 | |
| 715 | void RTCStatsCollector::AddPartialResults_s( |
| 716 | rtc::scoped_refptr<RTCStatsReport> partial_report) { |
| 717 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 718 | RTC_DCHECK_GT(num_pending_partial_reports_, 0); |
| 719 | if (!partial_report_) |
| 720 | partial_report_ = partial_report; |
| 721 | else |
| 722 | partial_report_->TakeMembersFrom(partial_report); |
| 723 | --num_pending_partial_reports_; |
| 724 | if (!num_pending_partial_reports_) { |
| 725 | cache_timestamp_us_ = partial_report_timestamp_us_; |
| 726 | cached_report_ = partial_report_; |
| 727 | partial_report_ = nullptr; |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 728 | channel_name_pairs_.reset(); |
| 729 | track_media_info_map_.reset(); |
| 730 | track_to_id_.clear(); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 731 | DeliverCachedReport(); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | void RTCStatsCollector::DeliverCachedReport() { |
| 736 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 737 | RTC_DCHECK(!callbacks_.empty()); |
| 738 | RTC_DCHECK(cached_report_); |
| 739 | for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback : |
| 740 | callbacks_) { |
| 741 | callback->OnStatsDelivered(cached_report_); |
| 742 | } |
| 743 | callbacks_.clear(); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 744 | } |
| 745 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 746 | void RTCStatsCollector::ProduceCertificateStats_n( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 747 | int64_t timestamp_us, |
| 748 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 749 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 750 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 751 | for (const auto& transport_cert_stats_pair : transport_cert_stats) { |
| 752 | if (transport_cert_stats_pair.second.local) { |
| 753 | ProduceCertificateStatsFromSSLCertificateStats( |
| 754 | timestamp_us, *transport_cert_stats_pair.second.local.get(), report); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 755 | } |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 756 | if (transport_cert_stats_pair.second.remote) { |
| 757 | ProduceCertificateStatsFromSSLCertificateStats( |
| 758 | timestamp_us, *transport_cert_stats_pair.second.remote.get(), report); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 763 | void RTCStatsCollector::ProduceCodecStats_n( |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 764 | int64_t timestamp_us, const TrackMediaInfoMap& track_media_info_map, |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 765 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 766 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 767 | // Audio |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 768 | if (track_media_info_map.voice_media_info()) { |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 769 | // Inbound |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 770 | for (const auto& pair : |
| 771 | track_media_info_map.voice_media_info()->receive_codecs) { |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 772 | report->AddStats(CodecStatsFromRtpCodecParameters( |
| 773 | timestamp_us, true, true, pair.second)); |
| 774 | } |
| 775 | // Outbound |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 776 | for (const auto& pair : |
| 777 | track_media_info_map.voice_media_info()->send_codecs) { |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 778 | report->AddStats(CodecStatsFromRtpCodecParameters( |
| 779 | timestamp_us, false, true, pair.second)); |
| 780 | } |
| 781 | } |
| 782 | // Video |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 783 | if (track_media_info_map.video_media_info()) { |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 784 | // Inbound |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 785 | for (const auto& pair : |
| 786 | track_media_info_map.video_media_info()->receive_codecs) { |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 787 | report->AddStats(CodecStatsFromRtpCodecParameters( |
| 788 | timestamp_us, true, false, pair.second)); |
| 789 | } |
| 790 | // Outbound |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 791 | for (const auto& pair : |
| 792 | track_media_info_map.video_media_info()->send_codecs) { |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 793 | report->AddStats(CodecStatsFromRtpCodecParameters( |
| 794 | timestamp_us, false, false, pair.second)); |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 799 | void RTCStatsCollector::ProduceDataChannelStats_s( |
| 800 | int64_t timestamp_us, RTCStatsReport* report) const { |
| 801 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 802 | for (const rtc::scoped_refptr<DataChannel>& data_channel : |
| 803 | pc_->sctp_data_channels()) { |
| 804 | std::unique_ptr<RTCDataChannelStats> data_channel_stats( |
| 805 | new RTCDataChannelStats( |
| 806 | "RTCDataChannel_" + rtc::ToString<>(data_channel->id()), |
| 807 | timestamp_us)); |
| 808 | data_channel_stats->label = data_channel->label(); |
| 809 | data_channel_stats->protocol = data_channel->protocol(); |
| 810 | data_channel_stats->datachannelid = data_channel->id(); |
| 811 | data_channel_stats->state = |
| 812 | DataStateToRTCDataChannelState(data_channel->state()); |
| 813 | data_channel_stats->messages_sent = data_channel->messages_sent(); |
| 814 | data_channel_stats->bytes_sent = data_channel->bytes_sent(); |
| 815 | data_channel_stats->messages_received = data_channel->messages_received(); |
| 816 | data_channel_stats->bytes_received = data_channel->bytes_received(); |
| 817 | report->AddStats(std::move(data_channel_stats)); |
| 818 | } |
| 819 | } |
| 820 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 821 | void RTCStatsCollector::ProduceIceCandidateAndPairStats_n( |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 822 | int64_t timestamp_us, const SessionStats& session_stats, |
| 823 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 824 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 825 | for (const auto& transport_stats : session_stats.transport_stats) { |
| 826 | for (const auto& channel_stats : transport_stats.second.channel_stats) { |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 827 | std::string transport_id = RTCTransportStatsIDFromTransportChannel( |
| 828 | transport_stats.second.transport_name, channel_stats.component); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 829 | for (const cricket::ConnectionInfo& info : |
| 830 | channel_stats.connection_infos) { |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 831 | std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 832 | new RTCIceCandidatePairStats( |
| 833 | RTCIceCandidatePairStatsIDFromConnectionInfo(info), |
| 834 | timestamp_us)); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 835 | |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 836 | candidate_pair_stats->transport_id = transport_id; |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 837 | // TODO(hbos): There could be other candidates that are not paired with |
| 838 | // anything. We don't have a complete list. Local candidates come from |
| 839 | // Port objects, and prflx candidates (both local and remote) are only |
| 840 | // stored in candidate pairs. crbug.com/632723 |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 841 | candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats( |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 842 | timestamp_us, info.local_candidate, true, transport_id, report); |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 843 | candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats( |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 844 | timestamp_us, info.remote_candidate, false, transport_id, report); |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 845 | candidate_pair_stats->state = |
| 846 | IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state); |
| 847 | candidate_pair_stats->priority = info.priority; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 848 | // TODO(hbos): This writable is different than the spec. It goes to |
| 849 | // false after a certain amount of time without a response passes. |
| 850 | // crbug.com/633550 |
| 851 | candidate_pair_stats->writable = info.writable; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 852 | candidate_pair_stats->bytes_sent = |
| 853 | static_cast<uint64_t>(info.sent_total_bytes); |
| 854 | candidate_pair_stats->bytes_received = |
| 855 | static_cast<uint64_t>(info.recv_total_bytes); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 856 | // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be |
| 857 | // smoothed according to the spec. crbug.com/633550. See |
| 858 | // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt |
hbos | 3168c7a | 2016-12-15 06:17:08 -0800 | [diff] [blame] | 859 | candidate_pair_stats->current_round_trip_time = |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 860 | static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec; |
hbos | d82f512 | 2016-12-09 04:12:39 -0800 | [diff] [blame] | 861 | candidate_pair_stats->requests_received = |
| 862 | static_cast<uint64_t>(info.recv_ping_requests); |
hbos | e448dd5 | 2016-12-12 01:22:53 -0800 | [diff] [blame] | 863 | candidate_pair_stats->requests_sent = static_cast<uint64_t>( |
| 864 | info.sent_ping_requests_before_first_response); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 865 | candidate_pair_stats->responses_received = |
| 866 | static_cast<uint64_t>(info.recv_ping_responses); |
| 867 | candidate_pair_stats->responses_sent = |
| 868 | static_cast<uint64_t>(info.sent_ping_responses); |
hbos | e448dd5 | 2016-12-12 01:22:53 -0800 | [diff] [blame] | 869 | RTC_DCHECK_GE(info.sent_ping_requests_total, |
| 870 | info.sent_ping_requests_before_first_response); |
| 871 | candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>( |
| 872 | info.sent_ping_requests_total - |
| 873 | info.sent_ping_requests_before_first_response); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 874 | |
| 875 | report->AddStats(std::move(candidate_pair_stats)); |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 881 | void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s( |
| 882 | int64_t timestamp_us, RTCStatsReport* report) const { |
| 883 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 884 | RTC_DCHECK(track_media_info_map_); |
| 885 | ProduceMediaStreamAndTrackStats(timestamp_us, |
| 886 | *track_media_info_map_, |
| 887 | pc_->local_streams(), |
| 888 | true, |
| 889 | report); |
| 890 | ProduceMediaStreamAndTrackStats(timestamp_us, |
| 891 | *track_media_info_map_, |
| 892 | pc_->remote_streams(), |
| 893 | false, |
| 894 | report); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 895 | } |
| 896 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 897 | void RTCStatsCollector::ProducePeerConnectionStats_s( |
| 898 | int64_t timestamp_us, RTCStatsReport* report) const { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 899 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 900 | std::unique_ptr<RTCPeerConnectionStats> stats( |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 901 | new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us)); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 902 | stats->data_channels_opened = internal_record_.data_channels_opened; |
| 903 | stats->data_channels_closed = internal_record_.data_channels_closed; |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 904 | report->AddStats(std::move(stats)); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 905 | } |
| 906 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 907 | void RTCStatsCollector::ProduceRTPStreamStats_n( |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 908 | int64_t timestamp_us, const SessionStats& session_stats, |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 909 | const TrackMediaInfoMap& track_media_info_map, |
| 910 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 911 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 912 | |
| 913 | // Audio |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 914 | if (track_media_info_map.voice_media_info()) { |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 915 | std::string transport_id = RTCTransportStatsIDFromBaseChannel( |
| 916 | session_stats.proxy_to_transport, *pc_->session()->voice_channel()); |
| 917 | RTC_DCHECK(!transport_id.empty()); |
| 918 | // Inbound |
| 919 | for (const cricket::VoiceReceiverInfo& voice_receiver_info : |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 920 | track_media_info_map.voice_media_info()->receivers) { |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 921 | // TODO(nisse): SSRC == 0 currently means none. Delete check when that |
| 922 | // is fixed. |
| 923 | if (voice_receiver_info.ssrc() == 0) |
| 924 | continue; |
| 925 | std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio( |
| 926 | new RTCInboundRTPStreamStats( |
| 927 | RTCInboundRTPStreamStatsIDFromSSRC( |
| 928 | true, voice_receiver_info.ssrc()), |
| 929 | timestamp_us)); |
| 930 | SetInboundRTPStreamStatsFromVoiceReceiverInfo( |
| 931 | voice_receiver_info, inbound_audio.get()); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 932 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
| 933 | track_media_info_map_->GetAudioTrack(voice_receiver_info); |
| 934 | if (audio_track) { |
| 935 | RTC_DCHECK(track_to_id_.find(audio_track.get()) != track_to_id_.end()); |
hbos | b0ae920 | 2017-01-27 06:35:16 -0800 | [diff] [blame] | 936 | inbound_audio->track_id = |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 937 | RTCMediaStreamTrackStatsIDFromTrackKindIDAndSsrc( |
| 938 | false, |
| 939 | MediaStreamTrackInterface::kAudioKind, |
| 940 | track_to_id_.find(audio_track.get())->second, |
| 941 | voice_receiver_info.ssrc()); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 942 | } |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 943 | inbound_audio->transport_id = transport_id; |
| 944 | if (voice_receiver_info.codec_payload_type) { |
| 945 | inbound_audio->codec_id = |
| 946 | RTCCodecStatsIDFromDirectionMediaAndPayload( |
| 947 | true, true, *voice_receiver_info.codec_payload_type); |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 948 | } |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 949 | report->AddStats(std::move(inbound_audio)); |
| 950 | } |
| 951 | // Outbound |
| 952 | for (const cricket::VoiceSenderInfo& voice_sender_info : |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 953 | track_media_info_map.voice_media_info()->senders) { |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 954 | // TODO(nisse): SSRC == 0 currently means none. Delete check when that |
| 955 | // is fixed. |
| 956 | if (voice_sender_info.ssrc() == 0) |
| 957 | continue; |
| 958 | std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio( |
| 959 | new RTCOutboundRTPStreamStats( |
| 960 | RTCOutboundRTPStreamStatsIDFromSSRC( |
| 961 | true, voice_sender_info.ssrc()), |
| 962 | timestamp_us)); |
| 963 | SetOutboundRTPStreamStatsFromVoiceSenderInfo( |
| 964 | voice_sender_info, outbound_audio.get()); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 965 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
| 966 | track_media_info_map_->GetAudioTrack(voice_sender_info); |
| 967 | if (audio_track) { |
| 968 | RTC_DCHECK(track_to_id_.find(audio_track.get()) != track_to_id_.end()); |
hbos | b0ae920 | 2017-01-27 06:35:16 -0800 | [diff] [blame] | 969 | outbound_audio->track_id = |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 970 | RTCMediaStreamTrackStatsIDFromTrackKindIDAndSsrc( |
| 971 | true, |
| 972 | MediaStreamTrackInterface::kAudioKind, |
| 973 | track_to_id_.find(audio_track.get())->second, |
| 974 | voice_sender_info.ssrc()); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 975 | } |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 976 | outbound_audio->transport_id = transport_id; |
| 977 | if (voice_sender_info.codec_payload_type) { |
| 978 | outbound_audio->codec_id = |
| 979 | RTCCodecStatsIDFromDirectionMediaAndPayload( |
| 980 | false, true, *voice_sender_info.codec_payload_type); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 981 | } |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 982 | report->AddStats(std::move(outbound_audio)); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | // Video |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 986 | if (track_media_info_map.video_media_info()) { |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 987 | std::string transport_id = RTCTransportStatsIDFromBaseChannel( |
| 988 | session_stats.proxy_to_transport, *pc_->session()->video_channel()); |
| 989 | RTC_DCHECK(!transport_id.empty()); |
| 990 | // Inbound |
| 991 | for (const cricket::VideoReceiverInfo& video_receiver_info : |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 992 | track_media_info_map.video_media_info()->receivers) { |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 993 | // TODO(nisse): SSRC == 0 currently means none. Delete check when that |
| 994 | // is fixed. |
| 995 | if (video_receiver_info.ssrc() == 0) |
| 996 | continue; |
| 997 | std::unique_ptr<RTCInboundRTPStreamStats> inbound_video( |
| 998 | new RTCInboundRTPStreamStats( |
| 999 | RTCInboundRTPStreamStatsIDFromSSRC( |
| 1000 | false, video_receiver_info.ssrc()), |
| 1001 | timestamp_us)); |
| 1002 | SetInboundRTPStreamStatsFromVideoReceiverInfo( |
| 1003 | video_receiver_info, inbound_video.get()); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1004 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
| 1005 | track_media_info_map_->GetVideoTrack(video_receiver_info); |
| 1006 | if (video_track) { |
| 1007 | RTC_DCHECK(track_to_id_.find(video_track.get()) != track_to_id_.end()); |
hbos | b0ae920 | 2017-01-27 06:35:16 -0800 | [diff] [blame] | 1008 | inbound_video->track_id = |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1009 | RTCMediaStreamTrackStatsIDFromTrackKindIDAndSsrc( |
| 1010 | false, |
| 1011 | MediaStreamTrackInterface::kVideoKind, |
| 1012 | track_to_id_.find(video_track.get())->second, |
| 1013 | video_receiver_info.ssrc()); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1014 | } |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1015 | inbound_video->transport_id = transport_id; |
| 1016 | if (video_receiver_info.codec_payload_type) { |
| 1017 | inbound_video->codec_id = |
| 1018 | RTCCodecStatsIDFromDirectionMediaAndPayload( |
| 1019 | true, false, *video_receiver_info.codec_payload_type); |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1020 | } |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1021 | report->AddStats(std::move(inbound_video)); |
| 1022 | } |
| 1023 | // Outbound |
| 1024 | for (const cricket::VideoSenderInfo& video_sender_info : |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1025 | track_media_info_map.video_media_info()->senders) { |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1026 | // TODO(nisse): SSRC == 0 currently means none. Delete check when that |
| 1027 | // is fixed. |
| 1028 | if (video_sender_info.ssrc() == 0) |
| 1029 | continue; |
| 1030 | std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video( |
| 1031 | new RTCOutboundRTPStreamStats( |
| 1032 | RTCOutboundRTPStreamStatsIDFromSSRC( |
| 1033 | false, video_sender_info.ssrc()), |
| 1034 | timestamp_us)); |
| 1035 | SetOutboundRTPStreamStatsFromVideoSenderInfo( |
| 1036 | video_sender_info, outbound_video.get()); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1037 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
| 1038 | track_media_info_map_->GetVideoTrack(video_sender_info); |
| 1039 | if (video_track) { |
| 1040 | RTC_DCHECK(track_to_id_.find(video_track.get()) != track_to_id_.end()); |
hbos | b0ae920 | 2017-01-27 06:35:16 -0800 | [diff] [blame] | 1041 | outbound_video->track_id = |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1042 | RTCMediaStreamTrackStatsIDFromTrackKindIDAndSsrc( |
| 1043 | true, |
| 1044 | MediaStreamTrackInterface::kVideoKind, |
| 1045 | track_to_id_.find(video_track.get())->second, |
| 1046 | video_sender_info.ssrc()); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1047 | } |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1048 | outbound_video->transport_id = transport_id; |
| 1049 | if (video_sender_info.codec_payload_type) { |
| 1050 | outbound_video->codec_id = |
| 1051 | RTCCodecStatsIDFromDirectionMediaAndPayload( |
| 1052 | false, false, *video_sender_info.codec_payload_type); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1053 | } |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1054 | report->AddStats(std::move(outbound_video)); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1059 | void RTCStatsCollector::ProduceTransportStats_n( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1060 | int64_t timestamp_us, const SessionStats& session_stats, |
| 1061 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
| 1062 | RTCStatsReport* report) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1063 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1064 | for (const auto& transport : session_stats.transport_stats) { |
| 1065 | // Get reference to RTCP channel, if it exists. |
| 1066 | std::string rtcp_transport_stats_id; |
| 1067 | for (const auto& channel_stats : transport.second.channel_stats) { |
| 1068 | if (channel_stats.component == |
| 1069 | cricket::ICE_CANDIDATE_COMPONENT_RTCP) { |
| 1070 | rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel( |
| 1071 | transport.second.transport_name, channel_stats.component); |
| 1072 | break; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | // Get reference to local and remote certificates of this transport, if they |
| 1077 | // exist. |
| 1078 | const auto& certificate_stats_it = transport_cert_stats.find( |
| 1079 | transport.second.transport_name); |
| 1080 | RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend()); |
| 1081 | std::string local_certificate_id; |
| 1082 | if (certificate_stats_it->second.local) { |
| 1083 | local_certificate_id = RTCCertificateIDFromFingerprint( |
| 1084 | certificate_stats_it->second.local->fingerprint); |
| 1085 | } |
| 1086 | std::string remote_certificate_id; |
| 1087 | if (certificate_stats_it->second.remote) { |
| 1088 | remote_certificate_id = RTCCertificateIDFromFingerprint( |
| 1089 | certificate_stats_it->second.remote->fingerprint); |
| 1090 | } |
| 1091 | |
| 1092 | // There is one transport stats for each channel. |
| 1093 | for (const auto& channel_stats : transport.second.channel_stats) { |
| 1094 | std::unique_ptr<RTCTransportStats> transport_stats( |
| 1095 | new RTCTransportStats( |
| 1096 | RTCTransportStatsIDFromTransportChannel( |
| 1097 | transport.second.transport_name, channel_stats.component), |
| 1098 | timestamp_us)); |
| 1099 | transport_stats->bytes_sent = 0; |
| 1100 | transport_stats->bytes_received = 0; |
hbos | 7064d59 | 2017-01-16 07:38:02 -0800 | [diff] [blame] | 1101 | transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState( |
| 1102 | channel_stats.dtls_state); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1103 | for (const cricket::ConnectionInfo& info : |
| 1104 | channel_stats.connection_infos) { |
| 1105 | *transport_stats->bytes_sent += info.sent_total_bytes; |
| 1106 | *transport_stats->bytes_received += info.recv_total_bytes; |
| 1107 | if (info.best_connection) { |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1108 | transport_stats->selected_candidate_pair_id = |
| 1109 | RTCIceCandidatePairStatsIDFromConnectionInfo(info); |
| 1110 | } |
| 1111 | } |
| 1112 | if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP && |
| 1113 | !rtcp_transport_stats_id.empty()) { |
| 1114 | transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id; |
| 1115 | } |
| 1116 | if (!local_certificate_id.empty()) |
| 1117 | transport_stats->local_certificate_id = local_certificate_id; |
| 1118 | if (!remote_certificate_id.empty()) |
| 1119 | transport_stats->remote_certificate_id = remote_certificate_id; |
| 1120 | report->AddStats(std::move(transport_stats)); |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | std::map<std::string, RTCStatsCollector::CertificateStatsPair> |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1126 | RTCStatsCollector::PrepareTransportCertificateStats_n( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1127 | const SessionStats& session_stats) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1128 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1129 | std::map<std::string, CertificateStatsPair> transport_cert_stats; |
| 1130 | for (const auto& transport_stats : session_stats.transport_stats) { |
| 1131 | CertificateStatsPair certificate_stats_pair; |
| 1132 | rtc::scoped_refptr<rtc::RTCCertificate> local_certificate; |
| 1133 | if (pc_->session()->GetLocalCertificate( |
| 1134 | transport_stats.second.transport_name, &local_certificate)) { |
| 1135 | certificate_stats_pair.local = |
| 1136 | local_certificate->ssl_certificate().GetStats(); |
| 1137 | } |
| 1138 | std::unique_ptr<rtc::SSLCertificate> remote_certificate = |
| 1139 | pc_->session()->GetRemoteSSLCertificate( |
| 1140 | transport_stats.second.transport_name); |
| 1141 | if (remote_certificate) { |
| 1142 | certificate_stats_pair.remote = remote_certificate->GetStats(); |
| 1143 | } |
| 1144 | transport_cert_stats.insert( |
| 1145 | std::make_pair(transport_stats.second.transport_name, |
| 1146 | std::move(certificate_stats_pair))); |
| 1147 | } |
| 1148 | return transport_cert_stats; |
| 1149 | } |
| 1150 | |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1151 | std::unique_ptr<TrackMediaInfoMap> |
| 1152 | RTCStatsCollector::PrepareTrackMediaInfoMap_s() const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1153 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1154 | std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1155 | if (pc_->session()->voice_channel()) { |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1156 | voice_media_info.reset(new cricket::VoiceMediaInfo()); |
| 1157 | if (!pc_->session()->voice_channel()->GetStats(voice_media_info.get())) { |
| 1158 | voice_media_info.reset(); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1159 | } |
| 1160 | } |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1161 | std::unique_ptr<cricket::VideoMediaInfo> video_media_info; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1162 | if (pc_->session()->video_channel()) { |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1163 | video_media_info.reset(new cricket::VideoMediaInfo()); |
| 1164 | if (!pc_->session()->video_channel()->GetStats(video_media_info.get())) { |
| 1165 | video_media_info.reset(); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1166 | } |
| 1167 | } |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1168 | std::unique_ptr<TrackMediaInfoMap> track_media_info_map( |
| 1169 | new TrackMediaInfoMap(std::move(voice_media_info), |
| 1170 | std::move(video_media_info), |
| 1171 | pc_->GetSenders(), |
| 1172 | pc_->GetReceivers())); |
| 1173 | return track_media_info_map; |
| 1174 | } |
| 1175 | |
| 1176 | std::map<MediaStreamTrackInterface*, std::string> |
| 1177 | RTCStatsCollector::PrepareTrackToID_s() const { |
| 1178 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 1179 | std::map<MediaStreamTrackInterface*, std::string> track_to_id; |
| 1180 | StreamCollectionInterface* local_and_remote_streams[] = |
| 1181 | { pc_->local_streams().get(), pc_->remote_streams().get() }; |
| 1182 | for (auto& streams : local_and_remote_streams) { |
| 1183 | if (streams) { |
| 1184 | for (size_t i = 0; i < streams->count(); ++i) { |
| 1185 | MediaStreamInterface* stream = streams->at(i); |
| 1186 | for (const rtc::scoped_refptr<AudioTrackInterface>& audio_track : |
| 1187 | stream->GetAudioTracks()) { |
| 1188 | track_to_id[audio_track.get()] = audio_track->id(); |
| 1189 | } |
| 1190 | for (const rtc::scoped_refptr<VideoTrackInterface>& video_track : |
| 1191 | stream->GetVideoTracks()) { |
| 1192 | track_to_id[video_track.get()] = video_track->id(); |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | return track_to_id; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1200 | void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) { |
| 1201 | channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened); |
| 1202 | channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed); |
| 1203 | } |
| 1204 | |
| 1205 | void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) { |
| 1206 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 1207 | bool result = internal_record_.opened_data_channels.insert( |
| 1208 | reinterpret_cast<uintptr_t>(channel)).second; |
| 1209 | ++internal_record_.data_channels_opened; |
| 1210 | RTC_DCHECK(result); |
| 1211 | } |
| 1212 | |
| 1213 | void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) { |
| 1214 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 1215 | // Only channels that have been fully opened (and have increased the |
| 1216 | // |data_channels_opened_| counter) increase the closed counter. |
| 1217 | if (internal_record_.opened_data_channels.find( |
| 1218 | reinterpret_cast<uintptr_t>(channel)) != |
| 1219 | internal_record_.opened_data_channels.end()) { |
| 1220 | ++internal_record_.data_channels_closed; |
| 1221 | } |
| 1222 | } |
| 1223 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 1224 | const char* CandidateTypeToRTCIceCandidateTypeForTesting( |
| 1225 | const std::string& type) { |
| 1226 | return CandidateTypeToRTCIceCandidateType(type); |
| 1227 | } |
| 1228 | |
| 1229 | const char* DataStateToRTCDataChannelStateForTesting( |
| 1230 | DataChannelInterface::DataState state) { |
| 1231 | return DataStateToRTCDataChannelState(state); |
| 1232 | } |
| 1233 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1234 | } // namespace webrtc |