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