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 { |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 32 | NSString *constraint = [[self settingsStore] videoResolutionConstraints]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 33 | if (!constraint) { |
| 34 | constraint = [self defaultVideoResolutionMediaConstraint]; |
| 35 | // To ensure consistency add the default to the store. |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 36 | [[self settingsStore] setVideoResolutionConstraints:constraint]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 37 | } |
| 38 | return constraint; |
| 39 | } |
| 40 | |
| 41 | - (BOOL)storeVideoResoultionConstraint:(NSString *)constraint { |
| 42 | if (![[self availableVideoResoultionsMediaConstraints] containsObject:constraint]) { |
| 43 | return NO; |
| 44 | } |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 45 | [[self settingsStore] setVideoResolutionConstraints:constraint]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 46 | return YES; |
| 47 | } |
| 48 | |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 49 | - (nullable NSNumber *)currentMaxBitrateSettingFromStore { |
| 50 | return [[self settingsStore] maxBitrate]; |
| 51 | } |
| 52 | |
| 53 | - (void)storeMaxBitrateSetting:(nullable NSNumber *)bitrate { |
| 54 | [[self settingsStore] setMaxBitrate:bitrate]; |
| 55 | } |
| 56 | |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 57 | #pragma mark - Testable |
| 58 | |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 59 | - (ARDSettingsStore *)settingsStore { |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 60 | if (!_settingsStore) { |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 61 | _settingsStore = [[ARDSettingsStore alloc] init]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 62 | } |
| 63 | return _settingsStore; |
| 64 | } |
| 65 | |
| 66 | - (nullable NSString *)currentVideoResolutionWidthFromStore { |
| 67 | NSString *mediaConstraintFromStore = [self currentVideoResoultionConstraintFromStore]; |
| 68 | |
| 69 | return [self videoResolutionComponentAtIndex:0 inConstraintsString:mediaConstraintFromStore]; |
| 70 | } |
| 71 | |
| 72 | - (nullable NSString *)currentVideoResolutionHeightFromStore { |
| 73 | NSString *mediaConstraintFromStore = [self currentVideoResoultionConstraintFromStore]; |
| 74 | return [self videoResolutionComponentAtIndex:1 inConstraintsString:mediaConstraintFromStore]; |
| 75 | } |
| 76 | |
| 77 | #pragma mark - |
| 78 | |
| 79 | - (NSString *)defaultVideoResolutionMediaConstraint { |
| 80 | return videoResolutionsStaticValues()[0]; |
| 81 | } |
| 82 | |
| 83 | - (nullable NSString *)videoResolutionComponentAtIndex:(int)index |
| 84 | inConstraintsString:(NSString *)constraint { |
| 85 | if (index != 0 && index != 1) { |
| 86 | return nil; |
| 87 | } |
| 88 | NSArray *components = [constraint componentsSeparatedByString:@"x"]; |
| 89 | if (components.count != 2) { |
| 90 | return nil; |
| 91 | } |
| 92 | return components[index]; |
| 93 | } |
| 94 | |
| 95 | #pragma mark - Conversion to RTCMediaConstraints |
| 96 | |
| 97 | - (nullable NSDictionary *)currentMediaConstraintFromStoreAsRTCDictionary { |
| 98 | NSDictionary *mediaConstraintsDictionary = nil; |
| 99 | |
| 100 | NSString *widthConstraint = [self currentVideoResolutionWidthFromStore]; |
| 101 | NSString *heightConstraint = [self currentVideoResolutionHeightFromStore]; |
| 102 | if (widthConstraint && heightConstraint) { |
| 103 | mediaConstraintsDictionary = @{ |
| 104 | kRTCMediaConstraintsMinWidth : widthConstraint, |
| 105 | kRTCMediaConstraintsMinHeight : heightConstraint |
| 106 | }; |
| 107 | } |
| 108 | return mediaConstraintsDictionary; |
| 109 | } |
| 110 | |
| 111 | @end |
| 112 | NS_ASSUME_NONNULL_END |