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/criticalsection.h" | |
21 #include "webrtc/base/refcount.h" | |
18 #include "webrtc/base/scoped_ref_ptr.h" | 22 #include "webrtc/base/scoped_ref_ptr.h" |
19 #include "webrtc/base/timing.h" | 23 #include "webrtc/base/timing.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 double cache_lifetime = 0.05, | 45 double cache_lifetime = 0.05, |
32 std::unique_ptr<rtc::Timing> timing = std::unique_ptr<rtc::Timing>( | 46 std::unique_ptr<rtc::Timing> timing = std::unique_ptr<rtc::Timing>( |
33 new rtc::Timing())); | 47 new rtc::Timing())); |
34 | 48 |
35 // Gets a recent stats report. If there is a report cached that is still fresh | 49 // 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 | 50 // it is returned, otherwise new stats are gathered and returned. A report is |
37 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe | 51 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe |
38 // to use across multiple threads and may be destructed on any thread. | 52 // to use across multiple threads and may be destructed on any thread. |
39 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport(); | 53 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback); |
40 // Clears the cache's reference to the most recent stats report. Subsequently | 54 // Clears the cache's reference to the most recent stats report. Subsequently |
41 // calling |GetStatsReport| guarantees fresh stats. | 55 // calling |GetStatsReport| guarantees fresh stats. |
42 void ClearCachedStatsReport(); | 56 void ClearCachedStatsReport(); |
43 | 57 |
58 protected: | |
59 // Takes ownership of |timing| (not using |std::unique_ptr| due to | |
60 // incompatibility with |rtc::RefCountedObject| constructors). | |
61 RTCStatsCollector( | |
62 PeerConnection* pc, | |
63 double cache_lifetime, | |
64 rtc::Timing* timing); | |
65 | |
66 // Stats gathering on a particular thread. Calls |AddPartialResults| before | |
67 // returning. Virtual for the sake of testing. | |
68 virtual void ProducePartialResultsOnSignalingThread_s(double timestamp); | |
69 virtual void ProducePartialResultsOnWorkerThread_w(double timestamp); | |
70 virtual void ProducePartialResultsOnNetworkThread_n(double timestamp); | |
hta-webrtc
2016/08/31 09:57:36
Is the _s/_n/_w suffix a WebRTC convention that we
hbos
2016/09/01 14:16:16
Probably not, I'll skip it for these three.
| |
71 | |
72 // Can be called on any thread. | |
73 void AddPartialResults( | |
74 const rtc::scoped_refptr<RTCStatsReport>& partial_report, | |
75 double timestamp); | |
76 | |
44 private: | 77 private: |
45 bool IsOnSignalingThread() const; | 78 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report, |
79 double timestamp); | |
46 | 80 |
47 std::unique_ptr<RTCPeerConnectionStats> ProducePeerConnectionStats() const; | 81 std::unique_ptr<RTCPeerConnectionStats> ProducePeerConnectionStats_s( |
82 double timestamp) const; | |
48 | 83 |
49 PeerConnection* const pc_; | 84 PeerConnection* const pc_; |
50 mutable std::unique_ptr<rtc::Timing> timing_; | 85 rtc::Thread* const signaling_thread_; |
86 rtc::Thread* const worker_thread_; | |
87 rtc::Thread* const network_thread_; | |
88 rtc::AsyncInvoker invoker_; | |
89 | |
90 rtc::CriticalSection lock_; | |
hta-webrtc
2016/08/31 09:57:36
Please add GUARDED_BY statements on the stuff that
hbos
2016/09/01 14:16:16
Done.
| |
91 int num_pending_partial_reports_; | |
92 rtc::scoped_refptr<RTCStatsReport> partial_report_; | |
93 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_; | |
94 | |
95 const std::unique_ptr<rtc::Timing> timing_; | |
51 // Time relative to the UNIX epoch (Jan 1, 1970, UTC), in seconds. | 96 // Time relative to the UNIX epoch (Jan 1, 1970, UTC), in seconds. |
52 double cache_timestamp_; | 97 double cache_timestamp_; |
53 double cache_lifetime_; // In seconds. | 98 double cache_lifetime_; // In seconds. |
54 rtc::scoped_refptr<const RTCStatsReport> cached_report_; | 99 rtc::scoped_refptr<const RTCStatsReport> cached_report_; |
55 }; | 100 }; |
56 | 101 |
57 } // namespace webrtc | 102 } // namespace webrtc |
58 | 103 |
59 #endif // WEBRTC_STATS_RTCSTATSCOLLECTOR_H_ | 104 #endif // WEBRTC_STATS_RTCSTATSCOLLECTOR_H_ |
OLD | NEW |