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
35namespace webrtc {
36
37IceCandidateInterface* CreateIceCandidate(const std::string& sdp_mid,
38                                          int sdp_mline_index,
39                                          const std::string& sdp) {
40  return CreateIceCandidate(sdp_mid, sdp_mline_index, sdp, NULL);
41}
42
43IceCandidateInterface* CreateIceCandidate(const std::string& sdp_mid,
44                                          int sdp_mline_index,
45                                          const std::string& sdp,
46                                          SdpParseError* error) {
47  JsepIceCandidate* jsep_ice = new JsepIceCandidate(sdp_mid, sdp_mline_index);
48  if (!jsep_ice->Initialize(sdp, error)) {
49    delete jsep_ice;
50    return NULL;
51  }
52  return jsep_ice;
53}
54
55JsepIceCandidate::JsepIceCandidate(const std::string& sdp_mid,
56                                   int sdp_mline_index)
57    : sdp_mid_(sdp_mid),
58      sdp_mline_index_(sdp_mline_index) {
59}
60
61JsepIceCandidate::JsepIceCandidate(const std::string& sdp_mid,
62                                   int sdp_mline_index,
63                                   const cricket::Candidate& candidate)
64    : sdp_mid_(sdp_mid),
65      sdp_mline_index_(sdp_mline_index),
66      candidate_(candidate) {
67}
68
69JsepIceCandidate::~JsepIceCandidate() {
70}
71
72bool JsepIceCandidate::Initialize(const std::string& sdp, SdpParseError* err) {
73  return SdpDeserializeCandidate(sdp, this, err);
74}
75
76bool JsepIceCandidate::ToString(std::string* out) const {
77  if (!out)
78    return false;
79  *out = SdpSerializeCandidate(*this);
80  return !out->empty();
81}
82
83JsepCandidateCollection::~JsepCandidateCollection() {
84  for (std::vector<JsepIceCandidate*>::iterator it = candidates_.begin();
85       it != candidates_.end(); ++it) {
86    delete *it;
87  }
88}
89
90bool JsepCandidateCollection::HasCandidate(
91    const IceCandidateInterface* candidate) const {
92  bool ret = false;
93  for (std::vector<JsepIceCandidate*>::const_iterator it = candidates_.begin();
94      it != candidates_.end(); ++it) {
95    if ((*it)->sdp_mid() == candidate->sdp_mid() &&
96        (*it)->sdp_mline_index() == candidate->sdp_mline_index() &&
97        (*it)->candidate().IsEquivalent(candidate->candidate())) {
98      ret = true;
99      break;
100    }
101  }
102  return ret;
103}
104
105}  // namespace webrtc
106