blob: 2f5ed8610a6399fc57ea59bd085bcb9beadd08ac [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
hbos74e1a4f2016-09-15 23:33:01 -070017#include "webrtc/api/stats/rtcstats_objects.h"
18#include "webrtc/api/stats/rtcstatsreport.h"
hbosc82f2e12016-09-05 01:36:50 -070019#include "webrtc/base/asyncinvoker.h"
20#include "webrtc/base/refcount.h"
hbosd565b732016-08-30 14:04:35 -070021#include "webrtc/base/scoped_ref_ptr.h"
hbos0e6758d2016-08-31 07:57:36 -070022#include "webrtc/base/timeutils.h"
hbosd565b732016-08-30 14:04:35 -070023
hbos6ab97ce2016-10-03 14:16:56 -070024namespace rtc {
25
26class SSLCertificate;
27
28} // namespace rtc
29
hbosd565b732016-08-30 14:04:35 -070030namespace webrtc {
31
32class PeerConnection;
hbos6ab97ce2016-10-03 14:16:56 -070033struct SessionStats;
hbosd565b732016-08-30 14:04:35 -070034
hbosc82f2e12016-09-05 01:36:50 -070035class RTCStatsCollectorCallback : public virtual rtc::RefCountInterface {
hbosd565b732016-08-30 14:04:35 -070036 public:
hbosc82f2e12016-09-05 01:36:50 -070037 virtual ~RTCStatsCollectorCallback() {}
38
39 virtual void OnStatsDelivered(
40 const rtc::scoped_refptr<const RTCStatsReport>& report) = 0;
41};
42
43// All public methods of the collector are to be called on the signaling thread.
44// Stats are gathered on the signaling, worker and network threads
45// asynchronously. The callback is invoked on the signaling thread. Resulting
46// reports are cached for |cache_lifetime_| ms.
47class RTCStatsCollector : public virtual rtc::RefCountInterface {
48 public:
49 static rtc::scoped_refptr<RTCStatsCollector> Create(
hbosd565b732016-08-30 14:04:35 -070050 PeerConnection* pc,
hbos0e6758d2016-08-31 07:57:36 -070051 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec);
hbosd565b732016-08-30 14:04:35 -070052
53 // Gets a recent stats report. If there is a report cached that is still fresh
54 // it is returned, otherwise new stats are gathered and returned. A report is
55 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe
56 // to use across multiple threads and may be destructed on any thread.
hbosc82f2e12016-09-05 01:36:50 -070057 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
hbosd565b732016-08-30 14:04:35 -070058 // Clears the cache's reference to the most recent stats report. Subsequently
59 // calling |GetStatsReport| guarantees fresh stats.
60 void ClearCachedStatsReport();
61
hbosc82f2e12016-09-05 01:36:50 -070062 protected:
63 RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us);
hbosd565b732016-08-30 14:04:35 -070064
hbosc82f2e12016-09-05 01:36:50 -070065 // Stats gathering on a particular thread. Calls |AddPartialResults| before
66 // returning. Virtual for the sake of testing.
67 virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us);
68 virtual void ProducePartialResultsOnWorkerThread(int64_t timestamp_us);
69 virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us);
70
71 // Can be called on any thread.
72 void AddPartialResults(
73 const rtc::scoped_refptr<RTCStatsReport>& partial_report);
74
75 private:
76 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report);
77 void DeliverCachedReport();
78
hbos6ab97ce2016-10-03 14:16:56 -070079 void ProduceCertificateStats_s(
80 int64_t timestamp_us, const SessionStats& session_stats,
81 RTCStatsReport* report) const;
82 void ProduceCertificateStatsFromSSLCertificateAndChain_s(
83 int64_t timestamp_us, const rtc::SSLCertificate& certificate,
84 RTCStatsReport* report) const;
85 void ProducePeerConnectionStats_s(
86 int64_t timestamp_us, RTCStatsReport* report) const;
hbosd565b732016-08-30 14:04:35 -070087
88 PeerConnection* const pc_;
hbosc82f2e12016-09-05 01:36:50 -070089 rtc::Thread* const signaling_thread_;
90 rtc::Thread* const worker_thread_;
91 rtc::Thread* const network_thread_;
92 rtc::AsyncInvoker invoker_;
93
94 int num_pending_partial_reports_;
95 int64_t partial_report_timestamp_us_;
96 rtc::scoped_refptr<RTCStatsReport> partial_report_;
97 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_;
98
hbos0e6758d2016-08-31 07:57:36 -070099 // A timestamp, in microseconds, that is based on a timer that is
100 // monotonically increasing. That is, even if the system clock is modified the
101 // difference between the timer and this timestamp is how fresh the cached
102 // report is.
103 int64_t cache_timestamp_us_;
104 int64_t cache_lifetime_us_;
hbosd565b732016-08-30 14:04:35 -0700105 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
106};
107
108} // namespace webrtc
109
hbos74e1a4f2016-09-15 23:33:01 -0700110#endif // WEBRTC_API_RTCSTATSCOLLECTOR_H_