blob: 3c0aaf9b8ec86f78bf365f6f72acc7d405b2b68a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// This file contains a class used for gathering statistics from an ongoing
29// libjingle PeerConnection.
30
31#ifndef TALK_APP_WEBRTC_STATSCOLLECTOR_H_
32#define TALK_APP_WEBRTC_STATSCOLLECTOR_H_
33
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include <map>
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000035#include <string>
36#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
38#include "talk/app/webrtc/mediastreaminterface.h"
decurtis@webrtc.org487a4442015-01-15 22:55:07 +000039#include "talk/app/webrtc/mediastreamsignaling.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000040#include "talk/app/webrtc/peerconnectioninterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#include "talk/app/webrtc/statstypes.h"
42#include "talk/app/webrtc/webrtcsession.h"
43
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044namespace webrtc {
45
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000046// Conversion function to convert candidate type string to the corresponding one
47// from enum RTCStatsIceCandidateType.
48const char* IceCandidateTypeToStatsType(const std::string& candidate_type);
49
50// Conversion function to convert adapter type to report string which are more
51// fitting to the general style of http://w3c.github.io/webrtc-stats. This is
52// only used by stats collector.
53const char* AdapterTypeToStatsType(rtc::AdapterType type);
54
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055class StatsCollector {
56 public:
tommi@webrtc.org03505bc2014-07-14 20:15:26 +000057 // The caller is responsible for ensuring that the session outlives the
58 // StatsCollector instance.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000059 explicit StatsCollector(WebRtcSession* session);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +000060 virtual ~StatsCollector();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061
62 // Adds a MediaStream with tracks that can be used as a |selector| in a call
63 // to GetStats.
64 void AddStream(MediaStreamInterface* stream);
65
henrike@webrtc.org40b3b682014-03-03 18:30:11 +000066 // Adds a local audio track that is used for getting some voice statistics.
67 void AddLocalAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc);
68
69 // Removes a local audio tracks that is used for getting some voice
70 // statistics.
71 void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc);
72
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 // Gather statistics from the session and store them for future use.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000074 void UpdateStats(PeerConnectionInterface::StatsOutputLevel level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075
76 // Gets a StatsReports of the last collected stats. Note that UpdateStats must
77 // be called before this function to get the most recent stats. |selector| is
78 // a track label or empty string. The most recent reports are stored in
79 // |reports|.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +000080 // TODO(tommi): Change this contract to accept a callback object instead
81 // of filling in |reports|. As is, there's a requirement that the caller
82 // uses |reports| immediately without allowing any async activity on
83 // the thread (message handling etc) and then discard the results.
84 void GetStats(MediaStreamTrackInterface* track,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000085 StatsReports* reports);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000087 // Prepare a local or remote SSRC report for the given ssrc. Used internally
wu@webrtc.org97077a32013-10-25 21:18:33 +000088 // in the ExtractStatsFromList template.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000089 StatsReport* PrepareReport(bool local, uint32 ssrc,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000090 const StatsReport::Id& transport_id, StatsReport::Direction direction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091
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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 private:
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000098 friend class StatsCollectorTest;
99
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000100 // Overridden in unit tests to fake timing.
101 virtual double GetTimeNow();
102
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 bool CopySelectedReports(const std::string& selector, StatsReports* reports);
104
wu@webrtc.org4551b792013-10-09 15:37:36 +0000105 // Helper method for AddCertificateReports.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000106 StatsReport* AddOneCertificateReport(
107 const rtc::SSLCertificate* cert, const StatsReport* issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000108
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000109 // Helper method for creating IceCandidate report. |is_local| indicates
110 // whether this candidate is local or remote.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000111 StatsReport* AddCandidateReport(const cricket::Candidate& candidate,
112 bool local);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000113
wu@webrtc.org4551b792013-10-09 15:37:36 +0000114 // Adds a report for this certificate and every certificate in its chain, and
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000115 // returns the leaf certificate's report.
116 StatsReport* AddCertificateReports(const rtc::SSLCertificate* cert);
117
118 StatsReport* AddConnectionInfoReport(const std::string& content_name,
119 int component, int connection_id,
120 const StatsReport::Id& channel_report_id,
121 const cricket::ConnectionInfo& info);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000122
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000123 void ExtractDataInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 void ExtractSessionInfo();
125 void ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000126 void ExtractVideoInfo(PeerConnectionInterface::StatsOutputLevel level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 void BuildSsrcToTransportId();
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000128 webrtc::StatsReport* GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000129 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000130 StatsReport::Direction direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000131
132 // Helper method to get stats from the local audio tracks.
133 void UpdateStatsFromExistingLocalAudioTracks();
134 void UpdateReportFromAudioTrack(AudioTrackInterface* track,
135 StatsReport* report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000137 // Helper method to get the id for the track identified by ssrc.
138 // |direction| tells if the track is for sending or receiving.
139 bool GetTrackIdBySsrc(uint32 ssrc, std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000140 StatsReport::Direction direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000141
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000142 // A collection for all of our stats reports.
143 StatsCollection reports_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 // Raw pointer to the session the statistics are gathered from.
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000145 WebRtcSession* const session_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 double stats_gathering_started_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 cricket::ProxyTransportMap proxy_to_transport_;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000148
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000149 // TODO(tommi): We appear to be holding on to raw pointers to reference
150 // counted objects? We should be using scoped_refptr here.
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000151 typedef std::vector<std::pair<AudioTrackInterface*, uint32> >
152 LocalAudioTrackVector;
153 LocalAudioTrackVector local_audio_tracks_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154};
155
156} // namespace webrtc
157
158#endif // TALK_APP_WEBRTC_STATSCOLLECTOR_H_