hjon | 6f5ca08 | 2016-01-07 09:29:29 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 11 | #import "RTCMediaConstraints+Private.h" |
hjon | 6f5ca08 | 2016-01-07 09:29:29 -0800 | [diff] [blame] | 12 | |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame^] | 13 | #import "helpers/NSString+StdString.h" |
hjon | 6f5ca08 | 2016-01-07 09:29:29 -0800 | [diff] [blame] | 14 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 15 | #include <memory> |
| 16 | |
magjed | 78810b6 | 2016-08-17 11:07:37 -0700 | [diff] [blame] | 17 | NSString * const kRTCMediaConstraintsMinAspectRatio = |
| 18 | @(webrtc::MediaConstraintsInterface::kMinAspectRatio); |
| 19 | NSString * const kRTCMediaConstraintsMaxAspectRatio = |
| 20 | @(webrtc::MediaConstraintsInterface::kMaxAspectRatio); |
| 21 | NSString * const kRTCMediaConstraintsMinWidth = |
| 22 | @(webrtc::MediaConstraintsInterface::kMinWidth); |
| 23 | NSString * const kRTCMediaConstraintsMaxWidth = |
| 24 | @(webrtc::MediaConstraintsInterface::kMaxWidth); |
| 25 | NSString * const kRTCMediaConstraintsMinHeight = |
| 26 | @(webrtc::MediaConstraintsInterface::kMinHeight); |
| 27 | NSString * const kRTCMediaConstraintsMaxHeight = |
| 28 | @(webrtc::MediaConstraintsInterface::kMaxHeight); |
| 29 | NSString * const kRTCMediaConstraintsMinFrameRate = |
| 30 | @(webrtc::MediaConstraintsInterface::kMinFrameRate); |
| 31 | NSString * const kRTCMediaConstraintsMaxFrameRate = |
| 32 | @(webrtc::MediaConstraintsInterface::kMaxFrameRate); |
haysc | 98c4374 | 2017-02-03 13:03:39 -0800 | [diff] [blame] | 33 | NSString * const kRTCMediaConstraintsAudioNetworkAdaptorConfig = |
| 34 | @(webrtc::MediaConstraintsInterface::kAudioNetworkAdaptorConfig); |
tkchin | ab1293a | 2016-08-30 12:35:05 -0700 | [diff] [blame] | 35 | |
VladimirTechMan | 7b3eca9 | 2017-05-31 18:25:48 -0700 | [diff] [blame] | 36 | NSString * const kRTCMediaConstraintsIceRestart = |
| 37 | @(webrtc::MediaConstraintsInterface::kIceRestart); |
| 38 | NSString * const kRTCMediaConstraintsOfferToReceiveAudio = |
| 39 | @(webrtc::MediaConstraintsInterface::kOfferToReceiveAudio); |
| 40 | NSString * const kRTCMediaConstraintsOfferToReceiveVideo = |
| 41 | @(webrtc::MediaConstraintsInterface::kOfferToReceiveVideo); |
| 42 | NSString * const kRTCMediaConstraintsVoiceActivityDetection = |
| 43 | @(webrtc::MediaConstraintsInterface::kVoiceActivityDetection); |
| 44 | |
tkchin | ab1293a | 2016-08-30 12:35:05 -0700 | [diff] [blame] | 45 | NSString * const kRTCMediaConstraintsValueTrue = |
| 46 | @(webrtc::MediaConstraintsInterface::kValueTrue); |
| 47 | NSString * const kRTCMediaConstraintsValueFalse = |
| 48 | @(webrtc::MediaConstraintsInterface::kValueFalse); |
magjed | 78810b6 | 2016-08-17 11:07:37 -0700 | [diff] [blame] | 49 | |
hjon | 6f5ca08 | 2016-01-07 09:29:29 -0800 | [diff] [blame] | 50 | namespace webrtc { |
| 51 | |
| 52 | MediaConstraints::~MediaConstraints() {} |
| 53 | |
| 54 | MediaConstraints::MediaConstraints() {} |
| 55 | |
| 56 | MediaConstraints::MediaConstraints( |
| 57 | const MediaConstraintsInterface::Constraints& mandatory, |
| 58 | const MediaConstraintsInterface::Constraints& optional) |
| 59 | : mandatory_(mandatory), optional_(optional) {} |
| 60 | |
| 61 | const MediaConstraintsInterface::Constraints& |
| 62 | MediaConstraints::GetMandatory() const { |
| 63 | return mandatory_; |
| 64 | } |
| 65 | |
| 66 | const MediaConstraintsInterface::Constraints& |
| 67 | MediaConstraints::GetOptional() const { |
| 68 | return optional_; |
| 69 | } |
| 70 | |
| 71 | } // namespace webrtc |
| 72 | |
| 73 | |
| 74 | @implementation RTCMediaConstraints { |
Jon Hjelle | 32e0c01 | 2016-03-08 16:04:46 -0800 | [diff] [blame] | 75 | NSDictionary<NSString *, NSString *> *_mandatory; |
| 76 | NSDictionary<NSString *, NSString *> *_optional; |
hjon | 6f5ca08 | 2016-01-07 09:29:29 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | - (instancetype)initWithMandatoryConstraints: |
Jon Hjelle | 32e0c01 | 2016-03-08 16:04:46 -0800 | [diff] [blame] | 80 | (NSDictionary<NSString *, NSString *> *)mandatory |
hjon | 6f5ca08 | 2016-01-07 09:29:29 -0800 | [diff] [blame] | 81 | optionalConstraints: |
Jon Hjelle | 32e0c01 | 2016-03-08 16:04:46 -0800 | [diff] [blame] | 82 | (NSDictionary<NSString *, NSString *> *)optional { |
hjon | 6f5ca08 | 2016-01-07 09:29:29 -0800 | [diff] [blame] | 83 | if (self = [super init]) { |
| 84 | _mandatory = [[NSDictionary alloc] initWithDictionary:mandatory |
| 85 | copyItems:YES]; |
| 86 | _optional = [[NSDictionary alloc] initWithDictionary:optional |
| 87 | copyItems:YES]; |
| 88 | } |
| 89 | return self; |
| 90 | } |
| 91 | |
| 92 | - (NSString *)description { |
| 93 | return [NSString stringWithFormat:@"RTCMediaConstraints:\n%@\n%@", |
| 94 | _mandatory, |
| 95 | _optional]; |
| 96 | } |
| 97 | |
| 98 | #pragma mark - Private |
| 99 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 100 | - (std::unique_ptr<webrtc::MediaConstraints>)nativeConstraints { |
hjon | 6f5ca08 | 2016-01-07 09:29:29 -0800 | [diff] [blame] | 101 | webrtc::MediaConstraintsInterface::Constraints mandatory = |
| 102 | [[self class] nativeConstraintsForConstraints:_mandatory]; |
| 103 | webrtc::MediaConstraintsInterface::Constraints optional = |
| 104 | [[self class] nativeConstraintsForConstraints:_optional]; |
| 105 | |
| 106 | webrtc::MediaConstraints *nativeConstraints = |
| 107 | new webrtc::MediaConstraints(mandatory, optional); |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 108 | return std::unique_ptr<webrtc::MediaConstraints>(nativeConstraints); |
hjon | 6f5ca08 | 2016-01-07 09:29:29 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | + (webrtc::MediaConstraintsInterface::Constraints) |
| 112 | nativeConstraintsForConstraints: |
Jon Hjelle | 32e0c01 | 2016-03-08 16:04:46 -0800 | [diff] [blame] | 113 | (NSDictionary<NSString *, NSString *> *)constraints { |
hjon | 6f5ca08 | 2016-01-07 09:29:29 -0800 | [diff] [blame] | 114 | webrtc::MediaConstraintsInterface::Constraints nativeConstraints; |
| 115 | for (NSString *key in constraints) { |
| 116 | NSAssert([key isKindOfClass:[NSString class]], |
| 117 | @"%@ is not an NSString.", key); |
hjon | 6b03995 | 2016-02-25 12:32:58 -0800 | [diff] [blame] | 118 | NSString *value = [constraints objectForKey:key]; |
| 119 | NSAssert([value isKindOfClass:[NSString class]], |
| 120 | @"%@ is not an NSString.", value); |
haysc | 98c4374 | 2017-02-03 13:03:39 -0800 | [diff] [blame] | 121 | if ([kRTCMediaConstraintsAudioNetworkAdaptorConfig isEqualToString:key]) { |
| 122 | // This value is base64 encoded. |
| 123 | NSData *charData = [[NSData alloc] initWithBase64EncodedString:value options:0]; |
| 124 | std::string configValue = |
| 125 | std::string(reinterpret_cast<const char *>(charData.bytes), charData.length); |
| 126 | nativeConstraints.push_back(webrtc::MediaConstraintsInterface::Constraint( |
| 127 | key.stdString, configValue)); |
| 128 | } else { |
| 129 | nativeConstraints.push_back(webrtc::MediaConstraintsInterface::Constraint( |
| 130 | key.stdString, value.stdString)); |
| 131 | } |
hjon | 6f5ca08 | 2016-01-07 09:29:29 -0800 | [diff] [blame] | 132 | } |
| 133 | return nativeConstraints; |
| 134 | } |
| 135 | |
| 136 | @end |