henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2012 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | // This file contains a class used for gathering statistics from an ongoing |
| 29 | // libjingle PeerConnection. |
| 30 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 31 | #ifndef WEBRTC_API_STATSCOLLECTOR_H_ |
| 32 | #define WEBRTC_API_STATSCOLLECTOR_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | #include <map> |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 35 | #include <string> |
| 36 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 38 | #include "webrtc/api/mediastreaminterface.h" |
| 39 | #include "webrtc/api/peerconnectioninterface.h" |
| 40 | #include "webrtc/api/statstypes.h" |
| 41 | #include "webrtc/api/webrtcsession.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 43 | namespace webrtc { |
| 44 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 45 | class PeerConnection; |
| 46 | |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 47 | // Conversion function to convert candidate type string to the corresponding one |
| 48 | // from enum RTCStatsIceCandidateType. |
| 49 | const char* IceCandidateTypeToStatsType(const std::string& candidate_type); |
| 50 | |
| 51 | // Conversion function to convert adapter type to report string which are more |
| 52 | // fitting to the general style of http://w3c.github.io/webrtc-stats. This is |
| 53 | // only used by stats collector. |
| 54 | const char* AdapterTypeToStatsType(rtc::AdapterType type); |
| 55 | |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 56 | // A mapping between track ids and their StatsReport. |
| 57 | typedef std::map<std::string, StatsReport*> TrackIdMap; |
| 58 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | class StatsCollector { |
| 60 | public: |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 61 | // The caller is responsible for ensuring that the pc outlives the |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 62 | // StatsCollector instance. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 63 | explicit StatsCollector(PeerConnection* pc); |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 64 | virtual ~StatsCollector(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | |
| 66 | // Adds a MediaStream with tracks that can be used as a |selector| in a call |
| 67 | // to GetStats. |
| 68 | void AddStream(MediaStreamInterface* stream); |
| 69 | |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 70 | // 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] | 71 | void AddLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 72 | |
| 73 | // Removes a local audio tracks that is used for getting some voice |
| 74 | // statistics. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 75 | void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 76 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | // Gather statistics from the session and store them for future use. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 78 | void UpdateStats(PeerConnectionInterface::StatsOutputLevel level); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | |
| 80 | // Gets a StatsReports of the last collected stats. Note that UpdateStats must |
| 81 | // be called before this function to get the most recent stats. |selector| is |
| 82 | // a track label or empty string. The most recent reports are stored in |
| 83 | // |reports|. |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 84 | // TODO(tommi): Change this contract to accept a callback object instead |
| 85 | // of filling in |reports|. As is, there's a requirement that the caller |
| 86 | // uses |reports| immediately without allowing any async activity on |
| 87 | // the thread (message handling etc) and then discard the results. |
| 88 | void GetStats(MediaStreamTrackInterface* track, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 89 | StatsReports* reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 91 | // 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] | 92 | // in the ExtractStatsFromList template. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 93 | StatsReport* PrepareReport(bool local, |
| 94 | uint32_t ssrc, |
| 95 | const StatsReport::Id& transport_id, |
| 96 | StatsReport::Direction direction); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 98 | // Method used by the unittest to force a update of stats since UpdateStats() |
| 99 | // that occur less than kMinGatherStatsPeriod number of ms apart will be |
| 100 | // ignored. |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 101 | void ClearUpdateStatsCacheForTest(); |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 102 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | private: |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 104 | friend class StatsCollectorTest; |
| 105 | |
decurtis@webrtc.org | 322a564 | 2015-02-03 22:09:37 +0000 | [diff] [blame] | 106 | // Overridden in unit tests to fake timing. |
| 107 | virtual double GetTimeNow(); |
| 108 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | bool CopySelectedReports(const std::string& selector, StatsReports* reports); |
| 110 | |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 111 | // Helper method for AddCertificateReports. |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame] | 112 | StatsReport* AddOneCertificateReport( |
| 113 | const rtc::SSLCertificate* cert, const StatsReport* issuer); |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 114 | |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 115 | // Helper method for creating IceCandidate report. |is_local| indicates |
| 116 | // whether this candidate is local or remote. |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame] | 117 | StatsReport* AddCandidateReport(const cricket::Candidate& candidate, |
| 118 | bool local); |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 119 | |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 120 | // Adds a report for this certificate and every certificate in its chain, and |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame] | 121 | // returns the leaf certificate's report. |
| 122 | StatsReport* AddCertificateReports(const rtc::SSLCertificate* cert); |
| 123 | |
| 124 | StatsReport* AddConnectionInfoReport(const std::string& content_name, |
| 125 | int component, int connection_id, |
| 126 | const StatsReport::Id& channel_report_id, |
| 127 | const cricket::ConnectionInfo& info); |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 128 | |
decurtis@webrtc.org | 487a444 | 2015-01-15 22:55:07 +0000 | [diff] [blame] | 129 | void ExtractDataInfo(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 130 | void ExtractSessionInfo(); |
| 131 | void ExtractVoiceInfo(); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 132 | void ExtractVideoInfo(PeerConnectionInterface::StatsOutputLevel level); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 133 | void BuildSsrcToTransportId(); |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 134 | webrtc::StatsReport* GetReport(const StatsReport::StatsType& type, |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 135 | const std::string& id, |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 136 | StatsReport::Direction direction); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 137 | |
| 138 | // Helper method to get stats from the local audio tracks. |
| 139 | void UpdateStatsFromExistingLocalAudioTracks(); |
| 140 | void UpdateReportFromAudioTrack(AudioTrackInterface* track, |
| 141 | StatsReport* report); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 142 | |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 143 | // Helper method to get the id for the track identified by ssrc. |
| 144 | // |direction| tells if the track is for sending or receiving. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 145 | bool GetTrackIdBySsrc(uint32_t ssrc, |
| 146 | std::string* track_id, |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 147 | StatsReport::Direction direction); |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 148 | |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 149 | // Helper method to update the timestamp of track records. |
| 150 | void UpdateTrackReports(); |
| 151 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 152 | // A collection for all of our stats reports. |
| 153 | StatsCollection reports_; |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 154 | TrackIdMap track_ids_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 155 | // Raw pointer to the peer connection the statistics are gathered from. |
| 156 | PeerConnection* const pc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | double stats_gathering_started_; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 158 | ProxyTransportMap proxy_to_transport_; |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 159 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame] | 160 | // TODO(tommi): We appear to be holding on to raw pointers to reference |
| 161 | // counted objects? We should be using scoped_refptr here. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 162 | typedef std::vector<std::pair<AudioTrackInterface*, uint32_t> > |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 163 | LocalAudioTrackVector; |
| 164 | LocalAudioTrackVector local_audio_tracks_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | } // namespace webrtc |
| 168 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 169 | #endif // WEBRTC_API_STATSCOLLECTOR_H_ |