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