blob: 37572799fe68fcb33bc0a4bc90458604e1db318f [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11// This file contains a class used for gathering statistics from an ongoing
12// libjingle PeerConnection.
13
Henrik Kjellander15583c12016-02-10 10:53:12 +010014#ifndef WEBRTC_API_STATSCOLLECTOR_H_
15#define WEBRTC_API_STATSCOLLECTOR_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <map>
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000018#include <string>
19#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
Henrik Kjellander15583c12016-02-10 10:53:12 +010021#include "webrtc/api/mediastreaminterface.h"
22#include "webrtc/api/peerconnectioninterface.h"
23#include "webrtc/api/statstypes.h"
24#include "webrtc/api/webrtcsession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026namespace webrtc {
27
deadbeefab9b2d12015-10-14 11:33:11 -070028class PeerConnection;
29
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000030// Conversion function to convert candidate type string to the corresponding one
31// from enum RTCStatsIceCandidateType.
32const 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.
37const char* AdapterTypeToStatsType(rtc::AdapterType type);
38
jbauchbe24c942015-06-22 15:06:43 -070039// A mapping between track ids and their StatsReport.
40typedef std::map<std::string, StatsReport*> TrackIdMap;
41
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042class StatsCollector {
43 public:
deadbeefab9b2d12015-10-14 11:33:11 -070044 // The caller is responsible for ensuring that the pc outlives the
tommi@webrtc.org03505bc2014-07-14 20:15:26 +000045 // StatsCollector instance.
deadbeefab9b2d12015-10-14 11:33:11 -070046 explicit StatsCollector(PeerConnection* pc);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +000047 virtual ~StatsCollector();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
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
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000053 // Adds a local audio track that is used for getting some voice statistics.
Peter Boström0c4e06b2015-10-07 12:23:21 +020054 void AddLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000055
56 // Removes a local audio tracks that is used for getting some voice
57 // statistics.
Peter Boström0c4e06b2015-10-07 12:23:21 +020058 void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000059
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 // Gather statistics from the session and store them for future use.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000061 void UpdateStats(PeerConnectionInterface::StatsOutputLevel level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
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|.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +000067 // 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,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000072 StatsReports* reports);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000074 // Prepare a local or remote SSRC report for the given ssrc. Used internally
wu@webrtc.org97077a32013-10-25 21:18:33 +000075 // in the ExtractStatsFromList template.
Peter Boström0c4e06b2015-10-07 12:23:21 +020076 StatsReport* PrepareReport(bool local,
77 uint32_t ssrc,
78 const StatsReport::Id& transport_id,
79 StatsReport::Direction direction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080
xians@webrtc.org01bda202014-07-09 07:38:38 +000081 // Method used by the unittest to force a update of stats since UpdateStats()
82 // that occur less than kMinGatherStatsPeriod number of ms apart will be
83 // ignored.
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +000084 void ClearUpdateStatsCacheForTest();
xians@webrtc.org01bda202014-07-09 07:38:38 +000085
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 private:
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000087 friend class StatsCollectorTest;
88
decurtis@webrtc.org322a5642015-02-03 22:09:37 +000089 // Overridden in unit tests to fake timing.
90 virtual double GetTimeNow();
91
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 bool CopySelectedReports(const std::string& selector, StatsReports* reports);
93
wu@webrtc.org4551b792013-10-09 15:37:36 +000094 // Helper method for AddCertificateReports.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000095 StatsReport* AddOneCertificateReport(
96 const rtc::SSLCertificate* cert, const StatsReport* issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +000097
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000098 // Helper method for creating IceCandidate report. |is_local| indicates
99 // whether this candidate is local or remote.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000100 StatsReport* AddCandidateReport(const cricket::Candidate& candidate,
101 bool local);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000102
wu@webrtc.org4551b792013-10-09 15:37:36 +0000103 // Adds a report for this certificate and every certificate in its chain, and
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000104 // returns the leaf certificate's report.
105 StatsReport* AddCertificateReports(const rtc::SSLCertificate* cert);
106
107 StatsReport* AddConnectionInfoReport(const std::string& content_name,
108 int component, int connection_id,
109 const StatsReport::Id& channel_report_id,
110 const cricket::ConnectionInfo& info);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000111
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000112 void ExtractDataInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 void ExtractSessionInfo();
114 void ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000115 void ExtractVideoInfo(PeerConnectionInterface::StatsOutputLevel level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 void BuildSsrcToTransportId();
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000117 webrtc::StatsReport* GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000118 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000119 StatsReport::Direction 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.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200128 bool GetTrackIdBySsrc(uint32_t ssrc,
129 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000130 StatsReport::Direction direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000131
jbauchbe24c942015-06-22 15:06:43 -0700132 // Helper method to update the timestamp of track records.
133 void UpdateTrackReports();
134
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000135 // A collection for all of our stats reports.
136 StatsCollection reports_;
jbauchbe24c942015-06-22 15:06:43 -0700137 TrackIdMap track_ids_;
deadbeefab9b2d12015-10-14 11:33:11 -0700138 // Raw pointer to the peer connection the statistics are gathered from.
139 PeerConnection* const pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 double stats_gathering_started_;
deadbeefd59daf82015-10-14 15:02:44 -0700141 ProxyTransportMap proxy_to_transport_;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000142
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000143 // TODO(tommi): We appear to be holding on to raw pointers to reference
144 // counted objects? We should be using scoped_refptr here.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200145 typedef std::vector<std::pair<AudioTrackInterface*, uint32_t> >
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000146 LocalAudioTrackVector;
147 LocalAudioTrackVector local_audio_tracks_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148};
149
150} // namespace webrtc
151
Henrik Kjellander15583c12016-02-10 10:53:12 +0100152#endif // WEBRTC_API_STATSCOLLECTOR_H_