• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2012 The WebRTC project authors. All Rights Reserved.
3  *
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.
9  */
10 
11 // This file contains a class used for gathering statistics from an ongoing
12 // libjingle PeerConnection.
13 
14 #ifndef PC_STATS_COLLECTOR_H_
15 #define PC_STATS_COLLECTOR_H_
16 
17 #include <stdint.h>
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "api/media_stream_interface.h"
26 #include "api/peer_connection_interface.h"
27 #include "api/stats_types.h"
28 #include "p2p/base/port.h"
29 #include "pc/peer_connection_internal.h"
30 #include "rtc_base/network_constants.h"
31 #include "rtc_base/ssl_certificate.h"
32 
33 namespace webrtc {
34 
35 // Conversion function to convert candidate type string to the corresponding one
36 // from  enum RTCStatsIceCandidateType.
37 const char* IceCandidateTypeToStatsType(const std::string& candidate_type);
38 
39 // Conversion function to convert adapter type to report string which are more
40 // fitting to the general style of http://w3c.github.io/webrtc-stats. This is
41 // only used by stats collector.
42 const char* AdapterTypeToStatsType(rtc::AdapterType type);
43 
44 // A mapping between track ids and their StatsReport.
45 typedef std::map<std::string, StatsReport*> TrackIdMap;
46 
47 class StatsCollector {
48  public:
49   // The caller is responsible for ensuring that the pc outlives the
50   // StatsCollector instance.
51   explicit StatsCollector(PeerConnectionInternal* pc);
52   virtual ~StatsCollector();
53 
54   // Adds a MediaStream with tracks that can be used as a |selector| in a call
55   // to GetStats.
56   void AddStream(MediaStreamInterface* stream);
57   void AddTrack(MediaStreamTrackInterface* track);
58 
59   // Adds a local audio track that is used for getting some voice statistics.
60   void AddLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
61 
62   // Removes a local audio tracks that is used for getting some voice
63   // statistics.
64   void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
65 
66   // Gather statistics from the session and store them for future use.
67   void UpdateStats(PeerConnectionInterface::StatsOutputLevel level);
68 
69   // Gets a StatsReports of the last collected stats. Note that UpdateStats must
70   // be called before this function to get the most recent stats. |selector| is
71   // a track label or empty string. The most recent reports are stored in
72   // |reports|.
73   // TODO(tommi): Change this contract to accept a callback object instead
74   // of filling in |reports|.  As is, there's a requirement that the caller
75   // uses |reports| immediately without allowing any async activity on
76   // the thread (message handling etc) and then discard the results.
77   void GetStats(MediaStreamTrackInterface* track, StatsReports* reports);
78 
79   // Prepare a local or remote SSRC report for the given ssrc. Used internally
80   // in the ExtractStatsFromList template.
81   StatsReport* PrepareReport(bool local,
82                              uint32_t ssrc,
83                              const std::string& track_id,
84                              const StatsReport::Id& transport_id,
85                              StatsReport::Direction direction);
86 
87   StatsReport* PrepareADMReport();
88 
89   // A track is invalid if there is no report data for it.
90   bool IsValidTrack(const std::string& track_id);
91 
92   // 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 ClearUpdateStatsCacheForTest();
96 
UseStandardBytesStats()97   bool UseStandardBytesStats() const { return use_standard_bytes_stats_; }
98 
99  private:
100   friend class StatsCollectorTest;
101 
102   // Overridden in unit tests to fake timing.
103   virtual double GetTimeNow();
104 
105   bool CopySelectedReports(const std::string& selector, StatsReports* reports);
106 
107   // Helper method for creating IceCandidate report. |is_local| indicates
108   // whether this candidate is local or remote.
109   StatsReport* AddCandidateReport(
110       const cricket::CandidateStats& candidate_stats,
111       bool local);
112 
113   // Adds a report for this certificate and every certificate in its chain, and
114   // returns the leaf certificate's report (|cert_stats|'s report).
115   StatsReport* AddCertificateReports(
116       std::unique_ptr<rtc::SSLCertificateStats> cert_stats);
117 
118   StatsReport* AddConnectionInfoReport(const std::string& content_name,
119                                        int component,
120                                        int connection_id,
121                                        const StatsReport::Id& channel_report_id,
122                                        const cricket::ConnectionInfo& info);
123 
124   void ExtractDataInfo();
125   void ExtractSessionInfo();
126   void ExtractBweInfo();
127   void ExtractMediaInfo();
128   void ExtractSenderInfo();
129   webrtc::StatsReport* GetReport(const StatsReport::StatsType& type,
130                                  const std::string& id,
131                                  StatsReport::Direction direction);
132 
133   // Helper method to get stats from the local audio tracks.
134   void UpdateStatsFromExistingLocalAudioTracks(bool has_remote_tracks);
135   void UpdateReportFromAudioTrack(AudioTrackInterface* track,
136                                   StatsReport* report,
137                                   bool has_remote_tracks);
138 
139   // Helper method to update the timestamp of track records.
140   void UpdateTrackReports();
141 
142   // A collection for all of our stats reports.
143   StatsCollection reports_;
144   TrackIdMap track_ids_;
145   // Raw pointer to the peer connection the statistics are gathered from.
146   PeerConnectionInternal* const pc_;
147   double stats_gathering_started_;
148   const bool use_standard_bytes_stats_;
149 
150   // TODO(tommi): We appear to be holding on to raw pointers to reference
151   // counted objects?  We should be using scoped_refptr here.
152   typedef std::vector<std::pair<AudioTrackInterface*, uint32_t> >
153       LocalAudioTrackVector;
154   LocalAudioTrackVector local_audio_tracks_;
155 };
156 
157 }  // namespace webrtc
158 
159 #endif  // PC_STATS_COLLECTOR_H_
160