blob: 543181ede46c232f01a3e2020c8fe754ae13afb5 [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"
hbos6ab97ce2016-10-03 14:16:56 -070018#include "webrtc/api/webrtcsession.h"
hbosd565b732016-08-30 14:04:35 -070019#include "webrtc/base/checks.h"
hbos69e9cb02016-10-31 14:48:26 -070020#include "webrtc/base/timeutils.h"
21#include "webrtc/media/base/mediachannel.h"
hbosab9f6e42016-10-07 02:18:47 -070022#include "webrtc/p2p/base/candidate.h"
hbos2fa7c672016-10-24 04:00:05 -070023#include "webrtc/p2p/base/p2pconstants.h"
hbosab9f6e42016-10-07 02:18:47 -070024#include "webrtc/p2p/base/port.h"
hbosd565b732016-08-30 14:04:35 -070025
26namespace webrtc {
27
hboscc555c52016-10-18 12:48:31 -070028namespace {
29
hbos2fa7c672016-10-24 04:00:05 -070030std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
31 return "RTCCertificate_" + fingerprint;
32}
33
34std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
35 const cricket::ConnectionInfo& info) {
36 return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" +
37 info.remote_candidate.id();
38}
39
40std::string RTCTransportStatsIDFromTransportChannel(
41 const std::string& transport_name, int channel_component) {
42 return "RTCTransport_" + transport_name + "_" +
43 rtc::ToString<>(channel_component);
44}
45
hbos69e9cb02016-10-31 14:48:26 -070046std::string RTCTransportStatsIDFromBaseChannel(
47 const ProxyTransportMap& proxy_to_transport,
48 const cricket::BaseChannel& base_channel) {
49 auto proxy_it = proxy_to_transport.find(base_channel.content_name());
50 if (proxy_it == proxy_to_transport.cend())
51 return "";
52 return RTCTransportStatsIDFromTransportChannel(
53 proxy_it->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
54}
55
56std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
57 return audio ? "RTCOutboundRTPAudioStream_" + rtc::ToString<>(ssrc)
58 : "RTCOutboundRTPVideoStream_" + rtc::ToString<>(ssrc);
59}
60
hbosab9f6e42016-10-07 02:18:47 -070061const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
62 if (type == cricket::LOCAL_PORT_TYPE)
63 return RTCIceCandidateType::kHost;
64 if (type == cricket::STUN_PORT_TYPE)
65 return RTCIceCandidateType::kSrflx;
66 if (type == cricket::PRFLX_PORT_TYPE)
67 return RTCIceCandidateType::kPrflx;
68 if (type == cricket::RELAY_PORT_TYPE)
69 return RTCIceCandidateType::kRelay;
70 RTC_NOTREACHED();
71 return nullptr;
72}
73
hboscc555c52016-10-18 12:48:31 -070074const char* DataStateToRTCDataChannelState(
75 DataChannelInterface::DataState state) {
76 switch (state) {
77 case DataChannelInterface::kConnecting:
78 return RTCDataChannelState::kConnecting;
79 case DataChannelInterface::kOpen:
80 return RTCDataChannelState::kOpen;
81 case DataChannelInterface::kClosing:
82 return RTCDataChannelState::kClosing;
83 case DataChannelInterface::kClosed:
84 return RTCDataChannelState::kClosed;
85 default:
86 RTC_NOTREACHED();
87 return nullptr;
88 }
89}
90
hbos69e9cb02016-10-31 14:48:26 -070091void SetOutboundRTPStreamStatsFromMediaSenderInfo(
92 const cricket::MediaSenderInfo& media_sender_info,
93 RTCOutboundRTPStreamStats* outbound_stats) {
94 RTC_DCHECK(outbound_stats);
95 outbound_stats->ssrc = rtc::ToString<>(media_sender_info.ssrc());
96 // TODO(hbos): Support the remote case. crbug.com/657856
97 outbound_stats->is_remote = false;
98 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
99 // |media_sender_info.codec_name|. crbug.com/657854, 657856, 659117
100 outbound_stats->packets_sent =
101 static_cast<uint32_t>(media_sender_info.packets_sent);
102 outbound_stats->bytes_sent =
103 static_cast<uint64_t>(media_sender_info.bytes_sent);
104 outbound_stats->round_trip_time =
105 static_cast<double>(media_sender_info.rtt_ms) / rtc::kNumMillisecsPerSec;
106}
107
108void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
109 const cricket::VoiceSenderInfo& voice_sender_info,
110 RTCOutboundRTPStreamStats* outbound_audio) {
111 SetOutboundRTPStreamStatsFromMediaSenderInfo(
112 voice_sender_info, outbound_audio);
113 outbound_audio->media_type = "audio";
114 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
115 // purposefully left undefined for audio.
116}
117
118void SetOutboundRTPStreamStatsFromVideoSenderInfo(
119 const cricket::VideoSenderInfo& video_sender_info,
120 RTCOutboundRTPStreamStats* outbound_video) {
121 SetOutboundRTPStreamStatsFromMediaSenderInfo(
122 video_sender_info, outbound_video);
123 outbound_video->media_type = "video";
124 outbound_video->fir_count =
125 static_cast<uint32_t>(video_sender_info.firs_rcvd);
126 outbound_video->pli_count =
127 static_cast<uint32_t>(video_sender_info.plis_rcvd);
128 outbound_video->nack_count =
129 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
130}
131
hbos02ba2112016-10-28 05:14:53 -0700132void ProduceCertificateStatsFromSSLCertificateStats(
133 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
134 RTCStatsReport* report) {
135 RTCCertificateStats* prev_certificate_stats = nullptr;
136 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
137 s = s->issuer.get()) {
138 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
139 RTCCertificateIDFromFingerprint(s->fingerprint), timestamp_us);
140 certificate_stats->fingerprint = s->fingerprint;
141 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
142 certificate_stats->base64_certificate = s->base64_certificate;
143 if (prev_certificate_stats)
144 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
145 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
146 prev_certificate_stats = certificate_stats;
147 }
148}
149
150const std::string& ProduceIceCandidateStats(
151 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
152 RTCStatsReport* report) {
153 const std::string& id = "RTCIceCandidate_" + candidate.id();
154 const RTCStats* stats = report->Get(id);
155 if (!stats) {
156 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
157 if (is_local)
158 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
159 else
160 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
161 candidate_stats->ip = candidate.address().ipaddr().ToString();
162 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
163 candidate_stats->protocol = candidate.protocol();
164 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
165 candidate.type());
166 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
167
168 stats = candidate_stats.get();
169 report->AddStats(std::move(candidate_stats));
170 }
171 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
172 : RTCRemoteIceCandidateStats::kType);
173 return stats->id();
174}
175
hboscc555c52016-10-18 12:48:31 -0700176} // namespace
177
hbosc82f2e12016-09-05 01:36:50 -0700178rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
179 PeerConnection* pc, int64_t cache_lifetime_us) {
180 return rtc::scoped_refptr<RTCStatsCollector>(
181 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
182}
183
184RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
185 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700186 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -0700187 signaling_thread_(pc->session()->signaling_thread()),
188 worker_thread_(pc->session()->worker_thread()),
189 network_thread_(pc->session()->network_thread()),
190 num_pending_partial_reports_(0),
191 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700192 cache_timestamp_us_(0),
193 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700194 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700195 RTC_DCHECK(signaling_thread_);
196 RTC_DCHECK(worker_thread_);
197 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700198 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbosd565b732016-08-30 14:04:35 -0700199}
200
hbosc82f2e12016-09-05 01:36:50 -0700201void RTCStatsCollector::GetStatsReport(
202 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
203 RTC_DCHECK(signaling_thread_->IsCurrent());
204 RTC_DCHECK(callback);
205 callbacks_.push_back(callback);
206
hbos0e6758d2016-08-31 07:57:36 -0700207 // "Now" using a monotonically increasing timer.
208 int64_t cache_now_us = rtc::TimeMicros();
209 if (cached_report_ &&
210 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700211 // We have a fresh cached report to deliver.
212 DeliverCachedReport();
213 } else if (!num_pending_partial_reports_) {
214 // Only start gathering stats if we're not already gathering stats. In the
215 // case of already gathering stats, |callback_| will be invoked when there
216 // are no more pending partial reports.
217
218 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
219 // UTC), in microseconds. The system clock could be modified and is not
220 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700221 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700222
223 num_pending_partial_reports_ = 3;
224 partial_report_timestamp_us_ = cache_now_us;
225 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
226 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread,
227 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
228 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_,
229 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread,
230 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
231 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
232 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
233 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbos0e6758d2016-08-31 07:57:36 -0700234 }
hbosd565b732016-08-30 14:04:35 -0700235}
236
237void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700238 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700239 cached_report_ = nullptr;
240}
241
hbosc82f2e12016-09-05 01:36:50 -0700242void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
243 int64_t timestamp_us) {
244 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos69e9cb02016-10-31 14:48:26 -0700245 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
246 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700247
hbos6ab97ce2016-10-03 14:16:56 -0700248 SessionStats session_stats;
249 if (pc_->session()->GetTransportStats(&session_stats)) {
hbos2fa7c672016-10-24 04:00:05 -0700250 std::map<std::string, CertificateStatsPair> transport_cert_stats =
251 PrepareTransportCertificateStats_s(session_stats);
252
253 ProduceCertificateStats_s(
254 timestamp_us, transport_cert_stats, report.get());
255 ProduceIceCandidateAndPairStats_s(
256 timestamp_us, session_stats, report.get());
hbos69e9cb02016-10-31 14:48:26 -0700257 ProduceRTPStreamStats_s(
258 timestamp_us, session_stats, report.get());
hbos2fa7c672016-10-24 04:00:05 -0700259 ProduceTransportStats_s(
260 timestamp_us, session_stats, transport_cert_stats, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700261 }
hboscc555c52016-10-18 12:48:31 -0700262 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700263 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700264
265 AddPartialResults(report);
266}
267
268void RTCStatsCollector::ProducePartialResultsOnWorkerThread(
269 int64_t timestamp_us) {
270 RTC_DCHECK(worker_thread_->IsCurrent());
hbos69e9cb02016-10-31 14:48:26 -0700271 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
272 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700273
274 // TODO(hbos): Gather stats on worker thread.
hbos69e9cb02016-10-31 14:48:26 -0700275 // pc_->session()'s channels are owned by the signaling thread but there are
276 // some stats that are gathered on the worker thread. Instead of a synchronous
277 // invoke on "s->w" we could to the "w" work here asynchronously if it wasn't
278 // for the ownership issue. Synchronous invokes in other places makes it
279 // difficult to introduce locks without introducing deadlocks and the channels
280 // are not reference counted.
hbosc82f2e12016-09-05 01:36:50 -0700281
282 AddPartialResults(report);
283}
284
285void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
286 int64_t timestamp_us) {
287 RTC_DCHECK(network_thread_->IsCurrent());
hbos69e9cb02016-10-31 14:48:26 -0700288 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
289 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700290
291 // TODO(hbos): Gather stats on network thread.
hbos69e9cb02016-10-31 14:48:26 -0700292 // pc_->session()'s channels are owned by the signaling thread but there are
293 // some stats that are gathered on the network thread. Instead of a
294 // synchronous invoke on "s->n" we could to the "n" work here asynchronously
295 // if it wasn't for the ownership issue. Synchronous invokes in other places
296 // makes it difficult to introduce locks without introducing deadlocks and the
297 // channels are not reference counted.
hbosc82f2e12016-09-05 01:36:50 -0700298
299 AddPartialResults(report);
300}
301
302void RTCStatsCollector::AddPartialResults(
303 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
304 if (!signaling_thread_->IsCurrent()) {
305 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
306 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
307 rtc::scoped_refptr<RTCStatsCollector>(this),
308 partial_report));
309 return;
310 }
311 AddPartialResults_s(partial_report);
312}
313
314void RTCStatsCollector::AddPartialResults_s(
315 rtc::scoped_refptr<RTCStatsReport> partial_report) {
316 RTC_DCHECK(signaling_thread_->IsCurrent());
317 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
318 if (!partial_report_)
319 partial_report_ = partial_report;
320 else
321 partial_report_->TakeMembersFrom(partial_report);
322 --num_pending_partial_reports_;
323 if (!num_pending_partial_reports_) {
324 cache_timestamp_us_ = partial_report_timestamp_us_;
325 cached_report_ = partial_report_;
326 partial_report_ = nullptr;
327 DeliverCachedReport();
328 }
329}
330
331void RTCStatsCollector::DeliverCachedReport() {
332 RTC_DCHECK(signaling_thread_->IsCurrent());
333 RTC_DCHECK(!callbacks_.empty());
334 RTC_DCHECK(cached_report_);
335 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
336 callbacks_) {
337 callback->OnStatsDelivered(cached_report_);
338 }
339 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700340}
341
hbos6ab97ce2016-10-03 14:16:56 -0700342void RTCStatsCollector::ProduceCertificateStats_s(
hbos2fa7c672016-10-24 04:00:05 -0700343 int64_t timestamp_us,
344 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700345 RTCStatsReport* report) const {
346 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700347 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
348 if (transport_cert_stats_pair.second.local) {
349 ProduceCertificateStatsFromSSLCertificateStats(
350 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700351 }
hbos02ba2112016-10-28 05:14:53 -0700352 if (transport_cert_stats_pair.second.remote) {
353 ProduceCertificateStatsFromSSLCertificateStats(
354 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700355 }
356 }
357}
358
hboscc555c52016-10-18 12:48:31 -0700359void RTCStatsCollector::ProduceDataChannelStats_s(
360 int64_t timestamp_us, RTCStatsReport* report) const {
361 RTC_DCHECK(signaling_thread_->IsCurrent());
362 for (const rtc::scoped_refptr<DataChannel>& data_channel :
363 pc_->sctp_data_channels()) {
364 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
365 new RTCDataChannelStats(
366 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
367 timestamp_us));
368 data_channel_stats->label = data_channel->label();
369 data_channel_stats->protocol = data_channel->protocol();
370 data_channel_stats->datachannelid = data_channel->id();
371 data_channel_stats->state =
372 DataStateToRTCDataChannelState(data_channel->state());
373 data_channel_stats->messages_sent = data_channel->messages_sent();
374 data_channel_stats->bytes_sent = data_channel->bytes_sent();
375 data_channel_stats->messages_received = data_channel->messages_received();
376 data_channel_stats->bytes_received = data_channel->bytes_received();
377 report->AddStats(std::move(data_channel_stats));
378 }
379}
380
hbosab9f6e42016-10-07 02:18:47 -0700381void RTCStatsCollector::ProduceIceCandidateAndPairStats_s(
382 int64_t timestamp_us, const SessionStats& session_stats,
383 RTCStatsReport* report) const {
384 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700385 for (const auto& transport_stats : session_stats.transport_stats) {
386 for (const auto& channel_stats : transport_stats.second.channel_stats) {
387 for (const cricket::ConnectionInfo& info :
388 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700389 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700390 new RTCIceCandidatePairStats(
391 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
392 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700393
hbosab9f6e42016-10-07 02:18:47 -0700394 // TODO(hbos): There could be other candidates that are not paired with
395 // anything. We don't have a complete list. Local candidates come from
396 // Port objects, and prflx candidates (both local and remote) are only
397 // stored in candidate pairs. crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700398 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700399 timestamp_us, info.local_candidate, true, report);
hbos02ba2112016-10-28 05:14:53 -0700400 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700401 timestamp_us, info.remote_candidate, false, report);
hbosc47a0c32016-10-11 14:54:49 -0700402
hbosc47a0c32016-10-11 14:54:49 -0700403 // TODO(hbos): This writable is different than the spec. It goes to
404 // false after a certain amount of time without a response passes.
405 // crbug.com/633550
406 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700407 candidate_pair_stats->bytes_sent =
408 static_cast<uint64_t>(info.sent_total_bytes);
409 candidate_pair_stats->bytes_received =
410 static_cast<uint64_t>(info.recv_total_bytes);
hbosc47a0c32016-10-11 14:54:49 -0700411 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
412 // smoothed according to the spec. crbug.com/633550. See
413 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
414 candidate_pair_stats->current_rtt =
hbos69e9cb02016-10-31 14:48:26 -0700415 static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec;
hbosc47a0c32016-10-11 14:54:49 -0700416 candidate_pair_stats->requests_sent =
417 static_cast<uint64_t>(info.sent_ping_requests_total);
418 candidate_pair_stats->responses_received =
419 static_cast<uint64_t>(info.recv_ping_responses);
420 candidate_pair_stats->responses_sent =
421 static_cast<uint64_t>(info.sent_ping_responses);
hbosc47a0c32016-10-11 14:54:49 -0700422
423 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700424 }
425 }
426 }
427}
428
hbos6ab97ce2016-10-03 14:16:56 -0700429void RTCStatsCollector::ProducePeerConnectionStats_s(
430 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700431 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700432 // TODO(hbos): If data channels are removed from the peer connection this will
433 // yield incorrect counts. Address before closing crbug.com/636818. See
434 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*.
435 uint32_t data_channels_opened = 0;
436 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels =
437 pc_->sctp_data_channels();
438 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) {
439 if (data_channel->state() == DataChannelInterface::kOpen)
440 ++data_channels_opened;
441 }
442 // There is always just one |RTCPeerConnectionStats| so its |id| can be a
443 // constant.
444 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700445 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbosd565b732016-08-30 14:04:35 -0700446 stats->data_channels_opened = data_channels_opened;
447 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) -
448 data_channels_opened;
hbos6ab97ce2016-10-03 14:16:56 -0700449 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700450}
451
hbos69e9cb02016-10-31 14:48:26 -0700452void RTCStatsCollector::ProduceRTPStreamStats_s(
453 int64_t timestamp_us, const SessionStats& session_stats,
454 RTCStatsReport* report) const {
455 RTC_DCHECK(signaling_thread_->IsCurrent());
456
457 // Audio
458 if (pc_->session()->voice_channel()) {
459 cricket::VoiceMediaInfo voice_media_info;
460 if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) {
461 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
462 session_stats.proxy_to_transport, *pc_->session()->voice_channel());
463 for (const cricket::VoiceSenderInfo& voice_sender_info :
464 voice_media_info.senders) {
465 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
466 // is fixed.
467 if (voice_sender_info.ssrc() == 0)
468 continue;
469 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
470 new RTCOutboundRTPStreamStats(
471 RTCOutboundRTPStreamStatsIDFromSSRC(
472 true, voice_sender_info.ssrc()),
473 timestamp_us));
474 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
475 voice_sender_info, outbound_audio.get());
perkj4ed07502016-10-31 23:50:53 -0700476 if (!transport_id.empty())
477 outbound_audio->transport_id = transport_id;
hbos69e9cb02016-10-31 14:48:26 -0700478 report->AddStats(std::move(outbound_audio));
479 }
480 }
481 }
482 // Video
483 if (pc_->session()->video_channel()) {
484 cricket::VideoMediaInfo video_media_info;
485 if (pc_->session()->video_channel()->GetStats(&video_media_info)) {
486 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
487 session_stats.proxy_to_transport, *pc_->session()->video_channel());
488 for (const cricket::VideoSenderInfo& video_sender_info :
489 video_media_info.senders) {
490 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
491 // is fixed.
492 if (video_sender_info.ssrc() == 0)
493 continue;
494 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
495 new RTCOutboundRTPStreamStats(
496 RTCOutboundRTPStreamStatsIDFromSSRC(
497 false, video_sender_info.ssrc()),
498 timestamp_us));
499 SetOutboundRTPStreamStatsFromVideoSenderInfo(
500 video_sender_info, outbound_video.get());
perkj4ed07502016-10-31 23:50:53 -0700501 if (!transport_id.empty())
502 outbound_video->transport_id = transport_id;
hbos69e9cb02016-10-31 14:48:26 -0700503 report->AddStats(std::move(outbound_video));
504 }
505 }
506 }
507}
508
hbos2fa7c672016-10-24 04:00:05 -0700509void RTCStatsCollector::ProduceTransportStats_s(
510 int64_t timestamp_us, const SessionStats& session_stats,
511 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
512 RTCStatsReport* report) const {
513 RTC_DCHECK(signaling_thread_->IsCurrent());
514 for (const auto& transport : session_stats.transport_stats) {
515 // Get reference to RTCP channel, if it exists.
516 std::string rtcp_transport_stats_id;
517 for (const auto& channel_stats : transport.second.channel_stats) {
518 if (channel_stats.component ==
519 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
520 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
521 transport.second.transport_name, channel_stats.component);
522 break;
523 }
524 }
525
526 // Get reference to local and remote certificates of this transport, if they
527 // exist.
528 const auto& certificate_stats_it = transport_cert_stats.find(
529 transport.second.transport_name);
530 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
531 std::string local_certificate_id;
532 if (certificate_stats_it->second.local) {
533 local_certificate_id = RTCCertificateIDFromFingerprint(
534 certificate_stats_it->second.local->fingerprint);
535 }
536 std::string remote_certificate_id;
537 if (certificate_stats_it->second.remote) {
538 remote_certificate_id = RTCCertificateIDFromFingerprint(
539 certificate_stats_it->second.remote->fingerprint);
540 }
541
542 // There is one transport stats for each channel.
543 for (const auto& channel_stats : transport.second.channel_stats) {
544 std::unique_ptr<RTCTransportStats> transport_stats(
545 new RTCTransportStats(
546 RTCTransportStatsIDFromTransportChannel(
547 transport.second.transport_name, channel_stats.component),
548 timestamp_us));
549 transport_stats->bytes_sent = 0;
550 transport_stats->bytes_received = 0;
551 transport_stats->active_connection = false;
552 for (const cricket::ConnectionInfo& info :
553 channel_stats.connection_infos) {
554 *transport_stats->bytes_sent += info.sent_total_bytes;
555 *transport_stats->bytes_received += info.recv_total_bytes;
556 if (info.best_connection) {
557 transport_stats->active_connection = true;
558 transport_stats->selected_candidate_pair_id =
559 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
560 }
561 }
562 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
563 !rtcp_transport_stats_id.empty()) {
564 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
565 }
566 if (!local_certificate_id.empty())
567 transport_stats->local_certificate_id = local_certificate_id;
568 if (!remote_certificate_id.empty())
569 transport_stats->remote_certificate_id = remote_certificate_id;
570 report->AddStats(std::move(transport_stats));
571 }
572 }
573}
574
575std::map<std::string, RTCStatsCollector::CertificateStatsPair>
576RTCStatsCollector::PrepareTransportCertificateStats_s(
577 const SessionStats& session_stats) const {
578 RTC_DCHECK(signaling_thread_->IsCurrent());
579 std::map<std::string, CertificateStatsPair> transport_cert_stats;
580 for (const auto& transport_stats : session_stats.transport_stats) {
581 CertificateStatsPair certificate_stats_pair;
582 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
583 if (pc_->session()->GetLocalCertificate(
584 transport_stats.second.transport_name, &local_certificate)) {
585 certificate_stats_pair.local =
586 local_certificate->ssl_certificate().GetStats();
587 }
588 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
589 pc_->session()->GetRemoteSSLCertificate(
590 transport_stats.second.transport_name);
591 if (remote_certificate) {
592 certificate_stats_pair.remote = remote_certificate->GetStats();
593 }
594 transport_cert_stats.insert(
595 std::make_pair(transport_stats.second.transport_name,
596 std::move(certificate_stats_pair)));
597 }
598 return transport_cert_stats;
599}
600
hboscc555c52016-10-18 12:48:31 -0700601const char* CandidateTypeToRTCIceCandidateTypeForTesting(
602 const std::string& type) {
603 return CandidateTypeToRTCIceCandidateType(type);
604}
605
606const char* DataStateToRTCDataChannelStateForTesting(
607 DataChannelInterface::DataState state) {
608 return DataStateToRTCDataChannelState(state);
609}
610
hbosd565b732016-08-30 14:04:35 -0700611} // namespace webrtc