blob: 10bb605f4994cbc7a899b92a60e5ae3ccfed537f [file] [log] [blame]
hbosd565b732016-08-30 14:04:35 -07001/*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/rtcstatscollector.h"
hbosd565b732016-08-30 14:04:35 -070012
13#include <memory>
hbos9e302742017-01-20 02:47:10 -080014#include <sstream>
Steve Anton36b29d12017-10-30 09:57:42 -070015#include <string>
hbosd565b732016-08-30 14:04:35 -070016#include <utility>
17#include <vector>
18
Patrik Höglunde2d6a062017-10-05 14:53:33 +020019#include "api/candidate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/mediastreaminterface.h"
21#include "api/peerconnectioninterface.h"
22#include "media/base/mediachannel.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "p2p/base/p2pconstants.h"
24#include "p2p/base/port.h"
25#include "pc/peerconnection.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/checks.h"
27#include "rtc_base/stringutils.h"
28#include "rtc_base/timeutils.h"
29#include "rtc_base/trace_event.h"
hbosd565b732016-08-30 14:04:35 -070030
31namespace webrtc {
32
hboscc555c52016-10-18 12:48:31 -070033namespace {
34
hbos2fa7c672016-10-24 04:00:05 -070035std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
36 return "RTCCertificate_" + fingerprint;
37}
38
hbos0adb8282016-11-23 02:32:06 -080039std::string RTCCodecStatsIDFromDirectionMediaAndPayload(
40 bool inbound, bool audio, uint32_t payload_type) {
hbos585a9b12017-02-07 04:59:16 -080041 // TODO(hbos): The present codec ID assignment is not sufficient to support
42 // Unified Plan or unbundled connections in all cases. When we are able to
43 // handle multiple m= lines of the same media type (and multiple BaseChannels
44 // for the same type is possible?) this needs to be updated to differentiate
45 // the transport being used, and stats need to be collected for all of them.
hbos0adb8282016-11-23 02:32:06 -080046 if (inbound) {
47 return audio ? "RTCCodec_InboundAudio_" + rtc::ToString<>(payload_type)
48 : "RTCCodec_InboundVideo_" + rtc::ToString<>(payload_type);
49 }
50 return audio ? "RTCCodec_OutboundAudio_" + rtc::ToString<>(payload_type)
51 : "RTCCodec_OutboundVideo_" + rtc::ToString<>(payload_type);
52}
53
hbos2fa7c672016-10-24 04:00:05 -070054std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
55 const cricket::ConnectionInfo& info) {
56 return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" +
57 info.remote_candidate.id();
58}
59
Harald Alvestranda3dab842018-01-14 09:18:58 +010060const char kSender[] = "sender";
61const char kReceiver[] = "receiver";
62
63std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
64 const char* direction,
Harald Alvestrandc72af932018-01-11 17:18:19 +010065 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -080066 std::ostringstream oss;
Harald Alvestranda3dab842018-01-14 09:18:58 +010067 oss << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
hbos9e302742017-01-20 02:47:10 -080068 return oss.str();
hbos09bc1282016-11-08 06:29:22 -080069}
70
hbos2fa7c672016-10-24 04:00:05 -070071std::string RTCTransportStatsIDFromTransportChannel(
72 const std::string& transport_name, int channel_component) {
73 return "RTCTransport_" + transport_name + "_" +
74 rtc::ToString<>(channel_component);
75}
76
hboseeafe942016-11-01 03:00:17 -070077std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
78 return audio ? "RTCInboundRTPAudioStream_" + rtc::ToString<>(ssrc)
79 : "RTCInboundRTPVideoStream_" + rtc::ToString<>(ssrc);
80}
81
hbos6ded1902016-11-01 01:50:46 -070082std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
83 return audio ? "RTCOutboundRTPAudioStream_" + rtc::ToString<>(ssrc)
84 : "RTCOutboundRTPVideoStream_" + rtc::ToString<>(ssrc);
85}
86
hbosab9f6e42016-10-07 02:18:47 -070087const 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
hboscc555c52016-10-18 12:48:31 -0700100const 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
hbos06495bc2017-01-02 08:08:18 -0800117const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
118 cricket::IceCandidatePairState state) {
119 switch (state) {
120 case cricket::IceCandidatePairState::WAITING:
121 return RTCStatsIceCandidatePairState::kWaiting;
122 case cricket::IceCandidatePairState::IN_PROGRESS:
123 return RTCStatsIceCandidatePairState::kInProgress;
124 case cricket::IceCandidatePairState::SUCCEEDED:
125 return RTCStatsIceCandidatePairState::kSucceeded;
126 case cricket::IceCandidatePairState::FAILED:
127 return RTCStatsIceCandidatePairState::kFailed;
128 default:
129 RTC_NOTREACHED();
130 return nullptr;
131 }
132}
133
hbos7064d592017-01-16 07:38:02 -0800134const char* DtlsTransportStateToRTCDtlsTransportState(
135 cricket::DtlsTransportState state) {
136 switch (state) {
137 case cricket::DTLS_TRANSPORT_NEW:
138 return RTCDtlsTransportState::kNew;
139 case cricket::DTLS_TRANSPORT_CONNECTING:
140 return RTCDtlsTransportState::kConnecting;
141 case cricket::DTLS_TRANSPORT_CONNECTED:
142 return RTCDtlsTransportState::kConnected;
143 case cricket::DTLS_TRANSPORT_CLOSED:
144 return RTCDtlsTransportState::kClosed;
145 case cricket::DTLS_TRANSPORT_FAILED:
146 return RTCDtlsTransportState::kFailed;
147 default:
148 RTC_NOTREACHED();
149 return nullptr;
150 }
151}
152
Gary Liu37e489c2017-11-21 10:49:36 -0800153const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
154 switch (type) {
155 case rtc::ADAPTER_TYPE_CELLULAR:
156 return RTCNetworkType::kCellular;
157 case rtc::ADAPTER_TYPE_ETHERNET:
158 return RTCNetworkType::kEthernet;
159 case rtc::ADAPTER_TYPE_WIFI:
160 return RTCNetworkType::kWifi;
161 case rtc::ADAPTER_TYPE_VPN:
162 return RTCNetworkType::kVpn;
163 case rtc::ADAPTER_TYPE_UNKNOWN:
164 case rtc::ADAPTER_TYPE_LOOPBACK:
165 return RTCNetworkType::kUnknown;
166 }
167 RTC_NOTREACHED();
168 return nullptr;
169}
170
hbos9e302742017-01-20 02:47:10 -0800171double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
172 RTC_DCHECK_GE(audio_level, 0);
173 RTC_DCHECK_LE(audio_level, 32767);
174 return audio_level / 32767.0;
175}
176
hbos0adb8282016-11-23 02:32:06 -0800177std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
178 uint64_t timestamp_us, bool inbound, bool audio,
179 const RtpCodecParameters& codec_params) {
180 RTC_DCHECK_GE(codec_params.payload_type, 0);
181 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 12:09:01 -0800182 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 02:32:06 -0800183 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
184 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
185 RTCCodecStatsIDFromDirectionMediaAndPayload(inbound, audio, payload_type),
186 timestamp_us));
187 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 06:56:04 -0800188 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 12:09:01 -0800189 if (codec_params.clock_rate) {
190 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
191 }
hbos0adb8282016-11-23 02:32:06 -0800192 return codec_stats;
193}
194
hbos09bc1282016-11-08 06:29:22 -0800195void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
196 const MediaStreamTrackInterface& track,
197 RTCMediaStreamTrackStats* track_stats) {
198 track_stats->track_identifier = track.id();
199 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
200}
201
hbos820f5782016-11-22 03:16:50 -0800202// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700203void SetInboundRTPStreamStatsFromMediaReceiverInfo(
204 const cricket::MediaReceiverInfo& media_receiver_info,
205 RTCInboundRTPStreamStats* inbound_stats) {
206 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800207 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100208 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 03:00:17 -0700209 inbound_stats->is_remote = false;
hboseeafe942016-11-01 03:00:17 -0700210 inbound_stats->packets_received =
211 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
212 inbound_stats->bytes_received =
213 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800214 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 12:26:04 +0100215 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700216 inbound_stats->fraction_lost =
217 static_cast<double>(media_receiver_info.fraction_lost);
218}
219
220void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
221 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800222 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700223 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800224 voice_receiver_info, inbound_audio);
225 inbound_audio->media_type = "audio";
hbos585a9b12017-02-07 04:59:16 -0800226 if (voice_receiver_info.codec_payload_type) {
227 inbound_audio->codec_id =
228 RTCCodecStatsIDFromDirectionMediaAndPayload(
229 true, true, *voice_receiver_info.codec_payload_type);
230 }
hbos820f5782016-11-22 03:16:50 -0800231 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700232 static_cast<double>(voice_receiver_info.jitter_ms) /
233 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800234 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
235 // purposefully left undefined for audio.
hboseeafe942016-11-01 03:00:17 -0700236}
237
238void SetInboundRTPStreamStatsFromVideoReceiverInfo(
239 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800240 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700241 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800242 video_receiver_info, inbound_video);
243 inbound_video->media_type = "video";
hbos585a9b12017-02-07 04:59:16 -0800244 if (video_receiver_info.codec_payload_type) {
245 inbound_video->codec_id =
246 RTCCodecStatsIDFromDirectionMediaAndPayload(
247 true, false, *video_receiver_info.codec_payload_type);
248 }
hbos820f5782016-11-22 03:16:50 -0800249 inbound_video->fir_count =
250 static_cast<uint32_t>(video_receiver_info.firs_sent);
251 inbound_video->pli_count =
252 static_cast<uint32_t>(video_receiver_info.plis_sent);
253 inbound_video->nack_count =
254 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800255 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800256 if (video_receiver_info.qp_sum)
257 inbound_video->qp_sum = *video_receiver_info.qp_sum;
hboseeafe942016-11-01 03:00:17 -0700258}
259
hbos820f5782016-11-22 03:16:50 -0800260// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700261void SetOutboundRTPStreamStatsFromMediaSenderInfo(
262 const cricket::MediaSenderInfo& media_sender_info,
263 RTCOutboundRTPStreamStats* outbound_stats) {
264 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800265 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100266 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700267 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700268 outbound_stats->packets_sent =
269 static_cast<uint32_t>(media_sender_info.packets_sent);
270 outbound_stats->bytes_sent =
271 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbos6ded1902016-11-01 01:50:46 -0700272}
273
274void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
275 const cricket::VoiceSenderInfo& voice_sender_info,
276 RTCOutboundRTPStreamStats* outbound_audio) {
277 SetOutboundRTPStreamStatsFromMediaSenderInfo(
278 voice_sender_info, outbound_audio);
279 outbound_audio->media_type = "audio";
hbos585a9b12017-02-07 04:59:16 -0800280 if (voice_sender_info.codec_payload_type) {
281 outbound_audio->codec_id =
282 RTCCodecStatsIDFromDirectionMediaAndPayload(
283 false, true, *voice_sender_info.codec_payload_type);
284 }
hbos6ded1902016-11-01 01:50:46 -0700285 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
286 // purposefully left undefined for audio.
287}
288
289void SetOutboundRTPStreamStatsFromVideoSenderInfo(
290 const cricket::VideoSenderInfo& video_sender_info,
291 RTCOutboundRTPStreamStats* outbound_video) {
292 SetOutboundRTPStreamStatsFromMediaSenderInfo(
293 video_sender_info, outbound_video);
294 outbound_video->media_type = "video";
hbos585a9b12017-02-07 04:59:16 -0800295 if (video_sender_info.codec_payload_type) {
296 outbound_video->codec_id =
297 RTCCodecStatsIDFromDirectionMediaAndPayload(
298 false, false, *video_sender_info.codec_payload_type);
299 }
hbos6ded1902016-11-01 01:50:46 -0700300 outbound_video->fir_count =
301 static_cast<uint32_t>(video_sender_info.firs_rcvd);
302 outbound_video->pli_count =
303 static_cast<uint32_t>(video_sender_info.plis_rcvd);
304 outbound_video->nack_count =
305 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800306 if (video_sender_info.qp_sum)
307 outbound_video->qp_sum = *video_sender_info.qp_sum;
308 outbound_video->frames_encoded = video_sender_info.frames_encoded;
hbos6ded1902016-11-01 01:50:46 -0700309}
310
hbos02ba2112016-10-28 05:14:53 -0700311void ProduceCertificateStatsFromSSLCertificateStats(
312 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
313 RTCStatsReport* report) {
314 RTCCertificateStats* prev_certificate_stats = nullptr;
315 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
316 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800317 std::string certificate_stats_id =
318 RTCCertificateIDFromFingerprint(s->fingerprint);
319 // It is possible for the same certificate to show up multiple times, e.g.
320 // if local and remote side use the same certificate in a loopback call.
321 // If the report already contains stats for this certificate, skip it.
322 if (report->Get(certificate_stats_id)) {
323 RTC_DCHECK_EQ(s, &certificate_stats);
324 break;
325 }
hbos02ba2112016-10-28 05:14:53 -0700326 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800327 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700328 certificate_stats->fingerprint = s->fingerprint;
329 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
330 certificate_stats->base64_certificate = s->base64_certificate;
331 if (prev_certificate_stats)
332 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
333 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
334 prev_certificate_stats = certificate_stats;
335 }
336}
337
338const std::string& ProduceIceCandidateStats(
339 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800340 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700341 const std::string& id = "RTCIceCandidate_" + candidate.id();
342 const RTCStats* stats = report->Get(id);
343 if (!stats) {
344 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
345 if (is_local)
346 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
347 else
348 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800349 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800350 if (is_local) {
351 candidate_stats->network_type =
352 NetworkAdapterTypeToStatsType(candidate.network_type());
353 } else {
354 // We don't expect to know the adapter type of remote candidates.
355 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
356 }
hbos02ba2112016-10-28 05:14:53 -0700357 candidate_stats->ip = candidate.address().ipaddr().ToString();
358 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
359 candidate_stats->protocol = candidate.protocol();
360 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
361 candidate.type());
362 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
363
364 stats = candidate_stats.get();
365 report->AddStats(std::move(candidate_stats));
366 }
367 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
368 : RTCRemoteIceCandidateStats::kType);
369 return stats->id();
370}
371
hbos9e302742017-01-20 02:47:10 -0800372std::unique_ptr<RTCMediaStreamTrackStats>
373ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
374 int64_t timestamp_us,
375 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100376 const cricket::VoiceSenderInfo& voice_sender_info,
377 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800378 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
379 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100380 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
381 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100382 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800383 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
384 audio_track, audio_track_stats.get());
385 audio_track_stats->remote_source = false;
386 audio_track_stats->detached = false;
387 if (voice_sender_info.audio_level >= 0) {
388 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
389 voice_sender_info.audio_level);
390 }
zsteine76bd3a2017-07-14 12:17:49 -0700391 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
392 audio_track_stats->total_samples_duration =
393 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100394 if (voice_sender_info.apm_statistics.echo_return_loss) {
395 audio_track_stats->echo_return_loss =
396 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800397 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100398 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
399 audio_track_stats->echo_return_loss_enhancement =
400 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800401 }
402 return audio_track_stats;
403}
404
405std::unique_ptr<RTCMediaStreamTrackStats>
406ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
407 int64_t timestamp_us,
408 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100409 const cricket::VoiceReceiverInfo& voice_receiver_info,
410 int attachment_id) {
411 // Since receiver tracks can't be reattached, we use the SSRC as
412 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800413 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
414 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100415 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
416 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100417 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800418 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
419 audio_track, audio_track_stats.get());
420 audio_track_stats->remote_source = true;
421 audio_track_stats->detached = false;
422 if (voice_receiver_info.audio_level >= 0) {
423 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
424 voice_receiver_info.audio_level);
425 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200426 audio_track_stats->jitter_buffer_delay =
427 voice_receiver_info.jitter_buffer_delay_seconds;
zsteine76bd3a2017-07-14 12:17:49 -0700428 audio_track_stats->total_audio_energy =
429 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700430 audio_track_stats->total_samples_received =
431 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700432 audio_track_stats->total_samples_duration =
433 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700434 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200435 audio_track_stats->concealment_events =
436 voice_receiver_info.concealment_events;
hbos9e302742017-01-20 02:47:10 -0800437 return audio_track_stats;
438}
439
440std::unique_ptr<RTCMediaStreamTrackStats>
441ProduceMediaStreamTrackStatsFromVideoSenderInfo(
442 int64_t timestamp_us,
443 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100444 const cricket::VideoSenderInfo& video_sender_info,
445 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800446 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
447 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100448 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
449
450 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100451 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800452 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
453 video_track, video_track_stats.get());
454 video_track_stats->remote_source = false;
455 video_track_stats->detached = false;
456 video_track_stats->frame_width = static_cast<uint32_t>(
457 video_sender_info.send_frame_width);
458 video_track_stats->frame_height = static_cast<uint32_t>(
459 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800460 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100461 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800462 video_track_stats->frames_sent = video_sender_info.frames_encoded;
hbos9e302742017-01-20 02:47:10 -0800463 return video_track_stats;
464}
465
466std::unique_ptr<RTCMediaStreamTrackStats>
467ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
468 int64_t timestamp_us,
469 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100470 const cricket::VideoReceiverInfo& video_receiver_info,
471 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800472 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
473 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100474 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
475
476 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100477 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800478 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
479 video_track, video_track_stats.get());
480 video_track_stats->remote_source = true;
481 video_track_stats->detached = false;
482 if (video_receiver_info.frame_width > 0 &&
483 video_receiver_info.frame_height > 0) {
484 video_track_stats->frame_width = static_cast<uint32_t>(
485 video_receiver_info.frame_width);
486 video_track_stats->frame_height = static_cast<uint32_t>(
487 video_receiver_info.frame_height);
488 }
hbos42f6d2f2017-01-20 03:56:50 -0800489 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800490 // TODO(hbos): When we support receiving simulcast, this should be the total
491 // number of frames correctly decoded, independent of which SSRC it was
492 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100493 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800494 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800495 RTC_DCHECK_GE(video_receiver_info.frames_received,
496 video_receiver_info.frames_rendered);
497 video_track_stats->frames_dropped = video_receiver_info.frames_received -
498 video_receiver_info.frames_rendered;
hbos9e302742017-01-20 02:47:10 -0800499 return video_track_stats;
500}
501
Harald Alvestrand89061872018-01-02 14:08:34 +0100502void ProduceSenderMediaTrackStats(
503 int64_t timestamp_us,
504 const TrackMediaInfoMap& track_media_info_map,
505 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders,
506 RTCStatsReport* report) {
507 // This function iterates over the senders to generate outgoing track stats.
508
509 // TODO(hbos): Return stats of detached tracks. We have to perform stats
510 // gathering at the time of detachment to get accurate stats and timestamps.
511 // https://crbug.com/659137
512 for (auto const sender : senders) {
513 // Don't report on tracks before starting to send.
514 // https://bugs.webrtc.org/8673
515 if (sender->ssrc() == 0)
516 continue;
517 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
518 AudioTrackInterface* track =
519 static_cast<AudioTrackInterface*>(sender->track().get());
520 if (!track)
521 continue;
522 auto voice_sender_info =
523 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
524 RTC_CHECK(voice_sender_info)
525 << "No voice sender info for sender with ssrc " << sender->ssrc();
526 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100527 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
528 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100529 report->AddStats(std::move(audio_track_stats));
530 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
531 VideoTrackInterface* track =
532 static_cast<VideoTrackInterface*>(sender->track().get());
533 if (!track)
534 continue;
535 auto video_sender_info =
536 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
537 RTC_CHECK(video_sender_info)
538 << "No video sender info for sender with ssrc " << sender->ssrc();
539 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100540 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
541 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100542 report->AddStats(std::move(video_track_stats));
543 } else {
544 RTC_NOTREACHED();
545 }
546 }
547}
548
549void ProduceReceiverMediaTrackStats(
550 int64_t timestamp_us,
551 const TrackMediaInfoMap& track_media_info_map,
552 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers,
553 RTCStatsReport* report) {
554 // This function iterates over the receivers to find the remote tracks.
555 for (auto const receiver : receivers) {
556 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
557 AudioTrackInterface* track =
558 static_cast<AudioTrackInterface*>(receiver->track().get());
559 const cricket::VoiceReceiverInfo* voice_receiver_info =
560 track_media_info_map.GetVoiceReceiverInfo(*track);
561 if (!voice_receiver_info) {
562 continue;
563 }
564 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
565 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100566 timestamp_us, *track, *voice_receiver_info,
567 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100568 report->AddStats(std::move(audio_track_stats));
569 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
570 VideoTrackInterface* track =
571 static_cast<VideoTrackInterface*>(receiver->track().get());
572 const cricket::VideoReceiverInfo* video_receiver_info =
573 track_media_info_map.GetVideoReceiverInfo(*track);
574 if (!video_receiver_info) {
575 continue;
576 }
577 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
578 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100579 timestamp_us, *track, *video_receiver_info,
580 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100581 report->AddStats(std::move(video_track_stats));
582 } else {
583 RTC_NOTREACHED();
584 }
585 }
586}
587
588void ProduceMediaStreamStats(
hbos09bc1282016-11-08 06:29:22 -0800589 int64_t timestamp_us,
Harald Alvestranda3dab842018-01-14 09:18:58 +0100590 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& rtp_senders,
591 const std::vector<rtc::scoped_refptr<RtpReceiverInterface>>& rtp_receivers,
hbos09bc1282016-11-08 06:29:22 -0800592 RTCStatsReport* report) {
Harald Alvestranda3dab842018-01-14 09:18:58 +0100593 std::map<std::string, std::vector<std::string>> track_ids;
hbos09bc1282016-11-08 06:29:22 -0800594
Harald Alvestranda3dab842018-01-14 09:18:58 +0100595 for (auto& sender : rtp_senders) {
596 std::string track_id = RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
597 kSender, sender->AttachmentId());
598 for (auto& stream_id : sender->stream_ids()) {
599 track_ids[stream_id].push_back(track_id);
hbos09bc1282016-11-08 06:29:22 -0800600 }
Harald Alvestranda3dab842018-01-14 09:18:58 +0100601 }
602 for (auto& receiver : rtp_receivers) {
603 std::string track_id = RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
604 kReceiver, receiver->AttachmentId());
605 for (auto& stream : receiver->streams()) {
606 track_ids[stream->label()].push_back(track_id);
607 }
608 }
609
610 // Build stats for each stream ID known.
611 for (auto& it : track_ids) {
612 std::unique_ptr<RTCMediaStreamStats> stream_stats(
613 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
614 stream_stats->stream_identifier = it.first;
615 stream_stats->track_ids = it.second;
hbos09bc1282016-11-08 06:29:22 -0800616 report->AddStats(std::move(stream_stats));
617 }
618}
619
hboscc555c52016-10-18 12:48:31 -0700620} // namespace
621
hbosc82f2e12016-09-05 01:36:50 -0700622rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
623 PeerConnection* pc, int64_t cache_lifetime_us) {
624 return rtc::scoped_refptr<RTCStatsCollector>(
625 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
626}
627
628RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
629 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700630 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700631 signaling_thread_(pc->signaling_thread()),
632 worker_thread_(pc->worker_thread()),
633 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700634 num_pending_partial_reports_(0),
635 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700636 cache_timestamp_us_(0),
637 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700638 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700639 RTC_DCHECK(signaling_thread_);
640 RTC_DCHECK(worker_thread_);
641 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700642 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbos82ebe022016-11-14 01:41:09 -0800643 pc_->SignalDataChannelCreated.connect(
644 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700645}
646
hbosb78306a2016-12-19 05:06:57 -0800647RTCStatsCollector::~RTCStatsCollector() {
648 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
649}
650
hbosc82f2e12016-09-05 01:36:50 -0700651void RTCStatsCollector::GetStatsReport(
652 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
653 RTC_DCHECK(signaling_thread_->IsCurrent());
654 RTC_DCHECK(callback);
655 callbacks_.push_back(callback);
656
hbos0e6758d2016-08-31 07:57:36 -0700657 // "Now" using a monotonically increasing timer.
658 int64_t cache_now_us = rtc::TimeMicros();
659 if (cached_report_ &&
660 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700661 // We have a fresh cached report to deliver.
662 DeliverCachedReport();
663 } else if (!num_pending_partial_reports_) {
664 // Only start gathering stats if we're not already gathering stats. In the
665 // case of already gathering stats, |callback_| will be invoked when there
666 // are no more pending partial reports.
667
668 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
669 // UTC), in microseconds. The system clock could be modified and is not
670 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700671 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700672
hbosf415f8a2017-01-02 04:28:51 -0800673 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700674 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800675
hbos84abeb12017-01-16 06:16:44 -0800676 // Prepare |channel_name_pairs_| for use in
hbosdf6075a2016-12-19 04:58:02 -0800677 // |ProducePartialResultsOnNetworkThread|.
678 channel_name_pairs_.reset(new ChannelNamePairs());
Steve Anton978b8762017-09-29 12:15:02 -0700679 if (pc_->voice_channel()) {
Oskar Sundbom89e71262017-11-16 10:56:31 +0100680 channel_name_pairs_->voice =
Steve Anton978b8762017-09-29 12:15:02 -0700681 ChannelNamePair(pc_->voice_channel()->content_name(),
Oskar Sundbom89e71262017-11-16 10:56:31 +0100682 pc_->voice_channel()->transport_name());
hbosdf6075a2016-12-19 04:58:02 -0800683 }
Steve Anton978b8762017-09-29 12:15:02 -0700684 if (pc_->video_channel()) {
Oskar Sundbom89e71262017-11-16 10:56:31 +0100685 channel_name_pairs_->video =
Steve Anton978b8762017-09-29 12:15:02 -0700686 ChannelNamePair(pc_->video_channel()->content_name(),
Oskar Sundbom89e71262017-11-16 10:56:31 +0100687 pc_->video_channel()->transport_name());
hbosdf6075a2016-12-19 04:58:02 -0800688 }
Steve Anton978b8762017-09-29 12:15:02 -0700689 if (pc_->rtp_data_channel()) {
Oskar Sundbom89e71262017-11-16 10:56:31 +0100690 channel_name_pairs_->data =
Steve Anton978b8762017-09-29 12:15:02 -0700691 ChannelNamePair(pc_->rtp_data_channel()->content_name(),
Oskar Sundbom89e71262017-11-16 10:56:31 +0100692 pc_->rtp_data_channel()->transport_name());
Steve Anton978b8762017-09-29 12:15:02 -0700693 }
694 if (pc_->sctp_content_name()) {
Oskar Sundbom89e71262017-11-16 10:56:31 +0100695 channel_name_pairs_->data = ChannelNamePair(*pc_->sctp_content_name(),
696 *pc_->sctp_transport_name());
hbosdf6075a2016-12-19 04:58:02 -0800697 }
hbos84abeb12017-01-16 06:16:44 -0800698 // Prepare |track_media_info_map_| for use in
699 // |ProducePartialResultsOnNetworkThread| and
700 // |ProducePartialResultsOnSignalingThread|.
701 track_media_info_map_.reset(PrepareTrackMediaInfoMap_s().release());
702 // Prepare |track_to_id_| for use in |ProducePartialResultsOnNetworkThread|.
703 // This avoids a possible deadlock if |MediaStreamTrackInterface::id| is
704 // implemented to invoke on the signaling thread.
705 track_to_id_ = PrepareTrackToID_s();
hbosf415f8a2017-01-02 04:28:51 -0800706
stefanf79ade12017-06-02 06:44:03 -0700707 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
708 // thread.
709 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
710 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700711 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700712
713 invoker_.AsyncInvoke<void>(
714 RTC_FROM_HERE, network_thread_,
hbosc82f2e12016-09-05 01:36:50 -0700715 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
stefanf79ade12017-06-02 06:44:03 -0700716 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800717 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700718 }
hbosd565b732016-08-30 14:04:35 -0700719}
720
721void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700722 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700723 cached_report_ = nullptr;
724}
725
hbosb78306a2016-12-19 05:06:57 -0800726void RTCStatsCollector::WaitForPendingRequest() {
727 RTC_DCHECK(signaling_thread_->IsCurrent());
728 if (num_pending_partial_reports_) {
729 rtc::Thread::Current()->ProcessMessages(0);
730 while (num_pending_partial_reports_) {
731 rtc::Thread::Current()->SleepMs(1);
732 rtc::Thread::Current()->ProcessMessages(0);
733 }
734 }
735}
736
hbosc82f2e12016-09-05 01:36:50 -0700737void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
738 int64_t timestamp_us) {
739 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700740 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
741 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700742
hboscc555c52016-10-18 12:48:31 -0700743 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos09bc1282016-11-08 06:29:22 -0800744 ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700745 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700746
747 AddPartialResults(report);
748}
749
hbosc82f2e12016-09-05 01:36:50 -0700750void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
751 int64_t timestamp_us) {
752 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700753 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
754 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700755
hbosdf6075a2016-12-19 04:58:02 -0800756 std::unique_ptr<SessionStats> session_stats =
Steve Anton978b8762017-09-29 12:15:02 -0700757 pc_->GetSessionStats(*channel_name_pairs_);
hbosdf6075a2016-12-19 04:58:02 -0800758 if (session_stats) {
759 std::map<std::string, CertificateStatsPair> transport_cert_stats =
760 PrepareTransportCertificateStats_n(*session_stats);
761
762 ProduceCertificateStats_n(
763 timestamp_us, transport_cert_stats, report.get());
764 ProduceCodecStats_n(
hbos84abeb12017-01-16 06:16:44 -0800765 timestamp_us, *track_media_info_map_, report.get());
stefanf79ade12017-06-02 06:44:03 -0700766 ProduceIceCandidateAndPairStats_n(timestamp_us, *session_stats,
767 track_media_info_map_->video_media_info(),
768 call_stats_, report.get());
Steve Anton593e3252017-12-15 11:44:48 -0800769 ProduceRTPStreamStats_n(timestamp_us, *session_stats, *channel_name_pairs_,
770 *track_media_info_map_, report.get());
hbosdf6075a2016-12-19 04:58:02 -0800771 ProduceTransportStats_n(
772 timestamp_us, *session_stats, transport_cert_stats, report.get());
773 }
hbosc82f2e12016-09-05 01:36:50 -0700774
775 AddPartialResults(report);
776}
777
778void RTCStatsCollector::AddPartialResults(
779 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
780 if (!signaling_thread_->IsCurrent()) {
781 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
782 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
783 rtc::scoped_refptr<RTCStatsCollector>(this),
784 partial_report));
785 return;
786 }
787 AddPartialResults_s(partial_report);
788}
789
790void RTCStatsCollector::AddPartialResults_s(
791 rtc::scoped_refptr<RTCStatsReport> partial_report) {
792 RTC_DCHECK(signaling_thread_->IsCurrent());
793 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
794 if (!partial_report_)
795 partial_report_ = partial_report;
796 else
797 partial_report_->TakeMembersFrom(partial_report);
798 --num_pending_partial_reports_;
799 if (!num_pending_partial_reports_) {
800 cache_timestamp_us_ = partial_report_timestamp_us_;
801 cached_report_ = partial_report_;
802 partial_report_ = nullptr;
hbos84abeb12017-01-16 06:16:44 -0800803 channel_name_pairs_.reset();
804 track_media_info_map_.reset();
805 track_to_id_.clear();
ehmaldonadoa26196b2017-07-18 03:30:29 -0700806 // Trace WebRTC Stats when getStats is called on Javascript.
807 // This allows access to WebRTC stats from trace logs. To enable them,
808 // select the "webrtc_stats" category when recording traces.
ehmaldonado8ab0fd82017-09-04 14:35:04 -0700809 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
810 cached_report_->ToJson());
hbosc82f2e12016-09-05 01:36:50 -0700811 DeliverCachedReport();
812 }
813}
814
815void RTCStatsCollector::DeliverCachedReport() {
816 RTC_DCHECK(signaling_thread_->IsCurrent());
817 RTC_DCHECK(!callbacks_.empty());
818 RTC_DCHECK(cached_report_);
819 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
820 callbacks_) {
821 callback->OnStatsDelivered(cached_report_);
822 }
823 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700824}
825
hbosdf6075a2016-12-19 04:58:02 -0800826void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700827 int64_t timestamp_us,
828 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700829 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800830 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700831 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
832 if (transport_cert_stats_pair.second.local) {
833 ProduceCertificateStatsFromSSLCertificateStats(
834 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700835 }
hbos02ba2112016-10-28 05:14:53 -0700836 if (transport_cert_stats_pair.second.remote) {
837 ProduceCertificateStatsFromSSLCertificateStats(
838 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700839 }
840 }
841}
842
hbosdf6075a2016-12-19 04:58:02 -0800843void RTCStatsCollector::ProduceCodecStats_n(
hbos84abeb12017-01-16 06:16:44 -0800844 int64_t timestamp_us, const TrackMediaInfoMap& track_media_info_map,
hbos0adb8282016-11-23 02:32:06 -0800845 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800846 RTC_DCHECK(network_thread_->IsCurrent());
hbos0adb8282016-11-23 02:32:06 -0800847 // Audio
hbos84abeb12017-01-16 06:16:44 -0800848 if (track_media_info_map.voice_media_info()) {
hbos0adb8282016-11-23 02:32:06 -0800849 // Inbound
hbos84abeb12017-01-16 06:16:44 -0800850 for (const auto& pair :
851 track_media_info_map.voice_media_info()->receive_codecs) {
hbos0adb8282016-11-23 02:32:06 -0800852 report->AddStats(CodecStatsFromRtpCodecParameters(
853 timestamp_us, true, true, pair.second));
854 }
855 // Outbound
hbos84abeb12017-01-16 06:16:44 -0800856 for (const auto& pair :
857 track_media_info_map.voice_media_info()->send_codecs) {
hbos0adb8282016-11-23 02:32:06 -0800858 report->AddStats(CodecStatsFromRtpCodecParameters(
859 timestamp_us, false, true, pair.second));
860 }
861 }
862 // Video
hbos84abeb12017-01-16 06:16:44 -0800863 if (track_media_info_map.video_media_info()) {
hbos0adb8282016-11-23 02:32:06 -0800864 // Inbound
hbos84abeb12017-01-16 06:16:44 -0800865 for (const auto& pair :
866 track_media_info_map.video_media_info()->receive_codecs) {
hbos0adb8282016-11-23 02:32:06 -0800867 report->AddStats(CodecStatsFromRtpCodecParameters(
868 timestamp_us, true, false, pair.second));
869 }
870 // Outbound
hbos84abeb12017-01-16 06:16:44 -0800871 for (const auto& pair :
872 track_media_info_map.video_media_info()->send_codecs) {
hbos0adb8282016-11-23 02:32:06 -0800873 report->AddStats(CodecStatsFromRtpCodecParameters(
874 timestamp_us, false, false, pair.second));
875 }
876 }
877}
878
hboscc555c52016-10-18 12:48:31 -0700879void RTCStatsCollector::ProduceDataChannelStats_s(
880 int64_t timestamp_us, RTCStatsReport* report) const {
881 RTC_DCHECK(signaling_thread_->IsCurrent());
882 for (const rtc::scoped_refptr<DataChannel>& data_channel :
883 pc_->sctp_data_channels()) {
884 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
885 new RTCDataChannelStats(
886 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
887 timestamp_us));
888 data_channel_stats->label = data_channel->label();
889 data_channel_stats->protocol = data_channel->protocol();
890 data_channel_stats->datachannelid = data_channel->id();
891 data_channel_stats->state =
892 DataStateToRTCDataChannelState(data_channel->state());
893 data_channel_stats->messages_sent = data_channel->messages_sent();
894 data_channel_stats->bytes_sent = data_channel->bytes_sent();
895 data_channel_stats->messages_received = data_channel->messages_received();
896 data_channel_stats->bytes_received = data_channel->bytes_received();
897 report->AddStats(std::move(data_channel_stats));
898 }
899}
900
hbosdf6075a2016-12-19 04:58:02 -0800901void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -0700902 int64_t timestamp_us,
903 const SessionStats& session_stats,
904 const cricket::VideoMediaInfo* video_media_info,
905 const Call::Stats& call_stats,
906 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800907 RTC_DCHECK(network_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700908 for (const auto& transport_stats : session_stats.transport_stats) {
909 for (const auto& channel_stats : transport_stats.second.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800910 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
911 transport_stats.second.transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700912 for (const cricket::ConnectionInfo& info :
913 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700914 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700915 new RTCIceCandidatePairStats(
916 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
917 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700918
hbos0583b282016-11-30 01:50:14 -0800919 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700920 // TODO(hbos): There could be other candidates that are not paired with
921 // anything. We don't have a complete list. Local candidates come from
922 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +0100923 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700924 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800925 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -0700926 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800927 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -0800928 candidate_pair_stats->state =
929 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
930 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -0800931 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -0700932 // TODO(hbos): This writable is different than the spec. It goes to
933 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +0100934 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -0700935 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700936 candidate_pair_stats->bytes_sent =
937 static_cast<uint64_t>(info.sent_total_bytes);
938 candidate_pair_stats->bytes_received =
939 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -0800940 candidate_pair_stats->total_round_trip_time =
941 static_cast<double>(info.total_round_trip_time_ms) /
942 rtc::kNumMillisecsPerSec;
943 if (info.current_round_trip_time_ms) {
944 candidate_pair_stats->current_round_trip_time =
945 static_cast<double>(*info.current_round_trip_time_ms) /
946 rtc::kNumMillisecsPerSec;
947 }
stefanf79ade12017-06-02 06:44:03 -0700948 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -0800949 // The bandwidth estimations we have are for the selected candidate
950 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -0700951 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
952 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
953 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -0800954 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -0700955 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -0800956 }
stefanf79ade12017-06-02 06:44:03 -0700957 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -0800958 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -0700959 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -0800960 }
961 }
hbosd82f5122016-12-09 04:12:39 -0800962 candidate_pair_stats->requests_received =
963 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -0800964 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
965 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700966 candidate_pair_stats->responses_received =
967 static_cast<uint64_t>(info.recv_ping_responses);
968 candidate_pair_stats->responses_sent =
969 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -0800970 RTC_DCHECK_GE(info.sent_ping_requests_total,
971 info.sent_ping_requests_before_first_response);
972 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
973 info.sent_ping_requests_total -
974 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700975
976 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700977 }
978 }
979 }
980}
981
hbos09bc1282016-11-08 06:29:22 -0800982void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s(
983 int64_t timestamp_us, RTCStatsReport* report) const {
984 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos9e302742017-01-20 02:47:10 -0800985 RTC_DCHECK(track_media_info_map_);
Harald Alvestranda3dab842018-01-14 09:18:58 +0100986 ProduceMediaStreamStats(timestamp_us, pc_->GetSenders(), pc_->GetReceivers(),
987 report);
Harald Alvestrand89061872018-01-02 14:08:34 +0100988 ProduceSenderMediaTrackStats(timestamp_us, *track_media_info_map_,
989 pc_->GetSenders(), report);
990 ProduceReceiverMediaTrackStats(timestamp_us, *track_media_info_map_,
991 pc_->GetReceivers(), report);
hbos09bc1282016-11-08 06:29:22 -0800992}
993
hbos6ab97ce2016-10-03 14:16:56 -0700994void RTCStatsCollector::ProducePeerConnectionStats_s(
995 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700996 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700997 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700998 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -0800999 stats->data_channels_opened = internal_record_.data_channels_opened;
1000 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001001 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001002}
1003
hbosdf6075a2016-12-19 04:58:02 -08001004void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001005 int64_t timestamp_us,
1006 const SessionStats& session_stats,
1007 const ChannelNamePairs& channel_name_pairs,
hbos84abeb12017-01-16 06:16:44 -08001008 const TrackMediaInfoMap& track_media_info_map,
1009 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001010 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001011
1012 // Audio
hbos84abeb12017-01-16 06:16:44 -08001013 if (track_media_info_map.voice_media_info()) {
Steve Anton593e3252017-12-15 11:44:48 -08001014 RTC_DCHECK(channel_name_pairs.voice);
1015 const std::string& transport_name =
1016 (*channel_name_pairs.voice).transport_name;
1017 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1018 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
hbos0adb8282016-11-23 02:32:06 -08001019 // Inbound
1020 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
hbos84abeb12017-01-16 06:16:44 -08001021 track_media_info_map.voice_media_info()->receivers) {
hbos0adb8282016-11-23 02:32:06 -08001022 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
1023 // is fixed.
1024 if (voice_receiver_info.ssrc() == 0)
1025 continue;
1026 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
Harald Alvestrandc72af932018-01-11 17:18:19 +01001027 new RTCInboundRTPStreamStats(RTCInboundRTPStreamStatsIDFromSSRC(
1028 true, voice_receiver_info.ssrc()),
1029 timestamp_us));
hbos0adb8282016-11-23 02:32:06 -08001030 SetInboundRTPStreamStatsFromVoiceReceiverInfo(
1031 voice_receiver_info, inbound_audio.get());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001032 // TODO(hta): This lookup should look for the sender, not the track.
hbos84abeb12017-01-16 06:16:44 -08001033 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1034 track_media_info_map_->GetAudioTrack(voice_receiver_info);
1035 if (audio_track) {
1036 RTC_DCHECK(track_to_id_.find(audio_track.get()) != track_to_id_.end());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001037 inbound_audio
Harald Alvestranda3dab842018-01-14 09:18:58 +01001038 ->track_id = RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1039 kReceiver,
Harald Alvestrandc72af932018-01-11 17:18:19 +01001040 track_media_info_map_->GetAttachmentIdByTrack(audio_track).value());
hbos84abeb12017-01-16 06:16:44 -08001041 }
hbos0adb8282016-11-23 02:32:06 -08001042 inbound_audio->transport_id = transport_id;
hbos0adb8282016-11-23 02:32:06 -08001043 report->AddStats(std::move(inbound_audio));
1044 }
1045 // Outbound
1046 for (const cricket::VoiceSenderInfo& voice_sender_info :
hbos84abeb12017-01-16 06:16:44 -08001047 track_media_info_map.voice_media_info()->senders) {
hbos0adb8282016-11-23 02:32:06 -08001048 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
1049 // is fixed.
1050 if (voice_sender_info.ssrc() == 0)
1051 continue;
1052 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
1053 new RTCOutboundRTPStreamStats(
1054 RTCOutboundRTPStreamStatsIDFromSSRC(
1055 true, voice_sender_info.ssrc()),
1056 timestamp_us));
1057 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
1058 voice_sender_info, outbound_audio.get());
hbos84abeb12017-01-16 06:16:44 -08001059 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1060 track_media_info_map_->GetAudioTrack(voice_sender_info);
1061 if (audio_track) {
1062 RTC_DCHECK(track_to_id_.find(audio_track.get()) != track_to_id_.end());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001063 outbound_audio
Harald Alvestranda3dab842018-01-14 09:18:58 +01001064 ->track_id = RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1065 kSender,
Harald Alvestrandc72af932018-01-11 17:18:19 +01001066 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos84abeb12017-01-16 06:16:44 -08001067 }
hbos0adb8282016-11-23 02:32:06 -08001068 outbound_audio->transport_id = transport_id;
hbos0adb8282016-11-23 02:32:06 -08001069 report->AddStats(std::move(outbound_audio));
hbos6ded1902016-11-01 01:50:46 -07001070 }
1071 }
1072 // Video
hbos84abeb12017-01-16 06:16:44 -08001073 if (track_media_info_map.video_media_info()) {
Steve Anton593e3252017-12-15 11:44:48 -08001074 RTC_DCHECK(channel_name_pairs.video);
1075 const std::string& transport_name =
1076 (*channel_name_pairs.video).transport_name;
1077 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1078 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
hbos0adb8282016-11-23 02:32:06 -08001079 // Inbound
1080 for (const cricket::VideoReceiverInfo& video_receiver_info :
hbos84abeb12017-01-16 06:16:44 -08001081 track_media_info_map.video_media_info()->receivers) {
hbos0adb8282016-11-23 02:32:06 -08001082 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
1083 // is fixed.
1084 if (video_receiver_info.ssrc() == 0)
1085 continue;
1086 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
1087 new RTCInboundRTPStreamStats(
1088 RTCInboundRTPStreamStatsIDFromSSRC(
1089 false, video_receiver_info.ssrc()),
1090 timestamp_us));
1091 SetInboundRTPStreamStatsFromVideoReceiverInfo(
1092 video_receiver_info, inbound_video.get());
hbos84abeb12017-01-16 06:16:44 -08001093 rtc::scoped_refptr<VideoTrackInterface> video_track =
1094 track_media_info_map_->GetVideoTrack(video_receiver_info);
1095 if (video_track) {
1096 RTC_DCHECK(track_to_id_.find(video_track.get()) != track_to_id_.end());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001097 inbound_video
Harald Alvestranda3dab842018-01-14 09:18:58 +01001098 ->track_id = RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1099 kReceiver,
Harald Alvestrandc72af932018-01-11 17:18:19 +01001100 track_media_info_map_->GetAttachmentIdByTrack(video_track).value());
hbos84abeb12017-01-16 06:16:44 -08001101 }
hbos0adb8282016-11-23 02:32:06 -08001102 inbound_video->transport_id = transport_id;
hbos0adb8282016-11-23 02:32:06 -08001103 report->AddStats(std::move(inbound_video));
1104 }
1105 // Outbound
1106 for (const cricket::VideoSenderInfo& video_sender_info :
hbos84abeb12017-01-16 06:16:44 -08001107 track_media_info_map.video_media_info()->senders) {
hbos0adb8282016-11-23 02:32:06 -08001108 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
1109 // is fixed.
1110 if (video_sender_info.ssrc() == 0)
1111 continue;
1112 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
Harald Alvestrandc72af932018-01-11 17:18:19 +01001113 new RTCOutboundRTPStreamStats(RTCOutboundRTPStreamStatsIDFromSSRC(
1114 false, video_sender_info.ssrc()),
1115 timestamp_us));
hbos0adb8282016-11-23 02:32:06 -08001116 SetOutboundRTPStreamStatsFromVideoSenderInfo(
1117 video_sender_info, outbound_video.get());
hbos84abeb12017-01-16 06:16:44 -08001118 rtc::scoped_refptr<VideoTrackInterface> video_track =
1119 track_media_info_map_->GetVideoTrack(video_sender_info);
1120 if (video_track) {
1121 RTC_DCHECK(track_to_id_.find(video_track.get()) != track_to_id_.end());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001122 outbound_video
Harald Alvestranda3dab842018-01-14 09:18:58 +01001123 ->track_id = RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1124 kSender,
Harald Alvestrandc72af932018-01-11 17:18:19 +01001125 track_media_info_map_->GetAttachmentIdByTrack(video_track).value());
hbos84abeb12017-01-16 06:16:44 -08001126 }
hbos0adb8282016-11-23 02:32:06 -08001127 outbound_video->transport_id = transport_id;
hbos0adb8282016-11-23 02:32:06 -08001128 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001129 }
1130 }
1131}
1132
hbosdf6075a2016-12-19 04:58:02 -08001133void RTCStatsCollector::ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001134 int64_t timestamp_us, const SessionStats& session_stats,
1135 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1136 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001137 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001138 for (const auto& transport : session_stats.transport_stats) {
1139 // Get reference to RTCP channel, if it exists.
1140 std::string rtcp_transport_stats_id;
1141 for (const auto& channel_stats : transport.second.channel_stats) {
1142 if (channel_stats.component ==
1143 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1144 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
1145 transport.second.transport_name, channel_stats.component);
1146 break;
1147 }
1148 }
1149
1150 // Get reference to local and remote certificates of this transport, if they
1151 // exist.
1152 const auto& certificate_stats_it = transport_cert_stats.find(
1153 transport.second.transport_name);
1154 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1155 std::string local_certificate_id;
1156 if (certificate_stats_it->second.local) {
1157 local_certificate_id = RTCCertificateIDFromFingerprint(
1158 certificate_stats_it->second.local->fingerprint);
1159 }
1160 std::string remote_certificate_id;
1161 if (certificate_stats_it->second.remote) {
1162 remote_certificate_id = RTCCertificateIDFromFingerprint(
1163 certificate_stats_it->second.remote->fingerprint);
1164 }
1165
1166 // There is one transport stats for each channel.
1167 for (const auto& channel_stats : transport.second.channel_stats) {
1168 std::unique_ptr<RTCTransportStats> transport_stats(
1169 new RTCTransportStats(
1170 RTCTransportStatsIDFromTransportChannel(
1171 transport.second.transport_name, channel_stats.component),
1172 timestamp_us));
1173 transport_stats->bytes_sent = 0;
1174 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001175 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1176 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001177 for (const cricket::ConnectionInfo& info :
1178 channel_stats.connection_infos) {
1179 *transport_stats->bytes_sent += info.sent_total_bytes;
1180 *transport_stats->bytes_received += info.recv_total_bytes;
1181 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001182 transport_stats->selected_candidate_pair_id =
1183 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1184 }
1185 }
1186 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1187 !rtcp_transport_stats_id.empty()) {
1188 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1189 }
1190 if (!local_certificate_id.empty())
1191 transport_stats->local_certificate_id = local_certificate_id;
1192 if (!remote_certificate_id.empty())
1193 transport_stats->remote_certificate_id = remote_certificate_id;
1194 report->AddStats(std::move(transport_stats));
1195 }
1196 }
1197}
1198
1199std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001200RTCStatsCollector::PrepareTransportCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001201 const SessionStats& session_stats) const {
hbosdf6075a2016-12-19 04:58:02 -08001202 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001203 std::map<std::string, CertificateStatsPair> transport_cert_stats;
1204 for (const auto& transport_stats : session_stats.transport_stats) {
1205 CertificateStatsPair certificate_stats_pair;
1206 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton978b8762017-09-29 12:15:02 -07001207 if (pc_->GetLocalCertificate(transport_stats.second.transport_name,
1208 &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001209 certificate_stats_pair.local =
1210 local_certificate->ssl_certificate().GetStats();
1211 }
1212 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
Steve Anton978b8762017-09-29 12:15:02 -07001213 pc_->GetRemoteSSLCertificate(transport_stats.second.transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001214 if (remote_certificate) {
1215 certificate_stats_pair.remote = remote_certificate->GetStats();
1216 }
1217 transport_cert_stats.insert(
1218 std::make_pair(transport_stats.second.transport_name,
1219 std::move(certificate_stats_pair)));
1220 }
1221 return transport_cert_stats;
1222}
1223
hbos84abeb12017-01-16 06:16:44 -08001224std::unique_ptr<TrackMediaInfoMap>
1225RTCStatsCollector::PrepareTrackMediaInfoMap_s() const {
hbosdf6075a2016-12-19 04:58:02 -08001226 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos84abeb12017-01-16 06:16:44 -08001227 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
Steve Anton978b8762017-09-29 12:15:02 -07001228 if (pc_->voice_channel()) {
hbos84abeb12017-01-16 06:16:44 -08001229 voice_media_info.reset(new cricket::VoiceMediaInfo());
Steve Anton978b8762017-09-29 12:15:02 -07001230 if (!pc_->voice_channel()->GetStats(voice_media_info.get())) {
hbos84abeb12017-01-16 06:16:44 -08001231 voice_media_info.reset();
hbos0adb8282016-11-23 02:32:06 -08001232 }
1233 }
hbos84abeb12017-01-16 06:16:44 -08001234 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
Steve Anton978b8762017-09-29 12:15:02 -07001235 if (pc_->video_channel()) {
hbos84abeb12017-01-16 06:16:44 -08001236 video_media_info.reset(new cricket::VideoMediaInfo());
Steve Anton978b8762017-09-29 12:15:02 -07001237 if (!pc_->video_channel()->GetStats(video_media_info.get())) {
hbos84abeb12017-01-16 06:16:44 -08001238 video_media_info.reset();
hbos0adb8282016-11-23 02:32:06 -08001239 }
1240 }
hbos84abeb12017-01-16 06:16:44 -08001241 std::unique_ptr<TrackMediaInfoMap> track_media_info_map(
1242 new TrackMediaInfoMap(std::move(voice_media_info),
1243 std::move(video_media_info),
1244 pc_->GetSenders(),
1245 pc_->GetReceivers()));
1246 return track_media_info_map;
1247}
1248
1249std::map<MediaStreamTrackInterface*, std::string>
1250RTCStatsCollector::PrepareTrackToID_s() const {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001251 // TODO(hta): Remove this method, and vector its callers via
1252 // senders / receivers instead.
1253 // It ignores tracks that are multiply connected to the PC.
hbos84abeb12017-01-16 06:16:44 -08001254 RTC_DCHECK(signaling_thread_->IsCurrent());
1255 std::map<MediaStreamTrackInterface*, std::string> track_to_id;
hbos7b0c6fa2017-07-12 16:22:34 -07001256 for (auto sender : pc_->GetSenders()) {
1257 auto track = sender->track();
1258 if (track)
1259 track_to_id[track.get()] = track->id();
1260 }
1261 for (auto receiver : pc_->GetReceivers()) {
1262 auto track = receiver->track();
1263 if (track)
1264 track_to_id[track.get()] = track->id();
hbos84abeb12017-01-16 06:16:44 -08001265 }
1266 return track_to_id;
hbos0adb8282016-11-23 02:32:06 -08001267}
1268
hbos82ebe022016-11-14 01:41:09 -08001269void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1270 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1271 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1272}
1273
1274void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1275 RTC_DCHECK(signaling_thread_->IsCurrent());
1276 bool result = internal_record_.opened_data_channels.insert(
1277 reinterpret_cast<uintptr_t>(channel)).second;
1278 ++internal_record_.data_channels_opened;
1279 RTC_DCHECK(result);
1280}
1281
1282void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1283 RTC_DCHECK(signaling_thread_->IsCurrent());
1284 // Only channels that have been fully opened (and have increased the
1285 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001286 if (internal_record_.opened_data_channels.erase(
1287 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001288 ++internal_record_.data_channels_closed;
1289 }
1290}
1291
hboscc555c52016-10-18 12:48:31 -07001292const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1293 const std::string& type) {
1294 return CandidateTypeToRTCIceCandidateType(type);
1295}
1296
1297const char* DataStateToRTCDataChannelStateForTesting(
1298 DataChannelInterface::DataState state) {
1299 return DataStateToRTCDataChannelState(state);
1300}
1301
hbosd565b732016-08-30 14:04:35 -07001302} // namespace webrtc