blob: cf97057c1b6884595618b3083beeacad5727f93d [file] [log] [blame]
hbosd565b732016-08-30 14:04:35 -07001/*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
hbos74e1a4f2016-09-15 23:33:01 -070011#include "webrtc/api/rtcstatscollector.h"
hbosd565b732016-08-30 14:04:35 -070012
13#include <memory>
14#include <utility>
15#include <vector>
16
17#include "webrtc/api/peerconnection.h"
hbos09bc1282016-11-08 06:29:22 -080018#include "webrtc/api/peerconnectioninterface.h"
19#include "webrtc/api/mediastreaminterface.h"
hbos6ab97ce2016-10-03 14:16:56 -070020#include "webrtc/api/webrtcsession.h"
hbosd565b732016-08-30 14:04:35 -070021#include "webrtc/base/checks.h"
hbos6ded1902016-11-01 01:50:46 -070022#include "webrtc/base/timeutils.h"
23#include "webrtc/media/base/mediachannel.h"
hbosab9f6e42016-10-07 02:18:47 -070024#include "webrtc/p2p/base/candidate.h"
hbos2fa7c672016-10-24 04:00:05 -070025#include "webrtc/p2p/base/p2pconstants.h"
hbosab9f6e42016-10-07 02:18:47 -070026#include "webrtc/p2p/base/port.h"
hbosd565b732016-08-30 14:04:35 -070027
28namespace webrtc {
29
hboscc555c52016-10-18 12:48:31 -070030namespace {
31
hbos2fa7c672016-10-24 04:00:05 -070032std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
33 return "RTCCertificate_" + fingerprint;
34}
35
hbos0adb8282016-11-23 02:32:06 -080036std::string RTCCodecStatsIDFromDirectionMediaAndPayload(
37 bool inbound, bool audio, uint32_t payload_type) {
38 // TODO(hbos): When we are able to handle multiple m= lines of the same media
39 // type (and multiple BaseChannels for the same type is possible?) this needs
40 // to be updated to differentiate the transport being used, and stats need to
41 // be collected for all of them. crbug.com/659117
42 if (inbound) {
43 return audio ? "RTCCodec_InboundAudio_" + rtc::ToString<>(payload_type)
44 : "RTCCodec_InboundVideo_" + rtc::ToString<>(payload_type);
45 }
46 return audio ? "RTCCodec_OutboundAudio_" + rtc::ToString<>(payload_type)
47 : "RTCCodec_OutboundVideo_" + rtc::ToString<>(payload_type);
48}
49
hbos2fa7c672016-10-24 04:00:05 -070050std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
51 const cricket::ConnectionInfo& info) {
52 return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" +
53 info.remote_candidate.id();
54}
55
hbos09bc1282016-11-08 06:29:22 -080056std::string RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
57 const MediaStreamTrackInterface& track) {
58 return "RTCMediaStreamTrack_" + track.id();
59}
60
hbos2fa7c672016-10-24 04:00:05 -070061std::string RTCTransportStatsIDFromTransportChannel(
62 const std::string& transport_name, int channel_component) {
63 return "RTCTransport_" + transport_name + "_" +
64 rtc::ToString<>(channel_component);
65}
66
hbos6ded1902016-11-01 01:50:46 -070067std::string RTCTransportStatsIDFromBaseChannel(
68 const ProxyTransportMap& proxy_to_transport,
69 const cricket::BaseChannel& base_channel) {
70 auto proxy_it = proxy_to_transport.find(base_channel.content_name());
71 if (proxy_it == proxy_to_transport.cend())
72 return "";
73 return RTCTransportStatsIDFromTransportChannel(
74 proxy_it->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
75}
76
hboseeafe942016-11-01 03:00:17 -070077std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
78 return audio ? "RTCInboundRTPAudioStream_" + rtc::ToString<>(ssrc)
79 : "RTCInboundRTPVideoStream_" + rtc::ToString<>(ssrc);
80}
81
hbos6ded1902016-11-01 01:50:46 -070082std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
83 return audio ? "RTCOutboundRTPAudioStream_" + rtc::ToString<>(ssrc)
84 : "RTCOutboundRTPVideoStream_" + rtc::ToString<>(ssrc);
85}
86
hbosab9f6e42016-10-07 02:18:47 -070087const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
88 if (type == cricket::LOCAL_PORT_TYPE)
89 return RTCIceCandidateType::kHost;
90 if (type == cricket::STUN_PORT_TYPE)
91 return RTCIceCandidateType::kSrflx;
92 if (type == cricket::PRFLX_PORT_TYPE)
93 return RTCIceCandidateType::kPrflx;
94 if (type == cricket::RELAY_PORT_TYPE)
95 return RTCIceCandidateType::kRelay;
96 RTC_NOTREACHED();
97 return nullptr;
98}
99
hboscc555c52016-10-18 12:48:31 -0700100const char* DataStateToRTCDataChannelState(
101 DataChannelInterface::DataState state) {
102 switch (state) {
103 case DataChannelInterface::kConnecting:
104 return RTCDataChannelState::kConnecting;
105 case DataChannelInterface::kOpen:
106 return RTCDataChannelState::kOpen;
107 case DataChannelInterface::kClosing:
108 return RTCDataChannelState::kClosing;
109 case DataChannelInterface::kClosed:
110 return RTCDataChannelState::kClosed;
111 default:
112 RTC_NOTREACHED();
113 return nullptr;
114 }
115}
116
hbos0adb8282016-11-23 02:32:06 -0800117std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
118 uint64_t timestamp_us, bool inbound, bool audio,
119 const RtpCodecParameters& codec_params) {
120 RTC_DCHECK_GE(codec_params.payload_type, 0);
121 RTC_DCHECK_LE(codec_params.payload_type, 127);
122 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
123 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
124 RTCCodecStatsIDFromDirectionMediaAndPayload(inbound, audio, payload_type),
125 timestamp_us));
126 codec_stats->payload_type = payload_type;
127 codec_stats->codec = (audio ? "audio/" : "video/") + codec_params.mime_type;
128 codec_stats->clock_rate = static_cast<uint32_t>(codec_params.clock_rate);
129 return codec_stats;
130}
131
hbos09bc1282016-11-08 06:29:22 -0800132void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
133 const MediaStreamTrackInterface& track,
134 RTCMediaStreamTrackStats* track_stats) {
135 track_stats->track_identifier = track.id();
136 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
137}
138
hbos820f5782016-11-22 03:16:50 -0800139// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700140void SetInboundRTPStreamStatsFromMediaReceiverInfo(
141 const cricket::MediaReceiverInfo& media_receiver_info,
142 RTCInboundRTPStreamStats* inbound_stats) {
143 RTC_DCHECK(inbound_stats);
144 inbound_stats->ssrc = rtc::ToString<>(media_receiver_info.ssrc());
145 // TODO(hbos): Support the remote case. crbug.com/657855
146 inbound_stats->is_remote = false;
147 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
148 // |media_receiver_info.codec_name|. crbug.com/657854, 657855, 659117
149 inbound_stats->packets_received =
150 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
151 inbound_stats->bytes_received =
152 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800153 inbound_stats->packets_lost =
154 static_cast<uint32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700155 inbound_stats->fraction_lost =
156 static_cast<double>(media_receiver_info.fraction_lost);
157}
158
159void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
160 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800161 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700162 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800163 voice_receiver_info, inbound_audio);
164 inbound_audio->media_type = "audio";
165 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700166 static_cast<double>(voice_receiver_info.jitter_ms) /
167 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800168 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
169 // purposefully left undefined for audio.
hboseeafe942016-11-01 03:00:17 -0700170}
171
172void SetInboundRTPStreamStatsFromVideoReceiverInfo(
173 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800174 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700175 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800176 video_receiver_info, inbound_video);
177 inbound_video->media_type = "video";
178 inbound_video->fir_count =
179 static_cast<uint32_t>(video_receiver_info.firs_sent);
180 inbound_video->pli_count =
181 static_cast<uint32_t>(video_receiver_info.plis_sent);
182 inbound_video->nack_count =
183 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hboseeafe942016-11-01 03:00:17 -0700184}
185
hbos820f5782016-11-22 03:16:50 -0800186// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700187void SetOutboundRTPStreamStatsFromMediaSenderInfo(
188 const cricket::MediaSenderInfo& media_sender_info,
189 RTCOutboundRTPStreamStats* outbound_stats) {
190 RTC_DCHECK(outbound_stats);
191 outbound_stats->ssrc = rtc::ToString<>(media_sender_info.ssrc());
192 // TODO(hbos): Support the remote case. crbug.com/657856
193 outbound_stats->is_remote = false;
194 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
195 // |media_sender_info.codec_name|. crbug.com/657854, 657856, 659117
196 outbound_stats->packets_sent =
197 static_cast<uint32_t>(media_sender_info.packets_sent);
198 outbound_stats->bytes_sent =
199 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbose10e6d12016-12-15 01:54:29 -0800200 if (media_sender_info.rtt_ms >= 0) {
201 outbound_stats->round_trip_time = static_cast<double>(
202 media_sender_info.rtt_ms) / rtc::kNumMillisecsPerSec;
203 }
hbos6ded1902016-11-01 01:50:46 -0700204}
205
206void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
207 const cricket::VoiceSenderInfo& voice_sender_info,
208 RTCOutboundRTPStreamStats* outbound_audio) {
209 SetOutboundRTPStreamStatsFromMediaSenderInfo(
210 voice_sender_info, outbound_audio);
211 outbound_audio->media_type = "audio";
212 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
213 // purposefully left undefined for audio.
214}
215
216void SetOutboundRTPStreamStatsFromVideoSenderInfo(
217 const cricket::VideoSenderInfo& video_sender_info,
218 RTCOutboundRTPStreamStats* outbound_video) {
219 SetOutboundRTPStreamStatsFromMediaSenderInfo(
220 video_sender_info, outbound_video);
221 outbound_video->media_type = "video";
222 outbound_video->fir_count =
223 static_cast<uint32_t>(video_sender_info.firs_rcvd);
224 outbound_video->pli_count =
225 static_cast<uint32_t>(video_sender_info.plis_rcvd);
226 outbound_video->nack_count =
227 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
228}
229
hbos02ba2112016-10-28 05:14:53 -0700230void ProduceCertificateStatsFromSSLCertificateStats(
231 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
232 RTCStatsReport* report) {
233 RTCCertificateStats* prev_certificate_stats = nullptr;
234 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
235 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800236 std::string certificate_stats_id =
237 RTCCertificateIDFromFingerprint(s->fingerprint);
238 // It is possible for the same certificate to show up multiple times, e.g.
239 // if local and remote side use the same certificate in a loopback call.
240 // If the report already contains stats for this certificate, skip it.
241 if (report->Get(certificate_stats_id)) {
242 RTC_DCHECK_EQ(s, &certificate_stats);
243 break;
244 }
hbos02ba2112016-10-28 05:14:53 -0700245 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800246 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700247 certificate_stats->fingerprint = s->fingerprint;
248 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
249 certificate_stats->base64_certificate = s->base64_certificate;
250 if (prev_certificate_stats)
251 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
252 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
253 prev_certificate_stats = certificate_stats;
254 }
255}
256
257const std::string& ProduceIceCandidateStats(
258 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
259 RTCStatsReport* report) {
260 const std::string& id = "RTCIceCandidate_" + candidate.id();
261 const RTCStats* stats = report->Get(id);
262 if (!stats) {
263 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
264 if (is_local)
265 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
266 else
267 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
268 candidate_stats->ip = candidate.address().ipaddr().ToString();
269 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
270 candidate_stats->protocol = candidate.protocol();
271 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
272 candidate.type());
273 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
274
275 stats = candidate_stats.get();
276 report->AddStats(std::move(candidate_stats));
277 }
278 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
279 : RTCRemoteIceCandidateStats::kType);
280 return stats->id();
281}
282
hbos09bc1282016-11-08 06:29:22 -0800283void ProduceMediaStreamAndTrackStats(
284 int64_t timestamp_us,
285 rtc::scoped_refptr<StreamCollectionInterface> streams,
286 bool is_local,
287 RTCStatsReport* report) {
288 // TODO(hbos): When "AddTrack" is implemented we should iterate tracks to
289 // find which streams exist, not iterate streams to find tracks.
290 // crbug.com/659137
291 // TODO(hbos): Return stats of detached tracks. We have to perform stats
292 // gathering at the time of detachment to get accurate stats and timestamps.
293 // crbug.com/659137
294 if (!streams)
295 return;
296 for (size_t i = 0; i < streams->count(); ++i) {
297 MediaStreamInterface* stream = streams->at(i);
298
299 std::unique_ptr<RTCMediaStreamStats> stream_stats(
hbos02d2a922016-12-21 01:29:05 -0800300 new RTCMediaStreamStats(
301 (is_local ? "RTCMediaStream_local_" : "RTCMediaStream_remote_") +
302 stream->label(), timestamp_us));
hbos09bc1282016-11-08 06:29:22 -0800303 stream_stats->stream_identifier = stream->label();
304 stream_stats->track_ids = std::vector<std::string>();
305 // Audio Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800306 for (const rtc::scoped_refptr<AudioTrackInterface>& audio_track :
hbos09bc1282016-11-08 06:29:22 -0800307 stream->GetAudioTracks()) {
308 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
309 *audio_track.get());
310 if (report->Get(id)) {
311 // Skip track, stats already exist for it.
312 continue;
313 }
314 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
315 new RTCMediaStreamTrackStats(id, timestamp_us));
316 stream_stats->track_ids->push_back(audio_track_stats->id());
317 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
318 *audio_track.get(),
319 audio_track_stats.get());
320 audio_track_stats->remote_source = !is_local;
321 audio_track_stats->detached = false;
322 int signal_level;
323 if (audio_track->GetSignalLevel(&signal_level)) {
324 // Convert signal level from [0,32767] int to [0,1] double.
325 RTC_DCHECK_GE(signal_level, 0);
326 RTC_DCHECK_LE(signal_level, 32767);
327 audio_track_stats->audio_level = signal_level / 32767.0;
328 }
329 if (audio_track->GetAudioProcessor()) {
330 AudioProcessorInterface::AudioProcessorStats audio_processor_stats;
331 audio_track->GetAudioProcessor()->GetStats(&audio_processor_stats);
hbos9a394f02016-12-14 07:58:22 -0800332 if (audio_processor_stats.echo_return_loss != -100) {
333 audio_track_stats->echo_return_loss = static_cast<double>(
334 audio_processor_stats.echo_return_loss);
335 }
336 if (audio_processor_stats.echo_return_loss_enhancement != -100) {
337 audio_track_stats->echo_return_loss_enhancement = static_cast<double>(
338 audio_processor_stats.echo_return_loss_enhancement);
339 }
hbos09bc1282016-11-08 06:29:22 -0800340 }
341 report->AddStats(std::move(audio_track_stats));
342 }
343 // Video Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800344 for (const rtc::scoped_refptr<VideoTrackInterface>& video_track :
hbos09bc1282016-11-08 06:29:22 -0800345 stream->GetVideoTracks()) {
346 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
347 *video_track.get());
348 if (report->Get(id)) {
349 // Skip track, stats already exist for it.
350 continue;
351 }
352 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
353 new RTCMediaStreamTrackStats(id, timestamp_us));
354 stream_stats->track_ids->push_back(video_track_stats->id());
355 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
356 *video_track.get(),
357 video_track_stats.get());
358 video_track_stats->remote_source = !is_local;
359 video_track_stats->detached = false;
360 if (video_track->GetSource()) {
361 VideoTrackSourceInterface::Stats video_track_source_stats;
362 if (video_track->GetSource()->GetStats(&video_track_source_stats)) {
363 video_track_stats->frame_width = static_cast<uint32_t>(
364 video_track_source_stats.input_width);
365 video_track_stats->frame_height = static_cast<uint32_t>(
366 video_track_source_stats.input_height);
367 }
368 }
369 report->AddStats(std::move(video_track_stats));
370 }
371 report->AddStats(std::move(stream_stats));
372 }
373}
374
hboscc555c52016-10-18 12:48:31 -0700375} // namespace
376
hbosc82f2e12016-09-05 01:36:50 -0700377rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
378 PeerConnection* pc, int64_t cache_lifetime_us) {
379 return rtc::scoped_refptr<RTCStatsCollector>(
380 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
381}
382
383RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
384 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700385 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -0700386 signaling_thread_(pc->session()->signaling_thread()),
387 worker_thread_(pc->session()->worker_thread()),
388 network_thread_(pc->session()->network_thread()),
389 num_pending_partial_reports_(0),
390 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700391 cache_timestamp_us_(0),
392 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700393 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700394 RTC_DCHECK(signaling_thread_);
395 RTC_DCHECK(worker_thread_);
396 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700397 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbos82ebe022016-11-14 01:41:09 -0800398 pc_->SignalDataChannelCreated.connect(
399 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700400}
401
hbosb78306a2016-12-19 05:06:57 -0800402RTCStatsCollector::~RTCStatsCollector() {
403 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
404}
405
hbosc82f2e12016-09-05 01:36:50 -0700406void RTCStatsCollector::GetStatsReport(
407 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
408 RTC_DCHECK(signaling_thread_->IsCurrent());
409 RTC_DCHECK(callback);
410 callbacks_.push_back(callback);
411
hbos0e6758d2016-08-31 07:57:36 -0700412 // "Now" using a monotonically increasing timer.
413 int64_t cache_now_us = rtc::TimeMicros();
414 if (cached_report_ &&
415 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700416 // We have a fresh cached report to deliver.
417 DeliverCachedReport();
418 } else if (!num_pending_partial_reports_) {
419 // Only start gathering stats if we're not already gathering stats. In the
420 // case of already gathering stats, |callback_| will be invoked when there
421 // are no more pending partial reports.
422
423 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
424 // UTC), in microseconds. The system clock could be modified and is not
425 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700426 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700427
428 num_pending_partial_reports_ = 3;
429 partial_report_timestamp_us_ = cache_now_us;
430 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
431 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread,
432 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosdf6075a2016-12-19 04:58:02 -0800433
434 // TODO(hbos): No stats are gathered by
435 // |ProducePartialResultsOnWorkerThread|, remove it.
hbosc82f2e12016-09-05 01:36:50 -0700436 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_,
437 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread,
438 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosdf6075a2016-12-19 04:58:02 -0800439
440 // Prepare |channel_names_| and |media_info_| for use in
441 // |ProducePartialResultsOnNetworkThread|.
442 channel_name_pairs_.reset(new ChannelNamePairs());
443 if (pc_->session()->voice_channel()) {
444 channel_name_pairs_->voice = rtc::Optional<ChannelNamePair>(
445 ChannelNamePair(pc_->session()->voice_channel()->content_name(),
446 pc_->session()->voice_channel()->transport_name()));
447 }
448 if (pc_->session()->video_channel()) {
449 channel_name_pairs_->video = rtc::Optional<ChannelNamePair>(
450 ChannelNamePair(pc_->session()->video_channel()->content_name(),
451 pc_->session()->video_channel()->transport_name()));
452 }
453 if (pc_->session()->data_channel()) {
454 channel_name_pairs_->data = rtc::Optional<ChannelNamePair>(
455 ChannelNamePair(pc_->session()->data_channel()->content_name(),
456 pc_->session()->data_channel()->transport_name()));
457 }
458 media_info_.reset(PrepareMediaInfo_s().release());
hbosc82f2e12016-09-05 01:36:50 -0700459 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
460 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
461 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbos0e6758d2016-08-31 07:57:36 -0700462 }
hbosd565b732016-08-30 14:04:35 -0700463}
464
465void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700466 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700467 cached_report_ = nullptr;
468}
469
hbosb78306a2016-12-19 05:06:57 -0800470void RTCStatsCollector::WaitForPendingRequest() {
471 RTC_DCHECK(signaling_thread_->IsCurrent());
472 if (num_pending_partial_reports_) {
473 rtc::Thread::Current()->ProcessMessages(0);
474 while (num_pending_partial_reports_) {
475 rtc::Thread::Current()->SleepMs(1);
476 rtc::Thread::Current()->ProcessMessages(0);
477 }
478 }
479}
480
hbosc82f2e12016-09-05 01:36:50 -0700481void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
482 int64_t timestamp_us) {
483 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700484 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
485 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700486
hboscc555c52016-10-18 12:48:31 -0700487 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos09bc1282016-11-08 06:29:22 -0800488 ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700489 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700490
491 AddPartialResults(report);
492}
493
494void RTCStatsCollector::ProducePartialResultsOnWorkerThread(
495 int64_t timestamp_us) {
496 RTC_DCHECK(worker_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700497 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
498 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700499
hbosdf6075a2016-12-19 04:58:02 -0800500 // TODO(hbos): There are no stats to be gathered on this thread, remove this
501 // method.
hbosc82f2e12016-09-05 01:36:50 -0700502
503 AddPartialResults(report);
504}
505
506void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
507 int64_t timestamp_us) {
508 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700509 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
510 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700511
hbosdf6075a2016-12-19 04:58:02 -0800512 std::unique_ptr<SessionStats> session_stats =
513 pc_->session()->GetStats(*channel_name_pairs_);
514 if (session_stats) {
515 std::map<std::string, CertificateStatsPair> transport_cert_stats =
516 PrepareTransportCertificateStats_n(*session_stats);
517
518 ProduceCertificateStats_n(
519 timestamp_us, transport_cert_stats, report.get());
520 ProduceCodecStats_n(
521 timestamp_us, *media_info_, report.get());
522 ProduceIceCandidateAndPairStats_n(
523 timestamp_us, *session_stats, report.get());
524 ProduceRTPStreamStats_n(
525 timestamp_us, *session_stats, *media_info_, report.get());
526 ProduceTransportStats_n(
527 timestamp_us, *session_stats, transport_cert_stats, report.get());
528 }
hbosc82f2e12016-09-05 01:36:50 -0700529
530 AddPartialResults(report);
531}
532
533void RTCStatsCollector::AddPartialResults(
534 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
535 if (!signaling_thread_->IsCurrent()) {
536 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
537 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
538 rtc::scoped_refptr<RTCStatsCollector>(this),
539 partial_report));
540 return;
541 }
542 AddPartialResults_s(partial_report);
543}
544
545void RTCStatsCollector::AddPartialResults_s(
546 rtc::scoped_refptr<RTCStatsReport> partial_report) {
547 RTC_DCHECK(signaling_thread_->IsCurrent());
548 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
549 if (!partial_report_)
550 partial_report_ = partial_report;
551 else
552 partial_report_->TakeMembersFrom(partial_report);
553 --num_pending_partial_reports_;
554 if (!num_pending_partial_reports_) {
555 cache_timestamp_us_ = partial_report_timestamp_us_;
556 cached_report_ = partial_report_;
557 partial_report_ = nullptr;
558 DeliverCachedReport();
559 }
560}
561
562void RTCStatsCollector::DeliverCachedReport() {
563 RTC_DCHECK(signaling_thread_->IsCurrent());
564 RTC_DCHECK(!callbacks_.empty());
565 RTC_DCHECK(cached_report_);
566 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
567 callbacks_) {
568 callback->OnStatsDelivered(cached_report_);
569 }
570 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700571}
572
hbosdf6075a2016-12-19 04:58:02 -0800573void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700574 int64_t timestamp_us,
575 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700576 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800577 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700578 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
579 if (transport_cert_stats_pair.second.local) {
580 ProduceCertificateStatsFromSSLCertificateStats(
581 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700582 }
hbos02ba2112016-10-28 05:14:53 -0700583 if (transport_cert_stats_pair.second.remote) {
584 ProduceCertificateStatsFromSSLCertificateStats(
585 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700586 }
587 }
588}
589
hbosdf6075a2016-12-19 04:58:02 -0800590void RTCStatsCollector::ProduceCodecStats_n(
hbos0adb8282016-11-23 02:32:06 -0800591 int64_t timestamp_us, const MediaInfo& media_info,
592 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800593 RTC_DCHECK(network_thread_->IsCurrent());
hbos0adb8282016-11-23 02:32:06 -0800594 // Audio
595 if (media_info.voice) {
596 // Inbound
597 for (const auto& pair : media_info.voice->receive_codecs) {
598 report->AddStats(CodecStatsFromRtpCodecParameters(
599 timestamp_us, true, true, pair.second));
600 }
601 // Outbound
602 for (const auto& pair : media_info.voice->send_codecs) {
603 report->AddStats(CodecStatsFromRtpCodecParameters(
604 timestamp_us, false, true, pair.second));
605 }
606 }
607 // Video
608 if (media_info.video) {
609 // Inbound
610 for (const auto& pair : media_info.video->receive_codecs) {
611 report->AddStats(CodecStatsFromRtpCodecParameters(
612 timestamp_us, true, false, pair.second));
613 }
614 // Outbound
615 for (const auto& pair : media_info.video->send_codecs) {
616 report->AddStats(CodecStatsFromRtpCodecParameters(
617 timestamp_us, false, false, pair.second));
618 }
619 }
620}
621
hboscc555c52016-10-18 12:48:31 -0700622void RTCStatsCollector::ProduceDataChannelStats_s(
623 int64_t timestamp_us, RTCStatsReport* report) const {
624 RTC_DCHECK(signaling_thread_->IsCurrent());
625 for (const rtc::scoped_refptr<DataChannel>& data_channel :
626 pc_->sctp_data_channels()) {
627 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
628 new RTCDataChannelStats(
629 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
630 timestamp_us));
631 data_channel_stats->label = data_channel->label();
632 data_channel_stats->protocol = data_channel->protocol();
633 data_channel_stats->datachannelid = data_channel->id();
634 data_channel_stats->state =
635 DataStateToRTCDataChannelState(data_channel->state());
636 data_channel_stats->messages_sent = data_channel->messages_sent();
637 data_channel_stats->bytes_sent = data_channel->bytes_sent();
638 data_channel_stats->messages_received = data_channel->messages_received();
639 data_channel_stats->bytes_received = data_channel->bytes_received();
640 report->AddStats(std::move(data_channel_stats));
641 }
642}
643
hbosdf6075a2016-12-19 04:58:02 -0800644void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
hbosab9f6e42016-10-07 02:18:47 -0700645 int64_t timestamp_us, const SessionStats& session_stats,
646 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800647 RTC_DCHECK(network_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700648 for (const auto& transport_stats : session_stats.transport_stats) {
649 for (const auto& channel_stats : transport_stats.second.channel_stats) {
hbos0583b282016-11-30 01:50:14 -0800650 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
651 transport_stats.second.transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -0700652 for (const cricket::ConnectionInfo& info :
653 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700654 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700655 new RTCIceCandidatePairStats(
656 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
657 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700658
hbos0583b282016-11-30 01:50:14 -0800659 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -0700660 // TODO(hbos): There could be other candidates that are not paired with
661 // anything. We don't have a complete list. Local candidates come from
662 // Port objects, and prflx candidates (both local and remote) are only
663 // stored in candidate pairs. crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700664 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700665 timestamp_us, info.local_candidate, true, report);
hbos02ba2112016-10-28 05:14:53 -0700666 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700667 timestamp_us, info.remote_candidate, false, report);
hbosc47a0c32016-10-11 14:54:49 -0700668 // TODO(hbos): This writable is different than the spec. It goes to
669 // false after a certain amount of time without a response passes.
670 // crbug.com/633550
671 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700672 candidate_pair_stats->bytes_sent =
673 static_cast<uint64_t>(info.sent_total_bytes);
674 candidate_pair_stats->bytes_received =
675 static_cast<uint64_t>(info.recv_total_bytes);
hbosc47a0c32016-10-11 14:54:49 -0700676 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
677 // smoothed according to the spec. crbug.com/633550. See
678 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
hbos3168c7a2016-12-15 06:17:08 -0800679 candidate_pair_stats->current_round_trip_time =
hbos6ded1902016-11-01 01:50:46 -0700680 static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec;
hbosd82f5122016-12-09 04:12:39 -0800681 candidate_pair_stats->requests_received =
682 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -0800683 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
684 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700685 candidate_pair_stats->responses_received =
686 static_cast<uint64_t>(info.recv_ping_responses);
687 candidate_pair_stats->responses_sent =
688 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -0800689 RTC_DCHECK_GE(info.sent_ping_requests_total,
690 info.sent_ping_requests_before_first_response);
691 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
692 info.sent_ping_requests_total -
693 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -0700694
695 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700696 }
697 }
698 }
699}
700
hbos09bc1282016-11-08 06:29:22 -0800701void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s(
702 int64_t timestamp_us, RTCStatsReport* report) const {
703 RTC_DCHECK(signaling_thread_->IsCurrent());
704 ProduceMediaStreamAndTrackStats(
705 timestamp_us, pc_->local_streams(), true, report);
706 ProduceMediaStreamAndTrackStats(
707 timestamp_us, pc_->remote_streams(), false, report);
708}
709
hbos6ab97ce2016-10-03 14:16:56 -0700710void RTCStatsCollector::ProducePeerConnectionStats_s(
711 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700712 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700713 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700714 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -0800715 stats->data_channels_opened = internal_record_.data_channels_opened;
716 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -0700717 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700718}
719
hbosdf6075a2016-12-19 04:58:02 -0800720void RTCStatsCollector::ProduceRTPStreamStats_n(
hbos6ded1902016-11-01 01:50:46 -0700721 int64_t timestamp_us, const SessionStats& session_stats,
hbos0adb8282016-11-23 02:32:06 -0800722 const MediaInfo& media_info, RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800723 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700724
725 // Audio
hbos0adb8282016-11-23 02:32:06 -0800726 if (media_info.voice) {
727 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
728 session_stats.proxy_to_transport, *pc_->session()->voice_channel());
729 RTC_DCHECK(!transport_id.empty());
730 // Inbound
731 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
732 media_info.voice->receivers) {
733 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
734 // is fixed.
735 if (voice_receiver_info.ssrc() == 0)
736 continue;
737 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
738 new RTCInboundRTPStreamStats(
739 RTCInboundRTPStreamStatsIDFromSSRC(
740 true, voice_receiver_info.ssrc()),
741 timestamp_us));
742 SetInboundRTPStreamStatsFromVoiceReceiverInfo(
743 voice_receiver_info, inbound_audio.get());
744 inbound_audio->transport_id = transport_id;
745 if (voice_receiver_info.codec_payload_type) {
746 inbound_audio->codec_id =
747 RTCCodecStatsIDFromDirectionMediaAndPayload(
748 true, true, *voice_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700749 }
hbos0adb8282016-11-23 02:32:06 -0800750 report->AddStats(std::move(inbound_audio));
751 }
752 // Outbound
753 for (const cricket::VoiceSenderInfo& voice_sender_info :
754 media_info.voice->senders) {
755 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
756 // is fixed.
757 if (voice_sender_info.ssrc() == 0)
758 continue;
759 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
760 new RTCOutboundRTPStreamStats(
761 RTCOutboundRTPStreamStatsIDFromSSRC(
762 true, voice_sender_info.ssrc()),
763 timestamp_us));
764 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
765 voice_sender_info, outbound_audio.get());
766 outbound_audio->transport_id = transport_id;
767 if (voice_sender_info.codec_payload_type) {
768 outbound_audio->codec_id =
769 RTCCodecStatsIDFromDirectionMediaAndPayload(
770 false, true, *voice_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700771 }
hbos0adb8282016-11-23 02:32:06 -0800772 report->AddStats(std::move(outbound_audio));
hbos6ded1902016-11-01 01:50:46 -0700773 }
774 }
775 // Video
hbos0adb8282016-11-23 02:32:06 -0800776 if (media_info.video) {
777 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
778 session_stats.proxy_to_transport, *pc_->session()->video_channel());
779 RTC_DCHECK(!transport_id.empty());
780 // Inbound
781 for (const cricket::VideoReceiverInfo& video_receiver_info :
782 media_info.video->receivers) {
783 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
784 // is fixed.
785 if (video_receiver_info.ssrc() == 0)
786 continue;
787 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
788 new RTCInboundRTPStreamStats(
789 RTCInboundRTPStreamStatsIDFromSSRC(
790 false, video_receiver_info.ssrc()),
791 timestamp_us));
792 SetInboundRTPStreamStatsFromVideoReceiverInfo(
793 video_receiver_info, inbound_video.get());
794 inbound_video->transport_id = transport_id;
795 if (video_receiver_info.codec_payload_type) {
796 inbound_video->codec_id =
797 RTCCodecStatsIDFromDirectionMediaAndPayload(
798 true, false, *video_receiver_info.codec_payload_type);
hboseeafe942016-11-01 03:00:17 -0700799 }
hbos0adb8282016-11-23 02:32:06 -0800800 report->AddStats(std::move(inbound_video));
801 }
802 // Outbound
803 for (const cricket::VideoSenderInfo& video_sender_info :
804 media_info.video->senders) {
805 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
806 // is fixed.
807 if (video_sender_info.ssrc() == 0)
808 continue;
809 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
810 new RTCOutboundRTPStreamStats(
811 RTCOutboundRTPStreamStatsIDFromSSRC(
812 false, video_sender_info.ssrc()),
813 timestamp_us));
814 SetOutboundRTPStreamStatsFromVideoSenderInfo(
815 video_sender_info, outbound_video.get());
816 outbound_video->transport_id = transport_id;
817 if (video_sender_info.codec_payload_type) {
818 outbound_video->codec_id =
819 RTCCodecStatsIDFromDirectionMediaAndPayload(
820 false, false, *video_sender_info.codec_payload_type);
hbos6ded1902016-11-01 01:50:46 -0700821 }
hbos0adb8282016-11-23 02:32:06 -0800822 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -0700823 }
824 }
825}
826
hbosdf6075a2016-12-19 04:58:02 -0800827void RTCStatsCollector::ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700828 int64_t timestamp_us, const SessionStats& session_stats,
829 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
830 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800831 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700832 for (const auto& transport : session_stats.transport_stats) {
833 // Get reference to RTCP channel, if it exists.
834 std::string rtcp_transport_stats_id;
835 for (const auto& channel_stats : transport.second.channel_stats) {
836 if (channel_stats.component ==
837 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
838 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
839 transport.second.transport_name, channel_stats.component);
840 break;
841 }
842 }
843
844 // Get reference to local and remote certificates of this transport, if they
845 // exist.
846 const auto& certificate_stats_it = transport_cert_stats.find(
847 transport.second.transport_name);
848 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
849 std::string local_certificate_id;
850 if (certificate_stats_it->second.local) {
851 local_certificate_id = RTCCertificateIDFromFingerprint(
852 certificate_stats_it->second.local->fingerprint);
853 }
854 std::string remote_certificate_id;
855 if (certificate_stats_it->second.remote) {
856 remote_certificate_id = RTCCertificateIDFromFingerprint(
857 certificate_stats_it->second.remote->fingerprint);
858 }
859
860 // There is one transport stats for each channel.
861 for (const auto& channel_stats : transport.second.channel_stats) {
862 std::unique_ptr<RTCTransportStats> transport_stats(
863 new RTCTransportStats(
864 RTCTransportStatsIDFromTransportChannel(
865 transport.second.transport_name, channel_stats.component),
866 timestamp_us));
867 transport_stats->bytes_sent = 0;
868 transport_stats->bytes_received = 0;
869 transport_stats->active_connection = false;
870 for (const cricket::ConnectionInfo& info :
871 channel_stats.connection_infos) {
872 *transport_stats->bytes_sent += info.sent_total_bytes;
873 *transport_stats->bytes_received += info.recv_total_bytes;
874 if (info.best_connection) {
875 transport_stats->active_connection = true;
876 transport_stats->selected_candidate_pair_id =
877 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
878 }
879 }
880 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
881 !rtcp_transport_stats_id.empty()) {
882 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
883 }
884 if (!local_certificate_id.empty())
885 transport_stats->local_certificate_id = local_certificate_id;
886 if (!remote_certificate_id.empty())
887 transport_stats->remote_certificate_id = remote_certificate_id;
888 report->AddStats(std::move(transport_stats));
889 }
890 }
891}
892
893std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -0800894RTCStatsCollector::PrepareTransportCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700895 const SessionStats& session_stats) const {
hbosdf6075a2016-12-19 04:58:02 -0800896 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700897 std::map<std::string, CertificateStatsPair> transport_cert_stats;
898 for (const auto& transport_stats : session_stats.transport_stats) {
899 CertificateStatsPair certificate_stats_pair;
900 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
901 if (pc_->session()->GetLocalCertificate(
902 transport_stats.second.transport_name, &local_certificate)) {
903 certificate_stats_pair.local =
904 local_certificate->ssl_certificate().GetStats();
905 }
906 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
907 pc_->session()->GetRemoteSSLCertificate(
908 transport_stats.second.transport_name);
909 if (remote_certificate) {
910 certificate_stats_pair.remote = remote_certificate->GetStats();
911 }
912 transport_cert_stats.insert(
913 std::make_pair(transport_stats.second.transport_name,
914 std::move(certificate_stats_pair)));
915 }
916 return transport_cert_stats;
917}
918
hbosdf6075a2016-12-19 04:58:02 -0800919std::unique_ptr<RTCStatsCollector::MediaInfo>
920RTCStatsCollector::PrepareMediaInfo_s() const {
921 RTC_DCHECK(signaling_thread_->IsCurrent());
922 std::unique_ptr<MediaInfo> media_info(new MediaInfo());
hbos0adb8282016-11-23 02:32:06 -0800923 if (pc_->session()->voice_channel()) {
924 cricket::VoiceMediaInfo voice_media_info;
925 if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800926 media_info->voice = rtc::Optional<cricket::VoiceMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800927 std::move(voice_media_info));
928 }
929 }
930 if (pc_->session()->video_channel()) {
931 cricket::VideoMediaInfo video_media_info;
932 if (pc_->session()->video_channel()->GetStats(&video_media_info)) {
hbosdf6075a2016-12-19 04:58:02 -0800933 media_info->video = rtc::Optional<cricket::VideoMediaInfo>(
hbos0adb8282016-11-23 02:32:06 -0800934 std::move(video_media_info));
935 }
936 }
937 return media_info;
938}
939
hbos82ebe022016-11-14 01:41:09 -0800940void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
941 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
942 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
943}
944
945void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
946 RTC_DCHECK(signaling_thread_->IsCurrent());
947 bool result = internal_record_.opened_data_channels.insert(
948 reinterpret_cast<uintptr_t>(channel)).second;
949 ++internal_record_.data_channels_opened;
950 RTC_DCHECK(result);
951}
952
953void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
954 RTC_DCHECK(signaling_thread_->IsCurrent());
955 // Only channels that have been fully opened (and have increased the
956 // |data_channels_opened_| counter) increase the closed counter.
957 if (internal_record_.opened_data_channels.find(
958 reinterpret_cast<uintptr_t>(channel)) !=
959 internal_record_.opened_data_channels.end()) {
960 ++internal_record_.data_channels_closed;
961 }
962}
963
hboscc555c52016-10-18 12:48:31 -0700964const char* CandidateTypeToRTCIceCandidateTypeForTesting(
965 const std::string& type) {
966 return CandidateTypeToRTCIceCandidateType(type);
967}
968
969const char* DataStateToRTCDataChannelStateForTesting(
970 DataChannelInterface::DataState state) {
971 return DataStateToRTCDataChannelState(state);
972}
973
hbosd565b732016-08-30 14:04:35 -0700974} // namespace webrtc