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 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_PC_RTCSTATSCOLLECTOR_H_ |
| 12 | #define WEBRTC_PC_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 | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 16 | #include <set> |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 17 | #include <vector> |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 18 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 19 | #include "webrtc/api/stats/rtcstats_objects.h" |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 20 | #include "webrtc/api/stats/rtcstatscollectorcallback.h" |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 21 | #include "webrtc/api/stats/rtcstatsreport.h" |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 22 | #include "webrtc/api/trackmediainfomap.h" |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 23 | #include "webrtc/base/asyncinvoker.h" |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 24 | #include "webrtc/base/optional.h" |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 25 | #include "webrtc/base/refcount.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 26 | #include "webrtc/base/scoped_ref_ptr.h" |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 27 | #include "webrtc/base/sigslot.h" |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 28 | #include "webrtc/base/sslidentity.h" |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 29 | #include "webrtc/base/timeutils.h" |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 30 | #include "webrtc/media/base/mediachannel.h" |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 31 | #include "webrtc/pc/datachannel.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 32 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 33 | namespace cricket { |
| 34 | class Candidate; |
| 35 | } // namespace cricket |
| 36 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 37 | namespace rtc { |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 38 | class SSLCertificate; |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 39 | } // namespace rtc |
| 40 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 41 | namespace webrtc { |
| 42 | |
| 43 | class PeerConnection; |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 44 | struct SessionStats; |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 45 | struct ChannelNamePairs; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 46 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 47 | // All public methods of the collector are to be called on the signaling thread. |
| 48 | // Stats are gathered on the signaling, worker and network threads |
| 49 | // asynchronously. The callback is invoked on the signaling thread. Resulting |
| 50 | // reports are cached for |cache_lifetime_| ms. |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 51 | class RTCStatsCollector : public virtual rtc::RefCountInterface, |
| 52 | public sigslot::has_slots<> { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 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 | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 67 | // If there is a |GetStatsReport| requests in-flight, waits until it has been |
| 68 | // completed. Must be called on the signaling thread. |
| 69 | void WaitForPendingRequest(); |
| 70 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 71 | protected: |
| 72 | RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 73 | ~RTCStatsCollector(); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 74 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 75 | // Stats gathering on a particular thread. Calls |AddPartialResults| before |
| 76 | // returning. Virtual for the sake of testing. |
| 77 | virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 78 | virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us); |
| 79 | |
| 80 | // Can be called on any thread. |
| 81 | void AddPartialResults( |
| 82 | const rtc::scoped_refptr<RTCStatsReport>& partial_report); |
| 83 | |
| 84 | private: |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 85 | struct CertificateStatsPair { |
| 86 | std::unique_ptr<rtc::SSLCertificateStats> local; |
| 87 | std::unique_ptr<rtc::SSLCertificateStats> remote; |
| 88 | }; |
| 89 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 90 | void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report); |
| 91 | void DeliverCachedReport(); |
| 92 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 93 | // Produces |RTCCertificateStats|. |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 94 | void ProduceCertificateStats_n( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 95 | int64_t timestamp_us, |
| 96 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 97 | RTCStatsReport* report) const; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 98 | // Produces |RTCCodecStats|. |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 99 | void ProduceCodecStats_n( |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 100 | int64_t timestamp_us, const TrackMediaInfoMap& track_media_info_map, |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 101 | RTCStatsReport* report) const; |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 102 | // Produces |RTCDataChannelStats|. |
| 103 | void ProduceDataChannelStats_s( |
| 104 | int64_t timestamp_us, RTCStatsReport* report) const; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 105 | // Produces |RTCIceCandidatePairStats| and |RTCIceCandidateStats|. |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 106 | void ProduceIceCandidateAndPairStats_n( |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 107 | int64_t timestamp_us, const SessionStats& session_stats, |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame^] | 108 | const cricket::VideoMediaInfo* video_media_info, |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 109 | RTCStatsReport* report) const; |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 110 | // Produces |RTCMediaStreamStats| and |RTCMediaStreamTrackStats|. |
| 111 | void ProduceMediaStreamAndTrackStats_s( |
| 112 | int64_t timestamp_us, RTCStatsReport* report) const; |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 113 | // Produces |RTCPeerConnectionStats|. |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 114 | void ProducePeerConnectionStats_s( |
| 115 | int64_t timestamp_us, RTCStatsReport* report) const; |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 116 | // Produces |RTCInboundRTPStreamStats| and |RTCOutboundRTPStreamStats|. |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 117 | void ProduceRTPStreamStats_n( |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 118 | int64_t timestamp_us, const SessionStats& session_stats, |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 119 | const TrackMediaInfoMap& track_media_info_map, |
| 120 | RTCStatsReport* report) const; |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 121 | // Produces |RTCTransportStats|. |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 122 | void ProduceTransportStats_n( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 123 | int64_t timestamp_us, const SessionStats& session_stats, |
| 124 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
| 125 | RTCStatsReport* report) const; |
| 126 | |
| 127 | // Helper function to stats-producing functions. |
| 128 | std::map<std::string, CertificateStatsPair> |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 129 | PrepareTransportCertificateStats_n(const SessionStats& session_stats) const; |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 130 | std::unique_ptr<TrackMediaInfoMap> PrepareTrackMediaInfoMap_s() const; |
| 131 | std::map<MediaStreamTrackInterface*, std::string> PrepareTrackToID_s() const; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 132 | |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 133 | // Slots for signals (sigslot) that are wired up to |pc_|. |
| 134 | void OnDataChannelCreated(DataChannel* channel); |
| 135 | // Slots for signals (sigslot) that are wired up to |channel|. |
| 136 | void OnDataChannelOpened(DataChannel* channel); |
| 137 | void OnDataChannelClosed(DataChannel* channel); |
| 138 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 139 | PeerConnection* const pc_; |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 140 | rtc::Thread* const signaling_thread_; |
| 141 | rtc::Thread* const worker_thread_; |
| 142 | rtc::Thread* const network_thread_; |
| 143 | rtc::AsyncInvoker invoker_; |
| 144 | |
| 145 | int num_pending_partial_reports_; |
| 146 | int64_t partial_report_timestamp_us_; |
| 147 | rtc::scoped_refptr<RTCStatsReport> partial_report_; |
| 148 | std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_; |
| 149 | |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 150 | // Set in |GetStatsReport|, read in |ProducePartialResultsOnNetworkThread| and |
| 151 | // |ProducePartialResultsOnSignalingThread|, reset after work is complete. Not |
| 152 | // passed as arguments to avoid copies. This is thread safe - when we |
| 153 | // set/reset we know there are no pending stats requests in progress. |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 154 | std::unique_ptr<ChannelNamePairs> channel_name_pairs_; |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 155 | std::unique_ptr<TrackMediaInfoMap> track_media_info_map_; |
| 156 | std::map<MediaStreamTrackInterface*, std::string> track_to_id_; |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 157 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 158 | // A timestamp, in microseconds, that is based on a timer that is |
| 159 | // monotonically increasing. That is, even if the system clock is modified the |
| 160 | // difference between the timer and this timestamp is how fresh the cached |
| 161 | // report is. |
| 162 | int64_t cache_timestamp_us_; |
| 163 | int64_t cache_lifetime_us_; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 164 | rtc::scoped_refptr<const RTCStatsReport> cached_report_; |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 165 | |
| 166 | // Data recorded and maintained by the stats collector during its lifetime. |
| 167 | // Some stats are produced from this record instead of other components. |
| 168 | struct InternalRecord { |
| 169 | InternalRecord() : data_channels_opened(0), |
| 170 | data_channels_closed(0) {} |
| 171 | |
| 172 | // The opened count goes up when a channel is fully opened and the closed |
| 173 | // count goes up if a previously opened channel has fully closed. The opened |
| 174 | // count does not go down when a channel closes, meaning (opened - closed) |
| 175 | // is the number of channels currently opened. A channel that is closed |
| 176 | // before reaching the open state does not affect these counters. |
| 177 | uint32_t data_channels_opened; |
| 178 | uint32_t data_channels_closed; |
| 179 | // Identifies by address channels that have been opened, which remain in the |
| 180 | // set until they have been fully closed. |
| 181 | std::set<uintptr_t> opened_data_channels; |
| 182 | }; |
| 183 | InternalRecord internal_record_; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 186 | const char* CandidateTypeToRTCIceCandidateTypeForTesting( |
| 187 | const std::string& type); |
| 188 | const char* DataStateToRTCDataChannelStateForTesting( |
| 189 | DataChannelInterface::DataState state); |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 190 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 191 | } // namespace webrtc |
| 192 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 193 | #endif // WEBRTC_PC_RTCSTATSCOLLECTOR_H_ |