blob: 7514689add53b0d5ee747aeae930f0c27a977a13 [file] [log] [blame]
denicijad17d5362016-11-02 02:56:09 -07001/*
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
denicija2256e042016-11-09 06:26:18 -080011#import "ARDSettingsModel+Private.h"
12#import "ARDSettingsStore.h"
denicijad17d5362016-11-02 02:56:09 -070013#import "WebRTC/RTCMediaConstraints.h"
14
15NS_ASSUME_NONNULL_BEGIN
16static NSArray<NSString *> *videoResolutionsStaticValues() {
17 return @[ @"640x480", @"960x540", @"1280x720" ];
18}
19
sakal68b5df92017-03-17 09:01:59 -070020static NSArray<NSString *> *videoCodecsStaticValues() {
21 return @[ @"H264", @"VP8", @"VP9" ];
22}
23
denicija2256e042016-11-09 06:26:18 -080024@interface ARDSettingsModel () {
25 ARDSettingsStore *_settingsStore;
denicijad17d5362016-11-02 02:56:09 -070026}
27@end
28
denicija2256e042016-11-09 06:26:18 -080029@implementation ARDSettingsModel
denicijad17d5362016-11-02 02:56:09 -070030
31- (NSArray<NSString *> *)availableVideoResoultionsMediaConstraints {
32 return videoResolutionsStaticValues();
33}
34
35- (NSString *)currentVideoResoultionConstraintFromStore {
denicija9af2b602016-11-17 00:43:43 -080036 NSString *constraint = [[self settingsStore] videoResolutionConstraints];
denicijad17d5362016-11-02 02:56:09 -070037 if (!constraint) {
38 constraint = [self defaultVideoResolutionMediaConstraint];
39 // To ensure consistency add the default to the store.
denicija9af2b602016-11-17 00:43:43 -080040 [[self settingsStore] setVideoResolutionConstraints:constraint];
denicijad17d5362016-11-02 02:56:09 -070041 }
42 return constraint;
43}
44
45- (BOOL)storeVideoResoultionConstraint:(NSString *)constraint {
46 if (![[self availableVideoResoultionsMediaConstraints] containsObject:constraint]) {
47 return NO;
48 }
denicija9af2b602016-11-17 00:43:43 -080049 [[self settingsStore] setVideoResolutionConstraints:constraint];
denicijad17d5362016-11-02 02:56:09 -070050 return YES;
51}
52
sakal68b5df92017-03-17 09:01:59 -070053- (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
denicija9af2b602016-11-17 00:43:43 -080074- (nullable NSNumber *)currentMaxBitrateSettingFromStore {
75 return [[self settingsStore] maxBitrate];
76}
77
78- (void)storeMaxBitrateSetting:(nullable NSNumber *)bitrate {
79 [[self settingsStore] setMaxBitrate:bitrate];
80}
81
denicijad17d5362016-11-02 02:56:09 -070082#pragma mark - Testable
83
denicija2256e042016-11-09 06:26:18 -080084- (ARDSettingsStore *)settingsStore {
denicijad17d5362016-11-02 02:56:09 -070085 if (!_settingsStore) {
denicija2256e042016-11-09 06:26:18 -080086 _settingsStore = [[ARDSettingsStore alloc] init];
denicijad17d5362016-11-02 02:56:09 -070087 }
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
sakal68b5df92017-03-17 09:01:59 -0700120- (NSString *)defaultVideoCodecSetting {
121 return videoCodecsStaticValues()[0];
122}
123
denicijad17d5362016-11-02 02:56:09 -0700124#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
141NS_ASSUME_NONNULL_END