blob: 3ab7ac94e1e12429a423d05d7180da4f84fdbef3 [file] [log] [blame]
hbosd565b732016-08-30 14:04:35 -07001/*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
hbos74e1a4f2016-09-15 23:33:01 -070011#include "webrtc/api/rtcstatscollector.h"
hbosd565b732016-08-30 14:04:35 -070012
13#include <memory>
14#include <utility>
15#include <vector>
16
17#include "webrtc/api/peerconnection.h"
hbos09bc1282016-11-08 06:29:22 -080018#include "webrtc/api/peerconnectioninterface.h"
19#include "webrtc/api/mediastreaminterface.h"
hbos6ab97ce2016-10-03 14:16:56 -070020#include "webrtc/api/webrtcsession.h"
hbosd565b732016-08-30 14:04:35 -070021#include "webrtc/base/checks.h"
hbos6ded1902016-11-01 01:50:46 -070022#include "webrtc/base/timeutils.h"
23#include "webrtc/media/base/mediachannel.h"
hbosab9f6e42016-10-07 02:18:47 -070024#include "webrtc/p2p/base/candidate.h"
hbos2fa7c672016-10-24 04:00:05 -070025#include "webrtc/p2p/base/p2pconstants.h"
hbosab9f6e42016-10-07 02:18:47 -070026#include "webrtc/p2p/base/port.h"
hbosd565b732016-08-30 14:04:35 -070027
28namespace webrtc {
29
hboscc555c52016-10-18 12:48:31 -070030namespace {
31
hbos2fa7c672016-10-24 04:00:05 -070032std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
33 return "RTCCertificate_" + fingerprint;
34}
35
hbos0adb8282016-11-23 02:32:06 -080036std::string RTCCodecStatsIDFromDirectionMediaAndPayload(
37 bool inbound, bool audio, uint32_t payload_type) {
38 // TODO(hbos): When we are able to handle multiple m= lines of the same media
39 // type (and multiple BaseChannels for the same type is possible?) this needs
40 // to be updated to differentiate the transport being used, and stats need to
41 // be collected for all of them. crbug.com/659117
42 if (inbound) {
43 return audio ? "RTCCodec_InboundAudio_" + rtc::ToString<>(payload_type)
44 : "RTCCodec_InboundVideo_" + rtc::ToString<>(payload_type);
45 }
46 return audio ? "RTCCodec_OutboundAudio_" + rtc::ToString<>(payload_type)
47 : "RTCCodec_OutboundVideo_" + rtc::ToString<>(payload_type);
48}
49
hbos2fa7c672016-10-24 04:00:05 -070050std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
51 const cricket::ConnectionInfo& info) {
52 return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" +
53 info.remote_candidate.id();
54}
55
hbos09bc1282016-11-08 06:29:22 -080056std::string RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
57 const MediaStreamTrackInterface& track) {
58 return "RTCMediaStreamTrack_" + track.id();
59}
60
hbos2fa7c672016-10-24 04:00:05 -070061std::string RTCTransportStatsIDFromTransportChannel(
62 const std::string& transport_name, int channel_component) {
63 return "RTCTransport_" + transport_name + "_" +
64 rtc::ToString<>(channel_component);
65}
66
hbos6ded1902016-11-01 01:50:46 -070067std::string RTCTransportStatsIDFromBaseChannel(
68 const ProxyTransportMap& proxy_to_transport,
69 const cricket::BaseChannel& base_channel) {
70 auto proxy_it = proxy_to_transport.find(base_channel.content_name());
71 if (proxy_it == proxy_to_transport.cend())
72 return "";
73 return RTCTransportStatsIDFromTransportChannel(
74 proxy_it->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
75}
76
hboseeafe942016-11-01 03:00:17 -070077std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
78 return audio ? "RTCInboundRTPAudioStream_" + rtc::ToString<>(ssrc)
79 : "RTCInboundRTPVideoStream_" + rtc::ToString<>(ssrc);
80}
81
hbos6ded1902016-11-01 01:50:46 -070082std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
83 return audio ? "RTCOutboundRTPAudioStream_" + rtc::ToString<>(ssrc)
84 : "RTCOutboundRTPVideoStream_" + rtc::ToString<>(ssrc);
85}
86
hbosab9f6e42016-10-07 02:18:47 -070087const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
88 if (type == cricket::LOCAL_PORT_TYPE)
89 return RTCIceCandidateType::kHost;
90 if (type == cricket::STUN_PORT_TYPE)
91 return RTCIceCandidateType::kSrflx;
92 if (type == cricket::PRFLX_PORT_TYPE)
93 return RTCIceCandidateType::kPrflx;
94 if (type == cricket::RELAY_PORT_TYPE)
95 return RTCIceCandidateType::kRelay;
96 RTC_NOTREACHED();
97 return nullptr;
98}
99
hboscc555c52016-10-18 12:48:31 -0700100const char* DataStateToRTCDataChannelState(
101 DataChannelInterface::DataState state) {
102 switch (state) {
103 case DataChannelInterface::kConnecting:
104 return RTCDataChannelState::kConnecting;
105 case DataChannelInterface::kOpen:
106 return RTCDataChannelState::kOpen;
107 case DataChannelInterface::kClosing:
108 return RTCDataChannelState::kClosing;
109 case DataChannelInterface::kClosed:
110 return RTCDataChannelState::kClosed;
111 default:
112 RTC_NOTREACHED();
113 return nullptr;
114 }
115}
116
hbos06495bc2017-01-02 08:08:18 -0800117const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
118 cricket::IceCandidatePairState state) {
119 switch (state) {
120 case cricket::IceCandidatePairState::WAITING:
121 return RTCStatsIceCandidatePairState::kWaiting;
122 case cricket::IceCandidatePairState::IN_PROGRESS:
123 return RTCStatsIceCandidatePairState::kInProgress;
124 case cricket::IceCandidatePairState::SUCCEEDED:
125 return RTCStatsIceCandidatePairState::kSucceeded;
126 case cricket::IceCandidatePairState::FAILED:
127 return RTCStatsIceCandidatePairState::kFailed;
128 default:
129 RTC_NOTREACHED();
130 return nullptr;
131 }
132}
133
hbos0adb8282016-11-23 02:32:06 -0800134std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
135 uint64_t timestamp_us, bool inbound, bool audio,
136 const RtpCodecParameters& codec_params) {
137 RTC_DCHECK_GE(codec_params.payload_type, 0);
138 RTC_DCHECK_LE(codec_params.payload_type, 127);
139 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
140 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
141 RTCCodecStatsIDFromDirectionMediaAndPayload(inbound, audio, payload_type),
142 timestamp_us));
143 codec_stats->payload_type = payload_type;
144 codec_stats->codec = (audio ? "audio/" : "video/") + codec_params.mime_type;
145 codec_stats->clock_rate = static_cast<uint32_t>(codec_params.clock_rate);
146 return codec_stats;
147}
148
hbos09bc1282016-11-08 06:29:22 -0800149void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
150 const MediaStreamTrackInterface& track,
151 RTCMediaStreamTrackStats* track_stats) {
152 track_stats->track_identifier = track.id();
153 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
154}
155
hbos820f5782016-11-22 03:16:50 -0800156// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700157void SetInboundRTPStreamStatsFromMediaReceiverInfo(
158 const cricket::MediaReceiverInfo& media_receiver_info,
159 RTCInboundRTPStreamStats* inbound_stats) {
160 RTC_DCHECK(inbound_stats);
161 inbound_stats->ssrc = rtc::ToString<>(media_receiver_info.ssrc());
162 // TODO(hbos): Support the remote case. crbug.com/657855
163 inbound_stats->is_remote = false;
164 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
165 // |media_receiver_info.codec_name|. crbug.com/657854, 657855, 659117
166 inbound_stats->packets_received =
167 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
168 inbound_stats->bytes_received =
169 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800170 inbound_stats->packets_lost =
171 static_cast<uint32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700172 inbound_stats->fraction_lost =
173 static_cast<double>(media_receiver_info.fraction_lost);
174}
175
176void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
177 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800178 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700179 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800180 voice_receiver_info, inbound_audio);
181 inbound_audio->media_type = "audio";
182 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700183 static_cast<double>(voice_receiver_info.jitter_ms) /
184 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800185 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
186 // purposefully left undefined for audio.
hboseeafe942016-11-01 03:00:17 -0700187}
188
189void SetInboundRTPStreamStatsFromVideoReceiverInfo(
190 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800191 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700192 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800193 video_receiver_info, inbound_video);
194 inbound_video->media_type = "video";
195 inbound_video->fir_count =
196 static_cast<uint32_t>(video_receiver_info.firs_sent);
197 inbound_video->pli_count =
198 static_cast<uint32_t>(video_receiver_info.plis_sent);
199 inbound_video->nack_count =
200 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800201 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hboseeafe942016-11-01 03:00:17 -0700202}
203
hbos820f5782016-11-22 03:16:50 -0800204// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700205void SetOutboundRTPStreamStatsFromMediaSenderInfo(
206 const cricket::MediaSenderInfo& media_sender_info,
207 RTCOutboundRTPStreamStats* outbound_stats) {
208 RTC_DCHECK(outbound_stats);
209 outbound_stats->ssrc = rtc::ToString<>(media_sender_info.ssrc());
210 // TODO(hbos): Support the remote case. crbug.com/657856
211 outbound_stats->is_remote = false;
212 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
213 // |media_sender_info.codec_name|. crbug.com/657854, 657856, 659117
214 outbound_stats->packets_sent =
215 static_cast<uint32_t>(media_sender_info.packets_sent);
216 outbound_stats->bytes_sent =
217 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbose10e6d12016-12-15 01:54:29 -0800218 if (media_sender_info.rtt_ms >= 0) {
219 outbound_stats->round_trip_time = static_cast<double>(
220 media_sender_info.rtt_ms) / rtc::kNumMillisecsPerSec;
221 }
hbos6ded1902016-11-01 01:50:46 -0700222}
223
224void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
225 const cricket::VoiceSenderInfo& voice_sender_info,
226 RTCOutboundRTPStreamStats* outbound_audio) {
227 SetOutboundRTPStreamStatsFromMediaSenderInfo(
228 voice_sender_info, outbound_audio);
229 outbound_audio->media_type = "audio";
230 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
231 // purposefully left undefined for audio.
232}
233
234void SetOutboundRTPStreamStatsFromVideoSenderInfo(
235 const cricket::VideoSenderInfo& video_sender_info,
236 RTCOutboundRTPStreamStats* outbound_video) {
237 SetOutboundRTPStreamStatsFromMediaSenderInfo(
238 video_sender_info, outbound_video);
239 outbound_video->media_type = "video";
240 outbound_video->fir_count =
241 static_cast<uint32_t>(video_sender_info.firs_rcvd);
242 outbound_video->pli_count =
243 static_cast<uint32_t>(video_sender_info.plis_rcvd);
244 outbound_video->nack_count =
245 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800246 if (video_sender_info.qp_sum)
247 outbound_video->qp_sum = *video_sender_info.qp_sum;
248 outbound_video->frames_encoded = video_sender_info.frames_encoded;
hbos6ded1902016-11-01 01:50:46 -0700249}
250
hbos02ba2112016-10-28 05:14:53 -0700251void ProduceCertificateStatsFromSSLCertificateStats(
252 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
253 RTCStatsReport* report) {
254 RTCCertificateStats* prev_certificate_stats = nullptr;
255 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
256 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800257 std::string certificate_stats_id =
258 RTCCertificateIDFromFingerprint(s->fingerprint);
259 // It is possible for the same certificate to show up multiple times, e.g.
260 // if local and remote side use the same certificate in a loopback call.
261 // If the report already contains stats for this certificate, skip it.
262 if (report->Get(certificate_stats_id)) {
263 RTC_DCHECK_EQ(s, &certificate_stats);
264 break;
265 }
hbos02ba2112016-10-28 05:14:53 -0700266 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800267 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700268 certificate_stats->fingerprint = s->fingerprint;
269 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
270 certificate_stats->base64_certificate = s->base64_certificate;
271 if (prev_certificate_stats)
272 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
273 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
274 prev_certificate_stats = certificate_stats;
275 }
276}
277
278const std::string& ProduceIceCandidateStats(
279 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800280 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700281 const std::string& id = "RTCIceCandidate_" + candidate.id();
282 const RTCStats* stats = report->Get(id);
283 if (!stats) {
284 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
285 if (is_local)
286 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
287 else
288 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800289 candidate_stats->transport_id = transport_id;
hbos02ba2112016-10-28 05:14:53 -0700290 candidate_stats->ip = candidate.address().ipaddr().ToString();
291 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
292 candidate_stats->protocol = candidate.protocol();
293 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
294 candidate.type());
295 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
296
297 stats = candidate_stats.get();
298 report->AddStats(std::move(candidate_stats));
299 }
300 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
301 : RTCRemoteIceCandidateStats::kType);
302 return stats->id();
303}
304
hbos09bc1282016-11-08 06:29:22 -0800305void ProduceMediaStreamAndTrackStats(
306 int64_t timestamp_us,
307 rtc::scoped_refptr<StreamCollectionInterface> streams,
308 bool is_local,
309 RTCStatsReport* report) {
310 // TODO(hbos): When "AddTrack" is implemented we should iterate tracks to
311 // find which streams exist, not iterate streams to find tracks.
312 // crbug.com/659137
313 // TODO(hbos): Return stats of detached tracks. We have to perform stats
314 // gathering at the time of detachment to get accurate stats and timestamps.
315 // crbug.com/659137
316 if (!streams)
317 return;
318 for (size_t i = 0; i < streams->count(); ++i) {
319 MediaStreamInterface* stream = streams->at(i);
320
321 std::unique_ptr<RTCMediaStreamStats> stream_stats(
hbos02d2a922016-12-21 01:29:05 -0800322 new RTCMediaStreamStats(
323 (is_local ? "RTCMediaStream_local_" : "RTCMediaStream_remote_") +
324 stream->label(), timestamp_us));
hbos09bc1282016-11-08 06:29:22 -0800325 stream_stats->stream_identifier = stream->label();
326 stream_stats->track_ids = std::vector<std::string>();
327 // Audio Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800328 for (const rtc::scoped_refptr<AudioTrackInterface>& audio_track :
hbos09bc1282016-11-08 06:29:22 -0800329 stream->GetAudioTracks()) {
330 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
331 *audio_track.get());
332 if (report->Get(id)) {
333 // Skip track, stats already exist for it.
334 continue;
335 }
336 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
337 new RTCMediaStreamTrackStats(id, timestamp_us));
338 stream_stats->track_ids->push_back(audio_track_stats->id());
339 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
340 *audio_track.get(),
341 audio_track_stats.get());
342 audio_track_stats->remote_source = !is_local;
343 audio_track_stats->detached = false;
344 int signal_level;
345 if (audio_track->GetSignalLevel(&signal_level)) {
346 // Convert signal level from [0,32767] int to [0,1] double.
347 RTC_DCHECK_GE(signal_level, 0);
348 RTC_DCHECK_LE(signal_level, 32767);
349 audio_track_stats->audio_level = signal_level / 32767.0;
350 }
351 if (audio_track->GetAudioProcessor()) {
352 AudioProcessorInterface::AudioProcessorStats audio_processor_stats;
353 audio_track->GetAudioProcessor()->GetStats(&audio_processor_stats);
hbos9a394f02016-12-14 07:58:22 -0800354 if (audio_processor_stats.echo_return_loss != -100) {
355 audio_track_stats->echo_return_loss = static_cast<double>(
356 audio_processor_stats.echo_return_loss);
357 }
358 if (audio_processor_stats.echo_return_loss_enhancement != -100) {
359 audio_track_stats->echo_return_loss_enhancement = static_cast<double>(
360 audio_processor_stats.echo_return_loss_enhancement);
361 }
hbos09bc1282016-11-08 06:29:22 -0800362 }
363 report->AddStats(std::move(audio_track_stats));
364 }
365 // Video Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800366 for (const rtc::scoped_refptr<VideoTrackInterface>& video_track :
hbos09bc1282016-11-08 06:29:22 -0800367 stream->GetVideoTracks()) {
368 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
369 *video_track.get());
370 if (report->Get(id)) {
371 // Skip track, stats already exist for it.
372 continue;
373 }
374 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
375 new RTCMediaStreamTrackStats(id, timestamp_us));
376 stream_stats->track_ids->push_back(video_track_stats->id());
377 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
378 *video_track.get(),
379 video_track_stats.get());
380 video_track_stats->remote_source = !is_local;
381 video_track_stats->detached = false;
382 if (video_track->GetSource()) {
383 VideoTrackSourceInterface::Stats video_track_source_stats;
384 if (video_track->GetSource()->GetStats(&video_track_source_stats)) {
385 video_track_stats->frame_width = static_cast<uint32_t>(
386 video_track_source_stats.input_width);
387 video_track_stats->frame_height = static_cast<uint32_t>(
388 video_track_source_stats.input_height);
389 }
390 }
391 report->AddStats(std::move(video_track_stats));
392 }
393 report->AddStats(std::move(stream_stats));
394 }
395}
396
hboscc555c52016-10-18 12:48:31 -0700397} // namespace
398
hbosc82f2e12016-09-05 01:36:50 -0700399rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
400 PeerConnection* pc, int64_t cache_lifetime_us) {
401 return rtc::scoped_refptr<RTCStatsCollector>(
402 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
403}
404
405RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
406 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700407 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -0700408 signaling_thread_(pc->session()->signaling_thread()),
409 worker_thread_(pc->session()->worker_thread()),
410 network_thread_(pc->session()->network_thread()),
411 num_pending_partial_reports_(0),
412 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700413 cache_timestamp_us_(0),
414 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700415 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700416 RTC_DCHECK(signaling_thread_);
417 RTC_DCHECK(worker_thread_);
418 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700419 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbos82ebe022016-11-14 01:41:09 -0800420 pc_->SignalDataChannelCreated.connect(
421 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700422}
423
hbosb78306a2016-12-19 05:06:57 -0800424RTCStatsCollector::~RTCStatsCollector() {
425 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
426}
427
hbosc82f2e12016-09-05 01:36:50 -0700428void RTCStatsCollector::GetStatsReport(
429 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
430 RTC_DCHECK(signaling_thread_->IsCurrent());
431 RTC_DCHECK(callback);
432 callbacks_.push_back(callback);
433
hbos0e6758d2016-08-31 07:57:36 -0700434 // "Now" using a monotonically increasing timer.
435 int64_t cache_now_us = rtc::TimeMicros();
436 if (cached_report_ &&
437 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700438 // We have a fresh cached report to deliver.
439 DeliverCachedReport();
440 } else if (!num_pending_partial_reports_) {
441 // Only start gathering stats if we're not already gathering stats. In the
442 // case of already gathering stats, |callback_| will be invoked when there
443 // are no more pending partial reports.
444
445 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
446 // UTC), in microseconds. The system clock could be modified and is not
447 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700448 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700449
hbosf415f8a2017-01-02 04:28:51 -0800450 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700451 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800452
453 // Prepare |channel_names_| and |media_info_| for use in
454 // |ProducePartialResultsOnNetworkThread|.
455 channel_name_pairs_.reset(new ChannelNamePairs());
456 if (pc_->session()->voice_channel()) {
457 channel_name_pairs_->voice = rtc::Optional<ChannelNamePair>(
458 ChannelNamePair(pc_->session()->voice_channel()->content_name(),
459 pc_->session()->voice_channel()->transport_name()));
460 }
461 if (pc_->session()->video_channel()) {
462 channel_name_pairs_->video = rtc::Optional<ChannelNamePair>(
463 ChannelNamePair(pc_->session()->video_channel()->content_name(),
464 pc_->session()->video_channel()->transport_name()));
465 }
466 if (pc_->session()->data_channel()) {
467 channel_name_pairs_->data = rtc::Optional<ChannelNamePair>(
468 ChannelNamePair(pc_->session()->data_channel()->content_name(),
469 pc_->session()->data_channel()->transport_name()));
470 }
471 media_info_.reset(PrepareMediaInfo_s().release());
hbosf415f8a2017-01-02 04:28:51 -0800472
hbosc82f2e12016-09-05 01:36:50 -0700473 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
474 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
475 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800476 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700477 }
hbosd565b732016-08-30 14:04:35 -0700478}
479
480void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700481 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700482 cached_report_ = nullptr;
483}
484
hbosb78306a2016-12-19 05:06:57 -0800485void RTCStatsCollector::WaitForPendingRequest() {
486 RTC_DCHECK(signaling_thread_->IsCurrent());
487 if (num_pending_partial_reports_) {
488 rtc::Thread::Current()->ProcessMessages(0);
489 while (num_pending_partial_reports_) {
490 rtc::Thread::Current()->SleepMs(1);
491 rtc::Thread::Current()->ProcessMessages(0);
492 }
493 }
494}
495
hbosc82f2e12016-09-05 01:36:50 -0700496void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
497 int64_t timestamp_us) {
498 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700499 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
500 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700501
hboscc555c52016-10-18 12:48:31 -0700502 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos09bc1282016-11-08 06:29:22 -0800503 ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700504 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700505
506 AddPartialResults(report);
507}
508
hbosc82f2e12016-09-05 01:36:50 -0700509void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
510 int64_t timestamp_us) {
511 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700512 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
513 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700514
hbosdf6075a2016-12-19 04:58:02 -0800515 std::unique_ptr<SessionStats> session_stats =
516 pc_->session()->GetStats(*channel_name_pairs_);
517 if (session_stats) {
518 std::map<std::string, CertificateStatsPair> transport_cert_stats =
519 PrepareTransportCertificateStats_n(*session_stats);
520
521 ProduceCertificateStats_n(
522 timestamp_us, transport_cert_stats, report.get());
523 ProduceCodecStats_n(
524 timestamp_us, *media_info_, report.get());
525 ProduceIceCandidateAndPairStats_n(
526 timestamp_us, *session_stats, report.get());
527 ProduceRTPStreamStats_n(
528 timestamp_us, *session_stats, *media_info_, report.get());
529 ProduceTransportStats_n(
530 timestamp_us, *session_stats, transport_cert_stats, report.get());
531 }
hbosc82f2e12016-09-05 01:36:50 -0700532
533 AddPartialResults(report);
534}
535
536void RTCStatsCollector::AddPartialResults(
537 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
538 if (!signaling_thread_->IsCurrent()) {
539 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
540 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
541 rtc::scoped_refptr<RTCStatsCollector>(this),
542 partial_report));
543 return;
544 }
545 AddPartialResults_s(partial_report);
546}
547
548void RTCStatsCollector::AddPartialResults_s(
549 rtc::scoped_refptr<RTCStatsReport> partial_report) {
550 RTC_DCHECK(signaling_thread_->IsCurrent());
551 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
552 if (!partial_report_)
553 partial_report_ = partial_report;
554 else
555 partial_report_->TakeMembersFrom(partial_report);
556 --num_pending_partial_reports_;
557 if (!num_pending_partial_reports_) {
558 cache_timestamp_us_ = partial_report_timestamp_us_;
559 cached_report_ = partial_report_;
560 partial_report_ = nullptr;
561 DeliverCachedReport();
562 }
563}
564
565void RTCStatsCollector::DeliverCachedReport() {
566 RTC_DCHECK(signaling_thread_->IsCurrent());
567 RTC_DCHECK(!callbacks_.empty());
568 RTC_DCHECK(cached_report_);
569 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
570 callbacks_) {
571 callback->OnStatsDelivered(cached_report_);
572 }
573 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700574}
575
hbosdf6075a2016-12-19 04:58:02 -0800576void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700577 int64_t timestamp_us,
578 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700579 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800580 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700581 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
582 if (transport_cert_stats_pair.second.local) {
583 ProduceCertificateStatsFromSSLCertificateStats(
584 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700585 }
hbos02ba2112016-10-28 05:14:53 -0700586 if (transport_cert_stats_pair.second.remote) {
587 ProduceCertificateStatsFromSSLCertificateStats(
588 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700589 }
590 }
591}
592
hbosdf6075a2016-12-19 04:58:02 -0800593void RTCStatsCollector::ProduceCodecStats_n(
hbos0adb8282016-11-23 02:32:06 -0800594 int64_t timestamp_us, const MediaInfo& media_info,
595 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800596 RTC_DCHECK(network_thread_->IsCurrent());
hbos0adb8282016-11-23 02:32:06 -0800597 // Audio
598 if (media_info.voice) {
599 // Inbound
600 for (const auto& pair : media_info.voice->receive_codecs) {
601 report->AddStats(CodecStatsFromRtpCodecParameters(
602 timestamp_us, true, true, pair.second));
603 }
604 // Outbound
605 for (const auto& pair : media_info.voice->send_codecs) {
606 report->AddStats(CodecStatsFromRtpCodecParameters(
607 timestamp_us, false, true, pair.second));
608 }
609 }
610 // Video
611 if (media_info.video) {
612 // Inbound
613 for (const auto& pair : media_info.video->receive_codecs) {
614 report->AddStats(CodecStatsFromRtpCodecParameters(
615 timestamp_us, true, false, pair.second));
616 }
617 // Outbound
618 for (const auto& pair : media_info.video->send_codecs) {
619 report->AddStats(CodecStatsFromRtpCodecParameters(
620 timestamp_us, false, false, pair.second));
621 }
622 }
623}
624
hboscc555c52016-10-18 12:48:31 -0700625void RTCStatsCollector::ProduceDataChannelStats_s(
626 int64_t timestamp_us, RTCStatsReport* report) const {
627 RTC_DCHECK(signaling_thread_->IsCurrent());
628 for (const rtc::scoped_refptr<DataChannel>& data_channel :
629 pc_->sctp_data_channels()) {
630 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
631 new RTCDataChannelStats(
632 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
633 timestamp_us));
634 data_channel_stats->label = data_channel->label();
635 data_channel_stats->protocol = data_channel->protocol();
636 data_channel_stats->datachannelid = data_channel->id();
637 data_channel_stats->state =
638 DataStateToRTCDataChannelState(data_channel->state());
639 data_channel_stats->messages_sent = data_channel->messages_sent();
640 data_channel_stats->bytes_sent = data_channel->bytes_sent();
641 data_channel_stats->messages_received = data_channel->messages_received();
642 data_channel_stats->bytes_received = data_channel->bytes_received();
643 report->AddStats(std::move(data_channel_stats));
644 }
645}
646
hbosdf6075a2016-12-19 04:58:02 -0800647void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
hbosab9f6e42016-10-07 02:18:47 -0700648 int64_t timestamp_us, const SessionStats& session_stats,
649 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800650 RTC_DCHECK(network_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700651 for (const auto& transport_stats : session_stats.transport_stats) {
652 for (const auto& channel_stats : transport_stats.second.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800653 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
654 transport_stats.second.transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700655 for (const cricket::ConnectionInfo& info :
656 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700657 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700658 new RTCIceCandidatePairStats(
659 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
660 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700661
hbos0583b282016-11-30 01:50:14 -0800662 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700663 // TODO(hbos): There could be other candidates that are not paired with
664 // anything. We don't have a complete list. Local candidates come from
665 // Port objects, and prflx candidates (both local and remote) are only
666 // stored in candidate pairs. crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700667 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800668 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -0700669 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800670 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -0800671 candidate_pair_stats->state =
672 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
673 candidate_pair_stats->priority = info.priority;
hbosc47a0c32016-10-11 14:54:49 -0700674 // TODO(hbos): This writable is different than the spec. It goes to
675 // false after a certain amount of time without a response passes.
676 // crbug.com/633550
677 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700678 candidate_pair_stats->bytes_sent =
679 static_cast<uint64_t>(info.sent_total_bytes);
680 candidate_pair_stats->bytes_received =
681 static_cast<uint64_t>(info.recv_total_bytes);
hbosc47a0c32016-10-11 14:54:49 -0700682 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
683 // smoothed according to the spec. crbug.com/633550. See
684 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
hbos3168c7a2016-12-15 06:17:08 -0800685 candidate_pair_stats->current_round_trip_time =
hbos6ded1902016-11-01 01:50:46 -0700686 static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec;
hbosd82f5122016-12-09 04:12:39 -0800687 candidate_pair_stats->requests_received =
688 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -0800689 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
690 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700691 candidate_pair_stats->responses_received =
692 static_cast<uint64_t>(info.recv_ping_responses);
693 candidate_pair_stats->responses_sent =
694 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -0800695 RTC_DCHECK_GE(info.sent_ping_requests_total,
696 info.sent_ping_requests_before_first_response);
697 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
698 info.sent_ping_requests_total -
699 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700700
701 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700702 }
703 }
704 }
705}
706
hbos09bc1282016-11-08 06:29:22 -0800707void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s(
708 int64_t timestamp_us, RTCStatsReport* report) const {
709 RTC_DCHECK(signaling_thread_->IsCurrent());
710 ProduceMediaStreamAndTrackStats(
711 timestamp_us, pc_->local_streams(), true, report);
712 ProduceMediaStreamAndTrackStats(
713 timestamp_us, pc_->remote_streams(), false, report);
714}
715
hbos6ab97ce2016-10-03 14:16:56 -0700716void RTCStatsCollector::ProducePeerConnectionStats_s(
717 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700718 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700719 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700720 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -0800721 stats->data_channels_opened = internal_record_.data_channels_opened;
722 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -0700723 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700724}
725
hbosdf6075a2016-12-19 04:58:02 -0800726void RTCStatsCollector::ProduceRTPStreamStats_n(
hbos6ded1902016-11-01 01:50:46 -0700727 int64_t timestamp_us, const SessionStats& session_stats,
hbos0adb8282016-11-23 02:32:06 -0800728 const MediaInfo& media_info, RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800729 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700730
731 // Audio
hbos0adb8282016-11-23 02:32:06 -0800732 if (media_info.voice) {
733 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
734 session_stats.proxy_to_transport, *pc_->session()->voice_channel());
735 RTC_DCHECK(!transport_id.empty());
736 // Inbound
737 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
738 media_info.voice->receivers) {
739 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
740 // is fixed.
741 if (voice_receiver_info.ssrc() == 0)
742 continue;
743 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
744 new RTCInboundRTPStreamStats(
745 RTCInboundRTPStreamStatsIDFromSSRC(
746 true, voice_receiver_info.ssrc()),
747 timestamp_us));
748 SetInboundRTPStreamStatsFromVoiceReceiverInfo(
749 voice_receiver_info, inbound_audio.get());
750 inbound_audio->transport_id = transport_id;
751 if (voice_receiver_info.codec_payload_type) {
752 inbound_audio->codec_id =
753 RTCCodecStatsIDFromDirectionMediaAndPayload(
754 true, true, *voice_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700755 }
hbos0adb8282016-11-23 02:32:06 -0800756 report->AddStats(std::move(inbound_audio));
757 }
758 // Outbound
759 for (const cricket::VoiceSenderInfo& voice_sender_info :
760 media_info.voice->senders) {
761 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
762 // is fixed.
763 if (voice_sender_info.ssrc() == 0)
764 continue;
765 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
766 new RTCOutboundRTPStreamStats(
767 RTCOutboundRTPStreamStatsIDFromSSRC(
768 true, voice_sender_info.ssrc()),
769 timestamp_us));
770 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
771 voice_sender_info, outbound_audio.get());
772 outbound_audio->transport_id = transport_id;
773 if (voice_sender_info.codec_payload_type) {
774 outbound_audio->codec_id =
775 RTCCodecStatsIDFromDirectionMediaAndPayload(
776 false, true, *voice_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700777 }
hbos0adb8282016-11-23 02:32:06 -0800778 report->AddStats(std::move(outbound_audio));
hbos6ded1902016-11-01 01:50:46 -0700779 }
780 }
781 // Video
hbos0adb8282016-11-23 02:32:06 -0800782 if (media_info.video) {
783 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
784 session_stats.proxy_to_transport, *pc_->session()->video_channel());
785 RTC_DCHECK(!transport_id.empty());
786 // Inbound
787 for (const cricket::VideoReceiverInfo& video_receiver_info :
788 media_info.video->receivers) {
789 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
790 // is fixed.
791 if (video_receiver_info.ssrc() == 0)
792 continue;
793 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
794 new RTCInboundRTPStreamStats(
795 RTCInboundRTPStreamStatsIDFromSSRC(
796 false, video_receiver_info.ssrc()),
797 timestamp_us));
798 SetInboundRTPStreamStatsFromVideoReceiverInfo(
799 video_receiver_info, inbound_video.get());
800 inbound_video->transport_id = transport_id;
801 if (video_receiver_info.codec_payload_type) {
802 inbound_video->codec_id =
803 RTCCodecStatsIDFromDirectionMediaAndPayload(
804 true, false, *video_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700805 }
hbos0adb8282016-11-23 02:32:06 -0800806 report->AddStats(std::move(inbound_video));
807 }
808 // Outbound
809 for (const cricket::VideoSenderInfo& video_sender_info :
810 media_info.video->senders) {
811 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
812 // is fixed.
813 if (video_sender_info.ssrc() == 0)
814 continue;
815 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
816 new RTCOutboundRTPStreamStats(
817 RTCOutboundRTPStreamStatsIDFromSSRC(
818 false, video_sender_info.ssrc()),
819 timestamp_us));
820 SetOutboundRTPStreamStatsFromVideoSenderInfo(
821 video_sender_info, outbound_video.get());
822 outbound_video->transport_id = transport_id;
823 if (video_sender_info.codec_payload_type) {
824 outbound_video->codec_id =
825 RTCCodecStatsIDFromDirectionMediaAndPayload(
826 false, false, *video_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700827 }
hbos0adb8282016-11-23 02:32:06 -0800828 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -0700829 }
830 }
831}
832
hbosdf6075a2016-12-19 04:58:02 -0800833void RTCStatsCollector::ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700834 int64_t timestamp_us, const SessionStats& session_stats,
835 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
836 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800837 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700838 for (const auto& transport : session_stats.transport_stats) {
839 // Get reference to RTCP channel, if it exists.
840 std::string rtcp_transport_stats_id;
841 for (const auto& channel_stats : transport.second.channel_stats) {
842 if (channel_stats.component ==
843 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
844 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
845 transport.second.transport_name, channel_stats.component);
846 break;
847 }
848 }
849
850 // Get reference to local and remote certificates of this transport, if they
851 // exist.
852 const auto& certificate_stats_it = transport_cert_stats.find(
853 transport.second.transport_name);
854 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
855 std::string local_certificate_id;
856 if (certificate_stats_it->second.local) {
857 local_certificate_id = RTCCertificateIDFromFingerprint(
858 certificate_stats_it->second.local->fingerprint);
859 }
860 std::string remote_certificate_id;
861 if (certificate_stats_it->second.remote) {
862 remote_certificate_id = RTCCertificateIDFromFingerprint(
863 certificate_stats_it->second.remote->fingerprint);
864 }
865
866 // There is one transport stats for each channel.
867 for (const auto& channel_stats : transport.second.channel_stats) {
868 std::unique_ptr<RTCTransportStats> transport_stats(
869 new RTCTransportStats(
870 RTCTransportStatsIDFromTransportChannel(
871 transport.second.transport_name, channel_stats.component),
872 timestamp_us));
873 transport_stats->bytes_sent = 0;
874 transport_stats->bytes_received = 0;
875 transport_stats->active_connection = false;
876 for (const cricket::ConnectionInfo& info :
877 channel_stats.connection_infos) {
878 *transport_stats->bytes_sent += info.sent_total_bytes;
879 *transport_stats->bytes_received += info.recv_total_bytes;
880 if (info.best_connection) {
881 transport_stats->active_connection = true;
882 transport_stats->selected_candidate_pair_id =
883 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
884 }
885 }
886 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
887 !rtcp_transport_stats_id.empty()) {
888 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
889 }
890 if (!local_certificate_id.empty())
891 transport_stats->local_certificate_id = local_certificate_id;
892 if (!remote_certificate_id.empty())
893 transport_stats->remote_certificate_id = remote_certificate_id;
894 report->AddStats(std::move(transport_stats));
895 }
896 }
897}
898
899std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -0800900RTCStatsCollector::PrepareTransportCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700901 const SessionStats& session_stats) const {
hbosdf6075a2016-12-19 04:58:02 -0800902 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700903 std::map<std::string, CertificateStatsPair> transport_cert_stats;
904 for (const auto& transport_stats : session_stats.transport_stats) {
905 CertificateStatsPair certificate_stats_pair;
906 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
907 if (pc_->session()->GetLocalCertificate(
908 transport_stats.second.transport_name, &local_certificate)) {
909 certificate_stats_pair.local =
910 local_certificate->ssl_certificate().GetStats();
911 }
912 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
913 pc_->session()->GetRemoteSSLCertificate(
914 transport_stats.second.transport_name);
915 if (remote_certificate) {
916 certificate_stats_pair.remote = remote_certificate->GetStats();
917 }
918 transport_cert_stats.insert(
919 std::make_pair(transport_stats.second.transport_name,
920 std::move(certificate_stats_pair)));
921 }
922 return transport_cert_stats;
923}
924
hbosdf6075a2016-12-19 04:58:02 -0800925std::unique_ptr<RTCStatsCollector::MediaInfo>
926RTCStatsCollector::PrepareMediaInfo_s() const {
927 RTC_DCHECK(signaling_thread_->IsCurrent());
928 std::unique_ptr<MediaInfo> media_info(new MediaInfo());
hbos0adb8282016-11-23 02:32:06 -0800929 if (pc_->session()->voice_channel()) {
930 cricket::VoiceMediaInfo voice_media_info;
931 if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800932 media_info->voice = rtc::Optional<cricket::VoiceMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800933 std::move(voice_media_info));
934 }
935 }
936 if (pc_->session()->video_channel()) {
937 cricket::VideoMediaInfo video_media_info;
938 if (pc_->session()->video_channel()->GetStats(&video_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800939 media_info->video = rtc::Optional<cricket::VideoMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800940 std::move(video_media_info));
941 }
942 }
943 return media_info;
944}
945
hbos82ebe022016-11-14 01:41:09 -0800946void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
947 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
948 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
949}
950
951void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
952 RTC_DCHECK(signaling_thread_->IsCurrent());
953 bool result = internal_record_.opened_data_channels.insert(
954 reinterpret_cast<uintptr_t>(channel)).second;
955 ++internal_record_.data_channels_opened;
956 RTC_DCHECK(result);
957}
958
959void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
960 RTC_DCHECK(signaling_thread_->IsCurrent());
961 // Only channels that have been fully opened (and have increased the
962 // |data_channels_opened_| counter) increase the closed counter.
963 if (internal_record_.opened_data_channels.find(
964 reinterpret_cast<uintptr_t>(channel)) !=
965 internal_record_.opened_data_channels.end()) {
966 ++internal_record_.data_channels_closed;
967 }
968}
969
hboscc555c52016-10-18 12:48:31 -0700970const char* CandidateTypeToRTCIceCandidateTypeForTesting(
971 const std::string& type) {
972 return CandidateTypeToRTCIceCandidateType(type);
973}
974
975const char* DataStateToRTCDataChannelStateForTesting(
976 DataChannelInterface::DataState state) {
977 return DataStateToRTCDataChannelState(state);
978}
979
hbosd565b732016-08-30 14:04:35 -0700980} // namespace webrtc