blob: a3c5992afc65f829512e76ccbfb879bdd4122c37 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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:369 */
10
11// This file contains a class used for gathering statistics from an ongoing
12// libjingle PeerConnection.
13
Mirko Bonadei92ea95e2017-09-15 04:47:3114#ifndef PC_STATSCOLLECTOR_H_
15#define PC_STATSCOLLECTOR_H_
henrike@webrtc.org28e20752013-07-10 00:45:3616
henrike@webrtc.org28e20752013-07-10 00:45:3617#include <map>
Taylor Brandstetterc3928662018-02-23 21:04:5118#include <memory>
henrike@webrtc.org40b3b682014-03-03 18:30:1119#include <string>
Steve Anton36b29d12017-10-30 16:57:4220#include <utility>
henrike@webrtc.org40b3b682014-03-03 18:30:1121#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:3622
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "api/mediastreaminterface.h"
24#include "api/peerconnectioninterface.h"
25#include "api/statstypes.h"
Steve Anton2d8609c2018-01-24 00:38:4626#include "pc/peerconnectioninternal.h"
henrike@webrtc.org28e20752013-07-10 00:45:3627
henrike@webrtc.org28e20752013-07-10 00:45:3628namespace webrtc {
29
guoweis@webrtc.org950c5182014-12-16 23:01:3130// 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 22:06:4339// A mapping between track ids and their StatsReport.
40typedef std::map<std::string, StatsReport*> TrackIdMap;
41
henrike@webrtc.org28e20752013-07-10 00:45:3642class StatsCollector {
43 public:
deadbeefab9b2d12015-10-14 18:33:1144 // The caller is responsible for ensuring that the pc outlives the
tommi@webrtc.org03505bc2014-07-14 20:15:2645 // StatsCollector instance.
Steve Anton2d8609c2018-01-24 00:38:4646 explicit StatsCollector(PeerConnectionInternal* pc);
tommi@webrtc.org03505bc2014-07-14 20:15:2647 virtual ~StatsCollector();
henrike@webrtc.org28e20752013-07-10 00:45:3648
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 14:26:1352 void AddTrack(MediaStreamTrackInterface* track);
henrike@webrtc.org28e20752013-07-10 00:45:3653
henrike@webrtc.org40b3b682014-03-03 18:30:1154 // Adds a local audio track that is used for getting some voice statistics.
Peter Boström0c4e06b2015-10-07 10:23:2155 void AddLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:1156
57 // Removes a local audio tracks that is used for getting some voice
58 // statistics.
Peter Boström0c4e06b2015-10-07 10:23:2159 void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:1160
henrike@webrtc.org28e20752013-07-10 00:45:3661 // Gather statistics from the session and store them for future use.
wu@webrtc.orgb9a088b2014-02-13 23:18:4962 void UpdateStats(PeerConnectionInterface::StatsOutputLevel level);
henrike@webrtc.org28e20752013-07-10 00:45:3663
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:3068 // 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.
Yves Gerey665174f2018-06-19 13:03:0572 void GetStats(MediaStreamTrackInterface* track, StatsReports* reports);
henrike@webrtc.org28e20752013-07-10 00:45:3673
tommi@webrtc.org4fb7e252015-01-21 11:36:1874 // Prepare a local or remote SSRC report for the given ssrc. Used internally
wu@webrtc.org97077a32013-10-25 21:18:3375 // in the ExtractStatsFromList template.
Peter Boström0c4e06b2015-10-07 10:23:2176 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:3680
zhihuange9e94c32016-11-04 18:38:1581 // A track is invalid if there is no report data for it.
82 bool IsValidTrack(const std::string& track_id);
83
xians@webrtc.org01bda202014-07-09 07:38:3884 // 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.
tommi@webrtc.org69bc5a32014-12-15 09:44:4887 void ClearUpdateStatsCacheForTest();
xians@webrtc.org01bda202014-07-09 07:38:3888
henrike@webrtc.org28e20752013-07-10 00:45:3689 private:
guoweis@webrtc.org950c5182014-12-16 23:01:3190 friend class StatsCollectorTest;
91
decurtis@webrtc.org322a5642015-02-03 22:09:3792 // Overridden in unit tests to fake timing.
93 virtual double GetTimeNow();
94
henrike@webrtc.org28e20752013-07-10 00:45:3695 bool CopySelectedReports(const std::string& selector, StatsReports* reports);
96
guoweis@webrtc.org950c5182014-12-16 23:01:3197 // Helper method for creating IceCandidate report. |is_local| indicates
98 // whether this candidate is local or remote.
Qingsi Wang72a43a12018-02-21 00:03:1899 StatsReport* AddCandidateReport(
100 const cricket::CandidateStats& candidate_stats,
101 bool local);
guoweis@webrtc.org950c5182014-12-16 23:01:31102
wu@webrtc.org4551b792013-10-09 15:37:36103 // Adds a report for this certificate and every certificate in its chain, and
Taylor Brandstetterc3928662018-02-23 21:04:51104 // returns the leaf certificate's report (|cert_stats|'s report).
105 StatsReport* AddCertificateReports(
106 std::unique_ptr<rtc::SSLCertificateStats> cert_stats);
tommi@webrtc.orgd3900292015-03-12 16:35:55107
108 StatsReport* AddConnectionInfoReport(const std::string& content_name,
Yves Gerey665174f2018-06-19 13:03:05109 int component,
110 int connection_id,
111 const StatsReport::Id& channel_report_id,
112 const cricket::ConnectionInfo& info);
wu@webrtc.org4551b792013-10-09 15:37:36113
decurtis@webrtc.org487a4442015-01-15 22:55:07114 void ExtractDataInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36115 void ExtractSessionInfo();
stefanf79ade12017-06-02 13:44:03116 void ExtractBweInfo();
Steve Antonb8867112018-02-13 18:07:54117 void ExtractMediaInfo();
nissefcc640f2016-04-01 08:10:42118 void ExtractSenderInfo();
tommi@webrtc.org4fb7e252015-01-21 11:36:18119 webrtc::StatsReport* GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05120 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18121 StatsReport::Direction direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11122
123 // Helper method to get stats from the local audio tracks.
Ivo Creusenae0260962017-11-20 12:07:16124 void UpdateStatsFromExistingLocalAudioTracks(bool has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11125 void UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae0260962017-11-20 12:07:16126 StatsReport* report,
127 bool has_remote_tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36128
xians@webrtc.org4cb01282014-06-12 14:57:05129 // 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 10:23:21131 bool GetTrackIdBySsrc(uint32_t ssrc,
132 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18133 StatsReport::Direction direction);
xians@webrtc.org4cb01282014-06-12 14:57:05134
jbauchbe24c942015-06-22 22:06:43135 // Helper method to update the timestamp of track records.
136 void UpdateTrackReports();
137
tommi@webrtc.org4fb7e252015-01-21 11:36:18138 // A collection for all of our stats reports.
139 StatsCollection reports_;
jbauchbe24c942015-06-22 22:06:43140 TrackIdMap track_ids_;
deadbeefab9b2d12015-10-14 18:33:11141 // Raw pointer to the peer connection the statistics are gathered from.
Steve Anton2d8609c2018-01-24 00:38:46142 PeerConnectionInternal* const pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36143 double stats_gathering_started_;
henrike@webrtc.org40b3b682014-03-03 18:30:11144
tommi@webrtc.orgd3900292015-03-12 16:35:55145 // 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 10:23:21147 typedef std::vector<std::pair<AudioTrackInterface*, uint32_t> >
henrike@webrtc.org40b3b682014-03-03 18:30:11148 LocalAudioTrackVector;
149 LocalAudioTrackVector local_audio_tracks_;
henrike@webrtc.org28e20752013-07-10 00:45:36150};
151
152} // namespace webrtc
153
Mirko Bonadei92ea95e2017-09-15 04:47:31154#endif // PC_STATSCOLLECTOR_H_