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 | |
| 11 | #import "ARDSettingsViewController.h" |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 12 | #import "ARDSettingsModel.h" |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 13 | |
| 14 | NS_ASSUME_NONNULL_BEGIN |
denicija | b04b5c2 | 2016-11-09 04:28:46 -0800 | [diff] [blame] | 15 | |
| 16 | typedef NS_ENUM(int, ARDSettingsSections) { |
| 17 | ARDSettingsSectionMediaConstraints = 0, |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 18 | ARDSettingsSectionVideoCodec, |
| 19 | ARDSettingsSectionBitRate, |
denicija | b04b5c2 | 2016-11-09 04:28:46 -0800 | [diff] [blame] | 20 | }; |
| 21 | |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 22 | @interface ARDSettingsViewController () <UITextFieldDelegate> { |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 23 | ARDSettingsModel *_settingsModel; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | @end |
| 27 | |
| 28 | @implementation ARDSettingsViewController |
| 29 | |
| 30 | - (instancetype)initWithStyle:(UITableViewStyle)style |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 31 | settingsModel:(ARDSettingsModel *)settingsModel { |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 32 | self = [super initWithStyle:style]; |
| 33 | if (self) { |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 34 | _settingsModel = settingsModel; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 35 | } |
| 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 | |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 47 | - (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 | |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 57 | - (void)viewDidAppear:(BOOL)animated { |
| 58 | [super viewDidAppear:animated]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | #pragma mark - Data source |
| 62 | |
| 63 | - (NSArray<NSString *> *)mediaConstraintsArray { |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 64 | return _settingsModel.availableVideoResoultionsMediaConstraints; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 65 | } |
| 66 | |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 67 | - (NSArray<NSString *> *)videoCodecArray { |
| 68 | return _settingsModel.availableVideoCodecs; |
| 69 | } |
| 70 | |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 71 | #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 | |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 81 | - (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; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 89 | } |
| 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 { |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 100 | return 3; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 101 | } |
| 102 | |
denicija | 40532a1 | 2016-11-08 04:00:53 -0800 | [diff] [blame] | 103 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 104 | switch (section) { |
| 105 | case ARDSettingsSectionMediaConstraints: |
| 106 | return self.mediaConstraintsArray.count; |
| 107 | case ARDSettingsSectionVideoCodec: |
| 108 | return self.videoCodecArray.count; |
| 109 | default: |
| 110 | return 1; |
denicija | b04b5c2 | 2016-11-09 04:28:46 -0800 | [diff] [blame] | 111 | } |
denicija | 3babb99 | 2016-11-07 07:23:56 -0800 | [diff] [blame] | 112 | } |
| 113 | |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 114 | #pragma mark - Table view delegate helpers |
denicija | 40532a1 | 2016-11-08 04:00:53 -0800 | [diff] [blame] | 115 | |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 116 | - (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 | } |
denicija | b04b5c2 | 2016-11-09 04:28:46 -0800 | [diff] [blame] | 124 | } |
| 125 | |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 126 | - (void)tableView:(UITableView *)tableView |
| 127 | updateListSelectionAtIndexPath:(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]; |
denicija | b04b5c2 | 2016-11-09 04:28:46 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | #pragma mark - Table view delegate |
| 136 | |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 137 | - (nullable NSString *)tableView:(UITableView *)tableView |
| 138 | titleForHeaderInSection:(NSInteger)section { |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 139 | 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 @""; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 148 | } |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | - (UITableViewCell *)tableView:(UITableView *)tableView |
| 152 | cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 153 | switch (indexPath.section) { |
| 154 | case ARDSettingsSectionMediaConstraints: |
| 155 | return [self mediaConstraintsTableViewCellForTableView:tableView atIndexPath:indexPath]; |
denicija | b04b5c2 | 2016-11-09 04:28:46 -0800 | [diff] [blame] | 156 | |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 157 | case ARDSettingsSectionVideoCodec: |
| 158 | return [self videoCodecTableViewCellForTableView:tableView atIndexPath:indexPath]; |
denicija | b04b5c2 | 2016-11-09 04:28:46 -0800 | [diff] [blame] | 159 | |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 160 | case ARDSettingsSectionBitRate: |
| 161 | return [self bitrateTableViewCellForTableView:tableView atIndexPath:indexPath]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 162 | |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 163 | default: |
| 164 | return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
| 165 | reuseIdentifier:@"identifier"]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 166 | } |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 170 | 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; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 178 | } |
| 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 { |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 197 | [self tableView:tableView |
| 198 | updateListSelectionAtIndexPath:indexPath |
| 199 | inSection:ARDSettingsSectionMediaConstraints]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 200 | |
| 201 | NSString *mediaConstraintsString = self.mediaConstraintsArray[indexPath.row]; |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 202 | [_settingsModel storeVideoResoultionConstraint:mediaConstraintsString]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 203 | } |
| 204 | |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame^] | 205 | #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]; |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 228 | } |
| 229 | |
denicija | b04b5c2 | 2016-11-09 04:28:46 -0800 | [diff] [blame] | 230 | #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)]; |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 242 | NSString *currentMaxBitrate = [_settingsModel currentMaxBitrateSettingFromStore].stringValue; |
| 243 | textField.text = currentMaxBitrate; |
denicija | b04b5c2 | 2016-11-09 04:28:46 -0800 | [diff] [blame] | 244 | textField.placeholder = @"Enter max bit rate (kbps)"; |
| 245 | textField.keyboardType = UIKeyboardTypeNumberPad; |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 246 | textField.delegate = self; |
denicija | b04b5c2 | 2016-11-09 04:28:46 -0800 | [diff] [blame] | 247 | |
| 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 | |
denicija | 9af2b60 | 2016-11-17 00:43:43 -0800 | [diff] [blame] | 272 | - (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 | |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 282 | @end |
| 283 | NS_ASSUME_NONNULL_END |