blob: fb59dd2769b1a9c5eacce912f2e59f24e5fb40eb [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
11#import "ARDSettingsViewController.h"
denicija2256e042016-11-09 06:26:18 -080012#import "ARDSettingsModel.h"
denicijad17d5362016-11-02 02:56:09 -070013
14NS_ASSUME_NONNULL_BEGIN
denicijab04b5c22016-11-09 04:28:46 -080015
16typedef NS_ENUM(int, ARDSettingsSections) {
17 ARDSettingsSectionMediaConstraints = 0,
sakal68b5df92017-03-17 09:01:59 -070018 ARDSettingsSectionVideoCodec,
19 ARDSettingsSectionBitRate,
denicijab04b5c22016-11-09 04:28:46 -080020};
21
denicija9af2b602016-11-17 00:43:43 -080022@interface ARDSettingsViewController () <UITextFieldDelegate> {
denicija2256e042016-11-09 06:26:18 -080023 ARDSettingsModel *_settingsModel;
denicijad17d5362016-11-02 02:56:09 -070024}
25
26@end
27
28@implementation ARDSettingsViewController
29
30- (instancetype)initWithStyle:(UITableViewStyle)style
denicija2256e042016-11-09 06:26:18 -080031 settingsModel:(ARDSettingsModel *)settingsModel {
denicijad17d5362016-11-02 02:56:09 -070032 self = [super initWithStyle:style];
33 if (self) {
denicija2256e042016-11-09 06:26:18 -080034 _settingsModel = settingsModel;
denicijad17d5362016-11-02 02:56:09 -070035 }
36 return self;
37}
38
39#pragma mark - View lifecycle
40
41- (void)viewDidLoad {
42 [super viewDidLoad];
43 self.title = @"Settings";
44 [self addDoneBarButton];
45}
46
sakal68b5df92017-03-17 09:01:59 -070047- (void)viewWillAppear:(BOOL)animated {
48 [super viewWillAppear:animated];
49 [self addCheckmarkInSection:ARDSettingsSectionMediaConstraints
50 withArray:[self mediaConstraintsArray]
51 selecting:[_settingsModel currentVideoResoultionConstraintFromStore]];
52 [self addCheckmarkInSection:ARDSettingsSectionVideoCodec
53 withArray:[self videoCodecArray]
54 selecting:[_settingsModel currentVideoCodecSettingFromStore]];
55}
56
denicijad17d5362016-11-02 02:56:09 -070057- (void)viewDidAppear:(BOOL)animated {
58 [super viewDidAppear:animated];
denicijad17d5362016-11-02 02:56:09 -070059}
60
61#pragma mark - Data source
62
63- (NSArray<NSString *> *)mediaConstraintsArray {
denicija2256e042016-11-09 06:26:18 -080064 return _settingsModel.availableVideoResoultionsMediaConstraints;
denicijad17d5362016-11-02 02:56:09 -070065}
66
sakal68b5df92017-03-17 09:01:59 -070067- (NSArray<NSString *> *)videoCodecArray {
68 return _settingsModel.availableVideoCodecs;
69}
70
denicijad17d5362016-11-02 02:56:09 -070071#pragma mark -
72
73- (void)addDoneBarButton {
74 UIBarButtonItem *barItem =
75 [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
76 target:self
77 action:@selector(dismissModally:)];
78 self.navigationItem.leftBarButtonItem = barItem;
79}
80
sakal68b5df92017-03-17 09:01:59 -070081- (void)addCheckmarkInSection:(int)section
82 withArray:(NSArray<NSString*>*) array
83 selecting:(NSString*)selection {
84 NSUInteger indexOfSelection = [array indexOfObject:selection];
85 NSIndexPath *pathToBeDecorated = [NSIndexPath indexPathForRow:indexOfSelection
86 inSection:section];
87 UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:pathToBeDecorated];
88 cell.accessoryType = UITableViewCellAccessoryCheckmark;
denicijad17d5362016-11-02 02:56:09 -070089}
90
91#pragma mark - Dismissal of view controller
92
93- (void)dismissModally:(id)sender {
94 [self dismissViewControllerAnimated:YES completion:nil];
95}
96
97#pragma mark - Table view data source
98
99- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
sakal68b5df92017-03-17 09:01:59 -0700100 return 3;
denicijad17d5362016-11-02 02:56:09 -0700101}
102
denicija40532a12016-11-08 04:00:53 -0800103- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
sakal68b5df92017-03-17 09:01:59 -0700104 switch (section) {
105 case ARDSettingsSectionMediaConstraints:
106 return self.mediaConstraintsArray.count;
107 case ARDSettingsSectionVideoCodec:
108 return self.videoCodecArray.count;
109 default:
110 return 1;
denicijab04b5c22016-11-09 04:28:46 -0800111 }
denicija3babb992016-11-07 07:23:56 -0800112}
113
sakal68b5df92017-03-17 09:01:59 -0700114#pragma mark - Table view delegate helpers
denicija40532a12016-11-08 04:00:53 -0800115
sakal68b5df92017-03-17 09:01:59 -0700116- (void)removeAllAccessories:(UITableView *)tableView
117 inSection:(int)section
118{
119 for (int i = 0; i < [tableView numberOfRowsInSection:section]; i++) {
120 NSIndexPath *rowPath = [NSIndexPath indexPathForRow:i inSection:section];
121 UITableViewCell *cell = [tableView cellForRowAtIndexPath:rowPath];
122 cell.accessoryType = UITableViewCellAccessoryNone;
123 }
denicijab04b5c22016-11-09 04:28:46 -0800124}
125
sakal68b5df92017-03-17 09:01:59 -0700126- (void)tableView:(UITableView *)tableView
127updateListSelectionAtIndexPath:(NSIndexPath *)indexPath
128 inSection:(int)section {
129 [self removeAllAccessories:tableView inSection:section];
130 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
131 cell.accessoryType = UITableViewCellAccessoryCheckmark;
132 [tableView deselectRowAtIndexPath:indexPath animated:YES];
denicijab04b5c22016-11-09 04:28:46 -0800133}
134
135#pragma mark - Table view delegate
136
denicijad17d5362016-11-02 02:56:09 -0700137- (nullable NSString *)tableView:(UITableView *)tableView
138 titleForHeaderInSection:(NSInteger)section {
sakal68b5df92017-03-17 09:01:59 -0700139 switch (section) {
140 case ARDSettingsSectionMediaConstraints:
141 return @"Media constraints";
142 case ARDSettingsSectionVideoCodec:
143 return @"Video codec";
144 case ARDSettingsSectionBitRate:
145 return @"Maximum bitrate";
146 default:
147 return @"";
denicijad17d5362016-11-02 02:56:09 -0700148 }
denicijad17d5362016-11-02 02:56:09 -0700149}
150
151- (UITableViewCell *)tableView:(UITableView *)tableView
152 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
sakal68b5df92017-03-17 09:01:59 -0700153 switch (indexPath.section) {
154 case ARDSettingsSectionMediaConstraints:
155 return [self mediaConstraintsTableViewCellForTableView:tableView atIndexPath:indexPath];
denicijab04b5c22016-11-09 04:28:46 -0800156
sakal68b5df92017-03-17 09:01:59 -0700157 case ARDSettingsSectionVideoCodec:
158 return [self videoCodecTableViewCellForTableView:tableView atIndexPath:indexPath];
denicijab04b5c22016-11-09 04:28:46 -0800159
sakal68b5df92017-03-17 09:01:59 -0700160 case ARDSettingsSectionBitRate:
161 return [self bitrateTableViewCellForTableView:tableView atIndexPath:indexPath];
denicijad17d5362016-11-02 02:56:09 -0700162
sakal68b5df92017-03-17 09:01:59 -0700163 default:
164 return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
165 reuseIdentifier:@"identifier"];
denicijad17d5362016-11-02 02:56:09 -0700166 }
denicijad17d5362016-11-02 02:56:09 -0700167}
168
169- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
sakal68b5df92017-03-17 09:01:59 -0700170 switch (indexPath.section) {
171 case ARDSettingsSectionMediaConstraints:
172 [self tableView:tableView didSelectMediaConstraintsCellAtIndexPath:indexPath];
173 break;
174
175 case ARDSettingsSectionVideoCodec:
176 [self tableView:tableView didSelectVideoCodecCellAtIndexPath:indexPath];
177 break;
denicijad17d5362016-11-02 02:56:09 -0700178 }
179}
180
181#pragma mark - Table view delegate(Media Constraints)
182
183- (UITableViewCell *)mediaConstraintsTableViewCellForTableView:(UITableView *)tableView
184 atIndexPath:(NSIndexPath *)indexPath {
185 NSString *dequeueIdentifier = @"ARDSettingsMediaConstraintsViewCellIdentifier";
186 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dequeueIdentifier];
187 if (!cell) {
188 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
189 reuseIdentifier:dequeueIdentifier];
190 }
191 cell.textLabel.text = self.mediaConstraintsArray[indexPath.row];
192 return cell;
193}
194
195- (void)tableView:(UITableView *)tableView
196 didSelectMediaConstraintsCellAtIndexPath:(NSIndexPath *)indexPath {
sakal68b5df92017-03-17 09:01:59 -0700197 [self tableView:tableView
198 updateListSelectionAtIndexPath:indexPath
199 inSection:ARDSettingsSectionMediaConstraints];
denicijad17d5362016-11-02 02:56:09 -0700200
201 NSString *mediaConstraintsString = self.mediaConstraintsArray[indexPath.row];
denicija2256e042016-11-09 06:26:18 -0800202 [_settingsModel storeVideoResoultionConstraint:mediaConstraintsString];
denicijad17d5362016-11-02 02:56:09 -0700203}
204
sakal68b5df92017-03-17 09:01:59 -0700205#pragma mark - Table view delegate(Video Codec)
206
207- (UITableViewCell *)videoCodecTableViewCellForTableView:(UITableView *)tableView
208 atIndexPath:(NSIndexPath *)indexPath {
209 NSString *dequeueIdentifier = @"ARDSettingsVideoCodecCellIdentifier";
210 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dequeueIdentifier];
211 if (!cell) {
212 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
213 reuseIdentifier:dequeueIdentifier];
214 }
215 cell.textLabel.text = self.videoCodecArray[indexPath.row];
216
217 return cell;
218}
219
220- (void)tableView:(UITableView *)tableView
221 didSelectVideoCodecCellAtIndexPath:(NSIndexPath *)indexPath {
222 [self tableView:tableView
223 updateListSelectionAtIndexPath:indexPath
224 inSection:ARDSettingsSectionVideoCodec];
225
226 NSString *videoCodec = self.videoCodecArray[indexPath.row];
227 [_settingsModel storeVideoCodecSetting:videoCodec];
denicijad17d5362016-11-02 02:56:09 -0700228}
229
denicijab04b5c22016-11-09 04:28:46 -0800230#pragma mark - Table view delegate(Bitrate)
231
232- (UITableViewCell *)bitrateTableViewCellForTableView:(UITableView *)tableView
233 atIndexPath:(NSIndexPath *)indexPath {
234 NSString *dequeueIdentifier = @"ARDSettingsBitrateCellIdentifier";
235 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dequeueIdentifier];
236 if (!cell) {
237 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
238 reuseIdentifier:dequeueIdentifier];
239
240 UITextField *textField = [[UITextField alloc]
241 initWithFrame:CGRectMake(10, 0, cell.bounds.size.width - 20, cell.bounds.size.height)];
denicija9af2b602016-11-17 00:43:43 -0800242 NSString *currentMaxBitrate = [_settingsModel currentMaxBitrateSettingFromStore].stringValue;
243 textField.text = currentMaxBitrate;
denicijab04b5c22016-11-09 04:28:46 -0800244 textField.placeholder = @"Enter max bit rate (kbps)";
245 textField.keyboardType = UIKeyboardTypeNumberPad;
denicija9af2b602016-11-17 00:43:43 -0800246 textField.delegate = self;
denicijab04b5c22016-11-09 04:28:46 -0800247
248 // Numerical keyboards have no return button, we need to add one manually.
249 UIToolbar *numberToolbar =
250 [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
251 numberToolbar.items = @[
252 [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
253 target:nil
254 action:nil],
255 [[UIBarButtonItem alloc] initWithTitle:@"Apply"
256 style:UIBarButtonItemStyleDone
257 target:self
258 action:@selector(numberTextFieldDidEndEditing:)]
259 ];
260 [numberToolbar sizeToFit];
261
262 textField.inputAccessoryView = numberToolbar;
263 [cell addSubview:textField];
264 }
265 return cell;
266}
267
268- (void)numberTextFieldDidEndEditing:(id)sender {
269 [self.view endEditing:YES];
270}
271
denicija9af2b602016-11-17 00:43:43 -0800272- (void)textFieldDidEndEditing:(UITextField *)textField {
273 NSNumber *bitrateNumber = nil;
274
275 if (textField.text.length != 0) {
276 bitrateNumber = [NSNumber numberWithInteger:textField.text.intValue];
277 }
278
279 [_settingsModel storeMaxBitrateSetting:bitrateNumber];
280}
281
denicijad17d5362016-11-02 02:56:09 -0700282@end
283NS_ASSUME_NONNULL_END