blob: 2c160ee414025ccfb0e9e936a1b7c88c3e4d5e04 [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>
hbosc82f2e12016-09-05 01:36:50 -070016#include <vector>
hbosd565b732016-08-30 14:04:35 -070017
hboscc555c52016-10-18 12:48:31 -070018#include "webrtc/api/datachannelinterface.h"
hbos74e1a4f2016-09-15 23:33:01 -070019#include "webrtc/api/stats/rtcstats_objects.h"
20#include "webrtc/api/stats/rtcstatsreport.h"
hbosc82f2e12016-09-05 01:36:50 -070021#include "webrtc/base/asyncinvoker.h"
22#include "webrtc/base/refcount.h"
hbosd565b732016-08-30 14:04:35 -070023#include "webrtc/base/scoped_ref_ptr.h"
hbos2fa7c672016-10-24 04:00:05 -070024#include "webrtc/base/sslidentity.h"
hbos0e6758d2016-08-31 07:57:36 -070025#include "webrtc/base/timeutils.h"
hbosd565b732016-08-30 14:04:35 -070026
hbosab9f6e42016-10-07 02:18:47 -070027namespace cricket {
28class Candidate;
29} // namespace cricket
30
hbos6ab97ce2016-10-03 14:16:56 -070031namespace rtc {
hbos6ab97ce2016-10-03 14:16:56 -070032class SSLCertificate;
hbos6ab97ce2016-10-03 14:16:56 -070033} // namespace rtc
34
hbosd565b732016-08-30 14:04:35 -070035namespace webrtc {
36
37class PeerConnection;
hbos6ab97ce2016-10-03 14:16:56 -070038struct SessionStats;
hbosd565b732016-08-30 14:04:35 -070039
hbosc82f2e12016-09-05 01:36:50 -070040class RTCStatsCollectorCallback : public virtual rtc::RefCountInterface {
hbosd565b732016-08-30 14:04:35 -070041 public:
hbosc82f2e12016-09-05 01:36:50 -070042 virtual ~RTCStatsCollectorCallback() {}
43
44 virtual void OnStatsDelivered(
45 const rtc::scoped_refptr<const RTCStatsReport>& report) = 0;
46};
47
48// 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.
52class RTCStatsCollector : public virtual rtc::RefCountInterface {
53 public:
54 static rtc::scoped_refptr<RTCStatsCollector> Create(
hbosd565b732016-08-30 14:04:35 -070055 PeerConnection* pc,
hbos0e6758d2016-08-31 07:57:36 -070056 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec);
hbosd565b732016-08-30 14:04:35 -070057
58 // Gets a recent stats report. If there is a report cached that is still fresh
59 // it is returned, otherwise new stats are gathered and returned. A report is
60 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe
61 // to use across multiple threads and may be destructed on any thread.
hbosc82f2e12016-09-05 01:36:50 -070062 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
hbosd565b732016-08-30 14:04:35 -070063 // Clears the cache's reference to the most recent stats report. Subsequently
64 // calling |GetStatsReport| guarantees fresh stats.
65 void ClearCachedStatsReport();
66
hbosc82f2e12016-09-05 01:36:50 -070067 protected:
68 RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us);
hbosd565b732016-08-30 14:04:35 -070069
hbosc82f2e12016-09-05 01:36:50 -070070 // Stats gathering on a particular thread. Calls |AddPartialResults| before
71 // returning. Virtual for the sake of testing.
72 virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us);
73 virtual void ProducePartialResultsOnWorkerThread(int64_t timestamp_us);
74 virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us);
75
76 // Can be called on any thread.
77 void AddPartialResults(
78 const rtc::scoped_refptr<RTCStatsReport>& partial_report);
79
80 private:
hbos2fa7c672016-10-24 04:00:05 -070081 struct CertificateStatsPair {
82 std::unique_ptr<rtc::SSLCertificateStats> local;
83 std::unique_ptr<rtc::SSLCertificateStats> remote;
84 };
85
hbosc82f2e12016-09-05 01:36:50 -070086 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report);
87 void DeliverCachedReport();
88
hbosab9f6e42016-10-07 02:18:47 -070089 // Produces |RTCCertificateStats|.
hbos6ab97ce2016-10-03 14:16:56 -070090 void ProduceCertificateStats_s(
hbos2fa7c672016-10-24 04:00:05 -070091 int64_t timestamp_us,
92 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -070093 RTCStatsReport* report) const;
hboscc555c52016-10-18 12:48:31 -070094 // Produces |RTCDataChannelStats|.
95 void ProduceDataChannelStats_s(
96 int64_t timestamp_us, RTCStatsReport* report) const;
hbosc47a0c32016-10-11 14:54:49 -070097 // Produces |RTCIceCandidatePairStats| and |RTCIceCandidateStats|.
hbosab9f6e42016-10-07 02:18:47 -070098 void ProduceIceCandidateAndPairStats_s(
99 int64_t timestamp_us, const SessionStats& session_stats,
100 RTCStatsReport* report) const;
hbosab9f6e42016-10-07 02:18:47 -0700101 // Produces |RTCPeerConnectionStats|.
hbos6ab97ce2016-10-03 14:16:56 -0700102 void ProducePeerConnectionStats_s(
103 int64_t timestamp_us, RTCStatsReport* report) const;
hboseeafe942016-11-01 03:00:17 -0700104 // Produces |RTCInboundRTPStreamStats| and |RTCOutboundRTPStreamStats|.
hbos6ded1902016-11-01 01:50:46 -0700105 void ProduceRTPStreamStats_s(
106 int64_t timestamp_us, const SessionStats& session_stats,
107 RTCStatsReport* report) const;
hbos2fa7c672016-10-24 04:00:05 -0700108 // Produces |RTCTransportStats|.
109 void ProduceTransportStats_s(
110 int64_t timestamp_us, const SessionStats& session_stats,
111 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
112 RTCStatsReport* report) const;
113
114 // Helper function to stats-producing functions.
115 std::map<std::string, CertificateStatsPair>
116 PrepareTransportCertificateStats_s(const SessionStats& session_stats) const;
hbosd565b732016-08-30 14:04:35 -0700117
118 PeerConnection* const pc_;
hbosc82f2e12016-09-05 01:36:50 -0700119 rtc::Thread* const signaling_thread_;
120 rtc::Thread* const worker_thread_;
121 rtc::Thread* const network_thread_;
122 rtc::AsyncInvoker invoker_;
123
124 int num_pending_partial_reports_;
125 int64_t partial_report_timestamp_us_;
126 rtc::scoped_refptr<RTCStatsReport> partial_report_;
127 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_;
128
hbos0e6758d2016-08-31 07:57:36 -0700129 // A timestamp, in microseconds, that is based on a timer that is
130 // monotonically increasing. That is, even if the system clock is modified the
131 // difference between the timer and this timestamp is how fresh the cached
132 // report is.
133 int64_t cache_timestamp_us_;
134 int64_t cache_lifetime_us_;
hbosd565b732016-08-30 14:04:35 -0700135 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
136};
137
hboscc555c52016-10-18 12:48:31 -0700138const char* CandidateTypeToRTCIceCandidateTypeForTesting(
139 const std::string& type);
140const char* DataStateToRTCDataChannelStateForTesting(
141 DataChannelInterface::DataState state);
hbosab9f6e42016-10-07 02:18:47 -0700142
hbosd565b732016-08-30 14:04:35 -0700143} // namespace webrtc
144
hbos74e1a4f2016-09-15 23:33:01 -0700145#endif // WEBRTC_API_RTCSTATSCOLLECTOR_H_