blob: ae04e61e56cd7256411f00230fd624941a7e63de [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
14#include <memory>
hbosc82f2e12016-09-05 01:36:50 -070015#include <vector>
hbosd565b732016-08-30 14:04:35 -070016
hboscc555c52016-10-18 12:48:31 -070017#include "webrtc/api/datachannelinterface.h"
hbos74e1a4f2016-09-15 23:33:01 -070018#include "webrtc/api/stats/rtcstats_objects.h"
19#include "webrtc/api/stats/rtcstatsreport.h"
hbosc82f2e12016-09-05 01:36:50 -070020#include "webrtc/base/asyncinvoker.h"
21#include "webrtc/base/refcount.h"
hbosd565b732016-08-30 14:04:35 -070022#include "webrtc/base/scoped_ref_ptr.h"
hbos0e6758d2016-08-31 07:57:36 -070023#include "webrtc/base/timeutils.h"
hbosd565b732016-08-30 14:04:35 -070024
hbosab9f6e42016-10-07 02:18:47 -070025namespace cricket {
26class Candidate;
27} // namespace cricket
28
hbos6ab97ce2016-10-03 14:16:56 -070029namespace rtc {
hbos6ab97ce2016-10-03 14:16:56 -070030class SSLCertificate;
hbos6ab97ce2016-10-03 14:16:56 -070031} // namespace rtc
32
hbosd565b732016-08-30 14:04:35 -070033namespace webrtc {
34
35class PeerConnection;
hbos6ab97ce2016-10-03 14:16:56 -070036struct SessionStats;
hbosd565b732016-08-30 14:04:35 -070037
hbosc82f2e12016-09-05 01:36:50 -070038class RTCStatsCollectorCallback : public virtual rtc::RefCountInterface {
hbosd565b732016-08-30 14:04:35 -070039 public:
hbosc82f2e12016-09-05 01:36:50 -070040 virtual ~RTCStatsCollectorCallback() {}
41
42 virtual void OnStatsDelivered(
43 const rtc::scoped_refptr<const RTCStatsReport>& report) = 0;
44};
45
46// All public methods of the collector are to be called on the signaling thread.
47// Stats are gathered on the signaling, worker and network threads
48// asynchronously. The callback is invoked on the signaling thread. Resulting
49// reports are cached for |cache_lifetime_| ms.
50class RTCStatsCollector : public virtual rtc::RefCountInterface {
51 public:
52 static rtc::scoped_refptr<RTCStatsCollector> Create(
hbosd565b732016-08-30 14:04:35 -070053 PeerConnection* pc,
hbos0e6758d2016-08-31 07:57:36 -070054 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec);
hbosd565b732016-08-30 14:04:35 -070055
56 // Gets a recent stats report. If there is a report cached that is still fresh
57 // it is returned, otherwise new stats are gathered and returned. A report is
58 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe
59 // to use across multiple threads and may be destructed on any thread.
hbosc82f2e12016-09-05 01:36:50 -070060 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
hbosd565b732016-08-30 14:04:35 -070061 // Clears the cache's reference to the most recent stats report. Subsequently
62 // calling |GetStatsReport| guarantees fresh stats.
63 void ClearCachedStatsReport();
64
hbosc82f2e12016-09-05 01:36:50 -070065 protected:
66 RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us);
hbosd565b732016-08-30 14:04:35 -070067
hbosc82f2e12016-09-05 01:36:50 -070068 // Stats gathering on a particular thread. Calls |AddPartialResults| before
69 // returning. Virtual for the sake of testing.
70 virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us);
71 virtual void ProducePartialResultsOnWorkerThread(int64_t timestamp_us);
72 virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us);
73
74 // Can be called on any thread.
75 void AddPartialResults(
76 const rtc::scoped_refptr<RTCStatsReport>& partial_report);
77
78 private:
79 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report);
80 void DeliverCachedReport();
81
hbosab9f6e42016-10-07 02:18:47 -070082 // Produces |RTCCertificateStats|.
hbos6ab97ce2016-10-03 14:16:56 -070083 void ProduceCertificateStats_s(
84 int64_t timestamp_us, const SessionStats& session_stats,
85 RTCStatsReport* report) const;
86 void ProduceCertificateStatsFromSSLCertificateAndChain_s(
87 int64_t timestamp_us, const rtc::SSLCertificate& certificate,
88 RTCStatsReport* report) const;
hboscc555c52016-10-18 12:48:31 -070089 // Produces |RTCDataChannelStats|.
90 void ProduceDataChannelStats_s(
91 int64_t timestamp_us, RTCStatsReport* report) const;
hbosc47a0c32016-10-11 14:54:49 -070092 // Produces |RTCIceCandidatePairStats| and |RTCIceCandidateStats|.
hbosab9f6e42016-10-07 02:18:47 -070093 void ProduceIceCandidateAndPairStats_s(
94 int64_t timestamp_us, const SessionStats& session_stats,
95 RTCStatsReport* report) const;
96 const std::string& ProduceIceCandidateStats_s(
97 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
98 RTCStatsReport* report) const;
99 // Produces |RTCPeerConnectionStats|.
hbos6ab97ce2016-10-03 14:16:56 -0700100 void ProducePeerConnectionStats_s(
101 int64_t timestamp_us, RTCStatsReport* report) const;
hbosd565b732016-08-30 14:04:35 -0700102
103 PeerConnection* const pc_;
hbosc82f2e12016-09-05 01:36:50 -0700104 rtc::Thread* const signaling_thread_;
105 rtc::Thread* const worker_thread_;
106 rtc::Thread* const network_thread_;
107 rtc::AsyncInvoker invoker_;
108
109 int num_pending_partial_reports_;
110 int64_t partial_report_timestamp_us_;
111 rtc::scoped_refptr<RTCStatsReport> partial_report_;
112 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_;
113
hbos0e6758d2016-08-31 07:57:36 -0700114 // A timestamp, in microseconds, that is based on a timer that is
115 // monotonically increasing. That is, even if the system clock is modified the
116 // difference between the timer and this timestamp is how fresh the cached
117 // report is.
118 int64_t cache_timestamp_us_;
119 int64_t cache_lifetime_us_;
hbosd565b732016-08-30 14:04:35 -0700120 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
121};
122
hboscc555c52016-10-18 12:48:31 -0700123const char* CandidateTypeToRTCIceCandidateTypeForTesting(
124 const std::string& type);
125const char* DataStateToRTCDataChannelStateForTesting(
126 DataChannelInterface::DataState state);
hbosab9f6e42016-10-07 02:18:47 -0700127
hbosd565b732016-08-30 14:04:35 -0700128} // namespace webrtc
129
hbos74e1a4f2016-09-15 23:33:01 -0700130#endif // WEBRTC_API_RTCSTATSCOLLECTOR_H_