blob: 53a6158ab83c318bed7980553b0c1dff4b4c682b [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"
Steve Anton57858b32018-02-15 15:19:50 -080027#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/stringutils.h"
29#include "rtc_base/timeutils.h"
30#include "rtc_base/trace_event.h"
hbosd565b732016-08-30 14:04:35 -070031
32namespace webrtc {
33
hboscc555c52016-10-18 12:48:31 -070034namespace {
35
hbos2fa7c672016-10-24 04:00:05 -070036std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
37 return "RTCCertificate_" + fingerprint;
38}
39
Steve Anton57858b32018-02-15 15:19:50 -080040std::string RTCCodecStatsIDFromMidDirectionAndPayload(const std::string& mid,
41 bool inbound,
42 uint32_t payload_type) {
43 return "RTCCodec_" + mid + "_" + (inbound ? "Inbound" : "Outbound") + "_" +
44 rtc::ToString<>(payload_type);
hbos0adb8282016-11-23 02:32:06 -080045}
46
hbos2fa7c672016-10-24 04:00:05 -070047std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
48 const cricket::ConnectionInfo& info) {
49 return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" +
50 info.remote_candidate.id();
51}
52
Harald Alvestranda3dab842018-01-14 09:18:58 +010053const char kSender[] = "sender";
54const char kReceiver[] = "receiver";
55
56std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
57 const char* direction,
Harald Alvestrandc72af932018-01-11 17:18:19 +010058 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -080059 std::ostringstream oss;
Harald Alvestranda3dab842018-01-14 09:18:58 +010060 oss << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
hbos9e302742017-01-20 02:47:10 -080061 return oss.str();
hbos09bc1282016-11-08 06:29:22 -080062}
63
hbos2fa7c672016-10-24 04:00:05 -070064std::string RTCTransportStatsIDFromTransportChannel(
65 const std::string& transport_name, int channel_component) {
66 return "RTCTransport_" + transport_name + "_" +
67 rtc::ToString<>(channel_component);
68}
69
hboseeafe942016-11-01 03:00:17 -070070std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
71 return audio ? "RTCInboundRTPAudioStream_" + rtc::ToString<>(ssrc)
72 : "RTCInboundRTPVideoStream_" + rtc::ToString<>(ssrc);
73}
74
hbos6ded1902016-11-01 01:50:46 -070075std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
76 return audio ? "RTCOutboundRTPAudioStream_" + rtc::ToString<>(ssrc)
77 : "RTCOutboundRTPVideoStream_" + rtc::ToString<>(ssrc);
78}
79
hbosab9f6e42016-10-07 02:18:47 -070080const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
81 if (type == cricket::LOCAL_PORT_TYPE)
82 return RTCIceCandidateType::kHost;
83 if (type == cricket::STUN_PORT_TYPE)
84 return RTCIceCandidateType::kSrflx;
85 if (type == cricket::PRFLX_PORT_TYPE)
86 return RTCIceCandidateType::kPrflx;
87 if (type == cricket::RELAY_PORT_TYPE)
88 return RTCIceCandidateType::kRelay;
89 RTC_NOTREACHED();
90 return nullptr;
91}
92
hboscc555c52016-10-18 12:48:31 -070093const char* DataStateToRTCDataChannelState(
94 DataChannelInterface::DataState state) {
95 switch (state) {
96 case DataChannelInterface::kConnecting:
97 return RTCDataChannelState::kConnecting;
98 case DataChannelInterface::kOpen:
99 return RTCDataChannelState::kOpen;
100 case DataChannelInterface::kClosing:
101 return RTCDataChannelState::kClosing;
102 case DataChannelInterface::kClosed:
103 return RTCDataChannelState::kClosed;
104 default:
105 RTC_NOTREACHED();
106 return nullptr;
107 }
108}
109
hbos06495bc2017-01-02 08:08:18 -0800110const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
111 cricket::IceCandidatePairState state) {
112 switch (state) {
113 case cricket::IceCandidatePairState::WAITING:
114 return RTCStatsIceCandidatePairState::kWaiting;
115 case cricket::IceCandidatePairState::IN_PROGRESS:
116 return RTCStatsIceCandidatePairState::kInProgress;
117 case cricket::IceCandidatePairState::SUCCEEDED:
118 return RTCStatsIceCandidatePairState::kSucceeded;
119 case cricket::IceCandidatePairState::FAILED:
120 return RTCStatsIceCandidatePairState::kFailed;
121 default:
122 RTC_NOTREACHED();
123 return nullptr;
124 }
125}
126
hbos7064d592017-01-16 07:38:02 -0800127const char* DtlsTransportStateToRTCDtlsTransportState(
128 cricket::DtlsTransportState state) {
129 switch (state) {
130 case cricket::DTLS_TRANSPORT_NEW:
131 return RTCDtlsTransportState::kNew;
132 case cricket::DTLS_TRANSPORT_CONNECTING:
133 return RTCDtlsTransportState::kConnecting;
134 case cricket::DTLS_TRANSPORT_CONNECTED:
135 return RTCDtlsTransportState::kConnected;
136 case cricket::DTLS_TRANSPORT_CLOSED:
137 return RTCDtlsTransportState::kClosed;
138 case cricket::DTLS_TRANSPORT_FAILED:
139 return RTCDtlsTransportState::kFailed;
140 default:
141 RTC_NOTREACHED();
142 return nullptr;
143 }
144}
145
Gary Liu37e489c2017-11-21 10:49:36 -0800146const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
147 switch (type) {
148 case rtc::ADAPTER_TYPE_CELLULAR:
149 return RTCNetworkType::kCellular;
150 case rtc::ADAPTER_TYPE_ETHERNET:
151 return RTCNetworkType::kEthernet;
152 case rtc::ADAPTER_TYPE_WIFI:
153 return RTCNetworkType::kWifi;
154 case rtc::ADAPTER_TYPE_VPN:
155 return RTCNetworkType::kVpn;
156 case rtc::ADAPTER_TYPE_UNKNOWN:
157 case rtc::ADAPTER_TYPE_LOOPBACK:
158 return RTCNetworkType::kUnknown;
159 }
160 RTC_NOTREACHED();
161 return nullptr;
162}
163
hbos9e302742017-01-20 02:47:10 -0800164double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
165 RTC_DCHECK_GE(audio_level, 0);
166 RTC_DCHECK_LE(audio_level, 32767);
167 return audio_level / 32767.0;
168}
169
hbos0adb8282016-11-23 02:32:06 -0800170std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 15:19:50 -0800171 uint64_t timestamp_us,
172 const std::string& mid,
173 bool inbound,
hbos0adb8282016-11-23 02:32:06 -0800174 const RtpCodecParameters& codec_params) {
175 RTC_DCHECK_GE(codec_params.payload_type, 0);
176 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 12:09:01 -0800177 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 02:32:06 -0800178 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
179 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 15:19:50 -0800180 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 02:32:06 -0800181 timestamp_us));
182 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 06:56:04 -0800183 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 12:09:01 -0800184 if (codec_params.clock_rate) {
185 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
186 }
hbos0adb8282016-11-23 02:32:06 -0800187 return codec_stats;
188}
189
hbos09bc1282016-11-08 06:29:22 -0800190void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
191 const MediaStreamTrackInterface& track,
192 RTCMediaStreamTrackStats* track_stats) {
193 track_stats->track_identifier = track.id();
194 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
195}
196
hbos820f5782016-11-22 03:16:50 -0800197// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700198void SetInboundRTPStreamStatsFromMediaReceiverInfo(
199 const cricket::MediaReceiverInfo& media_receiver_info,
200 RTCInboundRTPStreamStats* inbound_stats) {
201 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800202 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100203 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 03:00:17 -0700204 inbound_stats->is_remote = false;
hboseeafe942016-11-01 03:00:17 -0700205 inbound_stats->packets_received =
206 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
207 inbound_stats->bytes_received =
208 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800209 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 12:26:04 +0100210 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700211 inbound_stats->fraction_lost =
212 static_cast<double>(media_receiver_info.fraction_lost);
213}
214
215void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800216 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700217 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800218 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700219 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800220 voice_receiver_info, inbound_audio);
221 inbound_audio->media_type = "audio";
hbos585a9b12017-02-07 04:59:16 -0800222 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800223 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
224 mid, true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800225 }
hbos820f5782016-11-22 03:16:50 -0800226 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700227 static_cast<double>(voice_receiver_info.jitter_ms) /
228 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800229 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
230 // purposefully left undefined for audio.
hboseeafe942016-11-01 03:00:17 -0700231}
232
233void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800234 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700235 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800236 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700237 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800238 video_receiver_info, inbound_video);
239 inbound_video->media_type = "video";
hbos585a9b12017-02-07 04:59:16 -0800240 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800241 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
242 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800243 }
hbos820f5782016-11-22 03:16:50 -0800244 inbound_video->fir_count =
245 static_cast<uint32_t>(video_receiver_info.firs_sent);
246 inbound_video->pli_count =
247 static_cast<uint32_t>(video_receiver_info.plis_sent);
248 inbound_video->nack_count =
249 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800250 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800251 if (video_receiver_info.qp_sum)
252 inbound_video->qp_sum = *video_receiver_info.qp_sum;
hboseeafe942016-11-01 03:00:17 -0700253}
254
hbos820f5782016-11-22 03:16:50 -0800255// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700256void SetOutboundRTPStreamStatsFromMediaSenderInfo(
257 const cricket::MediaSenderInfo& media_sender_info,
258 RTCOutboundRTPStreamStats* outbound_stats) {
259 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800260 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100261 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700262 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700263 outbound_stats->packets_sent =
264 static_cast<uint32_t>(media_sender_info.packets_sent);
265 outbound_stats->bytes_sent =
266 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbos6ded1902016-11-01 01:50:46 -0700267}
268
269void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800270 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700271 const cricket::VoiceSenderInfo& voice_sender_info,
272 RTCOutboundRTPStreamStats* outbound_audio) {
273 SetOutboundRTPStreamStatsFromMediaSenderInfo(
274 voice_sender_info, outbound_audio);
275 outbound_audio->media_type = "audio";
hbos585a9b12017-02-07 04:59:16 -0800276 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800277 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
278 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800279 }
hbos6ded1902016-11-01 01:50:46 -0700280 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
281 // purposefully left undefined for audio.
282}
283
284void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800285 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700286 const cricket::VideoSenderInfo& video_sender_info,
287 RTCOutboundRTPStreamStats* outbound_video) {
288 SetOutboundRTPStreamStatsFromMediaSenderInfo(
289 video_sender_info, outbound_video);
290 outbound_video->media_type = "video";
hbos585a9b12017-02-07 04:59:16 -0800291 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800292 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
293 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800294 }
hbos6ded1902016-11-01 01:50:46 -0700295 outbound_video->fir_count =
296 static_cast<uint32_t>(video_sender_info.firs_rcvd);
297 outbound_video->pli_count =
298 static_cast<uint32_t>(video_sender_info.plis_rcvd);
299 outbound_video->nack_count =
300 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800301 if (video_sender_info.qp_sum)
302 outbound_video->qp_sum = *video_sender_info.qp_sum;
303 outbound_video->frames_encoded = video_sender_info.frames_encoded;
hbos6ded1902016-11-01 01:50:46 -0700304}
305
hbos02ba2112016-10-28 05:14:53 -0700306void ProduceCertificateStatsFromSSLCertificateStats(
307 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
308 RTCStatsReport* report) {
309 RTCCertificateStats* prev_certificate_stats = nullptr;
310 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
311 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800312 std::string certificate_stats_id =
313 RTCCertificateIDFromFingerprint(s->fingerprint);
314 // It is possible for the same certificate to show up multiple times, e.g.
315 // if local and remote side use the same certificate in a loopback call.
316 // If the report already contains stats for this certificate, skip it.
317 if (report->Get(certificate_stats_id)) {
318 RTC_DCHECK_EQ(s, &certificate_stats);
319 break;
320 }
hbos02ba2112016-10-28 05:14:53 -0700321 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800322 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700323 certificate_stats->fingerprint = s->fingerprint;
324 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
325 certificate_stats->base64_certificate = s->base64_certificate;
326 if (prev_certificate_stats)
327 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
328 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
329 prev_certificate_stats = certificate_stats;
330 }
331}
332
333const std::string& ProduceIceCandidateStats(
334 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800335 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700336 const std::string& id = "RTCIceCandidate_" + candidate.id();
337 const RTCStats* stats = report->Get(id);
338 if (!stats) {
339 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
340 if (is_local)
341 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
342 else
343 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800344 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800345 if (is_local) {
346 candidate_stats->network_type =
347 NetworkAdapterTypeToStatsType(candidate.network_type());
348 } else {
349 // We don't expect to know the adapter type of remote candidates.
350 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
351 }
hbos02ba2112016-10-28 05:14:53 -0700352 candidate_stats->ip = candidate.address().ipaddr().ToString();
353 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
354 candidate_stats->protocol = candidate.protocol();
355 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
356 candidate.type());
357 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
358
359 stats = candidate_stats.get();
360 report->AddStats(std::move(candidate_stats));
361 }
362 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
363 : RTCRemoteIceCandidateStats::kType);
364 return stats->id();
365}
366
hbos9e302742017-01-20 02:47:10 -0800367std::unique_ptr<RTCMediaStreamTrackStats>
368ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
369 int64_t timestamp_us,
370 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100371 const cricket::VoiceSenderInfo& voice_sender_info,
372 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800373 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
374 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100375 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
376 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100377 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800378 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
379 audio_track, audio_track_stats.get());
380 audio_track_stats->remote_source = false;
381 audio_track_stats->detached = false;
382 if (voice_sender_info.audio_level >= 0) {
383 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
384 voice_sender_info.audio_level);
385 }
zsteine76bd3a2017-07-14 12:17:49 -0700386 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
387 audio_track_stats->total_samples_duration =
388 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100389 if (voice_sender_info.apm_statistics.echo_return_loss) {
390 audio_track_stats->echo_return_loss =
391 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800392 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100393 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
394 audio_track_stats->echo_return_loss_enhancement =
395 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800396 }
397 return audio_track_stats;
398}
399
400std::unique_ptr<RTCMediaStreamTrackStats>
401ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
402 int64_t timestamp_us,
403 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100404 const cricket::VoiceReceiverInfo& voice_receiver_info,
405 int attachment_id) {
406 // Since receiver tracks can't be reattached, we use the SSRC as
407 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800408 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
409 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100410 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
411 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100412 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800413 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
414 audio_track, audio_track_stats.get());
415 audio_track_stats->remote_source = true;
416 audio_track_stats->detached = false;
417 if (voice_receiver_info.audio_level >= 0) {
418 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
419 voice_receiver_info.audio_level);
420 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200421 audio_track_stats->jitter_buffer_delay =
422 voice_receiver_info.jitter_buffer_delay_seconds;
zsteine76bd3a2017-07-14 12:17:49 -0700423 audio_track_stats->total_audio_energy =
424 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700425 audio_track_stats->total_samples_received =
426 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700427 audio_track_stats->total_samples_duration =
428 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700429 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200430 audio_track_stats->concealment_events =
431 voice_receiver_info.concealment_events;
hbos9e302742017-01-20 02:47:10 -0800432 return audio_track_stats;
433}
434
435std::unique_ptr<RTCMediaStreamTrackStats>
436ProduceMediaStreamTrackStatsFromVideoSenderInfo(
437 int64_t timestamp_us,
438 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100439 const cricket::VideoSenderInfo& video_sender_info,
440 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800441 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
442 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100443 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
444
445 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100446 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800447 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
448 video_track, video_track_stats.get());
449 video_track_stats->remote_source = false;
450 video_track_stats->detached = false;
451 video_track_stats->frame_width = static_cast<uint32_t>(
452 video_sender_info.send_frame_width);
453 video_track_stats->frame_height = static_cast<uint32_t>(
454 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800455 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100456 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800457 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100458 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800459 return video_track_stats;
460}
461
462std::unique_ptr<RTCMediaStreamTrackStats>
463ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
464 int64_t timestamp_us,
465 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100466 const cricket::VideoReceiverInfo& video_receiver_info,
467 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800468 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
469 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100470 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
471
472 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100473 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800474 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
475 video_track, video_track_stats.get());
476 video_track_stats->remote_source = true;
477 video_track_stats->detached = false;
478 if (video_receiver_info.frame_width > 0 &&
479 video_receiver_info.frame_height > 0) {
480 video_track_stats->frame_width = static_cast<uint32_t>(
481 video_receiver_info.frame_width);
482 video_track_stats->frame_height = static_cast<uint32_t>(
483 video_receiver_info.frame_height);
484 }
hbos42f6d2f2017-01-20 03:56:50 -0800485 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800486 // TODO(hbos): When we support receiving simulcast, this should be the total
487 // number of frames correctly decoded, independent of which SSRC it was
488 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100489 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800490 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800491 RTC_DCHECK_GE(video_receiver_info.frames_received,
492 video_receiver_info.frames_rendered);
493 video_track_stats->frames_dropped = video_receiver_info.frames_received -
494 video_receiver_info.frames_rendered;
hbos9e302742017-01-20 02:47:10 -0800495 return video_track_stats;
496}
497
Harald Alvestrand89061872018-01-02 14:08:34 +0100498void ProduceSenderMediaTrackStats(
499 int64_t timestamp_us,
500 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800501 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100502 RTCStatsReport* report) {
503 // This function iterates over the senders to generate outgoing track stats.
504
505 // TODO(hbos): Return stats of detached tracks. We have to perform stats
506 // gathering at the time of detachment to get accurate stats and timestamps.
507 // https://crbug.com/659137
Steve Anton57858b32018-02-15 15:19:50 -0800508 for (auto sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100509 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
510 AudioTrackInterface* track =
511 static_cast<AudioTrackInterface*>(sender->track().get());
512 if (!track)
513 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100514 cricket::VoiceSenderInfo null_sender_info;
515 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
516 // TODO(hta): Checking on ssrc is not proper. There should be a way
517 // to see from a sender whether it's connected or not.
518 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800519 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100520 // When pc.close is called, sender info is discarded, so
521 // we generate zeroes instead. Bug: It should be retained.
522 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800523 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100524 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100525 if (sender_info) {
526 voice_sender_info = sender_info;
527 } else {
528 RTC_LOG(LS_INFO)
529 << "RTCStatsCollector: No voice sender info for sender with ssrc "
530 << sender->ssrc();
531 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100532 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100533 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100534 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
535 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100536 report->AddStats(std::move(audio_track_stats));
537 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
538 VideoTrackInterface* track =
539 static_cast<VideoTrackInterface*>(sender->track().get());
540 if (!track)
541 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100542 cricket::VideoSenderInfo null_sender_info;
543 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
544 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100545 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
546 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800547 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100548 // When pc.close is called, sender info is discarded, so
549 // we generate zeroes instead. Bug: It should be retained.
550 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800551 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100552 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100553 if (sender_info) {
554 video_sender_info = sender_info;
555 } else {
556 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
557 << sender->ssrc();
558 }
559 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100560 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100561 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
562 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100563 report->AddStats(std::move(video_track_stats));
564 } else {
565 RTC_NOTREACHED();
566 }
567 }
568}
569
570void ProduceReceiverMediaTrackStats(
571 int64_t timestamp_us,
572 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800573 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100574 RTCStatsReport* report) {
575 // This function iterates over the receivers to find the remote tracks.
Steve Anton57858b32018-02-15 15:19:50 -0800576 for (auto receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100577 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
578 AudioTrackInterface* track =
579 static_cast<AudioTrackInterface*>(receiver->track().get());
580 const cricket::VoiceReceiverInfo* voice_receiver_info =
581 track_media_info_map.GetVoiceReceiverInfo(*track);
582 if (!voice_receiver_info) {
583 continue;
584 }
585 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
586 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100587 timestamp_us, *track, *voice_receiver_info,
588 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100589 report->AddStats(std::move(audio_track_stats));
590 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
591 VideoTrackInterface* track =
592 static_cast<VideoTrackInterface*>(receiver->track().get());
593 const cricket::VideoReceiverInfo* video_receiver_info =
594 track_media_info_map.GetVideoReceiverInfo(*track);
595 if (!video_receiver_info) {
596 continue;
597 }
598 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
599 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100600 timestamp_us, *track, *video_receiver_info,
601 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100602 report->AddStats(std::move(video_track_stats));
603 } else {
604 RTC_NOTREACHED();
605 }
606 }
607}
608
hboscc555c52016-10-18 12:48:31 -0700609} // namespace
610
hbosc82f2e12016-09-05 01:36:50 -0700611rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800612 PeerConnectionInternal* pc,
613 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700614 return rtc::scoped_refptr<RTCStatsCollector>(
615 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
616}
617
Steve Anton2d8609c2018-01-23 16:38:46 -0800618RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700619 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700620 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700621 signaling_thread_(pc->signaling_thread()),
622 worker_thread_(pc->worker_thread()),
623 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700624 num_pending_partial_reports_(0),
625 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700626 cache_timestamp_us_(0),
627 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700628 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700629 RTC_DCHECK(signaling_thread_);
630 RTC_DCHECK(worker_thread_);
631 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700632 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800633 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800634 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700635}
636
hbosb78306a2016-12-19 05:06:57 -0800637RTCStatsCollector::~RTCStatsCollector() {
638 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
639}
640
hbosc82f2e12016-09-05 01:36:50 -0700641void RTCStatsCollector::GetStatsReport(
642 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
643 RTC_DCHECK(signaling_thread_->IsCurrent());
644 RTC_DCHECK(callback);
645 callbacks_.push_back(callback);
646
hbos0e6758d2016-08-31 07:57:36 -0700647 // "Now" using a monotonically increasing timer.
648 int64_t cache_now_us = rtc::TimeMicros();
649 if (cached_report_ &&
650 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700651 // We have a fresh cached report to deliver.
652 DeliverCachedReport();
653 } else if (!num_pending_partial_reports_) {
654 // Only start gathering stats if we're not already gathering stats. In the
655 // case of already gathering stats, |callback_| will be invoked when there
656 // are no more pending partial reports.
657
658 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
659 // UTC), in microseconds. The system clock could be modified and is not
660 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700661 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700662
hbosf415f8a2017-01-02 04:28:51 -0800663 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700664 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800665
Steve Anton57858b32018-02-15 15:19:50 -0800666 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800667 // |ProducePartialResultsOnNetworkThread| and
668 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800669 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800670
stefanf79ade12017-06-02 06:44:03 -0700671 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
672 // thread.
673 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
674 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700675 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700676
677 invoker_.AsyncInvoke<void>(
678 RTC_FROM_HERE, network_thread_,
hbosc82f2e12016-09-05 01:36:50 -0700679 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
stefanf79ade12017-06-02 06:44:03 -0700680 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800681 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700682 }
hbosd565b732016-08-30 14:04:35 -0700683}
684
685void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700686 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700687 cached_report_ = nullptr;
688}
689
hbosb78306a2016-12-19 05:06:57 -0800690void RTCStatsCollector::WaitForPendingRequest() {
691 RTC_DCHECK(signaling_thread_->IsCurrent());
692 if (num_pending_partial_reports_) {
693 rtc::Thread::Current()->ProcessMessages(0);
694 while (num_pending_partial_reports_) {
695 rtc::Thread::Current()->SleepMs(1);
696 rtc::Thread::Current()->ProcessMessages(0);
697 }
698 }
699}
700
hbosc82f2e12016-09-05 01:36:50 -0700701void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
702 int64_t timestamp_us) {
703 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700704 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
705 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700706
hboscc555c52016-10-18 12:48:31 -0700707 ProduceDataChannelStats_s(timestamp_us, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800708 ProduceMediaStreamStats_s(timestamp_us, report.get());
709 ProduceMediaStreamTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700710 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700711
712 AddPartialResults(report);
713}
714
hbosc82f2e12016-09-05 01:36:50 -0700715void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
716 int64_t timestamp_us) {
717 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700718 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
719 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700720
Steve Anton5dfde182018-02-06 10:34:40 -0800721 std::set<std::string> transport_names;
Steve Anton57858b32018-02-15 15:19:50 -0800722 for (const auto& stats : transceiver_stats_infos_) {
723 if (stats.transport_name) {
724 transport_names.insert(*stats.transport_name);
725 }
hbosdf6075a2016-12-19 04:58:02 -0800726 }
Steve Anton5dfde182018-02-06 10:34:40 -0800727 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
728 pc_->GetTransportStatsByNames(transport_names);
729
730 std::map<std::string, CertificateStatsPair> transport_cert_stats =
731 PrepareTransportCertificateStats_n(transport_stats_by_name);
732
733 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800734 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, report.get());
Steve Anton5dfde182018-02-06 10:34:40 -0800735 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
Steve Anton5dfde182018-02-06 10:34:40 -0800736 call_stats_, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800737 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_, report.get());
Steve Anton5dfde182018-02-06 10:34:40 -0800738 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
739 transport_cert_stats, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700740
741 AddPartialResults(report);
742}
743
744void RTCStatsCollector::AddPartialResults(
745 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
746 if (!signaling_thread_->IsCurrent()) {
747 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
748 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
749 rtc::scoped_refptr<RTCStatsCollector>(this),
750 partial_report));
751 return;
752 }
753 AddPartialResults_s(partial_report);
754}
755
756void RTCStatsCollector::AddPartialResults_s(
757 rtc::scoped_refptr<RTCStatsReport> partial_report) {
758 RTC_DCHECK(signaling_thread_->IsCurrent());
759 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
760 if (!partial_report_)
761 partial_report_ = partial_report;
762 else
763 partial_report_->TakeMembersFrom(partial_report);
764 --num_pending_partial_reports_;
765 if (!num_pending_partial_reports_) {
766 cache_timestamp_us_ = partial_report_timestamp_us_;
767 cached_report_ = partial_report_;
768 partial_report_ = nullptr;
Steve Anton57858b32018-02-15 15:19:50 -0800769 transceiver_stats_infos_.clear();
ehmaldonadoa26196b2017-07-18 03:30:29 -0700770 // Trace WebRTC Stats when getStats is called on Javascript.
771 // This allows access to WebRTC stats from trace logs. To enable them,
772 // select the "webrtc_stats" category when recording traces.
ehmaldonado8ab0fd82017-09-04 14:35:04 -0700773 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
774 cached_report_->ToJson());
hbosc82f2e12016-09-05 01:36:50 -0700775 DeliverCachedReport();
776 }
777}
778
779void RTCStatsCollector::DeliverCachedReport() {
780 RTC_DCHECK(signaling_thread_->IsCurrent());
781 RTC_DCHECK(!callbacks_.empty());
782 RTC_DCHECK(cached_report_);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -0800783
784 // Swap the list of callbacks, in case one of them recursively calls
785 // GetStatsReport again and modifies the callback list.
786 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks;
787 callbacks.swap(callbacks_);
788
hbosc82f2e12016-09-05 01:36:50 -0700789 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
Taylor Brandstetter87d5a742018-03-06 09:42:25 -0800790 callbacks) {
hbosc82f2e12016-09-05 01:36:50 -0700791 callback->OnStatsDelivered(cached_report_);
792 }
hbosd565b732016-08-30 14:04:35 -0700793}
794
hbosdf6075a2016-12-19 04:58:02 -0800795void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700796 int64_t timestamp_us,
797 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700798 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800799 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700800 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
801 if (transport_cert_stats_pair.second.local) {
802 ProduceCertificateStatsFromSSLCertificateStats(
803 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700804 }
hbos02ba2112016-10-28 05:14:53 -0700805 if (transport_cert_stats_pair.second.remote) {
806 ProduceCertificateStatsFromSSLCertificateStats(
807 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700808 }
809 }
810}
811
hbosdf6075a2016-12-19 04:58:02 -0800812void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -0800813 int64_t timestamp_us,
814 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -0800815 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800816 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -0800817 for (const auto& stats : transceiver_stats_infos) {
818 if (!stats.mid) {
819 continue;
hbos0adb8282016-11-23 02:32:06 -0800820 }
Steve Anton57858b32018-02-15 15:19:50 -0800821 const cricket::VoiceMediaInfo* voice_media_info =
822 stats.track_media_info_map->voice_media_info();
823 const cricket::VideoMediaInfo* video_media_info =
824 stats.track_media_info_map->video_media_info();
825 // Audio
826 if (voice_media_info) {
827 // Inbound
828 for (const auto& pair : voice_media_info->receive_codecs) {
829 report->AddStats(CodecStatsFromRtpCodecParameters(
830 timestamp_us, *stats.mid, true, pair.second));
831 }
832 // Outbound
833 for (const auto& pair : voice_media_info->send_codecs) {
834 report->AddStats(CodecStatsFromRtpCodecParameters(
835 timestamp_us, *stats.mid, false, pair.second));
836 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +0000837 }
Steve Anton57858b32018-02-15 15:19:50 -0800838 // Video
839 if (video_media_info) {
840 // Inbound
841 for (const auto& pair : video_media_info->receive_codecs) {
842 report->AddStats(CodecStatsFromRtpCodecParameters(
843 timestamp_us, *stats.mid, true, pair.second));
844 }
845 // Outbound
846 for (const auto& pair : video_media_info->send_codecs) {
847 report->AddStats(CodecStatsFromRtpCodecParameters(
848 timestamp_us, *stats.mid, false, pair.second));
849 }
hbos0adb8282016-11-23 02:32:06 -0800850 }
851 }
852}
853
hboscc555c52016-10-18 12:48:31 -0700854void RTCStatsCollector::ProduceDataChannelStats_s(
855 int64_t timestamp_us, RTCStatsReport* report) const {
856 RTC_DCHECK(signaling_thread_->IsCurrent());
857 for (const rtc::scoped_refptr<DataChannel>& data_channel :
858 pc_->sctp_data_channels()) {
859 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
860 new RTCDataChannelStats(
861 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
862 timestamp_us));
863 data_channel_stats->label = data_channel->label();
864 data_channel_stats->protocol = data_channel->protocol();
865 data_channel_stats->datachannelid = data_channel->id();
866 data_channel_stats->state =
867 DataStateToRTCDataChannelState(data_channel->state());
868 data_channel_stats->messages_sent = data_channel->messages_sent();
869 data_channel_stats->bytes_sent = data_channel->bytes_sent();
870 data_channel_stats->messages_received = data_channel->messages_received();
871 data_channel_stats->bytes_received = data_channel->bytes_received();
872 report->AddStats(std::move(data_channel_stats));
873 }
874}
875
hbosdf6075a2016-12-19 04:58:02 -0800876void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -0700877 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -0800878 const std::map<std::string, cricket::TransportStats>&
879 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -0700880 const Call::Stats& call_stats,
881 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800882 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -0800883 for (const auto& entry : transport_stats_by_name) {
884 const std::string& transport_name = entry.first;
885 const cricket::TransportStats& transport_stats = entry.second;
886 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800887 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -0800888 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700889 for (const cricket::ConnectionInfo& info :
890 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700891 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700892 new RTCIceCandidatePairStats(
893 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
894 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700895
hbos0583b282016-11-30 01:50:14 -0800896 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700897 // TODO(hbos): There could be other candidates that are not paired with
898 // anything. We don't have a complete list. Local candidates come from
899 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +0100900 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700901 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800902 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -0700903 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800904 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -0800905 candidate_pair_stats->state =
906 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
907 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -0800908 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -0700909 // TODO(hbos): This writable is different than the spec. It goes to
910 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +0100911 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -0700912 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700913 candidate_pair_stats->bytes_sent =
914 static_cast<uint64_t>(info.sent_total_bytes);
915 candidate_pair_stats->bytes_received =
916 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -0800917 candidate_pair_stats->total_round_trip_time =
918 static_cast<double>(info.total_round_trip_time_ms) /
919 rtc::kNumMillisecsPerSec;
920 if (info.current_round_trip_time_ms) {
921 candidate_pair_stats->current_round_trip_time =
922 static_cast<double>(*info.current_round_trip_time_ms) /
923 rtc::kNumMillisecsPerSec;
924 }
stefanf79ade12017-06-02 06:44:03 -0700925 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -0800926 // The bandwidth estimations we have are for the selected candidate
927 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -0700928 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
929 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
930 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -0800931 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -0700932 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -0800933 }
stefanf79ade12017-06-02 06:44:03 -0700934 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -0800935 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -0700936 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -0800937 }
938 }
hbosd82f5122016-12-09 04:12:39 -0800939 candidate_pair_stats->requests_received =
940 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -0800941 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
942 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700943 candidate_pair_stats->responses_received =
944 static_cast<uint64_t>(info.recv_ping_responses);
945 candidate_pair_stats->responses_sent =
946 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -0800947 RTC_DCHECK_GE(info.sent_ping_requests_total,
948 info.sent_ping_requests_before_first_response);
949 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
950 info.sent_ping_requests_total -
951 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700952
953 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700954 }
955 }
956 }
957}
958
Steve Anton57858b32018-02-15 15:19:50 -0800959void RTCStatsCollector::ProduceMediaStreamStats_s(
960 int64_t timestamp_us,
961 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -0800962 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -0800963
964 std::map<std::string, std::vector<std::string>> track_ids;
965
966 for (const auto& stats : transceiver_stats_infos_) {
967 for (auto sender : stats.transceiver->senders()) {
968 std::string track_id =
969 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
970 kSender, sender->internal()->AttachmentId());
971 for (auto& stream_id : sender->stream_ids()) {
972 track_ids[stream_id].push_back(track_id);
973 }
974 }
975 for (auto receiver : stats.transceiver->receivers()) {
976 std::string track_id =
977 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
978 kReceiver, receiver->internal()->AttachmentId());
979 for (auto& stream : receiver->streams()) {
980 track_ids[stream->label()].push_back(track_id);
981 }
982 }
983 }
984
985 // Build stats for each stream ID known.
986 for (auto& it : track_ids) {
987 std::unique_ptr<RTCMediaStreamStats> stream_stats(
988 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
989 stream_stats->stream_identifier = it.first;
990 stream_stats->track_ids = it.second;
991 report->AddStats(std::move(stream_stats));
992 }
993}
994
995void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
996 int64_t timestamp_us,
997 RTCStatsReport* report) const {
998 RTC_DCHECK(signaling_thread_->IsCurrent());
999 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1000 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
1001 for (auto sender : stats.transceiver->senders()) {
1002 senders.push_back(sender->internal());
1003 }
1004 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1005 senders, report);
1006
1007 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
1008 for (auto receiver : stats.transceiver->receivers()) {
1009 receivers.push_back(receiver->internal());
1010 }
1011 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1012 receivers, report);
1013 }
hbos09bc1282016-11-08 06:29:22 -08001014}
1015
hbos6ab97ce2016-10-03 14:16:56 -07001016void RTCStatsCollector::ProducePeerConnectionStats_s(
1017 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001018 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001019 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001020 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001021 stats->data_channels_opened = internal_record_.data_channels_opened;
1022 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001023 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001024}
1025
hbosdf6075a2016-12-19 04:58:02 -08001026void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001027 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001028 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001029 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001030 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001031
Steve Anton57858b32018-02-15 15:19:50 -08001032 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1033 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1034 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1035 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1036 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1037 } else {
1038 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001039 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001040 }
Steve Anton57858b32018-02-15 15:19:50 -08001041}
1042
1043void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1044 int64_t timestamp_us,
1045 const RtpTransceiverStatsInfo& stats,
1046 RTCStatsReport* report) const {
1047 if (!stats.mid || !stats.transport_name) {
1048 return;
1049 }
1050 RTC_DCHECK(stats.track_media_info_map);
1051 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1052 RTC_DCHECK(track_media_info_map.voice_media_info());
1053 std::string mid = *stats.mid;
1054 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1055 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1056 // Inbound
1057 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1058 track_media_info_map.voice_media_info()->receivers) {
1059 if (!voice_receiver_info.connected())
1060 continue;
1061 auto inbound_audio = rtc::MakeUnique<RTCInboundRTPStreamStats>(
1062 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1063 timestamp_us);
1064 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1065 inbound_audio.get());
1066 // TODO(hta): This lookup should look for the sender, not the track.
1067 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1068 track_media_info_map.GetAudioTrack(voice_receiver_info);
1069 if (audio_track) {
1070 inbound_audio->track_id =
1071 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1072 kReceiver,
1073 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001074 }
Steve Anton57858b32018-02-15 15:19:50 -08001075 inbound_audio->transport_id = transport_id;
1076 report->AddStats(std::move(inbound_audio));
1077 }
1078 // Outbound
1079 for (const cricket::VoiceSenderInfo& voice_sender_info :
1080 track_media_info_map.voice_media_info()->senders) {
1081 if (!voice_sender_info.connected())
1082 continue;
1083 auto outbound_audio = rtc::MakeUnique<RTCOutboundRTPStreamStats>(
1084 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1085 timestamp_us);
1086 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1087 outbound_audio.get());
1088 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1089 track_media_info_map.GetAudioTrack(voice_sender_info);
1090 if (audio_track) {
1091 outbound_audio->track_id =
1092 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1093 kSender,
1094 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001095 }
Steve Anton57858b32018-02-15 15:19:50 -08001096 outbound_audio->transport_id = transport_id;
1097 report->AddStats(std::move(outbound_audio));
1098 }
1099}
1100
1101void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1102 int64_t timestamp_us,
1103 const RtpTransceiverStatsInfo& stats,
1104 RTCStatsReport* report) const {
1105 if (!stats.mid || !stats.transport_name) {
1106 return;
1107 }
1108 RTC_DCHECK(stats.track_media_info_map);
1109 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1110 RTC_DCHECK(track_media_info_map.video_media_info());
1111 std::string mid = *stats.mid;
1112 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1113 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1114 // Inbound
1115 for (const cricket::VideoReceiverInfo& video_receiver_info :
1116 track_media_info_map.video_media_info()->receivers) {
1117 if (!video_receiver_info.connected())
1118 continue;
1119 auto inbound_video = rtc::MakeUnique<RTCInboundRTPStreamStats>(
1120 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1121 timestamp_us);
1122 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1123 inbound_video.get());
1124 rtc::scoped_refptr<VideoTrackInterface> video_track =
1125 track_media_info_map.GetVideoTrack(video_receiver_info);
1126 if (video_track) {
1127 inbound_video->track_id =
1128 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1129 kReceiver,
1130 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1131 }
1132 inbound_video->transport_id = transport_id;
1133 report->AddStats(std::move(inbound_video));
1134 }
1135 // Outbound
1136 for (const cricket::VideoSenderInfo& video_sender_info :
1137 track_media_info_map.video_media_info()->senders) {
1138 if (!video_sender_info.connected())
1139 continue;
1140 auto outbound_video = rtc::MakeUnique<RTCOutboundRTPStreamStats>(
1141 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1142 timestamp_us);
1143 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1144 outbound_video.get());
1145 rtc::scoped_refptr<VideoTrackInterface> video_track =
1146 track_media_info_map.GetVideoTrack(video_sender_info);
1147 if (video_track) {
1148 outbound_video->track_id =
1149 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1150 kSender,
1151 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1152 }
1153 outbound_video->transport_id = transport_id;
1154 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001155 }
1156}
1157
hbosdf6075a2016-12-19 04:58:02 -08001158void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001159 int64_t timestamp_us,
1160 const std::map<std::string, cricket::TransportStats>&
1161 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001162 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1163 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001164 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001165 for (const auto& entry : transport_stats_by_name) {
1166 const std::string& transport_name = entry.first;
1167 const cricket::TransportStats& transport_stats = entry.second;
1168
hbos2fa7c672016-10-24 04:00:05 -07001169 // Get reference to RTCP channel, if it exists.
1170 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001171 for (const cricket::TransportChannelStats& channel_stats :
1172 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001173 if (channel_stats.component ==
1174 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1175 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001176 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001177 break;
1178 }
1179 }
1180
1181 // Get reference to local and remote certificates of this transport, if they
1182 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001183 const auto& certificate_stats_it =
1184 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001185 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1186 std::string local_certificate_id;
1187 if (certificate_stats_it->second.local) {
1188 local_certificate_id = RTCCertificateIDFromFingerprint(
1189 certificate_stats_it->second.local->fingerprint);
1190 }
1191 std::string remote_certificate_id;
1192 if (certificate_stats_it->second.remote) {
1193 remote_certificate_id = RTCCertificateIDFromFingerprint(
1194 certificate_stats_it->second.remote->fingerprint);
1195 }
1196
1197 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001198 for (const cricket::TransportChannelStats& channel_stats :
1199 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001200 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001201 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1202 transport_name, channel_stats.component),
1203 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001204 transport_stats->bytes_sent = 0;
1205 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001206 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1207 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001208 for (const cricket::ConnectionInfo& info :
1209 channel_stats.connection_infos) {
1210 *transport_stats->bytes_sent += info.sent_total_bytes;
1211 *transport_stats->bytes_received += info.recv_total_bytes;
1212 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001213 transport_stats->selected_candidate_pair_id =
1214 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1215 }
1216 }
1217 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1218 !rtcp_transport_stats_id.empty()) {
1219 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1220 }
1221 if (!local_certificate_id.empty())
1222 transport_stats->local_certificate_id = local_certificate_id;
1223 if (!remote_certificate_id.empty())
1224 transport_stats->remote_certificate_id = remote_certificate_id;
1225 report->AddStats(std::move(transport_stats));
1226 }
1227 }
1228}
1229
1230std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001231RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001232 const std::map<std::string, cricket::TransportStats>&
1233 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001234 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001235 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001236 for (const auto& entry : transport_stats_by_name) {
1237 const std::string& transport_name = entry.first;
1238
hbos2fa7c672016-10-24 04:00:05 -07001239 CertificateStatsPair certificate_stats_pair;
1240 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001241 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001242 certificate_stats_pair.local =
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001243 local_certificate->ssl_cert_chain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001244 }
Steve Anton5dfde182018-02-06 10:34:40 -08001245
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001246 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1247 pc_->GetRemoteSSLCertChain(transport_name);
1248 if (remote_cert_chain) {
1249 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001250 }
Steve Anton5dfde182018-02-06 10:34:40 -08001251
hbos2fa7c672016-10-24 04:00:05 -07001252 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001253 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001254 }
1255 return transport_cert_stats;
1256}
1257
Steve Anton57858b32018-02-15 15:19:50 -08001258std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1259RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1260 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001261
Steve Anton57858b32018-02-15 15:19:50 -08001262 // These are used to invoke GetStats for all the media channels together in
1263 // one worker thread hop.
1264 std::map<cricket::VoiceMediaChannel*,
1265 std::unique_ptr<cricket::VoiceMediaInfo>>
1266 voice_stats;
1267 std::map<cricket::VideoMediaChannel*,
1268 std::unique_ptr<cricket::VideoMediaInfo>>
1269 video_stats;
1270
1271 for (auto transceiver : pc_->GetTransceiversInternal()) {
1272 cricket::MediaType media_type = transceiver->media_type();
1273
1274 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1275 // stats have been fetched on the worker thread.
1276 transceiver_stats_infos.emplace_back();
1277 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1278 stats.transceiver = transceiver->internal();
1279 stats.media_type = media_type;
1280
1281 cricket::BaseChannel* channel = transceiver->internal()->channel();
1282 if (!channel) {
1283 // The remaining fields require a BaseChannel.
1284 continue;
1285 }
1286
1287 stats.mid = channel->content_name();
1288 stats.transport_name = channel->transport_name();
1289
1290 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1291 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1292 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1293 voice_stats.end());
1294 voice_stats[voice_channel->media_channel()] =
1295 rtc::MakeUnique<cricket::VoiceMediaInfo>();
1296 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1297 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1298 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1299 video_stats.end());
1300 video_stats[video_channel->media_channel()] =
1301 rtc::MakeUnique<cricket::VideoMediaInfo>();
1302 } else {
1303 RTC_NOTREACHED();
1304 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001305 }
Steve Anton57858b32018-02-15 15:19:50 -08001306
1307 // Call GetStats for all media channels together on the worker thread in one
1308 // hop.
1309 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1310 for (const auto& entry : voice_stats) {
1311 if (!entry.first->GetStats(entry.second.get())) {
1312 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1313 }
1314 }
1315 for (const auto& entry : video_stats) {
1316 if (!entry.first->GetStats(entry.second.get())) {
1317 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1318 }
1319 }
1320 });
1321
1322 // Create the TrackMediaInfoMap for each transceiver stats object.
1323 for (auto& stats : transceiver_stats_infos) {
1324 auto transceiver = stats.transceiver;
1325 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1326 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1327 if (transceiver->channel()) {
1328 cricket::MediaType media_type = transceiver->media_type();
1329 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1330 auto* voice_channel =
1331 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1332 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1333 voice_media_info =
1334 std::move(voice_stats[voice_channel->media_channel()]);
1335 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1336 auto* video_channel =
1337 static_cast<cricket::VideoChannel*>(transceiver->channel());
1338 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1339 video_media_info =
1340 std::move(video_stats[video_channel->media_channel()]);
1341 }
1342 }
1343 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
1344 for (auto sender : transceiver->senders()) {
1345 senders.push_back(sender->internal());
1346 }
1347 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
1348 for (auto receiver : transceiver->receivers()) {
1349 receivers.push_back(receiver->internal());
1350 }
1351 stats.track_media_info_map = rtc::MakeUnique<TrackMediaInfoMap>(
1352 std::move(voice_media_info), std::move(video_media_info), senders,
1353 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001354 }
Steve Anton57858b32018-02-15 15:19:50 -08001355
1356 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001357}
1358
hbos82ebe022016-11-14 01:41:09 -08001359void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1360 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1361 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1362}
1363
1364void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1365 RTC_DCHECK(signaling_thread_->IsCurrent());
1366 bool result = internal_record_.opened_data_channels.insert(
1367 reinterpret_cast<uintptr_t>(channel)).second;
1368 ++internal_record_.data_channels_opened;
1369 RTC_DCHECK(result);
1370}
1371
1372void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1373 RTC_DCHECK(signaling_thread_->IsCurrent());
1374 // Only channels that have been fully opened (and have increased the
1375 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001376 if (internal_record_.opened_data_channels.erase(
1377 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001378 ++internal_record_.data_channels_closed;
1379 }
1380}
1381
hboscc555c52016-10-18 12:48:31 -07001382const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1383 const std::string& type) {
1384 return CandidateTypeToRTCIceCandidateType(type);
1385}
1386
1387const char* DataStateToRTCDataChannelStateForTesting(
1388 DataChannelInterface::DataState state) {
1389 return DataStateToRTCDataChannelState(state);
1390}
1391
hbosd565b732016-08-30 14:04:35 -07001392} // namespace webrtc