blob: 2d6a7ac76705522ea8f90504cbd5704fd1f565ce [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 Anton5dfde182018-02-06 10:34:40 -0800779
stefanf79ade12017-06-02 06:44:03 -0700780 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
781 // thread.
782 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
783 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700784 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700785
786 invoker_.AsyncInvoke<void>(
787 RTC_FROM_HERE, network_thread_,
hbosc82f2e12016-09-05 01:36:50 -0700788 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
stefanf79ade12017-06-02 06:44:03 -0700789 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800790 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700791 }
hbosd565b732016-08-30 14:04:35 -0700792}
793
794void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700795 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700796 cached_report_ = nullptr;
797}
798
hbosb78306a2016-12-19 05:06:57 -0800799void RTCStatsCollector::WaitForPendingRequest() {
800 RTC_DCHECK(signaling_thread_->IsCurrent());
801 if (num_pending_partial_reports_) {
802 rtc::Thread::Current()->ProcessMessages(0);
803 while (num_pending_partial_reports_) {
804 rtc::Thread::Current()->SleepMs(1);
805 rtc::Thread::Current()->ProcessMessages(0);
806 }
807 }
808}
809
hbosc82f2e12016-09-05 01:36:50 -0700810void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
811 int64_t timestamp_us) {
812 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700813 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
814 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700815
hboscc555c52016-10-18 12:48:31 -0700816 ProduceDataChannelStats_s(timestamp_us, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800817 ProduceMediaStreamStats_s(timestamp_us, report.get());
818 ProduceMediaStreamTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700819 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700820
821 AddPartialResults(report);
822}
823
hbosc82f2e12016-09-05 01:36:50 -0700824void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
825 int64_t timestamp_us) {
826 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700827 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
828 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700829
Steve Anton5dfde182018-02-06 10:34:40 -0800830 std::set<std::string> transport_names;
Steve Anton57858b32018-02-15 15:19:50 -0800831 for (const auto& stats : transceiver_stats_infos_) {
832 if (stats.transport_name) {
833 transport_names.insert(*stats.transport_name);
834 }
hbosdf6075a2016-12-19 04:58:02 -0800835 }
Steve Anton5dfde182018-02-06 10:34:40 -0800836 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
837 pc_->GetTransportStatsByNames(transport_names);
838
839 std::map<std::string, CertificateStatsPair> transport_cert_stats =
840 PrepareTransportCertificateStats_n(transport_stats_by_name);
841
842 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800843 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, report.get());
Steve Anton5dfde182018-02-06 10:34:40 -0800844 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
Steve Anton5dfde182018-02-06 10:34:40 -0800845 call_stats_, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800846 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_, report.get());
Steve Anton5dfde182018-02-06 10:34:40 -0800847 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
848 transport_cert_stats, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700849
850 AddPartialResults(report);
851}
852
853void RTCStatsCollector::AddPartialResults(
854 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
855 if (!signaling_thread_->IsCurrent()) {
856 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
857 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
858 rtc::scoped_refptr<RTCStatsCollector>(this),
859 partial_report));
860 return;
861 }
862 AddPartialResults_s(partial_report);
863}
864
865void RTCStatsCollector::AddPartialResults_s(
866 rtc::scoped_refptr<RTCStatsReport> partial_report) {
867 RTC_DCHECK(signaling_thread_->IsCurrent());
868 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
869 if (!partial_report_)
870 partial_report_ = partial_report;
871 else
872 partial_report_->TakeMembersFrom(partial_report);
873 --num_pending_partial_reports_;
874 if (!num_pending_partial_reports_) {
875 cache_timestamp_us_ = partial_report_timestamp_us_;
876 cached_report_ = partial_report_;
877 partial_report_ = nullptr;
Steve Anton57858b32018-02-15 15:19:50 -0800878 transceiver_stats_infos_.clear();
ehmaldonadoa26196b2017-07-18 03:30:29 -0700879 // Trace WebRTC Stats when getStats is called on Javascript.
880 // This allows access to WebRTC stats from trace logs. To enable them,
881 // select the "webrtc_stats" category when recording traces.
ehmaldonado8ab0fd82017-09-04 14:35:04 -0700882 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
883 cached_report_->ToJson());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800884
Henrik Boström5b3541f2018-03-19 13:52:56 +0100885 // Deliver report and clear |requests_|.
886 std::vector<RequestInfo> requests;
887 requests.swap(requests_);
888 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -0700889 }
890}
891
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800892void RTCStatsCollector::DeliverCachedReport(
893 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +0100894 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -0700895 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100896 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800897 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -0800898
Henrik Boström5b3541f2018-03-19 13:52:56 +0100899 for (const RequestInfo& request : requests) {
900 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
901 request.callback()->OnStatsDelivered(cached_report);
902 } else {
903 bool filter_by_sender_selector;
904 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
905 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
906 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
907 filter_by_sender_selector = true;
908 sender_selector = request.sender_selector();
909 } else {
910 RTC_DCHECK(request.filter_mode() ==
911 RequestInfo::FilterMode::kReceiverSelector);
912 filter_by_sender_selector = false;
913 receiver_selector = request.receiver_selector();
914 }
915 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
916 filter_by_sender_selector, cached_report, sender_selector,
917 receiver_selector));
918 }
hbosc82f2e12016-09-05 01:36:50 -0700919 }
hbosd565b732016-08-30 14:04:35 -0700920}
921
hbosdf6075a2016-12-19 04:58:02 -0800922void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700923 int64_t timestamp_us,
924 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700925 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800926 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700927 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
928 if (transport_cert_stats_pair.second.local) {
929 ProduceCertificateStatsFromSSLCertificateStats(
930 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700931 }
hbos02ba2112016-10-28 05:14:53 -0700932 if (transport_cert_stats_pair.second.remote) {
933 ProduceCertificateStatsFromSSLCertificateStats(
934 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700935 }
936 }
937}
938
hbosdf6075a2016-12-19 04:58:02 -0800939void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -0800940 int64_t timestamp_us,
941 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -0800942 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800943 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -0800944 for (const auto& stats : transceiver_stats_infos) {
945 if (!stats.mid) {
946 continue;
hbos0adb8282016-11-23 02:32:06 -0800947 }
Steve Anton57858b32018-02-15 15:19:50 -0800948 const cricket::VoiceMediaInfo* voice_media_info =
949 stats.track_media_info_map->voice_media_info();
950 const cricket::VideoMediaInfo* video_media_info =
951 stats.track_media_info_map->video_media_info();
952 // Audio
953 if (voice_media_info) {
954 // Inbound
955 for (const auto& pair : voice_media_info->receive_codecs) {
956 report->AddStats(CodecStatsFromRtpCodecParameters(
957 timestamp_us, *stats.mid, true, pair.second));
958 }
959 // Outbound
960 for (const auto& pair : voice_media_info->send_codecs) {
961 report->AddStats(CodecStatsFromRtpCodecParameters(
962 timestamp_us, *stats.mid, false, pair.second));
963 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +0000964 }
Steve Anton57858b32018-02-15 15:19:50 -0800965 // Video
966 if (video_media_info) {
967 // Inbound
968 for (const auto& pair : video_media_info->receive_codecs) {
969 report->AddStats(CodecStatsFromRtpCodecParameters(
970 timestamp_us, *stats.mid, true, pair.second));
971 }
972 // Outbound
973 for (const auto& pair : video_media_info->send_codecs) {
974 report->AddStats(CodecStatsFromRtpCodecParameters(
975 timestamp_us, *stats.mid, false, pair.second));
976 }
hbos0adb8282016-11-23 02:32:06 -0800977 }
978 }
979}
980
hboscc555c52016-10-18 12:48:31 -0700981void RTCStatsCollector::ProduceDataChannelStats_s(
982 int64_t timestamp_us, RTCStatsReport* report) const {
983 RTC_DCHECK(signaling_thread_->IsCurrent());
984 for (const rtc::scoped_refptr<DataChannel>& data_channel :
985 pc_->sctp_data_channels()) {
986 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
987 new RTCDataChannelStats(
988 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
989 timestamp_us));
990 data_channel_stats->label = data_channel->label();
991 data_channel_stats->protocol = data_channel->protocol();
992 data_channel_stats->datachannelid = data_channel->id();
993 data_channel_stats->state =
994 DataStateToRTCDataChannelState(data_channel->state());
995 data_channel_stats->messages_sent = data_channel->messages_sent();
996 data_channel_stats->bytes_sent = data_channel->bytes_sent();
997 data_channel_stats->messages_received = data_channel->messages_received();
998 data_channel_stats->bytes_received = data_channel->bytes_received();
999 report->AddStats(std::move(data_channel_stats));
1000 }
1001}
1002
hbosdf6075a2016-12-19 04:58:02 -08001003void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001004 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001005 const std::map<std::string, cricket::TransportStats>&
1006 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001007 const Call::Stats& call_stats,
1008 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001009 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001010 for (const auto& entry : transport_stats_by_name) {
1011 const std::string& transport_name = entry.first;
1012 const cricket::TransportStats& transport_stats = entry.second;
1013 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001014 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001015 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001016 for (const cricket::ConnectionInfo& info :
1017 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001018 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001019 new RTCIceCandidatePairStats(
1020 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1021 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001022
hbos0583b282016-11-30 01:50:14 -08001023 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001024 // TODO(hbos): There could be other candidates that are not paired with
1025 // anything. We don't have a complete list. Local candidates come from
1026 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001027 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001028 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001029 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001030 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001031 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001032 candidate_pair_stats->state =
1033 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1034 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001035 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001036 // TODO(hbos): This writable is different than the spec. It goes to
1037 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001038 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001039 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001040 candidate_pair_stats->bytes_sent =
1041 static_cast<uint64_t>(info.sent_total_bytes);
1042 candidate_pair_stats->bytes_received =
1043 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001044 candidate_pair_stats->total_round_trip_time =
1045 static_cast<double>(info.total_round_trip_time_ms) /
1046 rtc::kNumMillisecsPerSec;
1047 if (info.current_round_trip_time_ms) {
1048 candidate_pair_stats->current_round_trip_time =
1049 static_cast<double>(*info.current_round_trip_time_ms) /
1050 rtc::kNumMillisecsPerSec;
1051 }
stefanf79ade12017-06-02 06:44:03 -07001052 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001053 // The bandwidth estimations we have are for the selected candidate
1054 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001055 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1056 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1057 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001058 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001059 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001060 }
stefanf79ade12017-06-02 06:44:03 -07001061 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001062 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001063 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001064 }
1065 }
hbosd82f5122016-12-09 04:12:39 -08001066 candidate_pair_stats->requests_received =
1067 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001068 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1069 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001070 candidate_pair_stats->responses_received =
1071 static_cast<uint64_t>(info.recv_ping_responses);
1072 candidate_pair_stats->responses_sent =
1073 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001074 RTC_DCHECK_GE(info.sent_ping_requests_total,
1075 info.sent_ping_requests_before_first_response);
1076 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1077 info.sent_ping_requests_total -
1078 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001079
1080 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001081 }
1082 }
1083 }
1084}
1085
Steve Anton57858b32018-02-15 15:19:50 -08001086void RTCStatsCollector::ProduceMediaStreamStats_s(
1087 int64_t timestamp_us,
1088 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001089 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001090
1091 std::map<std::string, std::vector<std::string>> track_ids;
1092
1093 for (const auto& stats : transceiver_stats_infos_) {
1094 for (auto sender : stats.transceiver->senders()) {
1095 std::string track_id =
1096 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1097 kSender, sender->internal()->AttachmentId());
1098 for (auto& stream_id : sender->stream_ids()) {
1099 track_ids[stream_id].push_back(track_id);
1100 }
1101 }
1102 for (auto receiver : stats.transceiver->receivers()) {
1103 std::string track_id =
1104 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1105 kReceiver, receiver->internal()->AttachmentId());
1106 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001107 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001108 }
1109 }
1110 }
1111
1112 // Build stats for each stream ID known.
1113 for (auto& it : track_ids) {
1114 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1115 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1116 stream_stats->stream_identifier = it.first;
1117 stream_stats->track_ids = it.second;
1118 report->AddStats(std::move(stream_stats));
1119 }
1120}
1121
1122void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1123 int64_t timestamp_us,
1124 RTCStatsReport* report) const {
1125 RTC_DCHECK(signaling_thread_->IsCurrent());
1126 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1127 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
1128 for (auto sender : stats.transceiver->senders()) {
1129 senders.push_back(sender->internal());
1130 }
1131 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1132 senders, report);
1133
1134 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
1135 for (auto receiver : stats.transceiver->receivers()) {
1136 receivers.push_back(receiver->internal());
1137 }
1138 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1139 receivers, report);
1140 }
hbos09bc1282016-11-08 06:29:22 -08001141}
1142
hbos6ab97ce2016-10-03 14:16:56 -07001143void RTCStatsCollector::ProducePeerConnectionStats_s(
1144 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001145 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001146 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001147 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001148 stats->data_channels_opened = internal_record_.data_channels_opened;
1149 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001150 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001151}
1152
hbosdf6075a2016-12-19 04:58:02 -08001153void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001154 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001155 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001156 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001157 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001158
Steve Anton57858b32018-02-15 15:19:50 -08001159 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1160 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1161 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1162 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1163 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1164 } else {
1165 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001166 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001167 }
Steve Anton57858b32018-02-15 15:19:50 -08001168}
1169
1170void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1171 int64_t timestamp_us,
1172 const RtpTransceiverStatsInfo& stats,
1173 RTCStatsReport* report) const {
1174 if (!stats.mid || !stats.transport_name) {
1175 return;
1176 }
1177 RTC_DCHECK(stats.track_media_info_map);
1178 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1179 RTC_DCHECK(track_media_info_map.voice_media_info());
1180 std::string mid = *stats.mid;
1181 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1182 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1183 // Inbound
1184 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1185 track_media_info_map.voice_media_info()->receivers) {
1186 if (!voice_receiver_info.connected())
1187 continue;
1188 auto inbound_audio = rtc::MakeUnique<RTCInboundRTPStreamStats>(
1189 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1190 timestamp_us);
1191 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1192 inbound_audio.get());
1193 // TODO(hta): This lookup should look for the sender, not the track.
1194 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1195 track_media_info_map.GetAudioTrack(voice_receiver_info);
1196 if (audio_track) {
1197 inbound_audio->track_id =
1198 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1199 kReceiver,
1200 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001201 }
Steve Anton57858b32018-02-15 15:19:50 -08001202 inbound_audio->transport_id = transport_id;
1203 report->AddStats(std::move(inbound_audio));
1204 }
1205 // Outbound
1206 for (const cricket::VoiceSenderInfo& voice_sender_info :
1207 track_media_info_map.voice_media_info()->senders) {
1208 if (!voice_sender_info.connected())
1209 continue;
1210 auto outbound_audio = rtc::MakeUnique<RTCOutboundRTPStreamStats>(
1211 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1212 timestamp_us);
1213 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1214 outbound_audio.get());
1215 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1216 track_media_info_map.GetAudioTrack(voice_sender_info);
1217 if (audio_track) {
1218 outbound_audio->track_id =
1219 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1220 kSender,
1221 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001222 }
Steve Anton57858b32018-02-15 15:19:50 -08001223 outbound_audio->transport_id = transport_id;
1224 report->AddStats(std::move(outbound_audio));
1225 }
1226}
1227
1228void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1229 int64_t timestamp_us,
1230 const RtpTransceiverStatsInfo& stats,
1231 RTCStatsReport* report) const {
1232 if (!stats.mid || !stats.transport_name) {
1233 return;
1234 }
1235 RTC_DCHECK(stats.track_media_info_map);
1236 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1237 RTC_DCHECK(track_media_info_map.video_media_info());
1238 std::string mid = *stats.mid;
1239 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1240 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1241 // Inbound
1242 for (const cricket::VideoReceiverInfo& video_receiver_info :
1243 track_media_info_map.video_media_info()->receivers) {
1244 if (!video_receiver_info.connected())
1245 continue;
1246 auto inbound_video = rtc::MakeUnique<RTCInboundRTPStreamStats>(
1247 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1248 timestamp_us);
1249 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1250 inbound_video.get());
1251 rtc::scoped_refptr<VideoTrackInterface> video_track =
1252 track_media_info_map.GetVideoTrack(video_receiver_info);
1253 if (video_track) {
1254 inbound_video->track_id =
1255 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1256 kReceiver,
1257 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1258 }
1259 inbound_video->transport_id = transport_id;
1260 report->AddStats(std::move(inbound_video));
1261 }
1262 // Outbound
1263 for (const cricket::VideoSenderInfo& video_sender_info :
1264 track_media_info_map.video_media_info()->senders) {
1265 if (!video_sender_info.connected())
1266 continue;
1267 auto outbound_video = rtc::MakeUnique<RTCOutboundRTPStreamStats>(
1268 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1269 timestamp_us);
1270 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1271 outbound_video.get());
1272 rtc::scoped_refptr<VideoTrackInterface> video_track =
1273 track_media_info_map.GetVideoTrack(video_sender_info);
1274 if (video_track) {
1275 outbound_video->track_id =
1276 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1277 kSender,
1278 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1279 }
1280 outbound_video->transport_id = transport_id;
1281 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001282 }
1283}
1284
hbosdf6075a2016-12-19 04:58:02 -08001285void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001286 int64_t timestamp_us,
1287 const std::map<std::string, cricket::TransportStats>&
1288 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001289 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1290 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001291 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001292 for (const auto& entry : transport_stats_by_name) {
1293 const std::string& transport_name = entry.first;
1294 const cricket::TransportStats& transport_stats = entry.second;
1295
hbos2fa7c672016-10-24 04:00:05 -07001296 // Get reference to RTCP channel, if it exists.
1297 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001298 for (const cricket::TransportChannelStats& channel_stats :
1299 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001300 if (channel_stats.component ==
1301 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1302 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001303 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001304 break;
1305 }
1306 }
1307
1308 // Get reference to local and remote certificates of this transport, if they
1309 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001310 const auto& certificate_stats_it =
1311 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001312 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1313 std::string local_certificate_id;
1314 if (certificate_stats_it->second.local) {
1315 local_certificate_id = RTCCertificateIDFromFingerprint(
1316 certificate_stats_it->second.local->fingerprint);
1317 }
1318 std::string remote_certificate_id;
1319 if (certificate_stats_it->second.remote) {
1320 remote_certificate_id = RTCCertificateIDFromFingerprint(
1321 certificate_stats_it->second.remote->fingerprint);
1322 }
1323
1324 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001325 for (const cricket::TransportChannelStats& channel_stats :
1326 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001327 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001328 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1329 transport_name, channel_stats.component),
1330 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001331 transport_stats->bytes_sent = 0;
1332 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001333 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1334 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001335 for (const cricket::ConnectionInfo& info :
1336 channel_stats.connection_infos) {
1337 *transport_stats->bytes_sent += info.sent_total_bytes;
1338 *transport_stats->bytes_received += info.recv_total_bytes;
1339 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001340 transport_stats->selected_candidate_pair_id =
1341 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1342 }
1343 }
1344 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1345 !rtcp_transport_stats_id.empty()) {
1346 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1347 }
1348 if (!local_certificate_id.empty())
1349 transport_stats->local_certificate_id = local_certificate_id;
1350 if (!remote_certificate_id.empty())
1351 transport_stats->remote_certificate_id = remote_certificate_id;
1352 report->AddStats(std::move(transport_stats));
1353 }
1354 }
1355}
1356
1357std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001358RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001359 const std::map<std::string, cricket::TransportStats>&
1360 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001361 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001362 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001363 for (const auto& entry : transport_stats_by_name) {
1364 const std::string& transport_name = entry.first;
1365
hbos2fa7c672016-10-24 04:00:05 -07001366 CertificateStatsPair certificate_stats_pair;
1367 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001368 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001369 certificate_stats_pair.local =
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001370 local_certificate->ssl_cert_chain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001371 }
Steve Anton5dfde182018-02-06 10:34:40 -08001372
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001373 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1374 pc_->GetRemoteSSLCertChain(transport_name);
1375 if (remote_cert_chain) {
1376 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001377 }
Steve Anton5dfde182018-02-06 10:34:40 -08001378
hbos2fa7c672016-10-24 04:00:05 -07001379 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001380 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001381 }
1382 return transport_cert_stats;
1383}
1384
Steve Anton57858b32018-02-15 15:19:50 -08001385std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1386RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1387 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001388
Steve Anton57858b32018-02-15 15:19:50 -08001389 // These are used to invoke GetStats for all the media channels together in
1390 // one worker thread hop.
1391 std::map<cricket::VoiceMediaChannel*,
1392 std::unique_ptr<cricket::VoiceMediaInfo>>
1393 voice_stats;
1394 std::map<cricket::VideoMediaChannel*,
1395 std::unique_ptr<cricket::VideoMediaInfo>>
1396 video_stats;
1397
1398 for (auto transceiver : pc_->GetTransceiversInternal()) {
1399 cricket::MediaType media_type = transceiver->media_type();
1400
1401 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1402 // stats have been fetched on the worker thread.
1403 transceiver_stats_infos.emplace_back();
1404 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1405 stats.transceiver = transceiver->internal();
1406 stats.media_type = media_type;
1407
1408 cricket::BaseChannel* channel = transceiver->internal()->channel();
1409 if (!channel) {
1410 // The remaining fields require a BaseChannel.
1411 continue;
1412 }
1413
1414 stats.mid = channel->content_name();
1415 stats.transport_name = channel->transport_name();
1416
1417 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1418 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1419 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1420 voice_stats.end());
1421 voice_stats[voice_channel->media_channel()] =
1422 rtc::MakeUnique<cricket::VoiceMediaInfo>();
1423 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1424 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1425 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1426 video_stats.end());
1427 video_stats[video_channel->media_channel()] =
1428 rtc::MakeUnique<cricket::VideoMediaInfo>();
1429 } else {
1430 RTC_NOTREACHED();
1431 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001432 }
Steve Anton57858b32018-02-15 15:19:50 -08001433
1434 // Call GetStats for all media channels together on the worker thread in one
1435 // hop.
1436 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1437 for (const auto& entry : voice_stats) {
1438 if (!entry.first->GetStats(entry.second.get())) {
1439 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1440 }
1441 }
1442 for (const auto& entry : video_stats) {
1443 if (!entry.first->GetStats(entry.second.get())) {
1444 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1445 }
1446 }
1447 });
1448
1449 // Create the TrackMediaInfoMap for each transceiver stats object.
1450 for (auto& stats : transceiver_stats_infos) {
1451 auto transceiver = stats.transceiver;
1452 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1453 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1454 if (transceiver->channel()) {
1455 cricket::MediaType media_type = transceiver->media_type();
1456 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1457 auto* voice_channel =
1458 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1459 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1460 voice_media_info =
1461 std::move(voice_stats[voice_channel->media_channel()]);
1462 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1463 auto* video_channel =
1464 static_cast<cricket::VideoChannel*>(transceiver->channel());
1465 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1466 video_media_info =
1467 std::move(video_stats[video_channel->media_channel()]);
1468 }
1469 }
1470 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
1471 for (auto sender : transceiver->senders()) {
1472 senders.push_back(sender->internal());
1473 }
1474 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
1475 for (auto receiver : transceiver->receivers()) {
1476 receivers.push_back(receiver->internal());
1477 }
1478 stats.track_media_info_map = rtc::MakeUnique<TrackMediaInfoMap>(
1479 std::move(voice_media_info), std::move(video_media_info), senders,
1480 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001481 }
Steve Anton57858b32018-02-15 15:19:50 -08001482
1483 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001484}
1485
hbos82ebe022016-11-14 01:41:09 -08001486void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1487 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1488 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1489}
1490
1491void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1492 RTC_DCHECK(signaling_thread_->IsCurrent());
1493 bool result = internal_record_.opened_data_channels.insert(
1494 reinterpret_cast<uintptr_t>(channel)).second;
1495 ++internal_record_.data_channels_opened;
1496 RTC_DCHECK(result);
1497}
1498
1499void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1500 RTC_DCHECK(signaling_thread_->IsCurrent());
1501 // Only channels that have been fully opened (and have increased the
1502 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001503 if (internal_record_.opened_data_channels.erase(
1504 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001505 ++internal_record_.data_channels_closed;
1506 }
1507}
1508
hboscc555c52016-10-18 12:48:31 -07001509const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1510 const std::string& type) {
1511 return CandidateTypeToRTCIceCandidateType(type);
1512}
1513
1514const char* DataStateToRTCDataChannelStateForTesting(
1515 DataChannelInterface::DataState state) {
1516 return DataStateToRTCDataChannelState(state);
1517}
1518
hbosd565b732016-08-30 14:04:35 -07001519} // namespace webrtc