blob: 0f46e4b8fed3683500c1b26f0b4b54935ce9312d [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
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020013#import "helpers/NSString+StdString.h"
hjon6f5ca082016-01-07 09:29:29 -080014
kwibergbfefb032016-05-01 14:53:46 -070015#include <memory>
16
Niels Möllerdac03d92019-02-13 08:52:27 +010017NSString *const kRTCMediaConstraintsAudioNetworkAdaptorConfig =
18 @(webrtc::MediaConstraints::kAudioNetworkAdaptorConfig);
tkchinab1293a2016-08-30 12:35:05 -070019
Niels Möllerdac03d92019-02-13 08:52:27 +010020NSString *const kRTCMediaConstraintsIceRestart = @(webrtc::MediaConstraints::kIceRestart);
21NSString *const kRTCMediaConstraintsOfferToReceiveAudio =
22 @(webrtc::MediaConstraints::kOfferToReceiveAudio);
23NSString *const kRTCMediaConstraintsOfferToReceiveVideo =
24 @(webrtc::MediaConstraints::kOfferToReceiveVideo);
25NSString *const kRTCMediaConstraintsVoiceActivityDetection =
26 @(webrtc::MediaConstraints::kVoiceActivityDetection);
VladimirTechMan7b3eca92017-05-31 18:25:48 -070027
Niels Möllerdac03d92019-02-13 08:52:27 +010028NSString *const kRTCMediaConstraintsValueTrue = @(webrtc::MediaConstraints::kValueTrue);
29NSString *const kRTCMediaConstraintsValueFalse = @(webrtc::MediaConstraints::kValueFalse);
hjon6f5ca082016-01-07 09:29:29 -080030
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020031@implementation RTC_OBJC_TYPE (RTCMediaConstraints) {
Jon Hjelle32e0c012016-03-08 16:04:46 -080032 NSDictionary<NSString *, NSString *> *_mandatory;
33 NSDictionary<NSString *, NSString *> *_optional;
hjon6f5ca082016-01-07 09:29:29 -080034}
35
36- (instancetype)initWithMandatoryConstraints:
Jon Hjelle32e0c012016-03-08 16:04:46 -080037 (NSDictionary<NSString *, NSString *> *)mandatory
hjon6f5ca082016-01-07 09:29:29 -080038 optionalConstraints:
Jon Hjelle32e0c012016-03-08 16:04:46 -080039 (NSDictionary<NSString *, NSString *> *)optional {
hjon6f5ca082016-01-07 09:29:29 -080040 if (self = [super init]) {
41 _mandatory = [[NSDictionary alloc] initWithDictionary:mandatory
42 copyItems:YES];
43 _optional = [[NSDictionary alloc] initWithDictionary:optional
44 copyItems:YES];
45 }
46 return self;
47}
48
49- (NSString *)description {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020050 return [NSString
51 stringWithFormat:@"RTC_OBJC_TYPE(RTCMediaConstraints):\n%@\n%@", _mandatory, _optional];
hjon6f5ca082016-01-07 09:29:29 -080052}
53
54#pragma mark - Private
55
kwibergbfefb032016-05-01 14:53:46 -070056- (std::unique_ptr<webrtc::MediaConstraints>)nativeConstraints {
Niels Möllerdac03d92019-02-13 08:52:27 +010057 webrtc::MediaConstraints::Constraints mandatory =
hjon6f5ca082016-01-07 09:29:29 -080058 [[self class] nativeConstraintsForConstraints:_mandatory];
Niels Möllerdac03d92019-02-13 08:52:27 +010059 webrtc::MediaConstraints::Constraints optional =
hjon6f5ca082016-01-07 09:29:29 -080060 [[self class] nativeConstraintsForConstraints:_optional];
61
62 webrtc::MediaConstraints *nativeConstraints =
63 new webrtc::MediaConstraints(mandatory, optional);
kwibergbfefb032016-05-01 14:53:46 -070064 return std::unique_ptr<webrtc::MediaConstraints>(nativeConstraints);
hjon6f5ca082016-01-07 09:29:29 -080065}
66
Niels Möllerdac03d92019-02-13 08:52:27 +010067+ (webrtc::MediaConstraints::Constraints)nativeConstraintsForConstraints:
68 (NSDictionary<NSString *, NSString *> *)constraints {
69 webrtc::MediaConstraints::Constraints nativeConstraints;
hjon6f5ca082016-01-07 09:29:29 -080070 for (NSString *key in constraints) {
71 NSAssert([key isKindOfClass:[NSString class]],
72 @"%@ is not an NSString.", key);
hjon6b039952016-02-25 12:32:58 -080073 NSString *value = [constraints objectForKey:key];
74 NSAssert([value isKindOfClass:[NSString class]],
75 @"%@ is not an NSString.", value);
haysc98c43742017-02-03 13:03:39 -080076 if ([kRTCMediaConstraintsAudioNetworkAdaptorConfig isEqualToString:key]) {
77 // This value is base64 encoded.
78 NSData *charData = [[NSData alloc] initWithBase64EncodedString:value options:0];
79 std::string configValue =
80 std::string(reinterpret_cast<const char *>(charData.bytes), charData.length);
Niels Möllerdac03d92019-02-13 08:52:27 +010081 nativeConstraints.push_back(webrtc::MediaConstraints::Constraint(key.stdString, configValue));
haysc98c43742017-02-03 13:03:39 -080082 } else {
Niels Möllerdac03d92019-02-13 08:52:27 +010083 nativeConstraints.push_back(
84 webrtc::MediaConstraints::Constraint(key.stdString, value.stdString));
haysc98c43742017-02-03 13:03:39 -080085 }
hjon6f5ca082016-01-07 09:29:29 -080086 }
87 return nativeConstraints;
88}
89
90@end