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}
diff --git a/webrtc/api/BUILD.gn b/webrtc/api/BUILD.gn
index 37109a1..26b7226 100644
--- a/webrtc/api/BUILD.gn
+++ b/webrtc/api/BUILD.gn
@@ -12,12 +12,17 @@
   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",
diff --git a/webrtc/api/api.gyp b/webrtc/api/api.gyp
index 5c6cd0d..d42d99d 100644
--- a/webrtc/api/api.gyp
+++ b/webrtc/api/api.gyp
@@ -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',
diff --git a/webrtc/api/api_tests.gyp b/webrtc/api/api_tests.gyp
index 98b74de..83c83e1 100644
--- a/webrtc/api/api_tests.gyp
+++ b/webrtc/api/api_tests.gyp
@@ -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',
diff --git a/webrtc/api/objc/RTCIceCandidate+Private.h b/webrtc/api/objc/RTCIceCandidate+Private.h
new file mode 100644
index 0000000..ca95a43
--- /dev/null
+++ b/webrtc/api/objc/RTCIceCandidate+Private.h
@@ -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
diff --git a/webrtc/api/objc/RTCIceCandidate.h b/webrtc/api/objc/RTCIceCandidate.h
new file mode 100644
index 0000000..41ea69e
--- /dev/null
+++ b/webrtc/api/objc/RTCIceCandidate.h
@@ -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
diff --git a/webrtc/api/objc/RTCIceCandidate.mm b/webrtc/api/objc/RTCIceCandidate.mm
new file mode 100644
index 0000000..9e094f6
--- /dev/null
+++ b/webrtc/api/objc/RTCIceCandidate.mm
@@ -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
diff --git a/webrtc/api/objctests/RTCIceCandidateTest.mm b/webrtc/api/objctests/RTCIceCandidateTest.mm
new file mode 100644
index 0000000..391db44
--- /dev/null
+++ b/webrtc/api/objctests/RTCIceCandidateTest.mm
@@ -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];
+  }
+}
diff --git a/webrtc/base/objc/NSString+StdString.h b/webrtc/base/objc/NSString+StdString.h
index 532032b..8bf6cc9 100644
--- a/webrtc/base/objc/NSString+StdString.h
+++ b/webrtc/base/objc/NSString+StdString.h
@@ -19,6 +19,7 @@
 @property(nonatomic, readonly) std::string stdString;
 
 + (std::string)stdStringForString:(NSString *)nsString;
++ (NSString *)stringForStdString:(const std::string&)stdString;
 
 @end
 
diff --git a/webrtc/base/objc/NSString+StdString.mm b/webrtc/base/objc/NSString+StdString.mm
index 95cbe7a..3210ff0 100644
--- a/webrtc/base/objc/NSString+StdString.mm
+++ b/webrtc/base/objc/NSString+StdString.mm
@@ -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