You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
5.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2018 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. #include "pc/rtc_stats_traversal.h"
  11. #include <memory>
  12. #include <string>
  13. #include <utility>
  14. #include <vector>
  15. #include "api/stats/rtcstats_objects.h"
  16. #include "rtc_base/checks.h"
  17. namespace webrtc {
  18. namespace {
  19. void TraverseAndTakeVisitedStats(RTCStatsReport* report,
  20. RTCStatsReport* visited_report,
  21. const std::string& current_id) {
  22. // Mark current stats object as visited by moving it |report| to
  23. // |visited_report|.
  24. std::unique_ptr<const RTCStats> current = report->Take(current_id);
  25. if (!current) {
  26. // This node has already been visited (or it is an invalid id).
  27. return;
  28. }
  29. std::vector<const std::string*> neighbor_ids =
  30. GetStatsReferencedIds(*current);
  31. visited_report->AddStats(std::move(current));
  32. // Recursively traverse all neighbors.
  33. for (const auto* neighbor_id : neighbor_ids) {
  34. TraverseAndTakeVisitedStats(report, visited_report, *neighbor_id);
  35. }
  36. }
  37. void AddIdIfDefined(const RTCStatsMember<std::string>& id,
  38. std::vector<const std::string*>* neighbor_ids) {
  39. if (id.is_defined())
  40. neighbor_ids->push_back(&(*id));
  41. }
  42. void AddIdsIfDefined(const RTCStatsMember<std::vector<std::string>>& ids,
  43. std::vector<const std::string*>* neighbor_ids) {
  44. if (ids.is_defined()) {
  45. for (const std::string& id : *ids)
  46. neighbor_ids->push_back(&id);
  47. }
  48. }
  49. } // namespace
  50. rtc::scoped_refptr<RTCStatsReport> TakeReferencedStats(
  51. rtc::scoped_refptr<RTCStatsReport> report,
  52. const std::vector<std::string>& ids) {
  53. rtc::scoped_refptr<RTCStatsReport> result =
  54. RTCStatsReport::Create(report->timestamp_us());
  55. for (const auto& id : ids) {
  56. TraverseAndTakeVisitedStats(report.get(), result.get(), id);
  57. }
  58. return result;
  59. }
  60. std::vector<const std::string*> GetStatsReferencedIds(const RTCStats& stats) {
  61. std::vector<const std::string*> neighbor_ids;
  62. const char* type = stats.type();
  63. if (type == RTCCertificateStats::kType) {
  64. const auto& certificate = static_cast<const RTCCertificateStats&>(stats);
  65. AddIdIfDefined(certificate.issuer_certificate_id, &neighbor_ids);
  66. } else if (type == RTCCodecStats::kType) {
  67. // RTCCodecStats does not have any neighbor references.
  68. } else if (type == RTCDataChannelStats::kType) {
  69. // RTCDataChannelStats does not have any neighbor references.
  70. } else if (type == RTCIceCandidatePairStats::kType) {
  71. const auto& candidate_pair =
  72. static_cast<const RTCIceCandidatePairStats&>(stats);
  73. AddIdIfDefined(candidate_pair.transport_id, &neighbor_ids);
  74. AddIdIfDefined(candidate_pair.local_candidate_id, &neighbor_ids);
  75. AddIdIfDefined(candidate_pair.remote_candidate_id, &neighbor_ids);
  76. } else if (type == RTCLocalIceCandidateStats::kType ||
  77. type == RTCRemoteIceCandidateStats::kType) {
  78. const auto& local_or_remote_candidate =
  79. static_cast<const RTCIceCandidateStats&>(stats);
  80. AddIdIfDefined(local_or_remote_candidate.transport_id, &neighbor_ids);
  81. } else if (type == RTCMediaStreamStats::kType) {
  82. const auto& stream = static_cast<const RTCMediaStreamStats&>(stats);
  83. AddIdsIfDefined(stream.track_ids, &neighbor_ids);
  84. } else if (type == RTCMediaStreamTrackStats::kType) {
  85. const auto& track = static_cast<const RTCMediaStreamTrackStats&>(stats);
  86. AddIdIfDefined(track.media_source_id, &neighbor_ids);
  87. } else if (type == RTCPeerConnectionStats::kType) {
  88. // RTCPeerConnectionStats does not have any neighbor references.
  89. } else if (type == RTCInboundRTPStreamStats::kType ||
  90. type == RTCOutboundRTPStreamStats::kType) {
  91. const auto& rtp = static_cast<const RTCRTPStreamStats&>(stats);
  92. AddIdIfDefined(rtp.associate_stats_id, &neighbor_ids);
  93. AddIdIfDefined(rtp.track_id, &neighbor_ids);
  94. AddIdIfDefined(rtp.transport_id, &neighbor_ids);
  95. AddIdIfDefined(rtp.codec_id, &neighbor_ids);
  96. if (type == RTCOutboundRTPStreamStats::kType) {
  97. const auto& outbound_rtp =
  98. static_cast<const RTCOutboundRTPStreamStats&>(stats);
  99. AddIdIfDefined(outbound_rtp.media_source_id, &neighbor_ids);
  100. }
  101. } else if (type == RTCRemoteInboundRtpStreamStats::kType) {
  102. const auto& remote_inbound_rtp =
  103. static_cast<const RTCRemoteInboundRtpStreamStats&>(stats);
  104. AddIdIfDefined(remote_inbound_rtp.transport_id, &neighbor_ids);
  105. AddIdIfDefined(remote_inbound_rtp.codec_id, &neighbor_ids);
  106. AddIdIfDefined(remote_inbound_rtp.local_id, &neighbor_ids);
  107. } else if (type == RTCAudioSourceStats::kType ||
  108. type == RTCVideoSourceStats::kType) {
  109. // RTC[Audio/Video]SourceStats does not have any neighbor references.
  110. } else if (type == RTCTransportStats::kType) {
  111. // RTCTransportStats does not have any neighbor references.
  112. const auto& transport = static_cast<const RTCTransportStats&>(stats);
  113. AddIdIfDefined(transport.rtcp_transport_stats_id, &neighbor_ids);
  114. AddIdIfDefined(transport.selected_candidate_pair_id, &neighbor_ids);
  115. AddIdIfDefined(transport.local_certificate_id, &neighbor_ids);
  116. AddIdIfDefined(transport.remote_certificate_id, &neighbor_ids);
  117. } else {
  118. RTC_NOTREACHED() << "Unrecognized type: " << type;
  119. }
  120. return neighbor_ids;
  121. }
  122. } // namespace webrtc