blob: 35cae8cb8a5792b7048e7f0d066ccd708bc0fb7e [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>
Steve Anton36b29d12017-10-30 09:57:42 -070014#include <string>
hbosd565b732016-08-30 14:04:35 -070015#include <utility>
16#include <vector>
17
Patrik Höglunde2d6a062017-10-05 14:53:33 +020018#include "api/candidate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/mediastreaminterface.h"
20#include "api/peerconnectioninterface.h"
21#include "media/base/mediachannel.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "p2p/base/p2pconstants.h"
23#include "p2p/base/port.h"
24#include "pc/peerconnection.h"
Henrik Boström5b3541f2018-03-19 13:52:56 +010025#include "pc/rtcstatstraversal.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"
Jonas Olsson43568dd2018-06-11 16:25:54 +020028#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#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) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020043 char buf[1024];
44 rtc::SimpleStringBuilder sb(buf);
45 sb << "RTCCodec_" << mid << (inbound ? "_Inbound_" : "_Outbound_")
46 << payload_type;
47 return sb.str();
hbos0adb8282016-11-23 02:32:06 -080048}
49
hbos2fa7c672016-10-24 04:00:05 -070050std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
51 const cricket::ConnectionInfo& info) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020052 char buf[4096];
53 rtc::SimpleStringBuilder sb(buf);
54 sb << "RTCIceCandidatePair_" << info.local_candidate.id() << "_"
55 << info.remote_candidate.id();
56 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070057}
58
Harald Alvestranda3dab842018-01-14 09:18:58 +010059const char kSender[] = "sender";
60const char kReceiver[] = "receiver";
61
62std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
63 const char* direction,
Harald Alvestrandc72af932018-01-11 17:18:19 +010064 int attachment_id) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020065 char buf[1024];
66 rtc::SimpleStringBuilder sb(buf);
67 sb << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
68 return sb.str();
hbos09bc1282016-11-08 06:29:22 -080069}
70
hbos2fa7c672016-10-24 04:00:05 -070071std::string RTCTransportStatsIDFromTransportChannel(
72 const std::string& transport_name, int channel_component) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020073 char buf[1024];
74 rtc::SimpleStringBuilder sb(buf);
75 sb << "RTCTransport_" << transport_name << "_" << channel_component;
76 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070077}
78
hboseeafe942016-11-01 03:00:17 -070079std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020080 char buf[1024];
81 rtc::SimpleStringBuilder sb(buf);
82 sb << "RTCInboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
83 return sb.str();
hboseeafe942016-11-01 03:00:17 -070084}
85
hbos6ded1902016-11-01 01:50:46 -070086std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020087 char buf[1024];
88 rtc::SimpleStringBuilder sb(buf);
89 sb << "RTCOutboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
90 return sb.str();
hbos6ded1902016-11-01 01:50:46 -070091}
92
hbosab9f6e42016-10-07 02:18:47 -070093const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
94 if (type == cricket::LOCAL_PORT_TYPE)
95 return RTCIceCandidateType::kHost;
96 if (type == cricket::STUN_PORT_TYPE)
97 return RTCIceCandidateType::kSrflx;
98 if (type == cricket::PRFLX_PORT_TYPE)
99 return RTCIceCandidateType::kPrflx;
100 if (type == cricket::RELAY_PORT_TYPE)
101 return RTCIceCandidateType::kRelay;
102 RTC_NOTREACHED();
103 return nullptr;
104}
105
hboscc555c52016-10-18 12:48:31 -0700106const char* DataStateToRTCDataChannelState(
107 DataChannelInterface::DataState state) {
108 switch (state) {
109 case DataChannelInterface::kConnecting:
110 return RTCDataChannelState::kConnecting;
111 case DataChannelInterface::kOpen:
112 return RTCDataChannelState::kOpen;
113 case DataChannelInterface::kClosing:
114 return RTCDataChannelState::kClosing;
115 case DataChannelInterface::kClosed:
116 return RTCDataChannelState::kClosed;
117 default:
118 RTC_NOTREACHED();
119 return nullptr;
120 }
121}
122
hbos06495bc2017-01-02 08:08:18 -0800123const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
124 cricket::IceCandidatePairState state) {
125 switch (state) {
126 case cricket::IceCandidatePairState::WAITING:
127 return RTCStatsIceCandidatePairState::kWaiting;
128 case cricket::IceCandidatePairState::IN_PROGRESS:
129 return RTCStatsIceCandidatePairState::kInProgress;
130 case cricket::IceCandidatePairState::SUCCEEDED:
131 return RTCStatsIceCandidatePairState::kSucceeded;
132 case cricket::IceCandidatePairState::FAILED:
133 return RTCStatsIceCandidatePairState::kFailed;
134 default:
135 RTC_NOTREACHED();
136 return nullptr;
137 }
138}
139
hbos7064d592017-01-16 07:38:02 -0800140const char* DtlsTransportStateToRTCDtlsTransportState(
141 cricket::DtlsTransportState state) {
142 switch (state) {
143 case cricket::DTLS_TRANSPORT_NEW:
144 return RTCDtlsTransportState::kNew;
145 case cricket::DTLS_TRANSPORT_CONNECTING:
146 return RTCDtlsTransportState::kConnecting;
147 case cricket::DTLS_TRANSPORT_CONNECTED:
148 return RTCDtlsTransportState::kConnected;
149 case cricket::DTLS_TRANSPORT_CLOSED:
150 return RTCDtlsTransportState::kClosed;
151 case cricket::DTLS_TRANSPORT_FAILED:
152 return RTCDtlsTransportState::kFailed;
153 default:
154 RTC_NOTREACHED();
155 return nullptr;
156 }
157}
158
Gary Liu37e489c2017-11-21 10:49:36 -0800159const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
160 switch (type) {
161 case rtc::ADAPTER_TYPE_CELLULAR:
162 return RTCNetworkType::kCellular;
163 case rtc::ADAPTER_TYPE_ETHERNET:
164 return RTCNetworkType::kEthernet;
165 case rtc::ADAPTER_TYPE_WIFI:
166 return RTCNetworkType::kWifi;
167 case rtc::ADAPTER_TYPE_VPN:
168 return RTCNetworkType::kVpn;
169 case rtc::ADAPTER_TYPE_UNKNOWN:
170 case rtc::ADAPTER_TYPE_LOOPBACK:
Qingsi Wang9f1de692018-06-28 15:38:09 -0700171 case rtc::ADAPTER_TYPE_ANY:
Gary Liu37e489c2017-11-21 10:49:36 -0800172 return RTCNetworkType::kUnknown;
173 }
174 RTC_NOTREACHED();
175 return nullptr;
176}
177
hbos9e302742017-01-20 02:47:10 -0800178double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
179 RTC_DCHECK_GE(audio_level, 0);
180 RTC_DCHECK_LE(audio_level, 32767);
181 return audio_level / 32767.0;
182}
183
hbos0adb8282016-11-23 02:32:06 -0800184std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 15:19:50 -0800185 uint64_t timestamp_us,
186 const std::string& mid,
187 bool inbound,
hbos0adb8282016-11-23 02:32:06 -0800188 const RtpCodecParameters& codec_params) {
189 RTC_DCHECK_GE(codec_params.payload_type, 0);
190 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 12:09:01 -0800191 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 02:32:06 -0800192 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
193 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 15:19:50 -0800194 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 02:32:06 -0800195 timestamp_us));
196 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 06:56:04 -0800197 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 12:09:01 -0800198 if (codec_params.clock_rate) {
199 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
200 }
hbos0adb8282016-11-23 02:32:06 -0800201 return codec_stats;
202}
203
hbos09bc1282016-11-08 06:29:22 -0800204void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
205 const MediaStreamTrackInterface& track,
206 RTCMediaStreamTrackStats* track_stats) {
207 track_stats->track_identifier = track.id();
208 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
209}
210
hbos820f5782016-11-22 03:16:50 -0800211// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700212void SetInboundRTPStreamStatsFromMediaReceiverInfo(
213 const cricket::MediaReceiverInfo& media_receiver_info,
214 RTCInboundRTPStreamStats* inbound_stats) {
215 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800216 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100217 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 03:00:17 -0700218 inbound_stats->is_remote = false;
hboseeafe942016-11-01 03:00:17 -0700219 inbound_stats->packets_received =
220 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
221 inbound_stats->bytes_received =
222 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800223 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 12:26:04 +0100224 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700225 inbound_stats->fraction_lost =
226 static_cast<double>(media_receiver_info.fraction_lost);
227}
228
229void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800230 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700231 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800232 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700233 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800234 voice_receiver_info, inbound_audio);
235 inbound_audio->media_type = "audio";
hbos585a9b12017-02-07 04:59:16 -0800236 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800237 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
238 mid, true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800239 }
hbos820f5782016-11-22 03:16:50 -0800240 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700241 static_cast<double>(voice_receiver_info.jitter_ms) /
242 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800243 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
244 // purposefully left undefined for audio.
hboseeafe942016-11-01 03:00:17 -0700245}
246
247void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800248 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700249 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800250 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700251 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800252 video_receiver_info, inbound_video);
253 inbound_video->media_type = "video";
hbos585a9b12017-02-07 04:59:16 -0800254 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800255 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
256 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800257 }
hbos820f5782016-11-22 03:16:50 -0800258 inbound_video->fir_count =
259 static_cast<uint32_t>(video_receiver_info.firs_sent);
260 inbound_video->pli_count =
261 static_cast<uint32_t>(video_receiver_info.plis_sent);
262 inbound_video->nack_count =
263 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800264 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800265 if (video_receiver_info.qp_sum)
266 inbound_video->qp_sum = *video_receiver_info.qp_sum;
hboseeafe942016-11-01 03:00:17 -0700267}
268
hbos820f5782016-11-22 03:16:50 -0800269// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700270void SetOutboundRTPStreamStatsFromMediaSenderInfo(
271 const cricket::MediaSenderInfo& media_sender_info,
272 RTCOutboundRTPStreamStats* outbound_stats) {
273 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800274 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100275 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700276 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700277 outbound_stats->packets_sent =
278 static_cast<uint32_t>(media_sender_info.packets_sent);
279 outbound_stats->bytes_sent =
280 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbos6ded1902016-11-01 01:50:46 -0700281}
282
283void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800284 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700285 const cricket::VoiceSenderInfo& voice_sender_info,
286 RTCOutboundRTPStreamStats* outbound_audio) {
287 SetOutboundRTPStreamStatsFromMediaSenderInfo(
288 voice_sender_info, outbound_audio);
289 outbound_audio->media_type = "audio";
hbos585a9b12017-02-07 04:59:16 -0800290 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800291 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
292 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800293 }
hbos6ded1902016-11-01 01:50:46 -0700294 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
295 // purposefully left undefined for audio.
296}
297
298void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800299 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700300 const cricket::VideoSenderInfo& video_sender_info,
301 RTCOutboundRTPStreamStats* outbound_video) {
302 SetOutboundRTPStreamStatsFromMediaSenderInfo(
303 video_sender_info, outbound_video);
304 outbound_video->media_type = "video";
hbos585a9b12017-02-07 04:59:16 -0800305 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800306 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
307 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800308 }
hbos6ded1902016-11-01 01:50:46 -0700309 outbound_video->fir_count =
310 static_cast<uint32_t>(video_sender_info.firs_rcvd);
311 outbound_video->pli_count =
312 static_cast<uint32_t>(video_sender_info.plis_rcvd);
313 outbound_video->nack_count =
314 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800315 if (video_sender_info.qp_sum)
316 outbound_video->qp_sum = *video_sender_info.qp_sum;
317 outbound_video->frames_encoded = video_sender_info.frames_encoded;
hbos6ded1902016-11-01 01:50:46 -0700318}
319
hbos02ba2112016-10-28 05:14:53 -0700320void ProduceCertificateStatsFromSSLCertificateStats(
321 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
322 RTCStatsReport* report) {
323 RTCCertificateStats* prev_certificate_stats = nullptr;
324 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
325 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800326 std::string certificate_stats_id =
327 RTCCertificateIDFromFingerprint(s->fingerprint);
328 // It is possible for the same certificate to show up multiple times, e.g.
329 // if local and remote side use the same certificate in a loopback call.
330 // If the report already contains stats for this certificate, skip it.
331 if (report->Get(certificate_stats_id)) {
332 RTC_DCHECK_EQ(s, &certificate_stats);
333 break;
334 }
hbos02ba2112016-10-28 05:14:53 -0700335 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800336 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700337 certificate_stats->fingerprint = s->fingerprint;
338 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
339 certificate_stats->base64_certificate = s->base64_certificate;
340 if (prev_certificate_stats)
341 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
342 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
343 prev_certificate_stats = certificate_stats;
344 }
345}
346
347const std::string& ProduceIceCandidateStats(
348 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800349 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700350 const std::string& id = "RTCIceCandidate_" + candidate.id();
351 const RTCStats* stats = report->Get(id);
352 if (!stats) {
353 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
354 if (is_local)
355 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
356 else
357 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800358 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800359 if (is_local) {
360 candidate_stats->network_type =
361 NetworkAdapterTypeToStatsType(candidate.network_type());
362 } else {
363 // We don't expect to know the adapter type of remote candidates.
364 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
365 }
hbos02ba2112016-10-28 05:14:53 -0700366 candidate_stats->ip = candidate.address().ipaddr().ToString();
367 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
368 candidate_stats->protocol = candidate.protocol();
369 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
370 candidate.type());
371 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
372
373 stats = candidate_stats.get();
374 report->AddStats(std::move(candidate_stats));
375 }
376 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
377 : RTCRemoteIceCandidateStats::kType);
378 return stats->id();
379}
380
hbos9e302742017-01-20 02:47:10 -0800381std::unique_ptr<RTCMediaStreamTrackStats>
382ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
383 int64_t timestamp_us,
384 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100385 const cricket::VoiceSenderInfo& voice_sender_info,
386 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800387 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
388 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100389 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
390 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100391 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800392 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
393 audio_track, audio_track_stats.get());
394 audio_track_stats->remote_source = false;
395 audio_track_stats->detached = false;
396 if (voice_sender_info.audio_level >= 0) {
397 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
398 voice_sender_info.audio_level);
399 }
zsteine76bd3a2017-07-14 12:17:49 -0700400 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
401 audio_track_stats->total_samples_duration =
402 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100403 if (voice_sender_info.apm_statistics.echo_return_loss) {
404 audio_track_stats->echo_return_loss =
405 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800406 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100407 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
408 audio_track_stats->echo_return_loss_enhancement =
409 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800410 }
411 return audio_track_stats;
412}
413
414std::unique_ptr<RTCMediaStreamTrackStats>
415ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
416 int64_t timestamp_us,
417 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100418 const cricket::VoiceReceiverInfo& voice_receiver_info,
419 int attachment_id) {
420 // Since receiver tracks can't be reattached, we use the SSRC as
421 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800422 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
423 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100424 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
425 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100426 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800427 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
428 audio_track, audio_track_stats.get());
429 audio_track_stats->remote_source = true;
430 audio_track_stats->detached = false;
431 if (voice_receiver_info.audio_level >= 0) {
432 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
433 voice_receiver_info.audio_level);
434 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200435 audio_track_stats->jitter_buffer_delay =
436 voice_receiver_info.jitter_buffer_delay_seconds;
zsteine76bd3a2017-07-14 12:17:49 -0700437 audio_track_stats->total_audio_energy =
438 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700439 audio_track_stats->total_samples_received =
440 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700441 audio_track_stats->total_samples_duration =
442 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700443 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200444 audio_track_stats->concealment_events =
445 voice_receiver_info.concealment_events;
hbos9e302742017-01-20 02:47:10 -0800446 return audio_track_stats;
447}
448
449std::unique_ptr<RTCMediaStreamTrackStats>
450ProduceMediaStreamTrackStatsFromVideoSenderInfo(
451 int64_t timestamp_us,
452 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100453 const cricket::VideoSenderInfo& video_sender_info,
454 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800455 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
456 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100457 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
458
459 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100460 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800461 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
462 video_track, video_track_stats.get());
463 video_track_stats->remote_source = false;
464 video_track_stats->detached = false;
465 video_track_stats->frame_width = static_cast<uint32_t>(
466 video_sender_info.send_frame_width);
467 video_track_stats->frame_height = static_cast<uint32_t>(
468 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800469 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100470 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800471 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100472 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800473 return video_track_stats;
474}
475
476std::unique_ptr<RTCMediaStreamTrackStats>
477ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
478 int64_t timestamp_us,
479 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100480 const cricket::VideoReceiverInfo& video_receiver_info,
481 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800482 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
483 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100484 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
485
486 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100487 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800488 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
489 video_track, video_track_stats.get());
490 video_track_stats->remote_source = true;
491 video_track_stats->detached = false;
492 if (video_receiver_info.frame_width > 0 &&
493 video_receiver_info.frame_height > 0) {
494 video_track_stats->frame_width = static_cast<uint32_t>(
495 video_receiver_info.frame_width);
496 video_track_stats->frame_height = static_cast<uint32_t>(
497 video_receiver_info.frame_height);
498 }
hbos42f6d2f2017-01-20 03:56:50 -0800499 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800500 // TODO(hbos): When we support receiving simulcast, this should be the total
501 // number of frames correctly decoded, independent of which SSRC it was
502 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100503 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800504 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800505 RTC_DCHECK_GE(video_receiver_info.frames_received,
506 video_receiver_info.frames_rendered);
507 video_track_stats->frames_dropped = video_receiver_info.frames_received -
508 video_receiver_info.frames_rendered;
hbos9e302742017-01-20 02:47:10 -0800509 return video_track_stats;
510}
511
Harald Alvestrand89061872018-01-02 14:08:34 +0100512void ProduceSenderMediaTrackStats(
513 int64_t timestamp_us,
514 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800515 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100516 RTCStatsReport* report) {
517 // This function iterates over the senders to generate outgoing track stats.
518
519 // TODO(hbos): Return stats of detached tracks. We have to perform stats
520 // gathering at the time of detachment to get accurate stats and timestamps.
521 // https://crbug.com/659137
Steve Anton57858b32018-02-15 15:19:50 -0800522 for (auto sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100523 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
524 AudioTrackInterface* track =
525 static_cast<AudioTrackInterface*>(sender->track().get());
526 if (!track)
527 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100528 cricket::VoiceSenderInfo null_sender_info;
529 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
530 // TODO(hta): Checking on ssrc is not proper. There should be a way
531 // to see from a sender whether it's connected or not.
532 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800533 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100534 // When pc.close is called, sender info is discarded, so
535 // we generate zeroes instead. Bug: It should be retained.
536 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800537 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100538 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100539 if (sender_info) {
540 voice_sender_info = sender_info;
541 } else {
542 RTC_LOG(LS_INFO)
543 << "RTCStatsCollector: No voice sender info for sender with ssrc "
544 << sender->ssrc();
545 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100546 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100547 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100548 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
549 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100550 report->AddStats(std::move(audio_track_stats));
551 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
552 VideoTrackInterface* track =
553 static_cast<VideoTrackInterface*>(sender->track().get());
554 if (!track)
555 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100556 cricket::VideoSenderInfo null_sender_info;
557 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
558 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100559 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
560 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800561 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100562 // When pc.close is called, sender info is discarded, so
563 // we generate zeroes instead. Bug: It should be retained.
564 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800565 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100566 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100567 if (sender_info) {
568 video_sender_info = sender_info;
569 } else {
570 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
571 << sender->ssrc();
572 }
573 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100574 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100575 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
576 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100577 report->AddStats(std::move(video_track_stats));
578 } else {
579 RTC_NOTREACHED();
580 }
581 }
582}
583
584void ProduceReceiverMediaTrackStats(
585 int64_t timestamp_us,
586 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800587 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100588 RTCStatsReport* report) {
589 // This function iterates over the receivers to find the remote tracks.
Steve Anton57858b32018-02-15 15:19:50 -0800590 for (auto receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100591 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
592 AudioTrackInterface* track =
593 static_cast<AudioTrackInterface*>(receiver->track().get());
594 const cricket::VoiceReceiverInfo* voice_receiver_info =
595 track_media_info_map.GetVoiceReceiverInfo(*track);
596 if (!voice_receiver_info) {
597 continue;
598 }
599 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
600 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100601 timestamp_us, *track, *voice_receiver_info,
602 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100603 report->AddStats(std::move(audio_track_stats));
604 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
605 VideoTrackInterface* track =
606 static_cast<VideoTrackInterface*>(receiver->track().get());
607 const cricket::VideoReceiverInfo* video_receiver_info =
608 track_media_info_map.GetVideoReceiverInfo(*track);
609 if (!video_receiver_info) {
610 continue;
611 }
612 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
613 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100614 timestamp_us, *track, *video_receiver_info,
615 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100616 report->AddStats(std::move(video_track_stats));
617 } else {
618 RTC_NOTREACHED();
619 }
620 }
621}
622
Henrik Boström5b3541f2018-03-19 13:52:56 +0100623rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
624 bool filter_by_sender_selector,
625 rtc::scoped_refptr<const RTCStatsReport> report,
626 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
627 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
628 std::vector<std::string> rtpstream_ids;
629 if (filter_by_sender_selector) {
630 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
631 if (sender_selector) {
632 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
633 // reference the sender stats.
634 // Because we do not implement sender stats, we look at outbound-rtp(s)
635 // that reference the track attachment stats for the sender instead.
636 std::string track_id =
637 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
638 kSender, sender_selector->AttachmentId());
639 for (const auto& stats : *report) {
640 if (stats.type() != RTCOutboundRTPStreamStats::kType)
641 continue;
642 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
643 if (outbound_rtp.track_id.is_defined() &&
644 *outbound_rtp.track_id == track_id) {
645 rtpstream_ids.push_back(outbound_rtp.id());
646 }
647 }
648 }
649 } else {
650 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
651 if (receiver_selector) {
652 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
653 // reference the receiver stats.
654 // Because we do not implement receiver stats, we look at inbound-rtp(s)
655 // that reference the track attachment stats for the receiver instead.
656 std::string track_id =
657 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
658 kReceiver, receiver_selector->AttachmentId());
659 for (const auto& stats : *report) {
660 if (stats.type() != RTCInboundRTPStreamStats::kType)
661 continue;
662 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
663 if (inbound_rtp.track_id.is_defined() &&
664 *inbound_rtp.track_id == track_id) {
665 rtpstream_ids.push_back(inbound_rtp.id());
666 }
667 }
668 }
669 }
670 if (rtpstream_ids.empty())
671 return RTCStatsReport::Create(report->timestamp_us());
672 return TakeReferencedStats(report->Copy(), rtpstream_ids);
673}
674
hboscc555c52016-10-18 12:48:31 -0700675} // namespace
676
Henrik Boström5b3541f2018-03-19 13:52:56 +0100677RTCStatsCollector::RequestInfo::RequestInfo(
678 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
679 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
680
681RTCStatsCollector::RequestInfo::RequestInfo(
682 rtc::scoped_refptr<RtpSenderInternal> selector,
683 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
684 : RequestInfo(FilterMode::kSenderSelector,
685 std::move(callback),
686 std::move(selector),
687 nullptr) {}
688
689RTCStatsCollector::RequestInfo::RequestInfo(
690 rtc::scoped_refptr<RtpReceiverInternal> selector,
691 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
692 : RequestInfo(FilterMode::kReceiverSelector,
693 std::move(callback),
694 nullptr,
695 std::move(selector)) {}
696
697RTCStatsCollector::RequestInfo::RequestInfo(
698 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
699 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
700 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
701 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
702 : filter_mode_(filter_mode),
703 callback_(std::move(callback)),
704 sender_selector_(std::move(sender_selector)),
705 receiver_selector_(std::move(receiver_selector)) {
706 RTC_DCHECK(callback_);
707 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
708}
709
hbosc82f2e12016-09-05 01:36:50 -0700710rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800711 PeerConnectionInternal* pc,
712 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700713 return rtc::scoped_refptr<RTCStatsCollector>(
714 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
715}
716
Steve Anton2d8609c2018-01-23 16:38:46 -0800717RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700718 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700719 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700720 signaling_thread_(pc->signaling_thread()),
721 worker_thread_(pc->worker_thread()),
722 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700723 num_pending_partial_reports_(0),
724 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700725 cache_timestamp_us_(0),
726 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700727 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700728 RTC_DCHECK(signaling_thread_);
729 RTC_DCHECK(worker_thread_);
730 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700731 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800732 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800733 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700734}
735
hbosb78306a2016-12-19 05:06:57 -0800736RTCStatsCollector::~RTCStatsCollector() {
737 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
738}
739
hbosc82f2e12016-09-05 01:36:50 -0700740void RTCStatsCollector::GetStatsReport(
741 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100742 GetStatsReportInternal(RequestInfo(std::move(callback)));
743}
744
745void RTCStatsCollector::GetStatsReport(
746 rtc::scoped_refptr<RtpSenderInternal> selector,
747 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
748 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
749}
750
751void RTCStatsCollector::GetStatsReport(
752 rtc::scoped_refptr<RtpReceiverInternal> selector,
753 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
754 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
755}
756
757void RTCStatsCollector::GetStatsReportInternal(
758 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700759 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100760 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700761
hbos0e6758d2016-08-31 07:57:36 -0700762 // "Now" using a monotonically increasing timer.
763 int64_t cache_now_us = rtc::TimeMicros();
764 if (cached_report_ &&
765 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800766 // We have a fresh cached report to deliver. Deliver asynchronously, since
767 // the caller may not be expecting a synchronous callback, and it avoids
768 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100769 std::vector<RequestInfo> requests;
770 requests.swap(requests_);
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800771 invoker_.AsyncInvoke<void>(
772 RTC_FROM_HERE, signaling_thread_,
773 rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this, cached_report_,
Henrik Boström5b3541f2018-03-19 13:52:56 +0100774 std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700775 } else if (!num_pending_partial_reports_) {
776 // Only start gathering stats if we're not already gathering stats. In the
777 // case of already gathering stats, |callback_| will be invoked when there
778 // are no more pending partial reports.
779
780 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
781 // UTC), in microseconds. The system clock could be modified and is not
782 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700783 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700784
hbosf415f8a2017-01-02 04:28:51 -0800785 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700786 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800787
Steve Anton57858b32018-02-15 15:19:50 -0800788 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800789 // |ProducePartialResultsOnNetworkThread| and
790 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800791 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -0700792 // Prepare |transport_names_| for use in
793 // |ProducePartialResultsOnNetworkThread|.
794 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800795
stefanf79ade12017-06-02 06:44:03 -0700796 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
797 // thread.
798 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
799 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700800 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700801
802 invoker_.AsyncInvoke<void>(
803 RTC_FROM_HERE, network_thread_,
hbosc82f2e12016-09-05 01:36:50 -0700804 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
stefanf79ade12017-06-02 06:44:03 -0700805 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800806 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700807 }
hbosd565b732016-08-30 14:04:35 -0700808}
809
810void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700811 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700812 cached_report_ = nullptr;
813}
814
hbosb78306a2016-12-19 05:06:57 -0800815void RTCStatsCollector::WaitForPendingRequest() {
816 RTC_DCHECK(signaling_thread_->IsCurrent());
817 if (num_pending_partial_reports_) {
818 rtc::Thread::Current()->ProcessMessages(0);
819 while (num_pending_partial_reports_) {
820 rtc::Thread::Current()->SleepMs(1);
821 rtc::Thread::Current()->ProcessMessages(0);
822 }
823 }
824}
825
hbosc82f2e12016-09-05 01:36:50 -0700826void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
827 int64_t timestamp_us) {
828 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700829 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
830 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700831
hboscc555c52016-10-18 12:48:31 -0700832 ProduceDataChannelStats_s(timestamp_us, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800833 ProduceMediaStreamStats_s(timestamp_us, report.get());
834 ProduceMediaStreamTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700835 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700836
837 AddPartialResults(report);
838}
839
hbosc82f2e12016-09-05 01:36:50 -0700840void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
841 int64_t timestamp_us) {
842 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700843 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
844 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700845
Steve Anton5dfde182018-02-06 10:34:40 -0800846 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -0700847 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -0800848
849 std::map<std::string, CertificateStatsPair> transport_cert_stats =
850 PrepareTransportCertificateStats_n(transport_stats_by_name);
851
852 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800853 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, report.get());
Steve Anton5dfde182018-02-06 10:34:40 -0800854 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
Steve Anton5dfde182018-02-06 10:34:40 -0800855 call_stats_, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800856 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_, report.get());
Steve Anton5dfde182018-02-06 10:34:40 -0800857 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
858 transport_cert_stats, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700859
860 AddPartialResults(report);
861}
862
863void RTCStatsCollector::AddPartialResults(
864 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
865 if (!signaling_thread_->IsCurrent()) {
866 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
867 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
868 rtc::scoped_refptr<RTCStatsCollector>(this),
869 partial_report));
870 return;
871 }
872 AddPartialResults_s(partial_report);
873}
874
875void RTCStatsCollector::AddPartialResults_s(
876 rtc::scoped_refptr<RTCStatsReport> partial_report) {
877 RTC_DCHECK(signaling_thread_->IsCurrent());
878 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
879 if (!partial_report_)
880 partial_report_ = partial_report;
881 else
882 partial_report_->TakeMembersFrom(partial_report);
883 --num_pending_partial_reports_;
884 if (!num_pending_partial_reports_) {
885 cache_timestamp_us_ = partial_report_timestamp_us_;
886 cached_report_ = partial_report_;
887 partial_report_ = nullptr;
Steve Anton57858b32018-02-15 15:19:50 -0800888 transceiver_stats_infos_.clear();
ehmaldonadoa26196b2017-07-18 03:30:29 -0700889 // Trace WebRTC Stats when getStats is called on Javascript.
890 // This allows access to WebRTC stats from trace logs. To enable them,
891 // select the "webrtc_stats" category when recording traces.
ehmaldonado8ab0fd82017-09-04 14:35:04 -0700892 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
893 cached_report_->ToJson());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800894
Henrik Boström5b3541f2018-03-19 13:52:56 +0100895 // Deliver report and clear |requests_|.
896 std::vector<RequestInfo> requests;
897 requests.swap(requests_);
898 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -0700899 }
900}
901
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800902void RTCStatsCollector::DeliverCachedReport(
903 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +0100904 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -0700905 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100906 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800907 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -0800908
Henrik Boström5b3541f2018-03-19 13:52:56 +0100909 for (const RequestInfo& request : requests) {
910 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
911 request.callback()->OnStatsDelivered(cached_report);
912 } else {
913 bool filter_by_sender_selector;
914 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
915 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
916 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
917 filter_by_sender_selector = true;
918 sender_selector = request.sender_selector();
919 } else {
920 RTC_DCHECK(request.filter_mode() ==
921 RequestInfo::FilterMode::kReceiverSelector);
922 filter_by_sender_selector = false;
923 receiver_selector = request.receiver_selector();
924 }
925 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
926 filter_by_sender_selector, cached_report, sender_selector,
927 receiver_selector));
928 }
hbosc82f2e12016-09-05 01:36:50 -0700929 }
hbosd565b732016-08-30 14:04:35 -0700930}
931
hbosdf6075a2016-12-19 04:58:02 -0800932void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700933 int64_t timestamp_us,
934 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700935 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800936 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700937 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
938 if (transport_cert_stats_pair.second.local) {
939 ProduceCertificateStatsFromSSLCertificateStats(
940 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700941 }
hbos02ba2112016-10-28 05:14:53 -0700942 if (transport_cert_stats_pair.second.remote) {
943 ProduceCertificateStatsFromSSLCertificateStats(
944 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700945 }
946 }
947}
948
hbosdf6075a2016-12-19 04:58:02 -0800949void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -0800950 int64_t timestamp_us,
951 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -0800952 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800953 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -0800954 for (const auto& stats : transceiver_stats_infos) {
955 if (!stats.mid) {
956 continue;
hbos0adb8282016-11-23 02:32:06 -0800957 }
Steve Anton57858b32018-02-15 15:19:50 -0800958 const cricket::VoiceMediaInfo* voice_media_info =
959 stats.track_media_info_map->voice_media_info();
960 const cricket::VideoMediaInfo* video_media_info =
961 stats.track_media_info_map->video_media_info();
962 // Audio
963 if (voice_media_info) {
964 // Inbound
965 for (const auto& pair : voice_media_info->receive_codecs) {
966 report->AddStats(CodecStatsFromRtpCodecParameters(
967 timestamp_us, *stats.mid, true, pair.second));
968 }
969 // Outbound
970 for (const auto& pair : voice_media_info->send_codecs) {
971 report->AddStats(CodecStatsFromRtpCodecParameters(
972 timestamp_us, *stats.mid, false, pair.second));
973 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +0000974 }
Steve Anton57858b32018-02-15 15:19:50 -0800975 // Video
976 if (video_media_info) {
977 // Inbound
978 for (const auto& pair : video_media_info->receive_codecs) {
979 report->AddStats(CodecStatsFromRtpCodecParameters(
980 timestamp_us, *stats.mid, true, pair.second));
981 }
982 // Outbound
983 for (const auto& pair : video_media_info->send_codecs) {
984 report->AddStats(CodecStatsFromRtpCodecParameters(
985 timestamp_us, *stats.mid, false, pair.second));
986 }
hbos0adb8282016-11-23 02:32:06 -0800987 }
988 }
989}
990
hboscc555c52016-10-18 12:48:31 -0700991void RTCStatsCollector::ProduceDataChannelStats_s(
992 int64_t timestamp_us, RTCStatsReport* report) const {
993 RTC_DCHECK(signaling_thread_->IsCurrent());
994 for (const rtc::scoped_refptr<DataChannel>& data_channel :
995 pc_->sctp_data_channels()) {
996 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
997 new RTCDataChannelStats(
998 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
999 timestamp_us));
1000 data_channel_stats->label = data_channel->label();
1001 data_channel_stats->protocol = data_channel->protocol();
1002 data_channel_stats->datachannelid = data_channel->id();
1003 data_channel_stats->state =
1004 DataStateToRTCDataChannelState(data_channel->state());
1005 data_channel_stats->messages_sent = data_channel->messages_sent();
1006 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1007 data_channel_stats->messages_received = data_channel->messages_received();
1008 data_channel_stats->bytes_received = data_channel->bytes_received();
1009 report->AddStats(std::move(data_channel_stats));
1010 }
1011}
1012
hbosdf6075a2016-12-19 04:58:02 -08001013void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001014 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001015 const std::map<std::string, cricket::TransportStats>&
1016 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001017 const Call::Stats& call_stats,
1018 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001019 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001020 for (const auto& entry : transport_stats_by_name) {
1021 const std::string& transport_name = entry.first;
1022 const cricket::TransportStats& transport_stats = entry.second;
1023 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001024 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001025 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001026 for (const cricket::ConnectionInfo& info :
1027 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001028 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001029 new RTCIceCandidatePairStats(
1030 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1031 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001032
hbos0583b282016-11-30 01:50:14 -08001033 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001034 // TODO(hbos): There could be other candidates that are not paired with
1035 // anything. We don't have a complete list. Local candidates come from
1036 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001037 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001038 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001039 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001040 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001041 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001042 candidate_pair_stats->state =
1043 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1044 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001045 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001046 // TODO(hbos): This writable is different than the spec. It goes to
1047 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001048 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001049 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001050 candidate_pair_stats->bytes_sent =
1051 static_cast<uint64_t>(info.sent_total_bytes);
1052 candidate_pair_stats->bytes_received =
1053 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001054 candidate_pair_stats->total_round_trip_time =
1055 static_cast<double>(info.total_round_trip_time_ms) /
1056 rtc::kNumMillisecsPerSec;
1057 if (info.current_round_trip_time_ms) {
1058 candidate_pair_stats->current_round_trip_time =
1059 static_cast<double>(*info.current_round_trip_time_ms) /
1060 rtc::kNumMillisecsPerSec;
1061 }
stefanf79ade12017-06-02 06:44:03 -07001062 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001063 // The bandwidth estimations we have are for the selected candidate
1064 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001065 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1066 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1067 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001068 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001069 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001070 }
stefanf79ade12017-06-02 06:44:03 -07001071 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001072 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001073 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001074 }
1075 }
hbosd82f5122016-12-09 04:12:39 -08001076 candidate_pair_stats->requests_received =
1077 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001078 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1079 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001080 candidate_pair_stats->responses_received =
1081 static_cast<uint64_t>(info.recv_ping_responses);
1082 candidate_pair_stats->responses_sent =
1083 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001084 RTC_DCHECK_GE(info.sent_ping_requests_total,
1085 info.sent_ping_requests_before_first_response);
1086 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1087 info.sent_ping_requests_total -
1088 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001089
1090 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001091 }
1092 }
1093 }
1094}
1095
Steve Anton57858b32018-02-15 15:19:50 -08001096void RTCStatsCollector::ProduceMediaStreamStats_s(
1097 int64_t timestamp_us,
1098 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001099 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001100
1101 std::map<std::string, std::vector<std::string>> track_ids;
1102
1103 for (const auto& stats : transceiver_stats_infos_) {
1104 for (auto sender : stats.transceiver->senders()) {
1105 std::string track_id =
1106 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1107 kSender, sender->internal()->AttachmentId());
1108 for (auto& stream_id : sender->stream_ids()) {
1109 track_ids[stream_id].push_back(track_id);
1110 }
1111 }
1112 for (auto receiver : stats.transceiver->receivers()) {
1113 std::string track_id =
1114 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1115 kReceiver, receiver->internal()->AttachmentId());
1116 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001117 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001118 }
1119 }
1120 }
1121
1122 // Build stats for each stream ID known.
1123 for (auto& it : track_ids) {
1124 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1125 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1126 stream_stats->stream_identifier = it.first;
1127 stream_stats->track_ids = it.second;
1128 report->AddStats(std::move(stream_stats));
1129 }
1130}
1131
1132void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1133 int64_t timestamp_us,
1134 RTCStatsReport* report) const {
1135 RTC_DCHECK(signaling_thread_->IsCurrent());
1136 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1137 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
1138 for (auto sender : stats.transceiver->senders()) {
1139 senders.push_back(sender->internal());
1140 }
1141 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1142 senders, report);
1143
1144 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
1145 for (auto receiver : stats.transceiver->receivers()) {
1146 receivers.push_back(receiver->internal());
1147 }
1148 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1149 receivers, report);
1150 }
hbos09bc1282016-11-08 06:29:22 -08001151}
1152
hbos6ab97ce2016-10-03 14:16:56 -07001153void RTCStatsCollector::ProducePeerConnectionStats_s(
1154 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001155 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001156 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001157 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001158 stats->data_channels_opened = internal_record_.data_channels_opened;
1159 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001160 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001161}
1162
hbosdf6075a2016-12-19 04:58:02 -08001163void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001164 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001165 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001166 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001167 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001168
Steve Anton57858b32018-02-15 15:19:50 -08001169 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1170 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1171 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1172 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1173 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1174 } else {
1175 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001176 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001177 }
Steve Anton57858b32018-02-15 15:19:50 -08001178}
1179
1180void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1181 int64_t timestamp_us,
1182 const RtpTransceiverStatsInfo& stats,
1183 RTCStatsReport* report) const {
1184 if (!stats.mid || !stats.transport_name) {
1185 return;
1186 }
1187 RTC_DCHECK(stats.track_media_info_map);
1188 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1189 RTC_DCHECK(track_media_info_map.voice_media_info());
1190 std::string mid = *stats.mid;
1191 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1192 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1193 // Inbound
1194 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1195 track_media_info_map.voice_media_info()->receivers) {
1196 if (!voice_receiver_info.connected())
1197 continue;
1198 auto inbound_audio = rtc::MakeUnique<RTCInboundRTPStreamStats>(
1199 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1200 timestamp_us);
1201 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1202 inbound_audio.get());
1203 // TODO(hta): This lookup should look for the sender, not the track.
1204 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1205 track_media_info_map.GetAudioTrack(voice_receiver_info);
1206 if (audio_track) {
1207 inbound_audio->track_id =
1208 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1209 kReceiver,
1210 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001211 }
Steve Anton57858b32018-02-15 15:19:50 -08001212 inbound_audio->transport_id = transport_id;
1213 report->AddStats(std::move(inbound_audio));
1214 }
1215 // Outbound
1216 for (const cricket::VoiceSenderInfo& voice_sender_info :
1217 track_media_info_map.voice_media_info()->senders) {
1218 if (!voice_sender_info.connected())
1219 continue;
1220 auto outbound_audio = rtc::MakeUnique<RTCOutboundRTPStreamStats>(
1221 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1222 timestamp_us);
1223 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1224 outbound_audio.get());
1225 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1226 track_media_info_map.GetAudioTrack(voice_sender_info);
1227 if (audio_track) {
1228 outbound_audio->track_id =
1229 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1230 kSender,
1231 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001232 }
Steve Anton57858b32018-02-15 15:19:50 -08001233 outbound_audio->transport_id = transport_id;
1234 report->AddStats(std::move(outbound_audio));
1235 }
1236}
1237
1238void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1239 int64_t timestamp_us,
1240 const RtpTransceiverStatsInfo& stats,
1241 RTCStatsReport* report) const {
1242 if (!stats.mid || !stats.transport_name) {
1243 return;
1244 }
1245 RTC_DCHECK(stats.track_media_info_map);
1246 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1247 RTC_DCHECK(track_media_info_map.video_media_info());
1248 std::string mid = *stats.mid;
1249 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1250 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1251 // Inbound
1252 for (const cricket::VideoReceiverInfo& video_receiver_info :
1253 track_media_info_map.video_media_info()->receivers) {
1254 if (!video_receiver_info.connected())
1255 continue;
1256 auto inbound_video = rtc::MakeUnique<RTCInboundRTPStreamStats>(
1257 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1258 timestamp_us);
1259 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1260 inbound_video.get());
1261 rtc::scoped_refptr<VideoTrackInterface> video_track =
1262 track_media_info_map.GetVideoTrack(video_receiver_info);
1263 if (video_track) {
1264 inbound_video->track_id =
1265 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1266 kReceiver,
1267 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1268 }
1269 inbound_video->transport_id = transport_id;
1270 report->AddStats(std::move(inbound_video));
1271 }
1272 // Outbound
1273 for (const cricket::VideoSenderInfo& video_sender_info :
1274 track_media_info_map.video_media_info()->senders) {
1275 if (!video_sender_info.connected())
1276 continue;
1277 auto outbound_video = rtc::MakeUnique<RTCOutboundRTPStreamStats>(
1278 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1279 timestamp_us);
1280 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1281 outbound_video.get());
1282 rtc::scoped_refptr<VideoTrackInterface> video_track =
1283 track_media_info_map.GetVideoTrack(video_sender_info);
1284 if (video_track) {
1285 outbound_video->track_id =
1286 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1287 kSender,
1288 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1289 }
1290 outbound_video->transport_id = transport_id;
1291 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001292 }
1293}
1294
hbosdf6075a2016-12-19 04:58:02 -08001295void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001296 int64_t timestamp_us,
1297 const std::map<std::string, cricket::TransportStats>&
1298 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001299 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1300 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001301 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001302 for (const auto& entry : transport_stats_by_name) {
1303 const std::string& transport_name = entry.first;
1304 const cricket::TransportStats& transport_stats = entry.second;
1305
hbos2fa7c672016-10-24 04:00:05 -07001306 // Get reference to RTCP channel, if it exists.
1307 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001308 for (const cricket::TransportChannelStats& channel_stats :
1309 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001310 if (channel_stats.component ==
1311 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1312 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001313 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001314 break;
1315 }
1316 }
1317
1318 // Get reference to local and remote certificates of this transport, if they
1319 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001320 const auto& certificate_stats_it =
1321 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001322 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1323 std::string local_certificate_id;
1324 if (certificate_stats_it->second.local) {
1325 local_certificate_id = RTCCertificateIDFromFingerprint(
1326 certificate_stats_it->second.local->fingerprint);
1327 }
1328 std::string remote_certificate_id;
1329 if (certificate_stats_it->second.remote) {
1330 remote_certificate_id = RTCCertificateIDFromFingerprint(
1331 certificate_stats_it->second.remote->fingerprint);
1332 }
1333
1334 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001335 for (const cricket::TransportChannelStats& channel_stats :
1336 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001337 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001338 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1339 transport_name, channel_stats.component),
1340 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001341 transport_stats->bytes_sent = 0;
1342 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001343 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1344 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001345 for (const cricket::ConnectionInfo& info :
1346 channel_stats.connection_infos) {
1347 *transport_stats->bytes_sent += info.sent_total_bytes;
1348 *transport_stats->bytes_received += info.recv_total_bytes;
1349 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001350 transport_stats->selected_candidate_pair_id =
1351 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1352 }
1353 }
1354 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1355 !rtcp_transport_stats_id.empty()) {
1356 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1357 }
1358 if (!local_certificate_id.empty())
1359 transport_stats->local_certificate_id = local_certificate_id;
1360 if (!remote_certificate_id.empty())
1361 transport_stats->remote_certificate_id = remote_certificate_id;
1362 report->AddStats(std::move(transport_stats));
1363 }
1364 }
1365}
1366
1367std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001368RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001369 const std::map<std::string, cricket::TransportStats>&
1370 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001371 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001372 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001373 for (const auto& entry : transport_stats_by_name) {
1374 const std::string& transport_name = entry.first;
1375
hbos2fa7c672016-10-24 04:00:05 -07001376 CertificateStatsPair certificate_stats_pair;
1377 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001378 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001379 certificate_stats_pair.local =
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001380 local_certificate->ssl_cert_chain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001381 }
Steve Anton5dfde182018-02-06 10:34:40 -08001382
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001383 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1384 pc_->GetRemoteSSLCertChain(transport_name);
1385 if (remote_cert_chain) {
1386 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001387 }
Steve Anton5dfde182018-02-06 10:34:40 -08001388
hbos2fa7c672016-10-24 04:00:05 -07001389 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001390 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001391 }
1392 return transport_cert_stats;
1393}
1394
Steve Anton57858b32018-02-15 15:19:50 -08001395std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1396RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1397 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001398
Steve Anton57858b32018-02-15 15:19:50 -08001399 // These are used to invoke GetStats for all the media channels together in
1400 // one worker thread hop.
1401 std::map<cricket::VoiceMediaChannel*,
1402 std::unique_ptr<cricket::VoiceMediaInfo>>
1403 voice_stats;
1404 std::map<cricket::VideoMediaChannel*,
1405 std::unique_ptr<cricket::VideoMediaInfo>>
1406 video_stats;
1407
1408 for (auto transceiver : pc_->GetTransceiversInternal()) {
1409 cricket::MediaType media_type = transceiver->media_type();
1410
1411 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1412 // stats have been fetched on the worker thread.
1413 transceiver_stats_infos.emplace_back();
1414 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1415 stats.transceiver = transceiver->internal();
1416 stats.media_type = media_type;
1417
1418 cricket::BaseChannel* channel = transceiver->internal()->channel();
1419 if (!channel) {
1420 // The remaining fields require a BaseChannel.
1421 continue;
1422 }
1423
1424 stats.mid = channel->content_name();
1425 stats.transport_name = channel->transport_name();
1426
1427 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1428 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1429 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1430 voice_stats.end());
1431 voice_stats[voice_channel->media_channel()] =
1432 rtc::MakeUnique<cricket::VoiceMediaInfo>();
1433 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1434 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1435 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1436 video_stats.end());
1437 video_stats[video_channel->media_channel()] =
1438 rtc::MakeUnique<cricket::VideoMediaInfo>();
1439 } else {
1440 RTC_NOTREACHED();
1441 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001442 }
Steve Anton57858b32018-02-15 15:19:50 -08001443
1444 // Call GetStats for all media channels together on the worker thread in one
1445 // hop.
1446 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1447 for (const auto& entry : voice_stats) {
1448 if (!entry.first->GetStats(entry.second.get())) {
1449 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1450 }
1451 }
1452 for (const auto& entry : video_stats) {
1453 if (!entry.first->GetStats(entry.second.get())) {
1454 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1455 }
1456 }
1457 });
1458
1459 // Create the TrackMediaInfoMap for each transceiver stats object.
1460 for (auto& stats : transceiver_stats_infos) {
1461 auto transceiver = stats.transceiver;
1462 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1463 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1464 if (transceiver->channel()) {
1465 cricket::MediaType media_type = transceiver->media_type();
1466 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1467 auto* voice_channel =
1468 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1469 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1470 voice_media_info =
1471 std::move(voice_stats[voice_channel->media_channel()]);
1472 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1473 auto* video_channel =
1474 static_cast<cricket::VideoChannel*>(transceiver->channel());
1475 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1476 video_media_info =
1477 std::move(video_stats[video_channel->media_channel()]);
1478 }
1479 }
1480 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
1481 for (auto sender : transceiver->senders()) {
1482 senders.push_back(sender->internal());
1483 }
1484 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
1485 for (auto receiver : transceiver->receivers()) {
1486 receivers.push_back(receiver->internal());
1487 }
1488 stats.track_media_info_map = rtc::MakeUnique<TrackMediaInfoMap>(
1489 std::move(voice_media_info), std::move(video_media_info), senders,
1490 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001491 }
Steve Anton57858b32018-02-15 15:19:50 -08001492
1493 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001494}
1495
Steve Anton7eca0932018-03-30 15:18:41 -07001496std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1497 std::set<std::string> transport_names;
1498 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1499 if (transceiver->internal()->channel()) {
1500 transport_names.insert(
1501 transceiver->internal()->channel()->transport_name());
1502 }
1503 }
1504 if (pc_->rtp_data_channel()) {
1505 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1506 }
1507 if (pc_->sctp_transport_name()) {
1508 transport_names.insert(*pc_->sctp_transport_name());
1509 }
1510 return transport_names;
1511}
1512
hbos82ebe022016-11-14 01:41:09 -08001513void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1514 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1515 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1516}
1517
1518void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1519 RTC_DCHECK(signaling_thread_->IsCurrent());
1520 bool result = internal_record_.opened_data_channels.insert(
1521 reinterpret_cast<uintptr_t>(channel)).second;
1522 ++internal_record_.data_channels_opened;
1523 RTC_DCHECK(result);
1524}
1525
1526void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1527 RTC_DCHECK(signaling_thread_->IsCurrent());
1528 // Only channels that have been fully opened (and have increased the
1529 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001530 if (internal_record_.opened_data_channels.erase(
1531 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001532 ++internal_record_.data_channels_closed;
1533 }
1534}
1535
hboscc555c52016-10-18 12:48:31 -07001536const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1537 const std::string& type) {
1538 return CandidateTypeToRTCIceCandidateType(type);
1539}
1540
1541const char* DataStateToRTCDataChannelStateForTesting(
1542 DataChannelInterface::DataState state) {
1543 return DataStateToRTCDataChannelState(state);
1544}
1545
hbosd565b732016-08-30 14:04:35 -07001546} // namespace webrtc