blob: bfdbdde6c55d73180acbc3d1f9addd6646afc0ba [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
31@implementation 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 {
50 return [NSString stringWithFormat:@"RTCMediaConstraints:\n%@\n%@",
51 _mandatory,
52 _optional];
53}
54
55#pragma mark - Private
56
kwibergbfefb032016-05-01 14:53:46 -070057- (std::unique_ptr<webrtc::MediaConstraints>)nativeConstraints {
Niels Möllerdac03d92019-02-13 08:52:27 +010058 webrtc::MediaConstraints::Constraints mandatory =
hjon6f5ca082016-01-07 09:29:29 -080059 [[self class] nativeConstraintsForConstraints:_mandatory];
Niels Möllerdac03d92019-02-13 08:52:27 +010060 webrtc::MediaConstraints::Constraints optional =
hjon6f5ca082016-01-07 09:29:29 -080061 [[self class] nativeConstraintsForConstraints:_optional];
62
63 webrtc::MediaConstraints *nativeConstraints =
64 new webrtc::MediaConstraints(mandatory, optional);
kwibergbfefb032016-05-01 14:53:46 -070065 return std::unique_ptr<webrtc::MediaConstraints>(nativeConstraints);
hjon6f5ca082016-01-07 09:29:29 -080066}
67
Niels Möllerdac03d92019-02-13 08:52:27 +010068+ (webrtc::MediaConstraints::Constraints)nativeConstraintsForConstraints:
69 (NSDictionary<NSString *, NSString *> *)constraints {
70 webrtc::MediaConstraints::Constraints nativeConstraints;
hjon6f5ca082016-01-07 09:29:29 -080071 for (NSString *key in constraints) {
72 NSAssert([key isKindOfClass:[NSString class]],
73 @"%@ is not an NSString.", key);
hjon6b039952016-02-25 12:32:58 -080074 NSString *value = [constraints objectForKey:key];
75 NSAssert([value isKindOfClass:[NSString class]],
76 @"%@ is not an NSString.", value);
haysc98c43742017-02-03 13:03:39 -080077 if ([kRTCMediaConstraintsAudioNetworkAdaptorConfig isEqualToString:key]) {
78 // This value is base64 encoded.
79 NSData *charData = [[NSData alloc] initWithBase64EncodedString:value options:0];
80 std::string configValue =
81 std::string(reinterpret_cast<const char *>(charData.bytes), charData.length);
Niels Möllerdac03d92019-02-13 08:52:27 +010082 nativeConstraints.push_back(webrtc::MediaConstraints::Constraint(key.stdString, configValue));
haysc98c43742017-02-03 13:03:39 -080083 } else {
Niels Möllerdac03d92019-02-13 08:52:27 +010084 nativeConstraints.push_back(
85 webrtc::MediaConstraints::Constraint(key.stdString, value.stdString));
haysc98c43742017-02-03 13:03:39 -080086 }
hjon6f5ca082016-01-07 09:29:29 -080087 }
88 return nativeConstraints;
89}
90
91@end