blob: 5289c4820b916be021e1c23f34aab3cd75f562d7 [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);
200 outbound_stats->round_trip_time =
201 static_cast<double>(media_sender_info.rtt_ms) / rtc::kNumMillisecsPerSec;
202}
203
204void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
205 const cricket::VoiceSenderInfo& voice_sender_info,
206 RTCOutboundRTPStreamStats* outbound_audio) {
207 SetOutboundRTPStreamStatsFromMediaSenderInfo(
208 voice_sender_info, outbound_audio);
209 outbound_audio->media_type = "audio";
210 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
211 // purposefully left undefined for audio.
212}
213
214void SetOutboundRTPStreamStatsFromVideoSenderInfo(
215 const cricket::VideoSenderInfo& video_sender_info,
216 RTCOutboundRTPStreamStats* outbound_video) {
217 SetOutboundRTPStreamStatsFromMediaSenderInfo(
218 video_sender_info, outbound_video);
219 outbound_video->media_type = "video";
220 outbound_video->fir_count =
221 static_cast<uint32_t>(video_sender_info.firs_rcvd);
222 outbound_video->pli_count =
223 static_cast<uint32_t>(video_sender_info.plis_rcvd);
224 outbound_video->nack_count =
225 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
226}
227
hbos02ba2112016-10-28 05:14:53 -0700228void ProduceCertificateStatsFromSSLCertificateStats(
229 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
230 RTCStatsReport* report) {
231 RTCCertificateStats* prev_certificate_stats = nullptr;
232 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
233 s = s->issuer.get()) {
234 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
235 RTCCertificateIDFromFingerprint(s->fingerprint), timestamp_us);
236 certificate_stats->fingerprint = s->fingerprint;
237 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
238 certificate_stats->base64_certificate = s->base64_certificate;
239 if (prev_certificate_stats)
240 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
241 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
242 prev_certificate_stats = certificate_stats;
243 }
244}
245
246const std::string& ProduceIceCandidateStats(
247 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
248 RTCStatsReport* report) {
249 const std::string& id = "RTCIceCandidate_" + candidate.id();
250 const RTCStats* stats = report->Get(id);
251 if (!stats) {
252 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
253 if (is_local)
254 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
255 else
256 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
257 candidate_stats->ip = candidate.address().ipaddr().ToString();
258 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
259 candidate_stats->protocol = candidate.protocol();
260 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
261 candidate.type());
262 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
263
264 stats = candidate_stats.get();
265 report->AddStats(std::move(candidate_stats));
266 }
267 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
268 : RTCRemoteIceCandidateStats::kType);
269 return stats->id();
270}
271
hbos09bc1282016-11-08 06:29:22 -0800272void ProduceMediaStreamAndTrackStats(
273 int64_t timestamp_us,
274 rtc::scoped_refptr<StreamCollectionInterface> streams,
275 bool is_local,
276 RTCStatsReport* report) {
277 // TODO(hbos): When "AddTrack" is implemented we should iterate tracks to
278 // find which streams exist, not iterate streams to find tracks.
279 // crbug.com/659137
280 // TODO(hbos): Return stats of detached tracks. We have to perform stats
281 // gathering at the time of detachment to get accurate stats and timestamps.
282 // crbug.com/659137
283 if (!streams)
284 return;
285 for (size_t i = 0; i < streams->count(); ++i) {
286 MediaStreamInterface* stream = streams->at(i);
287
288 std::unique_ptr<RTCMediaStreamStats> stream_stats(
289 new RTCMediaStreamStats("RTCMediaStream_" + stream->label(),
290 timestamp_us));
291 stream_stats->stream_identifier = stream->label();
292 stream_stats->track_ids = std::vector<std::string>();
293 // Audio Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800294 for (const rtc::scoped_refptr<AudioTrackInterface>& audio_track :
hbos09bc1282016-11-08 06:29:22 -0800295 stream->GetAudioTracks()) {
296 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
297 *audio_track.get());
298 if (report->Get(id)) {
299 // Skip track, stats already exist for it.
300 continue;
301 }
302 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
303 new RTCMediaStreamTrackStats(id, timestamp_us));
304 stream_stats->track_ids->push_back(audio_track_stats->id());
305 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
306 *audio_track.get(),
307 audio_track_stats.get());
308 audio_track_stats->remote_source = !is_local;
309 audio_track_stats->detached = false;
310 int signal_level;
311 if (audio_track->GetSignalLevel(&signal_level)) {
312 // Convert signal level from [0,32767] int to [0,1] double.
313 RTC_DCHECK_GE(signal_level, 0);
314 RTC_DCHECK_LE(signal_level, 32767);
315 audio_track_stats->audio_level = signal_level / 32767.0;
316 }
317 if (audio_track->GetAudioProcessor()) {
318 AudioProcessorInterface::AudioProcessorStats audio_processor_stats;
319 audio_track->GetAudioProcessor()->GetStats(&audio_processor_stats);
hbos9a394f02016-12-14 07:58:22 -0800320 if (audio_processor_stats.echo_return_loss != -100) {
321 audio_track_stats->echo_return_loss = static_cast<double>(
322 audio_processor_stats.echo_return_loss);
323 }
324 if (audio_processor_stats.echo_return_loss_enhancement != -100) {
325 audio_track_stats->echo_return_loss_enhancement = static_cast<double>(
326 audio_processor_stats.echo_return_loss_enhancement);
327 }
hbos09bc1282016-11-08 06:29:22 -0800328 }
329 report->AddStats(std::move(audio_track_stats));
330 }
331 // Video Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800332 for (const rtc::scoped_refptr<VideoTrackInterface>& video_track :
hbos09bc1282016-11-08 06:29:22 -0800333 stream->GetVideoTracks()) {
334 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
335 *video_track.get());
336 if (report->Get(id)) {
337 // Skip track, stats already exist for it.
338 continue;
339 }
340 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
341 new RTCMediaStreamTrackStats(id, timestamp_us));
342 stream_stats->track_ids->push_back(video_track_stats->id());
343 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
344 *video_track.get(),
345 video_track_stats.get());
346 video_track_stats->remote_source = !is_local;
347 video_track_stats->detached = false;
348 if (video_track->GetSource()) {
349 VideoTrackSourceInterface::Stats video_track_source_stats;
350 if (video_track->GetSource()->GetStats(&video_track_source_stats)) {
351 video_track_stats->frame_width = static_cast<uint32_t>(
352 video_track_source_stats.input_width);
353 video_track_stats->frame_height = static_cast<uint32_t>(
354 video_track_source_stats.input_height);
355 }
356 }
357 report->AddStats(std::move(video_track_stats));
358 }
359 report->AddStats(std::move(stream_stats));
360 }
361}
362
hboscc555c52016-10-18 12:48:31 -0700363} // namespace
364
hbosc82f2e12016-09-05 01:36:50 -0700365rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
366 PeerConnection* pc, int64_t cache_lifetime_us) {
367 return rtc::scoped_refptr<RTCStatsCollector>(
368 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
369}
370
371RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
372 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700373 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -0700374 signaling_thread_(pc->session()->signaling_thread()),
375 worker_thread_(pc->session()->worker_thread()),
376 network_thread_(pc->session()->network_thread()),
377 num_pending_partial_reports_(0),
378 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700379 cache_timestamp_us_(0),
380 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700381 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700382 RTC_DCHECK(signaling_thread_);
383 RTC_DCHECK(worker_thread_);
384 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700385 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbos82ebe022016-11-14 01:41:09 -0800386 pc_->SignalDataChannelCreated.connect(
387 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700388}
389
hbosc82f2e12016-09-05 01:36:50 -0700390void RTCStatsCollector::GetStatsReport(
391 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
392 RTC_DCHECK(signaling_thread_->IsCurrent());
393 RTC_DCHECK(callback);
394 callbacks_.push_back(callback);
395
hbos0e6758d2016-08-31 07:57:36 -0700396 // "Now" using a monotonically increasing timer.
397 int64_t cache_now_us = rtc::TimeMicros();
398 if (cached_report_ &&
399 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700400 // We have a fresh cached report to deliver.
401 DeliverCachedReport();
402 } else if (!num_pending_partial_reports_) {
403 // Only start gathering stats if we're not already gathering stats. In the
404 // case of already gathering stats, |callback_| will be invoked when there
405 // are no more pending partial reports.
406
407 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
408 // UTC), in microseconds. The system clock could be modified and is not
409 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700410 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700411
412 num_pending_partial_reports_ = 3;
413 partial_report_timestamp_us_ = cache_now_us;
414 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
415 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread,
416 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
417 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_,
418 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread,
419 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
420 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
421 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
422 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbos0e6758d2016-08-31 07:57:36 -0700423 }
hbosd565b732016-08-30 14:04:35 -0700424}
425
426void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700427 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700428 cached_report_ = nullptr;
429}
430
hbosc82f2e12016-09-05 01:36:50 -0700431void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
432 int64_t timestamp_us) {
433 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700434 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
435 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700436
hbos6ab97ce2016-10-03 14:16:56 -0700437 SessionStats session_stats;
438 if (pc_->session()->GetTransportStats(&session_stats)) {
hbos2fa7c672016-10-24 04:00:05 -0700439 std::map<std::string, CertificateStatsPair> transport_cert_stats =
hbos0adb8282016-11-23 02:32:06 -0800440 PrepareTransportCertificateStats(session_stats);
441 MediaInfo media_info = PrepareMediaInfo(session_stats);
hbos2fa7c672016-10-24 04:00:05 -0700442
443 ProduceCertificateStats_s(
444 timestamp_us, transport_cert_stats, report.get());
hbos0adb8282016-11-23 02:32:06 -0800445 ProduceCodecStats_s(
446 timestamp_us, media_info, report.get());
hbos2fa7c672016-10-24 04:00:05 -0700447 ProduceIceCandidateAndPairStats_s(
448 timestamp_us, session_stats, report.get());
hbos6ded1902016-11-01 01:50:46 -0700449 ProduceRTPStreamStats_s(
hbos0adb8282016-11-23 02:32:06 -0800450 timestamp_us, session_stats, media_info, report.get());
hbos2fa7c672016-10-24 04:00:05 -0700451 ProduceTransportStats_s(
452 timestamp_us, session_stats, transport_cert_stats, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700453 }
hboscc555c52016-10-18 12:48:31 -0700454 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos09bc1282016-11-08 06:29:22 -0800455 ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700456 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700457
458 AddPartialResults(report);
459}
460
461void RTCStatsCollector::ProducePartialResultsOnWorkerThread(
462 int64_t timestamp_us) {
463 RTC_DCHECK(worker_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700464 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
465 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700466
467 // TODO(hbos): Gather stats on worker thread.
hbos6ded1902016-11-01 01:50:46 -0700468 // pc_->session()'s channels are owned by the signaling thread but there are
469 // some stats that are gathered on the worker thread. Instead of a synchronous
470 // invoke on "s->w" we could to the "w" work here asynchronously if it wasn't
471 // for the ownership issue. Synchronous invokes in other places makes it
472 // difficult to introduce locks without introducing deadlocks and the channels
473 // are not reference counted.
hbosc82f2e12016-09-05 01:36:50 -0700474
475 AddPartialResults(report);
476}
477
478void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
479 int64_t timestamp_us) {
480 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700481 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
482 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700483
484 // TODO(hbos): Gather stats on network thread.
hbos6ded1902016-11-01 01:50:46 -0700485 // pc_->session()'s channels are owned by the signaling thread but there are
486 // some stats that are gathered on the network thread. Instead of a
487 // synchronous invoke on "s->n" we could to the "n" work here asynchronously
488 // if it wasn't for the ownership issue. Synchronous invokes in other places
489 // makes it difficult to introduce locks without introducing deadlocks and the
490 // channels are not reference counted.
hbosc82f2e12016-09-05 01:36:50 -0700491
492 AddPartialResults(report);
493}
494
495void RTCStatsCollector::AddPartialResults(
496 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
497 if (!signaling_thread_->IsCurrent()) {
498 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
499 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
500 rtc::scoped_refptr<RTCStatsCollector>(this),
501 partial_report));
502 return;
503 }
504 AddPartialResults_s(partial_report);
505}
506
507void RTCStatsCollector::AddPartialResults_s(
508 rtc::scoped_refptr<RTCStatsReport> partial_report) {
509 RTC_DCHECK(signaling_thread_->IsCurrent());
510 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
511 if (!partial_report_)
512 partial_report_ = partial_report;
513 else
514 partial_report_->TakeMembersFrom(partial_report);
515 --num_pending_partial_reports_;
516 if (!num_pending_partial_reports_) {
517 cache_timestamp_us_ = partial_report_timestamp_us_;
518 cached_report_ = partial_report_;
519 partial_report_ = nullptr;
520 DeliverCachedReport();
521 }
522}
523
524void RTCStatsCollector::DeliverCachedReport() {
525 RTC_DCHECK(signaling_thread_->IsCurrent());
526 RTC_DCHECK(!callbacks_.empty());
527 RTC_DCHECK(cached_report_);
528 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
529 callbacks_) {
530 callback->OnStatsDelivered(cached_report_);
531 }
532 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700533}
534
hbos6ab97ce2016-10-03 14:16:56 -0700535void RTCStatsCollector::ProduceCertificateStats_s(
hbos2fa7c672016-10-24 04:00:05 -0700536 int64_t timestamp_us,
537 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700538 RTCStatsReport* report) const {
539 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700540 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
541 if (transport_cert_stats_pair.second.local) {
542 ProduceCertificateStatsFromSSLCertificateStats(
543 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700544 }
hbos02ba2112016-10-28 05:14:53 -0700545 if (transport_cert_stats_pair.second.remote) {
546 ProduceCertificateStatsFromSSLCertificateStats(
547 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700548 }
549 }
550}
551
hbos0adb8282016-11-23 02:32:06 -0800552void RTCStatsCollector::ProduceCodecStats_s(
553 int64_t timestamp_us, const MediaInfo& media_info,
554 RTCStatsReport* report) const {
555 RTC_DCHECK(signaling_thread_->IsCurrent());
556 // Audio
557 if (media_info.voice) {
558 // Inbound
559 for (const auto& pair : media_info.voice->receive_codecs) {
560 report->AddStats(CodecStatsFromRtpCodecParameters(
561 timestamp_us, true, true, pair.second));
562 }
563 // Outbound
564 for (const auto& pair : media_info.voice->send_codecs) {
565 report->AddStats(CodecStatsFromRtpCodecParameters(
566 timestamp_us, false, true, pair.second));
567 }
568 }
569 // Video
570 if (media_info.video) {
571 // Inbound
572 for (const auto& pair : media_info.video->receive_codecs) {
573 report->AddStats(CodecStatsFromRtpCodecParameters(
574 timestamp_us, true, false, pair.second));
575 }
576 // Outbound
577 for (const auto& pair : media_info.video->send_codecs) {
578 report->AddStats(CodecStatsFromRtpCodecParameters(
579 timestamp_us, false, false, pair.second));
580 }
581 }
582}
583
hboscc555c52016-10-18 12:48:31 -0700584void RTCStatsCollector::ProduceDataChannelStats_s(
585 int64_t timestamp_us, RTCStatsReport* report) const {
586 RTC_DCHECK(signaling_thread_->IsCurrent());
587 for (const rtc::scoped_refptr<DataChannel>& data_channel :
588 pc_->sctp_data_channels()) {
589 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
590 new RTCDataChannelStats(
591 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
592 timestamp_us));
593 data_channel_stats->label = data_channel->label();
594 data_channel_stats->protocol = data_channel->protocol();
595 data_channel_stats->datachannelid = data_channel->id();
596 data_channel_stats->state =
597 DataStateToRTCDataChannelState(data_channel->state());
598 data_channel_stats->messages_sent = data_channel->messages_sent();
599 data_channel_stats->bytes_sent = data_channel->bytes_sent();
600 data_channel_stats->messages_received = data_channel->messages_received();
601 data_channel_stats->bytes_received = data_channel->bytes_received();
602 report->AddStats(std::move(data_channel_stats));
603 }
604}
605
hbosab9f6e42016-10-07 02:18:47 -0700606void RTCStatsCollector::ProduceIceCandidateAndPairStats_s(
607 int64_t timestamp_us, const SessionStats& session_stats,
608 RTCStatsReport* report) const {
609 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700610 for (const auto& transport_stats : session_stats.transport_stats) {
611 for (const auto& channel_stats : transport_stats.second.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800612 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
613 transport_stats.second.transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700614 for (const cricket::ConnectionInfo& info :
615 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700616 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700617 new RTCIceCandidatePairStats(
618 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
619 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700620
hbos0583b282016-11-30 01:50:14 -0800621 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700622 // TODO(hbos): There could be other candidates that are not paired with
623 // anything. We don't have a complete list. Local candidates come from
624 // Port objects, and prflx candidates (both local and remote) are only
625 // stored in candidate pairs. crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700626 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700627 timestamp_us, info.local_candidate, true, report);
hbos02ba2112016-10-28 05:14:53 -0700628 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700629 timestamp_us, info.remote_candidate, false, report);
hbosc47a0c32016-10-11 14:54:49 -0700630 // TODO(hbos): This writable is different than the spec. It goes to
631 // false after a certain amount of time without a response passes.
632 // crbug.com/633550
633 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700634 candidate_pair_stats->bytes_sent =
635 static_cast<uint64_t>(info.sent_total_bytes);
636 candidate_pair_stats->bytes_received =
637 static_cast<uint64_t>(info.recv_total_bytes);
hbosc47a0c32016-10-11 14:54:49 -0700638 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
639 // smoothed according to the spec. crbug.com/633550. See
640 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
641 candidate_pair_stats->current_rtt =
hbos6ded1902016-11-01 01:50:46 -0700642 static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec;
hbosd82f5122016-12-09 04:12:39 -0800643 candidate_pair_stats->requests_received =
644 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -0800645 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
646 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700647 candidate_pair_stats->responses_received =
648 static_cast<uint64_t>(info.recv_ping_responses);
649 candidate_pair_stats->responses_sent =
650 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -0800651 RTC_DCHECK_GE(info.sent_ping_requests_total,
652 info.sent_ping_requests_before_first_response);
653 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
654 info.sent_ping_requests_total -
655 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700656
657 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700658 }
659 }
660 }
661}
662
hbos09bc1282016-11-08 06:29:22 -0800663void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s(
664 int64_t timestamp_us, RTCStatsReport* report) const {
665 RTC_DCHECK(signaling_thread_->IsCurrent());
666 ProduceMediaStreamAndTrackStats(
667 timestamp_us, pc_->local_streams(), true, report);
668 ProduceMediaStreamAndTrackStats(
669 timestamp_us, pc_->remote_streams(), false, report);
670}
671
hbos6ab97ce2016-10-03 14:16:56 -0700672void RTCStatsCollector::ProducePeerConnectionStats_s(
673 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700674 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700675 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700676 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -0800677 stats->data_channels_opened = internal_record_.data_channels_opened;
678 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -0700679 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700680}
681
hbos6ded1902016-11-01 01:50:46 -0700682void RTCStatsCollector::ProduceRTPStreamStats_s(
683 int64_t timestamp_us, const SessionStats& session_stats,
hbos0adb8282016-11-23 02:32:06 -0800684 const MediaInfo& media_info, RTCStatsReport* report) const {
hbos6ded1902016-11-01 01:50:46 -0700685 RTC_DCHECK(signaling_thread_->IsCurrent());
686
687 // Audio
hbos0adb8282016-11-23 02:32:06 -0800688 if (media_info.voice) {
689 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
690 session_stats.proxy_to_transport, *pc_->session()->voice_channel());
691 RTC_DCHECK(!transport_id.empty());
692 // Inbound
693 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
694 media_info.voice->receivers) {
695 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
696 // is fixed.
697 if (voice_receiver_info.ssrc() == 0)
698 continue;
699 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
700 new RTCInboundRTPStreamStats(
701 RTCInboundRTPStreamStatsIDFromSSRC(
702 true, voice_receiver_info.ssrc()),
703 timestamp_us));
704 SetInboundRTPStreamStatsFromVoiceReceiverInfo(
705 voice_receiver_info, inbound_audio.get());
706 inbound_audio->transport_id = transport_id;
707 if (voice_receiver_info.codec_payload_type) {
708 inbound_audio->codec_id =
709 RTCCodecStatsIDFromDirectionMediaAndPayload(
710 true, true, *voice_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700711 }
hbos0adb8282016-11-23 02:32:06 -0800712 report->AddStats(std::move(inbound_audio));
713 }
714 // Outbound
715 for (const cricket::VoiceSenderInfo& voice_sender_info :
716 media_info.voice->senders) {
717 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
718 // is fixed.
719 if (voice_sender_info.ssrc() == 0)
720 continue;
721 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
722 new RTCOutboundRTPStreamStats(
723 RTCOutboundRTPStreamStatsIDFromSSRC(
724 true, voice_sender_info.ssrc()),
725 timestamp_us));
726 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
727 voice_sender_info, outbound_audio.get());
728 outbound_audio->transport_id = transport_id;
729 if (voice_sender_info.codec_payload_type) {
730 outbound_audio->codec_id =
731 RTCCodecStatsIDFromDirectionMediaAndPayload(
732 false, true, *voice_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700733 }
hbos0adb8282016-11-23 02:32:06 -0800734 report->AddStats(std::move(outbound_audio));
hbos6ded1902016-11-01 01:50:46 -0700735 }
736 }
737 // Video
hbos0adb8282016-11-23 02:32:06 -0800738 if (media_info.video) {
739 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
740 session_stats.proxy_to_transport, *pc_->session()->video_channel());
741 RTC_DCHECK(!transport_id.empty());
742 // Inbound
743 for (const cricket::VideoReceiverInfo& video_receiver_info :
744 media_info.video->receivers) {
745 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
746 // is fixed.
747 if (video_receiver_info.ssrc() == 0)
748 continue;
749 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
750 new RTCInboundRTPStreamStats(
751 RTCInboundRTPStreamStatsIDFromSSRC(
752 false, video_receiver_info.ssrc()),
753 timestamp_us));
754 SetInboundRTPStreamStatsFromVideoReceiverInfo(
755 video_receiver_info, inbound_video.get());
756 inbound_video->transport_id = transport_id;
757 if (video_receiver_info.codec_payload_type) {
758 inbound_video->codec_id =
759 RTCCodecStatsIDFromDirectionMediaAndPayload(
760 true, false, *video_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700761 }
hbos0adb8282016-11-23 02:32:06 -0800762 report->AddStats(std::move(inbound_video));
763 }
764 // Outbound
765 for (const cricket::VideoSenderInfo& video_sender_info :
766 media_info.video->senders) {
767 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
768 // is fixed.
769 if (video_sender_info.ssrc() == 0)
770 continue;
771 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
772 new RTCOutboundRTPStreamStats(
773 RTCOutboundRTPStreamStatsIDFromSSRC(
774 false, video_sender_info.ssrc()),
775 timestamp_us));
776 SetOutboundRTPStreamStatsFromVideoSenderInfo(
777 video_sender_info, outbound_video.get());
778 outbound_video->transport_id = transport_id;
779 if (video_sender_info.codec_payload_type) {
780 outbound_video->codec_id =
781 RTCCodecStatsIDFromDirectionMediaAndPayload(
782 false, false, *video_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700783 }
hbos0adb8282016-11-23 02:32:06 -0800784 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -0700785 }
786 }
787}
788
hbos2fa7c672016-10-24 04:00:05 -0700789void RTCStatsCollector::ProduceTransportStats_s(
790 int64_t timestamp_us, const SessionStats& session_stats,
791 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
792 RTCStatsReport* report) const {
793 RTC_DCHECK(signaling_thread_->IsCurrent());
794 for (const auto& transport : session_stats.transport_stats) {
795 // Get reference to RTCP channel, if it exists.
796 std::string rtcp_transport_stats_id;
797 for (const auto& channel_stats : transport.second.channel_stats) {
798 if (channel_stats.component ==
799 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
800 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
801 transport.second.transport_name, channel_stats.component);
802 break;
803 }
804 }
805
806 // Get reference to local and remote certificates of this transport, if they
807 // exist.
808 const auto& certificate_stats_it = transport_cert_stats.find(
809 transport.second.transport_name);
810 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
811 std::string local_certificate_id;
812 if (certificate_stats_it->second.local) {
813 local_certificate_id = RTCCertificateIDFromFingerprint(
814 certificate_stats_it->second.local->fingerprint);
815 }
816 std::string remote_certificate_id;
817 if (certificate_stats_it->second.remote) {
818 remote_certificate_id = RTCCertificateIDFromFingerprint(
819 certificate_stats_it->second.remote->fingerprint);
820 }
821
822 // There is one transport stats for each channel.
823 for (const auto& channel_stats : transport.second.channel_stats) {
824 std::unique_ptr<RTCTransportStats> transport_stats(
825 new RTCTransportStats(
826 RTCTransportStatsIDFromTransportChannel(
827 transport.second.transport_name, channel_stats.component),
828 timestamp_us));
829 transport_stats->bytes_sent = 0;
830 transport_stats->bytes_received = 0;
831 transport_stats->active_connection = false;
832 for (const cricket::ConnectionInfo& info :
833 channel_stats.connection_infos) {
834 *transport_stats->bytes_sent += info.sent_total_bytes;
835 *transport_stats->bytes_received += info.recv_total_bytes;
836 if (info.best_connection) {
837 transport_stats->active_connection = true;
838 transport_stats->selected_candidate_pair_id =
839 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
840 }
841 }
842 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
843 !rtcp_transport_stats_id.empty()) {
844 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
845 }
846 if (!local_certificate_id.empty())
847 transport_stats->local_certificate_id = local_certificate_id;
848 if (!remote_certificate_id.empty())
849 transport_stats->remote_certificate_id = remote_certificate_id;
850 report->AddStats(std::move(transport_stats));
851 }
852 }
853}
854
855std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbos0adb8282016-11-23 02:32:06 -0800856RTCStatsCollector::PrepareTransportCertificateStats(
hbos2fa7c672016-10-24 04:00:05 -0700857 const SessionStats& session_stats) const {
858 RTC_DCHECK(signaling_thread_->IsCurrent());
859 std::map<std::string, CertificateStatsPair> transport_cert_stats;
860 for (const auto& transport_stats : session_stats.transport_stats) {
861 CertificateStatsPair certificate_stats_pair;
862 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
863 if (pc_->session()->GetLocalCertificate(
864 transport_stats.second.transport_name, &local_certificate)) {
865 certificate_stats_pair.local =
866 local_certificate->ssl_certificate().GetStats();
867 }
868 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
869 pc_->session()->GetRemoteSSLCertificate(
870 transport_stats.second.transport_name);
871 if (remote_certificate) {
872 certificate_stats_pair.remote = remote_certificate->GetStats();
873 }
874 transport_cert_stats.insert(
875 std::make_pair(transport_stats.second.transport_name,
876 std::move(certificate_stats_pair)));
877 }
878 return transport_cert_stats;
879}
880
hbos0adb8282016-11-23 02:32:06 -0800881RTCStatsCollector::MediaInfo RTCStatsCollector::PrepareMediaInfo(
882 const SessionStats& session_stats) const {
883 MediaInfo media_info;
884 if (pc_->session()->voice_channel()) {
885 cricket::VoiceMediaInfo voice_media_info;
886 if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) {
887 media_info.voice = rtc::Optional<cricket::VoiceMediaInfo>(
888 std::move(voice_media_info));
889 }
890 }
891 if (pc_->session()->video_channel()) {
892 cricket::VideoMediaInfo video_media_info;
893 if (pc_->session()->video_channel()->GetStats(&video_media_info)) {
894 media_info.video = rtc::Optional<cricket::VideoMediaInfo>(
895 std::move(video_media_info));
896 }
897 }
898 return media_info;
899}
900
hbos82ebe022016-11-14 01:41:09 -0800901void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
902 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
903 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
904}
905
906void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
907 RTC_DCHECK(signaling_thread_->IsCurrent());
908 bool result = internal_record_.opened_data_channels.insert(
909 reinterpret_cast<uintptr_t>(channel)).second;
910 ++internal_record_.data_channels_opened;
911 RTC_DCHECK(result);
912}
913
914void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
915 RTC_DCHECK(signaling_thread_->IsCurrent());
916 // Only channels that have been fully opened (and have increased the
917 // |data_channels_opened_| counter) increase the closed counter.
918 if (internal_record_.opened_data_channels.find(
919 reinterpret_cast<uintptr_t>(channel)) !=
920 internal_record_.opened_data_channels.end()) {
921 ++internal_record_.data_channels_closed;
922 }
923}
924
hboscc555c52016-10-18 12:48:31 -0700925const char* CandidateTypeToRTCIceCandidateTypeForTesting(
926 const std::string& type) {
927 return CandidateTypeToRTCIceCandidateType(type);
928}
929
930const char* DataStateToRTCDataChannelStateForTesting(
931 DataChannelInterface::DataState state) {
932 return DataStateToRTCDataChannelState(state);
933}
934
hbosd565b732016-08-30 14:04:35 -0700935} // namespace webrtc