Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: webrtc/stats/rtcstatscollector.cc

Issue 2331373004: PeerConnection[Interface]::GetStats(RTCStatsCollectorCallback*) added. (Closed)
Patch Set: Reverted previous DEPS change (no need to rerun bots yet) Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
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"
19 #include "webrtc/base/timing.h"
20
21 namespace webrtc {
22
23 rtc::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
29 RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
30 int64_t cache_lifetime_us)
31 : pc_(pc),
32 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),
37 cache_timestamp_us_(0),
38 cache_lifetime_us_(cache_lifetime_us) {
39 RTC_DCHECK(pc_);
40 RTC_DCHECK(signaling_thread_);
41 RTC_DCHECK(worker_thread_);
42 RTC_DCHECK(network_thread_);
43 RTC_DCHECK_GE(cache_lifetime_us_, 0);
44 }
45
46 void RTCStatsCollector::GetStatsReport(
47 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
48 RTC_DCHECK(signaling_thread_->IsCurrent());
49 RTC_DCHECK(callback);
50 callbacks_.push_back(callback);
51
52 // "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_) {
56 // 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));
80 }
81 }
82
83 void RTCStatsCollector::ClearCachedStatsReport() {
84 RTC_DCHECK(signaling_thread_->IsCurrent());
85 cached_report_ = nullptr;
86 }
87
88 void 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
98 void 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
108 void 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
118 void 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
130 void 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
147 void 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();
156 }
157
158 std::unique_ptr<RTCPeerConnectionStats>
159 RTCStatsCollector::ProducePeerConnectionStats_s(int64_t timestamp_us) const {
160 RTC_DCHECK(signaling_thread_->IsCurrent());
161 // 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(
174 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
175 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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698