blob: ff7b7516f6fa2e992e6473950e515af0c06bf7f3 [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);
hbos6769c492017-01-02 08:35:13 -0800201 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hboseeafe942016-11-01 03:00:17 -0700202}
203
hbos820f5782016-11-22 03:16:50 -0800204// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700205void SetOutboundRTPStreamStatsFromMediaSenderInfo(
206 const cricket::MediaSenderInfo& media_sender_info,
207 RTCOutboundRTPStreamStats* outbound_stats) {
208 RTC_DCHECK(outbound_stats);
209 outbound_stats->ssrc = rtc::ToString<>(media_sender_info.ssrc());
210 // TODO(hbos): Support the remote case. crbug.com/657856
211 outbound_stats->is_remote = false;
212 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
213 // |media_sender_info.codec_name|. crbug.com/657854, 657856, 659117
214 outbound_stats->packets_sent =
215 static_cast<uint32_t>(media_sender_info.packets_sent);
216 outbound_stats->bytes_sent =
217 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbose10e6d12016-12-15 01:54:29 -0800218 if (media_sender_info.rtt_ms >= 0) {
219 outbound_stats->round_trip_time = static_cast<double>(
220 media_sender_info.rtt_ms) / rtc::kNumMillisecsPerSec;
221 }
hbos6ded1902016-11-01 01:50:46 -0700222}
223
224void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
225 const cricket::VoiceSenderInfo& voice_sender_info,
226 RTCOutboundRTPStreamStats* outbound_audio) {
227 SetOutboundRTPStreamStatsFromMediaSenderInfo(
228 voice_sender_info, outbound_audio);
229 outbound_audio->media_type = "audio";
230 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
231 // purposefully left undefined for audio.
232}
233
234void SetOutboundRTPStreamStatsFromVideoSenderInfo(
235 const cricket::VideoSenderInfo& video_sender_info,
236 RTCOutboundRTPStreamStats* outbound_video) {
237 SetOutboundRTPStreamStatsFromMediaSenderInfo(
238 video_sender_info, outbound_video);
239 outbound_video->media_type = "video";
240 outbound_video->fir_count =
241 static_cast<uint32_t>(video_sender_info.firs_rcvd);
242 outbound_video->pli_count =
243 static_cast<uint32_t>(video_sender_info.plis_rcvd);
244 outbound_video->nack_count =
245 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800246 if (video_sender_info.qp_sum)
247 outbound_video->qp_sum = *video_sender_info.qp_sum;
248 outbound_video->frames_encoded = video_sender_info.frames_encoded;
hbos6ded1902016-11-01 01:50:46 -0700249}
250
hbos02ba2112016-10-28 05:14:53 -0700251void ProduceCertificateStatsFromSSLCertificateStats(
252 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
253 RTCStatsReport* report) {
254 RTCCertificateStats* prev_certificate_stats = nullptr;
255 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
256 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800257 std::string certificate_stats_id =
258 RTCCertificateIDFromFingerprint(s->fingerprint);
259 // It is possible for the same certificate to show up multiple times, e.g.
260 // if local and remote side use the same certificate in a loopback call.
261 // If the report already contains stats for this certificate, skip it.
262 if (report->Get(certificate_stats_id)) {
263 RTC_DCHECK_EQ(s, &certificate_stats);
264 break;
265 }
hbos02ba2112016-10-28 05:14:53 -0700266 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800267 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700268 certificate_stats->fingerprint = s->fingerprint;
269 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
270 certificate_stats->base64_certificate = s->base64_certificate;
271 if (prev_certificate_stats)
272 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
273 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
274 prev_certificate_stats = certificate_stats;
275 }
276}
277
278const std::string& ProduceIceCandidateStats(
279 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
280 RTCStatsReport* report) {
281 const std::string& id = "RTCIceCandidate_" + candidate.id();
282 const RTCStats* stats = report->Get(id);
283 if (!stats) {
284 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
285 if (is_local)
286 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
287 else
288 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
289 candidate_stats->ip = candidate.address().ipaddr().ToString();
290 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
291 candidate_stats->protocol = candidate.protocol();
292 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
293 candidate.type());
294 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
295
296 stats = candidate_stats.get();
297 report->AddStats(std::move(candidate_stats));
298 }
299 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
300 : RTCRemoteIceCandidateStats::kType);
301 return stats->id();
302}
303
hbos09bc1282016-11-08 06:29:22 -0800304void ProduceMediaStreamAndTrackStats(
305 int64_t timestamp_us,
306 rtc::scoped_refptr<StreamCollectionInterface> streams,
307 bool is_local,
308 RTCStatsReport* report) {
309 // TODO(hbos): When "AddTrack" is implemented we should iterate tracks to
310 // find which streams exist, not iterate streams to find tracks.
311 // crbug.com/659137
312 // TODO(hbos): Return stats of detached tracks. We have to perform stats
313 // gathering at the time of detachment to get accurate stats and timestamps.
314 // crbug.com/659137
315 if (!streams)
316 return;
317 for (size_t i = 0; i < streams->count(); ++i) {
318 MediaStreamInterface* stream = streams->at(i);
319
320 std::unique_ptr<RTCMediaStreamStats> stream_stats(
hbos02d2a922016-12-21 01:29:05 -0800321 new RTCMediaStreamStats(
322 (is_local ? "RTCMediaStream_local_" : "RTCMediaStream_remote_") +
323 stream->label(), timestamp_us));
hbos09bc1282016-11-08 06:29:22 -0800324 stream_stats->stream_identifier = stream->label();
325 stream_stats->track_ids = std::vector<std::string>();
326 // Audio Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800327 for (const rtc::scoped_refptr<AudioTrackInterface>& audio_track :
hbos09bc1282016-11-08 06:29:22 -0800328 stream->GetAudioTracks()) {
329 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
330 *audio_track.get());
331 if (report->Get(id)) {
332 // Skip track, stats already exist for it.
333 continue;
334 }
335 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
336 new RTCMediaStreamTrackStats(id, timestamp_us));
337 stream_stats->track_ids->push_back(audio_track_stats->id());
338 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
339 *audio_track.get(),
340 audio_track_stats.get());
341 audio_track_stats->remote_source = !is_local;
342 audio_track_stats->detached = false;
343 int signal_level;
344 if (audio_track->GetSignalLevel(&signal_level)) {
345 // Convert signal level from [0,32767] int to [0,1] double.
346 RTC_DCHECK_GE(signal_level, 0);
347 RTC_DCHECK_LE(signal_level, 32767);
348 audio_track_stats->audio_level = signal_level / 32767.0;
349 }
350 if (audio_track->GetAudioProcessor()) {
351 AudioProcessorInterface::AudioProcessorStats audio_processor_stats;
352 audio_track->GetAudioProcessor()->GetStats(&audio_processor_stats);
hbos9a394f02016-12-14 07:58:22 -0800353 if (audio_processor_stats.echo_return_loss != -100) {
354 audio_track_stats->echo_return_loss = static_cast<double>(
355 audio_processor_stats.echo_return_loss);
356 }
357 if (audio_processor_stats.echo_return_loss_enhancement != -100) {
358 audio_track_stats->echo_return_loss_enhancement = static_cast<double>(
359 audio_processor_stats.echo_return_loss_enhancement);
360 }
hbos09bc1282016-11-08 06:29:22 -0800361 }
362 report->AddStats(std::move(audio_track_stats));
363 }
364 // Video Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800365 for (const rtc::scoped_refptr<VideoTrackInterface>& video_track :
hbos09bc1282016-11-08 06:29:22 -0800366 stream->GetVideoTracks()) {
367 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
368 *video_track.get());
369 if (report->Get(id)) {
370 // Skip track, stats already exist for it.
371 continue;
372 }
373 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
374 new RTCMediaStreamTrackStats(id, timestamp_us));
375 stream_stats->track_ids->push_back(video_track_stats->id());
376 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
377 *video_track.get(),
378 video_track_stats.get());
379 video_track_stats->remote_source = !is_local;
380 video_track_stats->detached = false;
381 if (video_track->GetSource()) {
382 VideoTrackSourceInterface::Stats video_track_source_stats;
383 if (video_track->GetSource()->GetStats(&video_track_source_stats)) {
384 video_track_stats->frame_width = static_cast<uint32_t>(
385 video_track_source_stats.input_width);
386 video_track_stats->frame_height = static_cast<uint32_t>(
387 video_track_source_stats.input_height);
388 }
389 }
390 report->AddStats(std::move(video_track_stats));
391 }
392 report->AddStats(std::move(stream_stats));
393 }
394}
395
hboscc555c52016-10-18 12:48:31 -0700396} // namespace
397
hbosc82f2e12016-09-05 01:36:50 -0700398rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
399 PeerConnection* pc, int64_t cache_lifetime_us) {
400 return rtc::scoped_refptr<RTCStatsCollector>(
401 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
402}
403
404RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
405 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700406 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -0700407 signaling_thread_(pc->session()->signaling_thread()),
408 worker_thread_(pc->session()->worker_thread()),
409 network_thread_(pc->session()->network_thread()),
410 num_pending_partial_reports_(0),
411 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700412 cache_timestamp_us_(0),
413 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700414 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700415 RTC_DCHECK(signaling_thread_);
416 RTC_DCHECK(worker_thread_);
417 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700418 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbos82ebe022016-11-14 01:41:09 -0800419 pc_->SignalDataChannelCreated.connect(
420 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700421}
422
hbosb78306a2016-12-19 05:06:57 -0800423RTCStatsCollector::~RTCStatsCollector() {
424 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
425}
426
hbosc82f2e12016-09-05 01:36:50 -0700427void RTCStatsCollector::GetStatsReport(
428 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
429 RTC_DCHECK(signaling_thread_->IsCurrent());
430 RTC_DCHECK(callback);
431 callbacks_.push_back(callback);
432
hbos0e6758d2016-08-31 07:57:36 -0700433 // "Now" using a monotonically increasing timer.
434 int64_t cache_now_us = rtc::TimeMicros();
435 if (cached_report_ &&
436 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700437 // We have a fresh cached report to deliver.
438 DeliverCachedReport();
439 } else if (!num_pending_partial_reports_) {
440 // Only start gathering stats if we're not already gathering stats. In the
441 // case of already gathering stats, |callback_| will be invoked when there
442 // are no more pending partial reports.
443
444 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
445 // UTC), in microseconds. The system clock could be modified and is not
446 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700447 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700448
hbosf415f8a2017-01-02 04:28:51 -0800449 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700450 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800451
452 // Prepare |channel_names_| and |media_info_| for use in
453 // |ProducePartialResultsOnNetworkThread|.
454 channel_name_pairs_.reset(new ChannelNamePairs());
455 if (pc_->session()->voice_channel()) {
456 channel_name_pairs_->voice = rtc::Optional<ChannelNamePair>(
457 ChannelNamePair(pc_->session()->voice_channel()->content_name(),
458 pc_->session()->voice_channel()->transport_name()));
459 }
460 if (pc_->session()->video_channel()) {
461 channel_name_pairs_->video = rtc::Optional<ChannelNamePair>(
462 ChannelNamePair(pc_->session()->video_channel()->content_name(),
463 pc_->session()->video_channel()->transport_name()));
464 }
465 if (pc_->session()->data_channel()) {
466 channel_name_pairs_->data = rtc::Optional<ChannelNamePair>(
467 ChannelNamePair(pc_->session()->data_channel()->content_name(),
468 pc_->session()->data_channel()->transport_name()));
469 }
470 media_info_.reset(PrepareMediaInfo_s().release());
hbosf415f8a2017-01-02 04:28:51 -0800471
hbosc82f2e12016-09-05 01:36:50 -0700472 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
473 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
474 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800475 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700476 }
hbosd565b732016-08-30 14:04:35 -0700477}
478
479void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700480 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700481 cached_report_ = nullptr;
482}
483
hbosb78306a2016-12-19 05:06:57 -0800484void RTCStatsCollector::WaitForPendingRequest() {
485 RTC_DCHECK(signaling_thread_->IsCurrent());
486 if (num_pending_partial_reports_) {
487 rtc::Thread::Current()->ProcessMessages(0);
488 while (num_pending_partial_reports_) {
489 rtc::Thread::Current()->SleepMs(1);
490 rtc::Thread::Current()->ProcessMessages(0);
491 }
492 }
493}
494
hbosc82f2e12016-09-05 01:36:50 -0700495void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
496 int64_t timestamp_us) {
497 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700498 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
499 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700500
hboscc555c52016-10-18 12:48:31 -0700501 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos09bc1282016-11-08 06:29:22 -0800502 ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700503 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700504
505 AddPartialResults(report);
506}
507
hbosc82f2e12016-09-05 01:36:50 -0700508void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
509 int64_t timestamp_us) {
510 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700511 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
512 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700513
hbosdf6075a2016-12-19 04:58:02 -0800514 std::unique_ptr<SessionStats> session_stats =
515 pc_->session()->GetStats(*channel_name_pairs_);
516 if (session_stats) {
517 std::map<std::string, CertificateStatsPair> transport_cert_stats =
518 PrepareTransportCertificateStats_n(*session_stats);
519
520 ProduceCertificateStats_n(
521 timestamp_us, transport_cert_stats, report.get());
522 ProduceCodecStats_n(
523 timestamp_us, *media_info_, report.get());
524 ProduceIceCandidateAndPairStats_n(
525 timestamp_us, *session_stats, report.get());
526 ProduceRTPStreamStats_n(
527 timestamp_us, *session_stats, *media_info_, report.get());
528 ProduceTransportStats_n(
529 timestamp_us, *session_stats, transport_cert_stats, report.get());
530 }
hbosc82f2e12016-09-05 01:36:50 -0700531
532 AddPartialResults(report);
533}
534
535void RTCStatsCollector::AddPartialResults(
536 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
537 if (!signaling_thread_->IsCurrent()) {
538 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
539 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
540 rtc::scoped_refptr<RTCStatsCollector>(this),
541 partial_report));
542 return;
543 }
544 AddPartialResults_s(partial_report);
545}
546
547void RTCStatsCollector::AddPartialResults_s(
548 rtc::scoped_refptr<RTCStatsReport> partial_report) {
549 RTC_DCHECK(signaling_thread_->IsCurrent());
550 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
551 if (!partial_report_)
552 partial_report_ = partial_report;
553 else
554 partial_report_->TakeMembersFrom(partial_report);
555 --num_pending_partial_reports_;
556 if (!num_pending_partial_reports_) {
557 cache_timestamp_us_ = partial_report_timestamp_us_;
558 cached_report_ = partial_report_;
559 partial_report_ = nullptr;
560 DeliverCachedReport();
561 }
562}
563
564void RTCStatsCollector::DeliverCachedReport() {
565 RTC_DCHECK(signaling_thread_->IsCurrent());
566 RTC_DCHECK(!callbacks_.empty());
567 RTC_DCHECK(cached_report_);
568 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
569 callbacks_) {
570 callback->OnStatsDelivered(cached_report_);
571 }
572 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700573}
574
hbosdf6075a2016-12-19 04:58:02 -0800575void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700576 int64_t timestamp_us,
577 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700578 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800579 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700580 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
581 if (transport_cert_stats_pair.second.local) {
582 ProduceCertificateStatsFromSSLCertificateStats(
583 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700584 }
hbos02ba2112016-10-28 05:14:53 -0700585 if (transport_cert_stats_pair.second.remote) {
586 ProduceCertificateStatsFromSSLCertificateStats(
587 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700588 }
589 }
590}
591
hbosdf6075a2016-12-19 04:58:02 -0800592void RTCStatsCollector::ProduceCodecStats_n(
hbos0adb8282016-11-23 02:32:06 -0800593 int64_t timestamp_us, const MediaInfo& media_info,
594 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800595 RTC_DCHECK(network_thread_->IsCurrent());
hbos0adb8282016-11-23 02:32:06 -0800596 // Audio
597 if (media_info.voice) {
598 // Inbound
599 for (const auto& pair : media_info.voice->receive_codecs) {
600 report->AddStats(CodecStatsFromRtpCodecParameters(
601 timestamp_us, true, true, pair.second));
602 }
603 // Outbound
604 for (const auto& pair : media_info.voice->send_codecs) {
605 report->AddStats(CodecStatsFromRtpCodecParameters(
606 timestamp_us, false, true, pair.second));
607 }
608 }
609 // Video
610 if (media_info.video) {
611 // Inbound
612 for (const auto& pair : media_info.video->receive_codecs) {
613 report->AddStats(CodecStatsFromRtpCodecParameters(
614 timestamp_us, true, false, pair.second));
615 }
616 // Outbound
617 for (const auto& pair : media_info.video->send_codecs) {
618 report->AddStats(CodecStatsFromRtpCodecParameters(
619 timestamp_us, false, false, pair.second));
620 }
621 }
622}
623
hboscc555c52016-10-18 12:48:31 -0700624void RTCStatsCollector::ProduceDataChannelStats_s(
625 int64_t timestamp_us, RTCStatsReport* report) const {
626 RTC_DCHECK(signaling_thread_->IsCurrent());
627 for (const rtc::scoped_refptr<DataChannel>& data_channel :
628 pc_->sctp_data_channels()) {
629 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
630 new RTCDataChannelStats(
631 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
632 timestamp_us));
633 data_channel_stats->label = data_channel->label();
634 data_channel_stats->protocol = data_channel->protocol();
635 data_channel_stats->datachannelid = data_channel->id();
636 data_channel_stats->state =
637 DataStateToRTCDataChannelState(data_channel->state());
638 data_channel_stats->messages_sent = data_channel->messages_sent();
639 data_channel_stats->bytes_sent = data_channel->bytes_sent();
640 data_channel_stats->messages_received = data_channel->messages_received();
641 data_channel_stats->bytes_received = data_channel->bytes_received();
642 report->AddStats(std::move(data_channel_stats));
643 }
644}
645
hbosdf6075a2016-12-19 04:58:02 -0800646void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
hbosab9f6e42016-10-07 02:18:47 -0700647 int64_t timestamp_us, const SessionStats& session_stats,
648 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800649 RTC_DCHECK(network_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700650 for (const auto& transport_stats : session_stats.transport_stats) {
651 for (const auto& channel_stats : transport_stats.second.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800652 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
653 transport_stats.second.transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700654 for (const cricket::ConnectionInfo& info :
655 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700656 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700657 new RTCIceCandidatePairStats(
658 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
659 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700660
hbos0583b282016-11-30 01:50:14 -0800661 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700662 // TODO(hbos): There could be other candidates that are not paired with
663 // anything. We don't have a complete list. Local candidates come from
664 // Port objects, and prflx candidates (both local and remote) are only
665 // stored in candidate pairs. crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700666 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700667 timestamp_us, info.local_candidate, true, report);
hbos02ba2112016-10-28 05:14:53 -0700668 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700669 timestamp_us, info.remote_candidate, false, report);
hbos06495bc2017-01-02 08:08:18 -0800670 candidate_pair_stats->state =
671 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
672 candidate_pair_stats->priority = info.priority;
hbosc47a0c32016-10-11 14:54:49 -0700673 // TODO(hbos): This writable is different than the spec. It goes to
674 // false after a certain amount of time without a response passes.
675 // crbug.com/633550
676 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700677 candidate_pair_stats->bytes_sent =
678 static_cast<uint64_t>(info.sent_total_bytes);
679 candidate_pair_stats->bytes_received =
680 static_cast<uint64_t>(info.recv_total_bytes);
hbosc47a0c32016-10-11 14:54:49 -0700681 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
682 // smoothed according to the spec. crbug.com/633550. See
683 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
hbos3168c7a2016-12-15 06:17:08 -0800684 candidate_pair_stats->current_round_trip_time =
hbos6ded1902016-11-01 01:50:46 -0700685 static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec;
hbosd82f5122016-12-09 04:12:39 -0800686 candidate_pair_stats->requests_received =
687 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -0800688 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
689 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700690 candidate_pair_stats->responses_received =
691 static_cast<uint64_t>(info.recv_ping_responses);
692 candidate_pair_stats->responses_sent =
693 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -0800694 RTC_DCHECK_GE(info.sent_ping_requests_total,
695 info.sent_ping_requests_before_first_response);
696 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
697 info.sent_ping_requests_total -
698 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700699
700 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700701 }
702 }
703 }
704}
705
hbos09bc1282016-11-08 06:29:22 -0800706void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s(
707 int64_t timestamp_us, RTCStatsReport* report) const {
708 RTC_DCHECK(signaling_thread_->IsCurrent());
709 ProduceMediaStreamAndTrackStats(
710 timestamp_us, pc_->local_streams(), true, report);
711 ProduceMediaStreamAndTrackStats(
712 timestamp_us, pc_->remote_streams(), false, report);
713}
714
hbos6ab97ce2016-10-03 14:16:56 -0700715void RTCStatsCollector::ProducePeerConnectionStats_s(
716 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700717 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700718 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700719 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -0800720 stats->data_channels_opened = internal_record_.data_channels_opened;
721 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -0700722 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700723}
724
hbosdf6075a2016-12-19 04:58:02 -0800725void RTCStatsCollector::ProduceRTPStreamStats_n(
hbos6ded1902016-11-01 01:50:46 -0700726 int64_t timestamp_us, const SessionStats& session_stats,
hbos0adb8282016-11-23 02:32:06 -0800727 const MediaInfo& media_info, RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800728 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700729
730 // Audio
hbos0adb8282016-11-23 02:32:06 -0800731 if (media_info.voice) {
732 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
733 session_stats.proxy_to_transport, *pc_->session()->voice_channel());
734 RTC_DCHECK(!transport_id.empty());
735 // Inbound
736 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
737 media_info.voice->receivers) {
738 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
739 // is fixed.
740 if (voice_receiver_info.ssrc() == 0)
741 continue;
742 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
743 new RTCInboundRTPStreamStats(
744 RTCInboundRTPStreamStatsIDFromSSRC(
745 true, voice_receiver_info.ssrc()),
746 timestamp_us));
747 SetInboundRTPStreamStatsFromVoiceReceiverInfo(
748 voice_receiver_info, inbound_audio.get());
749 inbound_audio->transport_id = transport_id;
750 if (voice_receiver_info.codec_payload_type) {
751 inbound_audio->codec_id =
752 RTCCodecStatsIDFromDirectionMediaAndPayload(
753 true, true, *voice_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700754 }
hbos0adb8282016-11-23 02:32:06 -0800755 report->AddStats(std::move(inbound_audio));
756 }
757 // Outbound
758 for (const cricket::VoiceSenderInfo& voice_sender_info :
759 media_info.voice->senders) {
760 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
761 // is fixed.
762 if (voice_sender_info.ssrc() == 0)
763 continue;
764 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
765 new RTCOutboundRTPStreamStats(
766 RTCOutboundRTPStreamStatsIDFromSSRC(
767 true, voice_sender_info.ssrc()),
768 timestamp_us));
769 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
770 voice_sender_info, outbound_audio.get());
771 outbound_audio->transport_id = transport_id;
772 if (voice_sender_info.codec_payload_type) {
773 outbound_audio->codec_id =
774 RTCCodecStatsIDFromDirectionMediaAndPayload(
775 false, true, *voice_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700776 }
hbos0adb8282016-11-23 02:32:06 -0800777 report->AddStats(std::move(outbound_audio));
hbos6ded1902016-11-01 01:50:46 -0700778 }
779 }
780 // Video
hbos0adb8282016-11-23 02:32:06 -0800781 if (media_info.video) {
782 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
783 session_stats.proxy_to_transport, *pc_->session()->video_channel());
784 RTC_DCHECK(!transport_id.empty());
785 // Inbound
786 for (const cricket::VideoReceiverInfo& video_receiver_info :
787 media_info.video->receivers) {
788 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
789 // is fixed.
790 if (video_receiver_info.ssrc() == 0)
791 continue;
792 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
793 new RTCInboundRTPStreamStats(
794 RTCInboundRTPStreamStatsIDFromSSRC(
795 false, video_receiver_info.ssrc()),
796 timestamp_us));
797 SetInboundRTPStreamStatsFromVideoReceiverInfo(
798 video_receiver_info, inbound_video.get());
799 inbound_video->transport_id = transport_id;
800 if (video_receiver_info.codec_payload_type) {
801 inbound_video->codec_id =
802 RTCCodecStatsIDFromDirectionMediaAndPayload(
803 true, false, *video_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700804 }
hbos0adb8282016-11-23 02:32:06 -0800805 report->AddStats(std::move(inbound_video));
806 }
807 // Outbound
808 for (const cricket::VideoSenderInfo& video_sender_info :
809 media_info.video->senders) {
810 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
811 // is fixed.
812 if (video_sender_info.ssrc() == 0)
813 continue;
814 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
815 new RTCOutboundRTPStreamStats(
816 RTCOutboundRTPStreamStatsIDFromSSRC(
817 false, video_sender_info.ssrc()),
818 timestamp_us));
819 SetOutboundRTPStreamStatsFromVideoSenderInfo(
820 video_sender_info, outbound_video.get());
821 outbound_video->transport_id = transport_id;
822 if (video_sender_info.codec_payload_type) {
823 outbound_video->codec_id =
824 RTCCodecStatsIDFromDirectionMediaAndPayload(
825 false, false, *video_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700826 }
hbos0adb8282016-11-23 02:32:06 -0800827 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -0700828 }
829 }
830}
831
hbosdf6075a2016-12-19 04:58:02 -0800832void RTCStatsCollector::ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700833 int64_t timestamp_us, const SessionStats& session_stats,
834 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
835 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800836 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700837 for (const auto& transport : session_stats.transport_stats) {
838 // Get reference to RTCP channel, if it exists.
839 std::string rtcp_transport_stats_id;
840 for (const auto& channel_stats : transport.second.channel_stats) {
841 if (channel_stats.component ==
842 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
843 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
844 transport.second.transport_name, channel_stats.component);
845 break;
846 }
847 }
848
849 // Get reference to local and remote certificates of this transport, if they
850 // exist.
851 const auto& certificate_stats_it = transport_cert_stats.find(
852 transport.second.transport_name);
853 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
854 std::string local_certificate_id;
855 if (certificate_stats_it->second.local) {
856 local_certificate_id = RTCCertificateIDFromFingerprint(
857 certificate_stats_it->second.local->fingerprint);
858 }
859 std::string remote_certificate_id;
860 if (certificate_stats_it->second.remote) {
861 remote_certificate_id = RTCCertificateIDFromFingerprint(
862 certificate_stats_it->second.remote->fingerprint);
863 }
864
865 // There is one transport stats for each channel.
866 for (const auto& channel_stats : transport.second.channel_stats) {
867 std::unique_ptr<RTCTransportStats> transport_stats(
868 new RTCTransportStats(
869 RTCTransportStatsIDFromTransportChannel(
870 transport.second.transport_name, channel_stats.component),
871 timestamp_us));
872 transport_stats->bytes_sent = 0;
873 transport_stats->bytes_received = 0;
874 transport_stats->active_connection = false;
875 for (const cricket::ConnectionInfo& info :
876 channel_stats.connection_infos) {
877 *transport_stats->bytes_sent += info.sent_total_bytes;
878 *transport_stats->bytes_received += info.recv_total_bytes;
879 if (info.best_connection) {
880 transport_stats->active_connection = true;
881 transport_stats->selected_candidate_pair_id =
882 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
883 }
884 }
885 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
886 !rtcp_transport_stats_id.empty()) {
887 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
888 }
889 if (!local_certificate_id.empty())
890 transport_stats->local_certificate_id = local_certificate_id;
891 if (!remote_certificate_id.empty())
892 transport_stats->remote_certificate_id = remote_certificate_id;
893 report->AddStats(std::move(transport_stats));
894 }
895 }
896}
897
898std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -0800899RTCStatsCollector::PrepareTransportCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700900 const SessionStats& session_stats) const {
hbosdf6075a2016-12-19 04:58:02 -0800901 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700902 std::map<std::string, CertificateStatsPair> transport_cert_stats;
903 for (const auto& transport_stats : session_stats.transport_stats) {
904 CertificateStatsPair certificate_stats_pair;
905 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
906 if (pc_->session()->GetLocalCertificate(
907 transport_stats.second.transport_name, &local_certificate)) {
908 certificate_stats_pair.local =
909 local_certificate->ssl_certificate().GetStats();
910 }
911 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
912 pc_->session()->GetRemoteSSLCertificate(
913 transport_stats.second.transport_name);
914 if (remote_certificate) {
915 certificate_stats_pair.remote = remote_certificate->GetStats();
916 }
917 transport_cert_stats.insert(
918 std::make_pair(transport_stats.second.transport_name,
919 std::move(certificate_stats_pair)));
920 }
921 return transport_cert_stats;
922}
923
hbosdf6075a2016-12-19 04:58:02 -0800924std::unique_ptr<RTCStatsCollector::MediaInfo>
925RTCStatsCollector::PrepareMediaInfo_s() const {
926 RTC_DCHECK(signaling_thread_->IsCurrent());
927 std::unique_ptr<MediaInfo> media_info(new MediaInfo());
hbos0adb8282016-11-23 02:32:06 -0800928 if (pc_->session()->voice_channel()) {
929 cricket::VoiceMediaInfo voice_media_info;
930 if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800931 media_info->voice = rtc::Optional<cricket::VoiceMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800932 std::move(voice_media_info));
933 }
934 }
935 if (pc_->session()->video_channel()) {
936 cricket::VideoMediaInfo video_media_info;
937 if (pc_->session()->video_channel()->GetStats(&video_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800938 media_info->video = rtc::Optional<cricket::VideoMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800939 std::move(video_media_info));
940 }
941 }
942 return media_info;
943}
944
hbos82ebe022016-11-14 01:41:09 -0800945void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
946 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
947 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
948}
949
950void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
951 RTC_DCHECK(signaling_thread_->IsCurrent());
952 bool result = internal_record_.opened_data_channels.insert(
953 reinterpret_cast<uintptr_t>(channel)).second;
954 ++internal_record_.data_channels_opened;
955 RTC_DCHECK(result);
956}
957
958void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
959 RTC_DCHECK(signaling_thread_->IsCurrent());
960 // Only channels that have been fully opened (and have increased the
961 // |data_channels_opened_| counter) increase the closed counter.
962 if (internal_record_.opened_data_channels.find(
963 reinterpret_cast<uintptr_t>(channel)) !=
964 internal_record_.opened_data_channels.end()) {
965 ++internal_record_.data_channels_closed;
966 }
967}
968
hboscc555c52016-10-18 12:48:31 -0700969const char* CandidateTypeToRTCIceCandidateTypeForTesting(
970 const std::string& type) {
971 return CandidateTypeToRTCIceCandidateType(type);
972}
973
974const char* DataStateToRTCDataChannelStateForTesting(
975 DataChannelInterface::DataState state) {
976 return DataStateToRTCDataChannelState(state);
977}
978
hbosd565b732016-08-30 14:04:35 -0700979} // namespace webrtc