henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [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 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | // This file contains a class used for gathering statistics from an ongoing |
| 12 | // libjingle PeerConnection. |
| 13 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 14 | #ifndef PC_STATS_COLLECTOR_H_ |
| 15 | #define PC_STATS_COLLECTOR_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 16 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 17 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 18 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | #include <map> |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 20 | #include <memory> |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 21 | #include <string> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 22 | #include <utility> |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 23 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 24 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "api/media_stream_interface.h" |
| 26 | #include "api/peer_connection_interface.h" |
| 27 | #include "api/stats_types.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 28 | #include "p2p/base/port.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 29 | #include "pc/peer_connection_internal.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 30 | #include "rtc_base/network_constants.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 31 | #include "rtc_base/ssl_certificate.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | namespace webrtc { |
| 34 | |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 35 | // Conversion function to convert candidate type string to the corresponding one |
| 36 | // from enum RTCStatsIceCandidateType. |
| 37 | const char* IceCandidateTypeToStatsType(const std::string& candidate_type); |
| 38 | |
| 39 | // Conversion function to convert adapter type to report string which are more |
| 40 | // fitting to the general style of http://w3c.github.io/webrtc-stats. This is |
| 41 | // only used by stats collector. |
| 42 | const char* AdapterTypeToStatsType(rtc::AdapterType type); |
| 43 | |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 44 | // A mapping between track ids and their StatsReport. |
| 45 | typedef std::map<std::string, StatsReport*> TrackIdMap; |
| 46 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 47 | class StatsCollector { |
| 48 | public: |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 49 | // The caller is responsible for ensuring that the pc outlives the |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 50 | // StatsCollector instance. |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 51 | explicit StatsCollector(PeerConnectionInternal* pc); |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 52 | virtual ~StatsCollector(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | |
| 54 | // Adds a MediaStream with tracks that can be used as a |selector| in a call |
| 55 | // to GetStats. |
| 56 | void AddStream(MediaStreamInterface* stream); |
Harald Alvestrand | 75ceef2 | 2018-01-04 15:26:13 +0100 | [diff] [blame] | 57 | void AddTrack(MediaStreamTrackInterface* track); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 59 | // Adds a local audio track that is used for getting some voice statistics. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 60 | void AddLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 61 | |
| 62 | // Removes a local audio tracks that is used for getting some voice |
| 63 | // statistics. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 64 | void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 65 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | // Gather statistics from the session and store them for future use. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 67 | void UpdateStats(PeerConnectionInterface::StatsOutputLevel level); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | |
| 69 | // Gets a StatsReports of the last collected stats. Note that UpdateStats must |
| 70 | // be called before this function to get the most recent stats. |selector| is |
| 71 | // a track label or empty string. The most recent reports are stored in |
| 72 | // |reports|. |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 73 | // TODO(tommi): Change this contract to accept a callback object instead |
| 74 | // of filling in |reports|. As is, there's a requirement that the caller |
| 75 | // uses |reports| immediately without allowing any async activity on |
| 76 | // the thread (message handling etc) and then discard the results. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 77 | void GetStats(MediaStreamTrackInterface* track, StatsReports* reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 79 | // Prepare a local or remote SSRC report for the given ssrc. Used internally |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 80 | // in the ExtractStatsFromList template. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 81 | StatsReport* PrepareReport(bool local, |
| 82 | uint32_t ssrc, |
Steve Anton | efe4c92 | 2019-03-27 10:26:06 -0700 | [diff] [blame] | 83 | const std::string& track_id, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 84 | const StatsReport::Id& transport_id, |
| 85 | StatsReport::Direction direction); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 86 | |
Alex Narest | bbeb109 | 2019-08-16 11:49:04 +0200 | [diff] [blame] | 87 | StatsReport* PrepareADMReport(); |
| 88 | |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 89 | // A track is invalid if there is no report data for it. |
| 90 | bool IsValidTrack(const std::string& track_id); |
| 91 | |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 92 | // Method used by the unittest to force a update of stats since UpdateStats() |
| 93 | // that occur less than kMinGatherStatsPeriod number of ms apart will be |
| 94 | // ignored. |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 95 | void ClearUpdateStatsCacheForTest(); |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 96 | |
Niels Möller | ac0a4cb | 2019-10-09 15:01:33 +0200 | [diff] [blame] | 97 | bool UseStandardBytesStats() const { return use_standard_bytes_stats_; } |
| 98 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | private: |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 100 | friend class StatsCollectorTest; |
| 101 | |
decurtis@webrtc.org | 322a564 | 2015-02-03 22:09:37 +0000 | [diff] [blame] | 102 | // Overridden in unit tests to fake timing. |
| 103 | virtual double GetTimeNow(); |
| 104 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | bool CopySelectedReports(const std::string& selector, StatsReports* reports); |
| 106 | |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 107 | // Helper method for creating IceCandidate report. |is_local| indicates |
| 108 | // whether this candidate is local or remote. |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 109 | StatsReport* AddCandidateReport( |
| 110 | const cricket::CandidateStats& candidate_stats, |
| 111 | bool local); |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 112 | |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 113 | // Adds a report for this certificate and every certificate in its chain, and |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 114 | // returns the leaf certificate's report (|cert_stats|'s report). |
| 115 | StatsReport* AddCertificateReports( |
| 116 | std::unique_ptr<rtc::SSLCertificateStats> cert_stats); |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame] | 117 | |
| 118 | StatsReport* AddConnectionInfoReport(const std::string& content_name, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 119 | int component, |
| 120 | int connection_id, |
| 121 | const StatsReport::Id& channel_report_id, |
| 122 | const cricket::ConnectionInfo& info); |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 123 | |
decurtis@webrtc.org | 487a444 | 2015-01-15 22:55:07 +0000 | [diff] [blame] | 124 | void ExtractDataInfo(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | void ExtractSessionInfo(); |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 126 | void ExtractBweInfo(); |
Steve Anton | b886711 | 2018-02-13 10:07:54 -0800 | [diff] [blame] | 127 | void ExtractMediaInfo(); |
nisse | fcc640f | 2016-04-01 01:10:42 -0700 | [diff] [blame] | 128 | void ExtractSenderInfo(); |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 129 | webrtc::StatsReport* GetReport(const StatsReport::StatsType& type, |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 130 | const std::string& id, |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 131 | StatsReport::Direction direction); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 132 | |
| 133 | // Helper method to get stats from the local audio tracks. |
Ivo Creusen | ae02609 | 2017-11-20 13:07:16 +0100 | [diff] [blame] | 134 | void UpdateStatsFromExistingLocalAudioTracks(bool has_remote_tracks); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 135 | void UpdateReportFromAudioTrack(AudioTrackInterface* track, |
Ivo Creusen | ae02609 | 2017-11-20 13:07:16 +0100 | [diff] [blame] | 136 | StatsReport* report, |
| 137 | bool has_remote_tracks); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 139 | // Helper method to update the timestamp of track records. |
| 140 | void UpdateTrackReports(); |
| 141 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 142 | // A collection for all of our stats reports. |
| 143 | StatsCollection reports_; |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 144 | TrackIdMap track_ids_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 145 | // Raw pointer to the peer connection the statistics are gathered from. |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 146 | PeerConnectionInternal* const pc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 147 | double stats_gathering_started_; |
Niels Möller | ac0a4cb | 2019-10-09 15:01:33 +0200 | [diff] [blame] | 148 | const bool use_standard_bytes_stats_; |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 149 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame] | 150 | // TODO(tommi): We appear to be holding on to raw pointers to reference |
| 151 | // counted objects? We should be using scoped_refptr here. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 152 | typedef std::vector<std::pair<AudioTrackInterface*, uint32_t> > |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 153 | LocalAudioTrackVector; |
| 154 | LocalAudioTrackVector local_audio_tracks_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | } // namespace webrtc |
| 158 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 159 | #endif // PC_STATS_COLLECTOR_H_ |