blob: 7d75ce8e53ef6a3ab1426cf25558aa431f6ee172 [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"
Henrik Boström5b3541f2018-03-19 13:52:56 +010026#include "pc/rtcstatstraversal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/checks.h"
Steve Anton57858b32018-02-15 15:19:50 -080028#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/stringutils.h"
30#include "rtc_base/timeutils.h"
31#include "rtc_base/trace_event.h"
hbosd565b732016-08-30 14:04:35 -070032
33namespace webrtc {
34
hboscc555c52016-10-18 12:48:31 -070035namespace {
36
hbos2fa7c672016-10-24 04:00:05 -070037std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
38 return "RTCCertificate_" + fingerprint;
39}
40
Steve Anton57858b32018-02-15 15:19:50 -080041std::string RTCCodecStatsIDFromMidDirectionAndPayload(const std::string& mid,
42 bool inbound,
43 uint32_t payload_type) {
44 return "RTCCodec_" + mid + "_" + (inbound ? "Inbound" : "Outbound") + "_" +
45 rtc::ToString<>(payload_type);
hbos0adb8282016-11-23 02:32:06 -080046}
47
hbos2fa7c672016-10-24 04:00:05 -070048std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
49 const cricket::ConnectionInfo& info) {
50 return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" +
51 info.remote_candidate.id();
52}
53
Harald Alvestranda3dab842018-01-14 09:18:58 +010054const char kSender[] = "sender";
55const char kReceiver[] = "receiver";
56
57std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
58 const char* direction,
Harald Alvestrandc72af932018-01-11 17:18:19 +010059 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -080060 std::ostringstream oss;
Harald Alvestranda3dab842018-01-14 09:18:58 +010061 oss << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
hbos9e302742017-01-20 02:47:10 -080062 return oss.str();
hbos09bc1282016-11-08 06:29:22 -080063}
64
hbos2fa7c672016-10-24 04:00:05 -070065std::string RTCTransportStatsIDFromTransportChannel(
66 const std::string& transport_name, int channel_component) {
67 return "RTCTransport_" + transport_name + "_" +
68 rtc::ToString<>(channel_component);
69}
70
hboseeafe942016-11-01 03:00:17 -070071std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
72 return audio ? "RTCInboundRTPAudioStream_" + rtc::ToString<>(ssrc)
73 : "RTCInboundRTPVideoStream_" + rtc::ToString<>(ssrc);
74}
75
hbos6ded1902016-11-01 01:50:46 -070076std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
77 return audio ? "RTCOutboundRTPAudioStream_" + rtc::ToString<>(ssrc)
78 : "RTCOutboundRTPVideoStream_" + rtc::ToString<>(ssrc);
79}
80
hbosab9f6e42016-10-07 02:18:47 -070081const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
82 if (type == cricket::LOCAL_PORT_TYPE)
83 return RTCIceCandidateType::kHost;
84 if (type == cricket::STUN_PORT_TYPE)
85 return RTCIceCandidateType::kSrflx;
86 if (type == cricket::PRFLX_PORT_TYPE)
87 return RTCIceCandidateType::kPrflx;
88 if (type == cricket::RELAY_PORT_TYPE)
89 return RTCIceCandidateType::kRelay;
90 RTC_NOTREACHED();
91 return nullptr;
92}
93
hboscc555c52016-10-18 12:48:31 -070094const char* DataStateToRTCDataChannelState(
95 DataChannelInterface::DataState state) {
96 switch (state) {
97 case DataChannelInterface::kConnecting:
98 return RTCDataChannelState::kConnecting;
99 case DataChannelInterface::kOpen:
100 return RTCDataChannelState::kOpen;
101 case DataChannelInterface::kClosing:
102 return RTCDataChannelState::kClosing;
103 case DataChannelInterface::kClosed:
104 return RTCDataChannelState::kClosed;
105 default:
106 RTC_NOTREACHED();
107 return nullptr;
108 }
109}
110
hbos06495bc2017-01-02 08:08:18 -0800111const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
112 cricket::IceCandidatePairState state) {
113 switch (state) {
114 case cricket::IceCandidatePairState::WAITING:
115 return RTCStatsIceCandidatePairState::kWaiting;
116 case cricket::IceCandidatePairState::IN_PROGRESS:
117 return RTCStatsIceCandidatePairState::kInProgress;
118 case cricket::IceCandidatePairState::SUCCEEDED:
119 return RTCStatsIceCandidatePairState::kSucceeded;
120 case cricket::IceCandidatePairState::FAILED:
121 return RTCStatsIceCandidatePairState::kFailed;
122 default:
123 RTC_NOTREACHED();
124 return nullptr;
125 }
126}
127
hbos7064d592017-01-16 07:38:02 -0800128const char* DtlsTransportStateToRTCDtlsTransportState(
129 cricket::DtlsTransportState state) {
130 switch (state) {
131 case cricket::DTLS_TRANSPORT_NEW:
132 return RTCDtlsTransportState::kNew;
133 case cricket::DTLS_TRANSPORT_CONNECTING:
134 return RTCDtlsTransportState::kConnecting;
135 case cricket::DTLS_TRANSPORT_CONNECTED:
136 return RTCDtlsTransportState::kConnected;
137 case cricket::DTLS_TRANSPORT_CLOSED:
138 return RTCDtlsTransportState::kClosed;
139 case cricket::DTLS_TRANSPORT_FAILED:
140 return RTCDtlsTransportState::kFailed;
141 default:
142 RTC_NOTREACHED();
143 return nullptr;
144 }
145}
146
Gary Liu37e489c2017-11-21 10:49:36 -0800147const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
148 switch (type) {
149 case rtc::ADAPTER_TYPE_CELLULAR:
150 return RTCNetworkType::kCellular;
151 case rtc::ADAPTER_TYPE_ETHERNET:
152 return RTCNetworkType::kEthernet;
153 case rtc::ADAPTER_TYPE_WIFI:
154 return RTCNetworkType::kWifi;
155 case rtc::ADAPTER_TYPE_VPN:
156 return RTCNetworkType::kVpn;
157 case rtc::ADAPTER_TYPE_UNKNOWN:
158 case rtc::ADAPTER_TYPE_LOOPBACK:
159 return RTCNetworkType::kUnknown;
160 }
161 RTC_NOTREACHED();
162 return nullptr;
163}
164
hbos9e302742017-01-20 02:47:10 -0800165double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
166 RTC_DCHECK_GE(audio_level, 0);
167 RTC_DCHECK_LE(audio_level, 32767);
168 return audio_level / 32767.0;
169}
170
hbos0adb8282016-11-23 02:32:06 -0800171std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 15:19:50 -0800172 uint64_t timestamp_us,
173 const std::string& mid,
174 bool inbound,
hbos0adb8282016-11-23 02:32:06 -0800175 const RtpCodecParameters& codec_params) {
176 RTC_DCHECK_GE(codec_params.payload_type, 0);
177 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 12:09:01 -0800178 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 02:32:06 -0800179 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
180 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 15:19:50 -0800181 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 02:32:06 -0800182 timestamp_us));
183 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 06:56:04 -0800184 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 12:09:01 -0800185 if (codec_params.clock_rate) {
186 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
187 }
hbos0adb8282016-11-23 02:32:06 -0800188 return codec_stats;
189}
190
hbos09bc1282016-11-08 06:29:22 -0800191void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
192 const MediaStreamTrackInterface& track,
193 RTCMediaStreamTrackStats* track_stats) {
194 track_stats->track_identifier = track.id();
195 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
196}
197
hbos820f5782016-11-22 03:16:50 -0800198// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700199void SetInboundRTPStreamStatsFromMediaReceiverInfo(
200 const cricket::MediaReceiverInfo& media_receiver_info,
201 RTCInboundRTPStreamStats* inbound_stats) {
202 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800203 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100204 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 03:00:17 -0700205 inbound_stats->is_remote = false;
hboseeafe942016-11-01 03:00:17 -0700206 inbound_stats->packets_received =
207 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
208 inbound_stats->bytes_received =
209 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800210 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 12:26:04 +0100211 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700212 inbound_stats->fraction_lost =
213 static_cast<double>(media_receiver_info.fraction_lost);
214}
215
216void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800217 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700218 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800219 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700220 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800221 voice_receiver_info, inbound_audio);
222 inbound_audio->media_type = "audio";
hbos585a9b12017-02-07 04:59:16 -0800223 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800224 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
225 mid, true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800226 }
hbos820f5782016-11-22 03:16:50 -0800227 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700228 static_cast<double>(voice_receiver_info.jitter_ms) /
229 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800230 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
231 // purposefully left undefined for audio.
hboseeafe942016-11-01 03:00:17 -0700232}
233
234void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800235 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700236 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800237 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700238 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800239 video_receiver_info, inbound_video);
240 inbound_video->media_type = "video";
hbos585a9b12017-02-07 04:59:16 -0800241 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800242 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
243 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800244 }
hbos820f5782016-11-22 03:16:50 -0800245 inbound_video->fir_count =
246 static_cast<uint32_t>(video_receiver_info.firs_sent);
247 inbound_video->pli_count =
248 static_cast<uint32_t>(video_receiver_info.plis_sent);
249 inbound_video->nack_count =
250 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800251 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800252 if (video_receiver_info.qp_sum)
253 inbound_video->qp_sum = *video_receiver_info.qp_sum;
hboseeafe942016-11-01 03:00:17 -0700254}
255
hbos820f5782016-11-22 03:16:50 -0800256// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700257void SetOutboundRTPStreamStatsFromMediaSenderInfo(
258 const cricket::MediaSenderInfo& media_sender_info,
259 RTCOutboundRTPStreamStats* outbound_stats) {
260 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800261 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100262 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700263 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700264 outbound_stats->packets_sent =
265 static_cast<uint32_t>(media_sender_info.packets_sent);
266 outbound_stats->bytes_sent =
267 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbos6ded1902016-11-01 01:50:46 -0700268}
269
270void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800271 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700272 const cricket::VoiceSenderInfo& voice_sender_info,
273 RTCOutboundRTPStreamStats* outbound_audio) {
274 SetOutboundRTPStreamStatsFromMediaSenderInfo(
275 voice_sender_info, outbound_audio);
276 outbound_audio->media_type = "audio";
hbos585a9b12017-02-07 04:59:16 -0800277 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800278 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
279 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800280 }
hbos6ded1902016-11-01 01:50:46 -0700281 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
282 // purposefully left undefined for audio.
283}
284
285void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800286 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700287 const cricket::VideoSenderInfo& video_sender_info,
288 RTCOutboundRTPStreamStats* outbound_video) {
289 SetOutboundRTPStreamStatsFromMediaSenderInfo(
290 video_sender_info, outbound_video);
291 outbound_video->media_type = "video";
hbos585a9b12017-02-07 04:59:16 -0800292 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800293 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
294 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800295 }
hbos6ded1902016-11-01 01:50:46 -0700296 outbound_video->fir_count =
297 static_cast<uint32_t>(video_sender_info.firs_rcvd);
298 outbound_video->pli_count =
299 static_cast<uint32_t>(video_sender_info.plis_rcvd);
300 outbound_video->nack_count =
301 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800302 if (video_sender_info.qp_sum)
303 outbound_video->qp_sum = *video_sender_info.qp_sum;
304 outbound_video->frames_encoded = video_sender_info.frames_encoded;
hbos6ded1902016-11-01 01:50:46 -0700305}
306
hbos02ba2112016-10-28 05:14:53 -0700307void ProduceCertificateStatsFromSSLCertificateStats(
308 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
309 RTCStatsReport* report) {
310 RTCCertificateStats* prev_certificate_stats = nullptr;
311 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
312 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800313 std::string certificate_stats_id =
314 RTCCertificateIDFromFingerprint(s->fingerprint);
315 // It is possible for the same certificate to show up multiple times, e.g.
316 // if local and remote side use the same certificate in a loopback call.
317 // If the report already contains stats for this certificate, skip it.
318 if (report->Get(certificate_stats_id)) {
319 RTC_DCHECK_EQ(s, &certificate_stats);
320 break;
321 }
hbos02ba2112016-10-28 05:14:53 -0700322 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800323 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700324 certificate_stats->fingerprint = s->fingerprint;
325 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
326 certificate_stats->base64_certificate = s->base64_certificate;
327 if (prev_certificate_stats)
328 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
329 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
330 prev_certificate_stats = certificate_stats;
331 }
332}
333
334const std::string& ProduceIceCandidateStats(
335 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800336 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700337 const std::string& id = "RTCIceCandidate_" + candidate.id();
338 const RTCStats* stats = report->Get(id);
339 if (!stats) {
340 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
341 if (is_local)
342 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
343 else
344 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800345 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800346 if (is_local) {
347 candidate_stats->network_type =
348 NetworkAdapterTypeToStatsType(candidate.network_type());
349 } else {
350 // We don't expect to know the adapter type of remote candidates.
351 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
352 }
hbos02ba2112016-10-28 05:14:53 -0700353 candidate_stats->ip = candidate.address().ipaddr().ToString();
354 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
355 candidate_stats->protocol = candidate.protocol();
356 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
357 candidate.type());
358 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
359
360 stats = candidate_stats.get();
361 report->AddStats(std::move(candidate_stats));
362 }
363 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
364 : RTCRemoteIceCandidateStats::kType);
365 return stats->id();
366}
367
hbos9e302742017-01-20 02:47:10 -0800368std::unique_ptr<RTCMediaStreamTrackStats>
369ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
370 int64_t timestamp_us,
371 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100372 const cricket::VoiceSenderInfo& voice_sender_info,
373 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800374 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
375 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100376 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
377 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100378 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800379 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
380 audio_track, audio_track_stats.get());
381 audio_track_stats->remote_source = false;
382 audio_track_stats->detached = false;
383 if (voice_sender_info.audio_level >= 0) {
384 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
385 voice_sender_info.audio_level);
386 }
zsteine76bd3a2017-07-14 12:17:49 -0700387 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
388 audio_track_stats->total_samples_duration =
389 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100390 if (voice_sender_info.apm_statistics.echo_return_loss) {
391 audio_track_stats->echo_return_loss =
392 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800393 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100394 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
395 audio_track_stats->echo_return_loss_enhancement =
396 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800397 }
398 return audio_track_stats;
399}
400
401std::unique_ptr<RTCMediaStreamTrackStats>
402ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
403 int64_t timestamp_us,
404 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100405 const cricket::VoiceReceiverInfo& voice_receiver_info,
406 int attachment_id) {
407 // Since receiver tracks can't be reattached, we use the SSRC as
408 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800409 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
410 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100411 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
412 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100413 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800414 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
415 audio_track, audio_track_stats.get());
416 audio_track_stats->remote_source = true;
417 audio_track_stats->detached = false;
418 if (voice_receiver_info.audio_level >= 0) {
419 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
420 voice_receiver_info.audio_level);
421 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200422 audio_track_stats->jitter_buffer_delay =
423 voice_receiver_info.jitter_buffer_delay_seconds;
zsteine76bd3a2017-07-14 12:17:49 -0700424 audio_track_stats->total_audio_energy =
425 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700426 audio_track_stats->total_samples_received =
427 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700428 audio_track_stats->total_samples_duration =
429 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700430 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200431 audio_track_stats->concealment_events =
432 voice_receiver_info.concealment_events;
hbos9e302742017-01-20 02:47:10 -0800433 return audio_track_stats;
434}
435
436std::unique_ptr<RTCMediaStreamTrackStats>
437ProduceMediaStreamTrackStatsFromVideoSenderInfo(
438 int64_t timestamp_us,
439 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100440 const cricket::VideoSenderInfo& video_sender_info,
441 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800442 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
443 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100444 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
445
446 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100447 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800448 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
449 video_track, video_track_stats.get());
450 video_track_stats->remote_source = false;
451 video_track_stats->detached = false;
452 video_track_stats->frame_width = static_cast<uint32_t>(
453 video_sender_info.send_frame_width);
454 video_track_stats->frame_height = static_cast<uint32_t>(
455 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800456 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100457 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800458 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100459 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800460 return video_track_stats;
461}
462
463std::unique_ptr<RTCMediaStreamTrackStats>
464ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
465 int64_t timestamp_us,
466 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100467 const cricket::VideoReceiverInfo& video_receiver_info,
468 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800469 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
470 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100471 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
472
473 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100474 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800475 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
476 video_track, video_track_stats.get());
477 video_track_stats->remote_source = true;
478 video_track_stats->detached = false;
479 if (video_receiver_info.frame_width > 0 &&
480 video_receiver_info.frame_height > 0) {
481 video_track_stats->frame_width = static_cast<uint32_t>(
482 video_receiver_info.frame_width);
483 video_track_stats->frame_height = static_cast<uint32_t>(
484 video_receiver_info.frame_height);
485 }
hbos42f6d2f2017-01-20 03:56:50 -0800486 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800487 // TODO(hbos): When we support receiving simulcast, this should be the total
488 // number of frames correctly decoded, independent of which SSRC it was
489 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100490 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800491 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800492 RTC_DCHECK_GE(video_receiver_info.frames_received,
493 video_receiver_info.frames_rendered);
494 video_track_stats->frames_dropped = video_receiver_info.frames_received -
495 video_receiver_info.frames_rendered;
hbos9e302742017-01-20 02:47:10 -0800496 return video_track_stats;
497}
498
Harald Alvestrand89061872018-01-02 14:08:34 +0100499void ProduceSenderMediaTrackStats(
500 int64_t timestamp_us,
501 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800502 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100503 RTCStatsReport* report) {
504 // This function iterates over the senders to generate outgoing track stats.
505
506 // TODO(hbos): Return stats of detached tracks. We have to perform stats
507 // gathering at the time of detachment to get accurate stats and timestamps.
508 // https://crbug.com/659137
Steve Anton57858b32018-02-15 15:19:50 -0800509 for (auto sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100510 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
511 AudioTrackInterface* track =
512 static_cast<AudioTrackInterface*>(sender->track().get());
513 if (!track)
514 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100515 cricket::VoiceSenderInfo null_sender_info;
516 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
517 // TODO(hta): Checking on ssrc is not proper. There should be a way
518 // to see from a sender whether it's connected or not.
519 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800520 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100521 // When pc.close is called, sender info is discarded, so
522 // we generate zeroes instead. Bug: It should be retained.
523 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800524 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100525 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100526 if (sender_info) {
527 voice_sender_info = sender_info;
528 } else {
529 RTC_LOG(LS_INFO)
530 << "RTCStatsCollector: No voice sender info for sender with ssrc "
531 << sender->ssrc();
532 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100533 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100534 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100535 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
536 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100537 report->AddStats(std::move(audio_track_stats));
538 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
539 VideoTrackInterface* track =
540 static_cast<VideoTrackInterface*>(sender->track().get());
541 if (!track)
542 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100543 cricket::VideoSenderInfo null_sender_info;
544 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
545 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100546 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
547 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800548 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100549 // When pc.close is called, sender info is discarded, so
550 // we generate zeroes instead. Bug: It should be retained.
551 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800552 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100553 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100554 if (sender_info) {
555 video_sender_info = sender_info;
556 } else {
557 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
558 << sender->ssrc();
559 }
560 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100561 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100562 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
563 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100564 report->AddStats(std::move(video_track_stats));
565 } else {
566 RTC_NOTREACHED();
567 }
568 }
569}
570
571void ProduceReceiverMediaTrackStats(
572 int64_t timestamp_us,
573 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800574 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100575 RTCStatsReport* report) {
576 // This function iterates over the receivers to find the remote tracks.
Steve Anton57858b32018-02-15 15:19:50 -0800577 for (auto receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100578 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
579 AudioTrackInterface* track =
580 static_cast<AudioTrackInterface*>(receiver->track().get());
581 const cricket::VoiceReceiverInfo* voice_receiver_info =
582 track_media_info_map.GetVoiceReceiverInfo(*track);
583 if (!voice_receiver_info) {
584 continue;
585 }
586 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
587 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100588 timestamp_us, *track, *voice_receiver_info,
589 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100590 report->AddStats(std::move(audio_track_stats));
591 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
592 VideoTrackInterface* track =
593 static_cast<VideoTrackInterface*>(receiver->track().get());
594 const cricket::VideoReceiverInfo* video_receiver_info =
595 track_media_info_map.GetVideoReceiverInfo(*track);
596 if (!video_receiver_info) {
597 continue;
598 }
599 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
600 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100601 timestamp_us, *track, *video_receiver_info,
602 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100603 report->AddStats(std::move(video_track_stats));
604 } else {
605 RTC_NOTREACHED();
606 }
607 }
608}
609
Henrik Boström5b3541f2018-03-19 13:52:56 +0100610rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
611 bool filter_by_sender_selector,
612 rtc::scoped_refptr<const RTCStatsReport> report,
613 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
614 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
615 std::vector<std::string> rtpstream_ids;
616 if (filter_by_sender_selector) {
617 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
618 if (sender_selector) {
619 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
620 // reference the sender stats.
621 // Because we do not implement sender stats, we look at outbound-rtp(s)
622 // that reference the track attachment stats for the sender instead.
623 std::string track_id =
624 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
625 kSender, sender_selector->AttachmentId());
626 for (const auto& stats : *report) {
627 if (stats.type() != RTCOutboundRTPStreamStats::kType)
628 continue;
629 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
630 if (outbound_rtp.track_id.is_defined() &&
631 *outbound_rtp.track_id == track_id) {
632 rtpstream_ids.push_back(outbound_rtp.id());
633 }
634 }
635 }
636 } else {
637 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
638 if (receiver_selector) {
639 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
640 // reference the receiver stats.
641 // Because we do not implement receiver stats, we look at inbound-rtp(s)
642 // that reference the track attachment stats for the receiver instead.
643 std::string track_id =
644 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
645 kReceiver, receiver_selector->AttachmentId());
646 for (const auto& stats : *report) {
647 if (stats.type() != RTCInboundRTPStreamStats::kType)
648 continue;
649 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
650 if (inbound_rtp.track_id.is_defined() &&
651 *inbound_rtp.track_id == track_id) {
652 rtpstream_ids.push_back(inbound_rtp.id());
653 }
654 }
655 }
656 }
657 if (rtpstream_ids.empty())
658 return RTCStatsReport::Create(report->timestamp_us());
659 return TakeReferencedStats(report->Copy(), rtpstream_ids);
660}
661
hboscc555c52016-10-18 12:48:31 -0700662} // namespace
663
Henrik Boström5b3541f2018-03-19 13:52:56 +0100664RTCStatsCollector::RequestInfo::RequestInfo(
665 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
666 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
667
668RTCStatsCollector::RequestInfo::RequestInfo(
669 rtc::scoped_refptr<RtpSenderInternal> selector,
670 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
671 : RequestInfo(FilterMode::kSenderSelector,
672 std::move(callback),
673 std::move(selector),
674 nullptr) {}
675
676RTCStatsCollector::RequestInfo::RequestInfo(
677 rtc::scoped_refptr<RtpReceiverInternal> selector,
678 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
679 : RequestInfo(FilterMode::kReceiverSelector,
680 std::move(callback),
681 nullptr,
682 std::move(selector)) {}
683
684RTCStatsCollector::RequestInfo::RequestInfo(
685 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
686 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
687 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
688 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
689 : filter_mode_(filter_mode),
690 callback_(std::move(callback)),
691 sender_selector_(std::move(sender_selector)),
692 receiver_selector_(std::move(receiver_selector)) {
693 RTC_DCHECK(callback_);
694 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
695}
696
hbosc82f2e12016-09-05 01:36:50 -0700697rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800698 PeerConnectionInternal* pc,
699 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700700 return rtc::scoped_refptr<RTCStatsCollector>(
701 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
702}
703
Steve Anton2d8609c2018-01-23 16:38:46 -0800704RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700705 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700706 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700707 signaling_thread_(pc->signaling_thread()),
708 worker_thread_(pc->worker_thread()),
709 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700710 num_pending_partial_reports_(0),
711 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700712 cache_timestamp_us_(0),
713 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700714 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700715 RTC_DCHECK(signaling_thread_);
716 RTC_DCHECK(worker_thread_);
717 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700718 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800719 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800720 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700721}
722
hbosb78306a2016-12-19 05:06:57 -0800723RTCStatsCollector::~RTCStatsCollector() {
724 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
725}
726
hbosc82f2e12016-09-05 01:36:50 -0700727void RTCStatsCollector::GetStatsReport(
728 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100729 GetStatsReportInternal(RequestInfo(std::move(callback)));
730}
731
732void RTCStatsCollector::GetStatsReport(
733 rtc::scoped_refptr<RtpSenderInternal> selector,
734 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
735 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
736}
737
738void RTCStatsCollector::GetStatsReport(
739 rtc::scoped_refptr<RtpReceiverInternal> selector,
740 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
741 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
742}
743
744void RTCStatsCollector::GetStatsReportInternal(
745 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700746 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100747 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700748
hbos0e6758d2016-08-31 07:57:36 -0700749 // "Now" using a monotonically increasing timer.
750 int64_t cache_now_us = rtc::TimeMicros();
751 if (cached_report_ &&
752 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800753 // We have a fresh cached report to deliver. Deliver asynchronously, since
754 // the caller may not be expecting a synchronous callback, and it avoids
755 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100756 std::vector<RequestInfo> requests;
757 requests.swap(requests_);
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800758 invoker_.AsyncInvoke<void>(
759 RTC_FROM_HERE, signaling_thread_,
760 rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this, cached_report_,
Henrik Boström5b3541f2018-03-19 13:52:56 +0100761 std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700762 } else if (!num_pending_partial_reports_) {
763 // Only start gathering stats if we're not already gathering stats. In the
764 // case of already gathering stats, |callback_| will be invoked when there
765 // are no more pending partial reports.
766
767 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
768 // UTC), in microseconds. The system clock could be modified and is not
769 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700770 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700771
hbosf415f8a2017-01-02 04:28:51 -0800772 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700773 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800774
Steve Anton57858b32018-02-15 15:19:50 -0800775 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800776 // |ProducePartialResultsOnNetworkThread| and
777 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800778 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -0700779 // Prepare |transport_names_| for use in
780 // |ProducePartialResultsOnNetworkThread|.
781 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800782
stefanf79ade12017-06-02 06:44:03 -0700783 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
784 // thread.
785 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
786 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700787 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700788
789 invoker_.AsyncInvoke<void>(
790 RTC_FROM_HERE, network_thread_,
hbosc82f2e12016-09-05 01:36:50 -0700791 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
stefanf79ade12017-06-02 06:44:03 -0700792 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800793 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700794 }
hbosd565b732016-08-30 14:04:35 -0700795}
796
797void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700798 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700799 cached_report_ = nullptr;
800}
801
hbosb78306a2016-12-19 05:06:57 -0800802void RTCStatsCollector::WaitForPendingRequest() {
803 RTC_DCHECK(signaling_thread_->IsCurrent());
804 if (num_pending_partial_reports_) {
805 rtc::Thread::Current()->ProcessMessages(0);
806 while (num_pending_partial_reports_) {
807 rtc::Thread::Current()->SleepMs(1);
808 rtc::Thread::Current()->ProcessMessages(0);
809 }
810 }
811}
812
hbosc82f2e12016-09-05 01:36:50 -0700813void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
814 int64_t timestamp_us) {
815 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700816 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
817 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700818
hboscc555c52016-10-18 12:48:31 -0700819 ProduceDataChannelStats_s(timestamp_us, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800820 ProduceMediaStreamStats_s(timestamp_us, report.get());
821 ProduceMediaStreamTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700822 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700823
824 AddPartialResults(report);
825}
826
hbosc82f2e12016-09-05 01:36:50 -0700827void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
828 int64_t timestamp_us) {
829 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700830 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
831 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700832
Steve Anton5dfde182018-02-06 10:34:40 -0800833 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -0700834 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -0800835
836 std::map<std::string, CertificateStatsPair> transport_cert_stats =
837 PrepareTransportCertificateStats_n(transport_stats_by_name);
838
839 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800840 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, report.get());
Steve Anton5dfde182018-02-06 10:34:40 -0800841 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
Steve Anton5dfde182018-02-06 10:34:40 -0800842 call_stats_, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800843 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_, report.get());
Steve Anton5dfde182018-02-06 10:34:40 -0800844 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
845 transport_cert_stats, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700846
847 AddPartialResults(report);
848}
849
850void RTCStatsCollector::AddPartialResults(
851 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
852 if (!signaling_thread_->IsCurrent()) {
853 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
854 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
855 rtc::scoped_refptr<RTCStatsCollector>(this),
856 partial_report));
857 return;
858 }
859 AddPartialResults_s(partial_report);
860}
861
862void RTCStatsCollector::AddPartialResults_s(
863 rtc::scoped_refptr<RTCStatsReport> partial_report) {
864 RTC_DCHECK(signaling_thread_->IsCurrent());
865 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
866 if (!partial_report_)
867 partial_report_ = partial_report;
868 else
869 partial_report_->TakeMembersFrom(partial_report);
870 --num_pending_partial_reports_;
871 if (!num_pending_partial_reports_) {
872 cache_timestamp_us_ = partial_report_timestamp_us_;
873 cached_report_ = partial_report_;
874 partial_report_ = nullptr;
Steve Anton57858b32018-02-15 15:19:50 -0800875 transceiver_stats_infos_.clear();
ehmaldonadoa26196b2017-07-18 03:30:29 -0700876 // Trace WebRTC Stats when getStats is called on Javascript.
877 // This allows access to WebRTC stats from trace logs. To enable them,
878 // select the "webrtc_stats" category when recording traces.
ehmaldonado8ab0fd82017-09-04 14:35:04 -0700879 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
880 cached_report_->ToJson());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800881
Henrik Boström5b3541f2018-03-19 13:52:56 +0100882 // Deliver report and clear |requests_|.
883 std::vector<RequestInfo> requests;
884 requests.swap(requests_);
885 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -0700886 }
887}
888
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800889void RTCStatsCollector::DeliverCachedReport(
890 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +0100891 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -0700892 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100893 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800894 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -0800895
Henrik Boström5b3541f2018-03-19 13:52:56 +0100896 for (const RequestInfo& request : requests) {
897 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
898 request.callback()->OnStatsDelivered(cached_report);
899 } else {
900 bool filter_by_sender_selector;
901 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
902 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
903 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
904 filter_by_sender_selector = true;
905 sender_selector = request.sender_selector();
906 } else {
907 RTC_DCHECK(request.filter_mode() ==
908 RequestInfo::FilterMode::kReceiverSelector);
909 filter_by_sender_selector = false;
910 receiver_selector = request.receiver_selector();
911 }
912 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
913 filter_by_sender_selector, cached_report, sender_selector,
914 receiver_selector));
915 }
hbosc82f2e12016-09-05 01:36:50 -0700916 }
hbosd565b732016-08-30 14:04:35 -0700917}
918
hbosdf6075a2016-12-19 04:58:02 -0800919void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700920 int64_t timestamp_us,
921 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700922 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800923 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700924 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
925 if (transport_cert_stats_pair.second.local) {
926 ProduceCertificateStatsFromSSLCertificateStats(
927 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700928 }
hbos02ba2112016-10-28 05:14:53 -0700929 if (transport_cert_stats_pair.second.remote) {
930 ProduceCertificateStatsFromSSLCertificateStats(
931 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700932 }
933 }
934}
935
hbosdf6075a2016-12-19 04:58:02 -0800936void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -0800937 int64_t timestamp_us,
938 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -0800939 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800940 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -0800941 for (const auto& stats : transceiver_stats_infos) {
942 if (!stats.mid) {
943 continue;
hbos0adb8282016-11-23 02:32:06 -0800944 }
Steve Anton57858b32018-02-15 15:19:50 -0800945 const cricket::VoiceMediaInfo* voice_media_info =
946 stats.track_media_info_map->voice_media_info();
947 const cricket::VideoMediaInfo* video_media_info =
948 stats.track_media_info_map->video_media_info();
949 // Audio
950 if (voice_media_info) {
951 // Inbound
952 for (const auto& pair : voice_media_info->receive_codecs) {
953 report->AddStats(CodecStatsFromRtpCodecParameters(
954 timestamp_us, *stats.mid, true, pair.second));
955 }
956 // Outbound
957 for (const auto& pair : voice_media_info->send_codecs) {
958 report->AddStats(CodecStatsFromRtpCodecParameters(
959 timestamp_us, *stats.mid, false, pair.second));
960 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +0000961 }
Steve Anton57858b32018-02-15 15:19:50 -0800962 // Video
963 if (video_media_info) {
964 // Inbound
965 for (const auto& pair : video_media_info->receive_codecs) {
966 report->AddStats(CodecStatsFromRtpCodecParameters(
967 timestamp_us, *stats.mid, true, pair.second));
968 }
969 // Outbound
970 for (const auto& pair : video_media_info->send_codecs) {
971 report->AddStats(CodecStatsFromRtpCodecParameters(
972 timestamp_us, *stats.mid, false, pair.second));
973 }
hbos0adb8282016-11-23 02:32:06 -0800974 }
975 }
976}
977
hboscc555c52016-10-18 12:48:31 -0700978void RTCStatsCollector::ProduceDataChannelStats_s(
979 int64_t timestamp_us, RTCStatsReport* report) const {
980 RTC_DCHECK(signaling_thread_->IsCurrent());
981 for (const rtc::scoped_refptr<DataChannel>& data_channel :
982 pc_->sctp_data_channels()) {
983 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
984 new RTCDataChannelStats(
985 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
986 timestamp_us));
987 data_channel_stats->label = data_channel->label();
988 data_channel_stats->protocol = data_channel->protocol();
989 data_channel_stats->datachannelid = data_channel->id();
990 data_channel_stats->state =
991 DataStateToRTCDataChannelState(data_channel->state());
992 data_channel_stats->messages_sent = data_channel->messages_sent();
993 data_channel_stats->bytes_sent = data_channel->bytes_sent();
994 data_channel_stats->messages_received = data_channel->messages_received();
995 data_channel_stats->bytes_received = data_channel->bytes_received();
996 report->AddStats(std::move(data_channel_stats));
997 }
998}
999
hbosdf6075a2016-12-19 04:58:02 -08001000void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001001 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001002 const std::map<std::string, cricket::TransportStats>&
1003 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001004 const Call::Stats& call_stats,
1005 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001006 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001007 for (const auto& entry : transport_stats_by_name) {
1008 const std::string& transport_name = entry.first;
1009 const cricket::TransportStats& transport_stats = entry.second;
1010 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001011 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001012 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001013 for (const cricket::ConnectionInfo& info :
1014 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001015 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001016 new RTCIceCandidatePairStats(
1017 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1018 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001019
hbos0583b282016-11-30 01:50:14 -08001020 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001021 // TODO(hbos): There could be other candidates that are not paired with
1022 // anything. We don't have a complete list. Local candidates come from
1023 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001024 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001025 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001026 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001027 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001028 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001029 candidate_pair_stats->state =
1030 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1031 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001032 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001033 // TODO(hbos): This writable is different than the spec. It goes to
1034 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001035 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001036 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001037 candidate_pair_stats->bytes_sent =
1038 static_cast<uint64_t>(info.sent_total_bytes);
1039 candidate_pair_stats->bytes_received =
1040 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001041 candidate_pair_stats->total_round_trip_time =
1042 static_cast<double>(info.total_round_trip_time_ms) /
1043 rtc::kNumMillisecsPerSec;
1044 if (info.current_round_trip_time_ms) {
1045 candidate_pair_stats->current_round_trip_time =
1046 static_cast<double>(*info.current_round_trip_time_ms) /
1047 rtc::kNumMillisecsPerSec;
1048 }
stefanf79ade12017-06-02 06:44:03 -07001049 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001050 // The bandwidth estimations we have are for the selected candidate
1051 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001052 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1053 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1054 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001055 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001056 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001057 }
stefanf79ade12017-06-02 06:44:03 -07001058 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001059 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001060 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001061 }
1062 }
hbosd82f5122016-12-09 04:12:39 -08001063 candidate_pair_stats->requests_received =
1064 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001065 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1066 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001067 candidate_pair_stats->responses_received =
1068 static_cast<uint64_t>(info.recv_ping_responses);
1069 candidate_pair_stats->responses_sent =
1070 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001071 RTC_DCHECK_GE(info.sent_ping_requests_total,
1072 info.sent_ping_requests_before_first_response);
1073 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1074 info.sent_ping_requests_total -
1075 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001076
1077 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001078 }
1079 }
1080 }
1081}
1082
Steve Anton57858b32018-02-15 15:19:50 -08001083void RTCStatsCollector::ProduceMediaStreamStats_s(
1084 int64_t timestamp_us,
1085 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001086 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001087
1088 std::map<std::string, std::vector<std::string>> track_ids;
1089
1090 for (const auto& stats : transceiver_stats_infos_) {
1091 for (auto sender : stats.transceiver->senders()) {
1092 std::string track_id =
1093 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1094 kSender, sender->internal()->AttachmentId());
1095 for (auto& stream_id : sender->stream_ids()) {
1096 track_ids[stream_id].push_back(track_id);
1097 }
1098 }
1099 for (auto receiver : stats.transceiver->receivers()) {
1100 std::string track_id =
1101 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1102 kReceiver, receiver->internal()->AttachmentId());
1103 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001104 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001105 }
1106 }
1107 }
1108
1109 // Build stats for each stream ID known.
1110 for (auto& it : track_ids) {
1111 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1112 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1113 stream_stats->stream_identifier = it.first;
1114 stream_stats->track_ids = it.second;
1115 report->AddStats(std::move(stream_stats));
1116 }
1117}
1118
1119void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1120 int64_t timestamp_us,
1121 RTCStatsReport* report) const {
1122 RTC_DCHECK(signaling_thread_->IsCurrent());
1123 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1124 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
1125 for (auto sender : stats.transceiver->senders()) {
1126 senders.push_back(sender->internal());
1127 }
1128 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1129 senders, report);
1130
1131 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
1132 for (auto receiver : stats.transceiver->receivers()) {
1133 receivers.push_back(receiver->internal());
1134 }
1135 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1136 receivers, report);
1137 }
hbos09bc1282016-11-08 06:29:22 -08001138}
1139
hbos6ab97ce2016-10-03 14:16:56 -07001140void RTCStatsCollector::ProducePeerConnectionStats_s(
1141 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001142 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001143 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001144 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001145 stats->data_channels_opened = internal_record_.data_channels_opened;
1146 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001147 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001148}
1149
hbosdf6075a2016-12-19 04:58:02 -08001150void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001151 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001152 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001153 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001154 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001155
Steve Anton57858b32018-02-15 15:19:50 -08001156 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1157 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1158 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1159 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1160 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1161 } else {
1162 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001163 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001164 }
Steve Anton57858b32018-02-15 15:19:50 -08001165}
1166
1167void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1168 int64_t timestamp_us,
1169 const RtpTransceiverStatsInfo& stats,
1170 RTCStatsReport* report) const {
1171 if (!stats.mid || !stats.transport_name) {
1172 return;
1173 }
1174 RTC_DCHECK(stats.track_media_info_map);
1175 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1176 RTC_DCHECK(track_media_info_map.voice_media_info());
1177 std::string mid = *stats.mid;
1178 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1179 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1180 // Inbound
1181 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1182 track_media_info_map.voice_media_info()->receivers) {
1183 if (!voice_receiver_info.connected())
1184 continue;
1185 auto inbound_audio = rtc::MakeUnique<RTCInboundRTPStreamStats>(
1186 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1187 timestamp_us);
1188 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1189 inbound_audio.get());
1190 // TODO(hta): This lookup should look for the sender, not the track.
1191 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1192 track_media_info_map.GetAudioTrack(voice_receiver_info);
1193 if (audio_track) {
1194 inbound_audio->track_id =
1195 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1196 kReceiver,
1197 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001198 }
Steve Anton57858b32018-02-15 15:19:50 -08001199 inbound_audio->transport_id = transport_id;
1200 report->AddStats(std::move(inbound_audio));
1201 }
1202 // Outbound
1203 for (const cricket::VoiceSenderInfo& voice_sender_info :
1204 track_media_info_map.voice_media_info()->senders) {
1205 if (!voice_sender_info.connected())
1206 continue;
1207 auto outbound_audio = rtc::MakeUnique<RTCOutboundRTPStreamStats>(
1208 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1209 timestamp_us);
1210 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1211 outbound_audio.get());
1212 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1213 track_media_info_map.GetAudioTrack(voice_sender_info);
1214 if (audio_track) {
1215 outbound_audio->track_id =
1216 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1217 kSender,
1218 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001219 }
Steve Anton57858b32018-02-15 15:19:50 -08001220 outbound_audio->transport_id = transport_id;
1221 report->AddStats(std::move(outbound_audio));
1222 }
1223}
1224
1225void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1226 int64_t timestamp_us,
1227 const RtpTransceiverStatsInfo& stats,
1228 RTCStatsReport* report) const {
1229 if (!stats.mid || !stats.transport_name) {
1230 return;
1231 }
1232 RTC_DCHECK(stats.track_media_info_map);
1233 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1234 RTC_DCHECK(track_media_info_map.video_media_info());
1235 std::string mid = *stats.mid;
1236 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1237 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1238 // Inbound
1239 for (const cricket::VideoReceiverInfo& video_receiver_info :
1240 track_media_info_map.video_media_info()->receivers) {
1241 if (!video_receiver_info.connected())
1242 continue;
1243 auto inbound_video = rtc::MakeUnique<RTCInboundRTPStreamStats>(
1244 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1245 timestamp_us);
1246 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1247 inbound_video.get());
1248 rtc::scoped_refptr<VideoTrackInterface> video_track =
1249 track_media_info_map.GetVideoTrack(video_receiver_info);
1250 if (video_track) {
1251 inbound_video->track_id =
1252 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1253 kReceiver,
1254 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1255 }
1256 inbound_video->transport_id = transport_id;
1257 report->AddStats(std::move(inbound_video));
1258 }
1259 // Outbound
1260 for (const cricket::VideoSenderInfo& video_sender_info :
1261 track_media_info_map.video_media_info()->senders) {
1262 if (!video_sender_info.connected())
1263 continue;
1264 auto outbound_video = rtc::MakeUnique<RTCOutboundRTPStreamStats>(
1265 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1266 timestamp_us);
1267 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1268 outbound_video.get());
1269 rtc::scoped_refptr<VideoTrackInterface> video_track =
1270 track_media_info_map.GetVideoTrack(video_sender_info);
1271 if (video_track) {
1272 outbound_video->track_id =
1273 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1274 kSender,
1275 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1276 }
1277 outbound_video->transport_id = transport_id;
1278 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001279 }
1280}
1281
hbosdf6075a2016-12-19 04:58:02 -08001282void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001283 int64_t timestamp_us,
1284 const std::map<std::string, cricket::TransportStats>&
1285 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001286 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1287 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001288 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001289 for (const auto& entry : transport_stats_by_name) {
1290 const std::string& transport_name = entry.first;
1291 const cricket::TransportStats& transport_stats = entry.second;
1292
hbos2fa7c672016-10-24 04:00:05 -07001293 // Get reference to RTCP channel, if it exists.
1294 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001295 for (const cricket::TransportChannelStats& channel_stats :
1296 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001297 if (channel_stats.component ==
1298 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1299 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001300 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001301 break;
1302 }
1303 }
1304
1305 // Get reference to local and remote certificates of this transport, if they
1306 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001307 const auto& certificate_stats_it =
1308 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001309 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1310 std::string local_certificate_id;
1311 if (certificate_stats_it->second.local) {
1312 local_certificate_id = RTCCertificateIDFromFingerprint(
1313 certificate_stats_it->second.local->fingerprint);
1314 }
1315 std::string remote_certificate_id;
1316 if (certificate_stats_it->second.remote) {
1317 remote_certificate_id = RTCCertificateIDFromFingerprint(
1318 certificate_stats_it->second.remote->fingerprint);
1319 }
1320
1321 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001322 for (const cricket::TransportChannelStats& channel_stats :
1323 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001324 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001325 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1326 transport_name, channel_stats.component),
1327 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001328 transport_stats->bytes_sent = 0;
1329 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001330 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1331 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001332 for (const cricket::ConnectionInfo& info :
1333 channel_stats.connection_infos) {
1334 *transport_stats->bytes_sent += info.sent_total_bytes;
1335 *transport_stats->bytes_received += info.recv_total_bytes;
1336 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001337 transport_stats->selected_candidate_pair_id =
1338 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1339 }
1340 }
1341 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1342 !rtcp_transport_stats_id.empty()) {
1343 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1344 }
1345 if (!local_certificate_id.empty())
1346 transport_stats->local_certificate_id = local_certificate_id;
1347 if (!remote_certificate_id.empty())
1348 transport_stats->remote_certificate_id = remote_certificate_id;
1349 report->AddStats(std::move(transport_stats));
1350 }
1351 }
1352}
1353
1354std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001355RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001356 const std::map<std::string, cricket::TransportStats>&
1357 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001358 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001359 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001360 for (const auto& entry : transport_stats_by_name) {
1361 const std::string& transport_name = entry.first;
1362
hbos2fa7c672016-10-24 04:00:05 -07001363 CertificateStatsPair certificate_stats_pair;
1364 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001365 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001366 certificate_stats_pair.local =
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001367 local_certificate->ssl_cert_chain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001368 }
Steve Anton5dfde182018-02-06 10:34:40 -08001369
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001370 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1371 pc_->GetRemoteSSLCertChain(transport_name);
1372 if (remote_cert_chain) {
1373 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001374 }
Steve Anton5dfde182018-02-06 10:34:40 -08001375
hbos2fa7c672016-10-24 04:00:05 -07001376 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001377 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001378 }
1379 return transport_cert_stats;
1380}
1381
Steve Anton57858b32018-02-15 15:19:50 -08001382std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1383RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1384 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001385
Steve Anton57858b32018-02-15 15:19:50 -08001386 // These are used to invoke GetStats for all the media channels together in
1387 // one worker thread hop.
1388 std::map<cricket::VoiceMediaChannel*,
1389 std::unique_ptr<cricket::VoiceMediaInfo>>
1390 voice_stats;
1391 std::map<cricket::VideoMediaChannel*,
1392 std::unique_ptr<cricket::VideoMediaInfo>>
1393 video_stats;
1394
1395 for (auto transceiver : pc_->GetTransceiversInternal()) {
1396 cricket::MediaType media_type = transceiver->media_type();
1397
1398 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1399 // stats have been fetched on the worker thread.
1400 transceiver_stats_infos.emplace_back();
1401 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1402 stats.transceiver = transceiver->internal();
1403 stats.media_type = media_type;
1404
1405 cricket::BaseChannel* channel = transceiver->internal()->channel();
1406 if (!channel) {
1407 // The remaining fields require a BaseChannel.
1408 continue;
1409 }
1410
1411 stats.mid = channel->content_name();
1412 stats.transport_name = channel->transport_name();
1413
1414 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1415 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1416 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1417 voice_stats.end());
1418 voice_stats[voice_channel->media_channel()] =
1419 rtc::MakeUnique<cricket::VoiceMediaInfo>();
1420 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1421 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1422 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1423 video_stats.end());
1424 video_stats[video_channel->media_channel()] =
1425 rtc::MakeUnique<cricket::VideoMediaInfo>();
1426 } else {
1427 RTC_NOTREACHED();
1428 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001429 }
Steve Anton57858b32018-02-15 15:19:50 -08001430
1431 // Call GetStats for all media channels together on the worker thread in one
1432 // hop.
1433 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1434 for (const auto& entry : voice_stats) {
1435 if (!entry.first->GetStats(entry.second.get())) {
1436 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1437 }
1438 }
1439 for (const auto& entry : video_stats) {
1440 if (!entry.first->GetStats(entry.second.get())) {
1441 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1442 }
1443 }
1444 });
1445
1446 // Create the TrackMediaInfoMap for each transceiver stats object.
1447 for (auto& stats : transceiver_stats_infos) {
1448 auto transceiver = stats.transceiver;
1449 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1450 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1451 if (transceiver->channel()) {
1452 cricket::MediaType media_type = transceiver->media_type();
1453 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1454 auto* voice_channel =
1455 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1456 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1457 voice_media_info =
1458 std::move(voice_stats[voice_channel->media_channel()]);
1459 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1460 auto* video_channel =
1461 static_cast<cricket::VideoChannel*>(transceiver->channel());
1462 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1463 video_media_info =
1464 std::move(video_stats[video_channel->media_channel()]);
1465 }
1466 }
1467 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
1468 for (auto sender : transceiver->senders()) {
1469 senders.push_back(sender->internal());
1470 }
1471 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
1472 for (auto receiver : transceiver->receivers()) {
1473 receivers.push_back(receiver->internal());
1474 }
1475 stats.track_media_info_map = rtc::MakeUnique<TrackMediaInfoMap>(
1476 std::move(voice_media_info), std::move(video_media_info), senders,
1477 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001478 }
Steve Anton57858b32018-02-15 15:19:50 -08001479
1480 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001481}
1482
Steve Anton7eca0932018-03-30 15:18:41 -07001483std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1484 std::set<std::string> transport_names;
1485 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1486 if (transceiver->internal()->channel()) {
1487 transport_names.insert(
1488 transceiver->internal()->channel()->transport_name());
1489 }
1490 }
1491 if (pc_->rtp_data_channel()) {
1492 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1493 }
1494 if (pc_->sctp_transport_name()) {
1495 transport_names.insert(*pc_->sctp_transport_name());
1496 }
1497 return transport_names;
1498}
1499
hbos82ebe022016-11-14 01:41:09 -08001500void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1501 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1502 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1503}
1504
1505void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1506 RTC_DCHECK(signaling_thread_->IsCurrent());
1507 bool result = internal_record_.opened_data_channels.insert(
1508 reinterpret_cast<uintptr_t>(channel)).second;
1509 ++internal_record_.data_channels_opened;
1510 RTC_DCHECK(result);
1511}
1512
1513void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1514 RTC_DCHECK(signaling_thread_->IsCurrent());
1515 // Only channels that have been fully opened (and have increased the
1516 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001517 if (internal_record_.opened_data_channels.erase(
1518 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001519 ++internal_record_.data_channels_closed;
1520 }
1521}
1522
hboscc555c52016-10-18 12:48:31 -07001523const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1524 const std::string& type) {
1525 return CandidateTypeToRTCIceCandidateType(type);
1526}
1527
1528const char* DataStateToRTCDataChannelStateForTesting(
1529 DataChannelInterface::DataState state) {
1530 return DataStateToRTCDataChannelState(state);
1531}
1532
hbosd565b732016-08-30 14:04:35 -07001533} // namespace webrtc