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 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 11 | #ifndef WEBRTC_API_RTCSTATSCOLLECTOR_H_ |
| 12 | #define WEBRTC_API_RTCSTATSCOLLECTOR_H_ |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 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 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 17 | #include "webrtc/api/stats/rtcstats_objects.h" |
| 18 | #include "webrtc/api/stats/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 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame^] | 24 | namespace cricket { |
| 25 | class Candidate; |
| 26 | } // namespace cricket |
| 27 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 28 | namespace rtc { |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 29 | class SSLCertificate; |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 30 | } // namespace rtc |
| 31 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 32 | namespace webrtc { |
| 33 | |
| 34 | class PeerConnection; |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 35 | struct SessionStats; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 36 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 37 | class RTCStatsCollectorCallback : public virtual rtc::RefCountInterface { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 38 | public: |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 39 | virtual ~RTCStatsCollectorCallback() {} |
| 40 | |
| 41 | virtual void OnStatsDelivered( |
| 42 | const rtc::scoped_refptr<const RTCStatsReport>& report) = 0; |
| 43 | }; |
| 44 | |
| 45 | // All public methods of the collector are to be called on the signaling thread. |
| 46 | // Stats are gathered on the signaling, worker and network threads |
| 47 | // asynchronously. The callback is invoked on the signaling thread. Resulting |
| 48 | // reports are cached for |cache_lifetime_| ms. |
| 49 | class RTCStatsCollector : public virtual rtc::RefCountInterface { |
| 50 | public: |
| 51 | static rtc::scoped_refptr<RTCStatsCollector> Create( |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 52 | PeerConnection* pc, |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 53 | int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 54 | |
| 55 | // Gets a recent stats report. If there is a report cached that is still fresh |
| 56 | // it is returned, otherwise new stats are gathered and returned. A report is |
| 57 | // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe |
| 58 | // to use across multiple threads and may be destructed on any thread. |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 59 | void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 60 | // Clears the cache's reference to the most recent stats report. Subsequently |
| 61 | // calling |GetStatsReport| guarantees fresh stats. |
| 62 | void ClearCachedStatsReport(); |
| 63 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 64 | protected: |
| 65 | RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 66 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 67 | // Stats gathering on a particular thread. Calls |AddPartialResults| before |
| 68 | // returning. Virtual for the sake of testing. |
| 69 | virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us); |
| 70 | virtual void ProducePartialResultsOnWorkerThread(int64_t timestamp_us); |
| 71 | virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us); |
| 72 | |
| 73 | // Can be called on any thread. |
| 74 | void AddPartialResults( |
| 75 | const rtc::scoped_refptr<RTCStatsReport>& partial_report); |
| 76 | |
| 77 | private: |
| 78 | void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report); |
| 79 | void DeliverCachedReport(); |
| 80 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame^] | 81 | // Produces |RTCCertificateStats|. |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 82 | void ProduceCertificateStats_s( |
| 83 | int64_t timestamp_us, const SessionStats& session_stats, |
| 84 | RTCStatsReport* report) const; |
| 85 | void ProduceCertificateStatsFromSSLCertificateAndChain_s( |
| 86 | int64_t timestamp_us, const rtc::SSLCertificate& certificate, |
| 87 | RTCStatsReport* report) const; |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame^] | 88 | // Produces |RTCIceCandidateStats|. TODO(hbos): Should also produce |
| 89 | // |RTCIceCandidatePairStats|. crbug.com/633550 |
| 90 | void ProduceIceCandidateAndPairStats_s( |
| 91 | int64_t timestamp_us, const SessionStats& session_stats, |
| 92 | RTCStatsReport* report) const; |
| 93 | const std::string& ProduceIceCandidateStats_s( |
| 94 | int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local, |
| 95 | RTCStatsReport* report) const; |
| 96 | // Produces |RTCPeerConnectionStats|. |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 97 | void ProducePeerConnectionStats_s( |
| 98 | int64_t timestamp_us, RTCStatsReport* report) const; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 99 | |
| 100 | PeerConnection* const pc_; |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 101 | rtc::Thread* const signaling_thread_; |
| 102 | rtc::Thread* const worker_thread_; |
| 103 | rtc::Thread* const network_thread_; |
| 104 | rtc::AsyncInvoker invoker_; |
| 105 | |
| 106 | int num_pending_partial_reports_; |
| 107 | int64_t partial_report_timestamp_us_; |
| 108 | rtc::scoped_refptr<RTCStatsReport> partial_report_; |
| 109 | std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_; |
| 110 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 111 | // A timestamp, in microseconds, that is based on a timer that is |
| 112 | // monotonically increasing. That is, even if the system clock is modified the |
| 113 | // difference between the timer and this timestamp is how fresh the cached |
| 114 | // report is. |
| 115 | int64_t cache_timestamp_us_; |
| 116 | int64_t cache_lifetime_us_; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 117 | rtc::scoped_refptr<const RTCStatsReport> cached_report_; |
| 118 | }; |
| 119 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame^] | 120 | // Helper function, exposed for unittests. |
| 121 | const char* CandidateTypeToRTCIceCandidateType(const std::string& type); |
| 122 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 123 | } // namespace webrtc |
| 124 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 125 | #endif // WEBRTC_API_RTCSTATSCOLLECTOR_H_ |