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