stats_collector.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * 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.
  9. */
  10. // This file contains a class used for gathering statistics from an ongoing
  11. // libjingle PeerConnection.
  12. #ifndef PC_STATS_COLLECTOR_H_
  13. #define PC_STATS_COLLECTOR_H_
  14. #include <stdint.h>
  15. #include <map>
  16. #include <memory>
  17. #include <string>
  18. #include <utility>
  19. #include <vector>
  20. #include "api/media_stream_interface.h"
  21. #include "api/peer_connection_interface.h"
  22. #include "api/stats_types.h"
  23. #include "p2p/base/port.h"
  24. #include "pc/peer_connection_internal.h"
  25. #include "rtc_base/network_constants.h"
  26. #include "rtc_base/ssl_certificate.h"
  27. namespace webrtc {
  28. // Conversion function to convert candidate type string to the corresponding one
  29. // from enum RTCStatsIceCandidateType.
  30. const char* IceCandidateTypeToStatsType(const std::string& candidate_type);
  31. // Conversion function to convert adapter type to report string which are more
  32. // fitting to the general style of http://w3c.github.io/webrtc-stats. This is
  33. // only used by stats collector.
  34. const char* AdapterTypeToStatsType(rtc::AdapterType type);
  35. // A mapping between track ids and their StatsReport.
  36. typedef std::map<std::string, StatsReport*> TrackIdMap;
  37. class StatsCollector {
  38. public:
  39. // The caller is responsible for ensuring that the pc outlives the
  40. // StatsCollector instance.
  41. explicit StatsCollector(PeerConnectionInternal* pc);
  42. virtual ~StatsCollector();
  43. // Adds a MediaStream with tracks that can be used as a |selector| in a call
  44. // to GetStats.
  45. void AddStream(MediaStreamInterface* stream);
  46. void AddTrack(MediaStreamTrackInterface* track);
  47. // Adds a local audio track that is used for getting some voice statistics.
  48. void AddLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
  49. // Removes a local audio tracks that is used for getting some voice
  50. // statistics.
  51. void RemoveLocalAudioTrack(AudioTrackInterface* audio_track, uint32_t ssrc);
  52. // Gather statistics from the session and store them for future use.
  53. void UpdateStats(PeerConnectionInterface::StatsOutputLevel level);
  54. // Gets a StatsReports of the last collected stats. Note that UpdateStats must
  55. // be called before this function to get the most recent stats. |selector| is
  56. // a track label or empty string. The most recent reports are stored in
  57. // |reports|.
  58. // TODO(tommi): Change this contract to accept a callback object instead
  59. // of filling in |reports|. As is, there's a requirement that the caller
  60. // uses |reports| immediately without allowing any async activity on
  61. // the thread (message handling etc) and then discard the results.
  62. void GetStats(MediaStreamTrackInterface* track, StatsReports* reports);
  63. // Prepare a local or remote SSRC report for the given ssrc. Used internally
  64. // in the ExtractStatsFromList template.
  65. StatsReport* PrepareReport(bool local,
  66. uint32_t ssrc,
  67. const std::string& track_id,
  68. const StatsReport::Id& transport_id,
  69. StatsReport::Direction direction);
  70. StatsReport* PrepareADMReport();
  71. // A track is invalid if there is no report data for it.
  72. bool IsValidTrack(const std::string& track_id);
  73. // Method used by the unittest to force a update of stats since UpdateStats()
  74. // that occur less than kMinGatherStatsPeriod number of ms apart will be
  75. // ignored.
  76. void ClearUpdateStatsCacheForTest();
  77. bool UseStandardBytesStats() const { return use_standard_bytes_stats_; }
  78. private:
  79. friend class StatsCollectorTest;
  80. // Overridden in unit tests to fake timing.
  81. virtual double GetTimeNow();
  82. bool CopySelectedReports(const std::string& selector, StatsReports* reports);
  83. // Helper method for creating IceCandidate report. |is_local| indicates
  84. // whether this candidate is local or remote.
  85. StatsReport* AddCandidateReport(
  86. const cricket::CandidateStats& candidate_stats,
  87. bool local);
  88. // Adds a report for this certificate and every certificate in its chain, and
  89. // returns the leaf certificate's report (|cert_stats|'s report).
  90. StatsReport* AddCertificateReports(
  91. std::unique_ptr<rtc::SSLCertificateStats> cert_stats);
  92. StatsReport* AddConnectionInfoReport(const std::string& content_name,
  93. int component,
  94. int connection_id,
  95. const StatsReport::Id& channel_report_id,
  96. const cricket::ConnectionInfo& info);
  97. void ExtractDataInfo();
  98. void ExtractSessionInfo();
  99. void ExtractBweInfo();
  100. void ExtractMediaInfo();
  101. void ExtractSenderInfo();
  102. webrtc::StatsReport* GetReport(const StatsReport::StatsType& type,
  103. const std::string& id,
  104. StatsReport::Direction direction);
  105. // Helper method to get stats from the local audio tracks.
  106. void UpdateStatsFromExistingLocalAudioTracks(bool has_remote_tracks);
  107. void UpdateReportFromAudioTrack(AudioTrackInterface* track,
  108. StatsReport* report,
  109. bool has_remote_tracks);
  110. // Helper method to update the timestamp of track records.
  111. void UpdateTrackReports();
  112. // A collection for all of our stats reports.
  113. StatsCollection reports_;
  114. TrackIdMap track_ids_;
  115. // Raw pointer to the peer connection the statistics are gathered from.
  116. PeerConnectionInternal* const pc_;
  117. double stats_gathering_started_;
  118. const bool use_standard_bytes_stats_;
  119. // TODO(tommi): We appear to be holding on to raw pointers to reference
  120. // counted objects? We should be using scoped_refptr here.
  121. typedef std::vector<std::pair<AudioTrackInterface*, uint32_t> >
  122. LocalAudioTrackVector;
  123. LocalAudioTrackVector local_audio_tracks_;
  124. };
  125. } // namespace webrtc
  126. #endif // PC_STATS_COLLECTOR_H_