blob: e905b39d486b9127be75250e7d13a592e13c94ad [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:369 */
10
11// This file contains a class used for gathering statistics from an ongoing
12// libjingle PeerConnection.
13
Henrik Boströmf7859892022-07-04 12:36:3714#ifndef PC_LEGACY_STATS_COLLECTOR_H_
15#define PC_LEGACY_STATS_COLLECTOR_H_
henrike@webrtc.org28e20752013-07-10 00:45:3616
Yves Gerey3e707812018-11-28 15:47:4917#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3318
Harald Alvestrand5761e7b2021-01-29 14:45:0819#include <algorithm>
20#include <cstdint>
henrike@webrtc.org28e20752013-07-10 00:45:3621#include <map>
Taylor Brandstetterc3928662018-02-23 21:04:5122#include <memory>
henrike@webrtc.org40b3b682014-03-03 18:30:1123#include <string>
Harald Alvestrandc24a2182022-02-23 13:44:5924#include <type_traits>
Steve Anton36b29d12017-10-30 16:57:4225#include <utility>
henrike@webrtc.org40b3b682014-03-03 18:30:1126#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:3627
Harald Alvestrandc24a2182022-02-23 13:44:5928#include "absl/types/optional.h"
Jonas Orelande62c2f22022-03-29 09:04:4829#include "api/field_trials_view.h"
Henrik Boström3e6931b2022-11-11 09:07:3430#include "api/legacy_stats_types.h"
Steve Anton10542f22019-01-11 17:11:0031#include "api/media_stream_interface.h"
32#include "api/peer_connection_interface.h"
Harald Alvestrandc24a2182022-02-23 13:44:5933#include "api/scoped_refptr.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0834#include "p2p/base/connection_info.h"
Yves Gerey3e707812018-11-28 15:47:4935#include "p2p/base/port.h"
Henrik Boströmf7859892022-07-04 12:36:3736#include "pc/legacy_stats_collector_interface.h"
Steve Anton10542f22019-01-11 17:11:0037#include "pc/peer_connection_internal.h"
Harald Alvestrandc24a2182022-02-23 13:44:5938#include "pc/rtp_transceiver.h"
Harald Alvestrandc24a2182022-02-23 13:44:5939#include "pc/transport_stats.h"
Yves Gerey3e707812018-11-28 15:47:4940#include "rtc_base/network_constants.h"
Steve Anton10542f22019-01-11 17:11:0041#include "rtc_base/ssl_certificate.h"
Harald Alvestrandc24a2182022-02-23 13:44:5942#include "rtc_base/thread_annotations.h"
henrike@webrtc.org28e20752013-07-10 00:45:3643
henrike@webrtc.org28e20752013-07-10 00:45:3644namespace webrtc {
45
guoweis@webrtc.org950c5182014-12-16 23:01:3146// Conversion function to convert candidate type string to the corresponding one
47// from enum RTCStatsIceCandidateType.
48const char* IceCandidateTypeToStatsType(const std::string& candidate_type);
49
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.
53const char* AdapterTypeToStatsType(rtc::AdapterType type);
54
jbauchbe24c942015-06-22 22:06:4355// A mapping between track ids and their StatsReport.
56typedef std::map<std::string, StatsReport*> TrackIdMap;
57
Henrik Boströmf7859892022-07-04 12:36:3758class LegacyStatsCollector : public LegacyStatsCollectorInterface {
henrike@webrtc.org28e20752013-07-10 00:45:3659 public:
deadbeefab9b2d12015-10-14 18:33:1160 // The caller is responsible for ensuring that the pc outlives the
Henrik Boströmf7859892022-07-04 12:36:3761 // LegacyStatsCollector instance.
62 explicit LegacyStatsCollector(PeerConnectionInternal* pc);
63 virtual ~LegacyStatsCollector();
henrike@webrtc.org28e20752013-07-10 00:45:3664
Tomas Gunnarssone1c8a432021-04-08 13:15:2865 // Adds a MediaStream with tracks that can be used as a `selector` in a call
henrike@webrtc.org28e20752013-07-10 00:45:3666 // to GetStats.
67 void AddStream(MediaStreamInterface* stream);
Harald Alvestrand75ceef22018-01-04 14:26:1368 void AddTrack(MediaStreamTrackInterface* track);
henrike@webrtc.org28e20752013-07-10 00:45:3669
henrike@webrtc.org40b3b682014-03-03 18:30:1170 // Adds a local audio track that is used for getting some voice statistics.
Harald Alvestrand445e6b02020-09-29 14:21:4771 void AddLocalAudioTrack(AudioTrackInterface* audio_track,
72 uint32_t ssrc) override;
henrike@webrtc.org40b3b682014-03-03 18:30:1173
74 // Removes a local audio tracks that is used for getting some voice
75 // statistics.
Harald Alvestrand445e6b02020-09-29 14:21:4776 void RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
77 uint32_t ssrc) override;
henrike@webrtc.org40b3b682014-03-03 18:30:1178
henrike@webrtc.org28e20752013-07-10 00:45:3679 // Gather statistics from the session and store them for future use.
wu@webrtc.orgb9a088b2014-02-13 23:18:4980 void UpdateStats(PeerConnectionInterface::StatsOutputLevel level);
henrike@webrtc.org28e20752013-07-10 00:45:3681
82 // Gets a StatsReports of the last collected stats. Note that UpdateStats must
Tomas Gunnarssone1c8a432021-04-08 13:15:2883 // be called before this function to get the most recent stats. `selector` is
henrike@webrtc.org28e20752013-07-10 00:45:3684 // a track label or empty string. The most recent reports are stored in
Tomas Gunnarssone1c8a432021-04-08 13:15:2885 // `reports`.
tommi@webrtc.org5b06b062014-08-15 08:38:3086 // TODO(tommi): Change this contract to accept a callback object instead
Tomas Gunnarssone1c8a432021-04-08 13:15:2887 // of filling in `reports`. As is, there's a requirement that the caller
88 // uses `reports` immediately without allowing any async activity on
tommi@webrtc.org5b06b062014-08-15 08:38:3089 // the thread (message handling etc) and then discard the results.
Harald Alvestrand1090e442020-10-05 07:01:0990 void GetStats(MediaStreamTrackInterface* track,
91 StatsReports* reports) override;
henrike@webrtc.org28e20752013-07-10 00:45:3692
tommi@webrtc.org4fb7e252015-01-21 11:36:1893 // Prepare a local or remote SSRC report for the given ssrc. Used internally
wu@webrtc.org97077a32013-10-25 21:18:3394 // in the ExtractStatsFromList template.
Peter Boström0c4e06b2015-10-07 10:23:2195 StatsReport* PrepareReport(bool local,
96 uint32_t ssrc,
Steve Antonefe4c922019-03-27 17:26:0697 const std::string& track_id,
Peter Boström0c4e06b2015-10-07 10:23:2198 const StatsReport::Id& transport_id,
99 StatsReport::Direction direction);
henrike@webrtc.org28e20752013-07-10 00:45:36100
Alex Narestbbeb1092019-08-16 09:49:04101 StatsReport* PrepareADMReport();
102
zhihuange9e94c32016-11-04 18:38:15103 // A track is invalid if there is no report data for it.
104 bool IsValidTrack(const std::string& track_id);
105
Tommic811ab52022-01-11 15:33:23106 // 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();
xians@webrtc.org01bda202014-07-09 07:38:38111
Niels Möllerac0a4cb2019-10-09 13:01:33112 bool UseStandardBytesStats() const { return use_standard_bytes_stats_; }
113
henrike@webrtc.org28e20752013-07-10 00:45:36114 private:
Henrik Boströmf7859892022-07-04 12:36:37115 friend class LegacyStatsCollectorTest;
guoweis@webrtc.org950c5182014-12-16 23:01:31116
Tomas Gunnarssone1c8a432021-04-08 13:15:28117 // 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;
126
127 std::string name;
128 cricket::TransportStats stats;
129 std::unique_ptr<rtc::SSLCertificateStats> local_cert_stats;
130 std::unique_ptr<rtc::SSLCertificateStats> remote_cert_stats;
131 };
132
133 struct SessionStats {
134 SessionStats() = default;
135 SessionStats(SessionStats&&) = default;
136 SessionStats(const SessionStats&) = delete;
137
138 SessionStats& operator=(SessionStats&&) = default;
139 SessionStats& operator=(SessionStats&) = delete;
140
141 cricket::CandidateStatsList candidate_stats;
142 std::vector<TransportStats> transport_stats;
143 std::map<std::string, std::string> transport_names_by_mid;
144 };
145
decurtis@webrtc.org322a5642015-02-03 22:09:37146 // Overridden in unit tests to fake timing.
147 virtual double GetTimeNow();
148
henrike@webrtc.org28e20752013-07-10 00:45:36149 bool CopySelectedReports(const std::string& selector, StatsReports* reports);
150
Tomas Gunnarssone1c8a432021-04-08 13:15:28151 // Helper method for creating IceCandidate report. `is_local` indicates
guoweis@webrtc.org950c5182014-12-16 23:01:31152 // whether this candidate is local or remote.
Qingsi Wang72a43a12018-02-21 00:03:18153 StatsReport* AddCandidateReport(
154 const cricket::CandidateStats& candidate_stats,
155 bool local);
guoweis@webrtc.org950c5182014-12-16 23:01:31156
wu@webrtc.org4551b792013-10-09 15:37:36157 // Adds a report for this certificate and every certificate in its chain, and
Tomas Gunnarssone1c8a432021-04-08 13:15:28158 // returns the leaf certificate's report (`cert_stats`'s report).
Taylor Brandstetterc3928662018-02-23 21:04:51159 StatsReport* AddCertificateReports(
160 std::unique_ptr<rtc::SSLCertificateStats> cert_stats);
tommi@webrtc.orgd3900292015-03-12 16:35:55161
162 StatsReport* AddConnectionInfoReport(const std::string& content_name,
Yves Gerey665174f2018-06-19 13:03:05163 int component,
164 int connection_id,
165 const StatsReport::Id& channel_report_id,
166 const cricket::ConnectionInfo& info);
wu@webrtc.org4551b792013-10-09 15:37:36167
Tommif9e13f82023-04-06 19:21:45168 void ExtractDataInfo_n(StatsCollection* reports);
Tomas Gunnarssone1c8a432021-04-08 13:15:28169
170 // Returns the `transport_names_by_mid` member from the SessionStats as
Tommif9e13f82023-04-06 19:21:45171 // 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();
Tomas Gunnarssone1c8a432021-04-08 13:15:28175
stefanf79ade12017-06-02 13:44:03176 void ExtractBweInfo();
Tomas Gunnarssone1c8a432021-04-08 13:15:28177 void ExtractMediaInfo(
178 const std::map<std::string, std::string>& transport_names_by_mid);
nissefcc640f2016-04-01 08:10:42179 void ExtractSenderInfo();
tommi@webrtc.org4fb7e252015-01-21 11:36:18180 webrtc::StatsReport* GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05181 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18182 StatsReport::Direction direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11183
184 // Helper method to get stats from the local audio tracks.
Ivo Creusenae0260962017-11-20 12:07:16185 void UpdateStatsFromExistingLocalAudioTracks(bool has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11186 void UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae0260962017-11-20 12:07:16187 StatsReport* report,
188 bool has_remote_tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36189
jbauchbe24c942015-06-22 22:06:43190 // Helper method to update the timestamp of track records.
191 void UpdateTrackReports();
192
Tomas Gunnarssonbfd9ba82021-04-18 09:55:57193 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);
Tomas Gunnarssone1c8a432021-04-08 13:15:28198 void ExtractSessionInfo_s(SessionStats& session_stats);
199
tommi@webrtc.org4fb7e252015-01-21 11:36:18200 // A collection for all of our stats reports.
201 StatsCollection reports_;
jbauchbe24c942015-06-22 22:06:43202 TrackIdMap track_ids_;
deadbeefab9b2d12015-10-14 18:33:11203 // Raw pointer to the peer connection the statistics are gathered from.
Steve Anton2d8609c2018-01-24 00:38:46204 PeerConnectionInternal* const pc_;
Tommic811ab52022-01-11 15:33:23205 int64_t cache_timestamp_ms_ RTC_GUARDED_BY(pc_->signaling_thread()) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36206 double stats_gathering_started_;
Niels Möllerac0a4cb2019-10-09 13:01:33207 const bool use_standard_bytes_stats_;
henrike@webrtc.org40b3b682014-03-03 18:30:11208
tommi@webrtc.orgd3900292015-03-12 16:35:55209 // TODO(tommi): We appear to be holding on to raw pointers to reference
210 // counted objects? We should be using scoped_refptr here.
Henrik Boströmf7859892022-07-04 12:36:37211 typedef std::vector<std::pair<AudioTrackInterface*, uint32_t>>
henrike@webrtc.org40b3b682014-03-03 18:30:11212 LocalAudioTrackVector;
213 LocalAudioTrackVector local_audio_tracks_;
henrike@webrtc.org28e20752013-07-10 00:45:36214};
215
216} // namespace webrtc
217
Henrik Boströmf7859892022-07-04 12:36:37218#endif // PC_LEGACY_STATS_COLLECTOR_H_