blob: be5980bffea6e82534824d9fac4db14f40d5fe37 [file] [log] [blame]
hbosd565b732016-08-30 14:04:35 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_RTCSTATSCOLLECTOR_H_
12#define PC_RTCSTATSCOLLECTOR_H_
hbosd565b732016-08-30 14:04:35 -070013
hbos2fa7c672016-10-24 04:00:05 -070014#include <map>
hbosd565b732016-08-30 14:04:35 -070015#include <memory>
hbos82ebe022016-11-14 01:41:09 -080016#include <set>
Steve Anton36b29d12017-10-30 09:57:42 -070017#include <string>
hbosc82f2e12016-09-05 01:36:50 -070018#include <vector>
hbosd565b732016-08-30 14:04:35 -070019
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#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"
Steve Anton2d8609c2018-01-23 16:38:46 -080027#include "pc/peerconnectioninternal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "pc/trackmediainfomap.h"
29#include "rtc_base/asyncinvoker.h"
30#include "rtc_base/refcount.h"
31#include "rtc_base/scoped_ref_ptr.h"
32#include "rtc_base/sigslot.h"
33#include "rtc_base/sslidentity.h"
34#include "rtc_base/timeutils.h"
hbosd565b732016-08-30 14:04:35 -070035
36namespace webrtc {
37
hbosc82f2e12016-09-05 01:36:50 -070038// All public methods of the collector are to be called on the signaling thread.
39// Stats are gathered on the signaling, worker and network threads
40// asynchronously. The callback is invoked on the signaling thread. Resulting
41// reports are cached for |cache_lifetime_| ms.
hbos82ebe022016-11-14 01:41:09 -080042class RTCStatsCollector : public virtual rtc::RefCountInterface,
43 public sigslot::has_slots<> {
hbosc82f2e12016-09-05 01:36:50 -070044 public:
45 static rtc::scoped_refptr<RTCStatsCollector> Create(
Steve Anton2d8609c2018-01-23 16:38:46 -080046 PeerConnectionInternal* pc,
hbos0e6758d2016-08-31 07:57:36 -070047 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec);
hbosd565b732016-08-30 14:04:35 -070048
49 // Gets a recent stats report. If there is a report cached that is still fresh
50 // it is returned, otherwise new stats are gathered and returned. A report is
51 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe
52 // to use across multiple threads and may be destructed on any thread.
hbosc82f2e12016-09-05 01:36:50 -070053 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
hbosd565b732016-08-30 14:04:35 -070054 // Clears the cache's reference to the most recent stats report. Subsequently
55 // calling |GetStatsReport| guarantees fresh stats.
56 void ClearCachedStatsReport();
57
hbosb78306a2016-12-19 05:06:57 -080058 // If there is a |GetStatsReport| requests in-flight, waits until it has been
59 // completed. Must be called on the signaling thread.
60 void WaitForPendingRequest();
61
hbosc82f2e12016-09-05 01:36:50 -070062 protected:
Steve Anton2d8609c2018-01-23 16:38:46 -080063 RTCStatsCollector(PeerConnectionInternal* pc, int64_t cache_lifetime_us);
hbosb78306a2016-12-19 05:06:57 -080064 ~RTCStatsCollector();
hbosd565b732016-08-30 14:04:35 -070065
hbosc82f2e12016-09-05 01:36:50 -070066 // Stats gathering on a particular thread. Calls |AddPartialResults| before
67 // returning. Virtual for the sake of testing.
68 virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -070069 virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us);
70
71 // Can be called on any thread.
72 void AddPartialResults(
73 const rtc::scoped_refptr<RTCStatsReport>& partial_report);
74
75 private:
hbos2fa7c672016-10-24 04:00:05 -070076 struct CertificateStatsPair {
77 std::unique_ptr<rtc::SSLCertificateStats> local;
78 std::unique_ptr<rtc::SSLCertificateStats> remote;
79 };
80
Steve Anton57858b32018-02-15 15:19:50 -080081 // Structure for tracking stats about each RtpTransceiver managed by the
82 // PeerConnection. This can either by a Plan B style or Unified Plan style
83 // transceiver (i.e., can have 0 or many senders and receivers).
84 // Some fields are copied from the RtpTransceiver/BaseChannel object so that
85 // they can be accessed safely on threads other than the signaling thread.
86 // If a BaseChannel is not available (e.g., if signaling has not started),
87 // then |mid| and |transport_name| will be null.
88 struct RtpTransceiverStatsInfo {
89 rtc::scoped_refptr<RtpTransceiver> transceiver;
90 cricket::MediaType media_type;
91 rtc::Optional<std::string> mid;
92 rtc::Optional<std::string> transport_name;
93 std::unique_ptr<TrackMediaInfoMap> track_media_info_map;
94 };
95
hbosc82f2e12016-09-05 01:36:50 -070096 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report);
Taylor Brandstetter25e022f2018-03-08 09:53:47 -080097 void DeliverCachedReport(
98 rtc::scoped_refptr<const RTCStatsReport> cached_report,
99 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks);
hbosc82f2e12016-09-05 01:36:50 -0700100
hbosab9f6e42016-10-07 02:18:47 -0700101 // Produces |RTCCertificateStats|.
hbosdf6075a2016-12-19 04:58:02 -0800102 void ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700103 int64_t timestamp_us,
104 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700105 RTCStatsReport* report) const;
hbos0adb8282016-11-23 02:32:06 -0800106 // Produces |RTCCodecStats|.
hbosdf6075a2016-12-19 04:58:02 -0800107 void ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -0800108 int64_t timestamp_us,
109 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -0800110 RTCStatsReport* report) const;
hboscc555c52016-10-18 12:48:31 -0700111 // Produces |RTCDataChannelStats|.
112 void ProduceDataChannelStats_s(
113 int64_t timestamp_us, RTCStatsReport* report) const;
hbosc47a0c32016-10-11 14:54:49 -0700114 // Produces |RTCIceCandidatePairStats| and |RTCIceCandidateStats|.
hbosdf6075a2016-12-19 04:58:02 -0800115 void ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -0700116 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -0800117 const std::map<std::string, cricket::TransportStats>&
118 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -0700119 const Call::Stats& call_stats,
hbosab9f6e42016-10-07 02:18:47 -0700120 RTCStatsReport* report) const;
Steve Anton57858b32018-02-15 15:19:50 -0800121 // Produces |RTCMediaStreamStats|.
122 void ProduceMediaStreamStats_s(int64_t timestamp_us,
123 RTCStatsReport* report) const;
124 // Produces |RTCMediaStreamTrackStats|.
125 void ProduceMediaStreamTrackStats_s(int64_t timestamp_us,
126 RTCStatsReport* report) const;
hbosab9f6e42016-10-07 02:18:47 -0700127 // Produces |RTCPeerConnectionStats|.
hbos6ab97ce2016-10-03 14:16:56 -0700128 void ProducePeerConnectionStats_s(
129 int64_t timestamp_us, RTCStatsReport* report) const;
hboseeafe942016-11-01 03:00:17 -0700130 // Produces |RTCInboundRTPStreamStats| and |RTCOutboundRTPStreamStats|.
Steve Anton5dfde182018-02-06 10:34:40 -0800131 void ProduceRTPStreamStats_n(
132 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -0800133 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
Steve Anton5dfde182018-02-06 10:34:40 -0800134 RTCStatsReport* report) const;
Steve Anton57858b32018-02-15 15:19:50 -0800135 void ProduceAudioRTPStreamStats_n(int64_t timestamp_us,
136 const RtpTransceiverStatsInfo& stats,
137 RTCStatsReport* report) const;
138 void ProduceVideoRTPStreamStats_n(int64_t timestamp_us,
139 const RtpTransceiverStatsInfo& stats,
140 RTCStatsReport* report) const;
hbos2fa7c672016-10-24 04:00:05 -0700141 // Produces |RTCTransportStats|.
hbosdf6075a2016-12-19 04:58:02 -0800142 void ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -0800143 int64_t timestamp_us,
144 const std::map<std::string, cricket::TransportStats>&
145 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -0700146 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
147 RTCStatsReport* report) const;
148
149 // Helper function to stats-producing functions.
150 std::map<std::string, CertificateStatsPair>
Steve Anton5dfde182018-02-06 10:34:40 -0800151 PrepareTransportCertificateStats_n(
152 const std::map<std::string, cricket::TransportStats>&
153 transport_stats_by_name) const;
Steve Anton57858b32018-02-15 15:19:50 -0800154 std::vector<RtpTransceiverStatsInfo> PrepareTransceiverStatsInfos_s() const;
hbosd565b732016-08-30 14:04:35 -0700155
hbos82ebe022016-11-14 01:41:09 -0800156 // Slots for signals (sigslot) that are wired up to |pc_|.
157 void OnDataChannelCreated(DataChannel* channel);
158 // Slots for signals (sigslot) that are wired up to |channel|.
159 void OnDataChannelOpened(DataChannel* channel);
160 void OnDataChannelClosed(DataChannel* channel);
161
Steve Anton2d8609c2018-01-23 16:38:46 -0800162 PeerConnectionInternal* const pc_;
hbosc82f2e12016-09-05 01:36:50 -0700163 rtc::Thread* const signaling_thread_;
164 rtc::Thread* const worker_thread_;
165 rtc::Thread* const network_thread_;
166 rtc::AsyncInvoker invoker_;
167
168 int num_pending_partial_reports_;
169 int64_t partial_report_timestamp_us_;
170 rtc::scoped_refptr<RTCStatsReport> partial_report_;
171 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_;
172
hbos84abeb12017-01-16 06:16:44 -0800173 // Set in |GetStatsReport|, read in |ProducePartialResultsOnNetworkThread| and
174 // |ProducePartialResultsOnSignalingThread|, reset after work is complete. Not
175 // passed as arguments to avoid copies. This is thread safe - when we
176 // set/reset we know there are no pending stats requests in progress.
Steve Anton57858b32018-02-15 15:19:50 -0800177 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos_;
Steve Anton5dfde182018-02-06 10:34:40 -0800178
stefanf79ade12017-06-02 06:44:03 -0700179 Call::Stats call_stats_;
hbosdf6075a2016-12-19 04:58:02 -0800180
hbos0e6758d2016-08-31 07:57:36 -0700181 // A timestamp, in microseconds, that is based on a timer that is
182 // monotonically increasing. That is, even if the system clock is modified the
183 // difference between the timer and this timestamp is how fresh the cached
184 // report is.
185 int64_t cache_timestamp_us_;
186 int64_t cache_lifetime_us_;
hbosd565b732016-08-30 14:04:35 -0700187 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
hbos82ebe022016-11-14 01:41:09 -0800188
189 // Data recorded and maintained by the stats collector during its lifetime.
190 // Some stats are produced from this record instead of other components.
191 struct InternalRecord {
192 InternalRecord() : data_channels_opened(0),
193 data_channels_closed(0) {}
194
195 // The opened count goes up when a channel is fully opened and the closed
196 // count goes up if a previously opened channel has fully closed. The opened
197 // count does not go down when a channel closes, meaning (opened - closed)
198 // is the number of channels currently opened. A channel that is closed
199 // before reaching the open state does not affect these counters.
200 uint32_t data_channels_opened;
201 uint32_t data_channels_closed;
202 // Identifies by address channels that have been opened, which remain in the
203 // set until they have been fully closed.
204 std::set<uintptr_t> opened_data_channels;
205 };
206 InternalRecord internal_record_;
hbosd565b732016-08-30 14:04:35 -0700207};
208
hboscc555c52016-10-18 12:48:31 -0700209const char* CandidateTypeToRTCIceCandidateTypeForTesting(
210 const std::string& type);
211const char* DataStateToRTCDataChannelStateForTesting(
212 DataChannelInterface::DataState state);
hbosab9f6e42016-10-07 02:18:47 -0700213
hbosd565b732016-08-30 14:04:35 -0700214} // namespace webrtc
215
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200216#endif // PC_RTCSTATSCOLLECTOR_H_