blob: f1c7ed7e1acb1ae773753d9586c55a3f07c132e8 [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
hbos0adb8282016-11-23 02:32:06 -0800117std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
118 uint64_t timestamp_us, bool inbound, bool audio,
119 const RtpCodecParameters& codec_params) {
120 RTC_DCHECK_GE(codec_params.payload_type, 0);
121 RTC_DCHECK_LE(codec_params.payload_type, 127);
122 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
123 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
124 RTCCodecStatsIDFromDirectionMediaAndPayload(inbound, audio, payload_type),
125 timestamp_us));
126 codec_stats->payload_type = payload_type;
127 codec_stats->codec = (audio ? "audio/" : "video/") + codec_params.mime_type;
128 codec_stats->clock_rate = static_cast<uint32_t>(codec_params.clock_rate);
129 return codec_stats;
130}
131
hbos09bc1282016-11-08 06:29:22 -0800132void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
133 const MediaStreamTrackInterface& track,
134 RTCMediaStreamTrackStats* track_stats) {
135 track_stats->track_identifier = track.id();
136 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
137}
138
hbos820f5782016-11-22 03:16:50 -0800139// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700140void SetInboundRTPStreamStatsFromMediaReceiverInfo(
141 const cricket::MediaReceiverInfo& media_receiver_info,
142 RTCInboundRTPStreamStats* inbound_stats) {
143 RTC_DCHECK(inbound_stats);
144 inbound_stats->ssrc = rtc::ToString<>(media_receiver_info.ssrc());
145 // TODO(hbos): Support the remote case. crbug.com/657855
146 inbound_stats->is_remote = false;
147 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
148 // |media_receiver_info.codec_name|. crbug.com/657854, 657855, 659117
149 inbound_stats->packets_received =
150 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
151 inbound_stats->bytes_received =
152 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800153 inbound_stats->packets_lost =
154 static_cast<uint32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700155 inbound_stats->fraction_lost =
156 static_cast<double>(media_receiver_info.fraction_lost);
157}
158
159void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
160 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800161 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700162 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800163 voice_receiver_info, inbound_audio);
164 inbound_audio->media_type = "audio";
165 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700166 static_cast<double>(voice_receiver_info.jitter_ms) /
167 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800168 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
169 // purposefully left undefined for audio.
hboseeafe942016-11-01 03:00:17 -0700170}
171
172void SetInboundRTPStreamStatsFromVideoReceiverInfo(
173 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800174 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700175 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800176 video_receiver_info, inbound_video);
177 inbound_video->media_type = "video";
178 inbound_video->fir_count =
179 static_cast<uint32_t>(video_receiver_info.firs_sent);
180 inbound_video->pli_count =
181 static_cast<uint32_t>(video_receiver_info.plis_sent);
182 inbound_video->nack_count =
183 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hboseeafe942016-11-01 03:00:17 -0700184}
185
hbos820f5782016-11-22 03:16:50 -0800186// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700187void SetOutboundRTPStreamStatsFromMediaSenderInfo(
188 const cricket::MediaSenderInfo& media_sender_info,
189 RTCOutboundRTPStreamStats* outbound_stats) {
190 RTC_DCHECK(outbound_stats);
191 outbound_stats->ssrc = rtc::ToString<>(media_sender_info.ssrc());
192 // TODO(hbos): Support the remote case. crbug.com/657856
193 outbound_stats->is_remote = false;
194 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
195 // |media_sender_info.codec_name|. crbug.com/657854, 657856, 659117
196 outbound_stats->packets_sent =
197 static_cast<uint32_t>(media_sender_info.packets_sent);
198 outbound_stats->bytes_sent =
199 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbose10e6d12016-12-15 01:54:29 -0800200 if (media_sender_info.rtt_ms >= 0) {
201 outbound_stats->round_trip_time = static_cast<double>(
202 media_sender_info.rtt_ms) / rtc::kNumMillisecsPerSec;
203 }
hbos6ded1902016-11-01 01:50:46 -0700204}
205
206void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
207 const cricket::VoiceSenderInfo& voice_sender_info,
208 RTCOutboundRTPStreamStats* outbound_audio) {
209 SetOutboundRTPStreamStatsFromMediaSenderInfo(
210 voice_sender_info, outbound_audio);
211 outbound_audio->media_type = "audio";
212 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
213 // purposefully left undefined for audio.
214}
215
216void SetOutboundRTPStreamStatsFromVideoSenderInfo(
217 const cricket::VideoSenderInfo& video_sender_info,
218 RTCOutboundRTPStreamStats* outbound_video) {
219 SetOutboundRTPStreamStatsFromMediaSenderInfo(
220 video_sender_info, outbound_video);
221 outbound_video->media_type = "video";
222 outbound_video->fir_count =
223 static_cast<uint32_t>(video_sender_info.firs_rcvd);
224 outbound_video->pli_count =
225 static_cast<uint32_t>(video_sender_info.plis_rcvd);
226 outbound_video->nack_count =
227 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
228}
229
hbos02ba2112016-10-28 05:14:53 -0700230void ProduceCertificateStatsFromSSLCertificateStats(
231 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
232 RTCStatsReport* report) {
233 RTCCertificateStats* prev_certificate_stats = nullptr;
234 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
235 s = s->issuer.get()) {
236 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
237 RTCCertificateIDFromFingerprint(s->fingerprint), timestamp_us);
238 certificate_stats->fingerprint = s->fingerprint;
239 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
240 certificate_stats->base64_certificate = s->base64_certificate;
241 if (prev_certificate_stats)
242 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
243 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
244 prev_certificate_stats = certificate_stats;
245 }
246}
247
248const std::string& ProduceIceCandidateStats(
249 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
250 RTCStatsReport* report) {
251 const std::string& id = "RTCIceCandidate_" + candidate.id();
252 const RTCStats* stats = report->Get(id);
253 if (!stats) {
254 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
255 if (is_local)
256 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
257 else
258 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
259 candidate_stats->ip = candidate.address().ipaddr().ToString();
260 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
261 candidate_stats->protocol = candidate.protocol();
262 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
263 candidate.type());
264 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
265
266 stats = candidate_stats.get();
267 report->AddStats(std::move(candidate_stats));
268 }
269 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
270 : RTCRemoteIceCandidateStats::kType);
271 return stats->id();
272}
273
hbos09bc1282016-11-08 06:29:22 -0800274void ProduceMediaStreamAndTrackStats(
275 int64_t timestamp_us,
276 rtc::scoped_refptr<StreamCollectionInterface> streams,
277 bool is_local,
278 RTCStatsReport* report) {
279 // TODO(hbos): When "AddTrack" is implemented we should iterate tracks to
280 // find which streams exist, not iterate streams to find tracks.
281 // crbug.com/659137
282 // TODO(hbos): Return stats of detached tracks. We have to perform stats
283 // gathering at the time of detachment to get accurate stats and timestamps.
284 // crbug.com/659137
285 if (!streams)
286 return;
287 for (size_t i = 0; i < streams->count(); ++i) {
288 MediaStreamInterface* stream = streams->at(i);
289
290 std::unique_ptr<RTCMediaStreamStats> stream_stats(
291 new RTCMediaStreamStats("RTCMediaStream_" + stream->label(),
292 timestamp_us));
293 stream_stats->stream_identifier = stream->label();
294 stream_stats->track_ids = std::vector<std::string>();
295 // Audio Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800296 for (const rtc::scoped_refptr<AudioTrackInterface>& audio_track :
hbos09bc1282016-11-08 06:29:22 -0800297 stream->GetAudioTracks()) {
298 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
299 *audio_track.get());
300 if (report->Get(id)) {
301 // Skip track, stats already exist for it.
302 continue;
303 }
304 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
305 new RTCMediaStreamTrackStats(id, timestamp_us));
306 stream_stats->track_ids->push_back(audio_track_stats->id());
307 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
308 *audio_track.get(),
309 audio_track_stats.get());
310 audio_track_stats->remote_source = !is_local;
311 audio_track_stats->detached = false;
312 int signal_level;
313 if (audio_track->GetSignalLevel(&signal_level)) {
314 // Convert signal level from [0,32767] int to [0,1] double.
315 RTC_DCHECK_GE(signal_level, 0);
316 RTC_DCHECK_LE(signal_level, 32767);
317 audio_track_stats->audio_level = signal_level / 32767.0;
318 }
319 if (audio_track->GetAudioProcessor()) {
320 AudioProcessorInterface::AudioProcessorStats audio_processor_stats;
321 audio_track->GetAudioProcessor()->GetStats(&audio_processor_stats);
hbos9a394f02016-12-14 07:58:22 -0800322 if (audio_processor_stats.echo_return_loss != -100) {
323 audio_track_stats->echo_return_loss = static_cast<double>(
324 audio_processor_stats.echo_return_loss);
325 }
326 if (audio_processor_stats.echo_return_loss_enhancement != -100) {
327 audio_track_stats->echo_return_loss_enhancement = static_cast<double>(
328 audio_processor_stats.echo_return_loss_enhancement);
329 }
hbos09bc1282016-11-08 06:29:22 -0800330 }
331 report->AddStats(std::move(audio_track_stats));
332 }
333 // Video Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800334 for (const rtc::scoped_refptr<VideoTrackInterface>& video_track :
hbos09bc1282016-11-08 06:29:22 -0800335 stream->GetVideoTracks()) {
336 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
337 *video_track.get());
338 if (report->Get(id)) {
339 // Skip track, stats already exist for it.
340 continue;
341 }
342 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
343 new RTCMediaStreamTrackStats(id, timestamp_us));
344 stream_stats->track_ids->push_back(video_track_stats->id());
345 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
346 *video_track.get(),
347 video_track_stats.get());
348 video_track_stats->remote_source = !is_local;
349 video_track_stats->detached = false;
350 if (video_track->GetSource()) {
351 VideoTrackSourceInterface::Stats video_track_source_stats;
352 if (video_track->GetSource()->GetStats(&video_track_source_stats)) {
353 video_track_stats->frame_width = static_cast<uint32_t>(
354 video_track_source_stats.input_width);
355 video_track_stats->frame_height = static_cast<uint32_t>(
356 video_track_source_stats.input_height);
357 }
358 }
359 report->AddStats(std::move(video_track_stats));
360 }
361 report->AddStats(std::move(stream_stats));
362 }
363}
364
hboscc555c52016-10-18 12:48:31 -0700365} // namespace
366
hbosc82f2e12016-09-05 01:36:50 -0700367rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
368 PeerConnection* pc, int64_t cache_lifetime_us) {
369 return rtc::scoped_refptr<RTCStatsCollector>(
370 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
371}
372
373RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
374 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700375 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -0700376 signaling_thread_(pc->session()->signaling_thread()),
377 worker_thread_(pc->session()->worker_thread()),
378 network_thread_(pc->session()->network_thread()),
379 num_pending_partial_reports_(0),
380 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700381 cache_timestamp_us_(0),
382 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700383 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700384 RTC_DCHECK(signaling_thread_);
385 RTC_DCHECK(worker_thread_);
386 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700387 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbos82ebe022016-11-14 01:41:09 -0800388 pc_->SignalDataChannelCreated.connect(
389 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700390}
391
hbosb78306a2016-12-19 05:06:57 -0800392RTCStatsCollector::~RTCStatsCollector() {
393 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
394}
395
hbosc82f2e12016-09-05 01:36:50 -0700396void RTCStatsCollector::GetStatsReport(
397 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
398 RTC_DCHECK(signaling_thread_->IsCurrent());
399 RTC_DCHECK(callback);
400 callbacks_.push_back(callback);
401
hbos0e6758d2016-08-31 07:57:36 -0700402 // "Now" using a monotonically increasing timer.
403 int64_t cache_now_us = rtc::TimeMicros();
404 if (cached_report_ &&
405 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700406 // We have a fresh cached report to deliver.
407 DeliverCachedReport();
408 } else if (!num_pending_partial_reports_) {
409 // Only start gathering stats if we're not already gathering stats. In the
410 // case of already gathering stats, |callback_| will be invoked when there
411 // are no more pending partial reports.
412
413 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
414 // UTC), in microseconds. The system clock could be modified and is not
415 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700416 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700417
418 num_pending_partial_reports_ = 3;
419 partial_report_timestamp_us_ = cache_now_us;
420 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
421 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread,
422 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosdf6075a2016-12-19 04:58:02 -0800423
424 // TODO(hbos): No stats are gathered by
425 // |ProducePartialResultsOnWorkerThread|, remove it.
hbosc82f2e12016-09-05 01:36:50 -0700426 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_,
427 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread,
428 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosdf6075a2016-12-19 04:58:02 -0800429
430 // Prepare |channel_names_| and |media_info_| for use in
431 // |ProducePartialResultsOnNetworkThread|.
432 channel_name_pairs_.reset(new ChannelNamePairs());
433 if (pc_->session()->voice_channel()) {
434 channel_name_pairs_->voice = rtc::Optional<ChannelNamePair>(
435 ChannelNamePair(pc_->session()->voice_channel()->content_name(),
436 pc_->session()->voice_channel()->transport_name()));
437 }
438 if (pc_->session()->video_channel()) {
439 channel_name_pairs_->video = rtc::Optional<ChannelNamePair>(
440 ChannelNamePair(pc_->session()->video_channel()->content_name(),
441 pc_->session()->video_channel()->transport_name()));
442 }
443 if (pc_->session()->data_channel()) {
444 channel_name_pairs_->data = rtc::Optional<ChannelNamePair>(
445 ChannelNamePair(pc_->session()->data_channel()->content_name(),
446 pc_->session()->data_channel()->transport_name()));
447 }
448 media_info_.reset(PrepareMediaInfo_s().release());
hbosc82f2e12016-09-05 01:36:50 -0700449 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
450 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
451 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbos0e6758d2016-08-31 07:57:36 -0700452 }
hbosd565b732016-08-30 14:04:35 -0700453}
454
455void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700456 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700457 cached_report_ = nullptr;
458}
459
hbosb78306a2016-12-19 05:06:57 -0800460void RTCStatsCollector::WaitForPendingRequest() {
461 RTC_DCHECK(signaling_thread_->IsCurrent());
462 if (num_pending_partial_reports_) {
463 rtc::Thread::Current()->ProcessMessages(0);
464 while (num_pending_partial_reports_) {
465 rtc::Thread::Current()->SleepMs(1);
466 rtc::Thread::Current()->ProcessMessages(0);
467 }
468 }
469}
470
hbosc82f2e12016-09-05 01:36:50 -0700471void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
472 int64_t timestamp_us) {
473 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700474 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
475 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700476
hboscc555c52016-10-18 12:48:31 -0700477 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos09bc1282016-11-08 06:29:22 -0800478 ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700479 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700480
481 AddPartialResults(report);
482}
483
484void RTCStatsCollector::ProducePartialResultsOnWorkerThread(
485 int64_t timestamp_us) {
486 RTC_DCHECK(worker_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700487 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
488 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700489
hbosdf6075a2016-12-19 04:58:02 -0800490 // TODO(hbos): There are no stats to be gathered on this thread, remove this
491 // method.
hbosc82f2e12016-09-05 01:36:50 -0700492
493 AddPartialResults(report);
494}
495
496void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
497 int64_t timestamp_us) {
498 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700499 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
500 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700501
hbosdf6075a2016-12-19 04:58:02 -0800502 std::unique_ptr<SessionStats> session_stats =
503 pc_->session()->GetStats(*channel_name_pairs_);
504 if (session_stats) {
505 std::map<std::string, CertificateStatsPair> transport_cert_stats =
506 PrepareTransportCertificateStats_n(*session_stats);
507
508 ProduceCertificateStats_n(
509 timestamp_us, transport_cert_stats, report.get());
510 ProduceCodecStats_n(
511 timestamp_us, *media_info_, report.get());
512 ProduceIceCandidateAndPairStats_n(
513 timestamp_us, *session_stats, report.get());
514 ProduceRTPStreamStats_n(
515 timestamp_us, *session_stats, *media_info_, report.get());
516 ProduceTransportStats_n(
517 timestamp_us, *session_stats, transport_cert_stats, report.get());
518 }
hbosc82f2e12016-09-05 01:36:50 -0700519
520 AddPartialResults(report);
521}
522
523void RTCStatsCollector::AddPartialResults(
524 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
525 if (!signaling_thread_->IsCurrent()) {
526 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
527 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
528 rtc::scoped_refptr<RTCStatsCollector>(this),
529 partial_report));
530 return;
531 }
532 AddPartialResults_s(partial_report);
533}
534
535void RTCStatsCollector::AddPartialResults_s(
536 rtc::scoped_refptr<RTCStatsReport> partial_report) {
537 RTC_DCHECK(signaling_thread_->IsCurrent());
538 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
539 if (!partial_report_)
540 partial_report_ = partial_report;
541 else
542 partial_report_->TakeMembersFrom(partial_report);
543 --num_pending_partial_reports_;
544 if (!num_pending_partial_reports_) {
545 cache_timestamp_us_ = partial_report_timestamp_us_;
546 cached_report_ = partial_report_;
547 partial_report_ = nullptr;
548 DeliverCachedReport();
549 }
550}
551
552void RTCStatsCollector::DeliverCachedReport() {
553 RTC_DCHECK(signaling_thread_->IsCurrent());
554 RTC_DCHECK(!callbacks_.empty());
555 RTC_DCHECK(cached_report_);
556 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
557 callbacks_) {
558 callback->OnStatsDelivered(cached_report_);
559 }
560 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700561}
562
hbosdf6075a2016-12-19 04:58:02 -0800563void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700564 int64_t timestamp_us,
565 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700566 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800567 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700568 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
569 if (transport_cert_stats_pair.second.local) {
570 ProduceCertificateStatsFromSSLCertificateStats(
571 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700572 }
hbos02ba2112016-10-28 05:14:53 -0700573 if (transport_cert_stats_pair.second.remote) {
574 ProduceCertificateStatsFromSSLCertificateStats(
575 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700576 }
577 }
578}
579
hbosdf6075a2016-12-19 04:58:02 -0800580void RTCStatsCollector::ProduceCodecStats_n(
hbos0adb8282016-11-23 02:32:06 -0800581 int64_t timestamp_us, const MediaInfo& media_info,
582 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800583 RTC_DCHECK(network_thread_->IsCurrent());
hbos0adb8282016-11-23 02:32:06 -0800584 // Audio
585 if (media_info.voice) {
586 // Inbound
587 for (const auto& pair : media_info.voice->receive_codecs) {
588 report->AddStats(CodecStatsFromRtpCodecParameters(
589 timestamp_us, true, true, pair.second));
590 }
591 // Outbound
592 for (const auto& pair : media_info.voice->send_codecs) {
593 report->AddStats(CodecStatsFromRtpCodecParameters(
594 timestamp_us, false, true, pair.second));
595 }
596 }
597 // Video
598 if (media_info.video) {
599 // Inbound
600 for (const auto& pair : media_info.video->receive_codecs) {
601 report->AddStats(CodecStatsFromRtpCodecParameters(
602 timestamp_us, true, false, pair.second));
603 }
604 // Outbound
605 for (const auto& pair : media_info.video->send_codecs) {
606 report->AddStats(CodecStatsFromRtpCodecParameters(
607 timestamp_us, false, false, pair.second));
608 }
609 }
610}
611
hboscc555c52016-10-18 12:48:31 -0700612void RTCStatsCollector::ProduceDataChannelStats_s(
613 int64_t timestamp_us, RTCStatsReport* report) const {
614 RTC_DCHECK(signaling_thread_->IsCurrent());
615 for (const rtc::scoped_refptr<DataChannel>& data_channel :
616 pc_->sctp_data_channels()) {
617 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
618 new RTCDataChannelStats(
619 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
620 timestamp_us));
621 data_channel_stats->label = data_channel->label();
622 data_channel_stats->protocol = data_channel->protocol();
623 data_channel_stats->datachannelid = data_channel->id();
624 data_channel_stats->state =
625 DataStateToRTCDataChannelState(data_channel->state());
626 data_channel_stats->messages_sent = data_channel->messages_sent();
627 data_channel_stats->bytes_sent = data_channel->bytes_sent();
628 data_channel_stats->messages_received = data_channel->messages_received();
629 data_channel_stats->bytes_received = data_channel->bytes_received();
630 report->AddStats(std::move(data_channel_stats));
631 }
632}
633
hbosdf6075a2016-12-19 04:58:02 -0800634void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
hbosab9f6e42016-10-07 02:18:47 -0700635 int64_t timestamp_us, const SessionStats& session_stats,
636 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800637 RTC_DCHECK(network_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700638 for (const auto& transport_stats : session_stats.transport_stats) {
639 for (const auto& channel_stats : transport_stats.second.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800640 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
641 transport_stats.second.transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700642 for (const cricket::ConnectionInfo& info :
643 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700644 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700645 new RTCIceCandidatePairStats(
646 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
647 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700648
hbos0583b282016-11-30 01:50:14 -0800649 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700650 // TODO(hbos): There could be other candidates that are not paired with
651 // anything. We don't have a complete list. Local candidates come from
652 // Port objects, and prflx candidates (both local and remote) are only
653 // stored in candidate pairs. crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700654 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700655 timestamp_us, info.local_candidate, true, report);
hbos02ba2112016-10-28 05:14:53 -0700656 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700657 timestamp_us, info.remote_candidate, false, report);
hbosc47a0c32016-10-11 14:54:49 -0700658 // TODO(hbos): This writable is different than the spec. It goes to
659 // false after a certain amount of time without a response passes.
660 // crbug.com/633550
661 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700662 candidate_pair_stats->bytes_sent =
663 static_cast<uint64_t>(info.sent_total_bytes);
664 candidate_pair_stats->bytes_received =
665 static_cast<uint64_t>(info.recv_total_bytes);
hbosc47a0c32016-10-11 14:54:49 -0700666 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
667 // smoothed according to the spec. crbug.com/633550. See
668 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
hbos3168c7a2016-12-15 06:17:08 -0800669 candidate_pair_stats->current_round_trip_time =
hbos6ded1902016-11-01 01:50:46 -0700670 static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec;
hbosd82f5122016-12-09 04:12:39 -0800671 candidate_pair_stats->requests_received =
672 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -0800673 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
674 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700675 candidate_pair_stats->responses_received =
676 static_cast<uint64_t>(info.recv_ping_responses);
677 candidate_pair_stats->responses_sent =
678 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -0800679 RTC_DCHECK_GE(info.sent_ping_requests_total,
680 info.sent_ping_requests_before_first_response);
681 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
682 info.sent_ping_requests_total -
683 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700684
685 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700686 }
687 }
688 }
689}
690
hbos09bc1282016-11-08 06:29:22 -0800691void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s(
692 int64_t timestamp_us, RTCStatsReport* report) const {
693 RTC_DCHECK(signaling_thread_->IsCurrent());
694 ProduceMediaStreamAndTrackStats(
695 timestamp_us, pc_->local_streams(), true, report);
696 ProduceMediaStreamAndTrackStats(
697 timestamp_us, pc_->remote_streams(), false, report);
698}
699
hbos6ab97ce2016-10-03 14:16:56 -0700700void RTCStatsCollector::ProducePeerConnectionStats_s(
701 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700702 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700703 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700704 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -0800705 stats->data_channels_opened = internal_record_.data_channels_opened;
706 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -0700707 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700708}
709
hbosdf6075a2016-12-19 04:58:02 -0800710void RTCStatsCollector::ProduceRTPStreamStats_n(
hbos6ded1902016-11-01 01:50:46 -0700711 int64_t timestamp_us, const SessionStats& session_stats,
hbos0adb8282016-11-23 02:32:06 -0800712 const MediaInfo& media_info, RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800713 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700714
715 // Audio
hbos0adb8282016-11-23 02:32:06 -0800716 if (media_info.voice) {
717 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
718 session_stats.proxy_to_transport, *pc_->session()->voice_channel());
719 RTC_DCHECK(!transport_id.empty());
720 // Inbound
721 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
722 media_info.voice->receivers) {
723 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
724 // is fixed.
725 if (voice_receiver_info.ssrc() == 0)
726 continue;
727 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
728 new RTCInboundRTPStreamStats(
729 RTCInboundRTPStreamStatsIDFromSSRC(
730 true, voice_receiver_info.ssrc()),
731 timestamp_us));
732 SetInboundRTPStreamStatsFromVoiceReceiverInfo(
733 voice_receiver_info, inbound_audio.get());
734 inbound_audio->transport_id = transport_id;
735 if (voice_receiver_info.codec_payload_type) {
736 inbound_audio->codec_id =
737 RTCCodecStatsIDFromDirectionMediaAndPayload(
738 true, true, *voice_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700739 }
hbos0adb8282016-11-23 02:32:06 -0800740 report->AddStats(std::move(inbound_audio));
741 }
742 // Outbound
743 for (const cricket::VoiceSenderInfo& voice_sender_info :
744 media_info.voice->senders) {
745 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
746 // is fixed.
747 if (voice_sender_info.ssrc() == 0)
748 continue;
749 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
750 new RTCOutboundRTPStreamStats(
751 RTCOutboundRTPStreamStatsIDFromSSRC(
752 true, voice_sender_info.ssrc()),
753 timestamp_us));
754 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
755 voice_sender_info, outbound_audio.get());
756 outbound_audio->transport_id = transport_id;
757 if (voice_sender_info.codec_payload_type) {
758 outbound_audio->codec_id =
759 RTCCodecStatsIDFromDirectionMediaAndPayload(
760 false, true, *voice_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700761 }
hbos0adb8282016-11-23 02:32:06 -0800762 report->AddStats(std::move(outbound_audio));
hbos6ded1902016-11-01 01:50:46 -0700763 }
764 }
765 // Video
hbos0adb8282016-11-23 02:32:06 -0800766 if (media_info.video) {
767 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
768 session_stats.proxy_to_transport, *pc_->session()->video_channel());
769 RTC_DCHECK(!transport_id.empty());
770 // Inbound
771 for (const cricket::VideoReceiverInfo& video_receiver_info :
772 media_info.video->receivers) {
773 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
774 // is fixed.
775 if (video_receiver_info.ssrc() == 0)
776 continue;
777 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
778 new RTCInboundRTPStreamStats(
779 RTCInboundRTPStreamStatsIDFromSSRC(
780 false, video_receiver_info.ssrc()),
781 timestamp_us));
782 SetInboundRTPStreamStatsFromVideoReceiverInfo(
783 video_receiver_info, inbound_video.get());
784 inbound_video->transport_id = transport_id;
785 if (video_receiver_info.codec_payload_type) {
786 inbound_video->codec_id =
787 RTCCodecStatsIDFromDirectionMediaAndPayload(
788 true, false, *video_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700789 }
hbos0adb8282016-11-23 02:32:06 -0800790 report->AddStats(std::move(inbound_video));
791 }
792 // Outbound
793 for (const cricket::VideoSenderInfo& video_sender_info :
794 media_info.video->senders) {
795 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
796 // is fixed.
797 if (video_sender_info.ssrc() == 0)
798 continue;
799 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
800 new RTCOutboundRTPStreamStats(
801 RTCOutboundRTPStreamStatsIDFromSSRC(
802 false, video_sender_info.ssrc()),
803 timestamp_us));
804 SetOutboundRTPStreamStatsFromVideoSenderInfo(
805 video_sender_info, outbound_video.get());
806 outbound_video->transport_id = transport_id;
807 if (video_sender_info.codec_payload_type) {
808 outbound_video->codec_id =
809 RTCCodecStatsIDFromDirectionMediaAndPayload(
810 false, false, *video_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700811 }
hbos0adb8282016-11-23 02:32:06 -0800812 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -0700813 }
814 }
815}
816
hbosdf6075a2016-12-19 04:58:02 -0800817void RTCStatsCollector::ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700818 int64_t timestamp_us, const SessionStats& session_stats,
819 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
820 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800821 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700822 for (const auto& transport : session_stats.transport_stats) {
823 // Get reference to RTCP channel, if it exists.
824 std::string rtcp_transport_stats_id;
825 for (const auto& channel_stats : transport.second.channel_stats) {
826 if (channel_stats.component ==
827 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
828 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
829 transport.second.transport_name, channel_stats.component);
830 break;
831 }
832 }
833
834 // Get reference to local and remote certificates of this transport, if they
835 // exist.
836 const auto& certificate_stats_it = transport_cert_stats.find(
837 transport.second.transport_name);
838 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
839 std::string local_certificate_id;
840 if (certificate_stats_it->second.local) {
841 local_certificate_id = RTCCertificateIDFromFingerprint(
842 certificate_stats_it->second.local->fingerprint);
843 }
844 std::string remote_certificate_id;
845 if (certificate_stats_it->second.remote) {
846 remote_certificate_id = RTCCertificateIDFromFingerprint(
847 certificate_stats_it->second.remote->fingerprint);
848 }
849
850 // There is one transport stats for each channel.
851 for (const auto& channel_stats : transport.second.channel_stats) {
852 std::unique_ptr<RTCTransportStats> transport_stats(
853 new RTCTransportStats(
854 RTCTransportStatsIDFromTransportChannel(
855 transport.second.transport_name, channel_stats.component),
856 timestamp_us));
857 transport_stats->bytes_sent = 0;
858 transport_stats->bytes_received = 0;
859 transport_stats->active_connection = false;
860 for (const cricket::ConnectionInfo& info :
861 channel_stats.connection_infos) {
862 *transport_stats->bytes_sent += info.sent_total_bytes;
863 *transport_stats->bytes_received += info.recv_total_bytes;
864 if (info.best_connection) {
865 transport_stats->active_connection = true;
866 transport_stats->selected_candidate_pair_id =
867 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
868 }
869 }
870 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
871 !rtcp_transport_stats_id.empty()) {
872 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
873 }
874 if (!local_certificate_id.empty())
875 transport_stats->local_certificate_id = local_certificate_id;
876 if (!remote_certificate_id.empty())
877 transport_stats->remote_certificate_id = remote_certificate_id;
878 report->AddStats(std::move(transport_stats));
879 }
880 }
881}
882
883std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -0800884RTCStatsCollector::PrepareTransportCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700885 const SessionStats& session_stats) const {
hbosdf6075a2016-12-19 04:58:02 -0800886 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700887 std::map<std::string, CertificateStatsPair> transport_cert_stats;
888 for (const auto& transport_stats : session_stats.transport_stats) {
889 CertificateStatsPair certificate_stats_pair;
890 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
891 if (pc_->session()->GetLocalCertificate(
892 transport_stats.second.transport_name, &local_certificate)) {
893 certificate_stats_pair.local =
894 local_certificate->ssl_certificate().GetStats();
895 }
896 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
897 pc_->session()->GetRemoteSSLCertificate(
898 transport_stats.second.transport_name);
899 if (remote_certificate) {
900 certificate_stats_pair.remote = remote_certificate->GetStats();
901 }
902 transport_cert_stats.insert(
903 std::make_pair(transport_stats.second.transport_name,
904 std::move(certificate_stats_pair)));
905 }
906 return transport_cert_stats;
907}
908
hbosdf6075a2016-12-19 04:58:02 -0800909std::unique_ptr<RTCStatsCollector::MediaInfo>
910RTCStatsCollector::PrepareMediaInfo_s() const {
911 RTC_DCHECK(signaling_thread_->IsCurrent());
912 std::unique_ptr<MediaInfo> media_info(new MediaInfo());
hbos0adb8282016-11-23 02:32:06 -0800913 if (pc_->session()->voice_channel()) {
914 cricket::VoiceMediaInfo voice_media_info;
915 if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800916 media_info->voice = rtc::Optional<cricket::VoiceMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800917 std::move(voice_media_info));
918 }
919 }
920 if (pc_->session()->video_channel()) {
921 cricket::VideoMediaInfo video_media_info;
922 if (pc_->session()->video_channel()->GetStats(&video_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800923 media_info->video = rtc::Optional<cricket::VideoMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800924 std::move(video_media_info));
925 }
926 }
927 return media_info;
928}
929
hbos82ebe022016-11-14 01:41:09 -0800930void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
931 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
932 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
933}
934
935void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
936 RTC_DCHECK(signaling_thread_->IsCurrent());
937 bool result = internal_record_.opened_data_channels.insert(
938 reinterpret_cast<uintptr_t>(channel)).second;
939 ++internal_record_.data_channels_opened;
940 RTC_DCHECK(result);
941}
942
943void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
944 RTC_DCHECK(signaling_thread_->IsCurrent());
945 // Only channels that have been fully opened (and have increased the
946 // |data_channels_opened_| counter) increase the closed counter.
947 if (internal_record_.opened_data_channels.find(
948 reinterpret_cast<uintptr_t>(channel)) !=
949 internal_record_.opened_data_channels.end()) {
950 ++internal_record_.data_channels_closed;
951 }
952}
953
hboscc555c52016-10-18 12:48:31 -0700954const char* CandidateTypeToRTCIceCandidateTypeForTesting(
955 const std::string& type) {
956 return CandidateTypeToRTCIceCandidateType(type);
957}
958
959const char* DataStateToRTCDataChannelStateForTesting(
960 DataChannelInterface::DataState state) {
961 return DataStateToRTCDataChannelState(state);
962}
963
hbosd565b732016-08-30 14:04:35 -0700964} // namespace webrtc