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