2016-08-30 14:04:35 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright 2016 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-09-15 23:33:01 -07:00
|
|
|
#ifndef WEBRTC_API_RTCSTATSCOLLECTOR_H_
|
|
|
|
|
#define WEBRTC_API_RTCSTATSCOLLECTOR_H_
|
2016-08-30 14:04:35 -07:00
|
|
|
|
2016-10-24 04:00:05 -07:00
|
|
|
#include <map>
|
2016-08-30 14:04:35 -07:00
|
|
|
#include <memory>
|
2016-11-14 01:41:09 -08:00
|
|
|
#include <set>
|
2016-09-05 01:36:50 -07:00
|
|
|
#include <vector>
|
2016-08-30 14:04:35 -07:00
|
|
|
|
2016-11-14 01:41:09 -08:00
|
|
|
#include "webrtc/api/datachannel.h"
|
2016-10-18 12:48:31 -07:00
|
|
|
#include "webrtc/api/datachannelinterface.h"
|
2016-09-15 23:33:01 -07:00
|
|
|
#include "webrtc/api/stats/rtcstats_objects.h"
|
|
|
|
|
#include "webrtc/api/stats/rtcstatsreport.h"
|
2016-09-05 01:36:50 -07:00
|
|
|
#include "webrtc/base/asyncinvoker.h"
|
|
|
|
|
#include "webrtc/base/refcount.h"
|
2016-08-30 14:04:35 -07:00
|
|
|
#include "webrtc/base/scoped_ref_ptr.h"
|
2016-11-14 01:41:09 -08:00
|
|
|
#include "webrtc/base/sigslot.h"
|
2016-10-24 04:00:05 -07:00
|
|
|
#include "webrtc/base/sslidentity.h"
|
2016-08-31 07:57:36 -07:00
|
|
|
#include "webrtc/base/timeutils.h"
|
2016-08-30 14:04:35 -07:00
|
|
|
|
2016-10-07 02:18:47 -07:00
|
|
|
namespace cricket {
|
|
|
|
|
class Candidate;
|
|
|
|
|
} // namespace cricket
|
2016-10-03 14:16:56 -07:00
|
|
|
|
2016-10-07 02:18:47 -07:00
|
|
|
namespace rtc {
|
2016-10-03 14:16:56 -07:00
|
|
|
class SSLCertificate;
|
|
|
|
|
} // namespace rtc
|
|
|
|
|
|
2016-08-30 14:04:35 -07:00
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
class PeerConnection;
|
2016-10-03 14:16:56 -07:00
|
|
|
struct SessionStats;
|
2016-08-30 14:04:35 -07:00
|
|
|
|
2016-09-05 01:36:50 -07:00
|
|
|
class RTCStatsCollectorCallback : public virtual rtc::RefCountInterface {
|
2016-08-30 14:04:35 -07:00
|
|
|
public:
|
2016-09-05 01:36:50 -07:00
|
|
|
virtual ~RTCStatsCollectorCallback() {}
|
|
|
|
|
|
|
|
|
|
virtual void OnStatsDelivered(
|
|
|
|
|
const rtc::scoped_refptr<const RTCStatsReport>& report) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// All public methods of the collector are to be called on the signaling thread.
|
|
|
|
|
// Stats are gathered on the signaling, worker and network threads
|
|
|
|
|
// asynchronously. The callback is invoked on the signaling thread. Resulting
|
|
|
|
|
// reports are cached for |cache_lifetime_| ms.
|
2016-11-14 01:41:09 -08:00
|
|
|
class RTCStatsCollector : public virtual rtc::RefCountInterface,
|
|
|
|
|
public sigslot::has_slots<> {
|
2016-09-05 01:36:50 -07:00
|
|
|
public:
|
|
|
|
|
static rtc::scoped_refptr<RTCStatsCollector> Create(
|
2016-08-30 14:04:35 -07:00
|
|
|
PeerConnection* pc,
|
2016-08-31 07:57:36 -07:00
|
|
|
int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec);
|
2016-08-30 14:04:35 -07:00
|
|
|
|
|
|
|
|
// Gets a recent stats report. If there is a report cached that is still fresh
|
|
|
|
|
// it is returned, otherwise new stats are gathered and returned. A report is
|
|
|
|
|
// considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe
|
|
|
|
|
// to use across multiple threads and may be destructed on any thread.
|
2016-09-05 01:36:50 -07:00
|
|
|
void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback);
|
2016-08-30 14:04:35 -07:00
|
|
|
// Clears the cache's reference to the most recent stats report. Subsequently
|
|
|
|
|
// calling |GetStatsReport| guarantees fresh stats.
|
|
|
|
|
void ClearCachedStatsReport();
|
|
|
|
|
|
2016-09-05 01:36:50 -07:00
|
|
|
protected:
|
|
|
|
|
RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us);
|
|
|
|
|
|
|
|
|
|
// Stats gathering on a particular thread. Calls |AddPartialResults| before
|
|
|
|
|
// returning. Virtual for the sake of testing.
|
|
|
|
|
virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us);
|
|
|
|
|
virtual void ProducePartialResultsOnWorkerThread(int64_t timestamp_us);
|
|
|
|
|
virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us);
|
|
|
|
|
|
|
|
|
|
// Can be called on any thread.
|
|
|
|
|
void AddPartialResults(
|
|
|
|
|
const rtc::scoped_refptr<RTCStatsReport>& partial_report);
|
|
|
|
|
|
2016-08-30 14:04:35 -07:00
|
|
|
private:
|
2016-10-24 04:00:05 -07:00
|
|
|
struct CertificateStatsPair {
|
|
|
|
|
std::unique_ptr<rtc::SSLCertificateStats> local;
|
|
|
|
|
std::unique_ptr<rtc::SSLCertificateStats> remote;
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-05 01:36:50 -07:00
|
|
|
void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report);
|
|
|
|
|
void DeliverCachedReport();
|
2016-08-30 14:04:35 -07:00
|
|
|
|
2016-10-07 02:18:47 -07:00
|
|
|
// Produces |RTCCertificateStats|.
|
2016-10-03 14:16:56 -07:00
|
|
|
void ProduceCertificateStats_s(
|
2016-10-24 04:00:05 -07:00
|
|
|
int64_t timestamp_us,
|
|
|
|
|
const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
|
2016-10-03 14:16:56 -07:00
|
|
|
RTCStatsReport* report) const;
|
2016-10-18 12:48:31 -07:00
|
|
|
// Produces |RTCDataChannelStats|.
|
|
|
|
|
void ProduceDataChannelStats_s(
|
|
|
|
|
int64_t timestamp_us, RTCStatsReport* report) const;
|
2016-10-11 14:54:49 -07:00
|
|
|
// Produces |RTCIceCandidatePairStats| and |RTCIceCandidateStats|.
|
2016-10-07 02:18:47 -07:00
|
|
|
void ProduceIceCandidateAndPairStats_s(
|
|
|
|
|
int64_t timestamp_us, const SessionStats& session_stats,
|
|
|
|
|
RTCStatsReport* report) const;
|
2016-11-08 06:29:22 -08:00
|
|
|
// Produces |RTCMediaStreamStats| and |RTCMediaStreamTrackStats|.
|
|
|
|
|
void ProduceMediaStreamAndTrackStats_s(
|
|
|
|
|
int64_t timestamp_us, RTCStatsReport* report) const;
|
2016-10-07 02:18:47 -07:00
|
|
|
// Produces |RTCPeerConnectionStats|.
|
2016-10-03 14:16:56 -07:00
|
|
|
void ProducePeerConnectionStats_s(
|
|
|
|
|
int64_t timestamp_us, RTCStatsReport* report) const;
|
2016-11-01 03:00:17 -07:00
|
|
|
// Produces |RTCInboundRTPStreamStats| and |RTCOutboundRTPStreamStats|.
|
2016-11-01 01:50:46 -07:00
|
|
|
void ProduceRTPStreamStats_s(
|
|
|
|
|
int64_t timestamp_us, const SessionStats& session_stats,
|
|
|
|
|
RTCStatsReport* report) const;
|
2016-10-24 04:00:05 -07:00
|
|
|
// Produces |RTCTransportStats|.
|
|
|
|
|
void ProduceTransportStats_s(
|
|
|
|
|
int64_t timestamp_us, const SessionStats& session_stats,
|
|
|
|
|
const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
|
|
|
|
|
RTCStatsReport* report) const;
|
|
|
|
|
|
|
|
|
|
// Helper function to stats-producing functions.
|
|
|
|
|
std::map<std::string, CertificateStatsPair>
|
|
|
|
|
PrepareTransportCertificateStats_s(const SessionStats& session_stats) const;
|
2016-08-30 14:04:35 -07:00
|
|
|
|
2016-11-14 01:41:09 -08:00
|
|
|
// Slots for signals (sigslot) that are wired up to |pc_|.
|
|
|
|
|
void OnDataChannelCreated(DataChannel* channel);
|
|
|
|
|
// Slots for signals (sigslot) that are wired up to |channel|.
|
|
|
|
|
void OnDataChannelOpened(DataChannel* channel);
|
|
|
|
|
void OnDataChannelClosed(DataChannel* channel);
|
|
|
|
|
|
2016-08-30 14:04:35 -07:00
|
|
|
PeerConnection* const pc_;
|
2016-09-05 01:36:50 -07:00
|
|
|
rtc::Thread* const signaling_thread_;
|
|
|
|
|
rtc::Thread* const worker_thread_;
|
|
|
|
|
rtc::Thread* const network_thread_;
|
|
|
|
|
rtc::AsyncInvoker invoker_;
|
|
|
|
|
|
|
|
|
|
int num_pending_partial_reports_;
|
|
|
|
|
int64_t partial_report_timestamp_us_;
|
|
|
|
|
rtc::scoped_refptr<RTCStatsReport> partial_report_;
|
|
|
|
|
std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_;
|
|
|
|
|
|
2016-08-31 07:57:36 -07:00
|
|
|
// A timestamp, in microseconds, that is based on a timer that is
|
|
|
|
|
// monotonically increasing. That is, even if the system clock is modified the
|
|
|
|
|
// difference between the timer and this timestamp is how fresh the cached
|
|
|
|
|
// report is.
|
|
|
|
|
int64_t cache_timestamp_us_;
|
|
|
|
|
int64_t cache_lifetime_us_;
|
2016-08-30 14:04:35 -07:00
|
|
|
rtc::scoped_refptr<const RTCStatsReport> cached_report_;
|
2016-11-14 01:41:09 -08:00
|
|
|
|
|
|
|
|
// Data recorded and maintained by the stats collector during its lifetime.
|
|
|
|
|
// Some stats are produced from this record instead of other components.
|
|
|
|
|
struct InternalRecord {
|
|
|
|
|
InternalRecord() : data_channels_opened(0),
|
|
|
|
|
data_channels_closed(0) {}
|
|
|
|
|
|
|
|
|
|
// The opened count goes up when a channel is fully opened and the closed
|
|
|
|
|
// count goes up if a previously opened channel has fully closed. The opened
|
|
|
|
|
// count does not go down when a channel closes, meaning (opened - closed)
|
|
|
|
|
// is the number of channels currently opened. A channel that is closed
|
|
|
|
|
// before reaching the open state does not affect these counters.
|
|
|
|
|
uint32_t data_channels_opened;
|
|
|
|
|
uint32_t data_channels_closed;
|
|
|
|
|
// Identifies by address channels that have been opened, which remain in the
|
|
|
|
|
// set until they have been fully closed.
|
|
|
|
|
std::set<uintptr_t> opened_data_channels;
|
|
|
|
|
};
|
|
|
|
|
InternalRecord internal_record_;
|
2016-08-30 14:04:35 -07:00
|
|
|
};
|
|
|
|
|
|
2016-10-18 12:48:31 -07:00
|
|
|
const char* CandidateTypeToRTCIceCandidateTypeForTesting(
|
|
|
|
|
const std::string& type);
|
|
|
|
|
const char* DataStateToRTCDataChannelStateForTesting(
|
|
|
|
|
DataChannelInterface::DataState state);
|
2016-10-07 02:18:47 -07:00
|
|
|
|
2016-08-30 14:04:35 -07:00
|
|
|
} // namespace webrtc
|
|
|
|
|
|
2016-09-15 23:33:01 -07:00
|
|
|
#endif // WEBRTC_API_RTCSTATSCOLLECTOR_H_
|