blob: 4e5e8d9739562bd862052265d85642ef685bac74 [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
hbos74e1a4f2016-09-15 23:33:01 -070011#include "webrtc/api/rtcstatscollector.h"
hbosd565b732016-08-30 14:04:35 -070012
13#include <memory>
14#include <utility>
15#include <vector>
16
17#include "webrtc/api/peerconnection.h"
hbos09bc1282016-11-08 06:29:22 -080018#include "webrtc/api/peerconnectioninterface.h"
19#include "webrtc/api/mediastreaminterface.h"
hbos6ab97ce2016-10-03 14:16:56 -070020#include "webrtc/api/webrtcsession.h"
hbosd565b732016-08-30 14:04:35 -070021#include "webrtc/base/checks.h"
hbos6ded1902016-11-01 01:50:46 -070022#include "webrtc/base/timeutils.h"
23#include "webrtc/media/base/mediachannel.h"
hbosab9f6e42016-10-07 02:18:47 -070024#include "webrtc/p2p/base/candidate.h"
hbos2fa7c672016-10-24 04:00:05 -070025#include "webrtc/p2p/base/p2pconstants.h"
hbosab9f6e42016-10-07 02:18:47 -070026#include "webrtc/p2p/base/port.h"
hbosd565b732016-08-30 14:04:35 -070027
28namespace webrtc {
29
hboscc555c52016-10-18 12:48:31 -070030namespace {
31
hbos2fa7c672016-10-24 04:00:05 -070032std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
33 return "RTCCertificate_" + fingerprint;
34}
35
hbos0adb8282016-11-23 02:32:06 -080036std::string RTCCodecStatsIDFromDirectionMediaAndPayload(
37 bool inbound, bool audio, uint32_t payload_type) {
38 // TODO(hbos): When we are able to handle multiple m= lines of the same media
39 // type (and multiple BaseChannels for the same type is possible?) this needs
40 // to be updated to differentiate the transport being used, and stats need to
41 // be collected for all of them. crbug.com/659117
42 if (inbound) {
43 return audio ? "RTCCodec_InboundAudio_" + rtc::ToString<>(payload_type)
44 : "RTCCodec_InboundVideo_" + rtc::ToString<>(payload_type);
45 }
46 return audio ? "RTCCodec_OutboundAudio_" + rtc::ToString<>(payload_type)
47 : "RTCCodec_OutboundVideo_" + rtc::ToString<>(payload_type);
48}
49
hbos2fa7c672016-10-24 04:00:05 -070050std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
51 const cricket::ConnectionInfo& info) {
52 return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" +
53 info.remote_candidate.id();
54}
55
hbos09bc1282016-11-08 06:29:22 -080056std::string RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
57 const MediaStreamTrackInterface& track) {
58 return "RTCMediaStreamTrack_" + track.id();
59}
60
hbos2fa7c672016-10-24 04:00:05 -070061std::string RTCTransportStatsIDFromTransportChannel(
62 const std::string& transport_name, int channel_component) {
63 return "RTCTransport_" + transport_name + "_" +
64 rtc::ToString<>(channel_component);
65}
66
hbos6ded1902016-11-01 01:50:46 -070067std::string RTCTransportStatsIDFromBaseChannel(
68 const ProxyTransportMap& proxy_to_transport,
69 const cricket::BaseChannel& base_channel) {
70 auto proxy_it = proxy_to_transport.find(base_channel.content_name());
71 if (proxy_it == proxy_to_transport.cend())
72 return "";
73 return RTCTransportStatsIDFromTransportChannel(
74 proxy_it->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
75}
76
hboseeafe942016-11-01 03:00:17 -070077std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
78 return audio ? "RTCInboundRTPAudioStream_" + rtc::ToString<>(ssrc)
79 : "RTCInboundRTPVideoStream_" + rtc::ToString<>(ssrc);
80}
81
hbos6ded1902016-11-01 01:50:46 -070082std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
83 return audio ? "RTCOutboundRTPAudioStream_" + rtc::ToString<>(ssrc)
84 : "RTCOutboundRTPVideoStream_" + rtc::ToString<>(ssrc);
85}
86
hbosab9f6e42016-10-07 02:18:47 -070087const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
88 if (type == cricket::LOCAL_PORT_TYPE)
89 return RTCIceCandidateType::kHost;
90 if (type == cricket::STUN_PORT_TYPE)
91 return RTCIceCandidateType::kSrflx;
92 if (type == cricket::PRFLX_PORT_TYPE)
93 return RTCIceCandidateType::kPrflx;
94 if (type == cricket::RELAY_PORT_TYPE)
95 return RTCIceCandidateType::kRelay;
96 RTC_NOTREACHED();
97 return nullptr;
98}
99
hboscc555c52016-10-18 12:48:31 -0700100const char* DataStateToRTCDataChannelState(
101 DataChannelInterface::DataState state) {
102 switch (state) {
103 case DataChannelInterface::kConnecting:
104 return RTCDataChannelState::kConnecting;
105 case DataChannelInterface::kOpen:
106 return RTCDataChannelState::kOpen;
107 case DataChannelInterface::kClosing:
108 return RTCDataChannelState::kClosing;
109 case DataChannelInterface::kClosed:
110 return RTCDataChannelState::kClosed;
111 default:
112 RTC_NOTREACHED();
113 return nullptr;
114 }
115}
116
hbos06495bc2017-01-02 08:08:18 -0800117const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
118 cricket::IceCandidatePairState state) {
119 switch (state) {
120 case cricket::IceCandidatePairState::WAITING:
121 return RTCStatsIceCandidatePairState::kWaiting;
122 case cricket::IceCandidatePairState::IN_PROGRESS:
123 return RTCStatsIceCandidatePairState::kInProgress;
124 case cricket::IceCandidatePairState::SUCCEEDED:
125 return RTCStatsIceCandidatePairState::kSucceeded;
126 case cricket::IceCandidatePairState::FAILED:
127 return RTCStatsIceCandidatePairState::kFailed;
128 default:
129 RTC_NOTREACHED();
130 return nullptr;
131 }
132}
133
hbos0adb8282016-11-23 02:32:06 -0800134std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
135 uint64_t timestamp_us, bool inbound, bool audio,
136 const RtpCodecParameters& codec_params) {
137 RTC_DCHECK_GE(codec_params.payload_type, 0);
138 RTC_DCHECK_LE(codec_params.payload_type, 127);
139 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
140 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
141 RTCCodecStatsIDFromDirectionMediaAndPayload(inbound, audio, payload_type),
142 timestamp_us));
143 codec_stats->payload_type = payload_type;
144 codec_stats->codec = (audio ? "audio/" : "video/") + codec_params.mime_type;
145 codec_stats->clock_rate = static_cast<uint32_t>(codec_params.clock_rate);
146 return codec_stats;
147}
148
hbos09bc1282016-11-08 06:29:22 -0800149void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
150 const MediaStreamTrackInterface& track,
151 RTCMediaStreamTrackStats* track_stats) {
152 track_stats->track_identifier = track.id();
153 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
154}
155
hbos820f5782016-11-22 03:16:50 -0800156// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700157void SetInboundRTPStreamStatsFromMediaReceiverInfo(
158 const cricket::MediaReceiverInfo& media_receiver_info,
159 RTCInboundRTPStreamStats* inbound_stats) {
160 RTC_DCHECK(inbound_stats);
161 inbound_stats->ssrc = rtc::ToString<>(media_receiver_info.ssrc());
162 // TODO(hbos): Support the remote case. crbug.com/657855
163 inbound_stats->is_remote = false;
164 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
165 // |media_receiver_info.codec_name|. crbug.com/657854, 657855, 659117
166 inbound_stats->packets_received =
167 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
168 inbound_stats->bytes_received =
169 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800170 inbound_stats->packets_lost =
171 static_cast<uint32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700172 inbound_stats->fraction_lost =
173 static_cast<double>(media_receiver_info.fraction_lost);
174}
175
176void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
177 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800178 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700179 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800180 voice_receiver_info, inbound_audio);
181 inbound_audio->media_type = "audio";
182 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700183 static_cast<double>(voice_receiver_info.jitter_ms) /
184 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800185 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
186 // purposefully left undefined for audio.
hboseeafe942016-11-01 03:00:17 -0700187}
188
189void SetInboundRTPStreamStatsFromVideoReceiverInfo(
190 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800191 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700192 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800193 video_receiver_info, inbound_video);
194 inbound_video->media_type = "video";
195 inbound_video->fir_count =
196 static_cast<uint32_t>(video_receiver_info.firs_sent);
197 inbound_video->pli_count =
198 static_cast<uint32_t>(video_receiver_info.plis_sent);
199 inbound_video->nack_count =
200 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hboseeafe942016-11-01 03:00:17 -0700201}
202
hbos820f5782016-11-22 03:16:50 -0800203// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700204void SetOutboundRTPStreamStatsFromMediaSenderInfo(
205 const cricket::MediaSenderInfo& media_sender_info,
206 RTCOutboundRTPStreamStats* outbound_stats) {
207 RTC_DCHECK(outbound_stats);
208 outbound_stats->ssrc = rtc::ToString<>(media_sender_info.ssrc());
209 // TODO(hbos): Support the remote case. crbug.com/657856
210 outbound_stats->is_remote = false;
211 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
212 // |media_sender_info.codec_name|. crbug.com/657854, 657856, 659117
213 outbound_stats->packets_sent =
214 static_cast<uint32_t>(media_sender_info.packets_sent);
215 outbound_stats->bytes_sent =
216 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbose10e6d12016-12-15 01:54:29 -0800217 if (media_sender_info.rtt_ms >= 0) {
218 outbound_stats->round_trip_time = static_cast<double>(
219 media_sender_info.rtt_ms) / rtc::kNumMillisecsPerSec;
220 }
hbos6ded1902016-11-01 01:50:46 -0700221}
222
223void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
224 const cricket::VoiceSenderInfo& voice_sender_info,
225 RTCOutboundRTPStreamStats* outbound_audio) {
226 SetOutboundRTPStreamStatsFromMediaSenderInfo(
227 voice_sender_info, outbound_audio);
228 outbound_audio->media_type = "audio";
229 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
230 // purposefully left undefined for audio.
231}
232
233void SetOutboundRTPStreamStatsFromVideoSenderInfo(
234 const cricket::VideoSenderInfo& video_sender_info,
235 RTCOutboundRTPStreamStats* outbound_video) {
236 SetOutboundRTPStreamStatsFromMediaSenderInfo(
237 video_sender_info, outbound_video);
238 outbound_video->media_type = "video";
239 outbound_video->fir_count =
240 static_cast<uint32_t>(video_sender_info.firs_rcvd);
241 outbound_video->pli_count =
242 static_cast<uint32_t>(video_sender_info.plis_rcvd);
243 outbound_video->nack_count =
244 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
245}
246
hbos02ba2112016-10-28 05:14:53 -0700247void ProduceCertificateStatsFromSSLCertificateStats(
248 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
249 RTCStatsReport* report) {
250 RTCCertificateStats* prev_certificate_stats = nullptr;
251 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
252 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800253 std::string certificate_stats_id =
254 RTCCertificateIDFromFingerprint(s->fingerprint);
255 // It is possible for the same certificate to show up multiple times, e.g.
256 // if local and remote side use the same certificate in a loopback call.
257 // If the report already contains stats for this certificate, skip it.
258 if (report->Get(certificate_stats_id)) {
259 RTC_DCHECK_EQ(s, &certificate_stats);
260 break;
261 }
hbos02ba2112016-10-28 05:14:53 -0700262 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800263 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700264 certificate_stats->fingerprint = s->fingerprint;
265 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
266 certificate_stats->base64_certificate = s->base64_certificate;
267 if (prev_certificate_stats)
268 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
269 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
270 prev_certificate_stats = certificate_stats;
271 }
272}
273
274const std::string& ProduceIceCandidateStats(
275 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
276 RTCStatsReport* report) {
277 const std::string& id = "RTCIceCandidate_" + candidate.id();
278 const RTCStats* stats = report->Get(id);
279 if (!stats) {
280 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
281 if (is_local)
282 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
283 else
284 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
285 candidate_stats->ip = candidate.address().ipaddr().ToString();
286 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
287 candidate_stats->protocol = candidate.protocol();
288 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
289 candidate.type());
290 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
291
292 stats = candidate_stats.get();
293 report->AddStats(std::move(candidate_stats));
294 }
295 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
296 : RTCRemoteIceCandidateStats::kType);
297 return stats->id();
298}
299
hbos09bc1282016-11-08 06:29:22 -0800300void ProduceMediaStreamAndTrackStats(
301 int64_t timestamp_us,
302 rtc::scoped_refptr<StreamCollectionInterface> streams,
303 bool is_local,
304 RTCStatsReport* report) {
305 // TODO(hbos): When "AddTrack" is implemented we should iterate tracks to
306 // find which streams exist, not iterate streams to find tracks.
307 // crbug.com/659137
308 // TODO(hbos): Return stats of detached tracks. We have to perform stats
309 // gathering at the time of detachment to get accurate stats and timestamps.
310 // crbug.com/659137
311 if (!streams)
312 return;
313 for (size_t i = 0; i < streams->count(); ++i) {
314 MediaStreamInterface* stream = streams->at(i);
315
316 std::unique_ptr<RTCMediaStreamStats> stream_stats(
hbos02d2a922016-12-21 01:29:05 -0800317 new RTCMediaStreamStats(
318 (is_local ? "RTCMediaStream_local_" : "RTCMediaStream_remote_") +
319 stream->label(), timestamp_us));
hbos09bc1282016-11-08 06:29:22 -0800320 stream_stats->stream_identifier = stream->label();
321 stream_stats->track_ids = std::vector<std::string>();
322 // Audio Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800323 for (const rtc::scoped_refptr<AudioTrackInterface>& audio_track :
hbos09bc1282016-11-08 06:29:22 -0800324 stream->GetAudioTracks()) {
325 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
326 *audio_track.get());
327 if (report->Get(id)) {
328 // Skip track, stats already exist for it.
329 continue;
330 }
331 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
332 new RTCMediaStreamTrackStats(id, timestamp_us));
333 stream_stats->track_ids->push_back(audio_track_stats->id());
334 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
335 *audio_track.get(),
336 audio_track_stats.get());
337 audio_track_stats->remote_source = !is_local;
338 audio_track_stats->detached = false;
339 int signal_level;
340 if (audio_track->GetSignalLevel(&signal_level)) {
341 // Convert signal level from [0,32767] int to [0,1] double.
342 RTC_DCHECK_GE(signal_level, 0);
343 RTC_DCHECK_LE(signal_level, 32767);
344 audio_track_stats->audio_level = signal_level / 32767.0;
345 }
346 if (audio_track->GetAudioProcessor()) {
347 AudioProcessorInterface::AudioProcessorStats audio_processor_stats;
348 audio_track->GetAudioProcessor()->GetStats(&audio_processor_stats);
hbos9a394f02016-12-14 07:58:22 -0800349 if (audio_processor_stats.echo_return_loss != -100) {
350 audio_track_stats->echo_return_loss = static_cast<double>(
351 audio_processor_stats.echo_return_loss);
352 }
353 if (audio_processor_stats.echo_return_loss_enhancement != -100) {
354 audio_track_stats->echo_return_loss_enhancement = static_cast<double>(
355 audio_processor_stats.echo_return_loss_enhancement);
356 }
hbos09bc1282016-11-08 06:29:22 -0800357 }
358 report->AddStats(std::move(audio_track_stats));
359 }
360 // Video Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800361 for (const rtc::scoped_refptr<VideoTrackInterface>& video_track :
hbos09bc1282016-11-08 06:29:22 -0800362 stream->GetVideoTracks()) {
363 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
364 *video_track.get());
365 if (report->Get(id)) {
366 // Skip track, stats already exist for it.
367 continue;
368 }
369 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
370 new RTCMediaStreamTrackStats(id, timestamp_us));
371 stream_stats->track_ids->push_back(video_track_stats->id());
372 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
373 *video_track.get(),
374 video_track_stats.get());
375 video_track_stats->remote_source = !is_local;
376 video_track_stats->detached = false;
377 if (video_track->GetSource()) {
378 VideoTrackSourceInterface::Stats video_track_source_stats;
379 if (video_track->GetSource()->GetStats(&video_track_source_stats)) {
380 video_track_stats->frame_width = static_cast<uint32_t>(
381 video_track_source_stats.input_width);
382 video_track_stats->frame_height = static_cast<uint32_t>(
383 video_track_source_stats.input_height);
384 }
385 }
386 report->AddStats(std::move(video_track_stats));
387 }
388 report->AddStats(std::move(stream_stats));
389 }
390}
391
hboscc555c52016-10-18 12:48:31 -0700392} // namespace
393
hbosc82f2e12016-09-05 01:36:50 -0700394rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
395 PeerConnection* pc, int64_t cache_lifetime_us) {
396 return rtc::scoped_refptr<RTCStatsCollector>(
397 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
398}
399
400RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
401 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700402 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -0700403 signaling_thread_(pc->session()->signaling_thread()),
404 worker_thread_(pc->session()->worker_thread()),
405 network_thread_(pc->session()->network_thread()),
406 num_pending_partial_reports_(0),
407 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700408 cache_timestamp_us_(0),
409 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700410 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700411 RTC_DCHECK(signaling_thread_);
412 RTC_DCHECK(worker_thread_);
413 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700414 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbos82ebe022016-11-14 01:41:09 -0800415 pc_->SignalDataChannelCreated.connect(
416 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700417}
418
hbosb78306a2016-12-19 05:06:57 -0800419RTCStatsCollector::~RTCStatsCollector() {
420 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
421}
422
hbosc82f2e12016-09-05 01:36:50 -0700423void RTCStatsCollector::GetStatsReport(
424 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
425 RTC_DCHECK(signaling_thread_->IsCurrent());
426 RTC_DCHECK(callback);
427 callbacks_.push_back(callback);
428
hbos0e6758d2016-08-31 07:57:36 -0700429 // "Now" using a monotonically increasing timer.
430 int64_t cache_now_us = rtc::TimeMicros();
431 if (cached_report_ &&
432 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700433 // We have a fresh cached report to deliver.
434 DeliverCachedReport();
435 } else if (!num_pending_partial_reports_) {
436 // Only start gathering stats if we're not already gathering stats. In the
437 // case of already gathering stats, |callback_| will be invoked when there
438 // are no more pending partial reports.
439
440 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
441 // UTC), in microseconds. The system clock could be modified and is not
442 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700443 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700444
hbosf415f8a2017-01-02 04:28:51 -0800445 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700446 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800447
448 // Prepare |channel_names_| and |media_info_| for use in
449 // |ProducePartialResultsOnNetworkThread|.
450 channel_name_pairs_.reset(new ChannelNamePairs());
451 if (pc_->session()->voice_channel()) {
452 channel_name_pairs_->voice = rtc::Optional<ChannelNamePair>(
453 ChannelNamePair(pc_->session()->voice_channel()->content_name(),
454 pc_->session()->voice_channel()->transport_name()));
455 }
456 if (pc_->session()->video_channel()) {
457 channel_name_pairs_->video = rtc::Optional<ChannelNamePair>(
458 ChannelNamePair(pc_->session()->video_channel()->content_name(),
459 pc_->session()->video_channel()->transport_name()));
460 }
461 if (pc_->session()->data_channel()) {
462 channel_name_pairs_->data = rtc::Optional<ChannelNamePair>(
463 ChannelNamePair(pc_->session()->data_channel()->content_name(),
464 pc_->session()->data_channel()->transport_name()));
465 }
466 media_info_.reset(PrepareMediaInfo_s().release());
hbosf415f8a2017-01-02 04:28:51 -0800467
hbosc82f2e12016-09-05 01:36:50 -0700468 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
469 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
470 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800471 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700472 }
hbosd565b732016-08-30 14:04:35 -0700473}
474
475void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700476 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700477 cached_report_ = nullptr;
478}
479
hbosb78306a2016-12-19 05:06:57 -0800480void RTCStatsCollector::WaitForPendingRequest() {
481 RTC_DCHECK(signaling_thread_->IsCurrent());
482 if (num_pending_partial_reports_) {
483 rtc::Thread::Current()->ProcessMessages(0);
484 while (num_pending_partial_reports_) {
485 rtc::Thread::Current()->SleepMs(1);
486 rtc::Thread::Current()->ProcessMessages(0);
487 }
488 }
489}
490
hbosc82f2e12016-09-05 01:36:50 -0700491void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
492 int64_t timestamp_us) {
493 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700494 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
495 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700496
hboscc555c52016-10-18 12:48:31 -0700497 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos09bc1282016-11-08 06:29:22 -0800498 ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700499 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700500
501 AddPartialResults(report);
502}
503
hbosc82f2e12016-09-05 01:36:50 -0700504void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
505 int64_t timestamp_us) {
506 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700507 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
508 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700509
hbosdf6075a2016-12-19 04:58:02 -0800510 std::unique_ptr<SessionStats> session_stats =
511 pc_->session()->GetStats(*channel_name_pairs_);
512 if (session_stats) {
513 std::map<std::string, CertificateStatsPair> transport_cert_stats =
514 PrepareTransportCertificateStats_n(*session_stats);
515
516 ProduceCertificateStats_n(
517 timestamp_us, transport_cert_stats, report.get());
518 ProduceCodecStats_n(
519 timestamp_us, *media_info_, report.get());
520 ProduceIceCandidateAndPairStats_n(
521 timestamp_us, *session_stats, report.get());
522 ProduceRTPStreamStats_n(
523 timestamp_us, *session_stats, *media_info_, report.get());
524 ProduceTransportStats_n(
525 timestamp_us, *session_stats, transport_cert_stats, report.get());
526 }
hbosc82f2e12016-09-05 01:36:50 -0700527
528 AddPartialResults(report);
529}
530
531void RTCStatsCollector::AddPartialResults(
532 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
533 if (!signaling_thread_->IsCurrent()) {
534 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
535 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
536 rtc::scoped_refptr<RTCStatsCollector>(this),
537 partial_report));
538 return;
539 }
540 AddPartialResults_s(partial_report);
541}
542
543void RTCStatsCollector::AddPartialResults_s(
544 rtc::scoped_refptr<RTCStatsReport> partial_report) {
545 RTC_DCHECK(signaling_thread_->IsCurrent());
546 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
547 if (!partial_report_)
548 partial_report_ = partial_report;
549 else
550 partial_report_->TakeMembersFrom(partial_report);
551 --num_pending_partial_reports_;
552 if (!num_pending_partial_reports_) {
553 cache_timestamp_us_ = partial_report_timestamp_us_;
554 cached_report_ = partial_report_;
555 partial_report_ = nullptr;
556 DeliverCachedReport();
557 }
558}
559
560void RTCStatsCollector::DeliverCachedReport() {
561 RTC_DCHECK(signaling_thread_->IsCurrent());
562 RTC_DCHECK(!callbacks_.empty());
563 RTC_DCHECK(cached_report_);
564 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
565 callbacks_) {
566 callback->OnStatsDelivered(cached_report_);
567 }
568 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700569}
570
hbosdf6075a2016-12-19 04:58:02 -0800571void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700572 int64_t timestamp_us,
573 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700574 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800575 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700576 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
577 if (transport_cert_stats_pair.second.local) {
578 ProduceCertificateStatsFromSSLCertificateStats(
579 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700580 }
hbos02ba2112016-10-28 05:14:53 -0700581 if (transport_cert_stats_pair.second.remote) {
582 ProduceCertificateStatsFromSSLCertificateStats(
583 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700584 }
585 }
586}
587
hbosdf6075a2016-12-19 04:58:02 -0800588void RTCStatsCollector::ProduceCodecStats_n(
hbos0adb8282016-11-23 02:32:06 -0800589 int64_t timestamp_us, const MediaInfo& media_info,
590 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800591 RTC_DCHECK(network_thread_->IsCurrent());
hbos0adb8282016-11-23 02:32:06 -0800592 // Audio
593 if (media_info.voice) {
594 // Inbound
595 for (const auto& pair : media_info.voice->receive_codecs) {
596 report->AddStats(CodecStatsFromRtpCodecParameters(
597 timestamp_us, true, true, pair.second));
598 }
599 // Outbound
600 for (const auto& pair : media_info.voice->send_codecs) {
601 report->AddStats(CodecStatsFromRtpCodecParameters(
602 timestamp_us, false, true, pair.second));
603 }
604 }
605 // Video
606 if (media_info.video) {
607 // Inbound
608 for (const auto& pair : media_info.video->receive_codecs) {
609 report->AddStats(CodecStatsFromRtpCodecParameters(
610 timestamp_us, true, false, pair.second));
611 }
612 // Outbound
613 for (const auto& pair : media_info.video->send_codecs) {
614 report->AddStats(CodecStatsFromRtpCodecParameters(
615 timestamp_us, false, false, pair.second));
616 }
617 }
618}
619
hboscc555c52016-10-18 12:48:31 -0700620void RTCStatsCollector::ProduceDataChannelStats_s(
621 int64_t timestamp_us, RTCStatsReport* report) const {
622 RTC_DCHECK(signaling_thread_->IsCurrent());
623 for (const rtc::scoped_refptr<DataChannel>& data_channel :
624 pc_->sctp_data_channels()) {
625 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
626 new RTCDataChannelStats(
627 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
628 timestamp_us));
629 data_channel_stats->label = data_channel->label();
630 data_channel_stats->protocol = data_channel->protocol();
631 data_channel_stats->datachannelid = data_channel->id();
632 data_channel_stats->state =
633 DataStateToRTCDataChannelState(data_channel->state());
634 data_channel_stats->messages_sent = data_channel->messages_sent();
635 data_channel_stats->bytes_sent = data_channel->bytes_sent();
636 data_channel_stats->messages_received = data_channel->messages_received();
637 data_channel_stats->bytes_received = data_channel->bytes_received();
638 report->AddStats(std::move(data_channel_stats));
639 }
640}
641
hbosdf6075a2016-12-19 04:58:02 -0800642void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
hbosab9f6e42016-10-07 02:18:47 -0700643 int64_t timestamp_us, const SessionStats& session_stats,
644 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800645 RTC_DCHECK(network_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700646 for (const auto& transport_stats : session_stats.transport_stats) {
647 for (const auto& channel_stats : transport_stats.second.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800648 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
649 transport_stats.second.transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700650 for (const cricket::ConnectionInfo& info :
651 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700652 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700653 new RTCIceCandidatePairStats(
654 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
655 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700656
hbos0583b282016-11-30 01:50:14 -0800657 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700658 // TODO(hbos): There could be other candidates that are not paired with
659 // anything. We don't have a complete list. Local candidates come from
660 // Port objects, and prflx candidates (both local and remote) are only
661 // stored in candidate pairs. crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700662 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700663 timestamp_us, info.local_candidate, true, report);
hbos02ba2112016-10-28 05:14:53 -0700664 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700665 timestamp_us, info.remote_candidate, false, report);
hbos06495bc2017-01-02 08:08:18 -0800666 candidate_pair_stats->state =
667 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
668 candidate_pair_stats->priority = info.priority;
hbosc47a0c32016-10-11 14:54:49 -0700669 // TODO(hbos): This writable is different than the spec. It goes to
670 // false after a certain amount of time without a response passes.
671 // crbug.com/633550
672 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700673 candidate_pair_stats->bytes_sent =
674 static_cast<uint64_t>(info.sent_total_bytes);
675 candidate_pair_stats->bytes_received =
676 static_cast<uint64_t>(info.recv_total_bytes);
hbosc47a0c32016-10-11 14:54:49 -0700677 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
678 // smoothed according to the spec. crbug.com/633550. See
679 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
hbos3168c7a2016-12-15 06:17:08 -0800680 candidate_pair_stats->current_round_trip_time =
hbos6ded1902016-11-01 01:50:46 -0700681 static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec;
hbosd82f5122016-12-09 04:12:39 -0800682 candidate_pair_stats->requests_received =
683 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -0800684 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
685 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700686 candidate_pair_stats->responses_received =
687 static_cast<uint64_t>(info.recv_ping_responses);
688 candidate_pair_stats->responses_sent =
689 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -0800690 RTC_DCHECK_GE(info.sent_ping_requests_total,
691 info.sent_ping_requests_before_first_response);
692 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
693 info.sent_ping_requests_total -
694 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700695
696 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700697 }
698 }
699 }
700}
701
hbos09bc1282016-11-08 06:29:22 -0800702void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s(
703 int64_t timestamp_us, RTCStatsReport* report) const {
704 RTC_DCHECK(signaling_thread_->IsCurrent());
705 ProduceMediaStreamAndTrackStats(
706 timestamp_us, pc_->local_streams(), true, report);
707 ProduceMediaStreamAndTrackStats(
708 timestamp_us, pc_->remote_streams(), false, report);
709}
710
hbos6ab97ce2016-10-03 14:16:56 -0700711void RTCStatsCollector::ProducePeerConnectionStats_s(
712 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700713 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700714 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700715 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -0800716 stats->data_channels_opened = internal_record_.data_channels_opened;
717 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -0700718 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700719}
720
hbosdf6075a2016-12-19 04:58:02 -0800721void RTCStatsCollector::ProduceRTPStreamStats_n(
hbos6ded1902016-11-01 01:50:46 -0700722 int64_t timestamp_us, const SessionStats& session_stats,
hbos0adb8282016-11-23 02:32:06 -0800723 const MediaInfo& media_info, RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800724 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700725
726 // Audio
hbos0adb8282016-11-23 02:32:06 -0800727 if (media_info.voice) {
728 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
729 session_stats.proxy_to_transport, *pc_->session()->voice_channel());
730 RTC_DCHECK(!transport_id.empty());
731 // Inbound
732 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
733 media_info.voice->receivers) {
734 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
735 // is fixed.
736 if (voice_receiver_info.ssrc() == 0)
737 continue;
738 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
739 new RTCInboundRTPStreamStats(
740 RTCInboundRTPStreamStatsIDFromSSRC(
741 true, voice_receiver_info.ssrc()),
742 timestamp_us));
743 SetInboundRTPStreamStatsFromVoiceReceiverInfo(
744 voice_receiver_info, inbound_audio.get());
745 inbound_audio->transport_id = transport_id;
746 if (voice_receiver_info.codec_payload_type) {
747 inbound_audio->codec_id =
748 RTCCodecStatsIDFromDirectionMediaAndPayload(
749 true, true, *voice_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700750 }
hbos0adb8282016-11-23 02:32:06 -0800751 report->AddStats(std::move(inbound_audio));
752 }
753 // Outbound
754 for (const cricket::VoiceSenderInfo& voice_sender_info :
755 media_info.voice->senders) {
756 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
757 // is fixed.
758 if (voice_sender_info.ssrc() == 0)
759 continue;
760 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
761 new RTCOutboundRTPStreamStats(
762 RTCOutboundRTPStreamStatsIDFromSSRC(
763 true, voice_sender_info.ssrc()),
764 timestamp_us));
765 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
766 voice_sender_info, outbound_audio.get());
767 outbound_audio->transport_id = transport_id;
768 if (voice_sender_info.codec_payload_type) {
769 outbound_audio->codec_id =
770 RTCCodecStatsIDFromDirectionMediaAndPayload(
771 false, true, *voice_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700772 }
hbos0adb8282016-11-23 02:32:06 -0800773 report->AddStats(std::move(outbound_audio));
hbos6ded1902016-11-01 01:50:46 -0700774 }
775 }
776 // Video
hbos0adb8282016-11-23 02:32:06 -0800777 if (media_info.video) {
778 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
779 session_stats.proxy_to_transport, *pc_->session()->video_channel());
780 RTC_DCHECK(!transport_id.empty());
781 // Inbound
782 for (const cricket::VideoReceiverInfo& video_receiver_info :
783 media_info.video->receivers) {
784 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
785 // is fixed.
786 if (video_receiver_info.ssrc() == 0)
787 continue;
788 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
789 new RTCInboundRTPStreamStats(
790 RTCInboundRTPStreamStatsIDFromSSRC(
791 false, video_receiver_info.ssrc()),
792 timestamp_us));
793 SetInboundRTPStreamStatsFromVideoReceiverInfo(
794 video_receiver_info, inbound_video.get());
795 inbound_video->transport_id = transport_id;
796 if (video_receiver_info.codec_payload_type) {
797 inbound_video->codec_id =
798 RTCCodecStatsIDFromDirectionMediaAndPayload(
799 true, false, *video_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700800 }
hbos0adb8282016-11-23 02:32:06 -0800801 report->AddStats(std::move(inbound_video));
802 }
803 // Outbound
804 for (const cricket::VideoSenderInfo& video_sender_info :
805 media_info.video->senders) {
806 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
807 // is fixed.
808 if (video_sender_info.ssrc() == 0)
809 continue;
810 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
811 new RTCOutboundRTPStreamStats(
812 RTCOutboundRTPStreamStatsIDFromSSRC(
813 false, video_sender_info.ssrc()),
814 timestamp_us));
815 SetOutboundRTPStreamStatsFromVideoSenderInfo(
816 video_sender_info, outbound_video.get());
817 outbound_video->transport_id = transport_id;
818 if (video_sender_info.codec_payload_type) {
819 outbound_video->codec_id =
820 RTCCodecStatsIDFromDirectionMediaAndPayload(
821 false, false, *video_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700822 }
hbos0adb8282016-11-23 02:32:06 -0800823 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -0700824 }
825 }
826}
827
hbosdf6075a2016-12-19 04:58:02 -0800828void RTCStatsCollector::ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700829 int64_t timestamp_us, const SessionStats& session_stats,
830 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
831 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800832 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700833 for (const auto& transport : session_stats.transport_stats) {
834 // Get reference to RTCP channel, if it exists.
835 std::string rtcp_transport_stats_id;
836 for (const auto& channel_stats : transport.second.channel_stats) {
837 if (channel_stats.component ==
838 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
839 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
840 transport.second.transport_name, channel_stats.component);
841 break;
842 }
843 }
844
845 // Get reference to local and remote certificates of this transport, if they
846 // exist.
847 const auto& certificate_stats_it = transport_cert_stats.find(
848 transport.second.transport_name);
849 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
850 std::string local_certificate_id;
851 if (certificate_stats_it->second.local) {
852 local_certificate_id = RTCCertificateIDFromFingerprint(
853 certificate_stats_it->second.local->fingerprint);
854 }
855 std::string remote_certificate_id;
856 if (certificate_stats_it->second.remote) {
857 remote_certificate_id = RTCCertificateIDFromFingerprint(
858 certificate_stats_it->second.remote->fingerprint);
859 }
860
861 // There is one transport stats for each channel.
862 for (const auto& channel_stats : transport.second.channel_stats) {
863 std::unique_ptr<RTCTransportStats> transport_stats(
864 new RTCTransportStats(
865 RTCTransportStatsIDFromTransportChannel(
866 transport.second.transport_name, channel_stats.component),
867 timestamp_us));
868 transport_stats->bytes_sent = 0;
869 transport_stats->bytes_received = 0;
870 transport_stats->active_connection = false;
871 for (const cricket::ConnectionInfo& info :
872 channel_stats.connection_infos) {
873 *transport_stats->bytes_sent += info.sent_total_bytes;
874 *transport_stats->bytes_received += info.recv_total_bytes;
875 if (info.best_connection) {
876 transport_stats->active_connection = true;
877 transport_stats->selected_candidate_pair_id =
878 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
879 }
880 }
881 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
882 !rtcp_transport_stats_id.empty()) {
883 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
884 }
885 if (!local_certificate_id.empty())
886 transport_stats->local_certificate_id = local_certificate_id;
887 if (!remote_certificate_id.empty())
888 transport_stats->remote_certificate_id = remote_certificate_id;
889 report->AddStats(std::move(transport_stats));
890 }
891 }
892}
893
894std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -0800895RTCStatsCollector::PrepareTransportCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700896 const SessionStats& session_stats) const {
hbosdf6075a2016-12-19 04:58:02 -0800897 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700898 std::map<std::string, CertificateStatsPair> transport_cert_stats;
899 for (const auto& transport_stats : session_stats.transport_stats) {
900 CertificateStatsPair certificate_stats_pair;
901 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
902 if (pc_->session()->GetLocalCertificate(
903 transport_stats.second.transport_name, &local_certificate)) {
904 certificate_stats_pair.local =
905 local_certificate->ssl_certificate().GetStats();
906 }
907 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
908 pc_->session()->GetRemoteSSLCertificate(
909 transport_stats.second.transport_name);
910 if (remote_certificate) {
911 certificate_stats_pair.remote = remote_certificate->GetStats();
912 }
913 transport_cert_stats.insert(
914 std::make_pair(transport_stats.second.transport_name,
915 std::move(certificate_stats_pair)));
916 }
917 return transport_cert_stats;
918}
919
hbosdf6075a2016-12-19 04:58:02 -0800920std::unique_ptr<RTCStatsCollector::MediaInfo>
921RTCStatsCollector::PrepareMediaInfo_s() const {
922 RTC_DCHECK(signaling_thread_->IsCurrent());
923 std::unique_ptr<MediaInfo> media_info(new MediaInfo());
hbos0adb8282016-11-23 02:32:06 -0800924 if (pc_->session()->voice_channel()) {
925 cricket::VoiceMediaInfo voice_media_info;
926 if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800927 media_info->voice = rtc::Optional<cricket::VoiceMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800928 std::move(voice_media_info));
929 }
930 }
931 if (pc_->session()->video_channel()) {
932 cricket::VideoMediaInfo video_media_info;
933 if (pc_->session()->video_channel()->GetStats(&video_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800934 media_info->video = rtc::Optional<cricket::VideoMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800935 std::move(video_media_info));
936 }
937 }
938 return media_info;
939}
940
hbos82ebe022016-11-14 01:41:09 -0800941void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
942 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
943 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
944}
945
946void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
947 RTC_DCHECK(signaling_thread_->IsCurrent());
948 bool result = internal_record_.opened_data_channels.insert(
949 reinterpret_cast<uintptr_t>(channel)).second;
950 ++internal_record_.data_channels_opened;
951 RTC_DCHECK(result);
952}
953
954void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
955 RTC_DCHECK(signaling_thread_->IsCurrent());
956 // Only channels that have been fully opened (and have increased the
957 // |data_channels_opened_| counter) increase the closed counter.
958 if (internal_record_.opened_data_channels.find(
959 reinterpret_cast<uintptr_t>(channel)) !=
960 internal_record_.opened_data_channels.end()) {
961 ++internal_record_.data_channels_closed;
962 }
963}
964
hboscc555c52016-10-18 12:48:31 -0700965const char* CandidateTypeToRTCIceCandidateTypeForTesting(
966 const std::string& type) {
967 return CandidateTypeToRTCIceCandidateType(type);
968}
969
970const char* DataStateToRTCDataChannelStateForTesting(
971 DataChannelInterface::DataState state) {
972 return DataStateToRTCDataChannelState(state);
973}
974
hbosd565b732016-08-30 14:04:35 -0700975} // namespace webrtc