denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 11 | #import "ARDSettingsModel+Private.h" |
| 12 | #import "ARDSettingsStore.h" |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 13 | #import "WebRTC/RTCMediaConstraints.h" |
| 14 | |
| 15 | NS_ASSUME_NONNULL_BEGIN |
| 16 | static NSArray<NSString *> *videoResolutionsStaticValues() { |
| 17 | return @[ @"640x480", @"960x540", @"1280x720" ]; |
| 18 | } |
| 19 | |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 20 | @interface ARDSettingsModel () { |
| 21 | ARDSettingsStore *_settingsStore; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 22 | } |
| 23 | @end |
| 24 | |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 25 | @implementation ARDSettingsModel |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 26 | |
| 27 | - (NSArray<NSString *> *)availableVideoResoultionsMediaConstraints { |
| 28 | return videoResolutionsStaticValues(); |
| 29 | } |
| 30 | |
| 31 | - (NSString *)currentVideoResoultionConstraintFromStore { |
| 32 | NSString *constraint = [[self settingsStore] videoResolutionConstraintsSetting]; |
| 33 | if (!constraint) { |
| 34 | constraint = [self defaultVideoResolutionMediaConstraint]; |
| 35 | // To ensure consistency add the default to the store. |
| 36 | [[self settingsStore] setVideoResolutionConstraintsSetting:constraint]; |
| 37 | } |
| 38 | return constraint; |
| 39 | } |
| 40 | |
| 41 | - (BOOL)storeVideoResoultionConstraint:(NSString *)constraint { |
| 42 | if (![[self availableVideoResoultionsMediaConstraints] containsObject:constraint]) { |
| 43 | return NO; |
| 44 | } |
| 45 | [[self settingsStore] setVideoResolutionConstraintsSetting:constraint]; |
| 46 | return YES; |
| 47 | } |
| 48 | |
| 49 | #pragma mark - Testable |
| 50 | |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 51 | - (ARDSettingsStore *)settingsStore { |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 52 | if (!_settingsStore) { |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 53 | _settingsStore = [[ARDSettingsStore alloc] init]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 54 | } |
| 55 | return _settingsStore; |
| 56 | } |
| 57 | |
| 58 | - (nullable NSString *)currentVideoResolutionWidthFromStore { |
| 59 | NSString *mediaConstraintFromStore = [self currentVideoResoultionConstraintFromStore]; |
| 60 | |
| 61 | return [self videoResolutionComponentAtIndex:0 inConstraintsString:mediaConstraintFromStore]; |
| 62 | } |
| 63 | |
| 64 | - (nullable NSString *)currentVideoResolutionHeightFromStore { |
| 65 | NSString *mediaConstraintFromStore = [self currentVideoResoultionConstraintFromStore]; |
| 66 | return [self videoResolutionComponentAtIndex:1 inConstraintsString:mediaConstraintFromStore]; |
| 67 | } |
| 68 | |
| 69 | #pragma mark - |
| 70 | |
| 71 | - (NSString *)defaultVideoResolutionMediaConstraint { |
| 72 | return videoResolutionsStaticValues()[0]; |
| 73 | } |
| 74 | |
| 75 | - (nullable NSString *)videoResolutionComponentAtIndex:(int)index |
| 76 | inConstraintsString:(NSString *)constraint { |
| 77 | if (index != 0 && index != 1) { |
| 78 | return nil; |
| 79 | } |
| 80 | NSArray *components = [constraint componentsSeparatedByString:@"x"]; |
| 81 | if (components.count != 2) { |
| 82 | return nil; |
| 83 | } |
| 84 | return components[index]; |
| 85 | } |
| 86 | |
| 87 | #pragma mark - Conversion to RTCMediaConstraints |
| 88 | |
| 89 | - (nullable NSDictionary *)currentMediaConstraintFromStoreAsRTCDictionary { |
| 90 | NSDictionary *mediaConstraintsDictionary = nil; |
| 91 | |
| 92 | NSString *widthConstraint = [self currentVideoResolutionWidthFromStore]; |
| 93 | NSString *heightConstraint = [self currentVideoResolutionHeightFromStore]; |
| 94 | if (widthConstraint && heightConstraint) { |
| 95 | mediaConstraintsDictionary = @{ |
| 96 | kRTCMediaConstraintsMinWidth : widthConstraint, |
| 97 | kRTCMediaConstraintsMinHeight : heightConstraint |
| 98 | }; |
| 99 | } |
| 100 | return mediaConstraintsDictionary; |
| 101 | } |
| 102 | |
| 103 | @end |
| 104 | NS_ASSUME_NONNULL_END |