blob: d1145ebc531ce12caee7eaba1dd59ad181c04d0d [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
36std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
37 const cricket::ConnectionInfo& info) {
38 return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" +
39 info.remote_candidate.id();
40}
41
hbos09bc1282016-11-08 06:29:22 -080042std::string RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
43 const MediaStreamTrackInterface& track) {
44 return "RTCMediaStreamTrack_" + track.id();
45}
46
hbos2fa7c672016-10-24 04:00:05 -070047std::string RTCTransportStatsIDFromTransportChannel(
48 const std::string& transport_name, int channel_component) {
49 return "RTCTransport_" + transport_name + "_" +
50 rtc::ToString<>(channel_component);
51}
52
hbos6ded1902016-11-01 01:50:46 -070053std::string RTCTransportStatsIDFromBaseChannel(
54 const ProxyTransportMap& proxy_to_transport,
55 const cricket::BaseChannel& base_channel) {
56 auto proxy_it = proxy_to_transport.find(base_channel.content_name());
57 if (proxy_it == proxy_to_transport.cend())
58 return "";
59 return RTCTransportStatsIDFromTransportChannel(
60 proxy_it->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
61}
62
hboseeafe942016-11-01 03:00:17 -070063std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
64 return audio ? "RTCInboundRTPAudioStream_" + rtc::ToString<>(ssrc)
65 : "RTCInboundRTPVideoStream_" + rtc::ToString<>(ssrc);
66}
67
hbos6ded1902016-11-01 01:50:46 -070068std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
69 return audio ? "RTCOutboundRTPAudioStream_" + rtc::ToString<>(ssrc)
70 : "RTCOutboundRTPVideoStream_" + rtc::ToString<>(ssrc);
71}
72
hbosab9f6e42016-10-07 02:18:47 -070073const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
74 if (type == cricket::LOCAL_PORT_TYPE)
75 return RTCIceCandidateType::kHost;
76 if (type == cricket::STUN_PORT_TYPE)
77 return RTCIceCandidateType::kSrflx;
78 if (type == cricket::PRFLX_PORT_TYPE)
79 return RTCIceCandidateType::kPrflx;
80 if (type == cricket::RELAY_PORT_TYPE)
81 return RTCIceCandidateType::kRelay;
82 RTC_NOTREACHED();
83 return nullptr;
84}
85
hboscc555c52016-10-18 12:48:31 -070086const char* DataStateToRTCDataChannelState(
87 DataChannelInterface::DataState state) {
88 switch (state) {
89 case DataChannelInterface::kConnecting:
90 return RTCDataChannelState::kConnecting;
91 case DataChannelInterface::kOpen:
92 return RTCDataChannelState::kOpen;
93 case DataChannelInterface::kClosing:
94 return RTCDataChannelState::kClosing;
95 case DataChannelInterface::kClosed:
96 return RTCDataChannelState::kClosed;
97 default:
98 RTC_NOTREACHED();
99 return nullptr;
100 }
101}
102
hbos09bc1282016-11-08 06:29:22 -0800103void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
104 const MediaStreamTrackInterface& track,
105 RTCMediaStreamTrackStats* track_stats) {
106 track_stats->track_identifier = track.id();
107 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
108}
109
hboseeafe942016-11-01 03:00:17 -0700110void SetInboundRTPStreamStatsFromMediaReceiverInfo(
111 const cricket::MediaReceiverInfo& media_receiver_info,
112 RTCInboundRTPStreamStats* inbound_stats) {
113 RTC_DCHECK(inbound_stats);
114 inbound_stats->ssrc = rtc::ToString<>(media_receiver_info.ssrc());
115 // TODO(hbos): Support the remote case. crbug.com/657855
116 inbound_stats->is_remote = false;
117 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
118 // |media_receiver_info.codec_name|. crbug.com/657854, 657855, 659117
119 inbound_stats->packets_received =
120 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
121 inbound_stats->bytes_received =
122 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
123 inbound_stats->fraction_lost =
124 static_cast<double>(media_receiver_info.fraction_lost);
125}
126
127void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
128 const cricket::VoiceReceiverInfo& voice_receiver_info,
129 RTCInboundRTPStreamStats* inbound_stats) {
130 SetInboundRTPStreamStatsFromMediaReceiverInfo(
131 voice_receiver_info, inbound_stats);
132 inbound_stats->media_type = "audio";
133 inbound_stats->jitter =
134 static_cast<double>(voice_receiver_info.jitter_ms) /
135 rtc::kNumMillisecsPerSec;
136}
137
138void SetInboundRTPStreamStatsFromVideoReceiverInfo(
139 const cricket::VideoReceiverInfo& video_receiver_info,
140 RTCInboundRTPStreamStats* inbound_stats) {
141 SetInboundRTPStreamStatsFromMediaReceiverInfo(
142 video_receiver_info, inbound_stats);
143 inbound_stats->media_type = "video";
144}
145
hbos6ded1902016-11-01 01:50:46 -0700146void SetOutboundRTPStreamStatsFromMediaSenderInfo(
147 const cricket::MediaSenderInfo& media_sender_info,
148 RTCOutboundRTPStreamStats* outbound_stats) {
149 RTC_DCHECK(outbound_stats);
150 outbound_stats->ssrc = rtc::ToString<>(media_sender_info.ssrc());
151 // TODO(hbos): Support the remote case. crbug.com/657856
152 outbound_stats->is_remote = false;
153 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
154 // |media_sender_info.codec_name|. crbug.com/657854, 657856, 659117
155 outbound_stats->packets_sent =
156 static_cast<uint32_t>(media_sender_info.packets_sent);
157 outbound_stats->bytes_sent =
158 static_cast<uint64_t>(media_sender_info.bytes_sent);
159 outbound_stats->round_trip_time =
160 static_cast<double>(media_sender_info.rtt_ms) / rtc::kNumMillisecsPerSec;
161}
162
163void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
164 const cricket::VoiceSenderInfo& voice_sender_info,
165 RTCOutboundRTPStreamStats* outbound_audio) {
166 SetOutboundRTPStreamStatsFromMediaSenderInfo(
167 voice_sender_info, outbound_audio);
168 outbound_audio->media_type = "audio";
169 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
170 // purposefully left undefined for audio.
171}
172
173void SetOutboundRTPStreamStatsFromVideoSenderInfo(
174 const cricket::VideoSenderInfo& video_sender_info,
175 RTCOutboundRTPStreamStats* outbound_video) {
176 SetOutboundRTPStreamStatsFromMediaSenderInfo(
177 video_sender_info, outbound_video);
178 outbound_video->media_type = "video";
179 outbound_video->fir_count =
180 static_cast<uint32_t>(video_sender_info.firs_rcvd);
181 outbound_video->pli_count =
182 static_cast<uint32_t>(video_sender_info.plis_rcvd);
183 outbound_video->nack_count =
184 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
185}
186
hbos02ba2112016-10-28 05:14:53 -0700187void ProduceCertificateStatsFromSSLCertificateStats(
188 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
189 RTCStatsReport* report) {
190 RTCCertificateStats* prev_certificate_stats = nullptr;
191 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
192 s = s->issuer.get()) {
193 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
194 RTCCertificateIDFromFingerprint(s->fingerprint), timestamp_us);
195 certificate_stats->fingerprint = s->fingerprint;
196 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
197 certificate_stats->base64_certificate = s->base64_certificate;
198 if (prev_certificate_stats)
199 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
200 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
201 prev_certificate_stats = certificate_stats;
202 }
203}
204
205const std::string& ProduceIceCandidateStats(
206 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
207 RTCStatsReport* report) {
208 const std::string& id = "RTCIceCandidate_" + candidate.id();
209 const RTCStats* stats = report->Get(id);
210 if (!stats) {
211 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
212 if (is_local)
213 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
214 else
215 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
216 candidate_stats->ip = candidate.address().ipaddr().ToString();
217 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
218 candidate_stats->protocol = candidate.protocol();
219 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
220 candidate.type());
221 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
222
223 stats = candidate_stats.get();
224 report->AddStats(std::move(candidate_stats));
225 }
226 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
227 : RTCRemoteIceCandidateStats::kType);
228 return stats->id();
229}
230
hbos09bc1282016-11-08 06:29:22 -0800231void ProduceMediaStreamAndTrackStats(
232 int64_t timestamp_us,
233 rtc::scoped_refptr<StreamCollectionInterface> streams,
234 bool is_local,
235 RTCStatsReport* report) {
236 // TODO(hbos): When "AddTrack" is implemented we should iterate tracks to
237 // find which streams exist, not iterate streams to find tracks.
238 // crbug.com/659137
239 // TODO(hbos): Return stats of detached tracks. We have to perform stats
240 // gathering at the time of detachment to get accurate stats and timestamps.
241 // crbug.com/659137
242 if (!streams)
243 return;
244 for (size_t i = 0; i < streams->count(); ++i) {
245 MediaStreamInterface* stream = streams->at(i);
246
247 std::unique_ptr<RTCMediaStreamStats> stream_stats(
248 new RTCMediaStreamStats("RTCMediaStream_" + stream->label(),
249 timestamp_us));
250 stream_stats->stream_identifier = stream->label();
251 stream_stats->track_ids = std::vector<std::string>();
252 // Audio Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800253 for (const rtc::scoped_refptr<AudioTrackInterface>& audio_track :
hbos09bc1282016-11-08 06:29:22 -0800254 stream->GetAudioTracks()) {
255 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
256 *audio_track.get());
257 if (report->Get(id)) {
258 // Skip track, stats already exist for it.
259 continue;
260 }
261 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
262 new RTCMediaStreamTrackStats(id, timestamp_us));
263 stream_stats->track_ids->push_back(audio_track_stats->id());
264 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
265 *audio_track.get(),
266 audio_track_stats.get());
267 audio_track_stats->remote_source = !is_local;
268 audio_track_stats->detached = false;
269 int signal_level;
270 if (audio_track->GetSignalLevel(&signal_level)) {
271 // Convert signal level from [0,32767] int to [0,1] double.
272 RTC_DCHECK_GE(signal_level, 0);
273 RTC_DCHECK_LE(signal_level, 32767);
274 audio_track_stats->audio_level = signal_level / 32767.0;
275 }
276 if (audio_track->GetAudioProcessor()) {
277 AudioProcessorInterface::AudioProcessorStats audio_processor_stats;
278 audio_track->GetAudioProcessor()->GetStats(&audio_processor_stats);
279 audio_track_stats->echo_return_loss = static_cast<double>(
280 audio_processor_stats.echo_return_loss);
281 audio_track_stats->echo_return_loss_enhancement = static_cast<double>(
282 audio_processor_stats.echo_return_loss_enhancement);
283 }
284 report->AddStats(std::move(audio_track_stats));
285 }
286 // Video Tracks
kwiberg1b35d4c2016-11-10 05:15:38 -0800287 for (const rtc::scoped_refptr<VideoTrackInterface>& video_track :
hbos09bc1282016-11-08 06:29:22 -0800288 stream->GetVideoTracks()) {
289 std::string id = RTCMediaStreamTrackStatsIDFromMediaStreamTrackInterface(
290 *video_track.get());
291 if (report->Get(id)) {
292 // Skip track, stats already exist for it.
293 continue;
294 }
295 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
296 new RTCMediaStreamTrackStats(id, timestamp_us));
297 stream_stats->track_ids->push_back(video_track_stats->id());
298 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
299 *video_track.get(),
300 video_track_stats.get());
301 video_track_stats->remote_source = !is_local;
302 video_track_stats->detached = false;
303 if (video_track->GetSource()) {
304 VideoTrackSourceInterface::Stats video_track_source_stats;
305 if (video_track->GetSource()->GetStats(&video_track_source_stats)) {
306 video_track_stats->frame_width = static_cast<uint32_t>(
307 video_track_source_stats.input_width);
308 video_track_stats->frame_height = static_cast<uint32_t>(
309 video_track_source_stats.input_height);
310 }
311 }
312 report->AddStats(std::move(video_track_stats));
313 }
314 report->AddStats(std::move(stream_stats));
315 }
316}
317
hboscc555c52016-10-18 12:48:31 -0700318} // namespace
319
hbosc82f2e12016-09-05 01:36:50 -0700320rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
321 PeerConnection* pc, int64_t cache_lifetime_us) {
322 return rtc::scoped_refptr<RTCStatsCollector>(
323 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
324}
325
326RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
327 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700328 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -0700329 signaling_thread_(pc->session()->signaling_thread()),
330 worker_thread_(pc->session()->worker_thread()),
331 network_thread_(pc->session()->network_thread()),
332 num_pending_partial_reports_(0),
333 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700334 cache_timestamp_us_(0),
335 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700336 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700337 RTC_DCHECK(signaling_thread_);
338 RTC_DCHECK(worker_thread_);
339 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700340 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbosd565b732016-08-30 14:04:35 -0700341}
342
hbosc82f2e12016-09-05 01:36:50 -0700343void RTCStatsCollector::GetStatsReport(
344 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
345 RTC_DCHECK(signaling_thread_->IsCurrent());
346 RTC_DCHECK(callback);
347 callbacks_.push_back(callback);
348
hbos0e6758d2016-08-31 07:57:36 -0700349 // "Now" using a monotonically increasing timer.
350 int64_t cache_now_us = rtc::TimeMicros();
351 if (cached_report_ &&
352 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700353 // We have a fresh cached report to deliver.
354 DeliverCachedReport();
355 } else if (!num_pending_partial_reports_) {
356 // Only start gathering stats if we're not already gathering stats. In the
357 // case of already gathering stats, |callback_| will be invoked when there
358 // are no more pending partial reports.
359
360 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
361 // UTC), in microseconds. The system clock could be modified and is not
362 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700363 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700364
365 num_pending_partial_reports_ = 3;
366 partial_report_timestamp_us_ = cache_now_us;
367 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
368 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread,
369 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
370 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_,
371 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread,
372 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
373 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
374 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
375 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbos0e6758d2016-08-31 07:57:36 -0700376 }
hbosd565b732016-08-30 14:04:35 -0700377}
378
379void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700380 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700381 cached_report_ = nullptr;
382}
383
hbosc82f2e12016-09-05 01:36:50 -0700384void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
385 int64_t timestamp_us) {
386 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700387 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
388 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700389
hbos6ab97ce2016-10-03 14:16:56 -0700390 SessionStats session_stats;
391 if (pc_->session()->GetTransportStats(&session_stats)) {
hbos2fa7c672016-10-24 04:00:05 -0700392 std::map<std::string, CertificateStatsPair> transport_cert_stats =
393 PrepareTransportCertificateStats_s(session_stats);
394
395 ProduceCertificateStats_s(
396 timestamp_us, transport_cert_stats, report.get());
397 ProduceIceCandidateAndPairStats_s(
398 timestamp_us, session_stats, report.get());
hbos6ded1902016-11-01 01:50:46 -0700399 ProduceRTPStreamStats_s(
400 timestamp_us, session_stats, report.get());
hbos2fa7c672016-10-24 04:00:05 -0700401 ProduceTransportStats_s(
402 timestamp_us, session_stats, transport_cert_stats, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700403 }
hboscc555c52016-10-18 12:48:31 -0700404 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos09bc1282016-11-08 06:29:22 -0800405 ProduceMediaStreamAndTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700406 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700407
408 AddPartialResults(report);
409}
410
411void RTCStatsCollector::ProducePartialResultsOnWorkerThread(
412 int64_t timestamp_us) {
413 RTC_DCHECK(worker_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700414 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
415 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700416
417 // TODO(hbos): Gather stats on worker thread.
hbos6ded1902016-11-01 01:50:46 -0700418 // pc_->session()'s channels are owned by the signaling thread but there are
419 // some stats that are gathered on the worker thread. Instead of a synchronous
420 // invoke on "s->w" we could to the "w" work here asynchronously if it wasn't
421 // for the ownership issue. Synchronous invokes in other places makes it
422 // difficult to introduce locks without introducing deadlocks and the channels
423 // are not reference counted.
hbosc82f2e12016-09-05 01:36:50 -0700424
425 AddPartialResults(report);
426}
427
428void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
429 int64_t timestamp_us) {
430 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700431 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
432 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700433
434 // TODO(hbos): Gather stats on network thread.
hbos6ded1902016-11-01 01:50:46 -0700435 // pc_->session()'s channels are owned by the signaling thread but there are
436 // some stats that are gathered on the network thread. Instead of a
437 // synchronous invoke on "s->n" we could to the "n" work here asynchronously
438 // if it wasn't for the ownership issue. Synchronous invokes in other places
439 // makes it difficult to introduce locks without introducing deadlocks and the
440 // channels are not reference counted.
hbosc82f2e12016-09-05 01:36:50 -0700441
442 AddPartialResults(report);
443}
444
445void RTCStatsCollector::AddPartialResults(
446 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
447 if (!signaling_thread_->IsCurrent()) {
448 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
449 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
450 rtc::scoped_refptr<RTCStatsCollector>(this),
451 partial_report));
452 return;
453 }
454 AddPartialResults_s(partial_report);
455}
456
457void RTCStatsCollector::AddPartialResults_s(
458 rtc::scoped_refptr<RTCStatsReport> partial_report) {
459 RTC_DCHECK(signaling_thread_->IsCurrent());
460 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
461 if (!partial_report_)
462 partial_report_ = partial_report;
463 else
464 partial_report_->TakeMembersFrom(partial_report);
465 --num_pending_partial_reports_;
466 if (!num_pending_partial_reports_) {
467 cache_timestamp_us_ = partial_report_timestamp_us_;
468 cached_report_ = partial_report_;
469 partial_report_ = nullptr;
470 DeliverCachedReport();
471 }
472}
473
474void RTCStatsCollector::DeliverCachedReport() {
475 RTC_DCHECK(signaling_thread_->IsCurrent());
476 RTC_DCHECK(!callbacks_.empty());
477 RTC_DCHECK(cached_report_);
478 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
479 callbacks_) {
480 callback->OnStatsDelivered(cached_report_);
481 }
482 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700483}
484
hbos6ab97ce2016-10-03 14:16:56 -0700485void RTCStatsCollector::ProduceCertificateStats_s(
hbos2fa7c672016-10-24 04:00:05 -0700486 int64_t timestamp_us,
487 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700488 RTCStatsReport* report) const {
489 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700490 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
491 if (transport_cert_stats_pair.second.local) {
492 ProduceCertificateStatsFromSSLCertificateStats(
493 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700494 }
hbos02ba2112016-10-28 05:14:53 -0700495 if (transport_cert_stats_pair.second.remote) {
496 ProduceCertificateStatsFromSSLCertificateStats(
497 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700498 }
499 }
500}
501
hboscc555c52016-10-18 12:48:31 -0700502void RTCStatsCollector::ProduceDataChannelStats_s(
503 int64_t timestamp_us, RTCStatsReport* report) const {
504 RTC_DCHECK(signaling_thread_->IsCurrent());
505 for (const rtc::scoped_refptr<DataChannel>& data_channel :
506 pc_->sctp_data_channels()) {
507 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
508 new RTCDataChannelStats(
509 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
510 timestamp_us));
511 data_channel_stats->label = data_channel->label();
512 data_channel_stats->protocol = data_channel->protocol();
513 data_channel_stats->datachannelid = data_channel->id();
514 data_channel_stats->state =
515 DataStateToRTCDataChannelState(data_channel->state());
516 data_channel_stats->messages_sent = data_channel->messages_sent();
517 data_channel_stats->bytes_sent = data_channel->bytes_sent();
518 data_channel_stats->messages_received = data_channel->messages_received();
519 data_channel_stats->bytes_received = data_channel->bytes_received();
520 report->AddStats(std::move(data_channel_stats));
521 }
522}
523
hbosab9f6e42016-10-07 02:18:47 -0700524void RTCStatsCollector::ProduceIceCandidateAndPairStats_s(
525 int64_t timestamp_us, const SessionStats& session_stats,
526 RTCStatsReport* report) const {
527 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700528 for (const auto& transport_stats : session_stats.transport_stats) {
529 for (const auto& channel_stats : transport_stats.second.channel_stats) {
530 for (const cricket::ConnectionInfo& info :
531 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700532 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700533 new RTCIceCandidatePairStats(
534 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
535 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700536
hbosab9f6e42016-10-07 02:18:47 -0700537 // TODO(hbos): There could be other candidates that are not paired with
538 // anything. We don't have a complete list. Local candidates come from
539 // Port objects, and prflx candidates (both local and remote) are only
540 // stored in candidate pairs. crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700541 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700542 timestamp_us, info.local_candidate, true, report);
hbos02ba2112016-10-28 05:14:53 -0700543 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700544 timestamp_us, info.remote_candidate, false, report);
hbosc47a0c32016-10-11 14:54:49 -0700545
hbosc47a0c32016-10-11 14:54:49 -0700546 // TODO(hbos): This writable is different than the spec. It goes to
547 // false after a certain amount of time without a response passes.
548 // crbug.com/633550
549 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700550 candidate_pair_stats->bytes_sent =
551 static_cast<uint64_t>(info.sent_total_bytes);
552 candidate_pair_stats->bytes_received =
553 static_cast<uint64_t>(info.recv_total_bytes);
hbosc47a0c32016-10-11 14:54:49 -0700554 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
555 // smoothed according to the spec. crbug.com/633550. See
556 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
557 candidate_pair_stats->current_rtt =
hbos6ded1902016-11-01 01:50:46 -0700558 static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec;
hbosc47a0c32016-10-11 14:54:49 -0700559 candidate_pair_stats->requests_sent =
560 static_cast<uint64_t>(info.sent_ping_requests_total);
561 candidate_pair_stats->responses_received =
562 static_cast<uint64_t>(info.recv_ping_responses);
563 candidate_pair_stats->responses_sent =
564 static_cast<uint64_t>(info.sent_ping_responses);
hbosc47a0c32016-10-11 14:54:49 -0700565
566 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700567 }
568 }
569 }
570}
571
hbos09bc1282016-11-08 06:29:22 -0800572void RTCStatsCollector::ProduceMediaStreamAndTrackStats_s(
573 int64_t timestamp_us, RTCStatsReport* report) const {
574 RTC_DCHECK(signaling_thread_->IsCurrent());
575 ProduceMediaStreamAndTrackStats(
576 timestamp_us, pc_->local_streams(), true, report);
577 ProduceMediaStreamAndTrackStats(
578 timestamp_us, pc_->remote_streams(), false, report);
579}
580
hbos6ab97ce2016-10-03 14:16:56 -0700581void RTCStatsCollector::ProducePeerConnectionStats_s(
582 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700583 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700584 // TODO(hbos): If data channels are removed from the peer connection this will
585 // yield incorrect counts. Address before closing crbug.com/636818. See
586 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*.
587 uint32_t data_channels_opened = 0;
588 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels =
589 pc_->sctp_data_channels();
590 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) {
591 if (data_channel->state() == DataChannelInterface::kOpen)
592 ++data_channels_opened;
593 }
594 // There is always just one |RTCPeerConnectionStats| so its |id| can be a
595 // constant.
596 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700597 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbosd565b732016-08-30 14:04:35 -0700598 stats->data_channels_opened = data_channels_opened;
599 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) -
600 data_channels_opened;
hbos6ab97ce2016-10-03 14:16:56 -0700601 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700602}
603
hbos6ded1902016-11-01 01:50:46 -0700604void RTCStatsCollector::ProduceRTPStreamStats_s(
605 int64_t timestamp_us, const SessionStats& session_stats,
606 RTCStatsReport* report) const {
607 RTC_DCHECK(signaling_thread_->IsCurrent());
608
609 // Audio
610 if (pc_->session()->voice_channel()) {
611 cricket::VoiceMediaInfo voice_media_info;
612 if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) {
613 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
614 session_stats.proxy_to_transport, *pc_->session()->voice_channel());
hboseeafe942016-11-01 03:00:17 -0700615 RTC_DCHECK(!transport_id.empty());
616 // Inbound
617 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
618 voice_media_info.receivers) {
619 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
620 // is fixed.
621 if (voice_receiver_info.ssrc() == 0)
622 continue;
623 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
624 new RTCInboundRTPStreamStats(
625 RTCInboundRTPStreamStatsIDFromSSRC(
626 true, voice_receiver_info.ssrc()),
627 timestamp_us));
628 SetInboundRTPStreamStatsFromVoiceReceiverInfo(
629 voice_receiver_info, inbound_audio.get());
630 inbound_audio->transport_id = transport_id;
631 report->AddStats(std::move(inbound_audio));
632 }
633 // Outbound
hbos6ded1902016-11-01 01:50:46 -0700634 for (const cricket::VoiceSenderInfo& voice_sender_info :
635 voice_media_info.senders) {
636 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
637 // is fixed.
638 if (voice_sender_info.ssrc() == 0)
639 continue;
640 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
641 new RTCOutboundRTPStreamStats(
642 RTCOutboundRTPStreamStatsIDFromSSRC(
643 true, voice_sender_info.ssrc()),
644 timestamp_us));
645 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
646 voice_sender_info, outbound_audio.get());
hboseeafe942016-11-01 03:00:17 -0700647 outbound_audio->transport_id = transport_id;
hbos6ded1902016-11-01 01:50:46 -0700648 report->AddStats(std::move(outbound_audio));
649 }
650 }
651 }
652 // Video
653 if (pc_->session()->video_channel()) {
654 cricket::VideoMediaInfo video_media_info;
655 if (pc_->session()->video_channel()->GetStats(&video_media_info)) {
656 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
657 session_stats.proxy_to_transport, *pc_->session()->video_channel());
hboseeafe942016-11-01 03:00:17 -0700658 RTC_DCHECK(!transport_id.empty());
659 // Inbound
660 for (const cricket::VideoReceiverInfo& video_receiver_info :
661 video_media_info.receivers) {
662 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
663 // is fixed.
664 if (video_receiver_info.ssrc() == 0)
665 continue;
666 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
667 new RTCInboundRTPStreamStats(
668 RTCInboundRTPStreamStatsIDFromSSRC(
669 false, video_receiver_info.ssrc()),
670 timestamp_us));
671 SetInboundRTPStreamStatsFromVideoReceiverInfo(
672 video_receiver_info, inbound_video.get());
673 inbound_video->transport_id = transport_id;
674 report->AddStats(std::move(inbound_video));
675 }
676 // Outbound
hbos6ded1902016-11-01 01:50:46 -0700677 for (const cricket::VideoSenderInfo& video_sender_info :
678 video_media_info.senders) {
679 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
680 // is fixed.
681 if (video_sender_info.ssrc() == 0)
682 continue;
683 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
684 new RTCOutboundRTPStreamStats(
685 RTCOutboundRTPStreamStatsIDFromSSRC(
686 false, video_sender_info.ssrc()),
687 timestamp_us));
688 SetOutboundRTPStreamStatsFromVideoSenderInfo(
689 video_sender_info, outbound_video.get());
hboseeafe942016-11-01 03:00:17 -0700690 outbound_video->transport_id = transport_id;
hbos6ded1902016-11-01 01:50:46 -0700691 report->AddStats(std::move(outbound_video));
692 }
693 }
694 }
695}
696
hbos2fa7c672016-10-24 04:00:05 -0700697void RTCStatsCollector::ProduceTransportStats_s(
698 int64_t timestamp_us, const SessionStats& session_stats,
699 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
700 RTCStatsReport* report) const {
701 RTC_DCHECK(signaling_thread_->IsCurrent());
702 for (const auto& transport : session_stats.transport_stats) {
703 // Get reference to RTCP channel, if it exists.
704 std::string rtcp_transport_stats_id;
705 for (const auto& channel_stats : transport.second.channel_stats) {
706 if (channel_stats.component ==
707 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
708 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
709 transport.second.transport_name, channel_stats.component);
710 break;
711 }
712 }
713
714 // Get reference to local and remote certificates of this transport, if they
715 // exist.
716 const auto& certificate_stats_it = transport_cert_stats.find(
717 transport.second.transport_name);
718 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
719 std::string local_certificate_id;
720 if (certificate_stats_it->second.local) {
721 local_certificate_id = RTCCertificateIDFromFingerprint(
722 certificate_stats_it->second.local->fingerprint);
723 }
724 std::string remote_certificate_id;
725 if (certificate_stats_it->second.remote) {
726 remote_certificate_id = RTCCertificateIDFromFingerprint(
727 certificate_stats_it->second.remote->fingerprint);
728 }
729
730 // There is one transport stats for each channel.
731 for (const auto& channel_stats : transport.second.channel_stats) {
732 std::unique_ptr<RTCTransportStats> transport_stats(
733 new RTCTransportStats(
734 RTCTransportStatsIDFromTransportChannel(
735 transport.second.transport_name, channel_stats.component),
736 timestamp_us));
737 transport_stats->bytes_sent = 0;
738 transport_stats->bytes_received = 0;
739 transport_stats->active_connection = false;
740 for (const cricket::ConnectionInfo& info :
741 channel_stats.connection_infos) {
742 *transport_stats->bytes_sent += info.sent_total_bytes;
743 *transport_stats->bytes_received += info.recv_total_bytes;
744 if (info.best_connection) {
745 transport_stats->active_connection = true;
746 transport_stats->selected_candidate_pair_id =
747 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
748 }
749 }
750 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
751 !rtcp_transport_stats_id.empty()) {
752 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
753 }
754 if (!local_certificate_id.empty())
755 transport_stats->local_certificate_id = local_certificate_id;
756 if (!remote_certificate_id.empty())
757 transport_stats->remote_certificate_id = remote_certificate_id;
758 report->AddStats(std::move(transport_stats));
759 }
760 }
761}
762
763std::map<std::string, RTCStatsCollector::CertificateStatsPair>
764RTCStatsCollector::PrepareTransportCertificateStats_s(
765 const SessionStats& session_stats) const {
766 RTC_DCHECK(signaling_thread_->IsCurrent());
767 std::map<std::string, CertificateStatsPair> transport_cert_stats;
768 for (const auto& transport_stats : session_stats.transport_stats) {
769 CertificateStatsPair certificate_stats_pair;
770 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
771 if (pc_->session()->GetLocalCertificate(
772 transport_stats.second.transport_name, &local_certificate)) {
773 certificate_stats_pair.local =
774 local_certificate->ssl_certificate().GetStats();
775 }
776 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
777 pc_->session()->GetRemoteSSLCertificate(
778 transport_stats.second.transport_name);
779 if (remote_certificate) {
780 certificate_stats_pair.remote = remote_certificate->GetStats();
781 }
782 transport_cert_stats.insert(
783 std::make_pair(transport_stats.second.transport_name,
784 std::move(certificate_stats_pair)));
785 }
786 return transport_cert_stats;
787}
788
hboscc555c52016-10-18 12:48:31 -0700789const char* CandidateTypeToRTCIceCandidateTypeForTesting(
790 const std::string& type) {
791 return CandidateTypeToRTCIceCandidateType(type);
792}
793
794const char* DataStateToRTCDataChannelStateForTesting(
795 DataChannelInterface::DataState state) {
796 return DataStateToRTCDataChannelState(state);
797}
798
hbosd565b732016-08-30 14:04:35 -0700799} // namespace webrtc