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 #include <vector> | |
15 | 16 |
16 #include "webrtc/api/rtcstats_objects.h" | 17 #include "webrtc/api/rtcstats_objects.h" |
17 #include "webrtc/api/rtcstatsreport.h" | 18 #include "webrtc/api/rtcstatsreport.h" |
19 #include "webrtc/base/asyncinvoker.h" | |
20 #include "webrtc/base/refcount.h" | |
18 #include "webrtc/base/scoped_ref_ptr.h" | 21 #include "webrtc/base/scoped_ref_ptr.h" |
22 #include "webrtc/base/thread_annotations.h" | |
hta-webrtc
2016/09/02 08:47:13
Now that the locks are gone, can you delete this i
hbos
2016/09/02 09:18:30
Done.
| |
19 #include "webrtc/base/timeutils.h" | 23 #include "webrtc/base/timeutils.h" |
20 | 24 |
21 namespace webrtc { | 25 namespace webrtc { |
22 | 26 |
23 class PeerConnection; | 27 class PeerConnection; |
24 | 28 |
25 // All calls to the collector and gathering of stats is performed on the | 29 class RTCStatsCollectorCallback : public virtual rtc::RefCountInterface { |
26 // signaling thread. A stats report is cached for |cache_lifetime_| ms. | |
27 class RTCStatsCollector { | |
28 public: | 30 public: |
29 explicit RTCStatsCollector( | 31 virtual ~RTCStatsCollectorCallback() {} |
32 | |
33 virtual void OnStatsDelivered( | |
34 const rtc::scoped_refptr<const RTCStatsReport>& report) = 0; | |
35 }; | |
36 | |
37 // All public methods of the collector are to be called on the signaling thread. | |
38 // Stats are gathered on the signaling, worker and network threads | |
39 // asynchronously. The callback is invoked on the signaling thread. Resulting | |
40 // reports are cached for |cache_lifetime_| ms. | |
41 class RTCStatsCollector : public virtual rtc::RefCountInterface { | |
42 public: | |
43 static rtc::scoped_refptr<RTCStatsCollector> Create( | |
30 PeerConnection* pc, | 44 PeerConnection* pc, |
31 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec); | 45 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec); |
32 | 46 |
33 // Gets a recent stats report. If there is a report cached that is still fresh | 47 // 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 | 48 // it is returned, otherwise new stats are gathered and returned. A report is |
35 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe | 49 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe |
36 // to use across multiple threads and may be destructed on any thread. | 50 // to use across multiple threads and may be destructed on any thread. |
37 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport(); | 51 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback); |
38 // Clears the cache's reference to the most recent stats report. Subsequently | 52 // Clears the cache's reference to the most recent stats report. Subsequently |
39 // calling |GetStatsReport| guarantees fresh stats. | 53 // calling |GetStatsReport| guarantees fresh stats. |
40 void ClearCachedStatsReport(); | 54 void ClearCachedStatsReport(); |
41 | 55 |
56 protected: | |
57 RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us); | |
58 | |
59 // Stats gathering on a particular thread. Calls |AddPartialResults| before | |
60 // returning. Virtual for the sake of testing. | |
61 virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us); | |
62 virtual void ProducePartialResultsOnWorkerThread(int64_t timestamp_us); | |
63 virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us); | |
64 | |
65 // Can be called on any thread. | |
66 void AddPartialResults( | |
67 const rtc::scoped_refptr<RTCStatsReport>& partial_report); | |
68 | |
42 private: | 69 private: |
43 bool IsOnSignalingThread() const; | 70 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report); |
71 void DeliverCachedReport(); | |
44 | 72 |
45 std::unique_ptr<RTCPeerConnectionStats> ProducePeerConnectionStats( | 73 std::unique_ptr<RTCPeerConnectionStats> ProducePeerConnectionStats_s( |
46 int64_t timestamp_us) const; | 74 int64_t timestamp_us) const; |
47 | 75 |
48 PeerConnection* const pc_; | 76 PeerConnection* const pc_; |
77 rtc::Thread* const signaling_thread_; | |
78 rtc::Thread* const worker_thread_; | |
79 rtc::Thread* const network_thread_; | |
80 rtc::AsyncInvoker invoker_; | |
81 | |
82 int num_pending_partial_reports_; | |
83 int64_t partial_report_timestamp_us_; | |
84 rtc::scoped_refptr<RTCStatsReport> partial_report_; | |
85 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_; | |
86 | |
49 // A timestamp, in microseconds, that is based on a timer that is | 87 // 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 | 88 // 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 | 89 // difference between the timer and this timestamp is how fresh the cached |
52 // report is. | 90 // report is. |
53 int64_t cache_timestamp_us_; | 91 int64_t cache_timestamp_us_; |
54 int64_t cache_lifetime_us_; | 92 int64_t cache_lifetime_us_; |
55 rtc::scoped_refptr<const RTCStatsReport> cached_report_; | 93 rtc::scoped_refptr<const RTCStatsReport> cached_report_; |
56 }; | 94 }; |
57 | 95 |
58 } // namespace webrtc | 96 } // namespace webrtc |
59 | 97 |
60 #endif // WEBRTC_STATS_RTCSTATSCOLLECTOR_H_ | 98 #endif // WEBRTC_STATS_RTCSTATSCOLLECTOR_H_ |
OLD | NEW |