blob: 735421d5410533e0b642125c3b27262aed30062a [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#ifndef WEBRTC_STATS_RTCSTATSCOLLECTOR_H_
12#define WEBRTC_STATS_RTCSTATSCOLLECTOR_H_
13
14#include <memory>
15
16#include "webrtc/api/rtcstats_objects.h"
17#include "webrtc/api/rtcstatsreport.h"
18#include "webrtc/base/scoped_ref_ptr.h"
hbos0e6758d2016-08-31 07:57:36 -070019#include "webrtc/base/timeutils.h"
hbosd565b732016-08-30 14:04:35 -070020
21namespace webrtc {
22
23class PeerConnection;
24
25// All calls to the collector and gathering of stats is performed on the
26// signaling thread. A stats report is cached for |cache_lifetime_| ms.
27class RTCStatsCollector {
28 public:
29 explicit RTCStatsCollector(
30 PeerConnection* pc,
hbos0e6758d2016-08-31 07:57:36 -070031 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec);
hbosd565b732016-08-30 14:04:35 -070032
33 // Gets a recent stats report. If there is a report cached that is still fresh
34 // it is returned, otherwise new stats are gathered and returned. A report is
35 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe
36 // to use across multiple threads and may be destructed on any thread.
37 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport();
38 // Clears the cache's reference to the most recent stats report. Subsequently
39 // calling |GetStatsReport| guarantees fresh stats.
40 void ClearCachedStatsReport();
41
42 private:
43 bool IsOnSignalingThread() const;
44
hbos0e6758d2016-08-31 07:57:36 -070045 std::unique_ptr<RTCPeerConnectionStats> ProducePeerConnectionStats(
46 int64_t timestamp_us) const;
hbosd565b732016-08-30 14:04:35 -070047
48 PeerConnection* const pc_;
hbos0e6758d2016-08-31 07:57:36 -070049 // A timestamp, in microseconds, that is based on a timer that is
50 // monotonically increasing. That is, even if the system clock is modified the
51 // difference between the timer and this timestamp is how fresh the cached
52 // report is.
53 int64_t cache_timestamp_us_;
54 int64_t cache_lifetime_us_;
hbosd565b732016-08-30 14:04:35 -070055 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
56};
57
58} // namespace webrtc
59
60#endif // WEBRTC_STATS_RTCSTATSCOLLECTOR_H_