Update API for Objective-C RTCMediaConstraints.

BUG=

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

Cr-Commit-Position: refs/heads/master@{#11172}
diff --git a/webrtc/api/BUILD.gn b/webrtc/api/BUILD.gn
index 08423fe..8a00e37 100644
--- a/webrtc/api/BUILD.gn
+++ b/webrtc/api/BUILD.gn
@@ -26,6 +26,9 @@
       "objc/RTCIceServer+Private.h",
       "objc/RTCIceServer.h",
       "objc/RTCIceServer.mm",
+      "objc/RTCMediaConstraints+Private.h",
+      "objc/RTCMediaConstraints.h",
+      "objc/RTCMediaConstraints.mm",
       "objc/RTCSessionDescription+Private.h",
       "objc/RTCSessionDescription.h",
       "objc/RTCSessionDescription.mm",
diff --git a/webrtc/api/api.gyp b/webrtc/api/api.gyp
index ee04a4d..4259520 100644
--- a/webrtc/api/api.gyp
+++ b/webrtc/api/api.gyp
@@ -25,6 +25,9 @@
             'objc/RTCIceServer+Private.h',
             'objc/RTCIceServer.h',
             'objc/RTCIceServer.mm',
+            'objc/RTCMediaConstraints+Private.h',
+            'objc/RTCMediaConstraints.h',
+            'objc/RTCMediaConstraints.mm',
             'objc/RTCSessionDescription+Private.h',
             'objc/RTCSessionDescription.h',
             'objc/RTCSessionDescription.mm',
diff --git a/webrtc/api/api_tests.gyp b/webrtc/api/api_tests.gyp
index 466709c..c2c18bc 100644
--- a/webrtc/api/api_tests.gyp
+++ b/webrtc/api/api_tests.gyp
@@ -21,6 +21,7 @@
           'sources': [
             'objctests/RTCIceCandidateTest.mm',
             'objctests/RTCIceServerTest.mm',
+            'objctests/RTCMediaConstraintsTest.mm',
             'objctests/RTCSessionDescriptionTest.mm',
           ],
           'xcode_settings': {
diff --git a/webrtc/api/objc/RTCMediaConstraints+Private.h b/webrtc/api/objc/RTCMediaConstraints+Private.h
new file mode 100644
index 0000000..2c4b722
--- /dev/null
+++ b/webrtc/api/objc/RTCMediaConstraints+Private.h
@@ -0,0 +1,53 @@
+/*
+ *  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 "RTCMediaConstraints.h"
+
+#include "talk/app/webrtc/mediaconstraintsinterface.h"
+#include "webrtc/base/scoped_ptr.h"
+
+namespace webrtc {
+
+class MediaConstraints : public MediaConstraintsInterface {
+ public:
+  virtual ~MediaConstraints();
+  MediaConstraints();
+  MediaConstraints(
+      const MediaConstraintsInterface::Constraints& mandatory,
+      const MediaConstraintsInterface::Constraints& optional);
+  virtual const Constraints& GetMandatory() const;
+  virtual const Constraints& GetOptional() const;
+
+ private:
+  MediaConstraintsInterface::Constraints mandatory_;
+  MediaConstraintsInterface::Constraints optional_;
+};
+
+}  // namespace webrtc
+
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface RTCMediaConstraints ()
+
+/**
+ * A MediaConstraints representation of this RTCMediaConstraints object. This is
+ * needed to pass to the underlying C++ APIs.
+ */
+- (rtc::scoped_ptr<webrtc::MediaConstraints>)nativeConstraints;
+
+/** Return a native Constraints object representing these constraints */
++ (webrtc::MediaConstraintsInterface::Constraints)
+    nativeConstraintsForConstraints:
+        (NSDictionary<NSString *, NSString *> *)constraints;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/webrtc/api/objc/RTCMediaConstraints.h b/webrtc/api/objc/RTCMediaConstraints.h
new file mode 100644
index 0000000..a8ad391
--- /dev/null
+++ b/webrtc/api/objc/RTCMediaConstraints.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 <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface RTCMediaConstraints : NSObject
+
+- (instancetype)init NS_UNAVAILABLE;
+
+/** Initialize with mandatory and/or optional constraints. */
+- (instancetype)initWithMandatoryConstraints:
+    (nullable NSDictionary<NSString *, NSString *> *)mandatory
+                         optionalConstraints:
+    (nullable NSDictionary<NSString *, NSString *> *)optional
+    NS_DESIGNATED_INITIALIZER;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/webrtc/api/objc/RTCMediaConstraints.mm b/webrtc/api/objc/RTCMediaConstraints.mm
new file mode 100644
index 0000000..a53a517
--- /dev/null
+++ b/webrtc/api/objc/RTCMediaConstraints.mm
@@ -0,0 +1,92 @@
+/*
+ *  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 "RTCMediaConstraints.h"
+
+#import "webrtc/api/objc/RTCMediaConstraints+Private.h"
+#import "webrtc/base/objc/NSString+StdString.h"
+
+namespace webrtc {
+
+MediaConstraints::~MediaConstraints() {}
+
+MediaConstraints::MediaConstraints() {}
+
+MediaConstraints::MediaConstraints(
+    const MediaConstraintsInterface::Constraints& mandatory,
+    const MediaConstraintsInterface::Constraints& optional)
+    : mandatory_(mandatory), optional_(optional) {}
+
+const MediaConstraintsInterface::Constraints&
+MediaConstraints::GetMandatory() const {
+  return mandatory_;
+}
+
+const MediaConstraintsInterface::Constraints&
+MediaConstraints::GetOptional() const {
+  return optional_;
+}
+
+}  // namespace webrtc
+
+
+@implementation RTCMediaConstraints {
+  NSDictionary<NSString *, NSString *> *_mandatory;
+  NSDictionary<NSString *, NSString *> *_optional;
+}
+
+- (instancetype)initWithMandatoryConstraints:
+    (NSDictionary<NSString *, NSString *> *)mandatory
+                         optionalConstraints:
+    (NSDictionary<NSString *, NSString *> *)optional {
+  if (self = [super init]) {
+    _mandatory = [[NSDictionary alloc] initWithDictionary:mandatory
+                                                copyItems:YES];
+    _optional = [[NSDictionary alloc] initWithDictionary:optional
+                                               copyItems:YES];
+  }
+  return self;
+}
+
+- (NSString *)description {
+  return [NSString stringWithFormat:@"RTCMediaConstraints:\n%@\n%@",
+                                    _mandatory,
+                                    _optional];
+}
+
+#pragma mark - Private
+
+- (rtc::scoped_ptr<webrtc::MediaConstraints>)nativeConstraints {
+  webrtc::MediaConstraintsInterface::Constraints mandatory =
+      [[self class] nativeConstraintsForConstraints:_mandatory];
+  webrtc::MediaConstraintsInterface::Constraints optional =
+      [[self class] nativeConstraintsForConstraints:_optional];
+
+  webrtc::MediaConstraints *nativeConstraints =
+      new webrtc::MediaConstraints(mandatory, optional);
+  return rtc::scoped_ptr<webrtc::MediaConstraints>(nativeConstraints);
+}
+
++ (webrtc::MediaConstraintsInterface::Constraints)
+    nativeConstraintsForConstraints:
+        (NSDictionary<NSString *, NSString *> *)constraints {
+  webrtc::MediaConstraintsInterface::Constraints nativeConstraints;
+  for (NSString *key in constraints) {
+    NSAssert([key isKindOfClass:[NSString class]],
+             @"%@ is not an NSString.", key);
+    NSAssert([constraints[key] isKindOfClass:[NSString class]],
+             @"%@ is not an NSString.", constraints[key]);
+    nativeConstraints.push_back(webrtc::MediaConstraintsInterface::Constraint(
+        key.stdString, constraints[key].stdString));
+  }
+  return nativeConstraints;
+}
+
+@end
diff --git a/webrtc/api/objctests/RTCMediaConstraintsTest.mm b/webrtc/api/objctests/RTCMediaConstraintsTest.mm
new file mode 100644
index 0000000..44ffe3d
--- /dev/null
+++ b/webrtc/api/objctests/RTCMediaConstraintsTest.mm
@@ -0,0 +1,66 @@
+/*
+ *  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/RTCMediaConstraints.h"
+#import "webrtc/api/objc/RTCMediaConstraints+Private.h"
+#import "webrtc/base/objc/NSString+StdString.h"
+
+@interface RTCMediaConstraintsTest : NSObject
+- (void)testMediaConstraints;
+@end
+
+@implementation RTCMediaConstraintsTest
+
+- (void)testMediaConstraints {
+  NSDictionary *mandatory = @{@"key1": @"value1", @"key2": @"value2"};
+  NSDictionary *optional = @{@"key3": @"value3", @"key4": @"value4"};
+
+  RTCMediaConstraints *constraints = [[RTCMediaConstraints alloc]
+      initWithMandatoryConstraints:mandatory
+               optionalConstraints:optional];
+  rtc::scoped_ptr<webrtc::MediaConstraints> nativeConstraints =
+      [constraints nativeConstraints];
+
+  webrtc::MediaConstraintsInterface::Constraints nativeMandatory =
+      nativeConstraints->GetMandatory();
+  [self expectConstraints:mandatory inNativeConstraints:nativeMandatory];
+
+  webrtc::MediaConstraintsInterface::Constraints nativeOptional =
+      nativeConstraints->GetOptional();
+  [self expectConstraints:optional inNativeConstraints:nativeOptional];
+}
+
+- (void)expectConstraints:(NSDictionary *)constraints
+      inNativeConstraints:
+    (webrtc::MediaConstraintsInterface::Constraints)nativeConstraints {
+  EXPECT_EQ(constraints.count, nativeConstraints.size());
+
+  for (NSString *key in constraints) {
+    NSString *value = constraints[key];
+
+    std::string nativeValue;
+    bool found = nativeConstraints.FindFirst(key.stdString, &nativeValue);
+    EXPECT_TRUE(found);
+    EXPECT_EQ(value.stdString, nativeValue);
+  }
+}
+
+@end
+
+TEST(RTCMediaConstraintsTest, MediaConstraintsTest) {
+  @autoreleasepool {
+    RTCMediaConstraintsTest *test = [[RTCMediaConstraintsTest alloc] init];
+    [test testMediaConstraints];
+  }
+}