blob: b3d5cb956f38716133ee342a457cd851266a4286 [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
Steve Anton10542f22019-01-11 17:11:0014#ifndef PC_STATS_COLLECTOR_H_
15#define PC_STATS_COLLECTOR_H_
henrike@webrtc.org28e20752013-07-10 00:45:3616
Yves Gerey3e707812018-11-28 15:47:4917#include <stdint.h>
henrike@webrtc.org28e20752013-07-10 00:45:3618#include <map>
Taylor Brandstetterc3928662018-02-23 21:04:5119#include <memory>
henrike@webrtc.org40b3b682014-03-03 18:30:1120#include <string>
Steve Anton36b29d12017-10-30 16:57:4221#include <utility>
henrike@webrtc.org40b3b682014-03-03 18:30:1122#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:3623
Steve Anton10542f22019-01-11 17:11:0024#include "api/media_stream_interface.h"
25#include "api/peer_connection_interface.h"
26#include "api/stats_types.h"
Yves Gerey3e707812018-11-28 15:47:4927#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 17:11:0028#include "pc/peer_connection_internal.h"
Yves Gerey3e707812018-11-28 15:47:4929#include "rtc_base/network_constants.h"
Steve Anton10542f22019-01-11 17:11:0030#include "rtc_base/ssl_certificate.h"
henrike@webrtc.org28e20752013-07-10 00:45:3631
henrike@webrtc.org28e20752013-07-10 00:45:3632namespace webrtc {
33
guoweis@webrtc.org950c5182014-12-16 23:01:3134// Conversion function to convert candidate type string to the corresponding one
35// from enum RTCStatsIceCandidateType.
36const char* IceCandidateTypeToStatsType(const std::string& candidate_type);
37
38// Conversion function to convert adapter type to report string which are more
39// fitting to the general style of http://w3c.github.io/webrtc-stats. This is
40// only used by stats collector.
41const char* AdapterTypeToStatsType(rtc::AdapterType type);
42
jbauchbe24c942015-06-22 22:06:4343// A mapping between track ids and their StatsReport.
44typedef std::map<std::string, StatsReport*> TrackIdMap;
45
henrike@webrtc.org28e20752013-07-10 00:45:3646class StatsCollector {
47 public:
deadbeefab9b2d12015-10-14 18:33:1148 // The caller is responsible for ensuring that the pc outlives the
tommi@webrtc.org03505bc2014-07-14 20:15:2649 // StatsCollector instance.
Steve Anton2d8609c2018-01-24 00:38:4650 explicit StatsCollector(PeerConnectionInternal* pc);
tommi@webrtc.org03505bc2014-07-14 20:15:2651 virtual ~StatsCollector();
henrike@webrtc.org28e20752013-07-10 00:45:3652
53 // Adds a MediaStream with tracks that can be used as a |selector| in a call
54 // to GetStats.
55 void AddStream(MediaStreamInterface* stream);
Harald Alvestrand75ceef22018-01-04 14:26:1356 void AddTrack(MediaStreamTrackInterface* track);
henrike@webrtc.org28e20752013-07-10 00:45:3657
henrike@webrtc.org40b3b682014-03-03 18:30:1158 // Adds a local audio track that is used for getting some voice statistics.
Peter Boström0c4e06b2015-10-07 10:23:2159 void AddLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:1160
61 // Removes a local audio tracks that is used for getting some voice
62 // statistics.
Peter Boström0c4e06b2015-10-07 10:23:2163 void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:1164
henrike@webrtc.org28e20752013-07-10 00:45:3665 // Gather statistics from the session and store them for future use.
wu@webrtc.orgb9a088b2014-02-13 23:18:4966 void UpdateStats(PeerConnectionInterface::StatsOutputLevel level);
henrike@webrtc.org28e20752013-07-10 00:45:3667
68 // Gets a StatsReports of the last collected stats. Note that UpdateStats must
69 // be called before this function to get the most recent stats. |selector| is
70 // a track label or empty string. The most recent reports are stored in
71 // |reports|.
tommi@webrtc.org5b06b062014-08-15 08:38:3072 // TODO(tommi): Change this contract to accept a callback object instead
73 // of filling in |reports|. As is, there's a requirement that the caller
74 // uses |reports| immediately without allowing any async activity on
75 // the thread (message handling etc) and then discard the results.
Yves Gerey665174f2018-06-19 13:03:0576 void GetStats(MediaStreamTrackInterface* track, StatsReports* reports);
henrike@webrtc.org28e20752013-07-10 00:45:3677
tommi@webrtc.org4fb7e252015-01-21 11:36:1878 // Prepare a local or remote SSRC report for the given ssrc. Used internally
wu@webrtc.org97077a32013-10-25 21:18:3379 // in the ExtractStatsFromList template.
Peter Boström0c4e06b2015-10-07 10:23:2180 StatsReport* PrepareReport(bool local,
81 uint32_t ssrc,
Steve Antonefe4c922019-03-27 17:26:0682 const std::string& track_id,
Peter Boström0c4e06b2015-10-07 10:23:2183 const StatsReport::Id& transport_id,
84 StatsReport::Direction direction);
henrike@webrtc.org28e20752013-07-10 00:45:3685
zhihuange9e94c32016-11-04 18:38:1586 // A track is invalid if there is no report data for it.
87 bool IsValidTrack(const std::string& track_id);
88
xians@webrtc.org01bda202014-07-09 07:38:3889 // Method used by the unittest to force a update of stats since UpdateStats()
90 // that occur less than kMinGatherStatsPeriod number of ms apart will be
91 // ignored.
tommi@webrtc.org69bc5a32014-12-15 09:44:4892 void ClearUpdateStatsCacheForTest();
xians@webrtc.org01bda202014-07-09 07:38:3893
henrike@webrtc.org28e20752013-07-10 00:45:3694 private:
guoweis@webrtc.org950c5182014-12-16 23:01:3195 friend class StatsCollectorTest;
96
decurtis@webrtc.org322a5642015-02-03 22:09:3797 // Overridden in unit tests to fake timing.
98 virtual double GetTimeNow();
99
henrike@webrtc.org28e20752013-07-10 00:45:36100 bool CopySelectedReports(const std::string& selector, StatsReports* reports);
101
guoweis@webrtc.org950c5182014-12-16 23:01:31102 // Helper method for creating IceCandidate report. |is_local| indicates
103 // whether this candidate is local or remote.
Qingsi Wang72a43a12018-02-21 00:03:18104 StatsReport* AddCandidateReport(
105 const cricket::CandidateStats& candidate_stats,
106 bool local);
guoweis@webrtc.org950c5182014-12-16 23:01:31107
wu@webrtc.org4551b792013-10-09 15:37:36108 // Adds a report for this certificate and every certificate in its chain, and
Taylor Brandstetterc3928662018-02-23 21:04:51109 // returns the leaf certificate's report (|cert_stats|'s report).
110 StatsReport* AddCertificateReports(
111 std::unique_ptr<rtc::SSLCertificateStats> cert_stats);
tommi@webrtc.orgd3900292015-03-12 16:35:55112
113 StatsReport* AddConnectionInfoReport(const std::string& content_name,
Yves Gerey665174f2018-06-19 13:03:05114 int component,
115 int connection_id,
116 const StatsReport::Id& channel_report_id,
117 const cricket::ConnectionInfo& info);
wu@webrtc.org4551b792013-10-09 15:37:36118
decurtis@webrtc.org487a4442015-01-15 22:55:07119 void ExtractDataInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36120 void ExtractSessionInfo();
stefanf79ade12017-06-02 13:44:03121 void ExtractBweInfo();
Steve Antonb8867112018-02-13 18:07:54122 void ExtractMediaInfo();
nissefcc640f2016-04-01 08:10:42123 void ExtractSenderInfo();
tommi@webrtc.org4fb7e252015-01-21 11:36:18124 webrtc::StatsReport* GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05125 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18126 StatsReport::Direction direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11127
128 // Helper method to get stats from the local audio tracks.
Ivo Creusenae0260962017-11-20 12:07:16129 void UpdateStatsFromExistingLocalAudioTracks(bool has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11130 void UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae0260962017-11-20 12:07:16131 StatsReport* report,
132 bool has_remote_tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36133
jbauchbe24c942015-06-22 22:06:43134 // Helper method to update the timestamp of track records.
135 void UpdateTrackReports();
136
tommi@webrtc.org4fb7e252015-01-21 11:36:18137 // A collection for all of our stats reports.
138 StatsCollection reports_;
jbauchbe24c942015-06-22 22:06:43139 TrackIdMap track_ids_;
deadbeefab9b2d12015-10-14 18:33:11140 // Raw pointer to the peer connection the statistics are gathered from.
Steve Anton2d8609c2018-01-24 00:38:46141 PeerConnectionInternal* const pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36142 double stats_gathering_started_;
henrike@webrtc.org40b3b682014-03-03 18:30:11143
tommi@webrtc.orgd3900292015-03-12 16:35:55144 // TODO(tommi): We appear to be holding on to raw pointers to reference
145 // counted objects? We should be using scoped_refptr here.
Peter Boström0c4e06b2015-10-07 10:23:21146 typedef std::vector<std::pair<AudioTrackInterface*, uint32_t> >
henrike@webrtc.org40b3b682014-03-03 18:30:11147 LocalAudioTrackVector;
148 LocalAudioTrackVector local_audio_tracks_;
henrike@webrtc.org28e20752013-07-10 00:45:36149};
150
151} // namespace webrtc
152
Steve Anton10542f22019-01-11 17:11:00153#endif // PC_STATS_COLLECTOR_H_