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