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 | |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 20 | static NSArray<NSString *> *videoCodecsStaticValues() { |
| 21 | return @[ @"H264", @"VP8", @"VP9" ]; |
| 22 | } |
| 23 | |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 24 | @interface ARDSettingsModel () { |
| 25 | ARDSettingsStore *_settingsStore; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 26 | } |
| 27 | @end |
| 28 | |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 29 | @implementation ARDSettingsModel |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 30 | |
| 31 | - (NSArray<NSString *> *)availableVideoResoultionsMediaConstraints { |
| 32 | return videoResolutionsStaticValues(); |
| 33 | } |
| 34 | |
| 35 | - (NSString *)currentVideoResoultionConstraintFromStore { |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 36 | NSString *constraint = [[self settingsStore] videoResolutionConstraints]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 37 | if (!constraint) { |
| 38 | constraint = [self defaultVideoResolutionMediaConstraint]; |
| 39 | // To ensure consistency add the default to the store. |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 40 | [[self settingsStore] setVideoResolutionConstraints:constraint]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 41 | } |
| 42 | return constraint; |
| 43 | } |
| 44 | |
| 45 | - (BOOL)storeVideoResoultionConstraint:(NSString *)constraint { |
| 46 | if (![[self availableVideoResoultionsMediaConstraints] containsObject:constraint]) { |
| 47 | return NO; |
| 48 | } |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 49 | [[self settingsStore] setVideoResolutionConstraints:constraint]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 50 | return YES; |
| 51 | } |
| 52 | |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 53 | - (NSArray<NSString *> *)availableVideoCodecs { |
| 54 | return videoCodecsStaticValues(); |
| 55 | } |
| 56 | |
| 57 | - (NSString *)currentVideoCodecSettingFromStore { |
| 58 | NSString *videoCodec = [[self settingsStore] videoCodec]; |
| 59 | if (!videoCodec) { |
| 60 | videoCodec = [self defaultVideoCodecSetting]; |
| 61 | [[self settingsStore] setVideoCodec:videoCodec]; |
| 62 | } |
| 63 | return videoCodec; |
| 64 | } |
| 65 | |
| 66 | - (BOOL)storeVideoCodecSetting:(NSString *)videoCodec { |
| 67 | if (![[self availableVideoCodecs] containsObject:videoCodec]) { |
| 68 | return NO; |
| 69 | } |
| 70 | [[self settingsStore] setVideoCodec:videoCodec]; |
| 71 | return YES; |
| 72 | } |
| 73 | |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 74 | - (nullable NSNumber *)currentMaxBitrateSettingFromStore { |
| 75 | return [[self settingsStore] maxBitrate]; |
| 76 | } |
| 77 | |
| 78 | - (void)storeMaxBitrateSetting:(nullable NSNumber *)bitrate { |
| 79 | [[self settingsStore] setMaxBitrate:bitrate]; |
| 80 | } |
| 81 | |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 82 | #pragma mark - Testable |
| 83 | |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 84 | - (ARDSettingsStore *)settingsStore { |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 85 | if (!_settingsStore) { |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 86 | _settingsStore = [[ARDSettingsStore alloc] init]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 87 | } |
| 88 | return _settingsStore; |
| 89 | } |
| 90 | |
| 91 | - (nullable NSString *)currentVideoResolutionWidthFromStore { |
| 92 | NSString *mediaConstraintFromStore = [self currentVideoResoultionConstraintFromStore]; |
| 93 | |
| 94 | return [self videoResolutionComponentAtIndex:0 inConstraintsString:mediaConstraintFromStore]; |
| 95 | } |
| 96 | |
| 97 | - (nullable NSString *)currentVideoResolutionHeightFromStore { |
| 98 | NSString *mediaConstraintFromStore = [self currentVideoResoultionConstraintFromStore]; |
| 99 | return [self videoResolutionComponentAtIndex:1 inConstraintsString:mediaConstraintFromStore]; |
| 100 | } |
| 101 | |
| 102 | #pragma mark - |
| 103 | |
| 104 | - (NSString *)defaultVideoResolutionMediaConstraint { |
| 105 | return videoResolutionsStaticValues()[0]; |
| 106 | } |
| 107 | |
| 108 | - (nullable NSString *)videoResolutionComponentAtIndex:(int)index |
| 109 | inConstraintsString:(NSString *)constraint { |
| 110 | if (index != 0 && index != 1) { |
| 111 | return nil; |
| 112 | } |
| 113 | NSArray *components = [constraint componentsSeparatedByString:@"x"]; |
| 114 | if (components.count != 2) { |
| 115 | return nil; |
| 116 | } |
| 117 | return components[index]; |
| 118 | } |
| 119 | |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 120 | - (NSString *)defaultVideoCodecSetting { |
| 121 | return videoCodecsStaticValues()[0]; |
| 122 | } |
| 123 | |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 124 | #pragma mark - Conversion to RTCMediaConstraints |
| 125 | |
| 126 | - (nullable NSDictionary *)currentMediaConstraintFromStoreAsRTCDictionary { |
| 127 | NSDictionary *mediaConstraintsDictionary = nil; |
| 128 | |
| 129 | NSString *widthConstraint = [self currentVideoResolutionWidthFromStore]; |
| 130 | NSString *heightConstraint = [self currentVideoResolutionHeightFromStore]; |
| 131 | if (widthConstraint && heightConstraint) { |
| 132 | mediaConstraintsDictionary = @{ |
| 133 | kRTCMediaConstraintsMinWidth : widthConstraint, |
| 134 | kRTCMediaConstraintsMinHeight : heightConstraint |
| 135 | }; |
| 136 | } |
| 137 | return mediaConstraintsDictionary; |
| 138 | } |
| 139 | |
| 140 | @end |
| 141 | NS_ASSUME_NONNULL_END |