henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | // This file contains a class used for gathering statistics from an ongoing |
| 12 | // libjingle PeerConnection. |
| 13 | |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 14 | #ifndef PC_LEGACY_STATS_COLLECTOR_H_ |
| 15 | #define PC_LEGACY_STATS_COLLECTOR_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 16 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 17 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 18 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <cstdint> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 21 | #include <map> |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 22 | #include <memory> |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 | [diff] [blame] | 23 | #include <string> |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 24 | #include <type_traits> |
Steve Anton | 36b29d1 | 2017-10-30 16:57:42 | [diff] [blame] | 25 | #include <utility> |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 | [diff] [blame] | 26 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 27 | |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 28 | #include "absl/types/optional.h" |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 29 | #include "api/field_trials_view.h" |
Henrik Boström | 3e6931b | 2022-11-11 09:07:34 | [diff] [blame] | 30 | #include "api/legacy_stats_types.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 31 | #include "api/media_stream_interface.h" |
| 32 | #include "api/peer_connection_interface.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 33 | #include "api/scoped_refptr.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 34 | #include "p2p/base/connection_info.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 35 | #include "p2p/base/port.h" |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 36 | #include "pc/legacy_stats_collector_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 37 | #include "pc/peer_connection_internal.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 38 | #include "pc/rtp_transceiver.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 39 | #include "pc/transport_stats.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 40 | #include "rtc_base/network_constants.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 41 | #include "rtc_base/ssl_certificate.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 42 | #include "rtc_base/thread_annotations.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 43 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 44 | namespace webrtc { |
| 45 | |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 | [diff] [blame] | 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); |
| 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. |
| 53 | const char* AdapterTypeToStatsType(rtc::AdapterType type); |
| 54 | |
jbauch | be24c94 | 2015-06-22 22:06:43 | [diff] [blame] | 55 | // A mapping between track ids and their StatsReport. |
| 56 | typedef std::map<std::string, StatsReport*> TrackIdMap; |
| 57 | |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 58 | class LegacyStatsCollector : public LegacyStatsCollectorInterface { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 59 | public: |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 60 | // The caller is responsible for ensuring that the pc outlives the |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 61 | // LegacyStatsCollector instance. |
| 62 | explicit LegacyStatsCollector(PeerConnectionInternal* pc); |
| 63 | virtual ~LegacyStatsCollector(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 64 | |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 65 | // Adds a MediaStream with tracks that can be used as a `selector` in a call |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 66 | // to GetStats. |
| 67 | void AddStream(MediaStreamInterface* stream); |
Harald Alvestrand | 75ceef2 | 2018-01-04 14:26:13 | [diff] [blame] | 68 | void AddTrack(MediaStreamTrackInterface* track); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 69 | |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 | [diff] [blame] | 70 | // Adds a local audio track that is used for getting some voice statistics. |
Harald Alvestrand | 445e6b0 | 2020-09-29 14:21:47 | [diff] [blame] | 71 | void AddLocalAudioTrack(AudioTrackInterface* audio_track, |
| 72 | uint32_t ssrc) override; |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 | [diff] [blame] | 73 | |
| 74 | // Removes a local audio tracks that is used for getting some voice |
| 75 | // statistics. |
Harald Alvestrand | 445e6b0 | 2020-09-29 14:21:47 | [diff] [blame] | 76 | void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, |
| 77 | uint32_t ssrc) override; |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 | [diff] [blame] | 78 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 79 | // Gather statistics from the session and store them for future use. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 | [diff] [blame] | 80 | void UpdateStats(PeerConnectionInterface::StatsOutputLevel level); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 81 | |
| 82 | // Gets a StatsReports of the last collected stats. Note that UpdateStats must |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 83 | // be called before this function to get the most recent stats. `selector` is |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 84 | // a track label or empty string. The most recent reports are stored in |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 85 | // `reports`. |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 | [diff] [blame] | 86 | // TODO(tommi): Change this contract to accept a callback object instead |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 87 | // 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.org | 5b06b06 | 2014-08-15 08:38:30 | [diff] [blame] | 89 | // the thread (message handling etc) and then discard the results. |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 90 | void GetStats(MediaStreamTrackInterface* track, |
| 91 | StatsReports* reports) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 92 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 | [diff] [blame] | 93 | // Prepare a local or remote SSRC report for the given ssrc. Used internally |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 94 | // in the ExtractStatsFromList template. |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 95 | StatsReport* PrepareReport(bool local, |
| 96 | uint32_t ssrc, |
Steve Anton | efe4c92 | 2019-03-27 17:26:06 | [diff] [blame] | 97 | const std::string& track_id, |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 98 | const StatsReport::Id& transport_id, |
| 99 | StatsReport::Direction direction); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 100 | |
Alex Narest | bbeb109 | 2019-08-16 09:49:04 | [diff] [blame] | 101 | StatsReport* PrepareADMReport(); |
| 102 | |
zhihuang | e9e94c3 | 2016-11-04 18:38:15 | [diff] [blame] | 103 | // A track is invalid if there is no report data for it. |
| 104 | bool IsValidTrack(const std::string& track_id); |
| 105 | |
Tommi | c811ab5 | 2022-01-11 15:33:23 | [diff] [blame] | 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(); |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 | [diff] [blame] | 111 | |
Niels Möller | ac0a4cb | 2019-10-09 13:01:33 | [diff] [blame] | 112 | bool UseStandardBytesStats() const { return use_standard_bytes_stats_; } |
| 113 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 114 | private: |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 115 | friend class LegacyStatsCollectorTest; |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 | [diff] [blame] | 116 | |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 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; |
| 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.org | 322a564 | 2015-02-03 22:09:37 | [diff] [blame] | 146 | // Overridden in unit tests to fake timing. |
| 147 | virtual double GetTimeNow(); |
| 148 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 149 | bool CopySelectedReports(const std::string& selector, StatsReports* reports); |
| 150 | |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 151 | // Helper method for creating IceCandidate report. `is_local` indicates |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 | [diff] [blame] | 152 | // whether this candidate is local or remote. |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 153 | StatsReport* AddCandidateReport( |
| 154 | const cricket::CandidateStats& candidate_stats, |
| 155 | bool local); |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 | [diff] [blame] | 156 | |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 | [diff] [blame] | 157 | // Adds a report for this certificate and every certificate in its chain, and |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 158 | // returns the leaf certificate's report (`cert_stats`'s report). |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 159 | StatsReport* AddCertificateReports( |
| 160 | std::unique_ptr<rtc::SSLCertificateStats> cert_stats); |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 | [diff] [blame] | 161 | |
| 162 | StatsReport* AddConnectionInfoReport(const std::string& content_name, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 163 | int component, |
| 164 | int connection_id, |
| 165 | const StatsReport::Id& channel_report_id, |
| 166 | const cricket::ConnectionInfo& info); |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 | [diff] [blame] | 167 | |
Tommi | f9e13f8 | 2023-04-06 19:21:45 | [diff] [blame] | 168 | void ExtractDataInfo_n(StatsCollection* reports); |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 169 | |
| 170 | // Returns the `transport_names_by_mid` member from the SessionStats as |
Tommi | f9e13f8 | 2023-04-06 19:21:45 | [diff] [blame] | 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(); |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 175 | |
stefan | f79ade1 | 2017-06-02 13:44:03 | [diff] [blame] | 176 | void ExtractBweInfo(); |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 177 | void ExtractMediaInfo( |
| 178 | const std::map<std::string, std::string>& transport_names_by_mid); |
nisse | fcc640f | 2016-04-01 08:10:42 | [diff] [blame] | 179 | void ExtractSenderInfo(); |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 | [diff] [blame] | 180 | webrtc::StatsReport* GetReport(const StatsReport::StatsType& type, |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 | [diff] [blame] | 181 | const std::string& id, |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 | [diff] [blame] | 182 | StatsReport::Direction direction); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 | [diff] [blame] | 183 | |
| 184 | // Helper method to get stats from the local audio tracks. |
Ivo Creusen | ae026096 | 2017-11-20 12:07:16 | [diff] [blame] | 185 | void UpdateStatsFromExistingLocalAudioTracks(bool has_remote_tracks); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 | [diff] [blame] | 186 | void UpdateReportFromAudioTrack(AudioTrackInterface* track, |
Ivo Creusen | ae026096 | 2017-11-20 12:07:16 | [diff] [blame] | 187 | StatsReport* report, |
| 188 | bool has_remote_tracks); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 189 | |
jbauch | be24c94 | 2015-06-22 22:06:43 | [diff] [blame] | 190 | // Helper method to update the timestamp of track records. |
| 191 | void UpdateTrackReports(); |
| 192 | |
Tomas Gunnarsson | bfd9ba8 | 2021-04-18 09:55:57 | [diff] [blame] | 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); |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 198 | void ExtractSessionInfo_s(SessionStats& session_stats); |
| 199 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 | [diff] [blame] | 200 | // A collection for all of our stats reports. |
| 201 | StatsCollection reports_; |
jbauch | be24c94 | 2015-06-22 22:06:43 | [diff] [blame] | 202 | TrackIdMap track_ids_; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 203 | // Raw pointer to the peer connection the statistics are gathered from. |
Steve Anton | 2d8609c | 2018-01-24 00:38:46 | [diff] [blame] | 204 | PeerConnectionInternal* const pc_; |
Tommi | c811ab5 | 2022-01-11 15:33:23 | [diff] [blame] | 205 | int64_t cache_timestamp_ms_ RTC_GUARDED_BY(pc_->signaling_thread()) = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 206 | double stats_gathering_started_; |
Niels Möller | ac0a4cb | 2019-10-09 13:01:33 | [diff] [blame] | 207 | const bool use_standard_bytes_stats_; |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 | [diff] [blame] | 208 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 | [diff] [blame] | 209 | // 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öm | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 211 | typedef std::vector<std::pair<AudioTrackInterface*, uint32_t>> |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 | [diff] [blame] | 212 | LocalAudioTrackVector; |
| 213 | LocalAudioTrackVector local_audio_tracks_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | } // namespace webrtc |
| 217 | |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 218 | #endif // PC_LEGACY_STATS_COLLECTOR_H_ |