blob: 9dce0fe79e668e8150b7fb770536b0a83368c4bd [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"
stefanf79ade12017-06-02 06:44:03 -070029#include "webrtc/call/call.h"
hbos0adb8282016-11-23 02:32:06 -080030#include "webrtc/media/base/mediachannel.h"
ossu7bb87ee2017-01-23 04:56:25 -080031#include "webrtc/pc/datachannel.h"
deadbeef804c1af2017-02-11 19:07:31 -080032#include "webrtc/pc/trackmediainfomap.h"
hbosd565b732016-08-30 14:04:35 -070033
hbosab9f6e42016-10-07 02:18:47 -070034namespace cricket {
35class Candidate;
36} // namespace cricket
37
hbos6ab97ce2016-10-03 14:16:56 -070038namespace rtc {
hbos6ab97ce2016-10-03 14:16:56 -070039class SSLCertificate;
hbos6ab97ce2016-10-03 14:16:56 -070040} // namespace rtc
41
hbosd565b732016-08-30 14:04:35 -070042namespace webrtc {
43
44class PeerConnection;
hbos6ab97ce2016-10-03 14:16:56 -070045struct SessionStats;
hbosdf6075a2016-12-19 04:58:02 -080046struct ChannelNamePairs;
hbosd565b732016-08-30 14:04:35 -070047
hbosc82f2e12016-09-05 01:36:50 -070048// All public methods of the collector are to be called on the signaling thread.
49// Stats are gathered on the signaling, worker and network threads
50// asynchronously. The callback is invoked on the signaling thread. Resulting
51// reports are cached for |cache_lifetime_| ms.
hbos82ebe022016-11-14 01:41:09 -080052class RTCStatsCollector : public virtual rtc::RefCountInterface,
53 public sigslot::has_slots<> {
hbosc82f2e12016-09-05 01:36:50 -070054 public:
55 static rtc::scoped_refptr<RTCStatsCollector> Create(
hbosd565b732016-08-30 14:04:35 -070056 PeerConnection* pc,
hbos0e6758d2016-08-31 07:57:36 -070057 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec);
hbosd565b732016-08-30 14:04:35 -070058
59 // Gets a recent stats report. If there is a report cached that is still fresh
60 // it is returned, otherwise new stats are gathered and returned. A report is
61 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe
62 // to use across multiple threads and may be destructed on any thread.
hbosc82f2e12016-09-05 01:36:50 -070063 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
hbosd565b732016-08-30 14:04:35 -070064 // Clears the cache's reference to the most recent stats report. Subsequently
65 // calling |GetStatsReport| guarantees fresh stats.
66 void ClearCachedStatsReport();
67
hbosb78306a2016-12-19 05:06:57 -080068 // If there is a |GetStatsReport| requests in-flight, waits until it has been
69 // completed. Must be called on the signaling thread.
70 void WaitForPendingRequest();
71
hbosc82f2e12016-09-05 01:36:50 -070072 protected:
73 RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us);
hbosb78306a2016-12-19 05:06:57 -080074 ~RTCStatsCollector();
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);
hbosc82f2e12016-09-05 01:36:50 -070079 virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us);
80
81 // Can be called on any thread.
82 void AddPartialResults(
83 const rtc::scoped_refptr<RTCStatsReport>& partial_report);
84
85 private:
hbos2fa7c672016-10-24 04:00:05 -070086 struct CertificateStatsPair {
87 std::unique_ptr<rtc::SSLCertificateStats> local;
88 std::unique_ptr<rtc::SSLCertificateStats> remote;
89 };
90
hbosc82f2e12016-09-05 01:36:50 -070091 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report);
92 void DeliverCachedReport();
93
hbosab9f6e42016-10-07 02:18:47 -070094 // Produces |RTCCertificateStats|.
hbosdf6075a2016-12-19 04:58:02 -080095 void ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -070096 int64_t timestamp_us,
97 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -070098 RTCStatsReport* report) const;
hbos0adb8282016-11-23 02:32:06 -080099 // Produces |RTCCodecStats|.
hbosdf6075a2016-12-19 04:58:02 -0800100 void ProduceCodecStats_n(
hbos84abeb12017-01-16 06:16:44 -0800101 int64_t timestamp_us, const TrackMediaInfoMap& track_media_info_map,
hbos0adb8282016-11-23 02:32:06 -0800102 RTCStatsReport* report) const;
hboscc555c52016-10-18 12:48:31 -0700103 // Produces |RTCDataChannelStats|.
104 void ProduceDataChannelStats_s(
105 int64_t timestamp_us, RTCStatsReport* report) const;
hbosc47a0c32016-10-11 14:54:49 -0700106 // Produces |RTCIceCandidatePairStats| and |RTCIceCandidateStats|.
hbosdf6075a2016-12-19 04:58:02 -0800107 void ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -0700108 int64_t timestamp_us,
109 const SessionStats& session_stats,
hbos338f78a2017-02-07 06:41:21 -0800110 const cricket::VideoMediaInfo* video_media_info,
stefanf79ade12017-06-02 06:44:03 -0700111 const Call::Stats& call_stats,
hbosab9f6e42016-10-07 02:18:47 -0700112 RTCStatsReport* report) const;
hbos09bc1282016-11-08 06:29:22 -0800113 // Produces |RTCMediaStreamStats| and |RTCMediaStreamTrackStats|.
114 void ProduceMediaStreamAndTrackStats_s(
115 int64_t timestamp_us, RTCStatsReport* report) const;
hbosab9f6e42016-10-07 02:18:47 -0700116 // Produces |RTCPeerConnectionStats|.
hbos6ab97ce2016-10-03 14:16:56 -0700117 void ProducePeerConnectionStats_s(
118 int64_t timestamp_us, RTCStatsReport* report) const;
hboseeafe942016-11-01 03:00:17 -0700119 // Produces |RTCInboundRTPStreamStats| and |RTCOutboundRTPStreamStats|.
hbosdf6075a2016-12-19 04:58:02 -0800120 void ProduceRTPStreamStats_n(
hbos6ded1902016-11-01 01:50:46 -0700121 int64_t timestamp_us, const SessionStats& session_stats,
hbos84abeb12017-01-16 06:16:44 -0800122 const TrackMediaInfoMap& track_media_info_map,
123 RTCStatsReport* report) const;
hbos2fa7c672016-10-24 04:00:05 -0700124 // Produces |RTCTransportStats|.
hbosdf6075a2016-12-19 04:58:02 -0800125 void ProduceTransportStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700126 int64_t timestamp_us, const SessionStats& session_stats,
127 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
128 RTCStatsReport* report) const;
129
130 // Helper function to stats-producing functions.
131 std::map<std::string, CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -0800132 PrepareTransportCertificateStats_n(const SessionStats& session_stats) const;
hbos84abeb12017-01-16 06:16:44 -0800133 std::unique_ptr<TrackMediaInfoMap> PrepareTrackMediaInfoMap_s() const;
134 std::map<MediaStreamTrackInterface*, std::string> PrepareTrackToID_s() 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
hbos84abeb12017-01-16 06:16:44 -0800153 // Set in |GetStatsReport|, read in |ProducePartialResultsOnNetworkThread| and
154 // |ProducePartialResultsOnSignalingThread|, reset after work is complete. Not
155 // passed as arguments to avoid copies. This is thread safe - when we
156 // set/reset we know there are no pending stats requests in progress.
hbosdf6075a2016-12-19 04:58:02 -0800157 std::unique_ptr<ChannelNamePairs> channel_name_pairs_;
hbos84abeb12017-01-16 06:16:44 -0800158 std::unique_ptr<TrackMediaInfoMap> track_media_info_map_;
159 std::map<MediaStreamTrackInterface*, std::string> track_to_id_;
stefanf79ade12017-06-02 06:44:03 -0700160 Call::Stats call_stats_;
hbosdf6075a2016-12-19 04:58:02 -0800161
hbos0e6758d2016-08-31 07:57:36 -0700162 // A timestamp, in microseconds, that is based on a timer that is
163 // monotonically increasing. That is, even if the system clock is modified the
164 // difference between the timer and this timestamp is how fresh the cached
165 // report is.
166 int64_t cache_timestamp_us_;
167 int64_t cache_lifetime_us_;
hbosd565b732016-08-30 14:04:35 -0700168 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
hbos82ebe022016-11-14 01:41:09 -0800169
170 // Data recorded and maintained by the stats collector during its lifetime.
171 // Some stats are produced from this record instead of other components.
172 struct InternalRecord {
173 InternalRecord() : data_channels_opened(0),
174 data_channels_closed(0) {}
175
176 // The opened count goes up when a channel is fully opened and the closed
177 // count goes up if a previously opened channel has fully closed. The opened
178 // count does not go down when a channel closes, meaning (opened - closed)
179 // is the number of channels currently opened. A channel that is closed
180 // before reaching the open state does not affect these counters.
181 uint32_t data_channels_opened;
182 uint32_t data_channels_closed;
183 // Identifies by address channels that have been opened, which remain in the
184 // set until they have been fully closed.
185 std::set<uintptr_t> opened_data_channels;
186 };
187 InternalRecord internal_record_;
hbosd565b732016-08-30 14:04:35 -0700188};
189
hboscc555c52016-10-18 12:48:31 -0700190const char* CandidateTypeToRTCIceCandidateTypeForTesting(
191 const std::string& type);
192const char* DataStateToRTCDataChannelStateForTesting(
193 DataChannelInterface::DataState state);
hbosab9f6e42016-10-07 02:18:47 -0700194
hbosd565b732016-08-30 14:04:35 -0700195} // namespace webrtc
196
ossu7bb87ee2017-01-23 04:56:25 -0800197#endif // WEBRTC_PC_RTCSTATSCOLLECTOR_H_