blob: 0f4dc3de2bd23307d85111e2abe8593ffa62e7ac [file] [log] [blame]
hbosd565b732016-08-30 14:04:35 -07001/*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/rtcstatscollector.h"
hbosd565b732016-08-30 14:04:35 -070012
13#include <memory>
hbos9e302742017-01-20 02:47:10 -080014#include <sstream>
Steve Anton36b29d12017-10-30 09:57:42 -070015#include <string>
hbosd565b732016-08-30 14:04:35 -070016#include <utility>
17#include <vector>
18
Patrik Höglunde2d6a062017-10-05 14:53:33 +020019#include "api/candidate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/mediastreaminterface.h"
21#include "api/peerconnectioninterface.h"
22#include "media/base/mediachannel.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "p2p/base/p2pconstants.h"
24#include "p2p/base/port.h"
25#include "pc/peerconnection.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/checks.h"
27#include "rtc_base/stringutils.h"
28#include "rtc_base/timeutils.h"
29#include "rtc_base/trace_event.h"
hbosd565b732016-08-30 14:04:35 -070030
31namespace webrtc {
32
hboscc555c52016-10-18 12:48:31 -070033namespace {
34
hbos2fa7c672016-10-24 04:00:05 -070035std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
36 return "RTCCertificate_" + fingerprint;
37}
38
hbos0adb8282016-11-23 02:32:06 -080039std::string RTCCodecStatsIDFromDirectionMediaAndPayload(
40 bool inbound, bool audio, uint32_t payload_type) {
hbos585a9b12017-02-07 04:59:16 -080041 // TODO(hbos): The present codec ID assignment is not sufficient to support
42 // Unified Plan or unbundled connections in all cases. When we are able to
43 // handle multiple m= lines of the same media type (and multiple BaseChannels
44 // for the same type is possible?) this needs to be updated to differentiate
45 // the transport being used, and stats need to be collected for all of them.
hbos0adb8282016-11-23 02:32:06 -080046 if (inbound) {
47 return audio ? "RTCCodec_InboundAudio_" + rtc::ToString<>(payload_type)
48 : "RTCCodec_InboundVideo_" + rtc::ToString<>(payload_type);
49 }
50 return audio ? "RTCCodec_OutboundAudio_" + rtc::ToString<>(payload_type)
51 : "RTCCodec_OutboundVideo_" + rtc::ToString<>(payload_type);
52}
53
hbos2fa7c672016-10-24 04:00:05 -070054std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
55 const cricket::ConnectionInfo& info) {
56 return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" +
57 info.remote_candidate.id();
58}
59
Harald Alvestrandc72af932018-01-11 17:18:19 +010060std::string RTCMediaStreamTrackStatsIDFromTrackKindIDAndAttachment(
61 bool is_local,
62 const char* kind,
63 const std::string& id,
64 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -080065 RTC_DCHECK(kind == MediaStreamTrackInterface::kAudioKind ||
66 kind == MediaStreamTrackInterface::kVideoKind);
67 std::ostringstream oss;
68 oss << (is_local ? "RTCMediaStreamTrack_local_"
69 : "RTCMediaStreamTrack_remote_");
70 oss << kind << "_";
71 oss << id << "_";
Harald Alvestrandc72af932018-01-11 17:18:19 +010072 oss << attachment_id;
hbos9e302742017-01-20 02:47:10 -080073 return oss.str();
hbos09bc1282016-11-08 06:29:22 -080074}
75
hbos2fa7c672016-10-24 04:00:05 -070076std::string RTCTransportStatsIDFromTransportChannel(
77 const std::string& transport_name, int channel_component) {
78 return "RTCTransport_" + transport_name + "_" +
79 rtc::ToString<>(channel_component);
80}
81
hboseeafe942016-11-01 03:00:17 -070082std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
83 return audio ? "RTCInboundRTPAudioStream_" + rtc::ToString<>(ssrc)
84 : "RTCInboundRTPVideoStream_" + rtc::ToString<>(ssrc);
85}
86
hbos6ded1902016-11-01 01:50:46 -070087std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
88 return audio ? "RTCOutboundRTPAudioStream_" + rtc::ToString<>(ssrc)
89 : "RTCOutboundRTPVideoStream_" + rtc::ToString<>(ssrc);
90}
91
hbosab9f6e42016-10-07 02:18:47 -070092const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
93 if (type == cricket::LOCAL_PORT_TYPE)
94 return RTCIceCandidateType::kHost;
95 if (type == cricket::STUN_PORT_TYPE)
96 return RTCIceCandidateType::kSrflx;
97 if (type == cricket::PRFLX_PORT_TYPE)
98 return RTCIceCandidateType::kPrflx;
99 if (type == cricket::RELAY_PORT_TYPE)
100 return RTCIceCandidateType::kRelay;
101 RTC_NOTREACHED();
102 return nullptr;
103}
104
hboscc555c52016-10-18 12:48:31 -0700105const char* DataStateToRTCDataChannelState(
106 DataChannelInterface::DataState state) {
107 switch (state) {
108 case DataChannelInterface::kConnecting:
109 return RTCDataChannelState::kConnecting;
110 case DataChannelInterface::kOpen:
111 return RTCDataChannelState::kOpen;
112 case DataChannelInterface::kClosing:
113 return RTCDataChannelState::kClosing;
114 case DataChannelInterface::kClosed:
115 return RTCDataChannelState::kClosed;
116 default:
117 RTC_NOTREACHED();
118 return nullptr;
119 }
120}
121
hbos06495bc2017-01-02 08:08:18 -0800122const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
123 cricket::IceCandidatePairState state) {
124 switch (state) {
125 case cricket::IceCandidatePairState::WAITING:
126 return RTCStatsIceCandidatePairState::kWaiting;
127 case cricket::IceCandidatePairState::IN_PROGRESS:
128 return RTCStatsIceCandidatePairState::kInProgress;
129 case cricket::IceCandidatePairState::SUCCEEDED:
130 return RTCStatsIceCandidatePairState::kSucceeded;
131 case cricket::IceCandidatePairState::FAILED:
132 return RTCStatsIceCandidatePairState::kFailed;
133 default:
134 RTC_NOTREACHED();
135 return nullptr;
136 }
137}
138
hbos7064d592017-01-16 07:38:02 -0800139const char* DtlsTransportStateToRTCDtlsTransportState(
140 cricket::DtlsTransportState state) {
141 switch (state) {
142 case cricket::DTLS_TRANSPORT_NEW:
143 return RTCDtlsTransportState::kNew;
144 case cricket::DTLS_TRANSPORT_CONNECTING:
145 return RTCDtlsTransportState::kConnecting;
146 case cricket::DTLS_TRANSPORT_CONNECTED:
147 return RTCDtlsTransportState::kConnected;
148 case cricket::DTLS_TRANSPORT_CLOSED:
149 return RTCDtlsTransportState::kClosed;
150 case cricket::DTLS_TRANSPORT_FAILED:
151 return RTCDtlsTransportState::kFailed;
152 default:
153 RTC_NOTREACHED();
154 return nullptr;
155 }
156}
157
Gary Liu37e489c2017-11-21 10:49:36 -0800158const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
159 switch (type) {
160 case rtc::ADAPTER_TYPE_CELLULAR:
161 return RTCNetworkType::kCellular;
162 case rtc::ADAPTER_TYPE_ETHERNET:
163 return RTCNetworkType::kEthernet;
164 case rtc::ADAPTER_TYPE_WIFI:
165 return RTCNetworkType::kWifi;
166 case rtc::ADAPTER_TYPE_VPN:
167 return RTCNetworkType::kVpn;
168 case rtc::ADAPTER_TYPE_UNKNOWN:
169 case rtc::ADAPTER_TYPE_LOOPBACK:
170 return RTCNetworkType::kUnknown;
171 }
172 RTC_NOTREACHED();
173 return nullptr;
174}
175
hbos9e302742017-01-20 02:47:10 -0800176double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
177 RTC_DCHECK_GE(audio_level, 0);
178 RTC_DCHECK_LE(audio_level, 32767);
179 return audio_level / 32767.0;
180}
181
hbos0adb8282016-11-23 02:32:06 -0800182std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
183 uint64_t timestamp_us, bool inbound, bool audio,
184 const RtpCodecParameters& codec_params) {
185 RTC_DCHECK_GE(codec_params.payload_type, 0);
186 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 12:09:01 -0800187 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 02:32:06 -0800188 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
189 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
190 RTCCodecStatsIDFromDirectionMediaAndPayload(inbound, audio, payload_type),
191 timestamp_us));
192 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 06:56:04 -0800193 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 12:09:01 -0800194 if (codec_params.clock_rate) {
195 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
196 }
hbos0adb8282016-11-23 02:32:06 -0800197 return codec_stats;
198}
199
hbos09bc1282016-11-08 06:29:22 -0800200void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
201 const MediaStreamTrackInterface& track,
202 RTCMediaStreamTrackStats* track_stats) {
203 track_stats->track_identifier = track.id();
204 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
205}
206
hbos820f5782016-11-22 03:16:50 -0800207// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700208void SetInboundRTPStreamStatsFromMediaReceiverInfo(
209 const cricket::MediaReceiverInfo& media_receiver_info,
210 RTCInboundRTPStreamStats* inbound_stats) {
211 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800212 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100213 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 03:00:17 -0700214 inbound_stats->is_remote = false;
hboseeafe942016-11-01 03:00:17 -0700215 inbound_stats->packets_received =
216 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
217 inbound_stats->bytes_received =
218 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800219 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 12:26:04 +0100220 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700221 inbound_stats->fraction_lost =
222 static_cast<double>(media_receiver_info.fraction_lost);
223}
224
225void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
226 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800227 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700228 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800229 voice_receiver_info, inbound_audio);
230 inbound_audio->media_type = "audio";
hbos585a9b12017-02-07 04:59:16 -0800231 if (voice_receiver_info.codec_payload_type) {
232 inbound_audio->codec_id =
233 RTCCodecStatsIDFromDirectionMediaAndPayload(
234 true, true, *voice_receiver_info.codec_payload_type);
235 }
hbos820f5782016-11-22 03:16:50 -0800236 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700237 static_cast<double>(voice_receiver_info.jitter_ms) /
238 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800239 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
240 // purposefully left undefined for audio.
hboseeafe942016-11-01 03:00:17 -0700241}
242
243void SetInboundRTPStreamStatsFromVideoReceiverInfo(
244 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800245 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700246 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800247 video_receiver_info, inbound_video);
248 inbound_video->media_type = "video";
hbos585a9b12017-02-07 04:59:16 -0800249 if (video_receiver_info.codec_payload_type) {
250 inbound_video->codec_id =
251 RTCCodecStatsIDFromDirectionMediaAndPayload(
252 true, false, *video_receiver_info.codec_payload_type);
253 }
hbos820f5782016-11-22 03:16:50 -0800254 inbound_video->fir_count =
255 static_cast<uint32_t>(video_receiver_info.firs_sent);
256 inbound_video->pli_count =
257 static_cast<uint32_t>(video_receiver_info.plis_sent);
258 inbound_video->nack_count =
259 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800260 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800261 if (video_receiver_info.qp_sum)
262 inbound_video->qp_sum = *video_receiver_info.qp_sum;
hboseeafe942016-11-01 03:00:17 -0700263}
264
hbos820f5782016-11-22 03:16:50 -0800265// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700266void SetOutboundRTPStreamStatsFromMediaSenderInfo(
267 const cricket::MediaSenderInfo& media_sender_info,
268 RTCOutboundRTPStreamStats* outbound_stats) {
269 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800270 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100271 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700272 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700273 outbound_stats->packets_sent =
274 static_cast<uint32_t>(media_sender_info.packets_sent);
275 outbound_stats->bytes_sent =
276 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbos6ded1902016-11-01 01:50:46 -0700277}
278
279void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
280 const cricket::VoiceSenderInfo& voice_sender_info,
281 RTCOutboundRTPStreamStats* outbound_audio) {
282 SetOutboundRTPStreamStatsFromMediaSenderInfo(
283 voice_sender_info, outbound_audio);
284 outbound_audio->media_type = "audio";
hbos585a9b12017-02-07 04:59:16 -0800285 if (voice_sender_info.codec_payload_type) {
286 outbound_audio->codec_id =
287 RTCCodecStatsIDFromDirectionMediaAndPayload(
288 false, true, *voice_sender_info.codec_payload_type);
289 }
hbos6ded1902016-11-01 01:50:46 -0700290 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
291 // purposefully left undefined for audio.
292}
293
294void SetOutboundRTPStreamStatsFromVideoSenderInfo(
295 const cricket::VideoSenderInfo& video_sender_info,
296 RTCOutboundRTPStreamStats* outbound_video) {
297 SetOutboundRTPStreamStatsFromMediaSenderInfo(
298 video_sender_info, outbound_video);
299 outbound_video->media_type = "video";
hbos585a9b12017-02-07 04:59:16 -0800300 if (video_sender_info.codec_payload_type) {
301 outbound_video->codec_id =
302 RTCCodecStatsIDFromDirectionMediaAndPayload(
303 false, false, *video_sender_info.codec_payload_type);
304 }
hbos6ded1902016-11-01 01:50:46 -0700305 outbound_video->fir_count =
306 static_cast<uint32_t>(video_sender_info.firs_rcvd);
307 outbound_video->pli_count =
308 static_cast<uint32_t>(video_sender_info.plis_rcvd);
309 outbound_video->nack_count =
310 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800311 if (video_sender_info.qp_sum)
312 outbound_video->qp_sum = *video_sender_info.qp_sum;
313 outbound_video->frames_encoded = video_sender_info.frames_encoded;
hbos6ded1902016-11-01 01:50:46 -0700314}
315
hbos02ba2112016-10-28 05:14:53 -0700316void ProduceCertificateStatsFromSSLCertificateStats(
317 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
318 RTCStatsReport* report) {
319 RTCCertificateStats* prev_certificate_stats = nullptr;
320 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
321 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800322 std::string certificate_stats_id =
323 RTCCertificateIDFromFingerprint(s->fingerprint);
324 // It is possible for the same certificate to show up multiple times, e.g.
325 // if local and remote side use the same certificate in a loopback call.
326 // If the report already contains stats for this certificate, skip it.
327 if (report->Get(certificate_stats_id)) {
328 RTC_DCHECK_EQ(s, &certificate_stats);
329 break;
330 }
hbos02ba2112016-10-28 05:14:53 -0700331 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800332 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700333 certificate_stats->fingerprint = s->fingerprint;
334 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
335 certificate_stats->base64_certificate = s->base64_certificate;
336 if (prev_certificate_stats)
337 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
338 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
339 prev_certificate_stats = certificate_stats;
340 }
341}
342
343const std::string& ProduceIceCandidateStats(
344 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800345 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700346 const std::string& id = "RTCIceCandidate_" + candidate.id();
347 const RTCStats* stats = report->Get(id);
348 if (!stats) {
349 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
350 if (is_local)
351 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
352 else
353 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800354 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800355 if (is_local) {
356 candidate_stats->network_type =
357 NetworkAdapterTypeToStatsType(candidate.network_type());
358 } else {
359 // We don't expect to know the adapter type of remote candidates.
360 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
361 }
hbos02ba2112016-10-28 05:14:53 -0700362 candidate_stats->ip = candidate.address().ipaddr().ToString();
363 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
364 candidate_stats->protocol = candidate.protocol();
365 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
366 candidate.type());
367 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
368
369 stats = candidate_stats.get();
370 report->AddStats(std::move(candidate_stats));
371 }
372 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
373 : RTCRemoteIceCandidateStats::kType);
374 return stats->id();
375}
376
hbos9e302742017-01-20 02:47:10 -0800377std::unique_ptr<RTCMediaStreamTrackStats>
378ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
379 int64_t timestamp_us,
380 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100381 const cricket::VoiceSenderInfo& voice_sender_info,
382 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800383 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
384 new RTCMediaStreamTrackStats(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100385 RTCMediaStreamTrackStatsIDFromTrackKindIDAndAttachment(
hbos9e302742017-01-20 02:47:10 -0800386 true, MediaStreamTrackInterface::kAudioKind, audio_track.id(),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100387 attachment_id),
388 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800389 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
390 audio_track, audio_track_stats.get());
391 audio_track_stats->remote_source = false;
392 audio_track_stats->detached = false;
393 if (voice_sender_info.audio_level >= 0) {
394 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
395 voice_sender_info.audio_level);
396 }
zsteine76bd3a2017-07-14 12:17:49 -0700397 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
398 audio_track_stats->total_samples_duration =
399 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100400 if (voice_sender_info.apm_statistics.echo_return_loss) {
401 audio_track_stats->echo_return_loss =
402 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800403 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100404 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
405 audio_track_stats->echo_return_loss_enhancement =
406 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800407 }
408 return audio_track_stats;
409}
410
411std::unique_ptr<RTCMediaStreamTrackStats>
412ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
413 int64_t timestamp_us,
414 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100415 const cricket::VoiceReceiverInfo& voice_receiver_info,
416 int attachment_id) {
417 // Since receiver tracks can't be reattached, we use the SSRC as
418 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800419 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
420 new RTCMediaStreamTrackStats(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100421 RTCMediaStreamTrackStatsIDFromTrackKindIDAndAttachment(
hbos9e302742017-01-20 02:47:10 -0800422 false, MediaStreamTrackInterface::kAudioKind, audio_track.id(),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100423 attachment_id),
424 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800425 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
426 audio_track, audio_track_stats.get());
427 audio_track_stats->remote_source = true;
428 audio_track_stats->detached = false;
429 if (voice_receiver_info.audio_level >= 0) {
430 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
431 voice_receiver_info.audio_level);
432 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200433 audio_track_stats->jitter_buffer_delay =
434 voice_receiver_info.jitter_buffer_delay_seconds;
zsteine76bd3a2017-07-14 12:17:49 -0700435 audio_track_stats->total_audio_energy =
436 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700437 audio_track_stats->total_samples_received =
438 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700439 audio_track_stats->total_samples_duration =
440 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700441 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200442 audio_track_stats->concealment_events =
443 voice_receiver_info.concealment_events;
hbos9e302742017-01-20 02:47:10 -0800444 return audio_track_stats;
445}
446
447std::unique_ptr<RTCMediaStreamTrackStats>
448ProduceMediaStreamTrackStatsFromVideoSenderInfo(
449 int64_t timestamp_us,
450 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100451 const cricket::VideoSenderInfo& video_sender_info,
452 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800453 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
454 new RTCMediaStreamTrackStats(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100455 RTCMediaStreamTrackStatsIDFromTrackKindIDAndAttachment(
hbos9e302742017-01-20 02:47:10 -0800456 true, MediaStreamTrackInterface::kVideoKind, video_track.id(),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100457 attachment_id),
458 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800459 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
460 video_track, video_track_stats.get());
461 video_track_stats->remote_source = false;
462 video_track_stats->detached = false;
463 video_track_stats->frame_width = static_cast<uint32_t>(
464 video_sender_info.send_frame_width);
465 video_track_stats->frame_height = static_cast<uint32_t>(
466 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800467 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100468 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800469 video_track_stats->frames_sent = video_sender_info.frames_encoded;
hbos9e302742017-01-20 02:47:10 -0800470 return video_track_stats;
471}
472
473std::unique_ptr<RTCMediaStreamTrackStats>
474ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
475 int64_t timestamp_us,
476 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100477 const cricket::VideoReceiverInfo& video_receiver_info,
478 int attachment_id) {
479 // Since receiver tracks can't be reattached, we use the SSRC as
480 // attachment ID.
hbos9e302742017-01-20 02:47:10 -0800481 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
482 new RTCMediaStreamTrackStats(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100483 RTCMediaStreamTrackStatsIDFromTrackKindIDAndAttachment(
hbos9e302742017-01-20 02:47:10 -0800484 false, MediaStreamTrackInterface::kVideoKind, video_track.id(),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100485 attachment_id),
486 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800487 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
488 video_track, video_track_stats.get());
489 video_track_stats->remote_source = true;
490 video_track_stats->detached = false;
491 if (video_receiver_info.frame_width > 0 &&
492 video_receiver_info.frame_height > 0) {
493 video_track_stats->frame_width = static_cast<uint32_t>(
494 video_receiver_info.frame_width);
495 video_track_stats->frame_height = static_cast<uint32_t>(
496 video_receiver_info.frame_height);
497 }
hbos42f6d2f2017-01-20 03:56:50 -0800498 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800499 // TODO(hbos): When we support receiving simulcast, this should be the total
500 // number of frames correctly decoded, independent of which SSRC it was
501 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100502 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800503 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800504 RTC_DCHECK_GE(video_receiver_info.frames_received,
505 video_receiver_info.frames_rendered);
506 video_track_stats->frames_dropped = video_receiver_info.frames_received -
507 video_receiver_info.frames_rendered;
hbos9e302742017-01-20 02:47:10 -0800508 return video_track_stats;
509}
510
Harald Alvestrand89061872018-01-02 14:08:34 +0100511void ProduceSenderMediaTrackStats(
512 int64_t timestamp_us,
513 const TrackMediaInfoMap& track_media_info_map,
514 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders,
515 RTCStatsReport* report) {
516 // This function iterates over the senders to generate outgoing track stats.
517
518 // TODO(hbos): Return stats of detached tracks. We have to perform stats
519 // gathering at the time of detachment to get accurate stats and timestamps.
520 // https://crbug.com/659137
521 for (auto const sender : senders) {
522 // Don't report on tracks before starting to send.
523 // https://bugs.webrtc.org/8673
524 if (sender->ssrc() == 0)
525 continue;
526 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
527 AudioTrackInterface* track =
528 static_cast<AudioTrackInterface*>(sender->track().get());
529 if (!track)
530 continue;
531 auto voice_sender_info =
532 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
533 RTC_CHECK(voice_sender_info)
534 << "No voice sender info for sender with ssrc " << sender->ssrc();
535 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100536 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
537 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100538 report->AddStats(std::move(audio_track_stats));
539 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
540 VideoTrackInterface* track =
541 static_cast<VideoTrackInterface*>(sender->track().get());
542 if (!track)
543 continue;
544 auto video_sender_info =
545 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
546 RTC_CHECK(video_sender_info)
547 << "No video sender info for sender with ssrc " << sender->ssrc();
548 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100549 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
550 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100551 report->AddStats(std::move(video_track_stats));
552 } else {
553 RTC_NOTREACHED();
554 }
555 }
556}
557
558void ProduceReceiverMediaTrackStats(
559 int64_t timestamp_us,
560 const TrackMediaInfoMap& track_media_info_map,
561 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers,
562 RTCStatsReport* report) {
563 // This function iterates over the receivers to find the remote tracks.
564 for (auto const receiver : receivers) {
565 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
566 AudioTrackInterface* track =
567 static_cast<AudioTrackInterface*>(receiver->track().get());
568 const cricket::VoiceReceiverInfo* voice_receiver_info =
569 track_media_info_map.GetVoiceReceiverInfo(*track);
570 if (!voice_receiver_info) {
571 continue;
572 }
573 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
574 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100575 timestamp_us, *track, *voice_receiver_info,
576 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100577 report->AddStats(std::move(audio_track_stats));
578 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
579 VideoTrackInterface* track =
580 static_cast<VideoTrackInterface*>(receiver->track().get());
581 const cricket::VideoReceiverInfo* video_receiver_info =
582 track_media_info_map.GetVideoReceiverInfo(*track);
583 if (!video_receiver_info) {
584 continue;
585 }
586 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
587 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100588 timestamp_us, *track, *video_receiver_info,
589 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100590 report->AddStats(std::move(video_track_stats));
591 } else {
592 RTC_NOTREACHED();
593 }
594 }
595}
596
597void ProduceMediaStreamStats(
hbos09bc1282016-11-08 06:29:22 -0800598 int64_t timestamp_us,
hbos9e302742017-01-20 02:47:10 -0800599 const TrackMediaInfoMap& track_media_info_map,
hbos09bc1282016-11-08 06:29:22 -0800600 rtc::scoped_refptr<StreamCollectionInterface> streams,
601 bool is_local,
602 RTCStatsReport* report) {
hbos09bc1282016-11-08 06:29:22 -0800603 if (!streams)
604 return;
Harald Alvestrand89061872018-01-02 14:08:34 +0100605 // Collect info about streams and which tracks in them are attached to PC.
hbos09bc1282016-11-08 06:29:22 -0800606 for (size_t i = 0; i < streams->count(); ++i) {
607 MediaStreamInterface* stream = streams->at(i);
608
609 std::unique_ptr<RTCMediaStreamStats> stream_stats(
hbos02d2a922016-12-21 01:29:05 -0800610 new RTCMediaStreamStats(
611 (is_local ? "RTCMediaStream_local_" : "RTCMediaStream_remote_") +
612 stream->label(), timestamp_us));
hbos09bc1282016-11-08 06:29:22 -0800613 stream_stats->stream_identifier = stream->label();
614 stream_stats->track_ids = std::vector<std::string>();
Harald Alvestrand89061872018-01-02 14:08:34 +0100615 // Record the IDs of tracks that are currently connected.
616 // Note: Iterating over streams may be iffy with AddTrack.
617 // TODO(hta): Revisit in conjunction with https://bugs.webrtc.org/8674
hbos9e302742017-01-20 02:47:10 -0800618 if (is_local) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100619 for (auto audio_track : stream->GetAudioTracks()) {
Harald Alvestrandc72af932018-01-11 17:18:19 +0100620 stream_stats->track_ids->push_back(
621 RTCMediaStreamTrackStatsIDFromTrackKindIDAndAttachment(
622 is_local, MediaStreamTrackInterface::kAudioKind,
623 audio_track->id(),
624 track_media_info_map.GetAttachmentIdByTrack(audio_track)
625 .value()));
hbos09bc1282016-11-08 06:29:22 -0800626 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100627 for (auto video_track : stream->GetVideoTracks()) {
Harald Alvestrandc72af932018-01-11 17:18:19 +0100628 stream_stats->track_ids->push_back(
629 RTCMediaStreamTrackStatsIDFromTrackKindIDAndAttachment(
630 is_local, MediaStreamTrackInterface::kVideoKind,
631 video_track->id(),
632 track_media_info_map.GetAttachmentIdByTrack(video_track)
633 .value()));
hbos09bc1282016-11-08 06:29:22 -0800634 }
hbos9e302742017-01-20 02:47:10 -0800635 } else {
Harald Alvestrand89061872018-01-02 14:08:34 +0100636 for (auto audio_track : stream->GetAudioTracks()) {
Harald Alvestrandc72af932018-01-11 17:18:19 +0100637 stream_stats->track_ids->push_back(
638 RTCMediaStreamTrackStatsIDFromTrackKindIDAndAttachment(
639 is_local, MediaStreamTrackInterface::kAudioKind,
640 audio_track->id(),
641 track_media_info_map.GetAttachmentIdByTrack(audio_track)
642 .value()));
hbos9e302742017-01-20 02:47:10 -0800643 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100644 for (auto video_track : stream->GetVideoTracks()) {
Harald Alvestrandc72af932018-01-11 17:18:19 +0100645 stream_stats->track_ids->push_back(
646 RTCMediaStreamTrackStatsIDFromTrackKindIDAndAttachment(
647 is_local, MediaStreamTrackInterface::kVideoKind,
648 video_track->id(),
649 track_media_info_map.GetAttachmentIdByTrack(video_track)
650 .value()));
hbos9e302742017-01-20 02:47:10 -0800651 }
hbos09bc1282016-11-08 06:29:22 -0800652 }
653 report->AddStats(std::move(stream_stats));
654 }
655}
656
hboscc555c52016-10-18 12:48:31 -0700657} // namespace
658
hbosc82f2e12016-09-05 01:36:50 -0700659rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
660 PeerConnection* pc, int64_t cache_lifetime_us) {
661 return rtc::scoped_refptr<RTCStatsCollector>(
662 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
663}
664
665RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
666 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700667 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700668 signaling_thread_(pc->signaling_thread()),
669 worker_thread_(pc->worker_thread()),
670 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700671 num_pending_partial_reports_(0),
672 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700673 cache_timestamp_us_(0),
674 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700675 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700676 RTC_DCHECK(signaling_thread_);
677 RTC_DCHECK(worker_thread_);
678 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700679 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbos82ebe022016-11-14 01:41:09 -0800680 pc_->SignalDataChannelCreated.connect(
681 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700682}
683
hbosb78306a2016-12-19 05:06:57 -0800684RTCStatsCollector::~RTCStatsCollector() {
685 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
686}
687
hbosc82f2e12016-09-05 01:36:50 -0700688void RTCStatsCollector::GetStatsReport(
689 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
690 RTC_DCHECK(signaling_thread_->IsCurrent());
691 RTC_DCHECK(callback);
692 callbacks_.push_back(callback);
693
hbos0e6758d2016-08-31 07:57:36 -0700694 // "Now" using a monotonically increasing timer.
695 int64_t cache_now_us = rtc::TimeMicros();
696 if (cached_report_ &&
697 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700698 // We have a fresh cached report to deliver.
699 DeliverCachedReport();
700 } else if (!num_pending_partial_reports_) {
701 // Only start gathering stats if we're not already gathering stats. In the
702 // case of already gathering stats, |callback_| will be invoked when there
703 // are no more pending partial reports.
704
705 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
706 // UTC), in microseconds. The system clock could be modified and is not
707 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700708 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700709
hbosf415f8a2017-01-02 04:28:51 -0800710 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700711 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800712
hbos84abeb12017-01-16 06:16:44 -0800713 // Prepare |channel_name_pairs_| for use in
hbosdf6075a2016-12-19 04:58:02 -0800714 // |ProducePartialResultsOnNetworkThread|.
715 channel_name_pairs_.reset(new ChannelNamePairs());
Steve Anton978b8762017-09-29 12:15:02 -0700716 if (pc_->voice_channel()) {
Oskar Sundbom89e71262017-11-16 10:56:31 +0100717 channel_name_pairs_->voice =
Steve Anton978b8762017-09-29 12:15:02 -0700718 ChannelNamePair(pc_->voice_channel()->content_name(),
Oskar Sundbom89e71262017-11-16 10:56:31 +0100719 pc_->voice_channel()->transport_name());
hbosdf6075a2016-12-19 04:58:02 -0800720 }
Steve Anton978b8762017-09-29 12:15:02 -0700721 if (pc_->video_channel()) {
Oskar Sundbom89e71262017-11-16 10:56:31 +0100722 channel_name_pairs_->video =
Steve Anton978b8762017-09-29 12:15:02 -0700723 ChannelNamePair(pc_->video_channel()->content_name(),
Oskar Sundbom89e71262017-11-16 10:56:31 +0100724 pc_->video_channel()->transport_name());
hbosdf6075a2016-12-19 04:58:02 -0800725 }
Steve Anton978b8762017-09-29 12:15:02 -0700726 if (pc_->rtp_data_channel()) {
Oskar Sundbom89e71262017-11-16 10:56:31 +0100727 channel_name_pairs_->data =
Steve Anton978b8762017-09-29 12:15:02 -0700728 ChannelNamePair(pc_->rtp_data_channel()->content_name(),
Oskar Sundbom89e71262017-11-16 10:56:31 +0100729 pc_->rtp_data_channel()->transport_name());
Steve Anton978b8762017-09-29 12:15:02 -0700730 }
731 if (pc_->sctp_content_name()) {
Oskar Sundbom89e71262017-11-16 10:56:31 +0100732 channel_name_pairs_->data = ChannelNamePair(*pc_->sctp_content_name(),
733 *pc_->sctp_transport_name());
hbosdf6075a2016-12-19 04:58:02 -0800734 }
hbos84abeb12017-01-16 06:16:44 -0800735 // Prepare |track_media_info_map_| for use in
736 // |ProducePartialResultsOnNetworkThread| and
737 // |ProducePartialResultsOnSignalingThread|.
738 track_media_info_map_.reset(PrepareTrackMediaInfoMap_s().release());
739 // Prepare |track_to_id_| for use in |ProducePartialResultsOnNetworkThread|.
740 // This avoids a possible deadlock if |MediaStreamTrackInterface::id| is
741 // implemented to invoke on the signaling thread.
742 track_to_id_ = PrepareTrackToID_s();
hbosf415f8a2017-01-02 04:28:51 -0800743
stefanf79ade12017-06-02 06:44:03 -0700744 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
745 // thread.
746 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
747 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700748 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700749
750 invoker_.AsyncInvoke<void>(
751 RTC_FROM_HERE, network_thread_,
hbosc82f2e12016-09-05 01:36:50 -0700752 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
stefanf79ade12017-06-02 06:44:03 -0700753 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800754 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700755 }
hbosd565b732016-08-30 14:04:35 -0700756}
757
758void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700759 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700760 cached_report_ = nullptr;
761}
762
hbosb78306a2016-12-19 05:06:57 -0800763void RTCStatsCollector::WaitForPendingRequest() {
764 RTC_DCHECK(signaling_thread_->IsCurrent());
765 if (num_pending_partial_reports_) {
766 rtc::Thread::Current()->ProcessMessages(0);
767 while (num_pending_partial_reports_) {
768 rtc::Thread::Current()->SleepMs(1);
769 rtc::Thread::Current()->ProcessMessages(0);
770 }
771 }
772}
773
hbosc82f2e12016-09-05 01:36:50 -0700774void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
775 int64_t timestamp_us) {
776 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700777 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
778 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700779
hboscc555c52016-10-18 12:48:31 -0700780 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos09bc1282016-11-08 06:29:22 -0800781 ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700782 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700783
784 AddPartialResults(report);
785}
786
hbosc82f2e12016-09-05 01:36:50 -0700787void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
788 int64_t timestamp_us) {
789 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700790 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
791 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700792
hbosdf6075a2016-12-19 04:58:02 -0800793 std::unique_ptr<SessionStats> session_stats =
Steve Anton978b8762017-09-29 12:15:02 -0700794 pc_->GetSessionStats(*channel_name_pairs_);
hbosdf6075a2016-12-19 04:58:02 -0800795 if (session_stats) {
796 std::map<std::string, CertificateStatsPair> transport_cert_stats =
797 PrepareTransportCertificateStats_n(*session_stats);
798
799 ProduceCertificateStats_n(
800 timestamp_us, transport_cert_stats, report.get());
801 ProduceCodecStats_n(
hbos84abeb12017-01-16 06:16:44 -0800802 timestamp_us, *track_media_info_map_, report.get());
stefanf79ade12017-06-02 06:44:03 -0700803 ProduceIceCandidateAndPairStats_n(timestamp_us, *session_stats,
804 track_media_info_map_->video_media_info(),
805 call_stats_, report.get());
Steve Anton593e3252017-12-15 11:44:48 -0800806 ProduceRTPStreamStats_n(timestamp_us, *session_stats, *channel_name_pairs_,
807 *track_media_info_map_, report.get());
hbosdf6075a2016-12-19 04:58:02 -0800808 ProduceTransportStats_n(
809 timestamp_us, *session_stats, transport_cert_stats, report.get());
810 }
hbosc82f2e12016-09-05 01:36:50 -0700811
812 AddPartialResults(report);
813}
814
815void RTCStatsCollector::AddPartialResults(
816 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
817 if (!signaling_thread_->IsCurrent()) {
818 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
819 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
820 rtc::scoped_refptr<RTCStatsCollector>(this),
821 partial_report));
822 return;
823 }
824 AddPartialResults_s(partial_report);
825}
826
827void RTCStatsCollector::AddPartialResults_s(
828 rtc::scoped_refptr<RTCStatsReport> partial_report) {
829 RTC_DCHECK(signaling_thread_->IsCurrent());
830 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
831 if (!partial_report_)
832 partial_report_ = partial_report;
833 else
834 partial_report_->TakeMembersFrom(partial_report);
835 --num_pending_partial_reports_;
836 if (!num_pending_partial_reports_) {
837 cache_timestamp_us_ = partial_report_timestamp_us_;
838 cached_report_ = partial_report_;
839 partial_report_ = nullptr;
hbos84abeb12017-01-16 06:16:44 -0800840 channel_name_pairs_.reset();
841 track_media_info_map_.reset();
842 track_to_id_.clear();
ehmaldonadoa26196b2017-07-18 03:30:29 -0700843 // Trace WebRTC Stats when getStats is called on Javascript.
844 // This allows access to WebRTC stats from trace logs. To enable them,
845 // select the "webrtc_stats" category when recording traces.
ehmaldonado8ab0fd82017-09-04 14:35:04 -0700846 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
847 cached_report_->ToJson());
hbosc82f2e12016-09-05 01:36:50 -0700848 DeliverCachedReport();
849 }
850}
851
852void RTCStatsCollector::DeliverCachedReport() {
853 RTC_DCHECK(signaling_thread_->IsCurrent());
854 RTC_DCHECK(!callbacks_.empty());
855 RTC_DCHECK(cached_report_);
856 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
857 callbacks_) {
858 callback->OnStatsDelivered(cached_report_);
859 }
860 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700861}
862
hbosdf6075a2016-12-19 04:58:02 -0800863void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700864 int64_t timestamp_us,
865 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700866 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800867 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700868 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
869 if (transport_cert_stats_pair.second.local) {
870 ProduceCertificateStatsFromSSLCertificateStats(
871 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700872 }
hbos02ba2112016-10-28 05:14:53 -0700873 if (transport_cert_stats_pair.second.remote) {
874 ProduceCertificateStatsFromSSLCertificateStats(
875 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700876 }
877 }
878}
879
hbosdf6075a2016-12-19 04:58:02 -0800880void RTCStatsCollector::ProduceCodecStats_n(
hbos84abeb12017-01-16 06:16:44 -0800881 int64_t timestamp_us, const TrackMediaInfoMap& track_media_info_map,
hbos0adb8282016-11-23 02:32:06 -0800882 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800883 RTC_DCHECK(network_thread_->IsCurrent());
hbos0adb8282016-11-23 02:32:06 -0800884 // Audio
hbos84abeb12017-01-16 06:16:44 -0800885 if (track_media_info_map.voice_media_info()) {
hbos0adb8282016-11-23 02:32:06 -0800886 // Inbound
hbos84abeb12017-01-16 06:16:44 -0800887 for (const auto& pair :
888 track_media_info_map.voice_media_info()->receive_codecs) {
hbos0adb8282016-11-23 02:32:06 -0800889 report->AddStats(CodecStatsFromRtpCodecParameters(
890 timestamp_us, true, true, pair.second));
891 }
892 // Outbound
hbos84abeb12017-01-16 06:16:44 -0800893 for (const auto& pair :
894 track_media_info_map.voice_media_info()->send_codecs) {
hbos0adb8282016-11-23 02:32:06 -0800895 report->AddStats(CodecStatsFromRtpCodecParameters(
896 timestamp_us, false, true, pair.second));
897 }
898 }
899 // Video
hbos84abeb12017-01-16 06:16:44 -0800900 if (track_media_info_map.video_media_info()) {
hbos0adb8282016-11-23 02:32:06 -0800901 // Inbound
hbos84abeb12017-01-16 06:16:44 -0800902 for (const auto& pair :
903 track_media_info_map.video_media_info()->receive_codecs) {
hbos0adb8282016-11-23 02:32:06 -0800904 report->AddStats(CodecStatsFromRtpCodecParameters(
905 timestamp_us, true, false, pair.second));
906 }
907 // Outbound
hbos84abeb12017-01-16 06:16:44 -0800908 for (const auto& pair :
909 track_media_info_map.video_media_info()->send_codecs) {
hbos0adb8282016-11-23 02:32:06 -0800910 report->AddStats(CodecStatsFromRtpCodecParameters(
911 timestamp_us, false, false, pair.second));
912 }
913 }
914}
915
hboscc555c52016-10-18 12:48:31 -0700916void RTCStatsCollector::ProduceDataChannelStats_s(
917 int64_t timestamp_us, RTCStatsReport* report) const {
918 RTC_DCHECK(signaling_thread_->IsCurrent());
919 for (const rtc::scoped_refptr<DataChannel>& data_channel :
920 pc_->sctp_data_channels()) {
921 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
922 new RTCDataChannelStats(
923 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
924 timestamp_us));
925 data_channel_stats->label = data_channel->label();
926 data_channel_stats->protocol = data_channel->protocol();
927 data_channel_stats->datachannelid = data_channel->id();
928 data_channel_stats->state =
929 DataStateToRTCDataChannelState(data_channel->state());
930 data_channel_stats->messages_sent = data_channel->messages_sent();
931 data_channel_stats->bytes_sent = data_channel->bytes_sent();
932 data_channel_stats->messages_received = data_channel->messages_received();
933 data_channel_stats->bytes_received = data_channel->bytes_received();
934 report->AddStats(std::move(data_channel_stats));
935 }
936}
937
hbosdf6075a2016-12-19 04:58:02 -0800938void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -0700939 int64_t timestamp_us,
940 const SessionStats& session_stats,
941 const cricket::VideoMediaInfo* video_media_info,
942 const Call::Stats& call_stats,
943 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800944 RTC_DCHECK(network_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700945 for (const auto& transport_stats : session_stats.transport_stats) {
946 for (const auto& channel_stats : transport_stats.second.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800947 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
948 transport_stats.second.transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700949 for (const cricket::ConnectionInfo& info :
950 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700951 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700952 new RTCIceCandidatePairStats(
953 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
954 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700955
hbos0583b282016-11-30 01:50:14 -0800956 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700957 // TODO(hbos): There could be other candidates that are not paired with
958 // anything. We don't have a complete list. Local candidates come from
959 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +0100960 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700961 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800962 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -0700963 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800964 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -0800965 candidate_pair_stats->state =
966 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
967 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -0800968 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -0700969 // TODO(hbos): This writable is different than the spec. It goes to
970 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +0100971 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -0700972 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700973 candidate_pair_stats->bytes_sent =
974 static_cast<uint64_t>(info.sent_total_bytes);
975 candidate_pair_stats->bytes_received =
976 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -0800977 candidate_pair_stats->total_round_trip_time =
978 static_cast<double>(info.total_round_trip_time_ms) /
979 rtc::kNumMillisecsPerSec;
980 if (info.current_round_trip_time_ms) {
981 candidate_pair_stats->current_round_trip_time =
982 static_cast<double>(*info.current_round_trip_time_ms) /
983 rtc::kNumMillisecsPerSec;
984 }
stefanf79ade12017-06-02 06:44:03 -0700985 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -0800986 // The bandwidth estimations we have are for the selected candidate
987 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -0700988 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
989 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
990 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -0800991 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -0700992 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -0800993 }
stefanf79ade12017-06-02 06:44:03 -0700994 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -0800995 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -0700996 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -0800997 }
998 }
hbosd82f5122016-12-09 04:12:39 -0800999 candidate_pair_stats->requests_received =
1000 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001001 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1002 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001003 candidate_pair_stats->responses_received =
1004 static_cast<uint64_t>(info.recv_ping_responses);
1005 candidate_pair_stats->responses_sent =
1006 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001007 RTC_DCHECK_GE(info.sent_ping_requests_total,
1008 info.sent_ping_requests_before_first_response);
1009 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1010 info.sent_ping_requests_total -
1011 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001012
1013 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001014 }
1015 }
1016 }
1017}
1018
hbos09bc1282016-11-08 06:29:22 -08001019void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s(
1020 int64_t timestamp_us, RTCStatsReport* report) const {
1021 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos9e302742017-01-20 02:47:10 -08001022 RTC_DCHECK(track_media_info_map_);
Harald Alvestrand89061872018-01-02 14:08:34 +01001023 // TODO(bugs.webrtc.org/8674): Use the stream list updated by AddTrack
1024 ProduceMediaStreamStats(timestamp_us, *track_media_info_map_,
1025 pc_->local_streams(), true, report);
1026 ProduceMediaStreamStats(timestamp_us, *track_media_info_map_,
1027 pc_->remote_streams(), false, report);
1028 ProduceSenderMediaTrackStats(timestamp_us, *track_media_info_map_,
1029 pc_->GetSenders(), report);
1030 ProduceReceiverMediaTrackStats(timestamp_us, *track_media_info_map_,
1031 pc_->GetReceivers(), report);
hbos09bc1282016-11-08 06:29:22 -08001032}
1033
hbos6ab97ce2016-10-03 14:16:56 -07001034void RTCStatsCollector::ProducePeerConnectionStats_s(
1035 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001036 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001037 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001038 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001039 stats->data_channels_opened = internal_record_.data_channels_opened;
1040 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001041 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001042}
1043
hbosdf6075a2016-12-19 04:58:02 -08001044void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001045 int64_t timestamp_us,
1046 const SessionStats& session_stats,
1047 const ChannelNamePairs& channel_name_pairs,
hbos84abeb12017-01-16 06:16:44 -08001048 const TrackMediaInfoMap& track_media_info_map,
1049 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001050 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001051
1052 // Audio
hbos84abeb12017-01-16 06:16:44 -08001053 if (track_media_info_map.voice_media_info()) {
Steve Anton593e3252017-12-15 11:44:48 -08001054 RTC_DCHECK(channel_name_pairs.voice);
1055 const std::string& transport_name =
1056 (*channel_name_pairs.voice).transport_name;
1057 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1058 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
hbos0adb8282016-11-23 02:32:06 -08001059 // Inbound
1060 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
hbos84abeb12017-01-16 06:16:44 -08001061 track_media_info_map.voice_media_info()->receivers) {
hbos0adb8282016-11-23 02:32:06 -08001062 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
1063 // is fixed.
1064 if (voice_receiver_info.ssrc() == 0)
1065 continue;
1066 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
Harald Alvestrandc72af932018-01-11 17:18:19 +01001067 new RTCInboundRTPStreamStats(RTCInboundRTPStreamStatsIDFromSSRC(
1068 true, voice_receiver_info.ssrc()),
1069 timestamp_us));
hbos0adb8282016-11-23 02:32:06 -08001070 SetInboundRTPStreamStatsFromVoiceReceiverInfo(
1071 voice_receiver_info, inbound_audio.get());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001072 // TODO(hta): This lookup should look for the sender, not the track.
hbos84abeb12017-01-16 06:16:44 -08001073 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1074 track_media_info_map_->GetAudioTrack(voice_receiver_info);
1075 if (audio_track) {
1076 RTC_DCHECK(track_to_id_.find(audio_track.get()) != track_to_id_.end());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001077 inbound_audio
1078 ->track_id = RTCMediaStreamTrackStatsIDFromTrackKindIDAndAttachment(
1079 false, MediaStreamTrackInterface::kAudioKind,
1080 track_to_id_.find(audio_track.get())->second,
1081 track_media_info_map_->GetAttachmentIdByTrack(audio_track).value());
hbos84abeb12017-01-16 06:16:44 -08001082 }
hbos0adb8282016-11-23 02:32:06 -08001083 inbound_audio->transport_id = transport_id;
hbos0adb8282016-11-23 02:32:06 -08001084 report->AddStats(std::move(inbound_audio));
1085 }
1086 // Outbound
1087 for (const cricket::VoiceSenderInfo& voice_sender_info :
hbos84abeb12017-01-16 06:16:44 -08001088 track_media_info_map.voice_media_info()->senders) {
hbos0adb8282016-11-23 02:32:06 -08001089 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
1090 // is fixed.
1091 if (voice_sender_info.ssrc() == 0)
1092 continue;
1093 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
1094 new RTCOutboundRTPStreamStats(
1095 RTCOutboundRTPStreamStatsIDFromSSRC(
1096 true, voice_sender_info.ssrc()),
1097 timestamp_us));
1098 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
1099 voice_sender_info, outbound_audio.get());
hbos84abeb12017-01-16 06:16:44 -08001100 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1101 track_media_info_map_->GetAudioTrack(voice_sender_info);
1102 if (audio_track) {
1103 RTC_DCHECK(track_to_id_.find(audio_track.get()) != track_to_id_.end());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001104 outbound_audio
1105 ->track_id = RTCMediaStreamTrackStatsIDFromTrackKindIDAndAttachment(
1106 true, MediaStreamTrackInterface::kAudioKind,
1107 track_to_id_.find(audio_track.get())->second,
1108 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos84abeb12017-01-16 06:16:44 -08001109 }
hbos0adb8282016-11-23 02:32:06 -08001110 outbound_audio->transport_id = transport_id;
hbos0adb8282016-11-23 02:32:06 -08001111 report->AddStats(std::move(outbound_audio));
hbos6ded1902016-11-01 01:50:46 -07001112 }
1113 }
1114 // Video
hbos84abeb12017-01-16 06:16:44 -08001115 if (track_media_info_map.video_media_info()) {
Steve Anton593e3252017-12-15 11:44:48 -08001116 RTC_DCHECK(channel_name_pairs.video);
1117 const std::string& transport_name =
1118 (*channel_name_pairs.video).transport_name;
1119 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1120 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
hbos0adb8282016-11-23 02:32:06 -08001121 // Inbound
1122 for (const cricket::VideoReceiverInfo& video_receiver_info :
hbos84abeb12017-01-16 06:16:44 -08001123 track_media_info_map.video_media_info()->receivers) {
hbos0adb8282016-11-23 02:32:06 -08001124 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
1125 // is fixed.
1126 if (video_receiver_info.ssrc() == 0)
1127 continue;
1128 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
1129 new RTCInboundRTPStreamStats(
1130 RTCInboundRTPStreamStatsIDFromSSRC(
1131 false, video_receiver_info.ssrc()),
1132 timestamp_us));
1133 SetInboundRTPStreamStatsFromVideoReceiverInfo(
1134 video_receiver_info, inbound_video.get());
hbos84abeb12017-01-16 06:16:44 -08001135 rtc::scoped_refptr<VideoTrackInterface> video_track =
1136 track_media_info_map_->GetVideoTrack(video_receiver_info);
1137 if (video_track) {
1138 RTC_DCHECK(track_to_id_.find(video_track.get()) != track_to_id_.end());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001139 inbound_video
1140 ->track_id = RTCMediaStreamTrackStatsIDFromTrackKindIDAndAttachment(
1141 false, MediaStreamTrackInterface::kVideoKind,
1142 track_to_id_.find(video_track.get())->second,
1143 track_media_info_map_->GetAttachmentIdByTrack(video_track).value());
hbos84abeb12017-01-16 06:16:44 -08001144 }
hbos0adb8282016-11-23 02:32:06 -08001145 inbound_video->transport_id = transport_id;
hbos0adb8282016-11-23 02:32:06 -08001146 report->AddStats(std::move(inbound_video));
1147 }
1148 // Outbound
1149 for (const cricket::VideoSenderInfo& video_sender_info :
hbos84abeb12017-01-16 06:16:44 -08001150 track_media_info_map.video_media_info()->senders) {
hbos0adb8282016-11-23 02:32:06 -08001151 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
1152 // is fixed.
1153 if (video_sender_info.ssrc() == 0)
1154 continue;
1155 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
Harald Alvestrandc72af932018-01-11 17:18:19 +01001156 new RTCOutboundRTPStreamStats(RTCOutboundRTPStreamStatsIDFromSSRC(
1157 false, video_sender_info.ssrc()),
1158 timestamp_us));
hbos0adb8282016-11-23 02:32:06 -08001159 SetOutboundRTPStreamStatsFromVideoSenderInfo(
1160 video_sender_info, outbound_video.get());
hbos84abeb12017-01-16 06:16:44 -08001161 rtc::scoped_refptr<VideoTrackInterface> video_track =
1162 track_media_info_map_->GetVideoTrack(video_sender_info);
1163 if (video_track) {
1164 RTC_DCHECK(track_to_id_.find(video_track.get()) != track_to_id_.end());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001165 outbound_video
1166 ->track_id = RTCMediaStreamTrackStatsIDFromTrackKindIDAndAttachment(
1167 true, MediaStreamTrackInterface::kVideoKind,
1168 track_to_id_.find(video_track.get())->second,
1169 track_media_info_map_->GetAttachmentIdByTrack(video_track).value());
hbos84abeb12017-01-16 06:16:44 -08001170 }
hbos0adb8282016-11-23 02:32:06 -08001171 outbound_video->transport_id = transport_id;
hbos0adb8282016-11-23 02:32:06 -08001172 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001173 }
1174 }
1175}
1176
hbosdf6075a2016-12-19 04:58:02 -08001177void RTCStatsCollector::ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001178 int64_t timestamp_us, const SessionStats& session_stats,
1179 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1180 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001181 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001182 for (const auto& transport : session_stats.transport_stats) {
1183 // Get reference to RTCP channel, if it exists.
1184 std::string rtcp_transport_stats_id;
1185 for (const auto& channel_stats : transport.second.channel_stats) {
1186 if (channel_stats.component ==
1187 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1188 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
1189 transport.second.transport_name, channel_stats.component);
1190 break;
1191 }
1192 }
1193
1194 // Get reference to local and remote certificates of this transport, if they
1195 // exist.
1196 const auto& certificate_stats_it = transport_cert_stats.find(
1197 transport.second.transport_name);
1198 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1199 std::string local_certificate_id;
1200 if (certificate_stats_it->second.local) {
1201 local_certificate_id = RTCCertificateIDFromFingerprint(
1202 certificate_stats_it->second.local->fingerprint);
1203 }
1204 std::string remote_certificate_id;
1205 if (certificate_stats_it->second.remote) {
1206 remote_certificate_id = RTCCertificateIDFromFingerprint(
1207 certificate_stats_it->second.remote->fingerprint);
1208 }
1209
1210 // There is one transport stats for each channel.
1211 for (const auto& channel_stats : transport.second.channel_stats) {
1212 std::unique_ptr<RTCTransportStats> transport_stats(
1213 new RTCTransportStats(
1214 RTCTransportStatsIDFromTransportChannel(
1215 transport.second.transport_name, channel_stats.component),
1216 timestamp_us));
1217 transport_stats->bytes_sent = 0;
1218 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001219 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1220 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001221 for (const cricket::ConnectionInfo& info :
1222 channel_stats.connection_infos) {
1223 *transport_stats->bytes_sent += info.sent_total_bytes;
1224 *transport_stats->bytes_received += info.recv_total_bytes;
1225 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001226 transport_stats->selected_candidate_pair_id =
1227 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1228 }
1229 }
1230 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1231 !rtcp_transport_stats_id.empty()) {
1232 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1233 }
1234 if (!local_certificate_id.empty())
1235 transport_stats->local_certificate_id = local_certificate_id;
1236 if (!remote_certificate_id.empty())
1237 transport_stats->remote_certificate_id = remote_certificate_id;
1238 report->AddStats(std::move(transport_stats));
1239 }
1240 }
1241}
1242
1243std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001244RTCStatsCollector::PrepareTransportCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001245 const SessionStats& session_stats) const {
hbosdf6075a2016-12-19 04:58:02 -08001246 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001247 std::map<std::string, CertificateStatsPair> transport_cert_stats;
1248 for (const auto& transport_stats : session_stats.transport_stats) {
1249 CertificateStatsPair certificate_stats_pair;
1250 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton978b8762017-09-29 12:15:02 -07001251 if (pc_->GetLocalCertificate(transport_stats.second.transport_name,
1252 &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001253 certificate_stats_pair.local =
1254 local_certificate->ssl_certificate().GetStats();
1255 }
1256 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
Steve Anton978b8762017-09-29 12:15:02 -07001257 pc_->GetRemoteSSLCertificate(transport_stats.second.transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001258 if (remote_certificate) {
1259 certificate_stats_pair.remote = remote_certificate->GetStats();
1260 }
1261 transport_cert_stats.insert(
1262 std::make_pair(transport_stats.second.transport_name,
1263 std::move(certificate_stats_pair)));
1264 }
1265 return transport_cert_stats;
1266}
1267
hbos84abeb12017-01-16 06:16:44 -08001268std::unique_ptr<TrackMediaInfoMap>
1269RTCStatsCollector::PrepareTrackMediaInfoMap_s() const {
hbosdf6075a2016-12-19 04:58:02 -08001270 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos84abeb12017-01-16 06:16:44 -08001271 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
Steve Anton978b8762017-09-29 12:15:02 -07001272 if (pc_->voice_channel()) {
hbos84abeb12017-01-16 06:16:44 -08001273 voice_media_info.reset(new cricket::VoiceMediaInfo());
Steve Anton978b8762017-09-29 12:15:02 -07001274 if (!pc_->voice_channel()->GetStats(voice_media_info.get())) {
hbos84abeb12017-01-16 06:16:44 -08001275 voice_media_info.reset();
hbos0adb8282016-11-23 02:32:06 -08001276 }
1277 }
hbos84abeb12017-01-16 06:16:44 -08001278 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
Steve Anton978b8762017-09-29 12:15:02 -07001279 if (pc_->video_channel()) {
hbos84abeb12017-01-16 06:16:44 -08001280 video_media_info.reset(new cricket::VideoMediaInfo());
Steve Anton978b8762017-09-29 12:15:02 -07001281 if (!pc_->video_channel()->GetStats(video_media_info.get())) {
hbos84abeb12017-01-16 06:16:44 -08001282 video_media_info.reset();
hbos0adb8282016-11-23 02:32:06 -08001283 }
1284 }
hbos84abeb12017-01-16 06:16:44 -08001285 std::unique_ptr<TrackMediaInfoMap> track_media_info_map(
1286 new TrackMediaInfoMap(std::move(voice_media_info),
1287 std::move(video_media_info),
1288 pc_->GetSenders(),
1289 pc_->GetReceivers()));
1290 return track_media_info_map;
1291}
1292
1293std::map<MediaStreamTrackInterface*, std::string>
1294RTCStatsCollector::PrepareTrackToID_s() const {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001295 // TODO(hta): Remove this method, and vector its callers via
1296 // senders / receivers instead.
1297 // It ignores tracks that are multiply connected to the PC.
hbos84abeb12017-01-16 06:16:44 -08001298 RTC_DCHECK(signaling_thread_->IsCurrent());
1299 std::map<MediaStreamTrackInterface*, std::string> track_to_id;
hbos7b0c6fa2017-07-12 16:22:34 -07001300 for (auto sender : pc_->GetSenders()) {
1301 auto track = sender->track();
1302 if (track)
1303 track_to_id[track.get()] = track->id();
1304 }
1305 for (auto receiver : pc_->GetReceivers()) {
1306 auto track = receiver->track();
1307 if (track)
1308 track_to_id[track.get()] = track->id();
hbos84abeb12017-01-16 06:16:44 -08001309 }
1310 return track_to_id;
hbos0adb8282016-11-23 02:32:06 -08001311}
1312
hbos82ebe022016-11-14 01:41:09 -08001313void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1314 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1315 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1316}
1317
1318void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1319 RTC_DCHECK(signaling_thread_->IsCurrent());
1320 bool result = internal_record_.opened_data_channels.insert(
1321 reinterpret_cast<uintptr_t>(channel)).second;
1322 ++internal_record_.data_channels_opened;
1323 RTC_DCHECK(result);
1324}
1325
1326void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1327 RTC_DCHECK(signaling_thread_->IsCurrent());
1328 // Only channels that have been fully opened (and have increased the
1329 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001330 if (internal_record_.opened_data_channels.erase(
1331 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001332 ++internal_record_.data_channels_closed;
1333 }
1334}
1335
hboscc555c52016-10-18 12:48:31 -07001336const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1337 const std::string& type) {
1338 return CandidateTypeToRTCIceCandidateType(type);
1339}
1340
1341const char* DataStateToRTCDataChannelStateForTesting(
1342 DataChannelInterface::DataState state) {
1343 return DataStateToRTCDataChannelState(state);
1344}
1345
hbosd565b732016-08-30 14:04:35 -07001346} // namespace webrtc