2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
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.
11 // This file contains a class used for gathering statistics from an ongoing
12 // libjingle PeerConnection.
14 #ifndef PC_LEGACY_STATS_COLLECTOR_H_
15 #define PC_LEGACY_STATS_COLLECTOR_H_
24 #include <type_traits>
28 #include "absl/types/optional.h"
29 #include "api/field_trials_view.h"
30 #include "api/legacy_stats_types.h"
31 #include "api/media_stream_interface.h"
32 #include "api/peer_connection_interface.h"
33 #include "api/scoped_refptr.h"
34 #include "p2p/base/connection_info.h"
35 #include "p2p/base/port.h"
36 #include "pc/legacy_stats_collector_interface.h"
37 #include "pc/peer_connection_internal.h"
38 #include "pc/rtp_transceiver.h"
39 #include "pc/transport_stats.h"
40 #include "rtc_base/network_constants.h"
41 #include "rtc_base/ssl_certificate.h"
42 #include "rtc_base/thread_annotations.h"
46 // Conversion function to convert candidate type string to the corresponding one
47 // from enum RTCStatsIceCandidateType.
48 const char* IceCandidateTypeToStatsType(const std::string
& candidate_type
);
50 // Conversion function to convert adapter type to report string which are more
51 // fitting to the general style of http://w3c.github.io/webrtc-stats. This is
52 // only used by stats collector.
53 const char* AdapterTypeToStatsType(rtc::AdapterType type
);
55 // A mapping between track ids and their StatsReport.
56 typedef std::map
<std::string
, StatsReport
*> TrackIdMap
;
58 class LegacyStatsCollector
: public LegacyStatsCollectorInterface
{
60 // The caller is responsible for ensuring that the pc outlives the
61 // LegacyStatsCollector instance.
62 explicit LegacyStatsCollector(PeerConnectionInternal
* pc
);
63 virtual ~LegacyStatsCollector();
65 // Adds a MediaStream with tracks that can be used as a `selector` in a call
67 void AddStream(MediaStreamInterface
* stream
);
68 void AddTrack(MediaStreamTrackInterface
* track
);
70 // Adds a local audio track that is used for getting some voice statistics.
71 void AddLocalAudioTrack(AudioTrackInterface
* audio_track
,
72 uint32_t ssrc
) override
;
74 // Removes a local audio tracks that is used for getting some voice
76 void RemoveLocalAudioTrack(AudioTrackInterface
* audio_track
,
77 uint32_t ssrc
) override
;
79 // Gather statistics from the session and store them for future use.
80 void UpdateStats(PeerConnectionInterface::StatsOutputLevel level
);
82 // Gets a StatsReports of the last collected stats. Note that UpdateStats must
83 // be called before this function to get the most recent stats. `selector` is
84 // a track label or empty string. The most recent reports are stored in
86 // TODO(tommi): Change this contract to accept a callback object instead
87 // of filling in `reports`. As is, there's a requirement that the caller
88 // uses `reports` immediately without allowing any async activity on
89 // the thread (message handling etc) and then discard the results.
90 void GetStats(MediaStreamTrackInterface
* track
,
91 StatsReports
* reports
) override
;
93 // Prepare a local or remote SSRC report for the given ssrc. Used internally
94 // in the ExtractStatsFromList template.
95 StatsReport
* PrepareReport(bool local
,
97 const std::string
& track_id
,
98 const StatsReport::Id
& transport_id
,
99 StatsReport::Direction direction
);
101 StatsReport
* PrepareADMReport();
103 // A track is invalid if there is no report data for it.
104 bool IsValidTrack(const std::string
& track_id
);
106 // Reset the internal cache timestamp to force an update of the stats next
107 // time UpdateStats() is called. This call needs to be made on the signaling
108 // thread and should be made every time configuration changes that affect
109 // stats have been made.
110 void InvalidateCache();
112 bool UseStandardBytesStats() const { return use_standard_bytes_stats_
; }
115 friend class LegacyStatsCollectorTest
;
117 // Struct that's populated on the network thread and carries the values to
118 // the signaling thread where the stats are added to the stats reports.
119 struct TransportStats
{
120 TransportStats() = default;
121 TransportStats(std::string transport_name
,
122 cricket::TransportStats transport_stats
)
123 : name(std::move(transport_name
)), stats(std::move(transport_stats
)) {}
124 TransportStats(TransportStats
&&) = default;
125 TransportStats(const TransportStats
&) = delete;
128 cricket::TransportStats stats
;
129 std::unique_ptr
<rtc::SSLCertificateStats
> local_cert_stats
;
130 std::unique_ptr
<rtc::SSLCertificateStats
> remote_cert_stats
;
133 struct SessionStats
{
134 SessionStats() = default;
135 SessionStats(SessionStats
&&) = default;
136 SessionStats(const SessionStats
&) = delete;
138 SessionStats
& operator=(SessionStats
&&) = default;
139 SessionStats
& operator=(SessionStats
&) = delete;
141 cricket::CandidateStatsList candidate_stats
;
142 std::vector
<TransportStats
> transport_stats
;
143 std::map
<std::string
, std::string
> transport_names_by_mid
;
146 // Overridden in unit tests to fake timing.
147 virtual double GetTimeNow();
149 bool CopySelectedReports(const std::string
& selector
, StatsReports
* reports
);
151 // Helper method for creating IceCandidate report. `is_local` indicates
152 // whether this candidate is local or remote.
153 StatsReport
* AddCandidateReport(
154 const cricket::CandidateStats
& candidate_stats
,
157 // Adds a report for this certificate and every certificate in its chain, and
158 // returns the leaf certificate's report (`cert_stats`'s report).
159 StatsReport
* AddCertificateReports(
160 std::unique_ptr
<rtc::SSLCertificateStats
> cert_stats
);
162 StatsReport
* AddConnectionInfoReport(const std::string
& content_name
,
165 const StatsReport::Id
& channel_report_id
,
166 const cricket::ConnectionInfo
& info
);
168 void ExtractDataInfo_n(StatsCollection
* reports
);
170 // Returns the `transport_names_by_mid` member from the SessionStats as
171 // gathered and used to populate the stats. Contains one synchronous hop
172 // to the network thread to get this information along with querying data
173 // channel stats at the same time and populating `reports_`.
174 std::map
<std::string
, std::string
> ExtractSessionAndDataInfo();
176 void ExtractBweInfo();
177 void ExtractMediaInfo(
178 const std::map
<std::string
, std::string
>& transport_names_by_mid
);
179 void ExtractSenderInfo();
180 webrtc::StatsReport
* GetReport(const StatsReport::StatsType
& type
,
181 const std::string
& id
,
182 StatsReport::Direction direction
);
184 // Helper method to get stats from the local audio tracks.
185 void UpdateStatsFromExistingLocalAudioTracks(bool has_remote_tracks
);
186 void UpdateReportFromAudioTrack(AudioTrackInterface
* track
,
188 bool has_remote_tracks
);
190 // Helper method to update the timestamp of track records.
191 void UpdateTrackReports();
193 SessionStats
ExtractSessionInfo_n(
194 const std::vector
<rtc::scoped_refptr
<
195 RtpTransceiverProxyWithInternal
<RtpTransceiver
>>>& transceivers
,
196 absl::optional
<std::string
> sctp_transport_name
,
197 absl::optional
<std::string
> sctp_mid
);
198 void ExtractSessionInfo_s(SessionStats
& session_stats
);
200 // A collection for all of our stats reports.
201 StatsCollection reports_
;
202 TrackIdMap track_ids_
;
203 // Raw pointer to the peer connection the statistics are gathered from.
204 PeerConnectionInternal
* const pc_
;
205 int64_t cache_timestamp_ms_
RTC_GUARDED_BY(pc_
->signaling_thread()) = 0;
206 double stats_gathering_started_
;
207 const bool use_standard_bytes_stats_
;
209 // TODO(tommi): We appear to be holding on to raw pointers to reference
210 // counted objects? We should be using scoped_refptr here.
211 typedef std::vector
<std::pair
<AudioTrackInterface
*, uint32_t>>
212 LocalAudioTrackVector
;
213 LocalAudioTrackVector local_audio_tracks_
;
216 } // namespace webrtc
218 #endif // PC_LEGACY_STATS_COLLECTOR_H_