Update API for Objective-C RTCIceCandidate.
BUG= R=tkchin@webrtc.org Review URL: https://codereview.webrtc.org/1517253005 . Patch from Jon Hjelle <hjon@andyet.net>. Cr-Commit-Position: refs/heads/master@{#11156}
This commit is contained in:
@ -12,12 +12,17 @@ if (is_ios) {
|
||||
source_set("rtc_api_objc") {
|
||||
deps = [
|
||||
"//webrtc/base:rtc_base_objc",
|
||||
#"//talk/libjingle:libjingle_peerconnection",
|
||||
]
|
||||
cflags = [
|
||||
"-fobjc-arc",
|
||||
"-Wobjc-missing-property-synthesis",
|
||||
]
|
||||
sources = [
|
||||
# Add RTCIceCandidate when there's a BUILD.gn for peer connection APIs
|
||||
#"objc/RTCIceCandidate+Private.h",
|
||||
#"objc/RTCIceCandidate.h",
|
||||
#"objc/RTCIceCandidate.mm",
|
||||
"objc/RTCIceServer+Private.h",
|
||||
"objc/RTCIceServer.h",
|
||||
"objc/RTCIceServer.mm",
|
||||
|
@ -16,8 +16,12 @@
|
||||
'type': 'static_library',
|
||||
'dependencies': [
|
||||
'<(webrtc_root)/base/base.gyp:rtc_base_objc',
|
||||
'../../talk/libjingle.gyp:libjingle_peerconnection',
|
||||
],
|
||||
'sources': [
|
||||
'objc/RTCIceCandidate+Private.h',
|
||||
'objc/RTCIceCandidate.h',
|
||||
'objc/RTCIceCandidate.mm',
|
||||
'objc/RTCIceServer+Private.h',
|
||||
'objc/RTCIceServer.h',
|
||||
'objc/RTCIceServer.mm',
|
||||
|
@ -19,7 +19,8 @@
|
||||
'<(webrtc_root)/base/base_tests.gyp:rtc_base_tests_utils',
|
||||
],
|
||||
'sources': [
|
||||
'objctests/RTCIceServerTest.mm'
|
||||
'objctests/RTCIceCandidateTest.mm',
|
||||
'objctests/RTCIceServerTest.mm',
|
||||
],
|
||||
'xcode_settings': {
|
||||
'CLANG_ENABLE_OBJC_ARC': 'YES',
|
||||
|
36
webrtc/api/objc/RTCIceCandidate+Private.h
Normal file
36
webrtc/api/objc/RTCIceCandidate+Private.h
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#import "RTCIceCandidate.h"
|
||||
|
||||
#include "talk/app/webrtc/jsep.h"
|
||||
#include "webrtc/base/scoped_ptr.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface RTCIceCandidate ()
|
||||
|
||||
/**
|
||||
* The native IceCandidateInterface representation of this RTCIceCandidate
|
||||
* object. This is needed to pass to the underlying C++ APIs.
|
||||
*/
|
||||
@property(nonatomic, readonly)
|
||||
rtc::scoped_ptr<webrtc::IceCandidateInterface> nativeCandidate;
|
||||
|
||||
/**
|
||||
* Initialize an RTCIceCandidate from a native IceCandidateInterface. No
|
||||
* ownership is taken of the native candidate.
|
||||
*/
|
||||
- (instancetype)initWithNativeCandidate:
|
||||
(webrtc::IceCandidateInterface *)candidate;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
44
webrtc/api/objc/RTCIceCandidate.h
Normal file
44
webrtc/api/objc/RTCIceCandidate.h
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface RTCIceCandidate : NSObject
|
||||
|
||||
/**
|
||||
* If present, the identifier of the "media stream identification" for the media
|
||||
* component this candidate is associated with.
|
||||
*/
|
||||
@property(nonatomic, readonly, nullable) NSString *sdpMid;
|
||||
|
||||
/**
|
||||
* The index (starting at zero) of the media description this candidate is
|
||||
* associated with in the SDP.
|
||||
*/
|
||||
@property(nonatomic, readonly) NSInteger sdpMLineIndex;
|
||||
|
||||
/** The SDP string for this candidate. */
|
||||
@property(nonatomic, readonly) NSString *sdp;
|
||||
|
||||
- (instancetype)init NS_UNAVAILABLE;
|
||||
|
||||
/**
|
||||
* Initialize an RTCIceCandidate from SDP.
|
||||
*/
|
||||
- (instancetype)initWithSdp:(NSString *)sdp
|
||||
sdpMLineIndex:(NSInteger)sdpMLineIndex
|
||||
sdpMid:(nullable NSString *)sdpMid
|
||||
NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
70
webrtc/api/objc/RTCIceCandidate.mm
Normal file
70
webrtc/api/objc/RTCIceCandidate.mm
Normal file
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#import "RTCIceCandidate.h"
|
||||
|
||||
#import "webrtc/api/objc/RTCIceCandidate+Private.h"
|
||||
#import "webrtc/base/objc/NSString+StdString.h"
|
||||
#import "webrtc/base/objc/RTCLogging.h"
|
||||
|
||||
@implementation RTCIceCandidate
|
||||
|
||||
@synthesize sdpMid = _sdpMid;
|
||||
@synthesize sdpMLineIndex = _sdpMLineIndex;
|
||||
@synthesize sdp = _sdp;
|
||||
|
||||
- (instancetype)initWithSdp:(NSString *)sdp
|
||||
sdpMLineIndex:(NSInteger)sdpMLineIndex
|
||||
sdpMid:(NSString *)sdpMid {
|
||||
NSParameterAssert(sdp.length);
|
||||
if (self = [super init]) {
|
||||
_sdpMid = [sdpMid copy];
|
||||
_sdpMLineIndex = sdpMLineIndex;
|
||||
_sdp = [sdp copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString *)description {
|
||||
return [NSString stringWithFormat:@"RTCIceCandidate:\n%@\n%ld\n%@",
|
||||
_sdpMid,
|
||||
(long)_sdpMLineIndex,
|
||||
_sdp];
|
||||
}
|
||||
|
||||
#pragma mark - Private
|
||||
|
||||
- (instancetype)initWithNativeCandidate:
|
||||
(webrtc::IceCandidateInterface *)candidate {
|
||||
NSParameterAssert(candidate);
|
||||
std::string sdp;
|
||||
candidate->ToString(&sdp);
|
||||
|
||||
return [self initWithSdp:[NSString stringForStdString:sdp]
|
||||
sdpMLineIndex:candidate->sdp_mline_index()
|
||||
sdpMid:[NSString stringForStdString:candidate->sdp_mid()]];
|
||||
}
|
||||
|
||||
- (rtc::scoped_ptr<webrtc::IceCandidateInterface>)nativeCandidate {
|
||||
webrtc::SdpParseError error;
|
||||
|
||||
webrtc::IceCandidateInterface *candidate = webrtc::CreateIceCandidate(
|
||||
_sdpMid.stdString, _sdpMLineIndex, _sdp.stdString, &error);
|
||||
|
||||
if (!candidate) {
|
||||
RTCLog(@"Failed to create ICE candidate: %s\nline: %s",
|
||||
error.description.c_str(),
|
||||
error.line.c_str());
|
||||
}
|
||||
|
||||
return rtc::scoped_ptr<webrtc::IceCandidateInterface>(candidate);
|
||||
}
|
||||
|
||||
@end
|
74
webrtc/api/objctests/RTCIceCandidateTest.mm
Normal file
74
webrtc/api/objctests/RTCIceCandidateTest.mm
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#include "webrtc/base/gunit.h"
|
||||
|
||||
#import "webrtc/api/objc/RTCIceCandidate.h"
|
||||
#import "webrtc/api/objc/RTCIceCandidate+Private.h"
|
||||
#import "webrtc/base/objc/NSString+StdString.h"
|
||||
|
||||
@interface RTCIceCandidateTest : NSObject
|
||||
- (void)testCandidate;
|
||||
- (void)testInitFromNativeCandidate;
|
||||
@end
|
||||
|
||||
@implementation RTCIceCandidateTest
|
||||
|
||||
- (void)testCandidate {
|
||||
NSString *sdp = @"candidate:4025901590 1 udp 2122265343 "
|
||||
"fdff:2642:12a6:fe38:c001:beda:fcf9:51aa "
|
||||
"59052 typ host generation 0";
|
||||
|
||||
RTCIceCandidate *candidate = [[RTCIceCandidate alloc] initWithSdp:sdp
|
||||
sdpMLineIndex:0
|
||||
sdpMid:@"audio"];
|
||||
|
||||
rtc::scoped_ptr<webrtc::IceCandidateInterface> nativeCandidate =
|
||||
candidate.nativeCandidate;
|
||||
EXPECT_EQ("audio", nativeCandidate->sdp_mid());
|
||||
EXPECT_EQ(0, nativeCandidate->sdp_mline_index());
|
||||
|
||||
std::string sdpString;
|
||||
nativeCandidate->ToString(&sdpString);
|
||||
EXPECT_EQ(sdp.stdString, sdpString);
|
||||
}
|
||||
|
||||
- (void)testInitFromNativeCandidate {
|
||||
std::string sdp("candidate:4025901590 1 udp 2122265343 "
|
||||
"fdff:2642:12a6:fe38:c001:beda:fcf9:51aa "
|
||||
"59052 typ host generation 0");
|
||||
webrtc::IceCandidateInterface *nativeCandidate =
|
||||
webrtc::CreateIceCandidate("audio", 0, sdp, nullptr);
|
||||
|
||||
RTCIceCandidate *iceCandidate =
|
||||
[[RTCIceCandidate alloc] initWithNativeCandidate:nativeCandidate];
|
||||
EXPECT_TRUE([@"audio" isEqualToString:iceCandidate.sdpMid]);
|
||||
EXPECT_EQ(0, iceCandidate.sdpMLineIndex);
|
||||
|
||||
EXPECT_EQ(sdp, iceCandidate.sdp.stdString);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
TEST(RTCIceCandidateTest, CandidateTest) {
|
||||
@autoreleasepool {
|
||||
RTCIceCandidateTest *test = [[RTCIceCandidateTest alloc] init];
|
||||
[test testCandidate];
|
||||
}
|
||||
}
|
||||
|
||||
TEST(RTCIceCandidateTest, InitFromCandidateTest) {
|
||||
@autoreleasepool {
|
||||
RTCIceCandidateTest *test = [[RTCIceCandidateTest alloc] init];
|
||||
[test testInitFromNativeCandidate];
|
||||
}
|
||||
}
|
@ -19,6 +19,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
@property(nonatomic, readonly) std::string stdString;
|
||||
|
||||
+ (std::string)stdStringForString:(NSString *)nsString;
|
||||
+ (NSString *)stringForStdString:(const std::string&)stdString;
|
||||
|
||||
@end
|
||||
|
||||
|
@ -22,4 +22,12 @@
|
||||
charData.length);
|
||||
}
|
||||
|
||||
+ (NSString *)stringForStdString:(const std::string&)stdString {
|
||||
// std::string may contain null termination character so we construct
|
||||
// using length.
|
||||
return [[NSString alloc] initWithBytes:stdString.data()
|
||||
length:stdString.length()
|
||||
encoding:NSUTF8StringEncoding];
|
||||
}
|
||||
|
||||
@end
|
||||
|
Reference in New Issue
Block a user