blob: b3ed1ed84711298050dbf4b61fb3734a2e9a1670 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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.org28e20752013-07-10 00:45:36 +000034#include <map>
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000035#include <string>
36#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
38#include "talk/app/webrtc/mediastreaminterface.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000039#include "talk/app/webrtc/peerconnectioninterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040#include "talk/app/webrtc/statstypes.h"
41#include "talk/app/webrtc/webrtcsession.h"
42
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043namespace webrtc {
44
45class StatsCollector {
46 public:
xians@webrtc.org4cb01282014-06-12 14:57:05 +000047 enum TrackDirection {
48 kSending = 0,
49 kReceiving,
50 };
51
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 StatsCollector();
53
54 // Register the session Stats should operate on.
55 // Set to NULL if the session has ended.
56 void set_session(WebRtcSession* session) {
57 session_ = session;
58 }
59
60 // Adds a MediaStream with tracks that can be used as a |selector| in a call
61 // to GetStats.
62 void AddStream(MediaStreamInterface* stream);
63
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000064 // Adds a local audio track that is used for getting some voice statistics.
65 void AddLocalAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc);
66
67 // Removes a local audio tracks that is used for getting some voice
68 // statistics.
69 void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc);
70
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 // Gather statistics from the session and store them for future use.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000072 void UpdateStats(PeerConnectionInterface::StatsOutputLevel level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073
74 // Gets a StatsReports of the last collected stats. Note that UpdateStats must
75 // be called before this function to get the most recent stats. |selector| is
76 // a track label or empty string. The most recent reports are stored in
77 // |reports|.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000078 bool GetStats(MediaStreamTrackInterface* track,
79 StatsReports* reports);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080
wu@webrtc.org97077a32013-10-25 21:18:33 +000081 // Prepare an SSRC report for the given ssrc. Used internally
82 // in the ExtractStatsFromList template.
xians@webrtc.org4cb01282014-06-12 14:57:05 +000083 StatsReport* PrepareLocalReport(uint32 ssrc, const std::string& transport,
84 TrackDirection direction);
wu@webrtc.org97077a32013-10-25 21:18:33 +000085 // Prepare an SSRC report for the given remote ssrc. Used internally.
xians@webrtc.org4cb01282014-06-12 14:57:05 +000086 StatsReport* PrepareRemoteReport(uint32 ssrc, const std::string& transport,
87 TrackDirection direction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 // Extracts the ID of a Transport belonging to an SSRC. Used internally.
89 bool GetTransportIdFromProxy(const std::string& proxy,
90 std::string* transport_id);
91
xians@webrtc.org01bda202014-07-09 07:38:38 +000092 // 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.
95 void ClearUpdateStatsCache();
96
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 private:
98 bool CopySelectedReports(const std::string& selector, StatsReports* reports);
99
wu@webrtc.org4551b792013-10-09 15:37:36 +0000100 // Helper method for AddCertificateReports.
101 std::string AddOneCertificateReport(
102 const talk_base::SSLCertificate* cert, const std::string& issuer_id);
103
104 // Adds a report for this certificate and every certificate in its chain, and
105 // returns the leaf certificate's report's ID.
106 std::string AddCertificateReports(const talk_base::SSLCertificate* cert);
107
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 void ExtractSessionInfo();
109 void ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000110 void ExtractVideoInfo(PeerConnectionInterface::StatsOutputLevel level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 double GetTimeNow();
112 void BuildSsrcToTransportId();
wu@webrtc.org97077a32013-10-25 21:18:33 +0000113 WebRtcSession* session() { return session_; }
114 webrtc::StatsReport* GetOrCreateReport(const std::string& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000115 const std::string& id,
116 TrackDirection direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000117 webrtc::StatsReport* GetReport(const std::string& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000118 const std::string& id,
119 TrackDirection direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000120
121 // Helper method to get stats from the local audio tracks.
122 void UpdateStatsFromExistingLocalAudioTracks();
123 void UpdateReportFromAudioTrack(AudioTrackInterface* track,
124 StatsReport* report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000126 // Helper method to get the id for the track identified by ssrc.
127 // |direction| tells if the track is for sending or receiving.
128 bool GetTrackIdBySsrc(uint32 ssrc, std::string* track_id,
129 TrackDirection direction);
130
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 // A map from the report id to the report.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000132 std::map<std::string, StatsReport> reports_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 // Raw pointer to the session the statistics are gathered from.
134 WebRtcSession* session_;
135 double stats_gathering_started_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 cricket::ProxyTransportMap proxy_to_transport_;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000137
138 typedef std::vector<std::pair<AudioTrackInterface*, uint32> >
139 LocalAudioTrackVector;
140 LocalAudioTrackVector local_audio_tracks_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141};
142
143} // namespace webrtc
144
145#endif // TALK_APP_WEBRTC_STATSCOLLECTOR_H_