blob: a3f159323f2b6460e4a7267a6f69f728f8f0909e [file] [log] [blame]
hbosd565b732016-08-30 21:04:351/*
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
Steve Anton10542f22019-01-11 17:11:0011#ifndef PC_RTC_STATS_COLLECTOR_H_
12#define PC_RTC_STATS_COLLECTOR_H_
hbosd565b732016-08-30 21:04:3513
hbos2fa7c672016-10-24 11:00:0514#include <map>
hbosd565b732016-08-30 21:04:3515#include <memory>
hbos82ebe022016-11-14 09:41:0916#include <set>
Steve Anton36b29d12017-10-30 16:57:4217#include <string>
hbosc82f2e12016-09-05 08:36:5018#include <vector>
hbosd565b732016-08-30 21:04:3519
Danil Chapovalov66cadcc2018-06-19 14:47:4320#include "absl/types/optional.h"
Mirko Bonadeid9708072019-01-25 19:26:4821#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 17:11:0022#include "api/stats/rtc_stats_collector_callback.h"
23#include "api/stats/rtc_stats_report.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3124#include "api/stats/rtcstats_objects.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3125#include "call/call.h"
Steve Anton10542f22019-01-11 17:11:0026#include "media/base/media_channel.h"
27#include "pc/data_channel.h"
28#include "pc/peer_connection_internal.h"
29#include "pc/track_media_info_map.h"
30#include "rtc_base/async_invoker.h"
31#include "rtc_base/ref_count.h"
Steve Anton10542f22019-01-11 17:11:0032#include "rtc_base/ssl_identity.h"
Artem Titove41c4332018-07-25 13:04:2833#include "rtc_base/third_party/sigslot/sigslot.h"
Steve Anton10542f22019-01-11 17:11:0034#include "rtc_base/time_utils.h"
hbosd565b732016-08-30 21:04:3535
36namespace webrtc {
37
Henrik Boström5b3541f2018-03-19 12:52:5638class RtpSenderInternal;
39class RtpReceiverInternal;
40
hbosc82f2e12016-09-05 08:36:5041// All public methods of the collector are to be called on the signaling thread.
42// Stats are gathered on the signaling, worker and network threads
43// asynchronously. The callback is invoked on the signaling thread. Resulting
44// reports are cached for |cache_lifetime_| ms.
hbos82ebe022016-11-14 09:41:0945class RTCStatsCollector : public virtual rtc::RefCountInterface,
46 public sigslot::has_slots<> {
hbosc82f2e12016-09-05 08:36:5047 public:
48 static rtc::scoped_refptr<RTCStatsCollector> Create(
Steve Anton2d8609c2018-01-24 00:38:4649 PeerConnectionInternal* pc,
hbos0e6758d2016-08-31 14:57:3650 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec);
hbosd565b732016-08-30 21:04:3551
52 // Gets a recent stats report. If there is a report cached that is still fresh
53 // it is returned, otherwise new stats are gathered and returned. A report is
54 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe
55 // to use across multiple threads and may be destructed on any thread.
Henrik Boström5b3541f2018-03-19 12:52:5656 // If the optional selector argument is used, stats are filtered according to
57 // stats selection algorithm before delivery.
58 // https://w3c.github.io/webrtc-pc/#dfn-stats-selection-algorithm
hbosc82f2e12016-09-05 08:36:5059 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
Henrik Boström5b3541f2018-03-19 12:52:5660 // If |selector| is null the selection algorithm is still applied (interpreted
61 // as: no RTP streams are sent by selector). The result is empty.
62 void GetStatsReport(rtc::scoped_refptr<RtpSenderInternal> selector,
63 rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
64 // If |selector| is null the selection algorithm is still applied (interpreted
65 // as: no RTP streams are received by selector). The result is empty.
66 void GetStatsReport(rtc::scoped_refptr<RtpReceiverInternal> selector,
67 rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
hbosd565b732016-08-30 21:04:3568 // Clears the cache's reference to the most recent stats report. Subsequently
69 // calling |GetStatsReport| guarantees fresh stats.
70 void ClearCachedStatsReport();
71
hbosb78306a2016-12-19 13:06:5772 // If there is a |GetStatsReport| requests in-flight, waits until it has been
73 // completed. Must be called on the signaling thread.
74 void WaitForPendingRequest();
75
hbosc82f2e12016-09-05 08:36:5076 protected:
Steve Anton2d8609c2018-01-24 00:38:4677 RTCStatsCollector(PeerConnectionInternal* pc, int64_t cache_lifetime_us);
hbosb78306a2016-12-19 13:06:5778 ~RTCStatsCollector();
hbosd565b732016-08-30 21:04:3579
hbosc82f2e12016-09-05 08:36:5080 // Stats gathering on a particular thread. Calls |AddPartialResults| before
81 // returning. Virtual for the sake of testing.
82 virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us);
hbosc82f2e12016-09-05 08:36:5083 virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us);
84
85 // Can be called on any thread.
86 void AddPartialResults(
87 const rtc::scoped_refptr<RTCStatsReport>& partial_report);
88
89 private:
Henrik Boström5b3541f2018-03-19 12:52:5690 class RequestInfo {
91 public:
92 enum class FilterMode { kAll, kSenderSelector, kReceiverSelector };
93
94 // Constructs with FilterMode::kAll.
95 explicit RequestInfo(
96 rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
97 // Constructs with FilterMode::kSenderSelector. The selection algorithm is
98 // applied even if |selector| is null, resulting in an empty report.
99 RequestInfo(rtc::scoped_refptr<RtpSenderInternal> selector,
100 rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
101 // Constructs with FilterMode::kReceiverSelector. The selection algorithm is
102 // applied even if |selector| is null, resulting in an empty report.
103 RequestInfo(rtc::scoped_refptr<RtpReceiverInternal> selector,
104 rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
105
106 FilterMode filter_mode() const { return filter_mode_; }
107 rtc::scoped_refptr<RTCStatsCollectorCallback> callback() const {
108 return callback_;
109 }
110 rtc::scoped_refptr<RtpSenderInternal> sender_selector() const {
111 RTC_DCHECK(filter_mode_ == FilterMode::kSenderSelector);
112 return sender_selector_;
113 }
114 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector() const {
115 RTC_DCHECK(filter_mode_ == FilterMode::kReceiverSelector);
116 return receiver_selector_;
117 }
118
119 private:
120 RequestInfo(FilterMode filter_mode,
121 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
122 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
123 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector);
124
125 FilterMode filter_mode_;
126 rtc::scoped_refptr<RTCStatsCollectorCallback> callback_;
127 rtc::scoped_refptr<RtpSenderInternal> sender_selector_;
128 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector_;
129 };
130
131 void GetStatsReportInternal(RequestInfo request);
132
hbos2fa7c672016-10-24 11:00:05133 struct CertificateStatsPair {
134 std::unique_ptr<rtc::SSLCertificateStats> local;
135 std::unique_ptr<rtc::SSLCertificateStats> remote;
136 };
137
Steve Anton57858b32018-02-15 23:19:50138 // Structure for tracking stats about each RtpTransceiver managed by the
139 // PeerConnection. This can either by a Plan B style or Unified Plan style
140 // transceiver (i.e., can have 0 or many senders and receivers).
141 // Some fields are copied from the RtpTransceiver/BaseChannel object so that
142 // they can be accessed safely on threads other than the signaling thread.
143 // If a BaseChannel is not available (e.g., if signaling has not started),
144 // then |mid| and |transport_name| will be null.
145 struct RtpTransceiverStatsInfo {
146 rtc::scoped_refptr<RtpTransceiver> transceiver;
147 cricket::MediaType media_type;
Danil Chapovalov66cadcc2018-06-19 14:47:43148 absl::optional<std::string> mid;
149 absl::optional<std::string> transport_name;
Steve Anton57858b32018-02-15 23:19:50150 std::unique_ptr<TrackMediaInfoMap> track_media_info_map;
151 };
152
hbosc82f2e12016-09-05 08:36:50153 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report);
Taylor Brandstetter25e022f2018-03-08 17:53:47154 void DeliverCachedReport(
155 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 12:52:56156 std::vector<RequestInfo> requests);
hbosc82f2e12016-09-05 08:36:50157
hbosab9f6e42016-10-07 09:18:47158 // Produces |RTCCertificateStats|.
hbosdf6075a2016-12-19 12:58:02159 void ProduceCertificateStats_n(
hbos2fa7c672016-10-24 11:00:05160 int64_t timestamp_us,
161 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce02016-10-03 21:16:56162 RTCStatsReport* report) const;
hbos0adb8282016-11-23 10:32:06163 // Produces |RTCCodecStats|.
hbosdf6075a2016-12-19 12:58:02164 void ProduceCodecStats_n(
Steve Anton57858b32018-02-15 23:19:50165 int64_t timestamp_us,
166 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 10:32:06167 RTCStatsReport* report) const;
hboscc555c52016-10-18 19:48:31168 // Produces |RTCDataChannelStats|.
Yves Gerey665174f2018-06-19 13:03:05169 void ProduceDataChannelStats_s(int64_t timestamp_us,
170 RTCStatsReport* report) const;
hbosc47a0c32016-10-11 21:54:49171 // Produces |RTCIceCandidatePairStats| and |RTCIceCandidateStats|.
hbosdf6075a2016-12-19 12:58:02172 void ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 13:44:03173 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 18:34:40174 const std::map<std::string, cricket::TransportStats>&
175 transport_stats_by_name,
stefanf79ade12017-06-02 13:44:03176 const Call::Stats& call_stats,
hbosab9f6e42016-10-07 09:18:47177 RTCStatsReport* report) const;
Steve Anton57858b32018-02-15 23:19:50178 // Produces |RTCMediaStreamStats|.
179 void ProduceMediaStreamStats_s(int64_t timestamp_us,
180 RTCStatsReport* report) const;
181 // Produces |RTCMediaStreamTrackStats|.
182 void ProduceMediaStreamTrackStats_s(int64_t timestamp_us,
183 RTCStatsReport* report) const;
hbosab9f6e42016-10-07 09:18:47184 // Produces |RTCPeerConnectionStats|.
Yves Gerey665174f2018-06-19 13:03:05185 void ProducePeerConnectionStats_s(int64_t timestamp_us,
186 RTCStatsReport* report) const;
hboseeafe942016-11-01 10:00:17187 // Produces |RTCInboundRTPStreamStats| and |RTCOutboundRTPStreamStats|.
Steve Anton5dfde182018-02-06 18:34:40188 void ProduceRTPStreamStats_n(
189 int64_t timestamp_us,
Steve Anton57858b32018-02-15 23:19:50190 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
Steve Anton5dfde182018-02-06 18:34:40191 RTCStatsReport* report) const;
Steve Anton57858b32018-02-15 23:19:50192 void ProduceAudioRTPStreamStats_n(int64_t timestamp_us,
193 const RtpTransceiverStatsInfo& stats,
194 RTCStatsReport* report) const;
195 void ProduceVideoRTPStreamStats_n(int64_t timestamp_us,
196 const RtpTransceiverStatsInfo& stats,
197 RTCStatsReport* report) const;
hbos2fa7c672016-10-24 11:00:05198 // Produces |RTCTransportStats|.
hbosdf6075a2016-12-19 12:58:02199 void ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 18:34:40200 int64_t timestamp_us,
201 const std::map<std::string, cricket::TransportStats>&
202 transport_stats_by_name,
hbos2fa7c672016-10-24 11:00:05203 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
204 RTCStatsReport* report) const;
205
206 // Helper function to stats-producing functions.
207 std::map<std::string, CertificateStatsPair>
Steve Anton5dfde182018-02-06 18:34:40208 PrepareTransportCertificateStats_n(
209 const std::map<std::string, cricket::TransportStats>&
210 transport_stats_by_name) const;
Steve Anton57858b32018-02-15 23:19:50211 std::vector<RtpTransceiverStatsInfo> PrepareTransceiverStatsInfos_s() const;
Steve Anton7eca0932018-03-30 22:18:41212 std::set<std::string> PrepareTransportNames_s() const;
hbosd565b732016-08-30 21:04:35213
hbos82ebe022016-11-14 09:41:09214 // Slots for signals (sigslot) that are wired up to |pc_|.
215 void OnDataChannelCreated(DataChannel* channel);
216 // Slots for signals (sigslot) that are wired up to |channel|.
217 void OnDataChannelOpened(DataChannel* channel);
218 void OnDataChannelClosed(DataChannel* channel);
219
Steve Anton2d8609c2018-01-24 00:38:46220 PeerConnectionInternal* const pc_;
hbosc82f2e12016-09-05 08:36:50221 rtc::Thread* const signaling_thread_;
222 rtc::Thread* const worker_thread_;
223 rtc::Thread* const network_thread_;
224 rtc::AsyncInvoker invoker_;
225
226 int num_pending_partial_reports_;
227 int64_t partial_report_timestamp_us_;
228 rtc::scoped_refptr<RTCStatsReport> partial_report_;
Henrik Boström5b3541f2018-03-19 12:52:56229 std::vector<RequestInfo> requests_;
hbosc82f2e12016-09-05 08:36:50230
hbos84abeb12017-01-16 14:16:44231 // Set in |GetStatsReport|, read in |ProducePartialResultsOnNetworkThread| and
232 // |ProducePartialResultsOnSignalingThread|, reset after work is complete. Not
233 // passed as arguments to avoid copies. This is thread safe - when we
234 // set/reset we know there are no pending stats requests in progress.
Steve Anton57858b32018-02-15 23:19:50235 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos_;
Steve Anton7eca0932018-03-30 22:18:41236 std::set<std::string> transport_names_;
Steve Anton5dfde182018-02-06 18:34:40237
stefanf79ade12017-06-02 13:44:03238 Call::Stats call_stats_;
hbosdf6075a2016-12-19 12:58:02239
hbos0e6758d2016-08-31 14:57:36240 // A timestamp, in microseconds, that is based on a timer that is
241 // monotonically increasing. That is, even if the system clock is modified the
242 // difference between the timer and this timestamp is how fresh the cached
243 // report is.
244 int64_t cache_timestamp_us_;
245 int64_t cache_lifetime_us_;
hbosd565b732016-08-30 21:04:35246 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
hbos82ebe022016-11-14 09:41:09247
248 // Data recorded and maintained by the stats collector during its lifetime.
249 // Some stats are produced from this record instead of other components.
250 struct InternalRecord {
Yves Gerey665174f2018-06-19 13:03:05251 InternalRecord() : data_channels_opened(0), data_channels_closed(0) {}
hbos82ebe022016-11-14 09:41:09252
253 // The opened count goes up when a channel is fully opened and the closed
254 // count goes up if a previously opened channel has fully closed. The opened
255 // count does not go down when a channel closes, meaning (opened - closed)
256 // is the number of channels currently opened. A channel that is closed
257 // before reaching the open state does not affect these counters.
258 uint32_t data_channels_opened;
259 uint32_t data_channels_closed;
260 // Identifies by address channels that have been opened, which remain in the
261 // set until they have been fully closed.
262 std::set<uintptr_t> opened_data_channels;
263 };
264 InternalRecord internal_record_;
hbosd565b732016-08-30 21:04:35265};
266
hboscc555c52016-10-18 19:48:31267const char* CandidateTypeToRTCIceCandidateTypeForTesting(
268 const std::string& type);
269const char* DataStateToRTCDataChannelStateForTesting(
270 DataChannelInterface::DataState state);
hbosab9f6e42016-10-07 09:18:47271
hbosd565b732016-08-30 21:04:35272} // namespace webrtc
273
Steve Anton10542f22019-01-11 17:11:00274#endif // PC_RTC_STATS_COLLECTOR_H_