Henrik Boström | b619936 | 2018-03-12 09:27:55 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "pc/rtc_stats_traversal.h" |
Henrik Boström | b619936 | 2018-03-12 09:27:55 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
| 14 | #include <string> |
| 15 | #include <utility> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "api/stats/rtcstats_objects.h" |
| 19 | #include "rtc_base/checks.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | void TraverseAndTakeVisitedStats(RTCStatsReport* report, |
| 26 | RTCStatsReport* visited_report, |
| 27 | const std::string& current_id) { |
| 28 | // Mark current stats object as visited by moving it |report| to |
| 29 | // |visited_report|. |
| 30 | std::unique_ptr<const RTCStats> current = report->Take(current_id); |
| 31 | if (!current) { |
| 32 | // This node has already been visited (or it is an invalid id). |
| 33 | return; |
| 34 | } |
| 35 | std::vector<const std::string*> neighbor_ids = |
| 36 | GetStatsReferencedIds(*current); |
| 37 | visited_report->AddStats(std::move(current)); |
| 38 | |
| 39 | // Recursively traverse all neighbors. |
| 40 | for (const auto* neighbor_id : neighbor_ids) { |
| 41 | TraverseAndTakeVisitedStats(report, visited_report, *neighbor_id); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void AddIdIfDefined(const RTCStatsMember<std::string>& id, |
| 46 | std::vector<const std::string*>* neighbor_ids) { |
| 47 | if (id.is_defined()) |
| 48 | neighbor_ids->push_back(&(*id)); |
| 49 | } |
| 50 | |
| 51 | void AddIdsIfDefined(const RTCStatsMember<std::vector<std::string>>& ids, |
| 52 | std::vector<const std::string*>* neighbor_ids) { |
| 53 | if (ids.is_defined()) { |
| 54 | for (const std::string& id : *ids) |
| 55 | neighbor_ids->push_back(&id); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
| 61 | rtc::scoped_refptr<RTCStatsReport> TakeReferencedStats( |
| 62 | rtc::scoped_refptr<RTCStatsReport> report, |
| 63 | const std::vector<std::string>& ids) { |
Henrik Boström | 5b3541f | 2018-03-19 12:52:56 | [diff] [blame] | 64 | rtc::scoped_refptr<RTCStatsReport> result = |
| 65 | RTCStatsReport::Create(report->timestamp_us()); |
Henrik Boström | b619936 | 2018-03-12 09:27:55 | [diff] [blame] | 66 | for (const auto& id : ids) { |
| 67 | TraverseAndTakeVisitedStats(report.get(), result.get(), id); |
| 68 | } |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | std::vector<const std::string*> GetStatsReferencedIds(const RTCStats& stats) { |
| 73 | std::vector<const std::string*> neighbor_ids; |
| 74 | const char* type = stats.type(); |
| 75 | if (type == RTCCertificateStats::kType) { |
| 76 | const auto& certificate = static_cast<const RTCCertificateStats&>(stats); |
| 77 | AddIdIfDefined(certificate.issuer_certificate_id, &neighbor_ids); |
| 78 | } else if (type == RTCCodecStats::kType) { |
Philipp Hancke | 95157a0 | 2020-11-16 19:08:27 | [diff] [blame] | 79 | const auto& codec = static_cast<const RTCCodecStats&>(stats); |
| 80 | AddIdIfDefined(codec.transport_id, &neighbor_ids); |
Henrik Boström | b619936 | 2018-03-12 09:27:55 | [diff] [blame] | 81 | } else if (type == RTCDataChannelStats::kType) { |
| 82 | // RTCDataChannelStats does not have any neighbor references. |
| 83 | } else if (type == RTCIceCandidatePairStats::kType) { |
| 84 | const auto& candidate_pair = |
| 85 | static_cast<const RTCIceCandidatePairStats&>(stats); |
| 86 | AddIdIfDefined(candidate_pair.transport_id, &neighbor_ids); |
| 87 | AddIdIfDefined(candidate_pair.local_candidate_id, &neighbor_ids); |
| 88 | AddIdIfDefined(candidate_pair.remote_candidate_id, &neighbor_ids); |
| 89 | } else if (type == RTCLocalIceCandidateStats::kType || |
| 90 | type == RTCRemoteIceCandidateStats::kType) { |
| 91 | const auto& local_or_remote_candidate = |
| 92 | static_cast<const RTCIceCandidateStats&>(stats); |
| 93 | AddIdIfDefined(local_or_remote_candidate.transport_id, &neighbor_ids); |
| 94 | } else if (type == RTCMediaStreamStats::kType) { |
| 95 | const auto& stream = static_cast<const RTCMediaStreamStats&>(stats); |
| 96 | AddIdsIfDefined(stream.track_ids, &neighbor_ids); |
| 97 | } else if (type == RTCMediaStreamTrackStats::kType) { |
Henrik Boström | 646fda0 | 2019-05-22 13:49:42 | [diff] [blame] | 98 | const auto& track = static_cast<const RTCMediaStreamTrackStats&>(stats); |
| 99 | AddIdIfDefined(track.media_source_id, &neighbor_ids); |
Henrik Boström | b619936 | 2018-03-12 09:27:55 | [diff] [blame] | 100 | } else if (type == RTCPeerConnectionStats::kType) { |
| 101 | // RTCPeerConnectionStats does not have any neighbor references. |
Alessio Bazzica | f7b1b95 | 2021-03-23 16:23:04 | [diff] [blame] | 102 | } else if (type == RTCInboundRTPStreamStats::kType) { |
| 103 | const auto& inbound_rtp = |
| 104 | static_cast<const RTCInboundRTPStreamStats&>(stats); |
| 105 | AddIdIfDefined(inbound_rtp.remote_id, &neighbor_ids); |
| 106 | AddIdIfDefined(inbound_rtp.track_id, &neighbor_ids); |
| 107 | AddIdIfDefined(inbound_rtp.transport_id, &neighbor_ids); |
| 108 | AddIdIfDefined(inbound_rtp.codec_id, &neighbor_ids); |
| 109 | } else if (type == RTCOutboundRTPStreamStats::kType) { |
| 110 | const auto& outbound_rtp = |
| 111 | static_cast<const RTCOutboundRTPStreamStats&>(stats); |
| 112 | AddIdIfDefined(outbound_rtp.remote_id, &neighbor_ids); |
| 113 | AddIdIfDefined(outbound_rtp.track_id, &neighbor_ids); |
| 114 | AddIdIfDefined(outbound_rtp.transport_id, &neighbor_ids); |
| 115 | AddIdIfDefined(outbound_rtp.codec_id, &neighbor_ids); |
| 116 | AddIdIfDefined(outbound_rtp.media_source_id, &neighbor_ids); |
Henrik Boström | 883eefc | 2019-05-27 11:40:25 | [diff] [blame] | 117 | } else if (type == RTCRemoteInboundRtpStreamStats::kType) { |
| 118 | const auto& remote_inbound_rtp = |
| 119 | static_cast<const RTCRemoteInboundRtpStreamStats&>(stats); |
| 120 | AddIdIfDefined(remote_inbound_rtp.transport_id, &neighbor_ids); |
| 121 | AddIdIfDefined(remote_inbound_rtp.codec_id, &neighbor_ids); |
| 122 | AddIdIfDefined(remote_inbound_rtp.local_id, &neighbor_ids); |
Alessio Bazzica | f7b1b95 | 2021-03-23 16:23:04 | [diff] [blame] | 123 | } else if (type == RTCRemoteOutboundRtpStreamStats::kType) { |
| 124 | const auto& remote_outbound_rtp = |
| 125 | static_cast<const RTCRemoteOutboundRtpStreamStats&>(stats); |
| 126 | // Inherited from `RTCRTPStreamStats`. |
| 127 | AddIdIfDefined(remote_outbound_rtp.track_id, &neighbor_ids); |
| 128 | AddIdIfDefined(remote_outbound_rtp.transport_id, &neighbor_ids); |
| 129 | AddIdIfDefined(remote_outbound_rtp.codec_id, &neighbor_ids); |
| 130 | // Direct members of `RTCRemoteOutboundRtpStreamStats`. |
| 131 | AddIdIfDefined(remote_outbound_rtp.local_id, &neighbor_ids); |
Henrik Boström | 646fda0 | 2019-05-22 13:49:42 | [diff] [blame] | 132 | } else if (type == RTCAudioSourceStats::kType || |
| 133 | type == RTCVideoSourceStats::kType) { |
| 134 | // RTC[Audio/Video]SourceStats does not have any neighbor references. |
Henrik Boström | b619936 | 2018-03-12 09:27:55 | [diff] [blame] | 135 | } else if (type == RTCTransportStats::kType) { |
| 136 | // RTCTransportStats does not have any neighbor references. |
| 137 | const auto& transport = static_cast<const RTCTransportStats&>(stats); |
| 138 | AddIdIfDefined(transport.rtcp_transport_stats_id, &neighbor_ids); |
| 139 | AddIdIfDefined(transport.selected_candidate_pair_id, &neighbor_ids); |
| 140 | AddIdIfDefined(transport.local_certificate_id, &neighbor_ids); |
| 141 | AddIdIfDefined(transport.remote_certificate_id, &neighbor_ids); |
| 142 | } else { |
| 143 | RTC_NOTREACHED() << "Unrecognized type: " << type; |
| 144 | } |
| 145 | return neighbor_ids; |
| 146 | } |
| 147 | |
| 148 | } // namespace webrtc |