blob: 4bedad62eea674333b2ffbc178ed7b60a9a8b36c [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
23RTCStatsCollector::RTCStatsCollector(
24 PeerConnection* pc,
hbos0e6758d2016-08-31 07:57:36 -070025 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -070026 : pc_(pc),
hbos0e6758d2016-08-31 07:57:36 -070027 cache_timestamp_us_(0),
28 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -070029 RTC_DCHECK(pc_);
hbosd565b732016-08-30 14:04:35 -070030 RTC_DCHECK(IsOnSignalingThread());
hbos0e6758d2016-08-31 07:57:36 -070031 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbosd565b732016-08-30 14:04:35 -070032}
33
34rtc::scoped_refptr<const RTCStatsReport> RTCStatsCollector::GetStatsReport() {
35 RTC_DCHECK(IsOnSignalingThread());
hbos0e6758d2016-08-31 07:57:36 -070036 // "Now" using a monotonically increasing timer.
37 int64_t cache_now_us = rtc::TimeMicros();
38 if (cached_report_ &&
39 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosd565b732016-08-30 14:04:35 -070040 return cached_report_;
hbos0e6758d2016-08-31 07:57:36 -070041 }
42 cache_timestamp_us_ = cache_now_us;
43 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970, UTC),
44 // in microseconds. The system clock could be modified and is not necessarily
45 // monotonically increasing.
46 int64_t timestamp_us = static_cast<int64_t>(
47 rtc::Timing::WallTimeNow() * rtc::kNumMicrosecsPerSec);
hbosd565b732016-08-30 14:04:35 -070048
49 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
hbos0e6758d2016-08-31 07:57:36 -070050 report->AddStats(ProducePeerConnectionStats(timestamp_us));
hbosd565b732016-08-30 14:04:35 -070051
52 cached_report_ = report;
53 return cached_report_;
54}
55
56void RTCStatsCollector::ClearCachedStatsReport() {
57 RTC_DCHECK(IsOnSignalingThread());
58 cached_report_ = nullptr;
59}
60
61bool RTCStatsCollector::IsOnSignalingThread() const {
62 return pc_->session()->signaling_thread()->IsCurrent();
63}
64
65std::unique_ptr<RTCPeerConnectionStats>
hbos0e6758d2016-08-31 07:57:36 -070066RTCStatsCollector::ProducePeerConnectionStats(int64_t timestamp_us) const {
hbosd565b732016-08-30 14:04:35 -070067 // TODO(hbos): If data channels are removed from the peer connection this will
68 // yield incorrect counts. Address before closing crbug.com/636818. See
69 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*.
70 uint32_t data_channels_opened = 0;
71 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels =
72 pc_->sctp_data_channels();
73 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) {
74 if (data_channel->state() == DataChannelInterface::kOpen)
75 ++data_channels_opened;
76 }
77 // There is always just one |RTCPeerConnectionStats| so its |id| can be a
78 // constant.
79 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -070080 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbosd565b732016-08-30 14:04:35 -070081 stats->data_channels_opened = data_channels_opened;
82 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) -
83 data_channels_opened;
84 return stats;
85}
86
87} // namespace webrtc