blob: 041fe2f8fe17dd6dc53c729640081abf68ab9b11 [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
Steve Anton10542f22019-01-11 09:11:00 -080014#ifndef PC_STATS_COLLECTOR_H_
15#define PC_STATS_COLLECTOR_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
Yves Gerey3e707812018-11-28 16:47:49 +010017#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020018
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019#include <map>
Taylor Brandstetterc3928662018-02-23 13:04:51 -080020#include <memory>
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000021#include <string>
Steve Anton36b29d12017-10-30 09:57:42 -070022#include <utility>
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000023#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
Steve Anton10542f22019-01-11 09:11:00 -080025#include "api/media_stream_interface.h"
26#include "api/peer_connection_interface.h"
27#include "api/stats_types.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "pc/peer_connection_internal.h"
Yves Gerey3e707812018-11-28 16:47:49 +010030#include "rtc_base/network_constants.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "rtc_base/ssl_certificate.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033namespace webrtc {
34
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000035// Conversion function to convert candidate type string to the corresponding one
36// from enum RTCStatsIceCandidateType.
37const char* IceCandidateTypeToStatsType(const std::string& candidate_type);
38
39// Conversion function to convert adapter type to report string which are more
40// fitting to the general style of http://w3c.github.io/webrtc-stats. This is
41// only used by stats collector.
42const char* AdapterTypeToStatsType(rtc::AdapterType type);
43
jbauchbe24c942015-06-22 15:06:43 -070044// A mapping between track ids and their StatsReport.
45typedef std::map<std::string, StatsReport*> TrackIdMap;
46
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047class StatsCollector {
48 public:
deadbeefab9b2d12015-10-14 11:33:11 -070049 // The caller is responsible for ensuring that the pc outlives the
tommi@webrtc.org03505bc2014-07-14 20:15:26 +000050 // StatsCollector instance.
Steve Anton2d8609c2018-01-23 16:38:46 -080051 explicit StatsCollector(PeerConnectionInternal* pc);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +000052 virtual ~StatsCollector();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
54 // Adds a MediaStream with tracks that can be used as a |selector| in a call
55 // to GetStats.
56 void AddStream(MediaStreamInterface* stream);
Harald Alvestrand75ceef22018-01-04 15:26:13 +010057 void AddTrack(MediaStreamTrackInterface* track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000059 // Adds a local audio track that is used for getting some voice statistics.
Peter Boström0c4e06b2015-10-07 12:23:21 +020060 void AddLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000061
62 // Removes a local audio tracks that is used for getting some voice
63 // statistics.
Peter Boström0c4e06b2015-10-07 12:23:21 +020064 void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000065
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 // Gather statistics from the session and store them for future use.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000067 void UpdateStats(PeerConnectionInterface::StatsOutputLevel level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068
69 // Gets a StatsReports of the last collected stats. Note that UpdateStats must
70 // be called before this function to get the most recent stats. |selector| is
71 // a track label or empty string. The most recent reports are stored in
72 // |reports|.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +000073 // TODO(tommi): Change this contract to accept a callback object instead
74 // of filling in |reports|. As is, there's a requirement that the caller
75 // uses |reports| immediately without allowing any async activity on
76 // the thread (message handling etc) and then discard the results.
Yves Gerey665174f2018-06-19 15:03:05 +020077 void GetStats(MediaStreamTrackInterface* track, StatsReports* reports);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000079 // Prepare a local or remote SSRC report for the given ssrc. Used internally
wu@webrtc.org97077a32013-10-25 21:18:33 +000080 // in the ExtractStatsFromList template.
Peter Boström0c4e06b2015-10-07 12:23:21 +020081 StatsReport* PrepareReport(bool local,
82 uint32_t ssrc,
Steve Antonefe4c922019-03-27 10:26:06 -070083 const std::string& track_id,
Peter Boström0c4e06b2015-10-07 12:23:21 +020084 const StatsReport::Id& transport_id,
85 StatsReport::Direction direction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
Alex Narestbbeb1092019-08-16 11:49:04 +020087 StatsReport* PrepareADMReport();
88
zhihuange9e94c32016-11-04 11:38:15 -070089 // A track is invalid if there is no report data for it.
90 bool IsValidTrack(const std::string& track_id);
91
xians@webrtc.org01bda202014-07-09 07:38:38 +000092 // Method used by the unittest to force a update of stats since UpdateStats()
93 // that occur less than kMinGatherStatsPeriod number of ms apart will be
94 // ignored.
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +000095 void ClearUpdateStatsCacheForTest();
xians@webrtc.org01bda202014-07-09 07:38:38 +000096
Niels Möllerac0a4cb2019-10-09 15:01:33 +020097 bool UseStandardBytesStats() const { return use_standard_bytes_stats_; }
98
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 private:
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000100 friend class StatsCollectorTest;
101
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000102 // Overridden in unit tests to fake timing.
103 virtual double GetTimeNow();
104
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 bool CopySelectedReports(const std::string& selector, StatsReports* reports);
106
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000107 // Helper method for creating IceCandidate report. |is_local| indicates
108 // whether this candidate is local or remote.
Qingsi Wang72a43a12018-02-20 16:03:18 -0800109 StatsReport* AddCandidateReport(
110 const cricket::CandidateStats& candidate_stats,
111 bool local);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000112
wu@webrtc.org4551b792013-10-09 15:37:36 +0000113 // Adds a report for this certificate and every certificate in its chain, and
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800114 // returns the leaf certificate's report (|cert_stats|'s report).
115 StatsReport* AddCertificateReports(
116 std::unique_ptr<rtc::SSLCertificateStats> cert_stats);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000117
118 StatsReport* AddConnectionInfoReport(const std::string& content_name,
Yves Gerey665174f2018-06-19 15:03:05 +0200119 int component,
120 int connection_id,
121 const StatsReport::Id& channel_report_id,
122 const cricket::ConnectionInfo& info);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000123
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000124 void ExtractDataInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 void ExtractSessionInfo();
stefanf79ade12017-06-02 06:44:03 -0700126 void ExtractBweInfo();
Steve Antonb8867112018-02-13 10:07:54 -0800127 void ExtractMediaInfo();
nissefcc640f2016-04-01 01:10:42 -0700128 void ExtractSenderInfo();
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000129 webrtc::StatsReport* GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000130 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000131 StatsReport::Direction direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000132
133 // Helper method to get stats from the local audio tracks.
Ivo Creusenae026092017-11-20 13:07:16 +0100134 void UpdateStatsFromExistingLocalAudioTracks(bool has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000135 void UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae026092017-11-20 13:07:16 +0100136 StatsReport* report,
137 bool has_remote_tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138
jbauchbe24c942015-06-22 15:06:43 -0700139 // Helper method to update the timestamp of track records.
140 void UpdateTrackReports();
141
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000142 // A collection for all of our stats reports.
143 StatsCollection reports_;
jbauchbe24c942015-06-22 15:06:43 -0700144 TrackIdMap track_ids_;
deadbeefab9b2d12015-10-14 11:33:11 -0700145 // Raw pointer to the peer connection the statistics are gathered from.
Steve Anton2d8609c2018-01-23 16:38:46 -0800146 PeerConnectionInternal* const pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 double stats_gathering_started_;
Niels Möllerac0a4cb2019-10-09 15:01:33 +0200148 const bool use_standard_bytes_stats_;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000149
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000150 // TODO(tommi): We appear to be holding on to raw pointers to reference
151 // counted objects? We should be using scoped_refptr here.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200152 typedef std::vector<std::pair<AudioTrackInterface*, uint32_t> >
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000153 LocalAudioTrackVector;
154 LocalAudioTrackVector local_audio_tracks_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155};
156
157} // namespace webrtc
158
Steve Anton10542f22019-01-11 09:11:00 -0800159#endif // PC_STATS_COLLECTOR_H_