henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2012, Google Inc. |
| 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 | |
| 31 | #ifndef TALK_APP_WEBRTC_STATSCOLLECTOR_H_ |
| 32 | #define TALK_APP_WEBRTC_STATSCOLLECTOR_H_ |
| 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 | |
| 38 | #include "talk/app/webrtc/mediastreaminterface.h" |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 39 | #include "talk/app/webrtc/peerconnectioninterface.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | #include "talk/app/webrtc/statstypes.h" |
| 41 | #include "talk/app/webrtc/webrtcsession.h" |
| 42 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 43 | namespace webrtc { |
| 44 | |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 45 | // Conversion function to convert candidate type string to the corresponding one |
| 46 | // from enum RTCStatsIceCandidateType. |
| 47 | const char* IceCandidateTypeToStatsType(const std::string& candidate_type); |
| 48 | |
| 49 | // Conversion function to convert adapter type to report string which are more |
| 50 | // fitting to the general style of http://w3c.github.io/webrtc-stats. This is |
| 51 | // only used by stats collector. |
| 52 | const char* AdapterTypeToStatsType(rtc::AdapterType type); |
| 53 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | class StatsCollector { |
| 55 | public: |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 56 | enum TrackDirection { |
| 57 | kSending = 0, |
| 58 | kReceiving, |
| 59 | }; |
| 60 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 61 | // The caller is responsible for ensuring that the session outlives the |
| 62 | // StatsCollector instance. |
| 63 | explicit StatsCollector(WebRtcSession* session); |
| 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. |
| 71 | void AddLocalAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc); |
| 72 | |
| 73 | // Removes a local audio tracks that is used for getting some voice |
| 74 | // statistics. |
| 75 | void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc); |
| 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 | |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 91 | // Prepare an SSRC report for the given ssrc. Used internally |
| 92 | // in the ExtractStatsFromList template. |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 93 | StatsReport* PrepareLocalReport(uint32 ssrc, const std::string& transport, |
| 94 | TrackDirection direction); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 95 | // Prepare an SSRC report for the given remote ssrc. Used internally. |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 96 | StatsReport* PrepareRemoteReport(uint32 ssrc, const std::string& transport, |
| 97 | TrackDirection direction); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 99 | // Method used by the unittest to force a update of stats since UpdateStats() |
| 100 | // that occur less than kMinGatherStatsPeriod number of ms apart will be |
| 101 | // ignored. |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 102 | void ClearUpdateStatsCacheForTest(); |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 103 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | private: |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 105 | friend class StatsCollectorTest; |
| 106 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | bool CopySelectedReports(const std::string& selector, StatsReports* reports); |
| 108 | |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 109 | // Helper method for AddCertificateReports. |
| 110 | std::string AddOneCertificateReport( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 111 | const rtc::SSLCertificate* cert, const std::string& issuer_id); |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 112 | |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 113 | // Helper method for creating IceCandidate report. |is_local| indicates |
| 114 | // whether this candidate is local or remote. |
| 115 | std::string AddCandidateReport(const cricket::Candidate& candidate, |
| 116 | const std::string& report_type); |
| 117 | |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 118 | // Adds a report for this certificate and every certificate in its chain, and |
| 119 | // returns the leaf certificate's report's ID. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 120 | std::string AddCertificateReports(const rtc::SSLCertificate* cert); |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 121 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | void ExtractSessionInfo(); |
| 123 | void ExtractVoiceInfo(); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 124 | void ExtractVideoInfo(PeerConnectionInterface::StatsOutputLevel level); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | void BuildSsrcToTransportId(); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 126 | webrtc::StatsReport* GetOrCreateReport(const std::string& type, |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 127 | const std::string& id, |
| 128 | TrackDirection direction); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 129 | webrtc::StatsReport* GetReport(const std::string& type, |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 130 | const std::string& id, |
| 131 | TrackDirection 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. |
| 134 | void UpdateStatsFromExistingLocalAudioTracks(); |
| 135 | void UpdateReportFromAudioTrack(AudioTrackInterface* track, |
| 136 | StatsReport* report); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 138 | // Helper method to get the id for the track identified by ssrc. |
| 139 | // |direction| tells if the track is for sending or receiving. |
| 140 | bool GetTrackIdBySsrc(uint32 ssrc, std::string* track_id, |
| 141 | TrackDirection direction); |
| 142 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 143 | // A map from the report id to the report. |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 144 | StatsSet reports_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 145 | // Raw pointer to the session the statistics are gathered from. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 146 | WebRtcSession* const session_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 147 | double stats_gathering_started_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 148 | cricket::ProxyTransportMap proxy_to_transport_; |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 149 | |
| 150 | typedef std::vector<std::pair<AudioTrackInterface*, uint32> > |
| 151 | LocalAudioTrackVector; |
| 152 | LocalAudioTrackVector local_audio_tracks_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | } // namespace webrtc |
| 156 | |
| 157 | #endif // TALK_APP_WEBRTC_STATSCOLLECTOR_H_ |