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 
11 #include "api/jsep_ice_candidate.h"
12 
13 #include <memory>
14 #include <utility>
15 
16 #include "absl/algorithm/container.h"
17 #include "absl/memory/memory.h"
18 
19 namespace webrtc {
20 
sdp_mid() const21 std::string JsepIceCandidate::sdp_mid() const {
22   return sdp_mid_;
23 }
24 
sdp_mline_index() const25 int JsepIceCandidate::sdp_mline_index() const {
26   return sdp_mline_index_;
27 }
28 
candidate() const29 const cricket::Candidate& JsepIceCandidate::candidate() const {
30   return candidate_;
31 }
32 
server_url() const33 std::string JsepIceCandidate::server_url() const {
34   return candidate_.url();
35 }
36 
37 JsepCandidateCollection::JsepCandidateCollection() = default;
38 
JsepCandidateCollection(JsepCandidateCollection && o)39 JsepCandidateCollection::JsepCandidateCollection(JsepCandidateCollection&& o)
40     : candidates_(std::move(o.candidates_)) {}
41 
count() const42 size_t JsepCandidateCollection::count() const {
43   return candidates_.size();
44 }
45 
add(JsepIceCandidate * candidate)46 void JsepCandidateCollection::add(JsepIceCandidate* candidate) {
47   candidates_.push_back(absl::WrapUnique(candidate));
48 }
49 
at(size_t index) const50 const IceCandidateInterface* JsepCandidateCollection::at(size_t index) const {
51   return candidates_[index].get();
52 }
53 
HasCandidate(const IceCandidateInterface * candidate) const54 bool JsepCandidateCollection::HasCandidate(
55     const IceCandidateInterface* candidate) const {
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       });
62 }
63 
remove(const cricket::Candidate & candidate)64 size_t JsepCandidateCollection::remove(const cricket::Candidate& candidate) {
65   auto iter = absl::c_find_if(
66       candidates_, [&](const std::unique_ptr<JsepIceCandidate>& c) {
67         return candidate.MatchesForRemoval(c->candidate());
68       });
69   if (iter != candidates_.end()) {
70     candidates_.erase(iter);
71     return 1;
72   }
73   return 0;
74 }
75 
76 }  // namespace webrtc
77