blob: 2d858c00e09bd633ce59e278241420e43bb85202 [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"
hbos6ab97ce2016-10-03 14:16:56 -070020#include "webrtc/base/sslidentity.h"
hbosab9f6e42016-10-07 02:18:47 -070021#include "webrtc/p2p/base/candidate.h"
22#include "webrtc/p2p/base/port.h"
hbosd565b732016-08-30 14:04:35 -070023
24namespace webrtc {
25
hbosab9f6e42016-10-07 02:18:47 -070026const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
27 if (type == cricket::LOCAL_PORT_TYPE)
28 return RTCIceCandidateType::kHost;
29 if (type == cricket::STUN_PORT_TYPE)
30 return RTCIceCandidateType::kSrflx;
31 if (type == cricket::PRFLX_PORT_TYPE)
32 return RTCIceCandidateType::kPrflx;
33 if (type == cricket::RELAY_PORT_TYPE)
34 return RTCIceCandidateType::kRelay;
35 RTC_NOTREACHED();
36 return nullptr;
37}
38
hbosc82f2e12016-09-05 01:36:50 -070039rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
40 PeerConnection* pc, int64_t cache_lifetime_us) {
41 return rtc::scoped_refptr<RTCStatsCollector>(
42 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
43}
44
45RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
46 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -070047 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -070048 signaling_thread_(pc->session()->signaling_thread()),
49 worker_thread_(pc->session()->worker_thread()),
50 network_thread_(pc->session()->network_thread()),
51 num_pending_partial_reports_(0),
52 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -070053 cache_timestamp_us_(0),
54 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -070055 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -070056 RTC_DCHECK(signaling_thread_);
57 RTC_DCHECK(worker_thread_);
58 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -070059 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbosd565b732016-08-30 14:04:35 -070060}
61
hbosc82f2e12016-09-05 01:36:50 -070062void RTCStatsCollector::GetStatsReport(
63 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
64 RTC_DCHECK(signaling_thread_->IsCurrent());
65 RTC_DCHECK(callback);
66 callbacks_.push_back(callback);
67
hbos0e6758d2016-08-31 07:57:36 -070068 // "Now" using a monotonically increasing timer.
69 int64_t cache_now_us = rtc::TimeMicros();
70 if (cached_report_ &&
71 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -070072 // We have a fresh cached report to deliver.
73 DeliverCachedReport();
74 } else if (!num_pending_partial_reports_) {
75 // Only start gathering stats if we're not already gathering stats. In the
76 // case of already gathering stats, |callback_| will be invoked when there
77 // are no more pending partial reports.
78
79 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
80 // UTC), in microseconds. The system clock could be modified and is not
81 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -070082 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -070083
84 num_pending_partial_reports_ = 3;
85 partial_report_timestamp_us_ = cache_now_us;
86 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
87 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread,
88 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
89 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_,
90 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread,
91 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
92 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
93 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
94 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbos0e6758d2016-08-31 07:57:36 -070095 }
hbosd565b732016-08-30 14:04:35 -070096}
97
98void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -070099 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700100 cached_report_ = nullptr;
101}
102
hbosc82f2e12016-09-05 01:36:50 -0700103void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
104 int64_t timestamp_us) {
105 RTC_DCHECK(signaling_thread_->IsCurrent());
106 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
107
hbos6ab97ce2016-10-03 14:16:56 -0700108 SessionStats session_stats;
109 if (pc_->session()->GetTransportStats(&session_stats)) {
110 ProduceCertificateStats_s(timestamp_us, session_stats, report.get());
hbosab9f6e42016-10-07 02:18:47 -0700111 ProduceIceCandidateAndPairStats_s(timestamp_us, session_stats,
112 report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700113 }
114 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700115
116 AddPartialResults(report);
117}
118
119void RTCStatsCollector::ProducePartialResultsOnWorkerThread(
120 int64_t timestamp_us) {
121 RTC_DCHECK(worker_thread_->IsCurrent());
122 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
123
124 // TODO(hbos): Gather stats on worker thread.
125
126 AddPartialResults(report);
127}
128
129void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
130 int64_t timestamp_us) {
131 RTC_DCHECK(network_thread_->IsCurrent());
132 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
133
134 // TODO(hbos): Gather stats on network thread.
135
136 AddPartialResults(report);
137}
138
139void RTCStatsCollector::AddPartialResults(
140 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
141 if (!signaling_thread_->IsCurrent()) {
142 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
143 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
144 rtc::scoped_refptr<RTCStatsCollector>(this),
145 partial_report));
146 return;
147 }
148 AddPartialResults_s(partial_report);
149}
150
151void RTCStatsCollector::AddPartialResults_s(
152 rtc::scoped_refptr<RTCStatsReport> partial_report) {
153 RTC_DCHECK(signaling_thread_->IsCurrent());
154 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
155 if (!partial_report_)
156 partial_report_ = partial_report;
157 else
158 partial_report_->TakeMembersFrom(partial_report);
159 --num_pending_partial_reports_;
160 if (!num_pending_partial_reports_) {
161 cache_timestamp_us_ = partial_report_timestamp_us_;
162 cached_report_ = partial_report_;
163 partial_report_ = nullptr;
164 DeliverCachedReport();
165 }
166}
167
168void RTCStatsCollector::DeliverCachedReport() {
169 RTC_DCHECK(signaling_thread_->IsCurrent());
170 RTC_DCHECK(!callbacks_.empty());
171 RTC_DCHECK(cached_report_);
172 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
173 callbacks_) {
174 callback->OnStatsDelivered(cached_report_);
175 }
176 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700177}
178
hbos6ab97ce2016-10-03 14:16:56 -0700179void RTCStatsCollector::ProduceCertificateStats_s(
180 int64_t timestamp_us, const SessionStats& session_stats,
181 RTCStatsReport* report) const {
182 RTC_DCHECK(signaling_thread_->IsCurrent());
183 for (const auto& transport : session_stats.transport_stats) {
184 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
185 if (pc_->session()->GetLocalCertificate(
186 transport.second.transport_name, &local_certificate)) {
187 ProduceCertificateStatsFromSSLCertificateAndChain_s(
188 timestamp_us, local_certificate->ssl_certificate(), report);
189 }
190 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
191 pc_->session()->GetRemoteSSLCertificate(
192 transport.second.transport_name);
193 if (remote_certificate) {
194 ProduceCertificateStatsFromSSLCertificateAndChain_s(
195 timestamp_us, *remote_certificate.get(), report);
196 }
197 }
198}
199
200void RTCStatsCollector::ProduceCertificateStatsFromSSLCertificateAndChain_s(
201 int64_t timestamp_us, const rtc::SSLCertificate& certificate,
202 RTCStatsReport* report) const {
203 RTC_DCHECK(signaling_thread_->IsCurrent());
204 std::unique_ptr<rtc::SSLCertificateStats> ssl_stats =
205 certificate.GetStats();
206 RTCCertificateStats* prev_stats = nullptr;
207 for (rtc::SSLCertificateStats* s = ssl_stats.get(); s;
208 s = s->issuer.get()) {
209 RTCCertificateStats* stats = new RTCCertificateStats(
210 "RTCCertificate_" + s->fingerprint, timestamp_us);
211 stats->fingerprint = s->fingerprint;
212 stats->fingerprint_algorithm = s->fingerprint_algorithm;
213 stats->base64_certificate = s->base64_certificate;
214 if (prev_stats)
215 prev_stats->issuer_certificate_id = stats->id();
216 report->AddStats(std::unique_ptr<RTCCertificateStats>(stats));
217 prev_stats = stats;
218 }
219}
220
hbosab9f6e42016-10-07 02:18:47 -0700221void RTCStatsCollector::ProduceIceCandidateAndPairStats_s(
222 int64_t timestamp_us, const SessionStats& session_stats,
223 RTCStatsReport* report) const {
224 RTC_DCHECK(signaling_thread_->IsCurrent());
225 for (const auto& transport : session_stats.transport_stats) {
226 for (const auto& channel : transport.second.channel_stats) {
227 for (const cricket::ConnectionInfo& info : channel.connection_infos) {
228 // TODO(hbos): Produce |RTCIceCandidatePairStats| referencing the
229 // resulting |RTCIceCandidateStats| pair. crbug.com/633550
230 // TODO(hbos): There could be other candidates that are not paired with
231 // anything. We don't have a complete list. Local candidates come from
232 // Port objects, and prflx candidates (both local and remote) are only
233 // stored in candidate pairs. crbug.com/632723
234 ProduceIceCandidateStats_s(
235 timestamp_us, info.local_candidate, true, report);
236 ProduceIceCandidateStats_s(
237 timestamp_us, info.remote_candidate, false, report);
238 }
239 }
240 }
241}
242
243const std::string& RTCStatsCollector::ProduceIceCandidateStats_s(
244 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
245 RTCStatsReport* report) const {
246 RTC_DCHECK(signaling_thread_->IsCurrent());
247 const std::string& id = "RTCIceCandidate_" + candidate.id();
248 const RTCStats* stats = report->Get(id);
249 if (!stats) {
250 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
251 if (is_local) {
252 candidate_stats.reset(
253 new RTCLocalIceCandidateStats(id, timestamp_us));
254 } else {
255 candidate_stats.reset(
256 new RTCRemoteIceCandidateStats(id, timestamp_us));
257 }
258 candidate_stats->ip = candidate.address().ipaddr().ToString();
259 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
260 candidate_stats->protocol = candidate.protocol();
261 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
262 candidate.type());
263 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
264 // TODO(hbos): Define candidate_stats->url. crbug.com/632723
265
266 stats = candidate_stats.get();
267 report->AddStats(std::move(candidate_stats));
268 }
269 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
270 : RTCRemoteIceCandidateStats::kType);
271 return stats->id();
272}
273
hbos6ab97ce2016-10-03 14:16:56 -0700274void RTCStatsCollector::ProducePeerConnectionStats_s(
275 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700276 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700277 // TODO(hbos): If data channels are removed from the peer connection this will
278 // yield incorrect counts. Address before closing crbug.com/636818. See
279 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*.
280 uint32_t data_channels_opened = 0;
281 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels =
282 pc_->sctp_data_channels();
283 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) {
284 if (data_channel->state() == DataChannelInterface::kOpen)
285 ++data_channels_opened;
286 }
287 // There is always just one |RTCPeerConnectionStats| so its |id| can be a
288 // constant.
289 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700290 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbosd565b732016-08-30 14:04:35 -0700291 stats->data_channels_opened = data_channels_opened;
292 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) -
293 data_channels_opened;
hbos6ab97ce2016-10-03 14:16:56 -0700294 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700295}
296
297} // namespace webrtc