blob: e442ffe78cc5fbff0ba9d987f9cdecd613584c8a [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_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800651 // We have a fresh cached report to deliver. Deliver asynchronously, since
652 // the caller may not be expecting a synchronous callback, and it avoids
653 // reentrancy problems.
654 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks;
655 callbacks.swap(callbacks_);
656 invoker_.AsyncInvoke<void>(
657 RTC_FROM_HERE, signaling_thread_,
658 rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this, cached_report_,
659 std::move(callbacks)));
660 callbacks_.clear();
hbosc82f2e12016-09-05 01:36:50 -0700661 } else if (!num_pending_partial_reports_) {
662 // Only start gathering stats if we're not already gathering stats. In the
663 // case of already gathering stats, |callback_| will be invoked when there
664 // are no more pending partial reports.
665
666 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
667 // UTC), in microseconds. The system clock could be modified and is not
668 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700669 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700670
hbosf415f8a2017-01-02 04:28:51 -0800671 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700672 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800673
Steve Anton57858b32018-02-15 15:19:50 -0800674 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800675 // |ProducePartialResultsOnNetworkThread| and
676 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800677 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800678
stefanf79ade12017-06-02 06:44:03 -0700679 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
680 // thread.
681 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
682 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700683 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700684
685 invoker_.AsyncInvoke<void>(
686 RTC_FROM_HERE, network_thread_,
hbosc82f2e12016-09-05 01:36:50 -0700687 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
stefanf79ade12017-06-02 06:44:03 -0700688 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800689 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700690 }
hbosd565b732016-08-30 14:04:35 -0700691}
692
693void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700694 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700695 cached_report_ = nullptr;
696}
697
hbosb78306a2016-12-19 05:06:57 -0800698void RTCStatsCollector::WaitForPendingRequest() {
699 RTC_DCHECK(signaling_thread_->IsCurrent());
700 if (num_pending_partial_reports_) {
701 rtc::Thread::Current()->ProcessMessages(0);
702 while (num_pending_partial_reports_) {
703 rtc::Thread::Current()->SleepMs(1);
704 rtc::Thread::Current()->ProcessMessages(0);
705 }
706 }
707}
708
hbosc82f2e12016-09-05 01:36:50 -0700709void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
710 int64_t timestamp_us) {
711 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700712 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
713 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700714
hboscc555c52016-10-18 12:48:31 -0700715 ProduceDataChannelStats_s(timestamp_us, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800716 ProduceMediaStreamStats_s(timestamp_us, report.get());
717 ProduceMediaStreamTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700718 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700719
720 AddPartialResults(report);
721}
722
hbosc82f2e12016-09-05 01:36:50 -0700723void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
724 int64_t timestamp_us) {
725 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700726 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
727 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700728
Steve Anton5dfde182018-02-06 10:34:40 -0800729 std::set<std::string> transport_names;
Steve Anton57858b32018-02-15 15:19:50 -0800730 for (const auto& stats : transceiver_stats_infos_) {
731 if (stats.transport_name) {
732 transport_names.insert(*stats.transport_name);
733 }
hbosdf6075a2016-12-19 04:58:02 -0800734 }
Steve Anton5dfde182018-02-06 10:34:40 -0800735 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
736 pc_->GetTransportStatsByNames(transport_names);
737
738 std::map<std::string, CertificateStatsPair> transport_cert_stats =
739 PrepareTransportCertificateStats_n(transport_stats_by_name);
740
741 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800742 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, report.get());
Steve Anton5dfde182018-02-06 10:34:40 -0800743 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
Steve Anton5dfde182018-02-06 10:34:40 -0800744 call_stats_, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800745 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_, report.get());
Steve Anton5dfde182018-02-06 10:34:40 -0800746 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
747 transport_cert_stats, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700748
749 AddPartialResults(report);
750}
751
752void RTCStatsCollector::AddPartialResults(
753 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
754 if (!signaling_thread_->IsCurrent()) {
755 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
756 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
757 rtc::scoped_refptr<RTCStatsCollector>(this),
758 partial_report));
759 return;
760 }
761 AddPartialResults_s(partial_report);
762}
763
764void RTCStatsCollector::AddPartialResults_s(
765 rtc::scoped_refptr<RTCStatsReport> partial_report) {
766 RTC_DCHECK(signaling_thread_->IsCurrent());
767 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
768 if (!partial_report_)
769 partial_report_ = partial_report;
770 else
771 partial_report_->TakeMembersFrom(partial_report);
772 --num_pending_partial_reports_;
773 if (!num_pending_partial_reports_) {
774 cache_timestamp_us_ = partial_report_timestamp_us_;
775 cached_report_ = partial_report_;
776 partial_report_ = nullptr;
Steve Anton57858b32018-02-15 15:19:50 -0800777 transceiver_stats_infos_.clear();
ehmaldonadoa26196b2017-07-18 03:30:29 -0700778 // Trace WebRTC Stats when getStats is called on Javascript.
779 // This allows access to WebRTC stats from trace logs. To enable them,
780 // select the "webrtc_stats" category when recording traces.
ehmaldonado8ab0fd82017-09-04 14:35:04 -0700781 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
782 cached_report_->ToJson());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -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 DeliverCachedReport(cached_report_, std::move(callbacks));
hbosc82f2e12016-09-05 01:36:50 -0700789 }
790}
791
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800792void RTCStatsCollector::DeliverCachedReport(
793 rtc::scoped_refptr<const RTCStatsReport> cached_report,
794 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks) {
hbosc82f2e12016-09-05 01:36:50 -0700795 RTC_DCHECK(signaling_thread_->IsCurrent());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800796 RTC_DCHECK(!callbacks.empty());
797 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -0800798
hbosc82f2e12016-09-05 01:36:50 -0700799 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
Taylor Brandstetter87d5a742018-03-06 09:42:25 -0800800 callbacks) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800801 callback->OnStatsDelivered(cached_report);
hbosc82f2e12016-09-05 01:36:50 -0700802 }
hbosd565b732016-08-30 14:04:35 -0700803}
804
hbosdf6075a2016-12-19 04:58:02 -0800805void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700806 int64_t timestamp_us,
807 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700808 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800809 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700810 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
811 if (transport_cert_stats_pair.second.local) {
812 ProduceCertificateStatsFromSSLCertificateStats(
813 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700814 }
hbos02ba2112016-10-28 05:14:53 -0700815 if (transport_cert_stats_pair.second.remote) {
816 ProduceCertificateStatsFromSSLCertificateStats(
817 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700818 }
819 }
820}
821
hbosdf6075a2016-12-19 04:58:02 -0800822void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -0800823 int64_t timestamp_us,
824 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -0800825 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800826 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -0800827 for (const auto& stats : transceiver_stats_infos) {
828 if (!stats.mid) {
829 continue;
hbos0adb8282016-11-23 02:32:06 -0800830 }
Steve Anton57858b32018-02-15 15:19:50 -0800831 const cricket::VoiceMediaInfo* voice_media_info =
832 stats.track_media_info_map->voice_media_info();
833 const cricket::VideoMediaInfo* video_media_info =
834 stats.track_media_info_map->video_media_info();
835 // Audio
836 if (voice_media_info) {
837 // Inbound
838 for (const auto& pair : voice_media_info->receive_codecs) {
839 report->AddStats(CodecStatsFromRtpCodecParameters(
840 timestamp_us, *stats.mid, true, pair.second));
841 }
842 // Outbound
843 for (const auto& pair : voice_media_info->send_codecs) {
844 report->AddStats(CodecStatsFromRtpCodecParameters(
845 timestamp_us, *stats.mid, false, pair.second));
846 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +0000847 }
Steve Anton57858b32018-02-15 15:19:50 -0800848 // Video
849 if (video_media_info) {
850 // Inbound
851 for (const auto& pair : video_media_info->receive_codecs) {
852 report->AddStats(CodecStatsFromRtpCodecParameters(
853 timestamp_us, *stats.mid, true, pair.second));
854 }
855 // Outbound
856 for (const auto& pair : video_media_info->send_codecs) {
857 report->AddStats(CodecStatsFromRtpCodecParameters(
858 timestamp_us, *stats.mid, false, pair.second));
859 }
hbos0adb8282016-11-23 02:32:06 -0800860 }
861 }
862}
863
hboscc555c52016-10-18 12:48:31 -0700864void RTCStatsCollector::ProduceDataChannelStats_s(
865 int64_t timestamp_us, RTCStatsReport* report) const {
866 RTC_DCHECK(signaling_thread_->IsCurrent());
867 for (const rtc::scoped_refptr<DataChannel>& data_channel :
868 pc_->sctp_data_channels()) {
869 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
870 new RTCDataChannelStats(
871 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
872 timestamp_us));
873 data_channel_stats->label = data_channel->label();
874 data_channel_stats->protocol = data_channel->protocol();
875 data_channel_stats->datachannelid = data_channel->id();
876 data_channel_stats->state =
877 DataStateToRTCDataChannelState(data_channel->state());
878 data_channel_stats->messages_sent = data_channel->messages_sent();
879 data_channel_stats->bytes_sent = data_channel->bytes_sent();
880 data_channel_stats->messages_received = data_channel->messages_received();
881 data_channel_stats->bytes_received = data_channel->bytes_received();
882 report->AddStats(std::move(data_channel_stats));
883 }
884}
885
hbosdf6075a2016-12-19 04:58:02 -0800886void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -0700887 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -0800888 const std::map<std::string, cricket::TransportStats>&
889 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -0700890 const Call::Stats& call_stats,
891 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800892 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -0800893 for (const auto& entry : transport_stats_by_name) {
894 const std::string& transport_name = entry.first;
895 const cricket::TransportStats& transport_stats = entry.second;
896 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800897 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -0800898 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700899 for (const cricket::ConnectionInfo& info :
900 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700901 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700902 new RTCIceCandidatePairStats(
903 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
904 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700905
hbos0583b282016-11-30 01:50:14 -0800906 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700907 // TODO(hbos): There could be other candidates that are not paired with
908 // anything. We don't have a complete list. Local candidates come from
909 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +0100910 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700911 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800912 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -0700913 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800914 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -0800915 candidate_pair_stats->state =
916 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
917 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -0800918 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -0700919 // TODO(hbos): This writable is different than the spec. It goes to
920 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +0100921 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -0700922 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700923 candidate_pair_stats->bytes_sent =
924 static_cast<uint64_t>(info.sent_total_bytes);
925 candidate_pair_stats->bytes_received =
926 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -0800927 candidate_pair_stats->total_round_trip_time =
928 static_cast<double>(info.total_round_trip_time_ms) /
929 rtc::kNumMillisecsPerSec;
930 if (info.current_round_trip_time_ms) {
931 candidate_pair_stats->current_round_trip_time =
932 static_cast<double>(*info.current_round_trip_time_ms) /
933 rtc::kNumMillisecsPerSec;
934 }
stefanf79ade12017-06-02 06:44:03 -0700935 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -0800936 // The bandwidth estimations we have are for the selected candidate
937 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -0700938 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
939 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
940 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -0800941 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -0700942 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -0800943 }
stefanf79ade12017-06-02 06:44:03 -0700944 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -0800945 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -0700946 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -0800947 }
948 }
hbosd82f5122016-12-09 04:12:39 -0800949 candidate_pair_stats->requests_received =
950 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -0800951 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
952 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700953 candidate_pair_stats->responses_received =
954 static_cast<uint64_t>(info.recv_ping_responses);
955 candidate_pair_stats->responses_sent =
956 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -0800957 RTC_DCHECK_GE(info.sent_ping_requests_total,
958 info.sent_ping_requests_before_first_response);
959 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
960 info.sent_ping_requests_total -
961 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700962
963 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700964 }
965 }
966 }
967}
968
Steve Anton57858b32018-02-15 15:19:50 -0800969void RTCStatsCollector::ProduceMediaStreamStats_s(
970 int64_t timestamp_us,
971 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -0800972 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -0800973
974 std::map<std::string, std::vector<std::string>> track_ids;
975
976 for (const auto& stats : transceiver_stats_infos_) {
977 for (auto sender : stats.transceiver->senders()) {
978 std::string track_id =
979 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
980 kSender, sender->internal()->AttachmentId());
981 for (auto& stream_id : sender->stream_ids()) {
982 track_ids[stream_id].push_back(track_id);
983 }
984 }
985 for (auto receiver : stats.transceiver->receivers()) {
986 std::string track_id =
987 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
988 kReceiver, receiver->internal()->AttachmentId());
989 for (auto& stream : receiver->streams()) {
990 track_ids[stream->label()].push_back(track_id);
991 }
992 }
993 }
994
995 // Build stats for each stream ID known.
996 for (auto& it : track_ids) {
997 std::unique_ptr<RTCMediaStreamStats> stream_stats(
998 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
999 stream_stats->stream_identifier = it.first;
1000 stream_stats->track_ids = it.second;
1001 report->AddStats(std::move(stream_stats));
1002 }
1003}
1004
1005void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1006 int64_t timestamp_us,
1007 RTCStatsReport* report) const {
1008 RTC_DCHECK(signaling_thread_->IsCurrent());
1009 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1010 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
1011 for (auto sender : stats.transceiver->senders()) {
1012 senders.push_back(sender->internal());
1013 }
1014 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1015 senders, report);
1016
1017 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
1018 for (auto receiver : stats.transceiver->receivers()) {
1019 receivers.push_back(receiver->internal());
1020 }
1021 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1022 receivers, report);
1023 }
hbos09bc1282016-11-08 06:29:22 -08001024}
1025
hbos6ab97ce2016-10-03 14:16:56 -07001026void RTCStatsCollector::ProducePeerConnectionStats_s(
1027 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001028 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001029 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001030 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001031 stats->data_channels_opened = internal_record_.data_channels_opened;
1032 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001033 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001034}
1035
hbosdf6075a2016-12-19 04:58:02 -08001036void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001037 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001038 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001039 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001040 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001041
Steve Anton57858b32018-02-15 15:19:50 -08001042 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1043 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1044 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1045 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1046 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1047 } else {
1048 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001049 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001050 }
Steve Anton57858b32018-02-15 15:19:50 -08001051}
1052
1053void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1054 int64_t timestamp_us,
1055 const RtpTransceiverStatsInfo& stats,
1056 RTCStatsReport* report) const {
1057 if (!stats.mid || !stats.transport_name) {
1058 return;
1059 }
1060 RTC_DCHECK(stats.track_media_info_map);
1061 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1062 RTC_DCHECK(track_media_info_map.voice_media_info());
1063 std::string mid = *stats.mid;
1064 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1065 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1066 // Inbound
1067 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1068 track_media_info_map.voice_media_info()->receivers) {
1069 if (!voice_receiver_info.connected())
1070 continue;
1071 auto inbound_audio = rtc::MakeUnique<RTCInboundRTPStreamStats>(
1072 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1073 timestamp_us);
1074 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1075 inbound_audio.get());
1076 // TODO(hta): This lookup should look for the sender, not the track.
1077 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1078 track_media_info_map.GetAudioTrack(voice_receiver_info);
1079 if (audio_track) {
1080 inbound_audio->track_id =
1081 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1082 kReceiver,
1083 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001084 }
Steve Anton57858b32018-02-15 15:19:50 -08001085 inbound_audio->transport_id = transport_id;
1086 report->AddStats(std::move(inbound_audio));
1087 }
1088 // Outbound
1089 for (const cricket::VoiceSenderInfo& voice_sender_info :
1090 track_media_info_map.voice_media_info()->senders) {
1091 if (!voice_sender_info.connected())
1092 continue;
1093 auto outbound_audio = rtc::MakeUnique<RTCOutboundRTPStreamStats>(
1094 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1095 timestamp_us);
1096 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1097 outbound_audio.get());
1098 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1099 track_media_info_map.GetAudioTrack(voice_sender_info);
1100 if (audio_track) {
1101 outbound_audio->track_id =
1102 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1103 kSender,
1104 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001105 }
Steve Anton57858b32018-02-15 15:19:50 -08001106 outbound_audio->transport_id = transport_id;
1107 report->AddStats(std::move(outbound_audio));
1108 }
1109}
1110
1111void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1112 int64_t timestamp_us,
1113 const RtpTransceiverStatsInfo& stats,
1114 RTCStatsReport* report) const {
1115 if (!stats.mid || !stats.transport_name) {
1116 return;
1117 }
1118 RTC_DCHECK(stats.track_media_info_map);
1119 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1120 RTC_DCHECK(track_media_info_map.video_media_info());
1121 std::string mid = *stats.mid;
1122 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1123 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1124 // Inbound
1125 for (const cricket::VideoReceiverInfo& video_receiver_info :
1126 track_media_info_map.video_media_info()->receivers) {
1127 if (!video_receiver_info.connected())
1128 continue;
1129 auto inbound_video = rtc::MakeUnique<RTCInboundRTPStreamStats>(
1130 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1131 timestamp_us);
1132 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1133 inbound_video.get());
1134 rtc::scoped_refptr<VideoTrackInterface> video_track =
1135 track_media_info_map.GetVideoTrack(video_receiver_info);
1136 if (video_track) {
1137 inbound_video->track_id =
1138 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1139 kReceiver,
1140 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1141 }
1142 inbound_video->transport_id = transport_id;
1143 report->AddStats(std::move(inbound_video));
1144 }
1145 // Outbound
1146 for (const cricket::VideoSenderInfo& video_sender_info :
1147 track_media_info_map.video_media_info()->senders) {
1148 if (!video_sender_info.connected())
1149 continue;
1150 auto outbound_video = rtc::MakeUnique<RTCOutboundRTPStreamStats>(
1151 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1152 timestamp_us);
1153 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1154 outbound_video.get());
1155 rtc::scoped_refptr<VideoTrackInterface> video_track =
1156 track_media_info_map.GetVideoTrack(video_sender_info);
1157 if (video_track) {
1158 outbound_video->track_id =
1159 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1160 kSender,
1161 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1162 }
1163 outbound_video->transport_id = transport_id;
1164 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001165 }
1166}
1167
hbosdf6075a2016-12-19 04:58:02 -08001168void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001169 int64_t timestamp_us,
1170 const std::map<std::string, cricket::TransportStats>&
1171 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001172 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1173 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001174 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001175 for (const auto& entry : transport_stats_by_name) {
1176 const std::string& transport_name = entry.first;
1177 const cricket::TransportStats& transport_stats = entry.second;
1178
hbos2fa7c672016-10-24 04:00:05 -07001179 // Get reference to RTCP channel, if it exists.
1180 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001181 for (const cricket::TransportChannelStats& channel_stats :
1182 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001183 if (channel_stats.component ==
1184 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1185 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001186 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001187 break;
1188 }
1189 }
1190
1191 // Get reference to local and remote certificates of this transport, if they
1192 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001193 const auto& certificate_stats_it =
1194 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001195 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1196 std::string local_certificate_id;
1197 if (certificate_stats_it->second.local) {
1198 local_certificate_id = RTCCertificateIDFromFingerprint(
1199 certificate_stats_it->second.local->fingerprint);
1200 }
1201 std::string remote_certificate_id;
1202 if (certificate_stats_it->second.remote) {
1203 remote_certificate_id = RTCCertificateIDFromFingerprint(
1204 certificate_stats_it->second.remote->fingerprint);
1205 }
1206
1207 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001208 for (const cricket::TransportChannelStats& channel_stats :
1209 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001210 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001211 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1212 transport_name, channel_stats.component),
1213 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001214 transport_stats->bytes_sent = 0;
1215 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001216 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1217 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001218 for (const cricket::ConnectionInfo& info :
1219 channel_stats.connection_infos) {
1220 *transport_stats->bytes_sent += info.sent_total_bytes;
1221 *transport_stats->bytes_received += info.recv_total_bytes;
1222 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001223 transport_stats->selected_candidate_pair_id =
1224 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1225 }
1226 }
1227 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1228 !rtcp_transport_stats_id.empty()) {
1229 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1230 }
1231 if (!local_certificate_id.empty())
1232 transport_stats->local_certificate_id = local_certificate_id;
1233 if (!remote_certificate_id.empty())
1234 transport_stats->remote_certificate_id = remote_certificate_id;
1235 report->AddStats(std::move(transport_stats));
1236 }
1237 }
1238}
1239
1240std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001241RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001242 const std::map<std::string, cricket::TransportStats>&
1243 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001244 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001245 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001246 for (const auto& entry : transport_stats_by_name) {
1247 const std::string& transport_name = entry.first;
1248
hbos2fa7c672016-10-24 04:00:05 -07001249 CertificateStatsPair certificate_stats_pair;
1250 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001251 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001252 certificate_stats_pair.local =
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001253 local_certificate->ssl_cert_chain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001254 }
Steve Anton5dfde182018-02-06 10:34:40 -08001255
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001256 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1257 pc_->GetRemoteSSLCertChain(transport_name);
1258 if (remote_cert_chain) {
1259 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001260 }
Steve Anton5dfde182018-02-06 10:34:40 -08001261
hbos2fa7c672016-10-24 04:00:05 -07001262 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001263 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001264 }
1265 return transport_cert_stats;
1266}
1267
Steve Anton57858b32018-02-15 15:19:50 -08001268std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1269RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1270 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001271
Steve Anton57858b32018-02-15 15:19:50 -08001272 // These are used to invoke GetStats for all the media channels together in
1273 // one worker thread hop.
1274 std::map<cricket::VoiceMediaChannel*,
1275 std::unique_ptr<cricket::VoiceMediaInfo>>
1276 voice_stats;
1277 std::map<cricket::VideoMediaChannel*,
1278 std::unique_ptr<cricket::VideoMediaInfo>>
1279 video_stats;
1280
1281 for (auto transceiver : pc_->GetTransceiversInternal()) {
1282 cricket::MediaType media_type = transceiver->media_type();
1283
1284 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1285 // stats have been fetched on the worker thread.
1286 transceiver_stats_infos.emplace_back();
1287 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1288 stats.transceiver = transceiver->internal();
1289 stats.media_type = media_type;
1290
1291 cricket::BaseChannel* channel = transceiver->internal()->channel();
1292 if (!channel) {
1293 // The remaining fields require a BaseChannel.
1294 continue;
1295 }
1296
1297 stats.mid = channel->content_name();
1298 stats.transport_name = channel->transport_name();
1299
1300 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1301 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1302 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1303 voice_stats.end());
1304 voice_stats[voice_channel->media_channel()] =
1305 rtc::MakeUnique<cricket::VoiceMediaInfo>();
1306 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1307 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1308 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1309 video_stats.end());
1310 video_stats[video_channel->media_channel()] =
1311 rtc::MakeUnique<cricket::VideoMediaInfo>();
1312 } else {
1313 RTC_NOTREACHED();
1314 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001315 }
Steve Anton57858b32018-02-15 15:19:50 -08001316
1317 // Call GetStats for all media channels together on the worker thread in one
1318 // hop.
1319 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1320 for (const auto& entry : voice_stats) {
1321 if (!entry.first->GetStats(entry.second.get())) {
1322 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1323 }
1324 }
1325 for (const auto& entry : video_stats) {
1326 if (!entry.first->GetStats(entry.second.get())) {
1327 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1328 }
1329 }
1330 });
1331
1332 // Create the TrackMediaInfoMap for each transceiver stats object.
1333 for (auto& stats : transceiver_stats_infos) {
1334 auto transceiver = stats.transceiver;
1335 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1336 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1337 if (transceiver->channel()) {
1338 cricket::MediaType media_type = transceiver->media_type();
1339 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1340 auto* voice_channel =
1341 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1342 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1343 voice_media_info =
1344 std::move(voice_stats[voice_channel->media_channel()]);
1345 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1346 auto* video_channel =
1347 static_cast<cricket::VideoChannel*>(transceiver->channel());
1348 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1349 video_media_info =
1350 std::move(video_stats[video_channel->media_channel()]);
1351 }
1352 }
1353 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
1354 for (auto sender : transceiver->senders()) {
1355 senders.push_back(sender->internal());
1356 }
1357 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
1358 for (auto receiver : transceiver->receivers()) {
1359 receivers.push_back(receiver->internal());
1360 }
1361 stats.track_media_info_map = rtc::MakeUnique<TrackMediaInfoMap>(
1362 std::move(voice_media_info), std::move(video_media_info), senders,
1363 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001364 }
Steve Anton57858b32018-02-15 15:19:50 -08001365
1366 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001367}
1368
hbos82ebe022016-11-14 01:41:09 -08001369void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1370 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1371 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1372}
1373
1374void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1375 RTC_DCHECK(signaling_thread_->IsCurrent());
1376 bool result = internal_record_.opened_data_channels.insert(
1377 reinterpret_cast<uintptr_t>(channel)).second;
1378 ++internal_record_.data_channels_opened;
1379 RTC_DCHECK(result);
1380}
1381
1382void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1383 RTC_DCHECK(signaling_thread_->IsCurrent());
1384 // Only channels that have been fully opened (and have increased the
1385 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001386 if (internal_record_.opened_data_channels.erase(
1387 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001388 ++internal_record_.data_channels_closed;
1389 }
1390}
1391
hboscc555c52016-10-18 12:48:31 -07001392const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1393 const std::string& type) {
1394 return CandidateTypeToRTCIceCandidateType(type);
1395}
1396
1397const char* DataStateToRTCDataChannelStateForTesting(
1398 DataChannelInterface::DataState state) {
1399 return DataStateToRTCDataChannelState(state);
1400}
1401
hbosd565b732016-08-30 14:04:35 -07001402} // namespace webrtc