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