From aa32c3e537643eb71d8a8cb8ea7089f8cda2098a Mon Sep 17 00:00:00 2001 From: hjon Date: Sun, 13 Dec 2015 19:58:11 -0800 Subject: [PATCH] Update API for Objective-C RTCIceServer BUG= Review URL: https://codereview.webrtc.org/1499653003 Cr-Commit-Position: refs/heads/master@{#11000} --- webrtc/api/BUILD.gn | 27 ++++++++ webrtc/api/OWNERS | 1 + webrtc/api/api.gyp | 34 ++++++++++ webrtc/api/api_tests.gyp | 37 +++++++++++ webrtc/api/objc/OWNERS | 1 + webrtc/api/objc/README | 3 + webrtc/api/objc/RTCIceServer+Private.h | 28 ++++++++ webrtc/api/objc/RTCIceServer.h | 42 ++++++++++++ webrtc/api/objc/RTCIceServer.mm | 64 ++++++++++++++++++ webrtc/api/objc/WebRTC-Prefix.pch | 13 ++++ webrtc/api/objctests/RTCIceServerTest.mm | 84 ++++++++++++++++++++++++ webrtc/base/BUILD.gn | 3 + webrtc/base/base.gyp | 19 +++++- webrtc/base/objc/NSString+StdString.h | 25 +++++++ webrtc/base/objc/NSString+StdString.mm | 25 +++++++ webrtc/webrtc_tests.gypi | 5 ++ 16 files changed, 409 insertions(+), 2 deletions(-) create mode 100644 webrtc/api/BUILD.gn create mode 100644 webrtc/api/OWNERS create mode 100644 webrtc/api/api.gyp create mode 100644 webrtc/api/api_tests.gyp create mode 100644 webrtc/api/objc/OWNERS create mode 100644 webrtc/api/objc/README create mode 100644 webrtc/api/objc/RTCIceServer+Private.h create mode 100644 webrtc/api/objc/RTCIceServer.h create mode 100644 webrtc/api/objc/RTCIceServer.mm create mode 100644 webrtc/api/objc/WebRTC-Prefix.pch create mode 100644 webrtc/api/objctests/RTCIceServerTest.mm create mode 100644 webrtc/base/objc/NSString+StdString.h create mode 100644 webrtc/base/objc/NSString+StdString.mm diff --git a/webrtc/api/BUILD.gn b/webrtc/api/BUILD.gn new file mode 100644 index 0000000000..37109a1f78 --- /dev/null +++ b/webrtc/api/BUILD.gn @@ -0,0 +1,27 @@ +# Copyright (c) 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("../build/webrtc.gni") + +if (is_ios) { + source_set("rtc_api_objc") { + deps = [ + "//webrtc/base:rtc_base_objc", + ] + cflags = [ + "-fobjc-arc", + "-Wobjc-missing-property-synthesis", + ] + sources = [ + "objc/RTCIceServer+Private.h", + "objc/RTCIceServer.h", + "objc/RTCIceServer.mm", + "objc/WebRTC-Prefix.pch", + ] + } +} diff --git a/webrtc/api/OWNERS b/webrtc/api/OWNERS new file mode 100644 index 0000000000..cd06158b7f --- /dev/null +++ b/webrtc/api/OWNERS @@ -0,0 +1 @@ +tkchin@webrtc.org diff --git a/webrtc/api/api.gyp b/webrtc/api/api.gyp new file mode 100644 index 0000000000..5c6cd0df63 --- /dev/null +++ b/webrtc/api/api.gyp @@ -0,0 +1,34 @@ +# Copyright (c) 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. + +{ + 'includes': [ '../build/common.gypi', ], + 'conditions': [ + ['OS=="ios"', { + 'targets': [ + { + 'target_name': 'rtc_api_objc', + 'type': 'static_library', + 'dependencies': [ + '<(webrtc_root)/base/base.gyp:rtc_base_objc', + ], + 'sources': [ + 'objc/RTCIceServer+Private.h', + 'objc/RTCIceServer.h', + 'objc/RTCIceServer.mm', + ], + 'xcode_settings': { + 'CLANG_ENABLE_OBJC_ARC': 'YES', + 'CLANG_WARN_OBJC_MISSING_PROPERTY_SYNTHESIS': 'YES', + 'GCC_PREFIX_HEADER': 'objc/WebRTC-Prefix.pch', + }, + } + ], + }], # OS=="ios" + ], +} diff --git a/webrtc/api/api_tests.gyp b/webrtc/api/api_tests.gyp new file mode 100644 index 0000000000..98b74dee6a --- /dev/null +++ b/webrtc/api/api_tests.gyp @@ -0,0 +1,37 @@ +# Copyright (c) 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. + +{ + 'includes': [ '../build/common.gypi', ], + 'conditions': [ + ['OS=="ios"', { + 'targets': [ + { + 'target_name': 'rtc_api_objc_test', + 'type': 'executable', + 'dependencies': [ + '<(webrtc_root)/api/api.gyp:rtc_api_objc', + '<(webrtc_root)/base/base_tests.gyp:rtc_base_tests_utils', + ], + 'sources': [ + 'objctests/RTCIceServerTest.mm' + ], + 'xcode_settings': { + 'CLANG_ENABLE_OBJC_ARC': 'YES', + 'CLANG_WARN_OBJC_MISSING_PROPERTY_SYNTHESIS': 'YES', + 'GCC_PREFIX_HEADER': 'objc/WebRTC-Prefix.pch', + # |-ObjC| flag needed to make sure category method implementations + # are included: + # https://developer.apple.com/library/mac/qa/qa1490/_index.html + 'OTHER_LDFLAGS': ['-ObjC'], + }, + } + ], + }], # OS=="ios" + ], +} diff --git a/webrtc/api/objc/OWNERS b/webrtc/api/objc/OWNERS new file mode 100644 index 0000000000..cd06158b7f --- /dev/null +++ b/webrtc/api/objc/OWNERS @@ -0,0 +1 @@ +tkchin@webrtc.org diff --git a/webrtc/api/objc/README b/webrtc/api/objc/README new file mode 100644 index 0000000000..bd33e61921 --- /dev/null +++ b/webrtc/api/objc/README @@ -0,0 +1,3 @@ +This is a work-in-progress to update the Objective-C API according to the W3C +specification. The Objective-C API located at talk/app/webrtc/objc is +deprecated, but will remain for the time being. diff --git a/webrtc/api/objc/RTCIceServer+Private.h b/webrtc/api/objc/RTCIceServer+Private.h new file mode 100644 index 0000000000..59f5a92dff --- /dev/null +++ b/webrtc/api/objc/RTCIceServer+Private.h @@ -0,0 +1,28 @@ +/* + * 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 "RTCIceServer.h" + +#include "talk/app/webrtc/peerconnectioninterface.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface RTCIceServer () + +/** + * IceServer struct representation of this RTCIceServer object's data. + * This is needed to pass to the underlying C++ APIs. + */ +@property(nonatomic, readonly) + webrtc::PeerConnectionInterface::IceServer iceServer; + +@end + +NS_ASSUME_NONNULL_END diff --git a/webrtc/api/objc/RTCIceServer.h b/webrtc/api/objc/RTCIceServer.h new file mode 100644 index 0000000000..be4e0d7b6e --- /dev/null +++ b/webrtc/api/objc/RTCIceServer.h @@ -0,0 +1,42 @@ +/* + * 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 + +NS_ASSUME_NONNULL_BEGIN + +@interface RTCIceServer : NSObject + +/** URI(s) for this server represented as NSStrings. */ +@property(nonatomic, copy, readonly) NSArray *urlStrings; + +/** Username to use if this RTCIceServer object is a TURN server. */ +@property(nonatomic, copy, readonly, nullable) NSString *username; + +/** Credential to use if this RTCIceServer object is a TURN server. */ +@property(nonatomic, copy, readonly, nullable) NSString *credential; + +- (instancetype)init NS_UNAVAILABLE; + +/** Convenience initializer for a server with no authentication (e.g. STUN). */ +- (instancetype)initWithURLStrings:(NSArray *)urlStrings; + +/** + * Initialize an RTCIceServer with its associated URLs, optional username, + * optional credential, and credentialType. + */ +- (instancetype)initWithURLStrings:(NSArray *)urlStrings + username:(nullable NSString *)username + credential:(nullable NSString *)credential + NS_DESIGNATED_INITIALIZER; + +@end + +NS_ASSUME_NONNULL_END diff --git a/webrtc/api/objc/RTCIceServer.mm b/webrtc/api/objc/RTCIceServer.mm new file mode 100644 index 0000000000..7a898e06d5 --- /dev/null +++ b/webrtc/api/objc/RTCIceServer.mm @@ -0,0 +1,64 @@ +/* + * 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 "RTCIceServer.h" + +#import "webrtc/api/objc/RTCIceServer+Private.h" +#import "webrtc/base/objc/NSString+StdString.h" + +@implementation RTCIceServer + +@synthesize urlStrings = _urlStrings; +@synthesize username = _username; +@synthesize credential = _credential; + +- (instancetype)initWithURLStrings:(NSArray *)urlStrings { + NSParameterAssert(urlStrings.count); + return [self initWithURLStrings:urlStrings + username:nil + credential:nil]; +} + +- (instancetype)initWithURLStrings:(NSArray *)urlStrings + username:(NSString *)username + credential:(NSString *)credential { + NSParameterAssert(urlStrings.count); + if (self = [super init]) { + _urlStrings = [[NSArray alloc] initWithArray:urlStrings copyItems:YES]; + _username = [username copy]; + _credential = [credential copy]; + } + return self; +} + +- (NSString *)description { + return [NSString stringWithFormat:@"RTCIceServer:\n%@\n%@\n%@", + _urlStrings, + _username, + _credential]; +} + +#pragma mark - Private + +- (webrtc::PeerConnectionInterface::IceServer)iceServer { + __block webrtc::PeerConnectionInterface::IceServer iceServer; + + iceServer.username = [NSString stdStringForString:_username]; + iceServer.password = [NSString stdStringForString:_credential]; + + [_urlStrings enumerateObjectsUsingBlock:^(NSString *url, + NSUInteger idx, + BOOL *stop) { + iceServer.urls.push_back(url.stdString); + }]; + return iceServer; +} + +@end diff --git a/webrtc/api/objc/WebRTC-Prefix.pch b/webrtc/api/objc/WebRTC-Prefix.pch new file mode 100644 index 0000000000..990b1602da --- /dev/null +++ b/webrtc/api/objc/WebRTC-Prefix.pch @@ -0,0 +1,13 @@ +/* + * 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. + */ + +#if !defined(__has_feature) || !__has_feature(objc_arc) +#error "This file requires ARC support." +#endif diff --git a/webrtc/api/objctests/RTCIceServerTest.mm b/webrtc/api/objctests/RTCIceServerTest.mm new file mode 100644 index 0000000000..5fa43f8447 --- /dev/null +++ b/webrtc/api/objctests/RTCIceServerTest.mm @@ -0,0 +1,84 @@ +/* + * 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 + +#include + +#include "webrtc/base/gunit.h" + +#import "webrtc/api/objc/RTCIceServer.h" +#import "webrtc/api/objc/RTCIceServer+Private.h" + +@interface RTCIceServerTest : NSObject +- (void)testOneURLServer; +- (void)testTwoURLServer; +- (void)testPasswordCredential; +@end + +@implementation RTCIceServerTest + +- (void)testOneURLServer { + RTCIceServer *server = [[RTCIceServer alloc] initWithURLStrings:@[ + @"stun:stun1.example.net" ]]; + + webrtc::PeerConnectionInterface::IceServer iceStruct = server.iceServer; + EXPECT_EQ((size_t)1, iceStruct.urls.size()); + EXPECT_EQ("stun:stun1.example.net", iceStruct.urls.front()); + EXPECT_EQ("", iceStruct.username); + EXPECT_EQ("", iceStruct.password); +} + +- (void)testTwoURLServer { + RTCIceServer *server = [[RTCIceServer alloc] initWithURLStrings:@[ + @"turn1:turn1.example.net", @"turn2:turn2.example.net" ]]; + + webrtc::PeerConnectionInterface::IceServer iceStruct = server.iceServer; + EXPECT_EQ((size_t)2, iceStruct.urls.size()); + EXPECT_EQ("turn1:turn1.example.net", iceStruct.urls.front()); + EXPECT_EQ("turn2:turn2.example.net", iceStruct.urls.back()); + EXPECT_EQ("", iceStruct.username); + EXPECT_EQ("", iceStruct.password); +} + +- (void)testPasswordCredential { + RTCIceServer *server = [[RTCIceServer alloc] + initWithURLStrings:@[ @"turn1:turn1.example.net" ] + username:@"username" + credential:@"credential"]; + webrtc::PeerConnectionInterface::IceServer iceStruct = server.iceServer; + EXPECT_EQ((size_t)1, iceStruct.urls.size()); + EXPECT_EQ("turn1:turn1.example.net", iceStruct.urls.front()); + EXPECT_EQ("username", iceStruct.username); + EXPECT_EQ("credential", iceStruct.password); +} + +@end + +TEST(RTCIceServerTest, OneURLTest) { + @autoreleasepool { + RTCIceServerTest *test = [[RTCIceServerTest alloc] init]; + [test testOneURLServer]; + } +} + +TEST(RTCIceServerTest, TwoURLTest) { + @autoreleasepool { + RTCIceServerTest *test = [[RTCIceServerTest alloc] init]; + [test testTwoURLServer]; + } +} + +TEST(RTCIceServerTest, PasswordCredentialTest) { + @autoreleasepool { + RTCIceServerTest *test = [[RTCIceServerTest alloc] init]; + [test testPasswordCredential]; + } +} diff --git a/webrtc/base/BUILD.gn b/webrtc/base/BUILD.gn index ca96e920e5..b867d8734f 100644 --- a/webrtc/base/BUILD.gn +++ b/webrtc/base/BUILD.gn @@ -48,6 +48,7 @@ config("openssl_config") { config("ios_config") { libs = [ + "AVFoundation.framework", "CFNetwork.framework", #"Foundation.framework", # Already included in //build/config:default_libs. @@ -618,6 +619,8 @@ if (is_ios) { public_configs = [ "..:common_inherited_config" ] sources = [ + "objc/NSString+StdString.h", + "objc/NSString+StdString.mm", "objc/RTCCameraPreviewView.h", "objc/RTCCameraPreviewView.m", "objc/RTCDispatcher.h", diff --git a/webrtc/base/base.gyp b/webrtc/base/base.gyp index 77cd011f1c..9ba6afc39e 100644 --- a/webrtc/base/base.gyp +++ b/webrtc/base/base.gyp @@ -33,13 +33,28 @@ 'rtc_base', ], 'sources': [ - 'objc/RTCCameraPreviewView.h', - 'objc/RTCCameraPreviewView.m', + 'objc/NSString+StdString.h', + 'objc/NSString+StdString.mm', 'objc/RTCDispatcher.h', 'objc/RTCDispatcher.m', 'objc/RTCLogging.h', 'objc/RTCLogging.mm', ], + 'conditions': [ + ['OS=="ios"', { + 'sources': [ + 'objc/RTCCameraPreviewView.h', + 'objc/RTCCameraPreviewView.m', + ], + 'all_dependent_settings': { + 'xcode_settings': { + 'OTHER_LDFLAGS': [ + '-framework AVFoundation', + ], + }, + }, + }], + ], 'xcode_settings': { 'CLANG_ENABLE_OBJC_ARC': 'YES', 'CLANG_WARN_OBJC_MISSING_PROPERTY_SYNTHESIS': 'YES', diff --git a/webrtc/base/objc/NSString+StdString.h b/webrtc/base/objc/NSString+StdString.h new file mode 100644 index 0000000000..532032b3fc --- /dev/null +++ b/webrtc/base/objc/NSString+StdString.h @@ -0,0 +1,25 @@ +/* + * 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 + +#include + +NS_ASSUME_NONNULL_BEGIN + +@interface NSString (StdString) + +@property(nonatomic, readonly) std::string stdString; + ++ (std::string)stdStringForString:(NSString *)nsString; + +@end + +NS_ASSUME_NONNULL_END diff --git a/webrtc/base/objc/NSString+StdString.mm b/webrtc/base/objc/NSString+StdString.mm new file mode 100644 index 0000000000..95cbe7aacf --- /dev/null +++ b/webrtc/base/objc/NSString+StdString.mm @@ -0,0 +1,25 @@ +/* + * 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 "NSString+StdString.h" + +@implementation NSString (StdString) + +- (std::string)stdString { + return [NSString stdStringForString:self]; +} + ++ (std::string)stdStringForString:(NSString *)nsString { + NSData *charData = [nsString dataUsingEncoding:NSUTF8StringEncoding]; + return std::string(reinterpret_cast(charData.bytes), + charData.length); +} + +@end diff --git a/webrtc/webrtc_tests.gypi b/webrtc/webrtc_tests.gypi index 238778bf10..796711353f 100644 --- a/webrtc/webrtc_tests.gypi +++ b/webrtc/webrtc_tests.gypi @@ -32,6 +32,11 @@ '<(DEPTH)/testing/android/native_test.gyp:native_test_native_code', ], }], + ['OS=="ios"', { + 'dependencies': [ + 'api/api_tests.gyp:rtc_api_objc_test', + ] + }] ], }, {