blob: 7b23d247264222151215b0c7425434d5df93d0b9 [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
hbosc82f2e12016-09-05 01:36:50 -070074 protected:
75 RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us);
hbosd565b732016-08-30 14:04:35 -070076
hbosc82f2e12016-09-05 01:36:50 -070077 // Stats gathering on a particular thread. Calls |AddPartialResults| before
78 // returning. Virtual for the sake of testing.
79 virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us);
80 virtual void ProducePartialResultsOnWorkerThread(int64_t timestamp_us);
81 virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us);
82
83 // Can be called on any thread.
84 void AddPartialResults(
85 const rtc::scoped_refptr<RTCStatsReport>& partial_report);
86
87 private:
hbos2fa7c672016-10-24 04:00:05 -070088 struct CertificateStatsPair {
89 std::unique_ptr<rtc::SSLCertificateStats> local;
90 std::unique_ptr<rtc::SSLCertificateStats> remote;
91 };
hbos0adb8282016-11-23 02:32:06 -080092 struct MediaInfo {
93 rtc::Optional<cricket::VoiceMediaInfo> voice;
94 rtc::Optional<cricket::VideoMediaInfo> video;
95 };
hbos2fa7c672016-10-24 04:00:05 -070096
hbosc82f2e12016-09-05 01:36:50 -070097 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report);
98 void DeliverCachedReport();
99
hbosab9f6e42016-10-07 02:18:47 -0700100 // Produces |RTCCertificateStats|.
hbosdf6075a2016-12-19 04:58:02 -0800101 void ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700102 int64_t timestamp_us,
103 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700104 RTCStatsReport* report) const;
hbos0adb8282016-11-23 02:32:06 -0800105 // Produces |RTCCodecStats|.
hbosdf6075a2016-12-19 04:58:02 -0800106 void ProduceCodecStats_n(
hbos0adb8282016-11-23 02:32:06 -0800107 int64_t timestamp_us, const MediaInfo& media_info,
108 RTCStatsReport* report) const;
hboscc555c52016-10-18 12:48:31 -0700109 // Produces |RTCDataChannelStats|.
110 void ProduceDataChannelStats_s(
111 int64_t timestamp_us, RTCStatsReport* report) const;
hbosc47a0c32016-10-11 14:54:49 -0700112 // Produces |RTCIceCandidatePairStats| and |RTCIceCandidateStats|.
hbosdf6075a2016-12-19 04:58:02 -0800113 void ProduceIceCandidateAndPairStats_n(
hbosab9f6e42016-10-07 02:18:47 -0700114 int64_t timestamp_us, const SessionStats& session_stats,
115 RTCStatsReport* report) const;
hbos09bc1282016-11-08 06:29:22 -0800116 // Produces |RTCMediaStreamStats| and |RTCMediaStreamTrackStats|.
117 void ProduceMediaStreamAndTrackStats_s(
118 int64_t timestamp_us, RTCStatsReport* report) const;
hbosab9f6e42016-10-07 02:18:47 -0700119 // Produces |RTCPeerConnectionStats|.
hbos6ab97ce2016-10-03 14:16:56 -0700120 void ProducePeerConnectionStats_s(
121 int64_t timestamp_us, RTCStatsReport* report) const;
hboseeafe942016-11-01 03:00:17 -0700122 // Produces |RTCInboundRTPStreamStats| and |RTCOutboundRTPStreamStats|.
hbosdf6075a2016-12-19 04:58:02 -0800123 void ProduceRTPStreamStats_n(
hbos6ded1902016-11-01 01:50:46 -0700124 int64_t timestamp_us, const SessionStats& session_stats,
hbos0adb8282016-11-23 02:32:06 -0800125 const MediaInfo& media_info, RTCStatsReport* report) const;
hbos2fa7c672016-10-24 04:00:05 -0700126 // Produces |RTCTransportStats|.
hbosdf6075a2016-12-19 04:58:02 -0800127 void ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700128 int64_t timestamp_us, const SessionStats& session_stats,
129 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
130 RTCStatsReport* report) const;
131
132 // Helper function to stats-producing functions.
133 std::map<std::string, CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -0800134 PrepareTransportCertificateStats_n(const SessionStats& session_stats) const;
135 std::unique_ptr<MediaInfo> PrepareMediaInfo_s() const;
hbosd565b732016-08-30 14:04:35 -0700136
hbos82ebe022016-11-14 01:41:09 -0800137 // Slots for signals (sigslot) that are wired up to |pc_|.
138 void OnDataChannelCreated(DataChannel* channel);
139 // Slots for signals (sigslot) that are wired up to |channel|.
140 void OnDataChannelOpened(DataChannel* channel);
141 void OnDataChannelClosed(DataChannel* channel);
142
hbosd565b732016-08-30 14:04:35 -0700143 PeerConnection* const pc_;
hbosc82f2e12016-09-05 01:36:50 -0700144 rtc::Thread* const signaling_thread_;
145 rtc::Thread* const worker_thread_;
146 rtc::Thread* const network_thread_;
147 rtc::AsyncInvoker invoker_;
148
149 int num_pending_partial_reports_;
150 int64_t partial_report_timestamp_us_;
151 rtc::scoped_refptr<RTCStatsReport> partial_report_;
152 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_;
153
hbosdf6075a2016-12-19 04:58:02 -0800154 // Set in |GetStatsReport|, used in |ProducePartialResultsOnNetworkThread|
155 // (not passed as arguments to avoid copies). This is thread safe - it is set
156 // at the start of |GetStatsReport| after making sure there are no pending
157 // stats requests in progress.
158 std::unique_ptr<ChannelNamePairs> channel_name_pairs_;
159 std::unique_ptr<MediaInfo> media_info_;
160
hbos0e6758d2016-08-31 07:57:36 -0700161 // A timestamp, in microseconds, that is based on a timer that is
162 // monotonically increasing. That is, even if the system clock is modified the
163 // difference between the timer and this timestamp is how fresh the cached
164 // report is.
165 int64_t cache_timestamp_us_;
166 int64_t cache_lifetime_us_;
hbosd565b732016-08-30 14:04:35 -0700167 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
hbos82ebe022016-11-14 01:41:09 -0800168
169 // Data recorded and maintained by the stats collector during its lifetime.
170 // Some stats are produced from this record instead of other components.
171 struct InternalRecord {
172 InternalRecord() : data_channels_opened(0),
173 data_channels_closed(0) {}
174
175 // The opened count goes up when a channel is fully opened and the closed
176 // count goes up if a previously opened channel has fully closed. The opened
177 // count does not go down when a channel closes, meaning (opened - closed)
178 // is the number of channels currently opened. A channel that is closed
179 // before reaching the open state does not affect these counters.
180 uint32_t data_channels_opened;
181 uint32_t data_channels_closed;
182 // Identifies by address channels that have been opened, which remain in the
183 // set until they have been fully closed.
184 std::set<uintptr_t> opened_data_channels;
185 };
186 InternalRecord internal_record_;
hbosd565b732016-08-30 14:04:35 -0700187};
188
hboscc555c52016-10-18 12:48:31 -0700189const char* CandidateTypeToRTCIceCandidateTypeForTesting(
190 const std::string& type);
191const char* DataStateToRTCDataChannelStateForTesting(
192 DataChannelInterface::DataState state);
hbosab9f6e42016-10-07 02:18:47 -0700193
hbosd565b732016-08-30 14:04:35 -0700194} // namespace webrtc
195
hbos74e1a4f2016-09-15 23:33:01 -0700196#endif // WEBRTC_API_RTCSTATSCOLLECTOR_H_