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
11#import "RTCMediaConstraints+Private.h"
12
13#import "helpers/NSString+StdString.h"
14
15#include <memory>
16
17NSString *const kRTCMediaConstraintsAudioNetworkAdaptorConfig =
18    @(webrtc::MediaConstraints::kAudioNetworkAdaptorConfig);
19
20NSString *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);
27
28NSString *const kRTCMediaConstraintsValueTrue = @(webrtc::MediaConstraints::kValueTrue);
29NSString *const kRTCMediaConstraintsValueFalse = @(webrtc::MediaConstraints::kValueFalse);
30
31@implementation RTC_OBJC_TYPE (RTCMediaConstraints) {
32  NSDictionary<NSString *, NSString *> *_mandatory;
33  NSDictionary<NSString *, NSString *> *_optional;
34}
35
36- (instancetype)initWithMandatoryConstraints:
37    (NSDictionary<NSString *, NSString *> *)mandatory
38                         optionalConstraints:
39    (NSDictionary<NSString *, NSString *> *)optional {
40  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
51      stringWithFormat:@"RTC_OBJC_TYPE(RTCMediaConstraints):\n%@\n%@", _mandatory, _optional];
52}
53
54#pragma mark - Private
55
56- (std::unique_ptr<webrtc::MediaConstraints>)nativeConstraints {
57  webrtc::MediaConstraints::Constraints mandatory =
58      [[self class] nativeConstraintsForConstraints:_mandatory];
59  webrtc::MediaConstraints::Constraints optional =
60      [[self class] nativeConstraintsForConstraints:_optional];
61
62  webrtc::MediaConstraints *nativeConstraints =
63      new webrtc::MediaConstraints(mandatory, optional);
64  return std::unique_ptr<webrtc::MediaConstraints>(nativeConstraints);
65}
66
67+ (webrtc::MediaConstraints::Constraints)nativeConstraintsForConstraints:
68    (NSDictionary<NSString *, NSString *> *)constraints {
69  webrtc::MediaConstraints::Constraints nativeConstraints;
70  for (NSString *key in constraints) {
71    NSAssert([key isKindOfClass:[NSString class]],
72             @"%@ is not an NSString.", key);
73    NSString *value = [constraints objectForKey:key];
74    NSAssert([value isKindOfClass:[NSString class]],
75             @"%@ is not an NSString.", value);
76    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);
81      nativeConstraints.push_back(webrtc::MediaConstraints::Constraint(key.stdString, configValue));
82    } else {
83      nativeConstraints.push_back(
84          webrtc::MediaConstraints::Constraint(key.stdString, value.stdString));
85    }
86  }
87  return nativeConstraints;
88}
89
90@end
91