xref: /aosp_15_r20/external/webrtc/sdk/objc/api/peerconnection/RTCIceCandidate.mm (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker/*
2*d9f75844SAndroid Build Coastguard Worker *  Copyright 2015 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#import "RTCIceCandidate+Private.h"
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard Worker#include <memory>
14*d9f75844SAndroid Build Coastguard Worker
15*d9f75844SAndroid Build Coastguard Worker#import "base/RTCLogging.h"
16*d9f75844SAndroid Build Coastguard Worker#import "helpers/NSString+StdString.h"
17*d9f75844SAndroid Build Coastguard Worker
18*d9f75844SAndroid Build Coastguard Worker@implementation RTC_OBJC_TYPE (RTCIceCandidate)
19*d9f75844SAndroid Build Coastguard Worker
20*d9f75844SAndroid Build Coastguard Worker@synthesize sdpMid = _sdpMid;
21*d9f75844SAndroid Build Coastguard Worker@synthesize sdpMLineIndex = _sdpMLineIndex;
22*d9f75844SAndroid Build Coastguard Worker@synthesize sdp = _sdp;
23*d9f75844SAndroid Build Coastguard Worker@synthesize serverUrl = _serverUrl;
24*d9f75844SAndroid Build Coastguard Worker
25*d9f75844SAndroid Build Coastguard Worker- (instancetype)initWithSdp:(NSString *)sdp
26*d9f75844SAndroid Build Coastguard Worker              sdpMLineIndex:(int)sdpMLineIndex
27*d9f75844SAndroid Build Coastguard Worker                     sdpMid:(NSString *)sdpMid {
28*d9f75844SAndroid Build Coastguard Worker  NSParameterAssert(sdp.length);
29*d9f75844SAndroid Build Coastguard Worker  if (self = [super init]) {
30*d9f75844SAndroid Build Coastguard Worker    _sdpMid = [sdpMid copy];
31*d9f75844SAndroid Build Coastguard Worker    _sdpMLineIndex = sdpMLineIndex;
32*d9f75844SAndroid Build Coastguard Worker    _sdp = [sdp copy];
33*d9f75844SAndroid Build Coastguard Worker  }
34*d9f75844SAndroid Build Coastguard Worker  return self;
35*d9f75844SAndroid Build Coastguard Worker}
36*d9f75844SAndroid Build Coastguard Worker
37*d9f75844SAndroid Build Coastguard Worker- (NSString *)description {
38*d9f75844SAndroid Build Coastguard Worker  return [NSString stringWithFormat:@"RTC_OBJC_TYPE(RTCIceCandidate):\n%@\n%d\n%@\n%@",
39*d9f75844SAndroid Build Coastguard Worker                                    _sdpMid,
40*d9f75844SAndroid Build Coastguard Worker                                    _sdpMLineIndex,
41*d9f75844SAndroid Build Coastguard Worker                                    _sdp,
42*d9f75844SAndroid Build Coastguard Worker                                    _serverUrl];
43*d9f75844SAndroid Build Coastguard Worker}
44*d9f75844SAndroid Build Coastguard Worker
45*d9f75844SAndroid Build Coastguard Worker#pragma mark - Private
46*d9f75844SAndroid Build Coastguard Worker
47*d9f75844SAndroid Build Coastguard Worker- (instancetype)initWithNativeCandidate:
48*d9f75844SAndroid Build Coastguard Worker    (const webrtc::IceCandidateInterface *)candidate {
49*d9f75844SAndroid Build Coastguard Worker  NSParameterAssert(candidate);
50*d9f75844SAndroid Build Coastguard Worker  std::string sdp;
51*d9f75844SAndroid Build Coastguard Worker  candidate->ToString(&sdp);
52*d9f75844SAndroid Build Coastguard Worker
53*d9f75844SAndroid Build Coastguard Worker  RTC_OBJC_TYPE(RTCIceCandidate) *rtcCandidate =
54*d9f75844SAndroid Build Coastguard Worker      [self initWithSdp:[NSString stringForStdString:sdp]
55*d9f75844SAndroid Build Coastguard Worker          sdpMLineIndex:candidate->sdp_mline_index()
56*d9f75844SAndroid Build Coastguard Worker                 sdpMid:[NSString stringForStdString:candidate->sdp_mid()]];
57*d9f75844SAndroid Build Coastguard Worker  rtcCandidate->_serverUrl = [NSString stringForStdString:candidate->server_url()];
58*d9f75844SAndroid Build Coastguard Worker  return rtcCandidate;
59*d9f75844SAndroid Build Coastguard Worker}
60*d9f75844SAndroid Build Coastguard Worker
61*d9f75844SAndroid Build Coastguard Worker- (std::unique_ptr<webrtc::IceCandidateInterface>)nativeCandidate {
62*d9f75844SAndroid Build Coastguard Worker  webrtc::SdpParseError error;
63*d9f75844SAndroid Build Coastguard Worker
64*d9f75844SAndroid Build Coastguard Worker  webrtc::IceCandidateInterface *candidate = webrtc::CreateIceCandidate(
65*d9f75844SAndroid Build Coastguard Worker      _sdpMid.stdString, _sdpMLineIndex, _sdp.stdString, &error);
66*d9f75844SAndroid Build Coastguard Worker
67*d9f75844SAndroid Build Coastguard Worker  if (!candidate) {
68*d9f75844SAndroid Build Coastguard Worker    RTCLog(@"Failed to create ICE candidate: %s\nline: %s",
69*d9f75844SAndroid Build Coastguard Worker           error.description.c_str(),
70*d9f75844SAndroid Build Coastguard Worker           error.line.c_str());
71*d9f75844SAndroid Build Coastguard Worker  }
72*d9f75844SAndroid Build Coastguard Worker
73*d9f75844SAndroid Build Coastguard Worker  return std::unique_ptr<webrtc::IceCandidateInterface>(candidate);
74*d9f75844SAndroid Build Coastguard Worker}
75*d9f75844SAndroid Build Coastguard Worker
76*d9f75844SAndroid Build Coastguard Worker@end
77