blob: 084eab177cc7e557df3c69579a9807463e481e2a [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"
hbosd565b732016-08-30 14:04:35 -070021
22namespace webrtc {
23
hbosc82f2e12016-09-05 01:36:50 -070024rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
25 PeerConnection* pc, int64_t cache_lifetime_us) {
26 return rtc::scoped_refptr<RTCStatsCollector>(
27 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
28}
29
30RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
31 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -070032 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -070033 signaling_thread_(pc->session()->signaling_thread()),
34 worker_thread_(pc->session()->worker_thread()),
35 network_thread_(pc->session()->network_thread()),
36 num_pending_partial_reports_(0),
37 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -070038 cache_timestamp_us_(0),
39 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -070040 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -070041 RTC_DCHECK(signaling_thread_);
42 RTC_DCHECK(worker_thread_);
43 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -070044 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbosd565b732016-08-30 14:04:35 -070045}
46
hbosc82f2e12016-09-05 01:36:50 -070047void RTCStatsCollector::GetStatsReport(
48 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
49 RTC_DCHECK(signaling_thread_->IsCurrent());
50 RTC_DCHECK(callback);
51 callbacks_.push_back(callback);
52
hbos0e6758d2016-08-31 07:57:36 -070053 // "Now" using a monotonically increasing timer.
54 int64_t cache_now_us = rtc::TimeMicros();
55 if (cached_report_ &&
56 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -070057 // We have a fresh cached report to deliver.
58 DeliverCachedReport();
59 } else if (!num_pending_partial_reports_) {
60 // Only start gathering stats if we're not already gathering stats. In the
61 // case of already gathering stats, |callback_| will be invoked when there
62 // are no more pending partial reports.
63
64 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
65 // UTC), in microseconds. The system clock could be modified and is not
66 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -070067 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -070068
69 num_pending_partial_reports_ = 3;
70 partial_report_timestamp_us_ = cache_now_us;
71 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
72 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread,
73 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
74 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_,
75 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread,
76 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
77 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
78 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
79 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbos0e6758d2016-08-31 07:57:36 -070080 }
hbosd565b732016-08-30 14:04:35 -070081}
82
83void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -070084 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -070085 cached_report_ = nullptr;
86}
87
hbosc82f2e12016-09-05 01:36:50 -070088void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
89 int64_t timestamp_us) {
90 RTC_DCHECK(signaling_thread_->IsCurrent());
91 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
92
hbos6ab97ce2016-10-03 14:16:56 -070093 SessionStats session_stats;
94 if (pc_->session()->GetTransportStats(&session_stats)) {
95 ProduceCertificateStats_s(timestamp_us, session_stats, report.get());
96 }
97 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -070098
99 AddPartialResults(report);
100}
101
102void RTCStatsCollector::ProducePartialResultsOnWorkerThread(
103 int64_t timestamp_us) {
104 RTC_DCHECK(worker_thread_->IsCurrent());
105 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
106
107 // TODO(hbos): Gather stats on worker thread.
108
109 AddPartialResults(report);
110}
111
112void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
113 int64_t timestamp_us) {
114 RTC_DCHECK(network_thread_->IsCurrent());
115 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
116
117 // TODO(hbos): Gather stats on network thread.
118
119 AddPartialResults(report);
120}
121
122void RTCStatsCollector::AddPartialResults(
123 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
124 if (!signaling_thread_->IsCurrent()) {
125 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
126 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
127 rtc::scoped_refptr<RTCStatsCollector>(this),
128 partial_report));
129 return;
130 }
131 AddPartialResults_s(partial_report);
132}
133
134void RTCStatsCollector::AddPartialResults_s(
135 rtc::scoped_refptr<RTCStatsReport> partial_report) {
136 RTC_DCHECK(signaling_thread_->IsCurrent());
137 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
138 if (!partial_report_)
139 partial_report_ = partial_report;
140 else
141 partial_report_->TakeMembersFrom(partial_report);
142 --num_pending_partial_reports_;
143 if (!num_pending_partial_reports_) {
144 cache_timestamp_us_ = partial_report_timestamp_us_;
145 cached_report_ = partial_report_;
146 partial_report_ = nullptr;
147 DeliverCachedReport();
148 }
149}
150
151void RTCStatsCollector::DeliverCachedReport() {
152 RTC_DCHECK(signaling_thread_->IsCurrent());
153 RTC_DCHECK(!callbacks_.empty());
154 RTC_DCHECK(cached_report_);
155 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
156 callbacks_) {
157 callback->OnStatsDelivered(cached_report_);
158 }
159 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700160}
161
hbos6ab97ce2016-10-03 14:16:56 -0700162void RTCStatsCollector::ProduceCertificateStats_s(
163 int64_t timestamp_us, const SessionStats& session_stats,
164 RTCStatsReport* report) const {
165 RTC_DCHECK(signaling_thread_->IsCurrent());
166 for (const auto& transport : session_stats.transport_stats) {
167 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
168 if (pc_->session()->GetLocalCertificate(
169 transport.second.transport_name, &local_certificate)) {
170 ProduceCertificateStatsFromSSLCertificateAndChain_s(
171 timestamp_us, local_certificate->ssl_certificate(), report);
172 }
173 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
174 pc_->session()->GetRemoteSSLCertificate(
175 transport.second.transport_name);
176 if (remote_certificate) {
177 ProduceCertificateStatsFromSSLCertificateAndChain_s(
178 timestamp_us, *remote_certificate.get(), report);
179 }
180 }
181}
182
183void RTCStatsCollector::ProduceCertificateStatsFromSSLCertificateAndChain_s(
184 int64_t timestamp_us, const rtc::SSLCertificate& certificate,
185 RTCStatsReport* report) const {
186 RTC_DCHECK(signaling_thread_->IsCurrent());
187 std::unique_ptr<rtc::SSLCertificateStats> ssl_stats =
188 certificate.GetStats();
189 RTCCertificateStats* prev_stats = nullptr;
190 for (rtc::SSLCertificateStats* s = ssl_stats.get(); s;
191 s = s->issuer.get()) {
192 RTCCertificateStats* stats = new RTCCertificateStats(
193 "RTCCertificate_" + s->fingerprint, timestamp_us);
194 stats->fingerprint = s->fingerprint;
195 stats->fingerprint_algorithm = s->fingerprint_algorithm;
196 stats->base64_certificate = s->base64_certificate;
197 if (prev_stats)
198 prev_stats->issuer_certificate_id = stats->id();
199 report->AddStats(std::unique_ptr<RTCCertificateStats>(stats));
200 prev_stats = stats;
201 }
202}
203
204void RTCStatsCollector::ProducePeerConnectionStats_s(
205 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700206 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700207 // TODO(hbos): If data channels are removed from the peer connection this will
208 // yield incorrect counts. Address before closing crbug.com/636818. See
209 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*.
210 uint32_t data_channels_opened = 0;
211 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels =
212 pc_->sctp_data_channels();
213 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) {
214 if (data_channel->state() == DataChannelInterface::kOpen)
215 ++data_channels_opened;
216 }
217 // There is always just one |RTCPeerConnectionStats| so its |id| can be a
218 // constant.
219 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700220 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbosd565b732016-08-30 14:04:35 -0700221 stats->data_channels_opened = data_channels_opened;
222 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) -
223 data_channels_opened;
hbos6ab97ce2016-10-03 14:16:56 -0700224 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700225}
226
227} // namespace webrtc