OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_STATS_RTCSTATSCOLLECTOR_H_ | 11 #ifndef WEBRTC_STATS_RTCSTATSCOLLECTOR_H_ |
12 #define WEBRTC_STATS_RTCSTATSCOLLECTOR_H_ | 12 #define WEBRTC_STATS_RTCSTATSCOLLECTOR_H_ |
13 | 13 |
14 #include <memory> | 14 #include <memory> |
15 | 15 |
16 #include "webrtc/api/rtcstats_objects.h" | 16 #include "webrtc/api/rtcstats_objects.h" |
17 #include "webrtc/api/rtcstatsreport.h" | 17 #include "webrtc/api/rtcstatsreport.h" |
18 #include "webrtc/base/scoped_ref_ptr.h" | 18 #include "webrtc/base/scoped_ref_ptr.h" |
19 #include "webrtc/base/timing.h" | 19 #include "webrtc/base/timeutils.h" |
20 | 20 |
21 namespace webrtc { | 21 namespace webrtc { |
22 | 22 |
23 class PeerConnection; | 23 class PeerConnection; |
24 | 24 |
25 // All calls to the collector and gathering of stats is performed on the | 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. | 26 // signaling thread. A stats report is cached for |cache_lifetime_| ms. |
27 class RTCStatsCollector { | 27 class RTCStatsCollector { |
28 public: | 28 public: |
29 explicit RTCStatsCollector( | 29 explicit RTCStatsCollector( |
30 PeerConnection* pc, | 30 PeerConnection* pc, |
31 double cache_lifetime = 0.05, | 31 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec); |
32 std::unique_ptr<rtc::Timing> timing = std::unique_ptr<rtc::Timing>( | |
33 new rtc::Timing())); | |
34 | 32 |
35 // Gets a recent stats report. If there is a report cached that is still fresh | 33 // Gets a recent stats report. If there is a report cached that is still fresh |
36 // it is returned, otherwise new stats are gathered and returned. A report is | 34 // it is returned, otherwise new stats are gathered and returned. A report is |
37 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe | 35 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe |
38 // to use across multiple threads and may be destructed on any thread. | 36 // to use across multiple threads and may be destructed on any thread. |
39 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport(); | 37 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport(); |
40 // Clears the cache's reference to the most recent stats report. Subsequently | 38 // Clears the cache's reference to the most recent stats report. Subsequently |
41 // calling |GetStatsReport| guarantees fresh stats. | 39 // calling |GetStatsReport| guarantees fresh stats. |
42 void ClearCachedStatsReport(); | 40 void ClearCachedStatsReport(); |
43 | 41 |
44 private: | 42 private: |
45 bool IsOnSignalingThread() const; | 43 bool IsOnSignalingThread() const; |
46 | 44 |
47 std::unique_ptr<RTCPeerConnectionStats> ProducePeerConnectionStats() const; | 45 std::unique_ptr<RTCPeerConnectionStats> ProducePeerConnectionStats( |
| 46 int64_t timestamp_us) const; |
48 | 47 |
49 PeerConnection* const pc_; | 48 PeerConnection* const pc_; |
50 mutable std::unique_ptr<rtc::Timing> timing_; | 49 // A timestamp, in microseconds, that is based on a timer that is |
51 // Time relative to the UNIX epoch (Jan 1, 1970, UTC), in seconds. | 50 // monotonically increasing. That is, even if the system clock is modified the |
52 double cache_timestamp_; | 51 // difference between the timer and this timestamp is how fresh the cached |
53 double cache_lifetime_; // In seconds. | 52 // report is. |
| 53 int64_t cache_timestamp_us_; |
| 54 int64_t cache_lifetime_us_; |
54 rtc::scoped_refptr<const RTCStatsReport> cached_report_; | 55 rtc::scoped_refptr<const RTCStatsReport> cached_report_; |
55 }; | 56 }; |
56 | 57 |
57 } // namespace webrtc | 58 } // namespace webrtc |
58 | 59 |
59 #endif // WEBRTC_STATS_RTCSTATSCOLLECTOR_H_ | 60 #endif // WEBRTC_STATS_RTCSTATSCOLLECTOR_H_ |
OLD | NEW |