From 6f5ca080b87e720c0731f4b5ccb028aa5a234b5f Mon Sep 17 00:00:00 2001 From: hjon Date: Thu, 7 Jan 2016 09:29:29 -0800 Subject: [PATCH] Update API for Objective-C RTCMediaConstraints. BUG= Review URL: https://codereview.webrtc.org/1543033003 Cr-Commit-Position: refs/heads/master@{#11172} --- webrtc/api/BUILD.gn | 3 + webrtc/api/api.gyp | 3 + webrtc/api/api_tests.gyp | 1 + webrtc/api/objc/RTCMediaConstraints+Private.h | 53 +++++++++++ webrtc/api/objc/RTCMediaConstraints.h | 28 ++++++ webrtc/api/objc/RTCMediaConstraints.mm | 92 +++++++++++++++++++ .../api/objctests/RTCMediaConstraintsTest.mm | 66 +++++++++++++ 7 files changed, 246 insertions(+) create mode 100644 webrtc/api/objc/RTCMediaConstraints+Private.h create mode 100644 webrtc/api/objc/RTCMediaConstraints.h create mode 100644 webrtc/api/objc/RTCMediaConstraints.mm create mode 100644 webrtc/api/objctests/RTCMediaConstraintsTest.mm diff --git a/webrtc/api/BUILD.gn b/webrtc/api/BUILD.gn index 08423fedbc..8a00e37f94 100644 --- a/webrtc/api/BUILD.gn +++ b/webrtc/api/BUILD.gn @@ -26,6 +26,9 @@ if (is_ios) { "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 ee04a4dc24..4259520330 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 466709cb8f..c2c18bc693 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 0000000000..2c4b722104 --- /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)nativeConstraints; + +/** Return a native Constraints object representing these constraints */ ++ (webrtc::MediaConstraintsInterface::Constraints) + nativeConstraintsForConstraints: + (NSDictionary *)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 0000000000..a8ad39142e --- /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 + +NS_ASSUME_NONNULL_BEGIN + +@interface RTCMediaConstraints : NSObject + +- (instancetype)init NS_UNAVAILABLE; + +/** Initialize with mandatory and/or optional constraints. */ +- (instancetype)initWithMandatoryConstraints: + (nullable NSDictionary *)mandatory + optionalConstraints: + (nullable NSDictionary *)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 0000000000..a53a517747 --- /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 *_mandatory; + NSDictionary *_optional; +} + +- (instancetype)initWithMandatoryConstraints: + (NSDictionary *)mandatory + optionalConstraints: + (NSDictionary *)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)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(nativeConstraints); +} + ++ (webrtc::MediaConstraintsInterface::Constraints) + nativeConstraintsForConstraints: + (NSDictionary *)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 0000000000..44ffe3d033 --- /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 + +#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 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]; + } +}