blob: 0a99ae9703b71c25993bd9c30ebbb98d58acb2d2 [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;
hbosdf6075a2016-12-19 04:58:02 -080044struct ChannelNamePairs;
hbosd565b732016-08-30 14:04:35 -070045
hbosc82f2e12016-09-05 01:36:50 -070046class RTCStatsCollectorCallback : public virtual rtc::RefCountInterface {
hbosd565b732016-08-30 14:04:35 -070047 public:
hbosc82f2e12016-09-05 01:36:50 -070048 virtual ~RTCStatsCollectorCallback() {}
49
50 virtual void OnStatsDelivered(
51 const rtc::scoped_refptr<const RTCStatsReport>& report) = 0;
52};
53
54// All public methods of the collector are to be called on the signaling thread.
55// Stats are gathered on the signaling, worker and network threads
56// asynchronously. The callback is invoked on the signaling thread. Resulting
57// reports are cached for |cache_lifetime_| ms.
hbos82ebe022016-11-14 01:41:09 -080058class RTCStatsCollector : public virtual rtc::RefCountInterface,
59 public sigslot::has_slots<> {
hbosc82f2e12016-09-05 01:36:50 -070060 public:
61 static rtc::scoped_refptr<RTCStatsCollector> Create(
hbosd565b732016-08-30 14:04:35 -070062 PeerConnection* pc,
hbos0e6758d2016-08-31 07:57:36 -070063 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec);
hbosd565b732016-08-30 14:04:35 -070064
65 // Gets a recent stats report. If there is a report cached that is still fresh
66 // it is returned, otherwise new stats are gathered and returned. A report is
67 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe
68 // to use across multiple threads and may be destructed on any thread.
hbosc82f2e12016-09-05 01:36:50 -070069 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
hbosd565b732016-08-30 14:04:35 -070070 // Clears the cache's reference to the most recent stats report. Subsequently
71 // calling |GetStatsReport| guarantees fresh stats.
72 void ClearCachedStatsReport();
73
hbosb78306a2016-12-19 05:06:57 -080074 // If there is a |GetStatsReport| requests in-flight, waits until it has been
75 // completed. Must be called on the signaling thread.
76 void WaitForPendingRequest();
77
hbosc82f2e12016-09-05 01:36:50 -070078 protected:
79 RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us);
hbosb78306a2016-12-19 05:06:57 -080080 ~RTCStatsCollector();
hbosd565b732016-08-30 14:04:35 -070081
hbosc82f2e12016-09-05 01:36:50 -070082 // Stats gathering on a particular thread. Calls |AddPartialResults| before
83 // returning. Virtual for the sake of testing.
84 virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -070085 virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us);
86
87 // Can be called on any thread.
88 void AddPartialResults(
89 const rtc::scoped_refptr<RTCStatsReport>& partial_report);
90
91 private:
hbos2fa7c672016-10-24 04:00:05 -070092 struct CertificateStatsPair {
93 std::unique_ptr<rtc::SSLCertificateStats> local;
94 std::unique_ptr<rtc::SSLCertificateStats> remote;
95 };
hbos0adb8282016-11-23 02:32:06 -080096 struct MediaInfo {
97 rtc::Optional<cricket::VoiceMediaInfo> voice;
98 rtc::Optional<cricket::VideoMediaInfo> video;
99 };
hbos2fa7c672016-10-24 04:00:05 -0700100
hbosc82f2e12016-09-05 01:36:50 -0700101 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report);
102 void DeliverCachedReport();
103
hbosab9f6e42016-10-07 02:18:47 -0700104 // Produces |RTCCertificateStats|.
hbosdf6075a2016-12-19 04:58:02 -0800105 void ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700106 int64_t timestamp_us,
107 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700108 RTCStatsReport* report) const;
hbos0adb8282016-11-23 02:32:06 -0800109 // Produces |RTCCodecStats|.
hbosdf6075a2016-12-19 04:58:02 -0800110 void ProduceCodecStats_n(
hbos0adb8282016-11-23 02:32:06 -0800111 int64_t timestamp_us, const MediaInfo& media_info,
112 RTCStatsReport* report) const;
hboscc555c52016-10-18 12:48:31 -0700113 // Produces |RTCDataChannelStats|.
114 void ProduceDataChannelStats_s(
115 int64_t timestamp_us, RTCStatsReport* report) const;
hbosc47a0c32016-10-11 14:54:49 -0700116 // Produces |RTCIceCandidatePairStats| and |RTCIceCandidateStats|.
hbosdf6075a2016-12-19 04:58:02 -0800117 void ProduceIceCandidateAndPairStats_n(
hbosab9f6e42016-10-07 02:18:47 -0700118 int64_t timestamp_us, const SessionStats& session_stats,
119 RTCStatsReport* report) const;
hbos09bc1282016-11-08 06:29:22 -0800120 // Produces |RTCMediaStreamStats| and |RTCMediaStreamTrackStats|.
121 void ProduceMediaStreamAndTrackStats_s(
122 int64_t timestamp_us, RTCStatsReport* report) const;
hbosab9f6e42016-10-07 02:18:47 -0700123 // Produces |RTCPeerConnectionStats|.
hbos6ab97ce2016-10-03 14:16:56 -0700124 void ProducePeerConnectionStats_s(
125 int64_t timestamp_us, RTCStatsReport* report) const;
hboseeafe942016-11-01 03:00:17 -0700126 // Produces |RTCInboundRTPStreamStats| and |RTCOutboundRTPStreamStats|.
hbosdf6075a2016-12-19 04:58:02 -0800127 void ProduceRTPStreamStats_n(
hbos6ded1902016-11-01 01:50:46 -0700128 int64_t timestamp_us, const SessionStats& session_stats,
hbos0adb8282016-11-23 02:32:06 -0800129 const MediaInfo& media_info, RTCStatsReport* report) const;
hbos2fa7c672016-10-24 04:00:05 -0700130 // Produces |RTCTransportStats|.
hbosdf6075a2016-12-19 04:58:02 -0800131 void ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700132 int64_t timestamp_us, const SessionStats& session_stats,
133 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
134 RTCStatsReport* report) const;
135
136 // Helper function to stats-producing functions.
137 std::map<std::string, CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -0800138 PrepareTransportCertificateStats_n(const SessionStats& session_stats) const;
139 std::unique_ptr<MediaInfo> PrepareMediaInfo_s() const;
hbosd565b732016-08-30 14:04:35 -0700140
hbos82ebe022016-11-14 01:41:09 -0800141 // Slots for signals (sigslot) that are wired up to |pc_|.
142 void OnDataChannelCreated(DataChannel* channel);
143 // Slots for signals (sigslot) that are wired up to |channel|.
144 void OnDataChannelOpened(DataChannel* channel);
145 void OnDataChannelClosed(DataChannel* channel);
146
hbosd565b732016-08-30 14:04:35 -0700147 PeerConnection* const pc_;
hbosc82f2e12016-09-05 01:36:50 -0700148 rtc::Thread* const signaling_thread_;
149 rtc::Thread* const worker_thread_;
150 rtc::Thread* const network_thread_;
151 rtc::AsyncInvoker invoker_;
152
153 int num_pending_partial_reports_;
154 int64_t partial_report_timestamp_us_;
155 rtc::scoped_refptr<RTCStatsReport> partial_report_;
156 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_;
157
hbosdf6075a2016-12-19 04:58:02 -0800158 // Set in |GetStatsReport|, used in |ProducePartialResultsOnNetworkThread|
159 // (not passed as arguments to avoid copies). This is thread safe - it is set
160 // at the start of |GetStatsReport| after making sure there are no pending
161 // stats requests in progress.
162 std::unique_ptr<ChannelNamePairs> channel_name_pairs_;
163 std::unique_ptr<MediaInfo> media_info_;
164
hbos0e6758d2016-08-31 07:57:36 -0700165 // A timestamp, in microseconds, that is based on a timer that is
166 // monotonically increasing. That is, even if the system clock is modified the
167 // difference between the timer and this timestamp is how fresh the cached
168 // report is.
169 int64_t cache_timestamp_us_;
170 int64_t cache_lifetime_us_;
hbosd565b732016-08-30 14:04:35 -0700171 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
hbos82ebe022016-11-14 01:41:09 -0800172
173 // Data recorded and maintained by the stats collector during its lifetime.
174 // Some stats are produced from this record instead of other components.
175 struct InternalRecord {
176 InternalRecord() : data_channels_opened(0),
177 data_channels_closed(0) {}
178
179 // The opened count goes up when a channel is fully opened and the closed
180 // count goes up if a previously opened channel has fully closed. The opened
181 // count does not go down when a channel closes, meaning (opened - closed)
182 // is the number of channels currently opened. A channel that is closed
183 // before reaching the open state does not affect these counters.
184 uint32_t data_channels_opened;
185 uint32_t data_channels_closed;
186 // Identifies by address channels that have been opened, which remain in the
187 // set until they have been fully closed.
188 std::set<uintptr_t> opened_data_channels;
189 };
190 InternalRecord internal_record_;
hbosd565b732016-08-30 14:04:35 -0700191};
192
hboscc555c52016-10-18 12:48:31 -0700193const char* CandidateTypeToRTCIceCandidateTypeForTesting(
194 const std::string& type);
195const char* DataStateToRTCDataChannelStateForTesting(
196 DataChannelInterface::DataState state);
hbosab9f6e42016-10-07 02:18:47 -0700197
hbosd565b732016-08-30 14:04:35 -0700198} // namespace webrtc
199
hbos74e1a4f2016-09-15 23:33:01 -0700200#endif // WEBRTC_API_RTCSTATSCOLLECTOR_H_