blob: 3b9d84584113bd458d139810309eb925eda037f6 [file] [log] [blame]
hjon6f5ca082016-01-07 09:29:29 -08001/*
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
tkchin9eeb6242016-04-27 01:54:20 -070011#import "RTCMediaConstraints+Private.h"
hjon6f5ca082016-01-07 09:29:29 -080012
tkchin9eeb6242016-04-27 01:54:20 -070013#import "NSString+StdString.h"
hjon6f5ca082016-01-07 09:29:29 -080014
kwibergbfefb032016-05-01 14:53:46 -070015#include <memory>
16
magjed78810b62016-08-17 11:07:37 -070017NSString * const kRTCMediaConstraintsMinAspectRatio =
18 @(webrtc::MediaConstraintsInterface::kMinAspectRatio);
19NSString * const kRTCMediaConstraintsMaxAspectRatio =
20 @(webrtc::MediaConstraintsInterface::kMaxAspectRatio);
21NSString * const kRTCMediaConstraintsMinWidth =
22 @(webrtc::MediaConstraintsInterface::kMinWidth);
23NSString * const kRTCMediaConstraintsMaxWidth =
24 @(webrtc::MediaConstraintsInterface::kMaxWidth);
25NSString * const kRTCMediaConstraintsMinHeight =
26 @(webrtc::MediaConstraintsInterface::kMinHeight);
27NSString * const kRTCMediaConstraintsMaxHeight =
28 @(webrtc::MediaConstraintsInterface::kMaxHeight);
29NSString * const kRTCMediaConstraintsMinFrameRate =
30 @(webrtc::MediaConstraintsInterface::kMinFrameRate);
31NSString * const kRTCMediaConstraintsMaxFrameRate =
32 @(webrtc::MediaConstraintsInterface::kMaxFrameRate);
tkchinab1293a2016-08-30 12:35:05 -070033NSString * const kRTCMediaConstraintsLevelControl =
34 @(webrtc::MediaConstraintsInterface::kLevelControl);
haysc98c43742017-02-03 13:03:39 -080035NSString * const kRTCMediaConstraintsAudioNetworkAdaptorConfig =
36 @(webrtc::MediaConstraintsInterface::kAudioNetworkAdaptorConfig);
tkchinab1293a2016-08-30 12:35:05 -070037
38NSString * const kRTCMediaConstraintsValueTrue =
39 @(webrtc::MediaConstraintsInterface::kValueTrue);
40NSString * const kRTCMediaConstraintsValueFalse =
41 @(webrtc::MediaConstraintsInterface::kValueFalse);
magjed78810b62016-08-17 11:07:37 -070042
hjon6f5ca082016-01-07 09:29:29 -080043namespace webrtc {
44
45MediaConstraints::~MediaConstraints() {}
46
47MediaConstraints::MediaConstraints() {}
48
49MediaConstraints::MediaConstraints(
50 const MediaConstraintsInterface::Constraints& mandatory,
51 const MediaConstraintsInterface::Constraints& optional)
52 : mandatory_(mandatory), optional_(optional) {}
53
54const MediaConstraintsInterface::Constraints&
55MediaConstraints::GetMandatory() const {
56 return mandatory_;
57}
58
59const MediaConstraintsInterface::Constraints&
60MediaConstraints::GetOptional() const {
61 return optional_;
62}
63
64} // namespace webrtc
65
66
67@implementation RTCMediaConstraints {
Jon Hjelle32e0c012016-03-08 16:04:46 -080068 NSDictionary<NSString *, NSString *> *_mandatory;
69 NSDictionary<NSString *, NSString *> *_optional;
hjon6f5ca082016-01-07 09:29:29 -080070}
71
72- (instancetype)initWithMandatoryConstraints:
Jon Hjelle32e0c012016-03-08 16:04:46 -080073 (NSDictionary<NSString *, NSString *> *)mandatory
hjon6f5ca082016-01-07 09:29:29 -080074 optionalConstraints:
Jon Hjelle32e0c012016-03-08 16:04:46 -080075 (NSDictionary<NSString *, NSString *> *)optional {
hjon6f5ca082016-01-07 09:29:29 -080076 if (self = [super init]) {
77 _mandatory = [[NSDictionary alloc] initWithDictionary:mandatory
78 copyItems:YES];
79 _optional = [[NSDictionary alloc] initWithDictionary:optional
80 copyItems:YES];
81 }
82 return self;
83}
84
85- (NSString *)description {
86 return [NSString stringWithFormat:@"RTCMediaConstraints:\n%@\n%@",
87 _mandatory,
88 _optional];
89}
90
91#pragma mark - Private
92
kwibergbfefb032016-05-01 14:53:46 -070093- (std::unique_ptr<webrtc::MediaConstraints>)nativeConstraints {
hjon6f5ca082016-01-07 09:29:29 -080094 webrtc::MediaConstraintsInterface::Constraints mandatory =
95 [[self class] nativeConstraintsForConstraints:_mandatory];
96 webrtc::MediaConstraintsInterface::Constraints optional =
97 [[self class] nativeConstraintsForConstraints:_optional];
98
99 webrtc::MediaConstraints *nativeConstraints =
100 new webrtc::MediaConstraints(mandatory, optional);
kwibergbfefb032016-05-01 14:53:46 -0700101 return std::unique_ptr<webrtc::MediaConstraints>(nativeConstraints);
hjon6f5ca082016-01-07 09:29:29 -0800102}
103
104+ (webrtc::MediaConstraintsInterface::Constraints)
105 nativeConstraintsForConstraints:
Jon Hjelle32e0c012016-03-08 16:04:46 -0800106 (NSDictionary<NSString *, NSString *> *)constraints {
hjon6f5ca082016-01-07 09:29:29 -0800107 webrtc::MediaConstraintsInterface::Constraints nativeConstraints;
108 for (NSString *key in constraints) {
109 NSAssert([key isKindOfClass:[NSString class]],
110 @"%@ is not an NSString.", key);
hjon6b039952016-02-25 12:32:58 -0800111 NSString *value = [constraints objectForKey:key];
112 NSAssert([value isKindOfClass:[NSString class]],
113 @"%@ is not an NSString.", value);
haysc98c43742017-02-03 13:03:39 -0800114 if ([kRTCMediaConstraintsAudioNetworkAdaptorConfig isEqualToString:key]) {
115 // This value is base64 encoded.
116 NSData *charData = [[NSData alloc] initWithBase64EncodedString:value options:0];
117 std::string configValue =
118 std::string(reinterpret_cast<const char *>(charData.bytes), charData.length);
119 nativeConstraints.push_back(webrtc::MediaConstraintsInterface::Constraint(
120 key.stdString, configValue));
121 } else {
122 nativeConstraints.push_back(webrtc::MediaConstraintsInterface::Constraint(
123 key.stdString, value.stdString));
124 }
hjon6f5ca082016-01-07 09:29:29 -0800125 }
126 return nativeConstraints;
127}
128
129@end