Mirko Bonadei | 2ffed6d | 2018-07-20 09:09:32 | [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 "api/jsep_ice_candidate.h" |
Mirko Bonadei | 2ffed6d | 2018-07-20 09:09:32 | [diff] [blame] | 12 | |
Steve Anton | 8e967df | 2019-07-31 01:07:40 | [diff] [blame] | 13 | #include <memory> |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include <utility> |
| 15 | |
Steve Anton | a59dcc3 | 2019-03-25 20:53:07 | [diff] [blame] | 16 | #include "absl/algorithm/container.h" |
Steve Anton | 8e967df | 2019-07-31 01:07:40 | [diff] [blame] | 17 | #include "absl/memory/memory.h" |
Steve Anton | a59dcc3 | 2019-03-25 20:53:07 | [diff] [blame] | 18 | |
Mirko Bonadei | 2ffed6d | 2018-07-20 09:09:32 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | |
| 21 | std::string JsepIceCandidate::sdp_mid() const { |
| 22 | return sdp_mid_; |
| 23 | } |
| 24 | |
| 25 | int JsepIceCandidate::sdp_mline_index() const { |
| 26 | return sdp_mline_index_; |
| 27 | } |
| 28 | |
| 29 | const cricket::Candidate& JsepIceCandidate::candidate() const { |
| 30 | return candidate_; |
| 31 | } |
| 32 | |
| 33 | std::string JsepIceCandidate::server_url() const { |
| 34 | return candidate_.url(); |
| 35 | } |
| 36 | |
| 37 | JsepCandidateCollection::JsepCandidateCollection() = default; |
| 38 | |
| 39 | JsepCandidateCollection::JsepCandidateCollection(JsepCandidateCollection&& o) |
| 40 | : candidates_(std::move(o.candidates_)) {} |
| 41 | |
| 42 | size_t JsepCandidateCollection::count() const { |
| 43 | return candidates_.size(); |
| 44 | } |
| 45 | |
| 46 | void JsepCandidateCollection::add(JsepIceCandidate* candidate) { |
Steve Anton | 8e967df | 2019-07-31 01:07:40 | [diff] [blame] | 47 | candidates_.push_back(absl::WrapUnique(candidate)); |
Mirko Bonadei | 2ffed6d | 2018-07-20 09:09:32 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | const IceCandidateInterface* JsepCandidateCollection::at(size_t index) const { |
Steve Anton | 8e967df | 2019-07-31 01:07:40 | [diff] [blame] | 51 | return candidates_[index].get(); |
Mirko Bonadei | 2ffed6d | 2018-07-20 09:09:32 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | bool JsepCandidateCollection::HasCandidate( |
| 55 | const IceCandidateInterface* candidate) const { |
Steve Anton | 8e967df | 2019-07-31 01:07:40 | [diff] [blame] | 56 | return absl::c_any_of( |
| 57 | candidates_, [&](const std::unique_ptr<JsepIceCandidate>& entry) { |
| 58 | return entry->sdp_mid() == candidate->sdp_mid() && |
| 59 | entry->sdp_mline_index() == candidate->sdp_mline_index() && |
| 60 | entry->candidate().IsEquivalent(candidate->candidate()); |
| 61 | }); |
Mirko Bonadei | 2ffed6d | 2018-07-20 09:09:32 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | size_t JsepCandidateCollection::remove(const cricket::Candidate& candidate) { |
Steve Anton | 8e967df | 2019-07-31 01:07:40 | [diff] [blame] | 65 | auto iter = absl::c_find_if( |
| 66 | candidates_, [&](const std::unique_ptr<JsepIceCandidate>& c) { |
| 67 | return candidate.MatchesForRemoval(c->candidate()); |
| 68 | }); |
Mirko Bonadei | 2ffed6d | 2018-07-20 09:09:32 | [diff] [blame] | 69 | if (iter != candidates_.end()) { |
Mirko Bonadei | 2ffed6d | 2018-07-20 09:09:32 | [diff] [blame] | 70 | candidates_.erase(iter); |
| 71 | return 1; |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | } // namespace webrtc |