Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / app / webrtc / statscollector.h
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
34 #include <map>
35 #include <string>
36 #include <vector>
37
38 #include "talk/app/webrtc/mediastreaminterface.h"
39 #include "talk/app/webrtc/peerconnectioninterface.h"
40 #include "talk/app/webrtc/statstypes.h"
41 #include "talk/app/webrtc/webrtcsession.h"
42
43 namespace webrtc {
44
45 class StatsCollector {
46  public:
47   enum TrackDirection {
48     kSending = 0,
49     kReceiving,
50   };
51
52   // The caller is responsible for ensuring that the session outlives the
53   // StatsCollector instance.
54   explicit StatsCollector(WebRtcSession* session);
55   virtual ~StatsCollector();
56
57   // Adds a MediaStream with tracks that can be used as a |selector| in a call
58   // to GetStats.
59   void AddStream(MediaStreamInterface* stream);
60
61   // Adds a local audio track that is used for getting some voice statistics.
62   void AddLocalAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc);
63
64   // Removes a local audio tracks that is used for getting some voice
65   // statistics.
66   void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc);
67
68   // Gather statistics from the session and store them for future use.
69   void UpdateStats(PeerConnectionInterface::StatsOutputLevel level);
70
71   // Gets a StatsReports of the last collected stats. Note that UpdateStats must
72   // be called before this function to get the most recent stats. |selector| is
73   // a track label or empty string. The most recent reports are stored in
74   // |reports|.
75   bool GetStats(MediaStreamTrackInterface* track,
76                 StatsReports* reports);
77
78   // Prepare an SSRC report for the given ssrc. Used internally
79   // in the ExtractStatsFromList template.
80   StatsReport* PrepareLocalReport(uint32 ssrc, const std::string& transport,
81                                   TrackDirection direction);
82   // Prepare an SSRC report for the given remote ssrc. Used internally.
83   StatsReport* PrepareRemoteReport(uint32 ssrc, const std::string& transport,
84                                    TrackDirection direction);
85
86   // Method used by the unittest to force a update of stats since UpdateStats()
87   // that occur less than kMinGatherStatsPeriod number of ms apart will be
88   // ignored.
89   void ClearUpdateStatsCache();
90
91  private:
92   bool CopySelectedReports(const std::string& selector, StatsReports* reports);
93
94   // Helper method for AddCertificateReports.
95   std::string AddOneCertificateReport(
96       const rtc::SSLCertificate* cert, const std::string& issuer_id);
97
98   // Adds a report for this certificate and every certificate in its chain, and
99   // returns the leaf certificate's report's ID.
100   std::string AddCertificateReports(const rtc::SSLCertificate* cert);
101
102   void ExtractSessionInfo();
103   void ExtractVoiceInfo();
104   void ExtractVideoInfo(PeerConnectionInterface::StatsOutputLevel level);
105   void BuildSsrcToTransportId();
106   webrtc::StatsReport* GetOrCreateReport(const std::string& type,
107                                          const std::string& id,
108                                          TrackDirection direction);
109   webrtc::StatsReport* GetReport(const std::string& type,
110                                  const std::string& id,
111                                  TrackDirection direction);
112
113   // Helper method to get stats from the local audio tracks.
114   void UpdateStatsFromExistingLocalAudioTracks();
115   void UpdateReportFromAudioTrack(AudioTrackInterface* track,
116                                   StatsReport* report);
117
118   // Helper method to get the id for the track identified by ssrc.
119   // |direction| tells if the track is for sending or receiving.
120   bool GetTrackIdBySsrc(uint32 ssrc, std::string* track_id,
121                         TrackDirection direction);
122
123   // A map from the report id to the report.
124   std::map<std::string, StatsReport> reports_;
125   // Raw pointer to the session the statistics are gathered from.
126   WebRtcSession* const session_;
127   double stats_gathering_started_;
128   cricket::ProxyTransportMap proxy_to_transport_;
129
130   typedef std::vector<std::pair<AudioTrackInterface*, uint32> >
131       LocalAudioTrackVector;
132   LocalAudioTrackVector local_audio_tracks_;
133 };
134
135 }  // namespace webrtc
136
137 #endif  // TALK_APP_WEBRTC_STATSCOLLECTOR_H_