stats_collector.h 6.4 KB

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