blob: dc14970854f0e8ffa10894f019cedfd25d5e7448 [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 }
deadbeef953c2ce2017-01-09 14:53:41 -0800466 if (pc_->session()->rtp_data_channel()) {
467 channel_name_pairs_->data =
468 rtc::Optional<ChannelNamePair>(ChannelNamePair(
469 pc_->session()->rtp_data_channel()->content_name(),
470 pc_->session()->rtp_data_channel()->transport_name()));
471 }
472 if (pc_->session()->sctp_content_name()) {
hbosdf6075a2016-12-19 04:58:02 -0800473 channel_name_pairs_->data = rtc::Optional<ChannelNamePair>(
deadbeef953c2ce2017-01-09 14:53:41 -0800474 ChannelNamePair(*pc_->session()->sctp_content_name(),
475 *pc_->session()->sctp_transport_name()));
hbosdf6075a2016-12-19 04:58:02 -0800476 }
477 media_info_.reset(PrepareMediaInfo_s().release());
hbosf415f8a2017-01-02 04:28:51 -0800478
hbosc82f2e12016-09-05 01:36:50 -0700479 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
480 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
481 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800482 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700483 }
hbosd565b732016-08-30 14:04:35 -0700484}
485
486void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700487 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700488 cached_report_ = nullptr;
489}
490
hbosb78306a2016-12-19 05:06:57 -0800491void RTCStatsCollector::WaitForPendingRequest() {
492 RTC_DCHECK(signaling_thread_->IsCurrent());
493 if (num_pending_partial_reports_) {
494 rtc::Thread::Current()->ProcessMessages(0);
495 while (num_pending_partial_reports_) {
496 rtc::Thread::Current()->SleepMs(1);
497 rtc::Thread::Current()->ProcessMessages(0);
498 }
499 }
500}
501
hbosc82f2e12016-09-05 01:36:50 -0700502void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
503 int64_t timestamp_us) {
504 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700505 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
506 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700507
hboscc555c52016-10-18 12:48:31 -0700508 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos09bc1282016-11-08 06:29:22 -0800509 ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700510 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700511
512 AddPartialResults(report);
513}
514
hbosc82f2e12016-09-05 01:36:50 -0700515void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
516 int64_t timestamp_us) {
517 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700518 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
519 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700520
hbosdf6075a2016-12-19 04:58:02 -0800521 std::unique_ptr<SessionStats> session_stats =
522 pc_->session()->GetStats(*channel_name_pairs_);
523 if (session_stats) {
524 std::map<std::string, CertificateStatsPair> transport_cert_stats =
525 PrepareTransportCertificateStats_n(*session_stats);
526
527 ProduceCertificateStats_n(
528 timestamp_us, transport_cert_stats, report.get());
529 ProduceCodecStats_n(
530 timestamp_us, *media_info_, report.get());
531 ProduceIceCandidateAndPairStats_n(
532 timestamp_us, *session_stats, report.get());
533 ProduceRTPStreamStats_n(
534 timestamp_us, *session_stats, *media_info_, report.get());
535 ProduceTransportStats_n(
536 timestamp_us, *session_stats, transport_cert_stats, report.get());
537 }
hbosc82f2e12016-09-05 01:36:50 -0700538
539 AddPartialResults(report);
540}
541
542void RTCStatsCollector::AddPartialResults(
543 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
544 if (!signaling_thread_->IsCurrent()) {
545 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
546 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
547 rtc::scoped_refptr<RTCStatsCollector>(this),
548 partial_report));
549 return;
550 }
551 AddPartialResults_s(partial_report);
552}
553
554void RTCStatsCollector::AddPartialResults_s(
555 rtc::scoped_refptr<RTCStatsReport> partial_report) {
556 RTC_DCHECK(signaling_thread_->IsCurrent());
557 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
558 if (!partial_report_)
559 partial_report_ = partial_report;
560 else
561 partial_report_->TakeMembersFrom(partial_report);
562 --num_pending_partial_reports_;
563 if (!num_pending_partial_reports_) {
564 cache_timestamp_us_ = partial_report_timestamp_us_;
565 cached_report_ = partial_report_;
566 partial_report_ = nullptr;
567 DeliverCachedReport();
568 }
569}
570
571void RTCStatsCollector::DeliverCachedReport() {
572 RTC_DCHECK(signaling_thread_->IsCurrent());
573 RTC_DCHECK(!callbacks_.empty());
574 RTC_DCHECK(cached_report_);
575 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
576 callbacks_) {
577 callback->OnStatsDelivered(cached_report_);
578 }
579 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700580}
581
hbosdf6075a2016-12-19 04:58:02 -0800582void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700583 int64_t timestamp_us,
584 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700585 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800586 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700587 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
588 if (transport_cert_stats_pair.second.local) {
589 ProduceCertificateStatsFromSSLCertificateStats(
590 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700591 }
hbos02ba2112016-10-28 05:14:53 -0700592 if (transport_cert_stats_pair.second.remote) {
593 ProduceCertificateStatsFromSSLCertificateStats(
594 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700595 }
596 }
597}
598
hbosdf6075a2016-12-19 04:58:02 -0800599void RTCStatsCollector::ProduceCodecStats_n(
hbos0adb8282016-11-23 02:32:06 -0800600 int64_t timestamp_us, const MediaInfo& media_info,
601 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800602 RTC_DCHECK(network_thread_->IsCurrent());
hbos0adb8282016-11-23 02:32:06 -0800603 // Audio
604 if (media_info.voice) {
605 // Inbound
606 for (const auto& pair : media_info.voice->receive_codecs) {
607 report->AddStats(CodecStatsFromRtpCodecParameters(
608 timestamp_us, true, true, pair.second));
609 }
610 // Outbound
611 for (const auto& pair : media_info.voice->send_codecs) {
612 report->AddStats(CodecStatsFromRtpCodecParameters(
613 timestamp_us, false, true, pair.second));
614 }
615 }
616 // Video
617 if (media_info.video) {
618 // Inbound
619 for (const auto& pair : media_info.video->receive_codecs) {
620 report->AddStats(CodecStatsFromRtpCodecParameters(
621 timestamp_us, true, false, pair.second));
622 }
623 // Outbound
624 for (const auto& pair : media_info.video->send_codecs) {
625 report->AddStats(CodecStatsFromRtpCodecParameters(
626 timestamp_us, false, false, pair.second));
627 }
628 }
629}
630
hboscc555c52016-10-18 12:48:31 -0700631void RTCStatsCollector::ProduceDataChannelStats_s(
632 int64_t timestamp_us, RTCStatsReport* report) const {
633 RTC_DCHECK(signaling_thread_->IsCurrent());
634 for (const rtc::scoped_refptr<DataChannel>& data_channel :
635 pc_->sctp_data_channels()) {
636 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
637 new RTCDataChannelStats(
638 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
639 timestamp_us));
640 data_channel_stats->label = data_channel->label();
641 data_channel_stats->protocol = data_channel->protocol();
642 data_channel_stats->datachannelid = data_channel->id();
643 data_channel_stats->state =
644 DataStateToRTCDataChannelState(data_channel->state());
645 data_channel_stats->messages_sent = data_channel->messages_sent();
646 data_channel_stats->bytes_sent = data_channel->bytes_sent();
647 data_channel_stats->messages_received = data_channel->messages_received();
648 data_channel_stats->bytes_received = data_channel->bytes_received();
649 report->AddStats(std::move(data_channel_stats));
650 }
651}
652
hbosdf6075a2016-12-19 04:58:02 -0800653void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
hbosab9f6e42016-10-07 02:18:47 -0700654 int64_t timestamp_us, const SessionStats& session_stats,
655 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800656 RTC_DCHECK(network_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700657 for (const auto& transport_stats : session_stats.transport_stats) {
658 for (const auto& channel_stats : transport_stats.second.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800659 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
660 transport_stats.second.transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700661 for (const cricket::ConnectionInfo& info :
662 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700663 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700664 new RTCIceCandidatePairStats(
665 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
666 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700667
hbos0583b282016-11-30 01:50:14 -0800668 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700669 // TODO(hbos): There could be other candidates that are not paired with
670 // anything. We don't have a complete list. Local candidates come from
671 // Port objects, and prflx candidates (both local and remote) are only
672 // stored in candidate pairs. crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700673 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800674 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -0700675 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -0800676 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -0800677 candidate_pair_stats->state =
678 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
679 candidate_pair_stats->priority = info.priority;
hbosc47a0c32016-10-11 14:54:49 -0700680 // TODO(hbos): This writable is different than the spec. It goes to
681 // false after a certain amount of time without a response passes.
682 // crbug.com/633550
683 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700684 candidate_pair_stats->bytes_sent =
685 static_cast<uint64_t>(info.sent_total_bytes);
686 candidate_pair_stats->bytes_received =
687 static_cast<uint64_t>(info.recv_total_bytes);
hbosc47a0c32016-10-11 14:54:49 -0700688 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
689 // smoothed according to the spec. crbug.com/633550. See
690 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
hbos3168c7a2016-12-15 06:17:08 -0800691 candidate_pair_stats->current_round_trip_time =
hbos6ded1902016-11-01 01:50:46 -0700692 static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec;
hbosd82f5122016-12-09 04:12:39 -0800693 candidate_pair_stats->requests_received =
694 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -0800695 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
696 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700697 candidate_pair_stats->responses_received =
698 static_cast<uint64_t>(info.recv_ping_responses);
699 candidate_pair_stats->responses_sent =
700 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -0800701 RTC_DCHECK_GE(info.sent_ping_requests_total,
702 info.sent_ping_requests_before_first_response);
703 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
704 info.sent_ping_requests_total -
705 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700706
707 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700708 }
709 }
710 }
711}
712
hbos09bc1282016-11-08 06:29:22 -0800713void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s(
714 int64_t timestamp_us, RTCStatsReport* report) const {
715 RTC_DCHECK(signaling_thread_->IsCurrent());
716 ProduceMediaStreamAndTrackStats(
717 timestamp_us, pc_->local_streams(), true, report);
718 ProduceMediaStreamAndTrackStats(
719 timestamp_us, pc_->remote_streams(), false, report);
720}
721
hbos6ab97ce2016-10-03 14:16:56 -0700722void RTCStatsCollector::ProducePeerConnectionStats_s(
723 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700724 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700725 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700726 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -0800727 stats->data_channels_opened = internal_record_.data_channels_opened;
728 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -0700729 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700730}
731
hbosdf6075a2016-12-19 04:58:02 -0800732void RTCStatsCollector::ProduceRTPStreamStats_n(
hbos6ded1902016-11-01 01:50:46 -0700733 int64_t timestamp_us, const SessionStats& session_stats,
hbos0adb8282016-11-23 02:32:06 -0800734 const MediaInfo& media_info, RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800735 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700736
737 // Audio
hbos0adb8282016-11-23 02:32:06 -0800738 if (media_info.voice) {
739 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
740 session_stats.proxy_to_transport, *pc_->session()->voice_channel());
741 RTC_DCHECK(!transport_id.empty());
742 // Inbound
743 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
744 media_info.voice->receivers) {
745 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
746 // is fixed.
747 if (voice_receiver_info.ssrc() == 0)
748 continue;
749 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
750 new RTCInboundRTPStreamStats(
751 RTCInboundRTPStreamStatsIDFromSSRC(
752 true, voice_receiver_info.ssrc()),
753 timestamp_us));
754 SetInboundRTPStreamStatsFromVoiceReceiverInfo(
755 voice_receiver_info, inbound_audio.get());
756 inbound_audio->transport_id = transport_id;
757 if (voice_receiver_info.codec_payload_type) {
758 inbound_audio->codec_id =
759 RTCCodecStatsIDFromDirectionMediaAndPayload(
760 true, true, *voice_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700761 }
hbos0adb8282016-11-23 02:32:06 -0800762 report->AddStats(std::move(inbound_audio));
763 }
764 // Outbound
765 for (const cricket::VoiceSenderInfo& voice_sender_info :
766 media_info.voice->senders) {
767 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
768 // is fixed.
769 if (voice_sender_info.ssrc() == 0)
770 continue;
771 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
772 new RTCOutboundRTPStreamStats(
773 RTCOutboundRTPStreamStatsIDFromSSRC(
774 true, voice_sender_info.ssrc()),
775 timestamp_us));
776 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
777 voice_sender_info, outbound_audio.get());
778 outbound_audio->transport_id = transport_id;
779 if (voice_sender_info.codec_payload_type) {
780 outbound_audio->codec_id =
781 RTCCodecStatsIDFromDirectionMediaAndPayload(
782 false, true, *voice_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700783 }
hbos0adb8282016-11-23 02:32:06 -0800784 report->AddStats(std::move(outbound_audio));
hbos6ded1902016-11-01 01:50:46 -0700785 }
786 }
787 // Video
hbos0adb8282016-11-23 02:32:06 -0800788 if (media_info.video) {
789 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
790 session_stats.proxy_to_transport, *pc_->session()->video_channel());
791 RTC_DCHECK(!transport_id.empty());
792 // Inbound
793 for (const cricket::VideoReceiverInfo& video_receiver_info :
794 media_info.video->receivers) {
795 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
796 // is fixed.
797 if (video_receiver_info.ssrc() == 0)
798 continue;
799 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
800 new RTCInboundRTPStreamStats(
801 RTCInboundRTPStreamStatsIDFromSSRC(
802 false, video_receiver_info.ssrc()),
803 timestamp_us));
804 SetInboundRTPStreamStatsFromVideoReceiverInfo(
805 video_receiver_info, inbound_video.get());
806 inbound_video->transport_id = transport_id;
807 if (video_receiver_info.codec_payload_type) {
808 inbound_video->codec_id =
809 RTCCodecStatsIDFromDirectionMediaAndPayload(
810 true, false, *video_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700811 }
hbos0adb8282016-11-23 02:32:06 -0800812 report->AddStats(std::move(inbound_video));
813 }
814 // Outbound
815 for (const cricket::VideoSenderInfo& video_sender_info :
816 media_info.video->senders) {
817 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
818 // is fixed.
819 if (video_sender_info.ssrc() == 0)
820 continue;
821 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
822 new RTCOutboundRTPStreamStats(
823 RTCOutboundRTPStreamStatsIDFromSSRC(
824 false, video_sender_info.ssrc()),
825 timestamp_us));
826 SetOutboundRTPStreamStatsFromVideoSenderInfo(
827 video_sender_info, outbound_video.get());
828 outbound_video->transport_id = transport_id;
829 if (video_sender_info.codec_payload_type) {
830 outbound_video->codec_id =
831 RTCCodecStatsIDFromDirectionMediaAndPayload(
832 false, false, *video_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700833 }
hbos0adb8282016-11-23 02:32:06 -0800834 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -0700835 }
836 }
837}
838
hbosdf6075a2016-12-19 04:58:02 -0800839void RTCStatsCollector::ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700840 int64_t timestamp_us, const SessionStats& session_stats,
841 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
842 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800843 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700844 for (const auto& transport : session_stats.transport_stats) {
845 // Get reference to RTCP channel, if it exists.
846 std::string rtcp_transport_stats_id;
847 for (const auto& channel_stats : transport.second.channel_stats) {
848 if (channel_stats.component ==
849 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
850 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
851 transport.second.transport_name, channel_stats.component);
852 break;
853 }
854 }
855
856 // Get reference to local and remote certificates of this transport, if they
857 // exist.
858 const auto& certificate_stats_it = transport_cert_stats.find(
859 transport.second.transport_name);
860 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
861 std::string local_certificate_id;
862 if (certificate_stats_it->second.local) {
863 local_certificate_id = RTCCertificateIDFromFingerprint(
864 certificate_stats_it->second.local->fingerprint);
865 }
866 std::string remote_certificate_id;
867 if (certificate_stats_it->second.remote) {
868 remote_certificate_id = RTCCertificateIDFromFingerprint(
869 certificate_stats_it->second.remote->fingerprint);
870 }
871
872 // There is one transport stats for each channel.
873 for (const auto& channel_stats : transport.second.channel_stats) {
874 std::unique_ptr<RTCTransportStats> transport_stats(
875 new RTCTransportStats(
876 RTCTransportStatsIDFromTransportChannel(
877 transport.second.transport_name, channel_stats.component),
878 timestamp_us));
879 transport_stats->bytes_sent = 0;
880 transport_stats->bytes_received = 0;
881 transport_stats->active_connection = false;
882 for (const cricket::ConnectionInfo& info :
883 channel_stats.connection_infos) {
884 *transport_stats->bytes_sent += info.sent_total_bytes;
885 *transport_stats->bytes_received += info.recv_total_bytes;
886 if (info.best_connection) {
887 transport_stats->active_connection = true;
888 transport_stats->selected_candidate_pair_id =
889 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
890 }
891 }
892 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
893 !rtcp_transport_stats_id.empty()) {
894 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
895 }
896 if (!local_certificate_id.empty())
897 transport_stats->local_certificate_id = local_certificate_id;
898 if (!remote_certificate_id.empty())
899 transport_stats->remote_certificate_id = remote_certificate_id;
900 report->AddStats(std::move(transport_stats));
901 }
902 }
903}
904
905std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -0800906RTCStatsCollector::PrepareTransportCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700907 const SessionStats& session_stats) const {
hbosdf6075a2016-12-19 04:58:02 -0800908 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700909 std::map<std::string, CertificateStatsPair> transport_cert_stats;
910 for (const auto& transport_stats : session_stats.transport_stats) {
911 CertificateStatsPair certificate_stats_pair;
912 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
913 if (pc_->session()->GetLocalCertificate(
914 transport_stats.second.transport_name, &local_certificate)) {
915 certificate_stats_pair.local =
916 local_certificate->ssl_certificate().GetStats();
917 }
918 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
919 pc_->session()->GetRemoteSSLCertificate(
920 transport_stats.second.transport_name);
921 if (remote_certificate) {
922 certificate_stats_pair.remote = remote_certificate->GetStats();
923 }
924 transport_cert_stats.insert(
925 std::make_pair(transport_stats.second.transport_name,
926 std::move(certificate_stats_pair)));
927 }
928 return transport_cert_stats;
929}
930
hbosdf6075a2016-12-19 04:58:02 -0800931std::unique_ptr<RTCStatsCollector::MediaInfo>
932RTCStatsCollector::PrepareMediaInfo_s() const {
933 RTC_DCHECK(signaling_thread_->IsCurrent());
934 std::unique_ptr<MediaInfo> media_info(new MediaInfo());
hbos0adb8282016-11-23 02:32:06 -0800935 if (pc_->session()->voice_channel()) {
936 cricket::VoiceMediaInfo voice_media_info;
937 if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800938 media_info->voice = rtc::Optional<cricket::VoiceMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800939 std::move(voice_media_info));
940 }
941 }
942 if (pc_->session()->video_channel()) {
943 cricket::VideoMediaInfo video_media_info;
944 if (pc_->session()->video_channel()->GetStats(&video_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800945 media_info->video = rtc::Optional<cricket::VideoMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800946 std::move(video_media_info));
947 }
948 }
949 return media_info;
950}
951
hbos82ebe022016-11-14 01:41:09 -0800952void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
953 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
954 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
955}
956
957void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
958 RTC_DCHECK(signaling_thread_->IsCurrent());
959 bool result = internal_record_.opened_data_channels.insert(
960 reinterpret_cast<uintptr_t>(channel)).second;
961 ++internal_record_.data_channels_opened;
962 RTC_DCHECK(result);
963}
964
965void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
966 RTC_DCHECK(signaling_thread_->IsCurrent());
967 // Only channels that have been fully opened (and have increased the
968 // |data_channels_opened_| counter) increase the closed counter.
969 if (internal_record_.opened_data_channels.find(
970 reinterpret_cast<uintptr_t>(channel)) !=
971 internal_record_.opened_data_channels.end()) {
972 ++internal_record_.data_channels_closed;
973 }
974}
975
hboscc555c52016-10-18 12:48:31 -0700976const char* CandidateTypeToRTCIceCandidateTypeForTesting(
977 const std::string& type) {
978 return CandidateTypeToRTCIceCandidateType(type);
979}
980
981const char* DataStateToRTCDataChannelStateForTesting(
982 DataChannelInterface::DataState state) {
983 return DataStateToRTCDataChannelState(state);
984}
985
hbosd565b732016-08-30 14:04:35 -0700986} // namespace webrtc