blob: 1cdafd6c914eea1f172e0b3646de600dca3e884d [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
11#include "webrtc/stats/rtcstatscollector.h"
12
13#include <memory>
14#include <utility>
15#include <vector>
16
17#include "webrtc/api/peerconnection.h"
18#include "webrtc/base/checks.h"
hbos0e6758d2016-08-31 07:57:36 -070019#include "webrtc/base/timing.h"
hbosd565b732016-08-30 14:04:35 -070020
21namespace webrtc {
22
hbosc82f2e12016-09-05 01:36:50 -070023rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
24 PeerConnection* pc, int64_t cache_lifetime_us) {
25 return rtc::scoped_refptr<RTCStatsCollector>(
26 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
27}
28
29RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
30 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -070031 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -070032 signaling_thread_(pc->session()->signaling_thread()),
33 worker_thread_(pc->session()->worker_thread()),
34 network_thread_(pc->session()->network_thread()),
35 num_pending_partial_reports_(0),
36 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -070037 cache_timestamp_us_(0),
38 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -070039 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -070040 RTC_DCHECK(signaling_thread_);
41 RTC_DCHECK(worker_thread_);
42 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -070043 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbosd565b732016-08-30 14:04:35 -070044}
45
hbosc82f2e12016-09-05 01:36:50 -070046void RTCStatsCollector::GetStatsReport(
47 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
48 RTC_DCHECK(signaling_thread_->IsCurrent());
49 RTC_DCHECK(callback);
50 callbacks_.push_back(callback);
51
hbos0e6758d2016-08-31 07:57:36 -070052 // "Now" using a monotonically increasing timer.
53 int64_t cache_now_us = rtc::TimeMicros();
54 if (cached_report_ &&
55 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -070056 // We have a fresh cached report to deliver.
57 DeliverCachedReport();
58 } else if (!num_pending_partial_reports_) {
59 // Only start gathering stats if we're not already gathering stats. In the
60 // case of already gathering stats, |callback_| will be invoked when there
61 // are no more pending partial reports.
62
63 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
64 // UTC), in microseconds. The system clock could be modified and is not
65 // necessarily monotonically increasing.
66 int64_t timestamp_us = static_cast<int64_t>(
67 rtc::Timing::WallTimeNow() * rtc::kNumMicrosecsPerSec);
68
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
93 report->AddStats(ProducePeerConnectionStats_s(timestamp_us));
94
95 AddPartialResults(report);
96}
97
98void RTCStatsCollector::ProducePartialResultsOnWorkerThread(
99 int64_t timestamp_us) {
100 RTC_DCHECK(worker_thread_->IsCurrent());
101 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
102
103 // TODO(hbos): Gather stats on worker thread.
104
105 AddPartialResults(report);
106}
107
108void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
109 int64_t timestamp_us) {
110 RTC_DCHECK(network_thread_->IsCurrent());
111 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
112
113 // TODO(hbos): Gather stats on network thread.
114
115 AddPartialResults(report);
116}
117
118void RTCStatsCollector::AddPartialResults(
119 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
120 if (!signaling_thread_->IsCurrent()) {
121 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
122 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
123 rtc::scoped_refptr<RTCStatsCollector>(this),
124 partial_report));
125 return;
126 }
127 AddPartialResults_s(partial_report);
128}
129
130void RTCStatsCollector::AddPartialResults_s(
131 rtc::scoped_refptr<RTCStatsReport> partial_report) {
132 RTC_DCHECK(signaling_thread_->IsCurrent());
133 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
134 if (!partial_report_)
135 partial_report_ = partial_report;
136 else
137 partial_report_->TakeMembersFrom(partial_report);
138 --num_pending_partial_reports_;
139 if (!num_pending_partial_reports_) {
140 cache_timestamp_us_ = partial_report_timestamp_us_;
141 cached_report_ = partial_report_;
142 partial_report_ = nullptr;
143 DeliverCachedReport();
144 }
145}
146
147void RTCStatsCollector::DeliverCachedReport() {
148 RTC_DCHECK(signaling_thread_->IsCurrent());
149 RTC_DCHECK(!callbacks_.empty());
150 RTC_DCHECK(cached_report_);
151 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
152 callbacks_) {
153 callback->OnStatsDelivered(cached_report_);
154 }
155 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700156}
157
158std::unique_ptr<RTCPeerConnectionStats>
hbosc82f2e12016-09-05 01:36:50 -0700159RTCStatsCollector::ProducePeerConnectionStats_s(int64_t timestamp_us) const {
160 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700161 // TODO(hbos): If data channels are removed from the peer connection this will
162 // yield incorrect counts. Address before closing crbug.com/636818. See
163 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*.
164 uint32_t data_channels_opened = 0;
165 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels =
166 pc_->sctp_data_channels();
167 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) {
168 if (data_channel->state() == DataChannelInterface::kOpen)
169 ++data_channels_opened;
170 }
171 // There is always just one |RTCPeerConnectionStats| so its |id| can be a
172 // constant.
173 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700174 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbosd565b732016-08-30 14:04:35 -0700175 stats->data_channels_opened = data_channels_opened;
176 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) -
177 data_channels_opened;
178 return stats;
179}
180
181} // namespace webrtc