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