blob: 48e66baf7eaf2222d5d59cc478cc46f0be34bfad [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
ossu7bb87ee2017-01-23 04:56:25 -080011#ifndef WEBRTC_PC_RTCSTATSCOLLECTOR_H_
12#define WEBRTC_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>
hbosc82f2e12016-09-05 01:36:50 -070017#include <vector>
hbosd565b732016-08-30 14:04:35 -070018
hbos74e1a4f2016-09-15 23:33:01 -070019#include "webrtc/api/stats/rtcstats_objects.h"
ossu7bb87ee2017-01-23 04:56:25 -080020#include "webrtc/api/stats/rtcstatscollectorcallback.h"
hbos74e1a4f2016-09-15 23:33:01 -070021#include "webrtc/api/stats/rtcstatsreport.h"
hbosc82f2e12016-09-05 01:36:50 -070022#include "webrtc/base/asyncinvoker.h"
hbos0adb8282016-11-23 02:32:06 -080023#include "webrtc/base/optional.h"
hbosc82f2e12016-09-05 01:36:50 -070024#include "webrtc/base/refcount.h"
hbosd565b732016-08-30 14:04:35 -070025#include "webrtc/base/scoped_ref_ptr.h"
hbos82ebe022016-11-14 01:41:09 -080026#include "webrtc/base/sigslot.h"
hbos2fa7c672016-10-24 04:00:05 -070027#include "webrtc/base/sslidentity.h"
hbos0e6758d2016-08-31 07:57:36 -070028#include "webrtc/base/timeutils.h"
hbos0adb8282016-11-23 02:32:06 -080029#include "webrtc/media/base/mediachannel.h"
ossu7bb87ee2017-01-23 04:56:25 -080030#include "webrtc/pc/datachannel.h"
deadbeef804c1af2017-02-11 19:07:31 -080031#include "webrtc/pc/trackmediainfomap.h"
hbosd565b732016-08-30 14:04:35 -070032
hbosab9f6e42016-10-07 02:18:47 -070033namespace cricket {
34class Candidate;
35} // namespace cricket
36
hbos6ab97ce2016-10-03 14:16:56 -070037namespace rtc {
hbos6ab97ce2016-10-03 14:16:56 -070038class SSLCertificate;
hbos6ab97ce2016-10-03 14:16:56 -070039} // namespace rtc
40
hbosd565b732016-08-30 14:04:35 -070041namespace webrtc {
42
43class PeerConnection;
hbos6ab97ce2016-10-03 14:16:56 -070044struct SessionStats;
hbosdf6075a2016-12-19 04:58:02 -080045struct ChannelNamePairs;
hbosd565b732016-08-30 14:04:35 -070046
hbosc82f2e12016-09-05 01:36:50 -070047// 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.
hbos82ebe022016-11-14 01:41:09 -080051class RTCStatsCollector : public virtual rtc::RefCountInterface,
52 public sigslot::has_slots<> {
hbosc82f2e12016-09-05 01:36:50 -070053 public:
54 static rtc::scoped_refptr<RTCStatsCollector> Create(
hbosd565b732016-08-30 14:04:35 -070055 PeerConnection* pc,
hbos0e6758d2016-08-31 07:57:36 -070056 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec);
hbosd565b732016-08-30 14:04:35 -070057
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.
hbosc82f2e12016-09-05 01:36:50 -070062 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
hbosd565b732016-08-30 14:04:35 -070063 // Clears the cache's reference to the most recent stats report. Subsequently
64 // calling |GetStatsReport| guarantees fresh stats.
65 void ClearCachedStatsReport();
66
hbosb78306a2016-12-19 05:06:57 -080067 // 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
hbosc82f2e12016-09-05 01:36:50 -070071 protected:
72 RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us);
hbosb78306a2016-12-19 05:06:57 -080073 ~RTCStatsCollector();
hbosd565b732016-08-30 14:04:35 -070074
hbosc82f2e12016-09-05 01:36:50 -070075 // 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);
hbosc82f2e12016-09-05 01:36:50 -070078 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:
hbos2fa7c672016-10-24 04:00:05 -070085 struct CertificateStatsPair {
86 std::unique_ptr<rtc::SSLCertificateStats> local;
87 std::unique_ptr<rtc::SSLCertificateStats> remote;
88 };
89
hbosc82f2e12016-09-05 01:36:50 -070090 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report);
91 void DeliverCachedReport();
92
hbosab9f6e42016-10-07 02:18:47 -070093 // Produces |RTCCertificateStats|.
hbosdf6075a2016-12-19 04:58:02 -080094 void ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -070095 int64_t timestamp_us,
96 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -070097 RTCStatsReport* report) const;
hbos0adb8282016-11-23 02:32:06 -080098 // Produces |RTCCodecStats|.
hbosdf6075a2016-12-19 04:58:02 -080099 void ProduceCodecStats_n(
hbos84abeb12017-01-16 06:16:44 -0800100 int64_t timestamp_us, const TrackMediaInfoMap& track_media_info_map,
hbos0adb8282016-11-23 02:32:06 -0800101 RTCStatsReport* report) const;
hboscc555c52016-10-18 12:48:31 -0700102 // Produces |RTCDataChannelStats|.
103 void ProduceDataChannelStats_s(
104 int64_t timestamp_us, RTCStatsReport* report) const;
hbosc47a0c32016-10-11 14:54:49 -0700105 // Produces |RTCIceCandidatePairStats| and |RTCIceCandidateStats|.
hbosdf6075a2016-12-19 04:58:02 -0800106 void ProduceIceCandidateAndPairStats_n(
charujaind72098a2017-06-01 08:54:47 -0700107 int64_t timestamp_us, const SessionStats& session_stats,
hbos338f78a2017-02-07 06:41:21 -0800108 const cricket::VideoMediaInfo* video_media_info,
hbosab9f6e42016-10-07 02:18:47 -0700109 RTCStatsReport* report) const;
hbos09bc1282016-11-08 06:29:22 -0800110 // Produces |RTCMediaStreamStats| and |RTCMediaStreamTrackStats|.
111 void ProduceMediaStreamAndTrackStats_s(
112 int64_t timestamp_us, RTCStatsReport* report) const;
hbosab9f6e42016-10-07 02:18:47 -0700113 // Produces |RTCPeerConnectionStats|.
hbos6ab97ce2016-10-03 14:16:56 -0700114 void ProducePeerConnectionStats_s(
115 int64_t timestamp_us, RTCStatsReport* report) const;
hboseeafe942016-11-01 03:00:17 -0700116 // Produces |RTCInboundRTPStreamStats| and |RTCOutboundRTPStreamStats|.
hbosdf6075a2016-12-19 04:58:02 -0800117 void ProduceRTPStreamStats_n(
hbos6ded1902016-11-01 01:50:46 -0700118 int64_t timestamp_us, const SessionStats& session_stats,
hbos84abeb12017-01-16 06:16:44 -0800119 const TrackMediaInfoMap& track_media_info_map,
120 RTCStatsReport* report) const;
hbos2fa7c672016-10-24 04:00:05 -0700121 // Produces |RTCTransportStats|.
hbosdf6075a2016-12-19 04:58:02 -0800122 void ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700123 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>
hbosdf6075a2016-12-19 04:58:02 -0800129 PrepareTransportCertificateStats_n(const SessionStats& session_stats) const;
hbos84abeb12017-01-16 06:16:44 -0800130 std::unique_ptr<TrackMediaInfoMap> PrepareTrackMediaInfoMap_s() const;
131 std::map<MediaStreamTrackInterface*, std::string> PrepareTrackToID_s() const;
hbosd565b732016-08-30 14:04:35 -0700132
hbos82ebe022016-11-14 01:41:09 -0800133 // 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
hbosd565b732016-08-30 14:04:35 -0700139 PeerConnection* const pc_;
hbosc82f2e12016-09-05 01:36:50 -0700140 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
hbos84abeb12017-01-16 06:16:44 -0800150 // 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.
hbosdf6075a2016-12-19 04:58:02 -0800154 std::unique_ptr<ChannelNamePairs> channel_name_pairs_;
hbos84abeb12017-01-16 06:16:44 -0800155 std::unique_ptr<TrackMediaInfoMap> track_media_info_map_;
156 std::map<MediaStreamTrackInterface*, std::string> track_to_id_;
hbosdf6075a2016-12-19 04:58:02 -0800157
hbos0e6758d2016-08-31 07:57:36 -0700158 // 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_;
hbosd565b732016-08-30 14:04:35 -0700164 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
hbos82ebe022016-11-14 01:41:09 -0800165
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_;
hbosd565b732016-08-30 14:04:35 -0700184};
185
hboscc555c52016-10-18 12:48:31 -0700186const char* CandidateTypeToRTCIceCandidateTypeForTesting(
187 const std::string& type);
188const char* DataStateToRTCDataChannelStateForTesting(
189 DataChannelInterface::DataState state);
hbosab9f6e42016-10-07 02:18:47 -0700190
hbosd565b732016-08-30 14:04:35 -0700191} // namespace webrtc
192
ossu7bb87ee2017-01-23 04:56:25 -0800193#endif // WEBRTC_PC_RTCSTATSCOLLECTOR_H_