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