blob: 1a3abfc9aafc46316010560c796f1b9cc5e36fe0 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#ifndef PC_STATSCOLLECTOR_H_
15#define PC_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>
Steve Anton36b29d12017-10-30 09:57:42 -070019#include <utility>
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000020#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/mediastreaminterface.h"
23#include "api/peerconnectioninterface.h"
24#include "api/statstypes.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);
Harald Alvestrand75ceef22018-01-04 15:26:13 +010052 void AddTrack(MediaStreamTrackInterface* track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000054 // Adds a local audio track that is used for getting some voice statistics.
Peter Boström0c4e06b2015-10-07 12:23:21 +020055 void AddLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000056
57 // Removes a local audio tracks that is used for getting some voice
58 // statistics.
Peter Boström0c4e06b2015-10-07 12:23:21 +020059 void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000060
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 // Gather statistics from the session and store them for future use.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000062 void UpdateStats(PeerConnectionInterface::StatsOutputLevel level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
64 // Gets a StatsReports of the last collected stats. Note that UpdateStats must
65 // be called before this function to get the most recent stats. |selector| is
66 // a track label or empty string. The most recent reports are stored in
67 // |reports|.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +000068 // TODO(tommi): Change this contract to accept a callback object instead
69 // of filling in |reports|. As is, there's a requirement that the caller
70 // uses |reports| immediately without allowing any async activity on
71 // the thread (message handling etc) and then discard the results.
72 void GetStats(MediaStreamTrackInterface* track,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000073 StatsReports* reports);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000075 // Prepare a local or remote SSRC report for the given ssrc. Used internally
wu@webrtc.org97077a32013-10-25 21:18:33 +000076 // in the ExtractStatsFromList template.
Peter Boström0c4e06b2015-10-07 12:23:21 +020077 StatsReport* PrepareReport(bool local,
78 uint32_t ssrc,
79 const StatsReport::Id& transport_id,
80 StatsReport::Direction direction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
zhihuange9e94c32016-11-04 11:38:15 -070082 // A track is invalid if there is no report data for it.
83 bool IsValidTrack(const std::string& track_id);
84
xians@webrtc.org01bda202014-07-09 07:38:38 +000085 // Method used by the unittest to force a update of stats since UpdateStats()
86 // that occur less than kMinGatherStatsPeriod number of ms apart will be
87 // ignored.
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +000088 void ClearUpdateStatsCacheForTest();
xians@webrtc.org01bda202014-07-09 07:38:38 +000089
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 private:
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000091 friend class StatsCollectorTest;
92
decurtis@webrtc.org322a5642015-02-03 22:09:37 +000093 // Overridden in unit tests to fake timing.
94 virtual double GetTimeNow();
95
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 bool CopySelectedReports(const std::string& selector, StatsReports* reports);
97
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
hbose29352b2016-08-25 03:52:38 -0700104 // returns the leaf certificate's report (|cert|'s report).
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000105 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();
stefanf79ade12017-06-02 06:44:03 -0700114 void ExtractBweInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 void ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000116 void ExtractVideoInfo(PeerConnectionInterface::StatsOutputLevel level);
nissefcc640f2016-04-01 01:10:42 -0700117 void ExtractSenderInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 void BuildSsrcToTransportId();
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000119 webrtc::StatsReport* GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000120 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000121 StatsReport::Direction direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000122
123 // Helper method to get stats from the local audio tracks.
Ivo Creusenae026092017-11-20 13:07:16 +0100124 void UpdateStatsFromExistingLocalAudioTracks(bool has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000125 void UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae026092017-11-20 13:07:16 +0100126 StatsReport* report,
127 bool has_remote_tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000129 // Helper method to get the id for the track identified by ssrc.
130 // |direction| tells if the track is for sending or receiving.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200131 bool GetTrackIdBySsrc(uint32_t ssrc,
132 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000133 StatsReport::Direction direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000134
jbauchbe24c942015-06-22 15:06:43 -0700135 // Helper method to update the timestamp of track records.
136 void UpdateTrackReports();
137
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000138 // A collection for all of our stats reports.
139 StatsCollection reports_;
jbauchbe24c942015-06-22 15:06:43 -0700140 TrackIdMap track_ids_;
deadbeefab9b2d12015-10-14 11:33:11 -0700141 // Raw pointer to the peer connection the statistics are gathered from.
142 PeerConnection* const pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 double stats_gathering_started_;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000144
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000145 // TODO(tommi): We appear to be holding on to raw pointers to reference
146 // counted objects? We should be using scoped_refptr here.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200147 typedef std::vector<std::pair<AudioTrackInterface*, uint32_t> >
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000148 LocalAudioTrackVector;
149 LocalAudioTrackVector local_audio_tracks_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150};
151
152} // namespace webrtc
153
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200154#endif // PC_STATSCOLLECTOR_H_