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

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

Issue 2331373004: PeerConnection[Interface]::GetStats(RTCStatsCollectorCallback*) added. (Closed)
Patch Set: (Corrected an incorrect inclusion) 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
« no previous file with comments | « webrtc/stats/rtcstatscollector.h ('k') | webrtc/stats/rtcstatscollector_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
20 namespace webrtc {
21
22 rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
23 PeerConnection* pc, int64_t cache_lifetime_us) {
24 return rtc::scoped_refptr<RTCStatsCollector>(
25 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
26 }
27
28 RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
29 int64_t cache_lifetime_us)
30 : pc_(pc),
31 signaling_thread_(pc->session()->signaling_thread()),
32 worker_thread_(pc->session()->worker_thread()),
33 network_thread_(pc->session()->network_thread()),
34 num_pending_partial_reports_(0),
35 partial_report_timestamp_us_(0),
36 cache_timestamp_us_(0),
37 cache_lifetime_us_(cache_lifetime_us) {
38 RTC_DCHECK(pc_);
39 RTC_DCHECK(signaling_thread_);
40 RTC_DCHECK(worker_thread_);
41 RTC_DCHECK(network_thread_);
42 RTC_DCHECK_GE(cache_lifetime_us_, 0);
43 }
44
45 void RTCStatsCollector::GetStatsReport(
46 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
47 RTC_DCHECK(signaling_thread_->IsCurrent());
48 RTC_DCHECK(callback);
49 callbacks_.push_back(callback);
50
51 // "Now" using a monotonically increasing timer.
52 int64_t cache_now_us = rtc::TimeMicros();
53 if (cached_report_ &&
54 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
55 // We have a fresh cached report to deliver.
56 DeliverCachedReport();
57 } else if (!num_pending_partial_reports_) {
58 // Only start gathering stats if we're not already gathering stats. In the
59 // case of already gathering stats, |callback_| will be invoked when there
60 // are no more pending partial reports.
61
62 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
63 // UTC), in microseconds. The system clock could be modified and is not
64 // necessarily monotonically increasing.
65 int64_t timestamp_us = rtc::TimeUTCMicros();
66
67 num_pending_partial_reports_ = 3;
68 partial_report_timestamp_us_ = cache_now_us;
69 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
70 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread,
71 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
72 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_,
73 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread,
74 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
75 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
76 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
77 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
78 }
79 }
80
81 void RTCStatsCollector::ClearCachedStatsReport() {
82 RTC_DCHECK(signaling_thread_->IsCurrent());
83 cached_report_ = nullptr;
84 }
85
86 void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
87 int64_t timestamp_us) {
88 RTC_DCHECK(signaling_thread_->IsCurrent());
89 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
90
91 report->AddStats(ProducePeerConnectionStats_s(timestamp_us));
92
93 AddPartialResults(report);
94 }
95
96 void RTCStatsCollector::ProducePartialResultsOnWorkerThread(
97 int64_t timestamp_us) {
98 RTC_DCHECK(worker_thread_->IsCurrent());
99 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
100
101 // TODO(hbos): Gather stats on worker thread.
102
103 AddPartialResults(report);
104 }
105
106 void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
107 int64_t timestamp_us) {
108 RTC_DCHECK(network_thread_->IsCurrent());
109 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
110
111 // TODO(hbos): Gather stats on network thread.
112
113 AddPartialResults(report);
114 }
115
116 void RTCStatsCollector::AddPartialResults(
117 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
118 if (!signaling_thread_->IsCurrent()) {
119 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
120 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
121 rtc::scoped_refptr<RTCStatsCollector>(this),
122 partial_report));
123 return;
124 }
125 AddPartialResults_s(partial_report);
126 }
127
128 void RTCStatsCollector::AddPartialResults_s(
129 rtc::scoped_refptr<RTCStatsReport> partial_report) {
130 RTC_DCHECK(signaling_thread_->IsCurrent());
131 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
132 if (!partial_report_)
133 partial_report_ = partial_report;
134 else
135 partial_report_->TakeMembersFrom(partial_report);
136 --num_pending_partial_reports_;
137 if (!num_pending_partial_reports_) {
138 cache_timestamp_us_ = partial_report_timestamp_us_;
139 cached_report_ = partial_report_;
140 partial_report_ = nullptr;
141 DeliverCachedReport();
142 }
143 }
144
145 void RTCStatsCollector::DeliverCachedReport() {
146 RTC_DCHECK(signaling_thread_->IsCurrent());
147 RTC_DCHECK(!callbacks_.empty());
148 RTC_DCHECK(cached_report_);
149 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
150 callbacks_) {
151 callback->OnStatsDelivered(cached_report_);
152 }
153 callbacks_.clear();
154 }
155
156 std::unique_ptr<RTCPeerConnectionStats>
157 RTCStatsCollector::ProducePeerConnectionStats_s(int64_t timestamp_us) const {
158 RTC_DCHECK(signaling_thread_->IsCurrent());
159 // TODO(hbos): If data channels are removed from the peer connection this will
160 // yield incorrect counts. Address before closing crbug.com/636818. See
161 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*.
162 uint32_t data_channels_opened = 0;
163 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels =
164 pc_->sctp_data_channels();
165 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) {
166 if (data_channel->state() == DataChannelInterface::kOpen)
167 ++data_channels_opened;
168 }
169 // There is always just one |RTCPeerConnectionStats| so its |id| can be a
170 // constant.
171 std::unique_ptr<RTCPeerConnectionStats> stats(
172 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
173 stats->data_channels_opened = data_channels_opened;
174 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) -
175 data_channels_opened;
176 return stats;
177 }
178
179 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/stats/rtcstatscollector.h ('k') | webrtc/stats/rtcstatscollector_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698