blob: 85113e69a2e925271ad1c1150a3e02ff62ed9880 [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()) {
hbos02d2a922016-12-21 01:29:05 -0800236 std::string certificate_stats_id =
237 RTCCertificateIDFromFingerprint(s->fingerprint);
238 // It is possible for the same certificate to show up multiple times, e.g.
239 // if local and remote side use the same certificate in a loopback call.
240 // If the report already contains stats for this certificate, skip it.
241 if (report->Get(certificate_stats_id)) {
242 RTC_DCHECK_EQ(s, &certificate_stats);
243 break;
244 }
hbos02ba2112016-10-28 05:14:53 -0700245 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800246 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700247 certificate_stats->fingerprint = s->fingerprint;
248 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
249 certificate_stats->base64_certificate = s->base64_certificate;
250 if (prev_certificate_stats)
251 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
252 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
253 prev_certificate_stats = certificate_stats;
254 }
255}
256
257const std::string& ProduceIceCandidateStats(
258 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
259 RTCStatsReport* report) {
260 const std::string& id = "RTCIceCandidate_" + candidate.id();
261 const RTCStats* stats = report->Get(id);
262 if (!stats) {
263 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
264 if (is_local)
265 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
266 else
267 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
268 candidate_stats->ip = candidate.address().ipaddr().ToString();
269 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
270 candidate_stats->protocol = candidate.protocol();
271 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
272 candidate.type());
273 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
274
275 stats = candidate_stats.get();
276 report->AddStats(std::move(candidate_stats));
277 }
278 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
279 : RTCRemoteIceCandidateStats::kType);
280 return stats->id();
281}
282
hbos09bc1282016-11-08 06:29:22 -0800283void ProduceMediaStreamAndTrackStats(
284 int64_t timestamp_us,
285 rtc::scoped_refptr<StreamCollectionInterface> streams,
286 bool is_local,
287 RTCStatsReport* report) {
288 // TODO(hbos): When "AddTrack" is implemented we should iterate tracks to
289 // find which streams exist, not iterate streams to find tracks.
290 // crbug.com/659137
291 // TODO(hbos): Return stats of detached tracks. We have to perform stats
292 // gathering at the time of detachment to get accurate stats and timestamps.
293 // crbug.com/659137
294 if (!streams)
295 return;
296 for (size_t i = 0; i < streams->count(); ++i) {
297 MediaStreamInterface* stream = streams->at(i);
298
299 std::unique_ptr<RTCMediaStreamStats> stream_stats(
hbos02d2a922016-12-21 01:29:05 -0800300 new RTCMediaStreamStats(
301 (is_local ? "RTCMediaStream_local_" : "RTCMediaStream_remote_") +
302 stream->label(), timestamp_us));
hbos09bc1282016-11-08 06:29:22 -0800303 stream_stats->stream_identifier = stream->label();
304 stream_stats->track_ids = std::vector<std::string>();
305 // Audio Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800306 for (const rtc::scoped_refptr<AudioTrackInterface>& audio_track :
hbos09bc1282016-11-08 06:29:22 -0800307 stream->GetAudioTracks()) {
308 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
309 *audio_track.get());
310 if (report->Get(id)) {
311 // Skip track, stats already exist for it.
312 continue;
313 }
314 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
315 new RTCMediaStreamTrackStats(id, timestamp_us));
316 stream_stats->track_ids->push_back(audio_track_stats->id());
317 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
318 *audio_track.get(),
319 audio_track_stats.get());
320 audio_track_stats->remote_source = !is_local;
321 audio_track_stats->detached = false;
322 int signal_level;
323 if (audio_track->GetSignalLevel(&signal_level)) {
324 // Convert signal level from [0,32767] int to [0,1] double.
325 RTC_DCHECK_GE(signal_level, 0);
326 RTC_DCHECK_LE(signal_level, 32767);
327 audio_track_stats->audio_level = signal_level / 32767.0;
328 }
329 if (audio_track->GetAudioProcessor()) {
330 AudioProcessorInterface::AudioProcessorStats audio_processor_stats;
331 audio_track->GetAudioProcessor()->GetStats(&audio_processor_stats);
hbos9a394f02016-12-14 07:58:22 -0800332 if (audio_processor_stats.echo_return_loss != -100) {
333 audio_track_stats->echo_return_loss = static_cast<double>(
334 audio_processor_stats.echo_return_loss);
335 }
336 if (audio_processor_stats.echo_return_loss_enhancement != -100) {
337 audio_track_stats->echo_return_loss_enhancement = static_cast<double>(
338 audio_processor_stats.echo_return_loss_enhancement);
339 }
hbos09bc1282016-11-08 06:29:22 -0800340 }
341 report->AddStats(std::move(audio_track_stats));
342 }
343 // Video Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800344 for (const rtc::scoped_refptr<VideoTrackInterface>& video_track :
hbos09bc1282016-11-08 06:29:22 -0800345 stream->GetVideoTracks()) {
346 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
347 *video_track.get());
348 if (report->Get(id)) {
349 // Skip track, stats already exist for it.
350 continue;
351 }
352 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
353 new RTCMediaStreamTrackStats(id, timestamp_us));
354 stream_stats->track_ids->push_back(video_track_stats->id());
355 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
356 *video_track.get(),
357 video_track_stats.get());
358 video_track_stats->remote_source = !is_local;
359 video_track_stats->detached = false;
360 if (video_track->GetSource()) {
361 VideoTrackSourceInterface::Stats video_track_source_stats;
362 if (video_track->GetSource()->GetStats(&video_track_source_stats)) {
363 video_track_stats->frame_width = static_cast<uint32_t>(
364 video_track_source_stats.input_width);
365 video_track_stats->frame_height = static_cast<uint32_t>(
366 video_track_source_stats.input_height);
367 }
368 }
369 report->AddStats(std::move(video_track_stats));
370 }
371 report->AddStats(std::move(stream_stats));
372 }
373}
374
hboscc555c52016-10-18 12:48:31 -0700375} // namespace
376
hbosc82f2e12016-09-05 01:36:50 -0700377rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
378 PeerConnection* pc, int64_t cache_lifetime_us) {
379 return rtc::scoped_refptr<RTCStatsCollector>(
380 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
381}
382
383RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
384 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700385 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -0700386 signaling_thread_(pc->session()->signaling_thread()),
387 worker_thread_(pc->session()->worker_thread()),
388 network_thread_(pc->session()->network_thread()),
389 num_pending_partial_reports_(0),
390 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700391 cache_timestamp_us_(0),
392 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700393 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700394 RTC_DCHECK(signaling_thread_);
395 RTC_DCHECK(worker_thread_);
396 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700397 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbos82ebe022016-11-14 01:41:09 -0800398 pc_->SignalDataChannelCreated.connect(
399 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700400}
401
hbosb78306a2016-12-19 05:06:57 -0800402RTCStatsCollector::~RTCStatsCollector() {
403 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
404}
405
hbosc82f2e12016-09-05 01:36:50 -0700406void RTCStatsCollector::GetStatsReport(
407 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
408 RTC_DCHECK(signaling_thread_->IsCurrent());
409 RTC_DCHECK(callback);
410 callbacks_.push_back(callback);
411
hbos0e6758d2016-08-31 07:57:36 -0700412 // "Now" using a monotonically increasing timer.
413 int64_t cache_now_us = rtc::TimeMicros();
414 if (cached_report_ &&
415 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700416 // We have a fresh cached report to deliver.
417 DeliverCachedReport();
418 } else if (!num_pending_partial_reports_) {
419 // Only start gathering stats if we're not already gathering stats. In the
420 // case of already gathering stats, |callback_| will be invoked when there
421 // are no more pending partial reports.
422
423 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
424 // UTC), in microseconds. The system clock could be modified and is not
425 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700426 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700427
hbosf415f8a2017-01-02 04:28:51 -0800428 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700429 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800430
431 // Prepare |channel_names_| and |media_info_| for use in
432 // |ProducePartialResultsOnNetworkThread|.
433 channel_name_pairs_.reset(new ChannelNamePairs());
434 if (pc_->session()->voice_channel()) {
435 channel_name_pairs_->voice = rtc::Optional<ChannelNamePair>(
436 ChannelNamePair(pc_->session()->voice_channel()->content_name(),
437 pc_->session()->voice_channel()->transport_name()));
438 }
439 if (pc_->session()->video_channel()) {
440 channel_name_pairs_->video = rtc::Optional<ChannelNamePair>(
441 ChannelNamePair(pc_->session()->video_channel()->content_name(),
442 pc_->session()->video_channel()->transport_name()));
443 }
444 if (pc_->session()->data_channel()) {
445 channel_name_pairs_->data = rtc::Optional<ChannelNamePair>(
446 ChannelNamePair(pc_->session()->data_channel()->content_name(),
447 pc_->session()->data_channel()->transport_name()));
448 }
449 media_info_.reset(PrepareMediaInfo_s().release());
hbosf415f8a2017-01-02 04:28:51 -0800450
hbosc82f2e12016-09-05 01:36:50 -0700451 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
452 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
453 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800454 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700455 }
hbosd565b732016-08-30 14:04:35 -0700456}
457
458void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700459 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700460 cached_report_ = nullptr;
461}
462
hbosb78306a2016-12-19 05:06:57 -0800463void RTCStatsCollector::WaitForPendingRequest() {
464 RTC_DCHECK(signaling_thread_->IsCurrent());
465 if (num_pending_partial_reports_) {
466 rtc::Thread::Current()->ProcessMessages(0);
467 while (num_pending_partial_reports_) {
468 rtc::Thread::Current()->SleepMs(1);
469 rtc::Thread::Current()->ProcessMessages(0);
470 }
471 }
472}
473
hbosc82f2e12016-09-05 01:36:50 -0700474void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
475 int64_t timestamp_us) {
476 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700477 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
478 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700479
hboscc555c52016-10-18 12:48:31 -0700480 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos09bc1282016-11-08 06:29:22 -0800481 ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700482 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700483
484 AddPartialResults(report);
485}
486
hbosc82f2e12016-09-05 01:36:50 -0700487void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
488 int64_t timestamp_us) {
489 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700490 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
491 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700492
hbosdf6075a2016-12-19 04:58:02 -0800493 std::unique_ptr<SessionStats> session_stats =
494 pc_->session()->GetStats(*channel_name_pairs_);
495 if (session_stats) {
496 std::map<std::string, CertificateStatsPair> transport_cert_stats =
497 PrepareTransportCertificateStats_n(*session_stats);
498
499 ProduceCertificateStats_n(
500 timestamp_us, transport_cert_stats, report.get());
501 ProduceCodecStats_n(
502 timestamp_us, *media_info_, report.get());
503 ProduceIceCandidateAndPairStats_n(
504 timestamp_us, *session_stats, report.get());
505 ProduceRTPStreamStats_n(
506 timestamp_us, *session_stats, *media_info_, report.get());
507 ProduceTransportStats_n(
508 timestamp_us, *session_stats, transport_cert_stats, report.get());
509 }
hbosc82f2e12016-09-05 01:36:50 -0700510
511 AddPartialResults(report);
512}
513
514void RTCStatsCollector::AddPartialResults(
515 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
516 if (!signaling_thread_->IsCurrent()) {
517 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
518 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
519 rtc::scoped_refptr<RTCStatsCollector>(this),
520 partial_report));
521 return;
522 }
523 AddPartialResults_s(partial_report);
524}
525
526void RTCStatsCollector::AddPartialResults_s(
527 rtc::scoped_refptr<RTCStatsReport> partial_report) {
528 RTC_DCHECK(signaling_thread_->IsCurrent());
529 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
530 if (!partial_report_)
531 partial_report_ = partial_report;
532 else
533 partial_report_->TakeMembersFrom(partial_report);
534 --num_pending_partial_reports_;
535 if (!num_pending_partial_reports_) {
536 cache_timestamp_us_ = partial_report_timestamp_us_;
537 cached_report_ = partial_report_;
538 partial_report_ = nullptr;
539 DeliverCachedReport();
540 }
541}
542
543void RTCStatsCollector::DeliverCachedReport() {
544 RTC_DCHECK(signaling_thread_->IsCurrent());
545 RTC_DCHECK(!callbacks_.empty());
546 RTC_DCHECK(cached_report_);
547 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
548 callbacks_) {
549 callback->OnStatsDelivered(cached_report_);
550 }
551 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700552}
553
hbosdf6075a2016-12-19 04:58:02 -0800554void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700555 int64_t timestamp_us,
556 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700557 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800558 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700559 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
560 if (transport_cert_stats_pair.second.local) {
561 ProduceCertificateStatsFromSSLCertificateStats(
562 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700563 }
hbos02ba2112016-10-28 05:14:53 -0700564 if (transport_cert_stats_pair.second.remote) {
565 ProduceCertificateStatsFromSSLCertificateStats(
566 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700567 }
568 }
569}
570
hbosdf6075a2016-12-19 04:58:02 -0800571void RTCStatsCollector::ProduceCodecStats_n(
hbos0adb8282016-11-23 02:32:06 -0800572 int64_t timestamp_us, const MediaInfo& media_info,
573 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800574 RTC_DCHECK(network_thread_->IsCurrent());
hbos0adb8282016-11-23 02:32:06 -0800575 // Audio
576 if (media_info.voice) {
577 // Inbound
578 for (const auto& pair : media_info.voice->receive_codecs) {
579 report->AddStats(CodecStatsFromRtpCodecParameters(
580 timestamp_us, true, true, pair.second));
581 }
582 // Outbound
583 for (const auto& pair : media_info.voice->send_codecs) {
584 report->AddStats(CodecStatsFromRtpCodecParameters(
585 timestamp_us, false, true, pair.second));
586 }
587 }
588 // Video
589 if (media_info.video) {
590 // Inbound
591 for (const auto& pair : media_info.video->receive_codecs) {
592 report->AddStats(CodecStatsFromRtpCodecParameters(
593 timestamp_us, true, false, pair.second));
594 }
595 // Outbound
596 for (const auto& pair : media_info.video->send_codecs) {
597 report->AddStats(CodecStatsFromRtpCodecParameters(
598 timestamp_us, false, false, pair.second));
599 }
600 }
601}
602
hboscc555c52016-10-18 12:48:31 -0700603void RTCStatsCollector::ProduceDataChannelStats_s(
604 int64_t timestamp_us, RTCStatsReport* report) const {
605 RTC_DCHECK(signaling_thread_->IsCurrent());
606 for (const rtc::scoped_refptr<DataChannel>& data_channel :
607 pc_->sctp_data_channels()) {
608 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
609 new RTCDataChannelStats(
610 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
611 timestamp_us));
612 data_channel_stats->label = data_channel->label();
613 data_channel_stats->protocol = data_channel->protocol();
614 data_channel_stats->datachannelid = data_channel->id();
615 data_channel_stats->state =
616 DataStateToRTCDataChannelState(data_channel->state());
617 data_channel_stats->messages_sent = data_channel->messages_sent();
618 data_channel_stats->bytes_sent = data_channel->bytes_sent();
619 data_channel_stats->messages_received = data_channel->messages_received();
620 data_channel_stats->bytes_received = data_channel->bytes_received();
621 report->AddStats(std::move(data_channel_stats));
622 }
623}
624
hbosdf6075a2016-12-19 04:58:02 -0800625void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
hbosab9f6e42016-10-07 02:18:47 -0700626 int64_t timestamp_us, const SessionStats& session_stats,
627 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800628 RTC_DCHECK(network_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700629 for (const auto& transport_stats : session_stats.transport_stats) {
630 for (const auto& channel_stats : transport_stats.second.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800631 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
632 transport_stats.second.transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700633 for (const cricket::ConnectionInfo& info :
634 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700635 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700636 new RTCIceCandidatePairStats(
637 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
638 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700639
hbos0583b282016-11-30 01:50:14 -0800640 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700641 // TODO(hbos): There could be other candidates that are not paired with
642 // anything. We don't have a complete list. Local candidates come from
643 // Port objects, and prflx candidates (both local and remote) are only
644 // stored in candidate pairs. crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700645 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700646 timestamp_us, info.local_candidate, true, report);
hbos02ba2112016-10-28 05:14:53 -0700647 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700648 timestamp_us, info.remote_candidate, false, report);
hbosc47a0c32016-10-11 14:54:49 -0700649 // TODO(hbos): This writable is different than the spec. It goes to
650 // false after a certain amount of time without a response passes.
651 // crbug.com/633550
652 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700653 candidate_pair_stats->bytes_sent =
654 static_cast<uint64_t>(info.sent_total_bytes);
655 candidate_pair_stats->bytes_received =
656 static_cast<uint64_t>(info.recv_total_bytes);
hbosc47a0c32016-10-11 14:54:49 -0700657 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
658 // smoothed according to the spec. crbug.com/633550. See
659 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
hbos3168c7a2016-12-15 06:17:08 -0800660 candidate_pair_stats->current_round_trip_time =
hbos6ded1902016-11-01 01:50:46 -0700661 static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec;
hbosd82f5122016-12-09 04:12:39 -0800662 candidate_pair_stats->requests_received =
663 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -0800664 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
665 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700666 candidate_pair_stats->responses_received =
667 static_cast<uint64_t>(info.recv_ping_responses);
668 candidate_pair_stats->responses_sent =
669 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -0800670 RTC_DCHECK_GE(info.sent_ping_requests_total,
671 info.sent_ping_requests_before_first_response);
672 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
673 info.sent_ping_requests_total -
674 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700675
676 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700677 }
678 }
679 }
680}
681
hbos09bc1282016-11-08 06:29:22 -0800682void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s(
683 int64_t timestamp_us, RTCStatsReport* report) const {
684 RTC_DCHECK(signaling_thread_->IsCurrent());
685 ProduceMediaStreamAndTrackStats(
686 timestamp_us, pc_->local_streams(), true, report);
687 ProduceMediaStreamAndTrackStats(
688 timestamp_us, pc_->remote_streams(), false, report);
689}
690
hbos6ab97ce2016-10-03 14:16:56 -0700691void RTCStatsCollector::ProducePeerConnectionStats_s(
692 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700693 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700694 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700695 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -0800696 stats->data_channels_opened = internal_record_.data_channels_opened;
697 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -0700698 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700699}
700
hbosdf6075a2016-12-19 04:58:02 -0800701void RTCStatsCollector::ProduceRTPStreamStats_n(
hbos6ded1902016-11-01 01:50:46 -0700702 int64_t timestamp_us, const SessionStats& session_stats,
hbos0adb8282016-11-23 02:32:06 -0800703 const MediaInfo& media_info, RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800704 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700705
706 // Audio
hbos0adb8282016-11-23 02:32:06 -0800707 if (media_info.voice) {
708 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
709 session_stats.proxy_to_transport, *pc_->session()->voice_channel());
710 RTC_DCHECK(!transport_id.empty());
711 // Inbound
712 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
713 media_info.voice->receivers) {
714 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
715 // is fixed.
716 if (voice_receiver_info.ssrc() == 0)
717 continue;
718 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
719 new RTCInboundRTPStreamStats(
720 RTCInboundRTPStreamStatsIDFromSSRC(
721 true, voice_receiver_info.ssrc()),
722 timestamp_us));
723 SetInboundRTPStreamStatsFromVoiceReceiverInfo(
724 voice_receiver_info, inbound_audio.get());
725 inbound_audio->transport_id = transport_id;
726 if (voice_receiver_info.codec_payload_type) {
727 inbound_audio->codec_id =
728 RTCCodecStatsIDFromDirectionMediaAndPayload(
729 true, true, *voice_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700730 }
hbos0adb8282016-11-23 02:32:06 -0800731 report->AddStats(std::move(inbound_audio));
732 }
733 // Outbound
734 for (const cricket::VoiceSenderInfo& voice_sender_info :
735 media_info.voice->senders) {
736 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
737 // is fixed.
738 if (voice_sender_info.ssrc() == 0)
739 continue;
740 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
741 new RTCOutboundRTPStreamStats(
742 RTCOutboundRTPStreamStatsIDFromSSRC(
743 true, voice_sender_info.ssrc()),
744 timestamp_us));
745 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
746 voice_sender_info, outbound_audio.get());
747 outbound_audio->transport_id = transport_id;
748 if (voice_sender_info.codec_payload_type) {
749 outbound_audio->codec_id =
750 RTCCodecStatsIDFromDirectionMediaAndPayload(
751 false, true, *voice_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700752 }
hbos0adb8282016-11-23 02:32:06 -0800753 report->AddStats(std::move(outbound_audio));
hbos6ded1902016-11-01 01:50:46 -0700754 }
755 }
756 // Video
hbos0adb8282016-11-23 02:32:06 -0800757 if (media_info.video) {
758 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
759 session_stats.proxy_to_transport, *pc_->session()->video_channel());
760 RTC_DCHECK(!transport_id.empty());
761 // Inbound
762 for (const cricket::VideoReceiverInfo& video_receiver_info :
763 media_info.video->receivers) {
764 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
765 // is fixed.
766 if (video_receiver_info.ssrc() == 0)
767 continue;
768 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
769 new RTCInboundRTPStreamStats(
770 RTCInboundRTPStreamStatsIDFromSSRC(
771 false, video_receiver_info.ssrc()),
772 timestamp_us));
773 SetInboundRTPStreamStatsFromVideoReceiverInfo(
774 video_receiver_info, inbound_video.get());
775 inbound_video->transport_id = transport_id;
776 if (video_receiver_info.codec_payload_type) {
777 inbound_video->codec_id =
778 RTCCodecStatsIDFromDirectionMediaAndPayload(
779 true, false, *video_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700780 }
hbos0adb8282016-11-23 02:32:06 -0800781 report->AddStats(std::move(inbound_video));
782 }
783 // Outbound
784 for (const cricket::VideoSenderInfo& video_sender_info :
785 media_info.video->senders) {
786 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
787 // is fixed.
788 if (video_sender_info.ssrc() == 0)
789 continue;
790 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
791 new RTCOutboundRTPStreamStats(
792 RTCOutboundRTPStreamStatsIDFromSSRC(
793 false, video_sender_info.ssrc()),
794 timestamp_us));
795 SetOutboundRTPStreamStatsFromVideoSenderInfo(
796 video_sender_info, outbound_video.get());
797 outbound_video->transport_id = transport_id;
798 if (video_sender_info.codec_payload_type) {
799 outbound_video->codec_id =
800 RTCCodecStatsIDFromDirectionMediaAndPayload(
801 false, false, *video_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700802 }
hbos0adb8282016-11-23 02:32:06 -0800803 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -0700804 }
805 }
806}
807
hbosdf6075a2016-12-19 04:58:02 -0800808void RTCStatsCollector::ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700809 int64_t timestamp_us, const SessionStats& session_stats,
810 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
811 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800812 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700813 for (const auto& transport : session_stats.transport_stats) {
814 // Get reference to RTCP channel, if it exists.
815 std::string rtcp_transport_stats_id;
816 for (const auto& channel_stats : transport.second.channel_stats) {
817 if (channel_stats.component ==
818 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
819 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
820 transport.second.transport_name, channel_stats.component);
821 break;
822 }
823 }
824
825 // Get reference to local and remote certificates of this transport, if they
826 // exist.
827 const auto& certificate_stats_it = transport_cert_stats.find(
828 transport.second.transport_name);
829 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
830 std::string local_certificate_id;
831 if (certificate_stats_it->second.local) {
832 local_certificate_id = RTCCertificateIDFromFingerprint(
833 certificate_stats_it->second.local->fingerprint);
834 }
835 std::string remote_certificate_id;
836 if (certificate_stats_it->second.remote) {
837 remote_certificate_id = RTCCertificateIDFromFingerprint(
838 certificate_stats_it->second.remote->fingerprint);
839 }
840
841 // There is one transport stats for each channel.
842 for (const auto& channel_stats : transport.second.channel_stats) {
843 std::unique_ptr<RTCTransportStats> transport_stats(
844 new RTCTransportStats(
845 RTCTransportStatsIDFromTransportChannel(
846 transport.second.transport_name, channel_stats.component),
847 timestamp_us));
848 transport_stats->bytes_sent = 0;
849 transport_stats->bytes_received = 0;
850 transport_stats->active_connection = false;
851 for (const cricket::ConnectionInfo& info :
852 channel_stats.connection_infos) {
853 *transport_stats->bytes_sent += info.sent_total_bytes;
854 *transport_stats->bytes_received += info.recv_total_bytes;
855 if (info.best_connection) {
856 transport_stats->active_connection = true;
857 transport_stats->selected_candidate_pair_id =
858 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
859 }
860 }
861 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
862 !rtcp_transport_stats_id.empty()) {
863 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
864 }
865 if (!local_certificate_id.empty())
866 transport_stats->local_certificate_id = local_certificate_id;
867 if (!remote_certificate_id.empty())
868 transport_stats->remote_certificate_id = remote_certificate_id;
869 report->AddStats(std::move(transport_stats));
870 }
871 }
872}
873
874std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -0800875RTCStatsCollector::PrepareTransportCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700876 const SessionStats& session_stats) const {
hbosdf6075a2016-12-19 04:58:02 -0800877 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700878 std::map<std::string, CertificateStatsPair> transport_cert_stats;
879 for (const auto& transport_stats : session_stats.transport_stats) {
880 CertificateStatsPair certificate_stats_pair;
881 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
882 if (pc_->session()->GetLocalCertificate(
883 transport_stats.second.transport_name, &local_certificate)) {
884 certificate_stats_pair.local =
885 local_certificate->ssl_certificate().GetStats();
886 }
887 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
888 pc_->session()->GetRemoteSSLCertificate(
889 transport_stats.second.transport_name);
890 if (remote_certificate) {
891 certificate_stats_pair.remote = remote_certificate->GetStats();
892 }
893 transport_cert_stats.insert(
894 std::make_pair(transport_stats.second.transport_name,
895 std::move(certificate_stats_pair)));
896 }
897 return transport_cert_stats;
898}
899
hbosdf6075a2016-12-19 04:58:02 -0800900std::unique_ptr<RTCStatsCollector::MediaInfo>
901RTCStatsCollector::PrepareMediaInfo_s() const {
902 RTC_DCHECK(signaling_thread_->IsCurrent());
903 std::unique_ptr<MediaInfo> media_info(new MediaInfo());
hbos0adb8282016-11-23 02:32:06 -0800904 if (pc_->session()->voice_channel()) {
905 cricket::VoiceMediaInfo voice_media_info;
906 if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800907 media_info->voice = rtc::Optional<cricket::VoiceMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800908 std::move(voice_media_info));
909 }
910 }
911 if (pc_->session()->video_channel()) {
912 cricket::VideoMediaInfo video_media_info;
913 if (pc_->session()->video_channel()->GetStats(&video_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800914 media_info->video = rtc::Optional<cricket::VideoMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800915 std::move(video_media_info));
916 }
917 }
918 return media_info;
919}
920
hbos82ebe022016-11-14 01:41:09 -0800921void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
922 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
923 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
924}
925
926void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
927 RTC_DCHECK(signaling_thread_->IsCurrent());
928 bool result = internal_record_.opened_data_channels.insert(
929 reinterpret_cast<uintptr_t>(channel)).second;
930 ++internal_record_.data_channels_opened;
931 RTC_DCHECK(result);
932}
933
934void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
935 RTC_DCHECK(signaling_thread_->IsCurrent());
936 // Only channels that have been fully opened (and have increased the
937 // |data_channels_opened_| counter) increase the closed counter.
938 if (internal_record_.opened_data_channels.find(
939 reinterpret_cast<uintptr_t>(channel)) !=
940 internal_record_.opened_data_channels.end()) {
941 ++internal_record_.data_channels_closed;
942 }
943}
944
hboscc555c52016-10-18 12:48:31 -0700945const char* CandidateTypeToRTCIceCandidateTypeForTesting(
946 const std::string& type) {
947 return CandidateTypeToRTCIceCandidateType(type);
948}
949
950const char* DataStateToRTCDataChannelStateForTesting(
951 DataChannelInterface::DataState state) {
952 return DataStateToRTCDataChannelState(state);
953}
954
hbosd565b732016-08-30 14:04:35 -0700955} // namespace webrtc