xref: /aosp_15_r20/external/webrtc/pc/rtc_stats_collector.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2016 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef PC_RTC_STATS_COLLECTOR_H_
12*d9f75844SAndroid Build Coastguard Worker #define PC_RTC_STATS_COLLECTOR_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <stdint.h>
15*d9f75844SAndroid Build Coastguard Worker 
16*d9f75844SAndroid Build Coastguard Worker #include <cstdint>
17*d9f75844SAndroid Build Coastguard Worker #include <map>
18*d9f75844SAndroid Build Coastguard Worker #include <memory>
19*d9f75844SAndroid Build Coastguard Worker #include <set>
20*d9f75844SAndroid Build Coastguard Worker #include <string>
21*d9f75844SAndroid Build Coastguard Worker #include <vector>
22*d9f75844SAndroid Build Coastguard Worker 
23*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
24*d9f75844SAndroid Build Coastguard Worker #include "api/data_channel_interface.h"
25*d9f75844SAndroid Build Coastguard Worker #include "api/media_types.h"
26*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h"
27*d9f75844SAndroid Build Coastguard Worker #include "api/stats/rtc_stats_collector_callback.h"
28*d9f75844SAndroid Build Coastguard Worker #include "api/stats/rtc_stats_report.h"
29*d9f75844SAndroid Build Coastguard Worker #include "api/stats/rtcstats_objects.h"
30*d9f75844SAndroid Build Coastguard Worker #include "call/call.h"
31*d9f75844SAndroid Build Coastguard Worker #include "media/base/media_channel.h"
32*d9f75844SAndroid Build Coastguard Worker #include "pc/data_channel_utils.h"
33*d9f75844SAndroid Build Coastguard Worker #include "pc/peer_connection_internal.h"
34*d9f75844SAndroid Build Coastguard Worker #include "pc/rtp_receiver.h"
35*d9f75844SAndroid Build Coastguard Worker #include "pc/rtp_sender.h"
36*d9f75844SAndroid Build Coastguard Worker #include "pc/rtp_transceiver.h"
37*d9f75844SAndroid Build Coastguard Worker #include "pc/sctp_data_channel.h"
38*d9f75844SAndroid Build Coastguard Worker #include "pc/track_media_info_map.h"
39*d9f75844SAndroid Build Coastguard Worker #include "pc/transport_stats.h"
40*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
41*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/event.h"
42*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ref_count.h"
43*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ssl_certificate.h"
44*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ssl_identity.h"
45*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/synchronization/mutex.h"
46*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/third_party/sigslot/sigslot.h"
47*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread.h"
48*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/time_utils.h"
49*d9f75844SAndroid Build Coastguard Worker 
50*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
51*d9f75844SAndroid Build Coastguard Worker 
52*d9f75844SAndroid Build Coastguard Worker class RtpSenderInternal;
53*d9f75844SAndroid Build Coastguard Worker class RtpReceiverInternal;
54*d9f75844SAndroid Build Coastguard Worker 
55*d9f75844SAndroid Build Coastguard Worker // All public methods of the collector are to be called on the signaling thread.
56*d9f75844SAndroid Build Coastguard Worker // Stats are gathered on the signaling, worker and network threads
57*d9f75844SAndroid Build Coastguard Worker // asynchronously. The callback is invoked on the signaling thread. Resulting
58*d9f75844SAndroid Build Coastguard Worker // reports are cached for `cache_lifetime_` ms.
59*d9f75844SAndroid Build Coastguard Worker class RTCStatsCollector : public rtc::RefCountInterface,
60*d9f75844SAndroid Build Coastguard Worker                           public sigslot::has_slots<> {
61*d9f75844SAndroid Build Coastguard Worker  public:
62*d9f75844SAndroid Build Coastguard Worker   static rtc::scoped_refptr<RTCStatsCollector> Create(
63*d9f75844SAndroid Build Coastguard Worker       PeerConnectionInternal* pc,
64*d9f75844SAndroid Build Coastguard Worker       int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec);
65*d9f75844SAndroid Build Coastguard Worker 
66*d9f75844SAndroid Build Coastguard Worker   // Gets a recent stats report. If there is a report cached that is still fresh
67*d9f75844SAndroid Build Coastguard Worker   // it is returned, otherwise new stats are gathered and returned. A report is
68*d9f75844SAndroid Build Coastguard Worker   // considered fresh for `cache_lifetime_` ms. const RTCStatsReports are safe
69*d9f75844SAndroid Build Coastguard Worker   // to use across multiple threads and may be destructed on any thread.
70*d9f75844SAndroid Build Coastguard Worker   // If the optional selector argument is used, stats are filtered according to
71*d9f75844SAndroid Build Coastguard Worker   // stats selection algorithm before delivery.
72*d9f75844SAndroid Build Coastguard Worker   // https://w3c.github.io/webrtc-pc/#dfn-stats-selection-algorithm
73*d9f75844SAndroid Build Coastguard Worker   void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
74*d9f75844SAndroid Build Coastguard Worker   // If `selector` is null the selection algorithm is still applied (interpreted
75*d9f75844SAndroid Build Coastguard Worker   // as: no RTP streams are sent by selector). The result is empty.
76*d9f75844SAndroid Build Coastguard Worker   void GetStatsReport(rtc::scoped_refptr<RtpSenderInternal> selector,
77*d9f75844SAndroid Build Coastguard Worker                       rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
78*d9f75844SAndroid Build Coastguard Worker   // If `selector` is null the selection algorithm is still applied (interpreted
79*d9f75844SAndroid Build Coastguard Worker   // as: no RTP streams are received by selector). The result is empty.
80*d9f75844SAndroid Build Coastguard Worker   void GetStatsReport(rtc::scoped_refptr<RtpReceiverInternal> selector,
81*d9f75844SAndroid Build Coastguard Worker                       rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
82*d9f75844SAndroid Build Coastguard Worker   // Clears the cache's reference to the most recent stats report. Subsequently
83*d9f75844SAndroid Build Coastguard Worker   // calling `GetStatsReport` guarantees fresh stats. This method must be called
84*d9f75844SAndroid Build Coastguard Worker   // any time the PeerConnection visibly changes as a result of an API call as
85*d9f75844SAndroid Build Coastguard Worker   // per
86*d9f75844SAndroid Build Coastguard Worker   // https://w3c.github.io/webrtc-stats/#guidelines-for-getstats-results-caching-throttling
87*d9f75844SAndroid Build Coastguard Worker   // and it must be called any time negotiation happens.
88*d9f75844SAndroid Build Coastguard Worker   void ClearCachedStatsReport();
89*d9f75844SAndroid Build Coastguard Worker 
90*d9f75844SAndroid Build Coastguard Worker   // If there is a `GetStatsReport` requests in-flight, waits until it has been
91*d9f75844SAndroid Build Coastguard Worker   // completed. Must be called on the signaling thread.
92*d9f75844SAndroid Build Coastguard Worker   void WaitForPendingRequest();
93*d9f75844SAndroid Build Coastguard Worker 
94*d9f75844SAndroid Build Coastguard Worker  protected:
95*d9f75844SAndroid Build Coastguard Worker   RTCStatsCollector(PeerConnectionInternal* pc, int64_t cache_lifetime_us);
96*d9f75844SAndroid Build Coastguard Worker   ~RTCStatsCollector();
97*d9f75844SAndroid Build Coastguard Worker 
98*d9f75844SAndroid Build Coastguard Worker   struct CertificateStatsPair {
99*d9f75844SAndroid Build Coastguard Worker     std::unique_ptr<rtc::SSLCertificateStats> local;
100*d9f75844SAndroid Build Coastguard Worker     std::unique_ptr<rtc::SSLCertificateStats> remote;
101*d9f75844SAndroid Build Coastguard Worker 
102*d9f75844SAndroid Build Coastguard Worker     CertificateStatsPair Copy() const;
103*d9f75844SAndroid Build Coastguard Worker   };
104*d9f75844SAndroid Build Coastguard Worker 
105*d9f75844SAndroid Build Coastguard Worker   // Stats gathering on a particular thread. Virtual for the sake of testing.
106*d9f75844SAndroid Build Coastguard Worker   virtual void ProducePartialResultsOnSignalingThreadImpl(
107*d9f75844SAndroid Build Coastguard Worker       int64_t timestamp_us,
108*d9f75844SAndroid Build Coastguard Worker       RTCStatsReport* partial_report);
109*d9f75844SAndroid Build Coastguard Worker   virtual void ProducePartialResultsOnNetworkThreadImpl(
110*d9f75844SAndroid Build Coastguard Worker       int64_t timestamp_us,
111*d9f75844SAndroid Build Coastguard Worker       const std::map<std::string, cricket::TransportStats>&
112*d9f75844SAndroid Build Coastguard Worker           transport_stats_by_name,
113*d9f75844SAndroid Build Coastguard Worker       const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
114*d9f75844SAndroid Build Coastguard Worker       RTCStatsReport* partial_report);
115*d9f75844SAndroid Build Coastguard Worker 
116*d9f75844SAndroid Build Coastguard Worker  private:
117*d9f75844SAndroid Build Coastguard Worker   class RequestInfo {
118*d9f75844SAndroid Build Coastguard Worker    public:
119*d9f75844SAndroid Build Coastguard Worker     enum class FilterMode { kAll, kSenderSelector, kReceiverSelector };
120*d9f75844SAndroid Build Coastguard Worker 
121*d9f75844SAndroid Build Coastguard Worker     // Constructs with FilterMode::kAll.
122*d9f75844SAndroid Build Coastguard Worker     explicit RequestInfo(
123*d9f75844SAndroid Build Coastguard Worker         rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
124*d9f75844SAndroid Build Coastguard Worker     // Constructs with FilterMode::kSenderSelector. The selection algorithm is
125*d9f75844SAndroid Build Coastguard Worker     // applied even if `selector` is null, resulting in an empty report.
126*d9f75844SAndroid Build Coastguard Worker     RequestInfo(rtc::scoped_refptr<RtpSenderInternal> selector,
127*d9f75844SAndroid Build Coastguard Worker                 rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
128*d9f75844SAndroid Build Coastguard Worker     // Constructs with FilterMode::kReceiverSelector. The selection algorithm is
129*d9f75844SAndroid Build Coastguard Worker     // applied even if `selector` is null, resulting in an empty report.
130*d9f75844SAndroid Build Coastguard Worker     RequestInfo(rtc::scoped_refptr<RtpReceiverInternal> selector,
131*d9f75844SAndroid Build Coastguard Worker                 rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
132*d9f75844SAndroid Build Coastguard Worker 
filter_mode()133*d9f75844SAndroid Build Coastguard Worker     FilterMode filter_mode() const { return filter_mode_; }
callback()134*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<RTCStatsCollectorCallback> callback() const {
135*d9f75844SAndroid Build Coastguard Worker       return callback_;
136*d9f75844SAndroid Build Coastguard Worker     }
sender_selector()137*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<RtpSenderInternal> sender_selector() const {
138*d9f75844SAndroid Build Coastguard Worker       RTC_DCHECK(filter_mode_ == FilterMode::kSenderSelector);
139*d9f75844SAndroid Build Coastguard Worker       return sender_selector_;
140*d9f75844SAndroid Build Coastguard Worker     }
receiver_selector()141*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<RtpReceiverInternal> receiver_selector() const {
142*d9f75844SAndroid Build Coastguard Worker       RTC_DCHECK(filter_mode_ == FilterMode::kReceiverSelector);
143*d9f75844SAndroid Build Coastguard Worker       return receiver_selector_;
144*d9f75844SAndroid Build Coastguard Worker     }
145*d9f75844SAndroid Build Coastguard Worker 
146*d9f75844SAndroid Build Coastguard Worker    private:
147*d9f75844SAndroid Build Coastguard Worker     RequestInfo(FilterMode filter_mode,
148*d9f75844SAndroid Build Coastguard Worker                 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
149*d9f75844SAndroid Build Coastguard Worker                 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
150*d9f75844SAndroid Build Coastguard Worker                 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector);
151*d9f75844SAndroid Build Coastguard Worker 
152*d9f75844SAndroid Build Coastguard Worker     FilterMode filter_mode_;
153*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<RTCStatsCollectorCallback> callback_;
154*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<RtpSenderInternal> sender_selector_;
155*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<RtpReceiverInternal> receiver_selector_;
156*d9f75844SAndroid Build Coastguard Worker   };
157*d9f75844SAndroid Build Coastguard Worker 
158*d9f75844SAndroid Build Coastguard Worker   void GetStatsReportInternal(RequestInfo request);
159*d9f75844SAndroid Build Coastguard Worker 
160*d9f75844SAndroid Build Coastguard Worker   // Structure for tracking stats about each RtpTransceiver managed by the
161*d9f75844SAndroid Build Coastguard Worker   // PeerConnection. This can either by a Plan B style or Unified Plan style
162*d9f75844SAndroid Build Coastguard Worker   // transceiver (i.e., can have 0 or many senders and receivers).
163*d9f75844SAndroid Build Coastguard Worker   // Some fields are copied from the RtpTransceiver/BaseChannel object so that
164*d9f75844SAndroid Build Coastguard Worker   // they can be accessed safely on threads other than the signaling thread.
165*d9f75844SAndroid Build Coastguard Worker   // If a BaseChannel is not available (e.g., if signaling has not started),
166*d9f75844SAndroid Build Coastguard Worker   // then `mid` and `transport_name` will be null.
167*d9f75844SAndroid Build Coastguard Worker   struct RtpTransceiverStatsInfo {
168*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<RtpTransceiver> transceiver;
169*d9f75844SAndroid Build Coastguard Worker     cricket::MediaType media_type;
170*d9f75844SAndroid Build Coastguard Worker     absl::optional<std::string> mid;
171*d9f75844SAndroid Build Coastguard Worker     absl::optional<std::string> transport_name;
172*d9f75844SAndroid Build Coastguard Worker     TrackMediaInfoMap track_media_info_map;
173*d9f75844SAndroid Build Coastguard Worker   };
174*d9f75844SAndroid Build Coastguard Worker 
175*d9f75844SAndroid Build Coastguard Worker   void DeliverCachedReport(
176*d9f75844SAndroid Build Coastguard Worker       rtc::scoped_refptr<const RTCStatsReport> cached_report,
177*d9f75844SAndroid Build Coastguard Worker       std::vector<RequestInfo> requests);
178*d9f75844SAndroid Build Coastguard Worker 
179*d9f75844SAndroid Build Coastguard Worker   // Produces `RTCCertificateStats`.
180*d9f75844SAndroid Build Coastguard Worker   void ProduceCertificateStats_n(
181*d9f75844SAndroid Build Coastguard Worker       int64_t timestamp_us,
182*d9f75844SAndroid Build Coastguard Worker       const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
183*d9f75844SAndroid Build Coastguard Worker       RTCStatsReport* report) const;
184*d9f75844SAndroid Build Coastguard Worker   // Produces `RTCDataChannelStats`.
185*d9f75844SAndroid Build Coastguard Worker   void ProduceDataChannelStats_s(int64_t timestamp_us,
186*d9f75844SAndroid Build Coastguard Worker                                  RTCStatsReport* report) const;
187*d9f75844SAndroid Build Coastguard Worker   // Produces `RTCIceCandidatePairStats` and `RTCIceCandidateStats`.
188*d9f75844SAndroid Build Coastguard Worker   void ProduceIceCandidateAndPairStats_n(
189*d9f75844SAndroid Build Coastguard Worker       int64_t timestamp_us,
190*d9f75844SAndroid Build Coastguard Worker       const std::map<std::string, cricket::TransportStats>&
191*d9f75844SAndroid Build Coastguard Worker           transport_stats_by_name,
192*d9f75844SAndroid Build Coastguard Worker       const Call::Stats& call_stats,
193*d9f75844SAndroid Build Coastguard Worker       RTCStatsReport* report) const;
194*d9f75844SAndroid Build Coastguard Worker   // Produces `RTCMediaStreamStats`.
195*d9f75844SAndroid Build Coastguard Worker   void ProduceMediaStreamStats_s(int64_t timestamp_us,
196*d9f75844SAndroid Build Coastguard Worker                                  RTCStatsReport* report) const;
197*d9f75844SAndroid Build Coastguard Worker   // Produces `RTCMediaStreamTrackStats`.
198*d9f75844SAndroid Build Coastguard Worker   void ProduceMediaStreamTrackStats_s(int64_t timestamp_us,
199*d9f75844SAndroid Build Coastguard Worker                                       RTCStatsReport* report) const;
200*d9f75844SAndroid Build Coastguard Worker   // Produces RTCMediaSourceStats, including RTCAudioSourceStats and
201*d9f75844SAndroid Build Coastguard Worker   // RTCVideoSourceStats.
202*d9f75844SAndroid Build Coastguard Worker   void ProduceMediaSourceStats_s(int64_t timestamp_us,
203*d9f75844SAndroid Build Coastguard Worker                                  RTCStatsReport* report) const;
204*d9f75844SAndroid Build Coastguard Worker   // Produces `RTCPeerConnectionStats`.
205*d9f75844SAndroid Build Coastguard Worker   void ProducePeerConnectionStats_s(int64_t timestamp_us,
206*d9f75844SAndroid Build Coastguard Worker                                     RTCStatsReport* report) const;
207*d9f75844SAndroid Build Coastguard Worker   // Produces `RTCInboundRTPStreamStats`, `RTCOutboundRTPStreamStats`,
208*d9f75844SAndroid Build Coastguard Worker   // `RTCRemoteInboundRtpStreamStats`, `RTCRemoteOutboundRtpStreamStats` and any
209*d9f75844SAndroid Build Coastguard Worker   // referenced `RTCCodecStats`. This has to be invoked after transport stats
210*d9f75844SAndroid Build Coastguard Worker   // have been created because some metrics are calculated through lookup of
211*d9f75844SAndroid Build Coastguard Worker   // other metrics.
212*d9f75844SAndroid Build Coastguard Worker   void ProduceRTPStreamStats_n(
213*d9f75844SAndroid Build Coastguard Worker       int64_t timestamp_us,
214*d9f75844SAndroid Build Coastguard Worker       const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
215*d9f75844SAndroid Build Coastguard Worker       RTCStatsReport* report) const;
216*d9f75844SAndroid Build Coastguard Worker   void ProduceAudioRTPStreamStats_n(int64_t timestamp_us,
217*d9f75844SAndroid Build Coastguard Worker                                     const RtpTransceiverStatsInfo& stats,
218*d9f75844SAndroid Build Coastguard Worker                                     RTCStatsReport* report) const;
219*d9f75844SAndroid Build Coastguard Worker   void ProduceVideoRTPStreamStats_n(int64_t timestamp_us,
220*d9f75844SAndroid Build Coastguard Worker                                     const RtpTransceiverStatsInfo& stats,
221*d9f75844SAndroid Build Coastguard Worker                                     RTCStatsReport* report) const;
222*d9f75844SAndroid Build Coastguard Worker   // Produces `RTCTransportStats`.
223*d9f75844SAndroid Build Coastguard Worker   void ProduceTransportStats_n(
224*d9f75844SAndroid Build Coastguard Worker       int64_t timestamp_us,
225*d9f75844SAndroid Build Coastguard Worker       const std::map<std::string, cricket::TransportStats>&
226*d9f75844SAndroid Build Coastguard Worker           transport_stats_by_name,
227*d9f75844SAndroid Build Coastguard Worker       const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
228*d9f75844SAndroid Build Coastguard Worker       RTCStatsReport* report) const;
229*d9f75844SAndroid Build Coastguard Worker 
230*d9f75844SAndroid Build Coastguard Worker   // Helper function to stats-producing functions.
231*d9f75844SAndroid Build Coastguard Worker   std::map<std::string, CertificateStatsPair>
232*d9f75844SAndroid Build Coastguard Worker   PrepareTransportCertificateStats_n(
233*d9f75844SAndroid Build Coastguard Worker       const std::map<std::string, cricket::TransportStats>&
234*d9f75844SAndroid Build Coastguard Worker           transport_stats_by_name);
235*d9f75844SAndroid Build Coastguard Worker   // The results are stored in `transceiver_stats_infos_` and `call_stats_`.
236*d9f75844SAndroid Build Coastguard Worker   void PrepareTransceiverStatsInfosAndCallStats_s_w_n();
237*d9f75844SAndroid Build Coastguard Worker 
238*d9f75844SAndroid Build Coastguard Worker   // Stats gathering on a particular thread.
239*d9f75844SAndroid Build Coastguard Worker   void ProducePartialResultsOnSignalingThread(int64_t timestamp_us);
240*d9f75844SAndroid Build Coastguard Worker   void ProducePartialResultsOnNetworkThread(
241*d9f75844SAndroid Build Coastguard Worker       int64_t timestamp_us,
242*d9f75844SAndroid Build Coastguard Worker       absl::optional<std::string> sctp_transport_name);
243*d9f75844SAndroid Build Coastguard Worker   // Merges `network_report_` into `partial_report_` and completes the request.
244*d9f75844SAndroid Build Coastguard Worker   // This is a NO-OP if `network_report_` is null.
245*d9f75844SAndroid Build Coastguard Worker   void MergeNetworkReport_s();
246*d9f75844SAndroid Build Coastguard Worker 
247*d9f75844SAndroid Build Coastguard Worker   // Slots for signals (sigslot) that are wired up to `pc_`.
248*d9f75844SAndroid Build Coastguard Worker   void OnSctpDataChannelCreated(SctpDataChannel* channel);
249*d9f75844SAndroid Build Coastguard Worker   // Slots for signals (sigslot) that are wired up to `channel`.
250*d9f75844SAndroid Build Coastguard Worker   void OnDataChannelOpened(DataChannelInterface* channel);
251*d9f75844SAndroid Build Coastguard Worker   void OnDataChannelClosed(DataChannelInterface* channel);
252*d9f75844SAndroid Build Coastguard Worker 
253*d9f75844SAndroid Build Coastguard Worker   PeerConnectionInternal* const pc_;
254*d9f75844SAndroid Build Coastguard Worker   rtc::Thread* const signaling_thread_;
255*d9f75844SAndroid Build Coastguard Worker   rtc::Thread* const worker_thread_;
256*d9f75844SAndroid Build Coastguard Worker   rtc::Thread* const network_thread_;
257*d9f75844SAndroid Build Coastguard Worker 
258*d9f75844SAndroid Build Coastguard Worker   int num_pending_partial_reports_;
259*d9f75844SAndroid Build Coastguard Worker   int64_t partial_report_timestamp_us_;
260*d9f75844SAndroid Build Coastguard Worker   // Reports that are produced on the signaling thread or the network thread are
261*d9f75844SAndroid Build Coastguard Worker   // merged into this report. It is only touched on the signaling thread. Once
262*d9f75844SAndroid Build Coastguard Worker   // all partial reports are merged this is the result of a request.
263*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<RTCStatsReport> partial_report_;
264*d9f75844SAndroid Build Coastguard Worker   std::vector<RequestInfo> requests_;
265*d9f75844SAndroid Build Coastguard Worker   // Holds the result of ProducePartialResultsOnNetworkThread(). It is merged
266*d9f75844SAndroid Build Coastguard Worker   // into `partial_report_` on the signaling thread and then nulled by
267*d9f75844SAndroid Build Coastguard Worker   // MergeNetworkReport_s(). Thread-safety is ensured by using
268*d9f75844SAndroid Build Coastguard Worker   // `network_report_event_`.
269*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<RTCStatsReport> network_report_;
270*d9f75844SAndroid Build Coastguard Worker   // If set, it is safe to touch the `network_report_` on the signaling thread.
271*d9f75844SAndroid Build Coastguard Worker   // This is reset before async-invoking ProducePartialResultsOnNetworkThread()
272*d9f75844SAndroid Build Coastguard Worker   // and set when ProducePartialResultsOnNetworkThread() is complete, after it
273*d9f75844SAndroid Build Coastguard Worker   // has updated the value of `network_report_`.
274*d9f75844SAndroid Build Coastguard Worker   rtc::Event network_report_event_;
275*d9f75844SAndroid Build Coastguard Worker 
276*d9f75844SAndroid Build Coastguard Worker   // Cleared and set in `PrepareTransceiverStatsInfosAndCallStats_s_w_n`,
277*d9f75844SAndroid Build Coastguard Worker   // starting out on the signaling thread, then network. Later read on the
278*d9f75844SAndroid Build Coastguard Worker   // network and signaling threads as part of collecting stats and finally
279*d9f75844SAndroid Build Coastguard Worker   // reset when the work is done. Initially this variable was added and not
280*d9f75844SAndroid Build Coastguard Worker   // passed around as an arguments to avoid copies. This is thread safe due to
281*d9f75844SAndroid Build Coastguard Worker   // how operations are sequenced and we don't start the stats collection
282*d9f75844SAndroid Build Coastguard Worker   // sequence if one is in progress. As a future improvement though, we could
283*d9f75844SAndroid Build Coastguard Worker   // now get rid of the variable and keep the data scoped within a stats
284*d9f75844SAndroid Build Coastguard Worker   // collection sequence.
285*d9f75844SAndroid Build Coastguard Worker   std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos_;
286*d9f75844SAndroid Build Coastguard Worker   // This cache avoids having to call rtc::SSLCertChain::GetStats(), which can
287*d9f75844SAndroid Build Coastguard Worker   // relatively expensive. ClearCachedStatsReport() needs to be called on
288*d9f75844SAndroid Build Coastguard Worker   // negotiation to ensure the cache is not obsolete.
289*d9f75844SAndroid Build Coastguard Worker   Mutex cached_certificates_mutex_;
290*d9f75844SAndroid Build Coastguard Worker   std::map<std::string, CertificateStatsPair> cached_certificates_by_transport_
291*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(cached_certificates_mutex_);
292*d9f75844SAndroid Build Coastguard Worker 
293*d9f75844SAndroid Build Coastguard Worker   Call::Stats call_stats_;
294*d9f75844SAndroid Build Coastguard Worker 
295*d9f75844SAndroid Build Coastguard Worker   // A timestamp, in microseconds, that is based on a timer that is
296*d9f75844SAndroid Build Coastguard Worker   // monotonically increasing. That is, even if the system clock is modified the
297*d9f75844SAndroid Build Coastguard Worker   // difference between the timer and this timestamp is how fresh the cached
298*d9f75844SAndroid Build Coastguard Worker   // report is.
299*d9f75844SAndroid Build Coastguard Worker   int64_t cache_timestamp_us_;
300*d9f75844SAndroid Build Coastguard Worker   int64_t cache_lifetime_us_;
301*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<const RTCStatsReport> cached_report_;
302*d9f75844SAndroid Build Coastguard Worker 
303*d9f75844SAndroid Build Coastguard Worker   // Data recorded and maintained by the stats collector during its lifetime.
304*d9f75844SAndroid Build Coastguard Worker   // Some stats are produced from this record instead of other components.
305*d9f75844SAndroid Build Coastguard Worker   struct InternalRecord {
InternalRecordInternalRecord306*d9f75844SAndroid Build Coastguard Worker     InternalRecord() : data_channels_opened(0), data_channels_closed(0) {}
307*d9f75844SAndroid Build Coastguard Worker 
308*d9f75844SAndroid Build Coastguard Worker     // The opened count goes up when a channel is fully opened and the closed
309*d9f75844SAndroid Build Coastguard Worker     // count goes up if a previously opened channel has fully closed. The opened
310*d9f75844SAndroid Build Coastguard Worker     // count does not go down when a channel closes, meaning (opened - closed)
311*d9f75844SAndroid Build Coastguard Worker     // is the number of channels currently opened. A channel that is closed
312*d9f75844SAndroid Build Coastguard Worker     // before reaching the open state does not affect these counters.
313*d9f75844SAndroid Build Coastguard Worker     uint32_t data_channels_opened;
314*d9f75844SAndroid Build Coastguard Worker     uint32_t data_channels_closed;
315*d9f75844SAndroid Build Coastguard Worker     // Identifies by address channels that have been opened, which remain in the
316*d9f75844SAndroid Build Coastguard Worker     // set until they have been fully closed.
317*d9f75844SAndroid Build Coastguard Worker     std::set<uintptr_t> opened_data_channels;
318*d9f75844SAndroid Build Coastguard Worker   };
319*d9f75844SAndroid Build Coastguard Worker   InternalRecord internal_record_;
320*d9f75844SAndroid Build Coastguard Worker };
321*d9f75844SAndroid Build Coastguard Worker 
322*d9f75844SAndroid Build Coastguard Worker const char* CandidateTypeToRTCIceCandidateTypeForTesting(
323*d9f75844SAndroid Build Coastguard Worker     const std::string& type);
324*d9f75844SAndroid Build Coastguard Worker const char* DataStateToRTCDataChannelStateForTesting(
325*d9f75844SAndroid Build Coastguard Worker     DataChannelInterface::DataState state);
326*d9f75844SAndroid Build Coastguard Worker 
327*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
328*d9f75844SAndroid Build Coastguard Worker 
329*d9f75844SAndroid Build Coastguard Worker #endif  // PC_RTC_STATS_COLLECTOR_H_
330