1 /*
2  * libjingle
3  * Copyright 2012 Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "talk/app/webrtc/jsepicecandidate.h"
29 
30 #include <vector>
31 
32 #include "talk/app/webrtc/webrtcsdp.h"
33 #include "webrtc/base/stringencode.h"
34 
35 namespace webrtc {
36 
CreateIceCandidate(const std::string & sdp_mid,int sdp_mline_index,const std::string & sdp,SdpParseError * error)37 IceCandidateInterface* CreateIceCandidate(const std::string& sdp_mid,
38                                           int sdp_mline_index,
39                                           const std::string& sdp,
40                                           SdpParseError* error) {
41   JsepIceCandidate* jsep_ice = new JsepIceCandidate(sdp_mid, sdp_mline_index);
42   if (!jsep_ice->Initialize(sdp, error)) {
43     delete jsep_ice;
44     return NULL;
45   }
46   return jsep_ice;
47 }
48 
JsepIceCandidate(const std::string & sdp_mid,int sdp_mline_index)49 JsepIceCandidate::JsepIceCandidate(const std::string& sdp_mid,
50                                    int sdp_mline_index)
51     : sdp_mid_(sdp_mid),
52       sdp_mline_index_(sdp_mline_index) {
53 }
54 
JsepIceCandidate(const std::string & sdp_mid,int sdp_mline_index,const cricket::Candidate & candidate)55 JsepIceCandidate::JsepIceCandidate(const std::string& sdp_mid,
56                                    int sdp_mline_index,
57                                    const cricket::Candidate& candidate)
58     : sdp_mid_(sdp_mid),
59       sdp_mline_index_(sdp_mline_index),
60       candidate_(candidate) {
61 }
62 
~JsepIceCandidate()63 JsepIceCandidate::~JsepIceCandidate() {
64 }
65 
Initialize(const std::string & sdp,SdpParseError * err)66 bool JsepIceCandidate::Initialize(const std::string& sdp, SdpParseError* err) {
67   return SdpDeserializeCandidate(sdp, this, err);
68 }
69 
ToString(std::string * out) const70 bool JsepIceCandidate::ToString(std::string* out) const {
71   if (!out)
72     return false;
73   *out = SdpSerializeCandidate(*this);
74   return !out->empty();
75 }
76 
~JsepCandidateCollection()77 JsepCandidateCollection::~JsepCandidateCollection() {
78   for (std::vector<JsepIceCandidate*>::iterator it = candidates_.begin();
79        it != candidates_.end(); ++it) {
80     delete *it;
81   }
82 }
83 
HasCandidate(const IceCandidateInterface * candidate) const84 bool JsepCandidateCollection::HasCandidate(
85     const IceCandidateInterface* candidate) const {
86   bool ret = false;
87   for (std::vector<JsepIceCandidate*>::const_iterator it = candidates_.begin();
88       it != candidates_.end(); ++it) {
89     if ((*it)->sdp_mid() == candidate->sdp_mid() &&
90         (*it)->sdp_mline_index() == candidate->sdp_mline_index() &&
91         (*it)->candidate().IsEquivalent(candidate->candidate())) {
92       ret = true;
93       break;
94     }
95   }
96   return ret;
97 }
98 
99 }  // namespace webrtc
100