Update API for Objective-C RTCIceServer

BUG=

Review URL: https://codereview.webrtc.org/1499653003

Cr-Commit-Position: refs/heads/master@{#11000}
diff --git a/webrtc/api/BUILD.gn b/webrtc/api/BUILD.gn
new file mode 100644
index 0000000..37109a1
--- /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 0000000..cd06158
--- /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 0000000..5c6cd0d
--- /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 0000000..98b74de
--- /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 0000000..cd06158
--- /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 0000000..bd33e61
--- /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 0000000..59f5a92
--- /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 0000000..be4e0d7
--- /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 <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface RTCIceServer : NSObject
+
+/** URI(s) for this server represented as NSStrings. */
+@property(nonatomic, copy, readonly) NSArray<NSString *> *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<NSString *> *)urlStrings;
+
+/**
+ * Initialize an RTCIceServer with its associated URLs, optional username,
+ * optional credential, and credentialType.
+ */
+- (instancetype)initWithURLStrings:(NSArray<NSString *> *)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 0000000..7a898e0
--- /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<NSString *> *)urlStrings {
+  NSParameterAssert(urlStrings.count);
+  return [self initWithURLStrings:urlStrings
+                         username:nil
+                       credential:nil];
+}
+
+- (instancetype)initWithURLStrings:(NSArray<NSString *> *)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 0000000..990b160
--- /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 0000000..5fa43f8
--- /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 <Foundation/Foundation.h>
+
+#include <vector>
+
+#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];
+  }
+}