blob: cd3c55ee8971e52476b61d836b4d4a5601287aa5 [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"
hbos84abeb12017-01-16 06:16:44 -080023#include "webrtc/api/trackmediainfomap.h"
hbosc82f2e12016-09-05 01:36:50 -070024#include "webrtc/base/asyncinvoker.h"
hbos0adb8282016-11-23 02:32:06 -080025#include "webrtc/base/optional.h"
hbosc82f2e12016-09-05 01:36:50 -070026#include "webrtc/base/refcount.h"
hbosd565b732016-08-30 14:04:35 -070027#include "webrtc/base/scoped_ref_ptr.h"
hbos82ebe022016-11-14 01:41:09 -080028#include "webrtc/base/sigslot.h"
hbos2fa7c672016-10-24 04:00:05 -070029#include "webrtc/base/sslidentity.h"
hbos0e6758d2016-08-31 07:57:36 -070030#include "webrtc/base/timeutils.h"
hbos0adb8282016-11-23 02:32:06 -080031#include "webrtc/media/base/mediachannel.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 -070047class RTCStatsCollectorCallback : public virtual rtc::RefCountInterface {
hbosd565b732016-08-30 14:04:35 -070048 public:
hbosc82f2e12016-09-05 01:36:50 -070049 virtual ~RTCStatsCollectorCallback() {}
50
51 virtual void OnStatsDelivered(
52 const rtc::scoped_refptr<const RTCStatsReport>& report) = 0;
53};
54
55// All public methods of the collector are to be called on the signaling thread.
56// Stats are gathered on the signaling, worker and network threads
57// asynchronously. The callback is invoked on the signaling thread. Resulting
58// reports are cached for |cache_lifetime_| ms.
hbos82ebe022016-11-14 01:41:09 -080059class RTCStatsCollector : public virtual rtc::RefCountInterface,
60 public sigslot::has_slots<> {
hbosc82f2e12016-09-05 01:36:50 -070061 public:
62 static rtc::scoped_refptr<RTCStatsCollector> Create(
hbosd565b732016-08-30 14:04:35 -070063 PeerConnection* pc,
hbos0e6758d2016-08-31 07:57:36 -070064 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec);
hbosd565b732016-08-30 14:04:35 -070065
66 // Gets a recent stats report. If there is a report cached that is still fresh
67 // it is returned, otherwise new stats are gathered and returned. A report is
68 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe
69 // to use across multiple threads and may be destructed on any thread.
hbosc82f2e12016-09-05 01:36:50 -070070 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
hbosd565b732016-08-30 14:04:35 -070071 // Clears the cache's reference to the most recent stats report. Subsequently
72 // calling |GetStatsReport| guarantees fresh stats.
73 void ClearCachedStatsReport();
74
hbosb78306a2016-12-19 05:06:57 -080075 // If there is a |GetStatsReport| requests in-flight, waits until it has been
76 // completed. Must be called on the signaling thread.
77 void WaitForPendingRequest();
78
hbosc82f2e12016-09-05 01:36:50 -070079 protected:
80 RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us);
hbosb78306a2016-12-19 05:06:57 -080081 ~RTCStatsCollector();
hbosd565b732016-08-30 14:04:35 -070082
hbosc82f2e12016-09-05 01:36:50 -070083 // Stats gathering on a particular thread. Calls |AddPartialResults| before
84 // returning. Virtual for the sake of testing.
85 virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -070086 virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us);
87
88 // Can be called on any thread.
89 void AddPartialResults(
90 const rtc::scoped_refptr<RTCStatsReport>& partial_report);
91
92 private:
hbos2fa7c672016-10-24 04:00:05 -070093 struct CertificateStatsPair {
94 std::unique_ptr<rtc::SSLCertificateStats> local;
95 std::unique_ptr<rtc::SSLCertificateStats> remote;
96 };
97
hbosc82f2e12016-09-05 01:36:50 -070098 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report);
99 void DeliverCachedReport();
100
hbosab9f6e42016-10-07 02:18:47 -0700101 // Produces |RTCCertificateStats|.
hbosdf6075a2016-12-19 04:58:02 -0800102 void ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700103 int64_t timestamp_us,
104 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700105 RTCStatsReport* report) const;
hbos0adb8282016-11-23 02:32:06 -0800106 // Produces |RTCCodecStats|.
hbosdf6075a2016-12-19 04:58:02 -0800107 void ProduceCodecStats_n(
hbos84abeb12017-01-16 06:16:44 -0800108 int64_t timestamp_us, const TrackMediaInfoMap& track_media_info_map,
hbos0adb8282016-11-23 02:32:06 -0800109 RTCStatsReport* report) const;
hboscc555c52016-10-18 12:48:31 -0700110 // Produces |RTCDataChannelStats|.
111 void ProduceDataChannelStats_s(
112 int64_t timestamp_us, RTCStatsReport* report) const;
hbosc47a0c32016-10-11 14:54:49 -0700113 // Produces |RTCIceCandidatePairStats| and |RTCIceCandidateStats|.
hbosdf6075a2016-12-19 04:58:02 -0800114 void ProduceIceCandidateAndPairStats_n(
hbosab9f6e42016-10-07 02:18:47 -0700115 int64_t timestamp_us, const SessionStats& session_stats,
116 RTCStatsReport* report) const;
hbos09bc1282016-11-08 06:29:22 -0800117 // Produces |RTCMediaStreamStats| and |RTCMediaStreamTrackStats|.
118 void ProduceMediaStreamAndTrackStats_s(
119 int64_t timestamp_us, RTCStatsReport* report) const;
hbosab9f6e42016-10-07 02:18:47 -0700120 // Produces |RTCPeerConnectionStats|.
hbos6ab97ce2016-10-03 14:16:56 -0700121 void ProducePeerConnectionStats_s(
122 int64_t timestamp_us, RTCStatsReport* report) const;
hboseeafe942016-11-01 03:00:17 -0700123 // Produces |RTCInboundRTPStreamStats| and |RTCOutboundRTPStreamStats|.
hbosdf6075a2016-12-19 04:58:02 -0800124 void ProduceRTPStreamStats_n(
hbos6ded1902016-11-01 01:50:46 -0700125 int64_t timestamp_us, const SessionStats& session_stats,
hbos84abeb12017-01-16 06:16:44 -0800126 const TrackMediaInfoMap& track_media_info_map,
127 RTCStatsReport* report) const;
hbos2fa7c672016-10-24 04:00:05 -0700128 // Produces |RTCTransportStats|.
hbosdf6075a2016-12-19 04:58:02 -0800129 void ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700130 int64_t timestamp_us, const SessionStats& session_stats,
131 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
132 RTCStatsReport* report) const;
133
134 // Helper function to stats-producing functions.
135 std::map<std::string, CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -0800136 PrepareTransportCertificateStats_n(const SessionStats& session_stats) const;
hbos84abeb12017-01-16 06:16:44 -0800137 std::unique_ptr<TrackMediaInfoMap> PrepareTrackMediaInfoMap_s() const;
138 std::map<MediaStreamTrackInterface*, std::string> PrepareTrackToID_s() const;
hbosd565b732016-08-30 14:04:35 -0700139
hbos82ebe022016-11-14 01:41:09 -0800140 // Slots for signals (sigslot) that are wired up to |pc_|.
141 void OnDataChannelCreated(DataChannel* channel);
142 // Slots for signals (sigslot) that are wired up to |channel|.
143 void OnDataChannelOpened(DataChannel* channel);
144 void OnDataChannelClosed(DataChannel* channel);
145
hbosd565b732016-08-30 14:04:35 -0700146 PeerConnection* const pc_;
hbosc82f2e12016-09-05 01:36:50 -0700147 rtc::Thread* const signaling_thread_;
148 rtc::Thread* const worker_thread_;
149 rtc::Thread* const network_thread_;
150 rtc::AsyncInvoker invoker_;
151
152 int num_pending_partial_reports_;
153 int64_t partial_report_timestamp_us_;
154 rtc::scoped_refptr<RTCStatsReport> partial_report_;
155 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_;
156
hbos84abeb12017-01-16 06:16:44 -0800157 // Set in |GetStatsReport|, read in |ProducePartialResultsOnNetworkThread| and
158 // |ProducePartialResultsOnSignalingThread|, reset after work is complete. Not
159 // passed as arguments to avoid copies. This is thread safe - when we
160 // set/reset we know there are no pending stats requests in progress.
hbosdf6075a2016-12-19 04:58:02 -0800161 std::unique_ptr<ChannelNamePairs> channel_name_pairs_;
hbos84abeb12017-01-16 06:16:44 -0800162 std::unique_ptr<TrackMediaInfoMap> track_media_info_map_;
163 std::map<MediaStreamTrackInterface*, std::string> track_to_id_;
hbosdf6075a2016-12-19 04:58:02 -0800164
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_