henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "api/jsepicecandidate.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
| 13 | #include <vector> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "pc/webrtcsdp.h" |
| 16 | #include "rtc_base/stringencode.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | IceCandidateInterface* CreateIceCandidate(const std::string& sdp_mid, |
| 21 | int sdp_mline_index, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 22 | const std::string& sdp, |
| 23 | SdpParseError* error) { |
| 24 | JsepIceCandidate* jsep_ice = new JsepIceCandidate(sdp_mid, sdp_mline_index); |
| 25 | if (!jsep_ice->Initialize(sdp, error)) { |
| 26 | delete jsep_ice; |
| 27 | return NULL; |
| 28 | } |
| 29 | return jsep_ice; |
| 30 | } |
| 31 | |
| 32 | JsepIceCandidate::JsepIceCandidate(const std::string& sdp_mid, |
| 33 | int sdp_mline_index) |
| 34 | : sdp_mid_(sdp_mid), |
| 35 | sdp_mline_index_(sdp_mline_index) { |
| 36 | } |
| 37 | |
| 38 | JsepIceCandidate::JsepIceCandidate(const std::string& sdp_mid, |
| 39 | int sdp_mline_index, |
| 40 | const cricket::Candidate& candidate) |
| 41 | : sdp_mid_(sdp_mid), |
| 42 | sdp_mline_index_(sdp_mline_index), |
| 43 | candidate_(candidate) { |
| 44 | } |
| 45 | |
| 46 | JsepIceCandidate::~JsepIceCandidate() { |
| 47 | } |
| 48 | |
| 49 | bool JsepIceCandidate::Initialize(const std::string& sdp, SdpParseError* err) { |
| 50 | return SdpDeserializeCandidate(sdp, this, err); |
| 51 | } |
| 52 | |
| 53 | bool JsepIceCandidate::ToString(std::string* out) const { |
| 54 | if (!out) |
| 55 | return false; |
| 56 | *out = SdpSerializeCandidate(*this); |
| 57 | return !out->empty(); |
| 58 | } |
| 59 | |
| 60 | JsepCandidateCollection::~JsepCandidateCollection() { |
| 61 | for (std::vector<JsepIceCandidate*>::iterator it = candidates_.begin(); |
| 62 | it != candidates_.end(); ++it) { |
| 63 | delete *it; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | bool JsepCandidateCollection::HasCandidate( |
| 68 | const IceCandidateInterface* candidate) const { |
| 69 | bool ret = false; |
| 70 | for (std::vector<JsepIceCandidate*>::const_iterator it = candidates_.begin(); |
| 71 | it != candidates_.end(); ++it) { |
| 72 | if ((*it)->sdp_mid() == candidate->sdp_mid() && |
| 73 | (*it)->sdp_mline_index() == candidate->sdp_mline_index() && |
| 74 | (*it)->candidate().IsEquivalent(candidate->candidate())) { |
| 75 | ret = true; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | return ret; |
| 80 | } |
| 81 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 82 | size_t JsepCandidateCollection::remove(const cricket::Candidate& candidate) { |
| 83 | auto iter = std::find_if(candidates_.begin(), candidates_.end(), |
| 84 | [candidate](JsepIceCandidate* c) { |
| 85 | return candidate.MatchesForRemoval(c->candidate()); |
| 86 | }); |
| 87 | if (iter != candidates_.end()) { |
| 88 | delete *iter; |
| 89 | candidates_.erase(iter); |
| 90 | return 1; |
| 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | } // namespace webrtc |