blob: ff68f37aaed1982c6e2e7ca6fa640befbcc00e0b [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) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100513 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
514 AudioTrackInterface* track =
515 static_cast<AudioTrackInterface*>(sender->track().get());
516 if (!track)
517 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100518 cricket::VoiceSenderInfo null_sender_info;
519 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
520 // TODO(hta): Checking on ssrc is not proper. There should be a way
521 // to see from a sender whether it's connected or not.
522 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
523 if (sender->ssrc()) {
524 voice_sender_info =
525 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
526 }
527
Harald Alvestrand89061872018-01-02 14:08:34 +0100528 RTC_CHECK(voice_sender_info)
529 << "No voice sender info for sender with ssrc " << sender->ssrc();
530 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100531 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
532 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100533 report->AddStats(std::move(audio_track_stats));
534 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
535 VideoTrackInterface* track =
536 static_cast<VideoTrackInterface*>(sender->track().get());
537 if (!track)
538 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100539 cricket::VideoSenderInfo null_sender_info;
540 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
541 // TODO(hta): Check on state not ssrc when state is available
542 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
543 if (sender->ssrc())
544 video_sender_info =
545 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand89061872018-01-02 14:08:34 +0100546 RTC_CHECK(video_sender_info)
547 << "No video sender info for sender with ssrc " << sender->ssrc();
548 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100549 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
550 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100551 report->AddStats(std::move(video_track_stats));
552 } else {
553 RTC_NOTREACHED();
554 }
555 }
556}
557
558void ProduceReceiverMediaTrackStats(
559 int64_t timestamp_us,
560 const TrackMediaInfoMap& track_media_info_map,
561 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers,
562 RTCStatsReport* report) {
563 // This function iterates over the receivers to find the remote tracks.
564 for (auto const receiver : receivers) {
565 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
566 AudioTrackInterface* track =
567 static_cast<AudioTrackInterface*>(receiver->track().get());
568 const cricket::VoiceReceiverInfo* voice_receiver_info =
569 track_media_info_map.GetVoiceReceiverInfo(*track);
570 if (!voice_receiver_info) {
571 continue;
572 }
573 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
574 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100575 timestamp_us, *track, *voice_receiver_info,
576 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100577 report->AddStats(std::move(audio_track_stats));
578 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
579 VideoTrackInterface* track =
580 static_cast<VideoTrackInterface*>(receiver->track().get());
581 const cricket::VideoReceiverInfo* video_receiver_info =
582 track_media_info_map.GetVideoReceiverInfo(*track);
583 if (!video_receiver_info) {
584 continue;
585 }
586 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
587 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100588 timestamp_us, *track, *video_receiver_info,
589 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100590 report->AddStats(std::move(video_track_stats));
591 } else {
592 RTC_NOTREACHED();
593 }
594 }
595}
596
597void ProduceMediaStreamStats(
hbos09bc1282016-11-08 06:29:22 -0800598 int64_t timestamp_us,
Harald Alvestranda3dab842018-01-14 09:18:58 +0100599 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& rtp_senders,
600 const std::vector<rtc::scoped_refptr<RtpReceiverInterface>>& rtp_receivers,
hbos09bc1282016-11-08 06:29:22 -0800601 RTCStatsReport* report) {
Harald Alvestranda3dab842018-01-14 09:18:58 +0100602 std::map<std::string, std::vector<std::string>> track_ids;
hbos09bc1282016-11-08 06:29:22 -0800603
Harald Alvestranda3dab842018-01-14 09:18:58 +0100604 for (auto& sender : rtp_senders) {
605 std::string track_id = RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
606 kSender, sender->AttachmentId());
607 for (auto& stream_id : sender->stream_ids()) {
608 track_ids[stream_id].push_back(track_id);
hbos09bc1282016-11-08 06:29:22 -0800609 }
Harald Alvestranda3dab842018-01-14 09:18:58 +0100610 }
611 for (auto& receiver : rtp_receivers) {
612 std::string track_id = RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
613 kReceiver, receiver->AttachmentId());
614 for (auto& stream : receiver->streams()) {
615 track_ids[stream->label()].push_back(track_id);
616 }
617 }
618
619 // Build stats for each stream ID known.
620 for (auto& it : track_ids) {
621 std::unique_ptr<RTCMediaStreamStats> stream_stats(
622 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
623 stream_stats->stream_identifier = it.first;
624 stream_stats->track_ids = it.second;
hbos09bc1282016-11-08 06:29:22 -0800625 report->AddStats(std::move(stream_stats));
626 }
627}
628
hboscc555c52016-10-18 12:48:31 -0700629} // namespace
630
hbosc82f2e12016-09-05 01:36:50 -0700631rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
632 PeerConnection* pc, int64_t cache_lifetime_us) {
633 return rtc::scoped_refptr<RTCStatsCollector>(
634 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
635}
636
637RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
638 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700639 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700640 signaling_thread_(pc->signaling_thread()),
641 worker_thread_(pc->worker_thread()),
642 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700643 num_pending_partial_reports_(0),
644 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700645 cache_timestamp_us_(0),
646 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700647 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700648 RTC_DCHECK(signaling_thread_);
649 RTC_DCHECK(worker_thread_);
650 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700651 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbos82ebe022016-11-14 01:41:09 -0800652 pc_->SignalDataChannelCreated.connect(
653 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700654}
655
hbosb78306a2016-12-19 05:06:57 -0800656RTCStatsCollector::~RTCStatsCollector() {
657 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
658}
659
hbosc82f2e12016-09-05 01:36:50 -0700660void RTCStatsCollector::GetStatsReport(
661 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
662 RTC_DCHECK(signaling_thread_->IsCurrent());
663 RTC_DCHECK(callback);
664 callbacks_.push_back(callback);
665
hbos0e6758d2016-08-31 07:57:36 -0700666 // "Now" using a monotonically increasing timer.
667 int64_t cache_now_us = rtc::TimeMicros();
668 if (cached_report_ &&
669 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700670 // We have a fresh cached report to deliver.
671 DeliverCachedReport();
672 } else if (!num_pending_partial_reports_) {
673 // Only start gathering stats if we're not already gathering stats. In the
674 // case of already gathering stats, |callback_| will be invoked when there
675 // are no more pending partial reports.
676
677 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
678 // UTC), in microseconds. The system clock could be modified and is not
679 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700680 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700681
hbosf415f8a2017-01-02 04:28:51 -0800682 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700683 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800684
hbos84abeb12017-01-16 06:16:44 -0800685 // Prepare |channel_name_pairs_| for use in
hbosdf6075a2016-12-19 04:58:02 -0800686 // |ProducePartialResultsOnNetworkThread|.
687 channel_name_pairs_.reset(new ChannelNamePairs());
Steve Anton978b8762017-09-29 12:15:02 -0700688 if (pc_->voice_channel()) {
Oskar Sundbom89e71262017-11-16 10:56:31 +0100689 channel_name_pairs_->voice =
Steve Anton978b8762017-09-29 12:15:02 -0700690 ChannelNamePair(pc_->voice_channel()->content_name(),
Oskar Sundbom89e71262017-11-16 10:56:31 +0100691 pc_->voice_channel()->transport_name());
hbosdf6075a2016-12-19 04:58:02 -0800692 }
Steve Anton978b8762017-09-29 12:15:02 -0700693 if (pc_->video_channel()) {
Oskar Sundbom89e71262017-11-16 10:56:31 +0100694 channel_name_pairs_->video =
Steve Anton978b8762017-09-29 12:15:02 -0700695 ChannelNamePair(pc_->video_channel()->content_name(),
Oskar Sundbom89e71262017-11-16 10:56:31 +0100696 pc_->video_channel()->transport_name());
hbosdf6075a2016-12-19 04:58:02 -0800697 }
Steve Anton978b8762017-09-29 12:15:02 -0700698 if (pc_->rtp_data_channel()) {
Oskar Sundbom89e71262017-11-16 10:56:31 +0100699 channel_name_pairs_->data =
Steve Anton978b8762017-09-29 12:15:02 -0700700 ChannelNamePair(pc_->rtp_data_channel()->content_name(),
Oskar Sundbom89e71262017-11-16 10:56:31 +0100701 pc_->rtp_data_channel()->transport_name());
Steve Anton978b8762017-09-29 12:15:02 -0700702 }
703 if (pc_->sctp_content_name()) {
Oskar Sundbom89e71262017-11-16 10:56:31 +0100704 channel_name_pairs_->data = ChannelNamePair(*pc_->sctp_content_name(),
705 *pc_->sctp_transport_name());
hbosdf6075a2016-12-19 04:58:02 -0800706 }
hbos84abeb12017-01-16 06:16:44 -0800707 // Prepare |track_media_info_map_| for use in
708 // |ProducePartialResultsOnNetworkThread| and
709 // |ProducePartialResultsOnSignalingThread|.
710 track_media_info_map_.reset(PrepareTrackMediaInfoMap_s().release());
711 // Prepare |track_to_id_| for use in |ProducePartialResultsOnNetworkThread|.
712 // This avoids a possible deadlock if |MediaStreamTrackInterface::id| is
713 // implemented to invoke on the signaling thread.
714 track_to_id_ = PrepareTrackToID_s();
hbosf415f8a2017-01-02 04:28:51 -0800715
stefanf79ade12017-06-02 06:44:03 -0700716 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
717 // thread.
718 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
719 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700720 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700721
722 invoker_.AsyncInvoke<void>(
723 RTC_FROM_HERE, network_thread_,
hbosc82f2e12016-09-05 01:36:50 -0700724 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
stefanf79ade12017-06-02 06:44:03 -0700725 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800726 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700727 }
hbosd565b732016-08-30 14:04:35 -0700728}
729
730void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700731 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700732 cached_report_ = nullptr;
733}
734
hbosb78306a2016-12-19 05:06:57 -0800735void RTCStatsCollector::WaitForPendingRequest() {
736 RTC_DCHECK(signaling_thread_->IsCurrent());
737 if (num_pending_partial_reports_) {
738 rtc::Thread::Current()->ProcessMessages(0);
739 while (num_pending_partial_reports_) {
740 rtc::Thread::Current()->SleepMs(1);
741 rtc::Thread::Current()->ProcessMessages(0);
742 }
743 }
744}
745
hbosc82f2e12016-09-05 01:36:50 -0700746void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
747 int64_t timestamp_us) {
748 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700749 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
750 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700751
hboscc555c52016-10-18 12:48:31 -0700752 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos09bc1282016-11-08 06:29:22 -0800753 ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700754 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700755
756 AddPartialResults(report);
757}
758
hbosc82f2e12016-09-05 01:36:50 -0700759void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
760 int64_t timestamp_us) {
761 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700762 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
763 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700764
hbosdf6075a2016-12-19 04:58:02 -0800765 std::unique_ptr<SessionStats> session_stats =
Steve Anton978b8762017-09-29 12:15:02 -0700766 pc_->GetSessionStats(*channel_name_pairs_);
hbosdf6075a2016-12-19 04:58:02 -0800767 if (session_stats) {
768 std::map<std::string, CertificateStatsPair> transport_cert_stats =
769 PrepareTransportCertificateStats_n(*session_stats);
770
771 ProduceCertificateStats_n(
772 timestamp_us, transport_cert_stats, report.get());
773 ProduceCodecStats_n(
hbos84abeb12017-01-16 06:16:44 -0800774 timestamp_us, *track_media_info_map_, report.get());
stefanf79ade12017-06-02 06:44:03 -0700775 ProduceIceCandidateAndPairStats_n(timestamp_us, *session_stats,
776 track_media_info_map_->video_media_info(),
777 call_stats_, report.get());
Steve Anton593e3252017-12-15 11:44:48 -0800778 ProduceRTPStreamStats_n(timestamp_us, *session_stats, *channel_name_pairs_,
779 *track_media_info_map_, report.get());
hbosdf6075a2016-12-19 04:58:02 -0800780 ProduceTransportStats_n(
781 timestamp_us, *session_stats, transport_cert_stats, report.get());
782 }
hbosc82f2e12016-09-05 01:36:50 -0700783
784 AddPartialResults(report);
785}
786
787void RTCStatsCollector::AddPartialResults(
788 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
789 if (!signaling_thread_->IsCurrent()) {
790 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
791 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
792 rtc::scoped_refptr<RTCStatsCollector>(this),
793 partial_report));
794 return;
795 }
796 AddPartialResults_s(partial_report);
797}
798
799void RTCStatsCollector::AddPartialResults_s(
800 rtc::scoped_refptr<RTCStatsReport> partial_report) {
801 RTC_DCHECK(signaling_thread_->IsCurrent());
802 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
803 if (!partial_report_)
804 partial_report_ = partial_report;
805 else
806 partial_report_->TakeMembersFrom(partial_report);
807 --num_pending_partial_reports_;
808 if (!num_pending_partial_reports_) {
809 cache_timestamp_us_ = partial_report_timestamp_us_;
810 cached_report_ = partial_report_;
811 partial_report_ = nullptr;
hbos84abeb12017-01-16 06:16:44 -0800812 channel_name_pairs_.reset();
813 track_media_info_map_.reset();
814 track_to_id_.clear();
ehmaldonadoa26196b2017-07-18 03:30:29 -0700815 // Trace WebRTC Stats when getStats is called on Javascript.
816 // This allows access to WebRTC stats from trace logs. To enable them,
817 // select the "webrtc_stats" category when recording traces.
ehmaldonado8ab0fd82017-09-04 14:35:04 -0700818 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
819 cached_report_->ToJson());
hbosc82f2e12016-09-05 01:36:50 -0700820 DeliverCachedReport();
821 }
822}
823
824void RTCStatsCollector::DeliverCachedReport() {
825 RTC_DCHECK(signaling_thread_->IsCurrent());
826 RTC_DCHECK(!callbacks_.empty());
827 RTC_DCHECK(cached_report_);
828 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
829 callbacks_) {
830 callback->OnStatsDelivered(cached_report_);
831 }
832 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700833}
834
hbosdf6075a2016-12-19 04:58:02 -0800835void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700836 int64_t timestamp_us,
837 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700838 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800839 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700840 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
841 if (transport_cert_stats_pair.second.local) {
842 ProduceCertificateStatsFromSSLCertificateStats(
843 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700844 }
hbos02ba2112016-10-28 05:14:53 -0700845 if (transport_cert_stats_pair.second.remote) {
846 ProduceCertificateStatsFromSSLCertificateStats(
847 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700848 }
849 }
850}
851
hbosdf6075a2016-12-19 04:58:02 -0800852void RTCStatsCollector::ProduceCodecStats_n(
hbos84abeb12017-01-16 06:16:44 -0800853 int64_t timestamp_us, const TrackMediaInfoMap& track_media_info_map,
hbos0adb8282016-11-23 02:32:06 -0800854 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800855 RTC_DCHECK(network_thread_->IsCurrent());
hbos0adb8282016-11-23 02:32:06 -0800856 // Audio
hbos84abeb12017-01-16 06:16:44 -0800857 if (track_media_info_map.voice_media_info()) {
hbos0adb8282016-11-23 02:32:06 -0800858 // Inbound
hbos84abeb12017-01-16 06:16:44 -0800859 for (const auto& pair :
860 track_media_info_map.voice_media_info()->receive_codecs) {
hbos0adb8282016-11-23 02:32:06 -0800861 report->AddStats(CodecStatsFromRtpCodecParameters(
862 timestamp_us, true, true, pair.second));
863 }
864 // Outbound
hbos84abeb12017-01-16 06:16:44 -0800865 for (const auto& pair :
866 track_media_info_map.voice_media_info()->send_codecs) {
hbos0adb8282016-11-23 02:32:06 -0800867 report->AddStats(CodecStatsFromRtpCodecParameters(
868 timestamp_us, false, true, pair.second));
869 }
870 }
871 // Video
hbos84abeb12017-01-16 06:16:44 -0800872 if (track_media_info_map.video_media_info()) {
hbos0adb8282016-11-23 02:32:06 -0800873 // Inbound
hbos84abeb12017-01-16 06:16:44 -0800874 for (const auto& pair :
875 track_media_info_map.video_media_info()->receive_codecs) {
hbos0adb8282016-11-23 02:32:06 -0800876 report->AddStats(CodecStatsFromRtpCodecParameters(
877 timestamp_us, true, false, pair.second));
878 }
879 // Outbound
hbos84abeb12017-01-16 06:16:44 -0800880 for (const auto& pair :
881 track_media_info_map.video_media_info()->send_codecs) {
hbos0adb8282016-11-23 02:32:06 -0800882 report->AddStats(CodecStatsFromRtpCodecParameters(
883 timestamp_us, false, false, pair.second));
884 }
885 }
886}
887
hboscc555c52016-10-18 12:48:31 -0700888void RTCStatsCollector::ProduceDataChannelStats_s(
889 int64_t timestamp_us, RTCStatsReport* report) const {
890 RTC_DCHECK(signaling_thread_->IsCurrent());
891 for (const rtc::scoped_refptr<DataChannel>& data_channel :
892 pc_->sctp_data_channels()) {
893 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
894 new RTCDataChannelStats(
895 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
896 timestamp_us));
897 data_channel_stats->label = data_channel->label();
898 data_channel_stats->protocol = data_channel->protocol();
899 data_channel_stats->datachannelid = data_channel->id();
900 data_channel_stats->state =
901 DataStateToRTCDataChannelState(data_channel->state());
902 data_channel_stats->messages_sent = data_channel->messages_sent();
903 data_channel_stats->bytes_sent = data_channel->bytes_sent();
904 data_channel_stats->messages_received = data_channel->messages_received();
905 data_channel_stats->bytes_received = data_channel->bytes_received();
906 report->AddStats(std::move(data_channel_stats));
907 }
908}
909
hbosdf6075a2016-12-19 04:58:02 -0800910void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -0700911 int64_t timestamp_us,
912 const SessionStats& session_stats,
913 const cricket::VideoMediaInfo* video_media_info,
914 const Call::Stats& call_stats,
915 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800916 RTC_DCHECK(network_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700917 for (const auto& transport_stats : session_stats.transport_stats) {
918 for (const auto& channel_stats : transport_stats.second.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800919 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
920 transport_stats.second.transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700921 for (const cricket::ConnectionInfo& info :
922 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700923 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700924 new RTCIceCandidatePairStats(
925 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
926 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700927
hbos0583b282016-11-30 01:50:14 -0800928 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700929 // TODO(hbos): There could be other candidates that are not paired with
930 // anything. We don't have a complete list. Local candidates come from
931 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +0100932 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700933 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800934 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -0700935 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800936 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -0800937 candidate_pair_stats->state =
938 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
939 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -0800940 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -0700941 // TODO(hbos): This writable is different than the spec. It goes to
942 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +0100943 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -0700944 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700945 candidate_pair_stats->bytes_sent =
946 static_cast<uint64_t>(info.sent_total_bytes);
947 candidate_pair_stats->bytes_received =
948 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -0800949 candidate_pair_stats->total_round_trip_time =
950 static_cast<double>(info.total_round_trip_time_ms) /
951 rtc::kNumMillisecsPerSec;
952 if (info.current_round_trip_time_ms) {
953 candidate_pair_stats->current_round_trip_time =
954 static_cast<double>(*info.current_round_trip_time_ms) /
955 rtc::kNumMillisecsPerSec;
956 }
stefanf79ade12017-06-02 06:44:03 -0700957 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -0800958 // The bandwidth estimations we have are for the selected candidate
959 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -0700960 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
961 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
962 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -0800963 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -0700964 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -0800965 }
stefanf79ade12017-06-02 06:44:03 -0700966 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -0800967 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -0700968 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -0800969 }
970 }
hbosd82f5122016-12-09 04:12:39 -0800971 candidate_pair_stats->requests_received =
972 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -0800973 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
974 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700975 candidate_pair_stats->responses_received =
976 static_cast<uint64_t>(info.recv_ping_responses);
977 candidate_pair_stats->responses_sent =
978 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -0800979 RTC_DCHECK_GE(info.sent_ping_requests_total,
980 info.sent_ping_requests_before_first_response);
981 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
982 info.sent_ping_requests_total -
983 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700984
985 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700986 }
987 }
988 }
989}
990
hbos09bc1282016-11-08 06:29:22 -0800991void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s(
992 int64_t timestamp_us, RTCStatsReport* report) const {
993 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos9e302742017-01-20 02:47:10 -0800994 RTC_DCHECK(track_media_info_map_);
Harald Alvestranda3dab842018-01-14 09:18:58 +0100995 ProduceMediaStreamStats(timestamp_us, pc_->GetSenders(), pc_->GetReceivers(),
996 report);
Harald Alvestrand89061872018-01-02 14:08:34 +0100997 ProduceSenderMediaTrackStats(timestamp_us, *track_media_info_map_,
998 pc_->GetSenders(), report);
999 ProduceReceiverMediaTrackStats(timestamp_us, *track_media_info_map_,
1000 pc_->GetReceivers(), report);
hbos09bc1282016-11-08 06:29:22 -08001001}
1002
hbos6ab97ce2016-10-03 14:16:56 -07001003void RTCStatsCollector::ProducePeerConnectionStats_s(
1004 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001005 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001006 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001007 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001008 stats->data_channels_opened = internal_record_.data_channels_opened;
1009 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001010 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001011}
1012
hbosdf6075a2016-12-19 04:58:02 -08001013void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001014 int64_t timestamp_us,
1015 const SessionStats& session_stats,
1016 const ChannelNamePairs& channel_name_pairs,
hbos84abeb12017-01-16 06:16:44 -08001017 const TrackMediaInfoMap& track_media_info_map,
1018 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001019 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001020
1021 // Audio
hbos84abeb12017-01-16 06:16:44 -08001022 if (track_media_info_map.voice_media_info()) {
Steve Anton593e3252017-12-15 11:44:48 -08001023 RTC_DCHECK(channel_name_pairs.voice);
1024 const std::string& transport_name =
1025 (*channel_name_pairs.voice).transport_name;
1026 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1027 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
hbos0adb8282016-11-23 02:32:06 -08001028 // Inbound
1029 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
hbos84abeb12017-01-16 06:16:44 -08001030 track_media_info_map.voice_media_info()->receivers) {
Harald Alvestrandb8e12012018-01-23 15:28:16 +01001031 if (!voice_receiver_info.connected())
hbos0adb8282016-11-23 02:32:06 -08001032 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +01001033
hbos0adb8282016-11-23 02:32:06 -08001034 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
Harald Alvestrandc72af932018-01-11 17:18:19 +01001035 new RTCInboundRTPStreamStats(RTCInboundRTPStreamStatsIDFromSSRC(
1036 true, voice_receiver_info.ssrc()),
1037 timestamp_us));
Harald Alvestrandb8e12012018-01-23 15:28:16 +01001038 SetInboundRTPStreamStatsFromVoiceReceiverInfo(voice_receiver_info,
1039 inbound_audio.get());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001040 // TODO(hta): This lookup should look for the sender, not the track.
hbos84abeb12017-01-16 06:16:44 -08001041 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1042 track_media_info_map_->GetAudioTrack(voice_receiver_info);
1043 if (audio_track) {
1044 RTC_DCHECK(track_to_id_.find(audio_track.get()) != track_to_id_.end());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001045 inbound_audio
Harald Alvestranda3dab842018-01-14 09:18:58 +01001046 ->track_id = RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1047 kReceiver,
Harald Alvestrandc72af932018-01-11 17:18:19 +01001048 track_media_info_map_->GetAttachmentIdByTrack(audio_track).value());
hbos84abeb12017-01-16 06:16:44 -08001049 }
hbos0adb8282016-11-23 02:32:06 -08001050 inbound_audio->transport_id = transport_id;
hbos0adb8282016-11-23 02:32:06 -08001051 report->AddStats(std::move(inbound_audio));
1052 }
1053 // Outbound
1054 for (const cricket::VoiceSenderInfo& voice_sender_info :
hbos84abeb12017-01-16 06:16:44 -08001055 track_media_info_map.voice_media_info()->senders) {
Harald Alvestrandb8e12012018-01-23 15:28:16 +01001056 if (!voice_sender_info.connected())
hbos0adb8282016-11-23 02:32:06 -08001057 continue;
1058 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
1059 new RTCOutboundRTPStreamStats(
1060 RTCOutboundRTPStreamStatsIDFromSSRC(
1061 true, voice_sender_info.ssrc()),
1062 timestamp_us));
1063 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
1064 voice_sender_info, outbound_audio.get());
hbos84abeb12017-01-16 06:16:44 -08001065 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1066 track_media_info_map_->GetAudioTrack(voice_sender_info);
1067 if (audio_track) {
1068 RTC_DCHECK(track_to_id_.find(audio_track.get()) != track_to_id_.end());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001069 outbound_audio
Harald Alvestranda3dab842018-01-14 09:18:58 +01001070 ->track_id = RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1071 kSender,
Harald Alvestrandc72af932018-01-11 17:18:19 +01001072 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos84abeb12017-01-16 06:16:44 -08001073 }
hbos0adb8282016-11-23 02:32:06 -08001074 outbound_audio->transport_id = transport_id;
hbos0adb8282016-11-23 02:32:06 -08001075 report->AddStats(std::move(outbound_audio));
hbos6ded1902016-11-01 01:50:46 -07001076 }
1077 }
1078 // Video
hbos84abeb12017-01-16 06:16:44 -08001079 if (track_media_info_map.video_media_info()) {
Steve Anton593e3252017-12-15 11:44:48 -08001080 RTC_DCHECK(channel_name_pairs.video);
1081 const std::string& transport_name =
1082 (*channel_name_pairs.video).transport_name;
1083 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1084 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
hbos0adb8282016-11-23 02:32:06 -08001085 // Inbound
1086 for (const cricket::VideoReceiverInfo& video_receiver_info :
hbos84abeb12017-01-16 06:16:44 -08001087 track_media_info_map.video_media_info()->receivers) {
hbos0adb8282016-11-23 02:32:06 -08001088 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
1089 // is fixed.
Harald Alvestrandb8e12012018-01-23 15:28:16 +01001090 if (!video_receiver_info.connected())
hbos0adb8282016-11-23 02:32:06 -08001091 continue;
1092 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
1093 new RTCInboundRTPStreamStats(
1094 RTCInboundRTPStreamStatsIDFromSSRC(
1095 false, video_receiver_info.ssrc()),
1096 timestamp_us));
1097 SetInboundRTPStreamStatsFromVideoReceiverInfo(
1098 video_receiver_info, inbound_video.get());
hbos84abeb12017-01-16 06:16:44 -08001099 rtc::scoped_refptr<VideoTrackInterface> video_track =
1100 track_media_info_map_->GetVideoTrack(video_receiver_info);
1101 if (video_track) {
1102 RTC_DCHECK(track_to_id_.find(video_track.get()) != track_to_id_.end());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001103 inbound_video
Harald Alvestranda3dab842018-01-14 09:18:58 +01001104 ->track_id = RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1105 kReceiver,
Harald Alvestrandc72af932018-01-11 17:18:19 +01001106 track_media_info_map_->GetAttachmentIdByTrack(video_track).value());
hbos84abeb12017-01-16 06:16:44 -08001107 }
hbos0adb8282016-11-23 02:32:06 -08001108 inbound_video->transport_id = transport_id;
hbos0adb8282016-11-23 02:32:06 -08001109 report->AddStats(std::move(inbound_video));
1110 }
1111 // Outbound
1112 for (const cricket::VideoSenderInfo& video_sender_info :
hbos84abeb12017-01-16 06:16:44 -08001113 track_media_info_map.video_media_info()->senders) {
Harald Alvestrandb8e12012018-01-23 15:28:16 +01001114 if (!video_sender_info.connected())
hbos0adb8282016-11-23 02:32:06 -08001115 continue;
1116 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
Harald Alvestrandc72af932018-01-11 17:18:19 +01001117 new RTCOutboundRTPStreamStats(RTCOutboundRTPStreamStatsIDFromSSRC(
1118 false, video_sender_info.ssrc()),
1119 timestamp_us));
hbos0adb8282016-11-23 02:32:06 -08001120 SetOutboundRTPStreamStatsFromVideoSenderInfo(
1121 video_sender_info, outbound_video.get());
hbos84abeb12017-01-16 06:16:44 -08001122 rtc::scoped_refptr<VideoTrackInterface> video_track =
1123 track_media_info_map_->GetVideoTrack(video_sender_info);
1124 if (video_track) {
1125 RTC_DCHECK(track_to_id_.find(video_track.get()) != track_to_id_.end());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001126 outbound_video
Harald Alvestranda3dab842018-01-14 09:18:58 +01001127 ->track_id = RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1128 kSender,
Harald Alvestrandc72af932018-01-11 17:18:19 +01001129 track_media_info_map_->GetAttachmentIdByTrack(video_track).value());
hbos84abeb12017-01-16 06:16:44 -08001130 }
hbos0adb8282016-11-23 02:32:06 -08001131 outbound_video->transport_id = transport_id;
hbos0adb8282016-11-23 02:32:06 -08001132 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001133 }
1134 }
1135}
1136
hbosdf6075a2016-12-19 04:58:02 -08001137void RTCStatsCollector::ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001138 int64_t timestamp_us, const SessionStats& session_stats,
1139 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1140 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001141 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001142 for (const auto& transport : session_stats.transport_stats) {
1143 // Get reference to RTCP channel, if it exists.
1144 std::string rtcp_transport_stats_id;
1145 for (const auto& channel_stats : transport.second.channel_stats) {
1146 if (channel_stats.component ==
1147 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1148 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
1149 transport.second.transport_name, channel_stats.component);
1150 break;
1151 }
1152 }
1153
1154 // Get reference to local and remote certificates of this transport, if they
1155 // exist.
1156 const auto& certificate_stats_it = transport_cert_stats.find(
1157 transport.second.transport_name);
1158 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1159 std::string local_certificate_id;
1160 if (certificate_stats_it->second.local) {
1161 local_certificate_id = RTCCertificateIDFromFingerprint(
1162 certificate_stats_it->second.local->fingerprint);
1163 }
1164 std::string remote_certificate_id;
1165 if (certificate_stats_it->second.remote) {
1166 remote_certificate_id = RTCCertificateIDFromFingerprint(
1167 certificate_stats_it->second.remote->fingerprint);
1168 }
1169
1170 // There is one transport stats for each channel.
1171 for (const auto& channel_stats : transport.second.channel_stats) {
1172 std::unique_ptr<RTCTransportStats> transport_stats(
1173 new RTCTransportStats(
1174 RTCTransportStatsIDFromTransportChannel(
1175 transport.second.transport_name, channel_stats.component),
1176 timestamp_us));
1177 transport_stats->bytes_sent = 0;
1178 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001179 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1180 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001181 for (const cricket::ConnectionInfo& info :
1182 channel_stats.connection_infos) {
1183 *transport_stats->bytes_sent += info.sent_total_bytes;
1184 *transport_stats->bytes_received += info.recv_total_bytes;
1185 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001186 transport_stats->selected_candidate_pair_id =
1187 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1188 }
1189 }
1190 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1191 !rtcp_transport_stats_id.empty()) {
1192 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1193 }
1194 if (!local_certificate_id.empty())
1195 transport_stats->local_certificate_id = local_certificate_id;
1196 if (!remote_certificate_id.empty())
1197 transport_stats->remote_certificate_id = remote_certificate_id;
1198 report->AddStats(std::move(transport_stats));
1199 }
1200 }
1201}
1202
1203std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001204RTCStatsCollector::PrepareTransportCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001205 const SessionStats& session_stats) const {
hbosdf6075a2016-12-19 04:58:02 -08001206 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001207 std::map<std::string, CertificateStatsPair> transport_cert_stats;
1208 for (const auto& transport_stats : session_stats.transport_stats) {
1209 CertificateStatsPair certificate_stats_pair;
1210 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton978b8762017-09-29 12:15:02 -07001211 if (pc_->GetLocalCertificate(transport_stats.second.transport_name,
1212 &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001213 certificate_stats_pair.local =
1214 local_certificate->ssl_certificate().GetStats();
1215 }
1216 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
Steve Anton978b8762017-09-29 12:15:02 -07001217 pc_->GetRemoteSSLCertificate(transport_stats.second.transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001218 if (remote_certificate) {
1219 certificate_stats_pair.remote = remote_certificate->GetStats();
1220 }
1221 transport_cert_stats.insert(
1222 std::make_pair(transport_stats.second.transport_name,
1223 std::move(certificate_stats_pair)));
1224 }
1225 return transport_cert_stats;
1226}
1227
hbos84abeb12017-01-16 06:16:44 -08001228std::unique_ptr<TrackMediaInfoMap>
1229RTCStatsCollector::PrepareTrackMediaInfoMap_s() const {
hbosdf6075a2016-12-19 04:58:02 -08001230 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos84abeb12017-01-16 06:16:44 -08001231 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
Steve Anton978b8762017-09-29 12:15:02 -07001232 if (pc_->voice_channel()) {
hbos84abeb12017-01-16 06:16:44 -08001233 voice_media_info.reset(new cricket::VoiceMediaInfo());
Steve Anton978b8762017-09-29 12:15:02 -07001234 if (!pc_->voice_channel()->GetStats(voice_media_info.get())) {
hbos84abeb12017-01-16 06:16:44 -08001235 voice_media_info.reset();
hbos0adb8282016-11-23 02:32:06 -08001236 }
1237 }
hbos84abeb12017-01-16 06:16:44 -08001238 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
Steve Anton978b8762017-09-29 12:15:02 -07001239 if (pc_->video_channel()) {
hbos84abeb12017-01-16 06:16:44 -08001240 video_media_info.reset(new cricket::VideoMediaInfo());
Steve Anton978b8762017-09-29 12:15:02 -07001241 if (!pc_->video_channel()->GetStats(video_media_info.get())) {
hbos84abeb12017-01-16 06:16:44 -08001242 video_media_info.reset();
hbos0adb8282016-11-23 02:32:06 -08001243 }
1244 }
hbos84abeb12017-01-16 06:16:44 -08001245 std::unique_ptr<TrackMediaInfoMap> track_media_info_map(
1246 new TrackMediaInfoMap(std::move(voice_media_info),
1247 std::move(video_media_info),
1248 pc_->GetSenders(),
1249 pc_->GetReceivers()));
1250 return track_media_info_map;
1251}
1252
1253std::map<MediaStreamTrackInterface*, std::string>
1254RTCStatsCollector::PrepareTrackToID_s() const {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001255 // TODO(hta): Remove this method, and vector its callers via
1256 // senders / receivers instead.
1257 // It ignores tracks that are multiply connected to the PC.
hbos84abeb12017-01-16 06:16:44 -08001258 RTC_DCHECK(signaling_thread_->IsCurrent());
1259 std::map<MediaStreamTrackInterface*, std::string> track_to_id;
hbos7b0c6fa2017-07-12 16:22:34 -07001260 for (auto sender : pc_->GetSenders()) {
1261 auto track = sender->track();
1262 if (track)
1263 track_to_id[track.get()] = track->id();
1264 }
1265 for (auto receiver : pc_->GetReceivers()) {
1266 auto track = receiver->track();
1267 if (track)
1268 track_to_id[track.get()] = track->id();
hbos84abeb12017-01-16 06:16:44 -08001269 }
1270 return track_to_id;
hbos0adb8282016-11-23 02:32:06 -08001271}
1272
hbos82ebe022016-11-14 01:41:09 -08001273void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1274 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1275 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1276}
1277
1278void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1279 RTC_DCHECK(signaling_thread_->IsCurrent());
1280 bool result = internal_record_.opened_data_channels.insert(
1281 reinterpret_cast<uintptr_t>(channel)).second;
1282 ++internal_record_.data_channels_opened;
1283 RTC_DCHECK(result);
1284}
1285
1286void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1287 RTC_DCHECK(signaling_thread_->IsCurrent());
1288 // Only channels that have been fully opened (and have increased the
1289 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001290 if (internal_record_.opened_data_channels.erase(
1291 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001292 ++internal_record_.data_channels_closed;
1293 }
1294}
1295
hboscc555c52016-10-18 12:48:31 -07001296const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1297 const std::string& type) {
1298 return CandidateTypeToRTCIceCandidateType(type);
1299}
1300
1301const char* DataStateToRTCDataChannelStateForTesting(
1302 DataChannelInterface::DataState state) {
1303 return DataStateToRTCDataChannelState(state);
1304}
1305
hbosd565b732016-08-30 14:04:35 -07001306} // namespace webrtc