xref: /aosp_15_r20/external/webrtc/pc/jsep_ice_candidate.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2012 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "api/jsep_ice_candidate.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include "pc/webrtc_sdp.h"
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker // This file contains JsepIceCandidate-related functions that are not
16*d9f75844SAndroid Build Coastguard Worker // included in api/jsep_ice_candidate.cc. Some of these link to SDP
17*d9f75844SAndroid Build Coastguard Worker // parsing/serializing functions, which some users may not want.
18*d9f75844SAndroid Build Coastguard Worker // TODO(bugs.webrtc.org/12330): Merge the two .cc files somehow.
19*d9f75844SAndroid Build Coastguard Worker 
20*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
21*d9f75844SAndroid Build Coastguard Worker 
CreateIceCandidate(const std::string & sdp_mid,int sdp_mline_index,const std::string & sdp,SdpParseError * error)22*d9f75844SAndroid Build Coastguard Worker IceCandidateInterface* CreateIceCandidate(const std::string& sdp_mid,
23*d9f75844SAndroid Build Coastguard Worker                                           int sdp_mline_index,
24*d9f75844SAndroid Build Coastguard Worker                                           const std::string& sdp,
25*d9f75844SAndroid Build Coastguard Worker                                           SdpParseError* error) {
26*d9f75844SAndroid Build Coastguard Worker   JsepIceCandidate* jsep_ice = new JsepIceCandidate(sdp_mid, sdp_mline_index);
27*d9f75844SAndroid Build Coastguard Worker   if (!jsep_ice->Initialize(sdp, error)) {
28*d9f75844SAndroid Build Coastguard Worker     delete jsep_ice;
29*d9f75844SAndroid Build Coastguard Worker     return NULL;
30*d9f75844SAndroid Build Coastguard Worker   }
31*d9f75844SAndroid Build Coastguard Worker   return jsep_ice;
32*d9f75844SAndroid Build Coastguard Worker }
33*d9f75844SAndroid Build Coastguard Worker 
CreateIceCandidate(const std::string & sdp_mid,int sdp_mline_index,const cricket::Candidate & candidate)34*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<IceCandidateInterface> CreateIceCandidate(
35*d9f75844SAndroid Build Coastguard Worker     const std::string& sdp_mid,
36*d9f75844SAndroid Build Coastguard Worker     int sdp_mline_index,
37*d9f75844SAndroid Build Coastguard Worker     const cricket::Candidate& candidate) {
38*d9f75844SAndroid Build Coastguard Worker   return std::make_unique<JsepIceCandidate>(sdp_mid, sdp_mline_index,
39*d9f75844SAndroid Build Coastguard Worker                                             candidate);
40*d9f75844SAndroid Build Coastguard Worker }
41*d9f75844SAndroid Build Coastguard Worker 
JsepIceCandidate(const std::string & sdp_mid,int sdp_mline_index)42*d9f75844SAndroid Build Coastguard Worker JsepIceCandidate::JsepIceCandidate(const std::string& sdp_mid,
43*d9f75844SAndroid Build Coastguard Worker                                    int sdp_mline_index)
44*d9f75844SAndroid Build Coastguard Worker     : sdp_mid_(sdp_mid), sdp_mline_index_(sdp_mline_index) {}
45*d9f75844SAndroid Build Coastguard Worker 
JsepIceCandidate(const std::string & sdp_mid,int sdp_mline_index,const cricket::Candidate & candidate)46*d9f75844SAndroid Build Coastguard Worker JsepIceCandidate::JsepIceCandidate(const std::string& sdp_mid,
47*d9f75844SAndroid Build Coastguard Worker                                    int sdp_mline_index,
48*d9f75844SAndroid Build Coastguard Worker                                    const cricket::Candidate& candidate)
49*d9f75844SAndroid Build Coastguard Worker     : sdp_mid_(sdp_mid),
50*d9f75844SAndroid Build Coastguard Worker       sdp_mline_index_(sdp_mline_index),
51*d9f75844SAndroid Build Coastguard Worker       candidate_(candidate) {}
52*d9f75844SAndroid Build Coastguard Worker 
~JsepIceCandidate()53*d9f75844SAndroid Build Coastguard Worker JsepIceCandidate::~JsepIceCandidate() {}
54*d9f75844SAndroid Build Coastguard Worker 
Clone() const55*d9f75844SAndroid Build Coastguard Worker JsepCandidateCollection JsepCandidateCollection::Clone() const {
56*d9f75844SAndroid Build Coastguard Worker   JsepCandidateCollection new_collection;
57*d9f75844SAndroid Build Coastguard Worker   for (const auto& candidate : candidates_) {
58*d9f75844SAndroid Build Coastguard Worker     new_collection.candidates_.push_back(std::make_unique<JsepIceCandidate>(
59*d9f75844SAndroid Build Coastguard Worker         candidate->sdp_mid(), candidate->sdp_mline_index(),
60*d9f75844SAndroid Build Coastguard Worker         candidate->candidate()));
61*d9f75844SAndroid Build Coastguard Worker   }
62*d9f75844SAndroid Build Coastguard Worker   return new_collection;
63*d9f75844SAndroid Build Coastguard Worker }
64*d9f75844SAndroid Build Coastguard Worker 
Initialize(const std::string & sdp,SdpParseError * err)65*d9f75844SAndroid Build Coastguard Worker bool JsepIceCandidate::Initialize(const std::string& sdp, SdpParseError* err) {
66*d9f75844SAndroid Build Coastguard Worker   return SdpDeserializeCandidate(sdp, this, err);
67*d9f75844SAndroid Build Coastguard Worker }
68*d9f75844SAndroid Build Coastguard Worker 
ToString(std::string * out) const69*d9f75844SAndroid Build Coastguard Worker bool JsepIceCandidate::ToString(std::string* out) const {
70*d9f75844SAndroid Build Coastguard Worker   if (!out)
71*d9f75844SAndroid Build Coastguard Worker     return false;
72*d9f75844SAndroid Build Coastguard Worker   *out = SdpSerializeCandidate(*this);
73*d9f75844SAndroid Build Coastguard Worker   return !out->empty();
74*d9f75844SAndroid Build Coastguard Worker }
75*d9f75844SAndroid Build Coastguard Worker 
76*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
77