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