blob: 9f08aa9aa0db22d2901a7078354522fcbf34ae49 [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);
153 inbound_stats->fraction_lost =
154 static_cast<double>(media_receiver_info.fraction_lost);
155}
156
157void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
158 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800159 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700160 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800161 voice_receiver_info, inbound_audio);
162 inbound_audio->media_type = "audio";
163 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700164 static_cast<double>(voice_receiver_info.jitter_ms) /
165 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800166 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
167 // purposefully left undefined for audio.
hboseeafe942016-11-01 03:00:17 -0700168}
169
170void SetInboundRTPStreamStatsFromVideoReceiverInfo(
171 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800172 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700173 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800174 video_receiver_info, inbound_video);
175 inbound_video->media_type = "video";
176 inbound_video->fir_count =
177 static_cast<uint32_t>(video_receiver_info.firs_sent);
178 inbound_video->pli_count =
179 static_cast<uint32_t>(video_receiver_info.plis_sent);
180 inbound_video->nack_count =
181 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hboseeafe942016-11-01 03:00:17 -0700182}
183
hbos820f5782016-11-22 03:16:50 -0800184// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700185void SetOutboundRTPStreamStatsFromMediaSenderInfo(
186 const cricket::MediaSenderInfo& media_sender_info,
187 RTCOutboundRTPStreamStats* outbound_stats) {
188 RTC_DCHECK(outbound_stats);
189 outbound_stats->ssrc = rtc::ToString<>(media_sender_info.ssrc());
190 // TODO(hbos): Support the remote case. crbug.com/657856
191 outbound_stats->is_remote = false;
192 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
193 // |media_sender_info.codec_name|. crbug.com/657854, 657856, 659117
194 outbound_stats->packets_sent =
195 static_cast<uint32_t>(media_sender_info.packets_sent);
196 outbound_stats->bytes_sent =
197 static_cast<uint64_t>(media_sender_info.bytes_sent);
198 outbound_stats->round_trip_time =
199 static_cast<double>(media_sender_info.rtt_ms) / rtc::kNumMillisecsPerSec;
200}
201
202void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
203 const cricket::VoiceSenderInfo& voice_sender_info,
204 RTCOutboundRTPStreamStats* outbound_audio) {
205 SetOutboundRTPStreamStatsFromMediaSenderInfo(
206 voice_sender_info, outbound_audio);
207 outbound_audio->media_type = "audio";
208 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
209 // purposefully left undefined for audio.
210}
211
212void SetOutboundRTPStreamStatsFromVideoSenderInfo(
213 const cricket::VideoSenderInfo& video_sender_info,
214 RTCOutboundRTPStreamStats* outbound_video) {
215 SetOutboundRTPStreamStatsFromMediaSenderInfo(
216 video_sender_info, outbound_video);
217 outbound_video->media_type = "video";
218 outbound_video->fir_count =
219 static_cast<uint32_t>(video_sender_info.firs_rcvd);
220 outbound_video->pli_count =
221 static_cast<uint32_t>(video_sender_info.plis_rcvd);
222 outbound_video->nack_count =
223 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
224}
225
hbos02ba2112016-10-28 05:14:53 -0700226void ProduceCertificateStatsFromSSLCertificateStats(
227 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
228 RTCStatsReport* report) {
229 RTCCertificateStats* prev_certificate_stats = nullptr;
230 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
231 s = s->issuer.get()) {
232 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
233 RTCCertificateIDFromFingerprint(s->fingerprint), timestamp_us);
234 certificate_stats->fingerprint = s->fingerprint;
235 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
236 certificate_stats->base64_certificate = s->base64_certificate;
237 if (prev_certificate_stats)
238 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
239 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
240 prev_certificate_stats = certificate_stats;
241 }
242}
243
244const std::string& ProduceIceCandidateStats(
245 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
246 RTCStatsReport* report) {
247 const std::string& id = "RTCIceCandidate_" + candidate.id();
248 const RTCStats* stats = report->Get(id);
249 if (!stats) {
250 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
251 if (is_local)
252 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
253 else
254 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
255 candidate_stats->ip = candidate.address().ipaddr().ToString();
256 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
257 candidate_stats->protocol = candidate.protocol();
258 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
259 candidate.type());
260 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
261
262 stats = candidate_stats.get();
263 report->AddStats(std::move(candidate_stats));
264 }
265 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
266 : RTCRemoteIceCandidateStats::kType);
267 return stats->id();
268}
269
hbos09bc1282016-11-08 06:29:22 -0800270void ProduceMediaStreamAndTrackStats(
271 int64_t timestamp_us,
272 rtc::scoped_refptr<StreamCollectionInterface> streams,
273 bool is_local,
274 RTCStatsReport* report) {
275 // TODO(hbos): When "AddTrack" is implemented we should iterate tracks to
276 // find which streams exist, not iterate streams to find tracks.
277 // crbug.com/659137
278 // TODO(hbos): Return stats of detached tracks. We have to perform stats
279 // gathering at the time of detachment to get accurate stats and timestamps.
280 // crbug.com/659137
281 if (!streams)
282 return;
283 for (size_t i = 0; i < streams->count(); ++i) {
284 MediaStreamInterface* stream = streams->at(i);
285
286 std::unique_ptr<RTCMediaStreamStats> stream_stats(
287 new RTCMediaStreamStats("RTCMediaStream_" + stream->label(),
288 timestamp_us));
289 stream_stats->stream_identifier = stream->label();
290 stream_stats->track_ids = std::vector<std::string>();
291 // Audio Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800292 for (const rtc::scoped_refptr<AudioTrackInterface>& audio_track :
hbos09bc1282016-11-08 06:29:22 -0800293 stream->GetAudioTracks()) {
294 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
295 *audio_track.get());
296 if (report->Get(id)) {
297 // Skip track, stats already exist for it.
298 continue;
299 }
300 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
301 new RTCMediaStreamTrackStats(id, timestamp_us));
302 stream_stats->track_ids->push_back(audio_track_stats->id());
303 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
304 *audio_track.get(),
305 audio_track_stats.get());
306 audio_track_stats->remote_source = !is_local;
307 audio_track_stats->detached = false;
308 int signal_level;
309 if (audio_track->GetSignalLevel(&signal_level)) {
310 // Convert signal level from [0,32767] int to [0,1] double.
311 RTC_DCHECK_GE(signal_level, 0);
312 RTC_DCHECK_LE(signal_level, 32767);
313 audio_track_stats->audio_level = signal_level / 32767.0;
314 }
315 if (audio_track->GetAudioProcessor()) {
316 AudioProcessorInterface::AudioProcessorStats audio_processor_stats;
317 audio_track->GetAudioProcessor()->GetStats(&audio_processor_stats);
318 audio_track_stats->echo_return_loss = static_cast<double>(
319 audio_processor_stats.echo_return_loss);
320 audio_track_stats->echo_return_loss_enhancement = static_cast<double>(
321 audio_processor_stats.echo_return_loss_enhancement);
322 }
323 report->AddStats(std::move(audio_track_stats));
324 }
325 // Video Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800326 for (const rtc::scoped_refptr<VideoTrackInterface>& video_track :
hbos09bc1282016-11-08 06:29:22 -0800327 stream->GetVideoTracks()) {
328 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
329 *video_track.get());
330 if (report->Get(id)) {
331 // Skip track, stats already exist for it.
332 continue;
333 }
334 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
335 new RTCMediaStreamTrackStats(id, timestamp_us));
336 stream_stats->track_ids->push_back(video_track_stats->id());
337 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
338 *video_track.get(),
339 video_track_stats.get());
340 video_track_stats->remote_source = !is_local;
341 video_track_stats->detached = false;
342 if (video_track->GetSource()) {
343 VideoTrackSourceInterface::Stats video_track_source_stats;
344 if (video_track->GetSource()->GetStats(&video_track_source_stats)) {
345 video_track_stats->frame_width = static_cast<uint32_t>(
346 video_track_source_stats.input_width);
347 video_track_stats->frame_height = static_cast<uint32_t>(
348 video_track_source_stats.input_height);
349 }
350 }
351 report->AddStats(std::move(video_track_stats));
352 }
353 report->AddStats(std::move(stream_stats));
354 }
355}
356
hboscc555c52016-10-18 12:48:31 -0700357} // namespace
358
hbosc82f2e12016-09-05 01:36:50 -0700359rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
360 PeerConnection* pc, int64_t cache_lifetime_us) {
361 return rtc::scoped_refptr<RTCStatsCollector>(
362 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
363}
364
365RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
366 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700367 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -0700368 signaling_thread_(pc->session()->signaling_thread()),
369 worker_thread_(pc->session()->worker_thread()),
370 network_thread_(pc->session()->network_thread()),
371 num_pending_partial_reports_(0),
372 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700373 cache_timestamp_us_(0),
374 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700375 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700376 RTC_DCHECK(signaling_thread_);
377 RTC_DCHECK(worker_thread_);
378 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700379 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbos82ebe022016-11-14 01:41:09 -0800380 pc_->SignalDataChannelCreated.connect(
381 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700382}
383
hbosc82f2e12016-09-05 01:36:50 -0700384void RTCStatsCollector::GetStatsReport(
385 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
386 RTC_DCHECK(signaling_thread_->IsCurrent());
387 RTC_DCHECK(callback);
388 callbacks_.push_back(callback);
389
hbos0e6758d2016-08-31 07:57:36 -0700390 // "Now" using a monotonically increasing timer.
391 int64_t cache_now_us = rtc::TimeMicros();
392 if (cached_report_ &&
393 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700394 // We have a fresh cached report to deliver.
395 DeliverCachedReport();
396 } else if (!num_pending_partial_reports_) {
397 // Only start gathering stats if we're not already gathering stats. In the
398 // case of already gathering stats, |callback_| will be invoked when there
399 // are no more pending partial reports.
400
401 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
402 // UTC), in microseconds. The system clock could be modified and is not
403 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700404 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700405
406 num_pending_partial_reports_ = 3;
407 partial_report_timestamp_us_ = cache_now_us;
408 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
409 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread,
410 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
411 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_,
412 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread,
413 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
414 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
415 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
416 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbos0e6758d2016-08-31 07:57:36 -0700417 }
hbosd565b732016-08-30 14:04:35 -0700418}
419
420void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700421 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700422 cached_report_ = nullptr;
423}
424
hbosc82f2e12016-09-05 01:36:50 -0700425void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
426 int64_t timestamp_us) {
427 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700428 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
429 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700430
hbos6ab97ce2016-10-03 14:16:56 -0700431 SessionStats session_stats;
432 if (pc_->session()->GetTransportStats(&session_stats)) {
hbos2fa7c672016-10-24 04:00:05 -0700433 std::map<std::string, CertificateStatsPair> transport_cert_stats =
hbos0adb8282016-11-23 02:32:06 -0800434 PrepareTransportCertificateStats(session_stats);
435 MediaInfo media_info = PrepareMediaInfo(session_stats);
hbos2fa7c672016-10-24 04:00:05 -0700436
437 ProduceCertificateStats_s(
438 timestamp_us, transport_cert_stats, report.get());
hbos0adb8282016-11-23 02:32:06 -0800439 ProduceCodecStats_s(
440 timestamp_us, media_info, report.get());
hbos2fa7c672016-10-24 04:00:05 -0700441 ProduceIceCandidateAndPairStats_s(
442 timestamp_us, session_stats, report.get());
hbos6ded1902016-11-01 01:50:46 -0700443 ProduceRTPStreamStats_s(
hbos0adb8282016-11-23 02:32:06 -0800444 timestamp_us, session_stats, media_info, report.get());
hbos2fa7c672016-10-24 04:00:05 -0700445 ProduceTransportStats_s(
446 timestamp_us, session_stats, transport_cert_stats, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700447 }
hboscc555c52016-10-18 12:48:31 -0700448 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos09bc1282016-11-08 06:29:22 -0800449 ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700450 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700451
452 AddPartialResults(report);
453}
454
455void RTCStatsCollector::ProducePartialResultsOnWorkerThread(
456 int64_t timestamp_us) {
457 RTC_DCHECK(worker_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700458 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
459 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700460
461 // TODO(hbos): Gather stats on worker thread.
hbos6ded1902016-11-01 01:50:46 -0700462 // pc_->session()'s channels are owned by the signaling thread but there are
463 // some stats that are gathered on the worker thread. Instead of a synchronous
464 // invoke on "s->w" we could to the "w" work here asynchronously if it wasn't
465 // for the ownership issue. Synchronous invokes in other places makes it
466 // difficult to introduce locks without introducing deadlocks and the channels
467 // are not reference counted.
hbosc82f2e12016-09-05 01:36:50 -0700468
469 AddPartialResults(report);
470}
471
472void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
473 int64_t timestamp_us) {
474 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700475 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
476 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700477
478 // TODO(hbos): Gather stats on network thread.
hbos6ded1902016-11-01 01:50:46 -0700479 // pc_->session()'s channels are owned by the signaling thread but there are
480 // some stats that are gathered on the network thread. Instead of a
481 // synchronous invoke on "s->n" we could to the "n" work here asynchronously
482 // if it wasn't for the ownership issue. Synchronous invokes in other places
483 // makes it difficult to introduce locks without introducing deadlocks and the
484 // channels are not reference counted.
hbosc82f2e12016-09-05 01:36:50 -0700485
486 AddPartialResults(report);
487}
488
489void RTCStatsCollector::AddPartialResults(
490 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
491 if (!signaling_thread_->IsCurrent()) {
492 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
493 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
494 rtc::scoped_refptr<RTCStatsCollector>(this),
495 partial_report));
496 return;
497 }
498 AddPartialResults_s(partial_report);
499}
500
501void RTCStatsCollector::AddPartialResults_s(
502 rtc::scoped_refptr<RTCStatsReport> partial_report) {
503 RTC_DCHECK(signaling_thread_->IsCurrent());
504 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
505 if (!partial_report_)
506 partial_report_ = partial_report;
507 else
508 partial_report_->TakeMembersFrom(partial_report);
509 --num_pending_partial_reports_;
510 if (!num_pending_partial_reports_) {
511 cache_timestamp_us_ = partial_report_timestamp_us_;
512 cached_report_ = partial_report_;
513 partial_report_ = nullptr;
514 DeliverCachedReport();
515 }
516}
517
518void RTCStatsCollector::DeliverCachedReport() {
519 RTC_DCHECK(signaling_thread_->IsCurrent());
520 RTC_DCHECK(!callbacks_.empty());
521 RTC_DCHECK(cached_report_);
522 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
523 callbacks_) {
524 callback->OnStatsDelivered(cached_report_);
525 }
526 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700527}
528
hbos6ab97ce2016-10-03 14:16:56 -0700529void RTCStatsCollector::ProduceCertificateStats_s(
hbos2fa7c672016-10-24 04:00:05 -0700530 int64_t timestamp_us,
531 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700532 RTCStatsReport* report) const {
533 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700534 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
535 if (transport_cert_stats_pair.second.local) {
536 ProduceCertificateStatsFromSSLCertificateStats(
537 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700538 }
hbos02ba2112016-10-28 05:14:53 -0700539 if (transport_cert_stats_pair.second.remote) {
540 ProduceCertificateStatsFromSSLCertificateStats(
541 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700542 }
543 }
544}
545
hbos0adb8282016-11-23 02:32:06 -0800546void RTCStatsCollector::ProduceCodecStats_s(
547 int64_t timestamp_us, const MediaInfo& media_info,
548 RTCStatsReport* report) const {
549 RTC_DCHECK(signaling_thread_->IsCurrent());
550 // Audio
551 if (media_info.voice) {
552 // Inbound
553 for (const auto& pair : media_info.voice->receive_codecs) {
554 report->AddStats(CodecStatsFromRtpCodecParameters(
555 timestamp_us, true, true, pair.second));
556 }
557 // Outbound
558 for (const auto& pair : media_info.voice->send_codecs) {
559 report->AddStats(CodecStatsFromRtpCodecParameters(
560 timestamp_us, false, true, pair.second));
561 }
562 }
563 // Video
564 if (media_info.video) {
565 // Inbound
566 for (const auto& pair : media_info.video->receive_codecs) {
567 report->AddStats(CodecStatsFromRtpCodecParameters(
568 timestamp_us, true, false, pair.second));
569 }
570 // Outbound
571 for (const auto& pair : media_info.video->send_codecs) {
572 report->AddStats(CodecStatsFromRtpCodecParameters(
573 timestamp_us, false, false, pair.second));
574 }
575 }
576}
577
hboscc555c52016-10-18 12:48:31 -0700578void RTCStatsCollector::ProduceDataChannelStats_s(
579 int64_t timestamp_us, RTCStatsReport* report) const {
580 RTC_DCHECK(signaling_thread_->IsCurrent());
581 for (const rtc::scoped_refptr<DataChannel>& data_channel :
582 pc_->sctp_data_channels()) {
583 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
584 new RTCDataChannelStats(
585 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
586 timestamp_us));
587 data_channel_stats->label = data_channel->label();
588 data_channel_stats->protocol = data_channel->protocol();
589 data_channel_stats->datachannelid = data_channel->id();
590 data_channel_stats->state =
591 DataStateToRTCDataChannelState(data_channel->state());
592 data_channel_stats->messages_sent = data_channel->messages_sent();
593 data_channel_stats->bytes_sent = data_channel->bytes_sent();
594 data_channel_stats->messages_received = data_channel->messages_received();
595 data_channel_stats->bytes_received = data_channel->bytes_received();
596 report->AddStats(std::move(data_channel_stats));
597 }
598}
599
hbosab9f6e42016-10-07 02:18:47 -0700600void RTCStatsCollector::ProduceIceCandidateAndPairStats_s(
601 int64_t timestamp_us, const SessionStats& session_stats,
602 RTCStatsReport* report) const {
603 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700604 for (const auto& transport_stats : session_stats.transport_stats) {
605 for (const auto& channel_stats : transport_stats.second.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800606 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
607 transport_stats.second.transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700608 for (const cricket::ConnectionInfo& info :
609 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700610 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700611 new RTCIceCandidatePairStats(
612 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
613 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700614
hbos0583b282016-11-30 01:50:14 -0800615 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700616 // TODO(hbos): There could be other candidates that are not paired with
617 // anything. We don't have a complete list. Local candidates come from
618 // Port objects, and prflx candidates (both local and remote) are only
619 // stored in candidate pairs. crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700620 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700621 timestamp_us, info.local_candidate, true, report);
hbos02ba2112016-10-28 05:14:53 -0700622 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700623 timestamp_us, info.remote_candidate, false, report);
hbosc47a0c32016-10-11 14:54:49 -0700624 // TODO(hbos): This writable is different than the spec. It goes to
625 // false after a certain amount of time without a response passes.
626 // crbug.com/633550
627 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700628 candidate_pair_stats->bytes_sent =
629 static_cast<uint64_t>(info.sent_total_bytes);
630 candidate_pair_stats->bytes_received =
631 static_cast<uint64_t>(info.recv_total_bytes);
hbosc47a0c32016-10-11 14:54:49 -0700632 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
633 // smoothed according to the spec. crbug.com/633550. See
634 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
635 candidate_pair_stats->current_rtt =
hbos6ded1902016-11-01 01:50:46 -0700636 static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec;
hbosd82f5122016-12-09 04:12:39 -0800637 candidate_pair_stats->requests_received =
638 static_cast<uint64_t>(info.recv_ping_requests);
hbosc47a0c32016-10-11 14:54:49 -0700639 candidate_pair_stats->requests_sent =
640 static_cast<uint64_t>(info.sent_ping_requests_total);
641 candidate_pair_stats->responses_received =
642 static_cast<uint64_t>(info.recv_ping_responses);
643 candidate_pair_stats->responses_sent =
644 static_cast<uint64_t>(info.sent_ping_responses);
hbosc47a0c32016-10-11 14:54:49 -0700645
646 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700647 }
648 }
649 }
650}
651
hbos09bc1282016-11-08 06:29:22 -0800652void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s(
653 int64_t timestamp_us, RTCStatsReport* report) const {
654 RTC_DCHECK(signaling_thread_->IsCurrent());
655 ProduceMediaStreamAndTrackStats(
656 timestamp_us, pc_->local_streams(), true, report);
657 ProduceMediaStreamAndTrackStats(
658 timestamp_us, pc_->remote_streams(), false, report);
659}
660
hbos6ab97ce2016-10-03 14:16:56 -0700661void RTCStatsCollector::ProducePeerConnectionStats_s(
662 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700663 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700664 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700665 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -0800666 stats->data_channels_opened = internal_record_.data_channels_opened;
667 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -0700668 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700669}
670
hbos6ded1902016-11-01 01:50:46 -0700671void RTCStatsCollector::ProduceRTPStreamStats_s(
672 int64_t timestamp_us, const SessionStats& session_stats,
hbos0adb8282016-11-23 02:32:06 -0800673 const MediaInfo& media_info, RTCStatsReport* report) const {
hbos6ded1902016-11-01 01:50:46 -0700674 RTC_DCHECK(signaling_thread_->IsCurrent());
675
676 // Audio
hbos0adb8282016-11-23 02:32:06 -0800677 if (media_info.voice) {
678 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
679 session_stats.proxy_to_transport, *pc_->session()->voice_channel());
680 RTC_DCHECK(!transport_id.empty());
681 // Inbound
682 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
683 media_info.voice->receivers) {
684 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
685 // is fixed.
686 if (voice_receiver_info.ssrc() == 0)
687 continue;
688 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
689 new RTCInboundRTPStreamStats(
690 RTCInboundRTPStreamStatsIDFromSSRC(
691 true, voice_receiver_info.ssrc()),
692 timestamp_us));
693 SetInboundRTPStreamStatsFromVoiceReceiverInfo(
694 voice_receiver_info, inbound_audio.get());
695 inbound_audio->transport_id = transport_id;
696 if (voice_receiver_info.codec_payload_type) {
697 inbound_audio->codec_id =
698 RTCCodecStatsIDFromDirectionMediaAndPayload(
699 true, true, *voice_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700700 }
hbos0adb8282016-11-23 02:32:06 -0800701 report->AddStats(std::move(inbound_audio));
702 }
703 // Outbound
704 for (const cricket::VoiceSenderInfo& voice_sender_info :
705 media_info.voice->senders) {
706 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
707 // is fixed.
708 if (voice_sender_info.ssrc() == 0)
709 continue;
710 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
711 new RTCOutboundRTPStreamStats(
712 RTCOutboundRTPStreamStatsIDFromSSRC(
713 true, voice_sender_info.ssrc()),
714 timestamp_us));
715 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
716 voice_sender_info, outbound_audio.get());
717 outbound_audio->transport_id = transport_id;
718 if (voice_sender_info.codec_payload_type) {
719 outbound_audio->codec_id =
720 RTCCodecStatsIDFromDirectionMediaAndPayload(
721 false, true, *voice_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700722 }
hbos0adb8282016-11-23 02:32:06 -0800723 report->AddStats(std::move(outbound_audio));
hbos6ded1902016-11-01 01:50:46 -0700724 }
725 }
726 // Video
hbos0adb8282016-11-23 02:32:06 -0800727 if (media_info.video) {
728 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
729 session_stats.proxy_to_transport, *pc_->session()->video_channel());
730 RTC_DCHECK(!transport_id.empty());
731 // Inbound
732 for (const cricket::VideoReceiverInfo& video_receiver_info :
733 media_info.video->receivers) {
734 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
735 // is fixed.
736 if (video_receiver_info.ssrc() == 0)
737 continue;
738 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
739 new RTCInboundRTPStreamStats(
740 RTCInboundRTPStreamStatsIDFromSSRC(
741 false, video_receiver_info.ssrc()),
742 timestamp_us));
743 SetInboundRTPStreamStatsFromVideoReceiverInfo(
744 video_receiver_info, inbound_video.get());
745 inbound_video->transport_id = transport_id;
746 if (video_receiver_info.codec_payload_type) {
747 inbound_video->codec_id =
748 RTCCodecStatsIDFromDirectionMediaAndPayload(
749 true, false, *video_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700750 }
hbos0adb8282016-11-23 02:32:06 -0800751 report->AddStats(std::move(inbound_video));
752 }
753 // Outbound
754 for (const cricket::VideoSenderInfo& video_sender_info :
755 media_info.video->senders) {
756 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
757 // is fixed.
758 if (video_sender_info.ssrc() == 0)
759 continue;
760 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
761 new RTCOutboundRTPStreamStats(
762 RTCOutboundRTPStreamStatsIDFromSSRC(
763 false, video_sender_info.ssrc()),
764 timestamp_us));
765 SetOutboundRTPStreamStatsFromVideoSenderInfo(
766 video_sender_info, outbound_video.get());
767 outbound_video->transport_id = transport_id;
768 if (video_sender_info.codec_payload_type) {
769 outbound_video->codec_id =
770 RTCCodecStatsIDFromDirectionMediaAndPayload(
771 false, false, *video_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700772 }
hbos0adb8282016-11-23 02:32:06 -0800773 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -0700774 }
775 }
776}
777
hbos2fa7c672016-10-24 04:00:05 -0700778void RTCStatsCollector::ProduceTransportStats_s(
779 int64_t timestamp_us, const SessionStats& session_stats,
780 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
781 RTCStatsReport* report) const {
782 RTC_DCHECK(signaling_thread_->IsCurrent());
783 for (const auto& transport : session_stats.transport_stats) {
784 // Get reference to RTCP channel, if it exists.
785 std::string rtcp_transport_stats_id;
786 for (const auto& channel_stats : transport.second.channel_stats) {
787 if (channel_stats.component ==
788 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
789 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
790 transport.second.transport_name, channel_stats.component);
791 break;
792 }
793 }
794
795 // Get reference to local and remote certificates of this transport, if they
796 // exist.
797 const auto& certificate_stats_it = transport_cert_stats.find(
798 transport.second.transport_name);
799 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
800 std::string local_certificate_id;
801 if (certificate_stats_it->second.local) {
802 local_certificate_id = RTCCertificateIDFromFingerprint(
803 certificate_stats_it->second.local->fingerprint);
804 }
805 std::string remote_certificate_id;
806 if (certificate_stats_it->second.remote) {
807 remote_certificate_id = RTCCertificateIDFromFingerprint(
808 certificate_stats_it->second.remote->fingerprint);
809 }
810
811 // There is one transport stats for each channel.
812 for (const auto& channel_stats : transport.second.channel_stats) {
813 std::unique_ptr<RTCTransportStats> transport_stats(
814 new RTCTransportStats(
815 RTCTransportStatsIDFromTransportChannel(
816 transport.second.transport_name, channel_stats.component),
817 timestamp_us));
818 transport_stats->bytes_sent = 0;
819 transport_stats->bytes_received = 0;
820 transport_stats->active_connection = false;
821 for (const cricket::ConnectionInfo& info :
822 channel_stats.connection_infos) {
823 *transport_stats->bytes_sent += info.sent_total_bytes;
824 *transport_stats->bytes_received += info.recv_total_bytes;
825 if (info.best_connection) {
826 transport_stats->active_connection = true;
827 transport_stats->selected_candidate_pair_id =
828 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
829 }
830 }
831 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
832 !rtcp_transport_stats_id.empty()) {
833 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
834 }
835 if (!local_certificate_id.empty())
836 transport_stats->local_certificate_id = local_certificate_id;
837 if (!remote_certificate_id.empty())
838 transport_stats->remote_certificate_id = remote_certificate_id;
839 report->AddStats(std::move(transport_stats));
840 }
841 }
842}
843
844std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbos0adb8282016-11-23 02:32:06 -0800845RTCStatsCollector::PrepareTransportCertificateStats(
hbos2fa7c672016-10-24 04:00:05 -0700846 const SessionStats& session_stats) const {
847 RTC_DCHECK(signaling_thread_->IsCurrent());
848 std::map<std::string, CertificateStatsPair> transport_cert_stats;
849 for (const auto& transport_stats : session_stats.transport_stats) {
850 CertificateStatsPair certificate_stats_pair;
851 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
852 if (pc_->session()->GetLocalCertificate(
853 transport_stats.second.transport_name, &local_certificate)) {
854 certificate_stats_pair.local =
855 local_certificate->ssl_certificate().GetStats();
856 }
857 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
858 pc_->session()->GetRemoteSSLCertificate(
859 transport_stats.second.transport_name);
860 if (remote_certificate) {
861 certificate_stats_pair.remote = remote_certificate->GetStats();
862 }
863 transport_cert_stats.insert(
864 std::make_pair(transport_stats.second.transport_name,
865 std::move(certificate_stats_pair)));
866 }
867 return transport_cert_stats;
868}
869
hbos0adb8282016-11-23 02:32:06 -0800870RTCStatsCollector::MediaInfo RTCStatsCollector::PrepareMediaInfo(
871 const SessionStats& session_stats) const {
872 MediaInfo media_info;
873 if (pc_->session()->voice_channel()) {
874 cricket::VoiceMediaInfo voice_media_info;
875 if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) {
876 media_info.voice = rtc::Optional<cricket::VoiceMediaInfo>(
877 std::move(voice_media_info));
878 }
879 }
880 if (pc_->session()->video_channel()) {
881 cricket::VideoMediaInfo video_media_info;
882 if (pc_->session()->video_channel()->GetStats(&video_media_info)) {
883 media_info.video = rtc::Optional<cricket::VideoMediaInfo>(
884 std::move(video_media_info));
885 }
886 }
887 return media_info;
888}
889
hbos82ebe022016-11-14 01:41:09 -0800890void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
891 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
892 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
893}
894
895void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
896 RTC_DCHECK(signaling_thread_->IsCurrent());
897 bool result = internal_record_.opened_data_channels.insert(
898 reinterpret_cast<uintptr_t>(channel)).second;
899 ++internal_record_.data_channels_opened;
900 RTC_DCHECK(result);
901}
902
903void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
904 RTC_DCHECK(signaling_thread_->IsCurrent());
905 // Only channels that have been fully opened (and have increased the
906 // |data_channels_opened_| counter) increase the closed counter.
907 if (internal_record_.opened_data_channels.find(
908 reinterpret_cast<uintptr_t>(channel)) !=
909 internal_record_.opened_data_channels.end()) {
910 ++internal_record_.data_channels_closed;
911 }
912}
913
hboscc555c52016-10-18 12:48:31 -0700914const char* CandidateTypeToRTCIceCandidateTypeForTesting(
915 const std::string& type) {
916 return CandidateTypeToRTCIceCandidateType(type);
917}
918
919const char* DataStateToRTCDataChannelStateForTesting(
920 DataChannelInterface::DataState state) {
921 return DataStateToRTCDataChannelState(state);
922}
923
hbosd565b732016-08-30 14:04:35 -0700924} // namespace webrtc