blob: c32d65f2460a83447946b70430b5bf1bc05b5074 [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
hbos74e1a4f2016-09-15 23:33:01 -070011#ifndef WEBRTC_API_RTCSTATSCOLLECTOR_H_
12#define WEBRTC_API_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
hbos82ebe022016-11-14 01:41:09 -080019#include "webrtc/api/datachannel.h"
hboscc555c52016-10-18 12:48:31 -070020#include "webrtc/api/datachannelinterface.h"
hbos74e1a4f2016-09-15 23:33:01 -070021#include "webrtc/api/stats/rtcstats_objects.h"
22#include "webrtc/api/stats/rtcstatsreport.h"
hbosc82f2e12016-09-05 01:36:50 -070023#include "webrtc/base/asyncinvoker.h"
hbos0adb8282016-11-23 02:32:06 -080024#include "webrtc/base/optional.h"
hbosc82f2e12016-09-05 01:36:50 -070025#include "webrtc/base/refcount.h"
hbosd565b732016-08-30 14:04:35 -070026#include "webrtc/base/scoped_ref_ptr.h"
hbos82ebe022016-11-14 01:41:09 -080027#include "webrtc/base/sigslot.h"
hbos2fa7c672016-10-24 04:00:05 -070028#include "webrtc/base/sslidentity.h"
hbos0e6758d2016-08-31 07:57:36 -070029#include "webrtc/base/timeutils.h"
hbos0adb8282016-11-23 02:32:06 -080030#include "webrtc/media/base/mediachannel.h"
hbosd565b732016-08-30 14:04:35 -070031
hbosab9f6e42016-10-07 02:18:47 -070032namespace cricket {
33class Candidate;
34} // namespace cricket
35
hbos6ab97ce2016-10-03 14:16:56 -070036namespace rtc {
hbos6ab97ce2016-10-03 14:16:56 -070037class SSLCertificate;
hbos6ab97ce2016-10-03 14:16:56 -070038} // namespace rtc
39
hbosd565b732016-08-30 14:04:35 -070040namespace webrtc {
41
42class PeerConnection;
hbos6ab97ce2016-10-03 14:16:56 -070043struct SessionStats;
hbosd565b732016-08-30 14:04:35 -070044
hbosc82f2e12016-09-05 01:36:50 -070045class RTCStatsCollectorCallback : public virtual rtc::RefCountInterface {
hbosd565b732016-08-30 14:04:35 -070046 public:
hbosc82f2e12016-09-05 01:36:50 -070047 virtual ~RTCStatsCollectorCallback() {}
48
49 virtual void OnStatsDelivered(
50 const rtc::scoped_refptr<const RTCStatsReport>& report) = 0;
51};
52
53// All public methods of the collector are to be called on the signaling thread.
54// Stats are gathered on the signaling, worker and network threads
55// asynchronously. The callback is invoked on the signaling thread. Resulting
56// reports are cached for |cache_lifetime_| ms.
hbos82ebe022016-11-14 01:41:09 -080057class RTCStatsCollector : public virtual rtc::RefCountInterface,
58 public sigslot::has_slots<> {
hbosc82f2e12016-09-05 01:36:50 -070059 public:
60 static rtc::scoped_refptr<RTCStatsCollector> Create(
hbosd565b732016-08-30 14:04:35 -070061 PeerConnection* pc,
hbos0e6758d2016-08-31 07:57:36 -070062 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec);
hbosd565b732016-08-30 14:04:35 -070063
64 // Gets a recent stats report. If there is a report cached that is still fresh
65 // it is returned, otherwise new stats are gathered and returned. A report is
66 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe
67 // to use across multiple threads and may be destructed on any thread.
hbosc82f2e12016-09-05 01:36:50 -070068 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
hbosd565b732016-08-30 14:04:35 -070069 // Clears the cache's reference to the most recent stats report. Subsequently
70 // calling |GetStatsReport| guarantees fresh stats.
71 void ClearCachedStatsReport();
72
hbosc82f2e12016-09-05 01:36:50 -070073 protected:
74 RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us);
hbosd565b732016-08-30 14:04:35 -070075
hbosc82f2e12016-09-05 01:36:50 -070076 // Stats gathering on a particular thread. Calls |AddPartialResults| before
77 // returning. Virtual for the sake of testing.
78 virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us);
79 virtual void ProducePartialResultsOnWorkerThread(int64_t timestamp_us);
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:
hbos2fa7c672016-10-24 04:00:05 -070087 struct CertificateStatsPair {
88 std::unique_ptr<rtc::SSLCertificateStats> local;
89 std::unique_ptr<rtc::SSLCertificateStats> remote;
90 };
hbos0adb8282016-11-23 02:32:06 -080091 struct MediaInfo {
92 rtc::Optional<cricket::VoiceMediaInfo> voice;
93 rtc::Optional<cricket::VideoMediaInfo> video;
94 };
hbos2fa7c672016-10-24 04:00:05 -070095
hbosc82f2e12016-09-05 01:36:50 -070096 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report);
97 void DeliverCachedReport();
98
hbosab9f6e42016-10-07 02:18:47 -070099 // Produces |RTCCertificateStats|.
hbos6ab97ce2016-10-03 14:16:56 -0700100 void ProduceCertificateStats_s(
hbos2fa7c672016-10-24 04:00:05 -0700101 int64_t timestamp_us,
102 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700103 RTCStatsReport* report) const;
hbos0adb8282016-11-23 02:32:06 -0800104 // Produces |RTCCodecStats|.
105 void ProduceCodecStats_s(
106 int64_t timestamp_us, const MediaInfo& media_info,
107 RTCStatsReport* report) const;
hboscc555c52016-10-18 12:48:31 -0700108 // Produces |RTCDataChannelStats|.
109 void ProduceDataChannelStats_s(
110 int64_t timestamp_us, RTCStatsReport* report) const;
hbosc47a0c32016-10-11 14:54:49 -0700111 // Produces |RTCIceCandidatePairStats| and |RTCIceCandidateStats|.
hbosab9f6e42016-10-07 02:18:47 -0700112 void ProduceIceCandidateAndPairStats_s(
113 int64_t timestamp_us, const SessionStats& session_stats,
114 RTCStatsReport* report) const;
hbos09bc1282016-11-08 06:29:22 -0800115 // Produces |RTCMediaStreamStats| and |RTCMediaStreamTrackStats|.
116 void ProduceMediaStreamAndTrackStats_s(
117 int64_t timestamp_us, RTCStatsReport* report) const;
hbosab9f6e42016-10-07 02:18:47 -0700118 // Produces |RTCPeerConnectionStats|.
hbos6ab97ce2016-10-03 14:16:56 -0700119 void ProducePeerConnectionStats_s(
120 int64_t timestamp_us, RTCStatsReport* report) const;
hboseeafe942016-11-01 03:00:17 -0700121 // Produces |RTCInboundRTPStreamStats| and |RTCOutboundRTPStreamStats|.
hbos6ded1902016-11-01 01:50:46 -0700122 void ProduceRTPStreamStats_s(
123 int64_t timestamp_us, const SessionStats& session_stats,
hbos0adb8282016-11-23 02:32:06 -0800124 const MediaInfo& media_info, RTCStatsReport* report) const;
hbos2fa7c672016-10-24 04:00:05 -0700125 // Produces |RTCTransportStats|.
126 void ProduceTransportStats_s(
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>
hbos0adb8282016-11-23 02:32:06 -0800133 PrepareTransportCertificateStats(const SessionStats& session_stats) const;
134 MediaInfo PrepareMediaInfo(const SessionStats& session_stats) const;
hbosd565b732016-08-30 14:04:35 -0700135
hbos82ebe022016-11-14 01:41:09 -0800136 // Slots for signals (sigslot) that are wired up to |pc_|.
137 void OnDataChannelCreated(DataChannel* channel);
138 // Slots for signals (sigslot) that are wired up to |channel|.
139 void OnDataChannelOpened(DataChannel* channel);
140 void OnDataChannelClosed(DataChannel* channel);
141
hbosd565b732016-08-30 14:04:35 -0700142 PeerConnection* const pc_;
hbosc82f2e12016-09-05 01:36:50 -0700143 rtc::Thread* const signaling_thread_;
144 rtc::Thread* const worker_thread_;
145 rtc::Thread* const network_thread_;
146 rtc::AsyncInvoker invoker_;
147
148 int num_pending_partial_reports_;
149 int64_t partial_report_timestamp_us_;
150 rtc::scoped_refptr<RTCStatsReport> partial_report_;
151 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_;
152
hbos0e6758d2016-08-31 07:57:36 -0700153 // A timestamp, in microseconds, that is based on a timer that is
154 // monotonically increasing. That is, even if the system clock is modified the
155 // difference between the timer and this timestamp is how fresh the cached
156 // report is.
157 int64_t cache_timestamp_us_;
158 int64_t cache_lifetime_us_;
hbosd565b732016-08-30 14:04:35 -0700159 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
hbos82ebe022016-11-14 01:41:09 -0800160
161 // Data recorded and maintained by the stats collector during its lifetime.
162 // Some stats are produced from this record instead of other components.
163 struct InternalRecord {
164 InternalRecord() : data_channels_opened(0),
165 data_channels_closed(0) {}
166
167 // The opened count goes up when a channel is fully opened and the closed
168 // count goes up if a previously opened channel has fully closed. The opened
169 // count does not go down when a channel closes, meaning (opened - closed)
170 // is the number of channels currently opened. A channel that is closed
171 // before reaching the open state does not affect these counters.
172 uint32_t data_channels_opened;
173 uint32_t data_channels_closed;
174 // Identifies by address channels that have been opened, which remain in the
175 // set until they have been fully closed.
176 std::set<uintptr_t> opened_data_channels;
177 };
178 InternalRecord internal_record_;
hbosd565b732016-08-30 14:04:35 -0700179};
180
hboscc555c52016-10-18 12:48:31 -0700181const char* CandidateTypeToRTCIceCandidateTypeForTesting(
182 const std::string& type);
183const char* DataStateToRTCDataChannelStateForTesting(
184 DataChannelInterface::DataState state);
hbosab9f6e42016-10-07 02:18:47 -0700185
hbosd565b732016-08-30 14:04:35 -0700186} // namespace webrtc
187
hbos74e1a4f2016-09-15 23:33:01 -0700188#endif // WEBRTC_API_RTCSTATSCOLLECTOR_H_