You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

361 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #import "ARDSettingsViewController.h"
  11. #import "ARDSettingsModel.h"
  12. #import "RTCVideoCodecInfo+HumanReadable.h"
  13. NS_ASSUME_NONNULL_BEGIN
  14. typedef NS_ENUM(int, ARDSettingsSections) {
  15. ARDSettingsSectionAudioSettings = 0,
  16. ARDSettingsSectionVideoResolution,
  17. ARDSettingsSectionVideoCodec,
  18. ARDSettingsSectionBitRate,
  19. };
  20. typedef NS_ENUM(int, ARDAudioSettingsOptions) {
  21. ARDAudioSettingsAudioOnly = 0,
  22. ARDAudioSettingsCreateAecDump,
  23. ARDAudioSettingsUseManualAudioConfig,
  24. };
  25. @interface ARDSettingsViewController () <UITextFieldDelegate> {
  26. ARDSettingsModel *_settingsModel;
  27. }
  28. @end
  29. @implementation ARDSettingsViewController
  30. - (instancetype)initWithStyle:(UITableViewStyle)style
  31. settingsModel:(ARDSettingsModel *)settingsModel {
  32. self = [super initWithStyle:style];
  33. if (self) {
  34. _settingsModel = settingsModel;
  35. }
  36. return self;
  37. }
  38. #pragma mark - View lifecycle
  39. - (void)viewDidLoad {
  40. [super viewDidLoad];
  41. self.title = @"Settings";
  42. [self addDoneBarButton];
  43. }
  44. - (void)viewWillAppear:(BOOL)animated {
  45. [super viewWillAppear:animated];
  46. }
  47. #pragma mark - Data source
  48. - (NSArray<NSString *> *)videoResolutionArray {
  49. return [_settingsModel availableVideoResolutions];
  50. }
  51. - (NSArray<RTCVideoCodecInfo *> *)videoCodecArray {
  52. return [_settingsModel availableVideoCodecs];
  53. }
  54. #pragma mark -
  55. - (void)addDoneBarButton {
  56. UIBarButtonItem *barItem =
  57. [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  58. target:self
  59. action:@selector(dismissModally:)];
  60. self.navigationItem.leftBarButtonItem = barItem;
  61. }
  62. #pragma mark - Dismissal of view controller
  63. - (void)dismissModally:(id)sender {
  64. [self dismissViewControllerAnimated:YES completion:nil];
  65. }
  66. #pragma mark - Table view data source
  67. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  68. return 4;
  69. }
  70. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  71. switch (section) {
  72. case ARDSettingsSectionAudioSettings:
  73. return 3;
  74. case ARDSettingsSectionVideoResolution:
  75. return self.videoResolutionArray.count;
  76. case ARDSettingsSectionVideoCodec:
  77. return self.videoCodecArray.count;
  78. default:
  79. return 1;
  80. }
  81. }
  82. #pragma mark - Table view delegate helpers
  83. - (void)removeAllAccessories:(UITableView *)tableView
  84. inSection:(int)section
  85. {
  86. for (int i = 0; i < [tableView numberOfRowsInSection:section]; i++) {
  87. NSIndexPath *rowPath = [NSIndexPath indexPathForRow:i inSection:section];
  88. UITableViewCell *cell = [tableView cellForRowAtIndexPath:rowPath];
  89. cell.accessoryType = UITableViewCellAccessoryNone;
  90. }
  91. }
  92. - (void)tableView:(UITableView *)tableView
  93. updateListSelectionAtIndexPath:(NSIndexPath *)indexPath
  94. inSection:(int)section {
  95. [self removeAllAccessories:tableView inSection:section];
  96. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  97. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  98. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  99. }
  100. #pragma mark - Table view delegate
  101. - (nullable NSString *)tableView:(UITableView *)tableView
  102. titleForHeaderInSection:(NSInteger)section {
  103. switch (section) {
  104. case ARDSettingsSectionAudioSettings:
  105. return @"Audio";
  106. case ARDSettingsSectionVideoResolution:
  107. return @"Video resolution";
  108. case ARDSettingsSectionVideoCodec:
  109. return @"Video codec";
  110. case ARDSettingsSectionBitRate:
  111. return @"Maximum bitrate";
  112. default:
  113. return @"";
  114. }
  115. }
  116. - (UITableViewCell *)tableView:(UITableView *)tableView
  117. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  118. switch (indexPath.section) {
  119. case ARDSettingsSectionAudioSettings:
  120. return [self audioSettingsTableViewCellForTableView:tableView atIndexPath:indexPath];
  121. case ARDSettingsSectionVideoResolution:
  122. return [self videoResolutionTableViewCellForTableView:tableView atIndexPath:indexPath];
  123. case ARDSettingsSectionVideoCodec:
  124. return [self videoCodecTableViewCellForTableView:tableView atIndexPath:indexPath];
  125. case ARDSettingsSectionBitRate:
  126. return [self bitrateTableViewCellForTableView:tableView atIndexPath:indexPath];
  127. default:
  128. return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  129. reuseIdentifier:@"identifier"];
  130. }
  131. }
  132. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  133. switch (indexPath.section) {
  134. case ARDSettingsSectionVideoResolution:
  135. [self tableView:tableView disSelectVideoResolutionAtIndex:indexPath];
  136. break;
  137. case ARDSettingsSectionVideoCodec:
  138. [self tableView:tableView didSelectVideoCodecCellAtIndexPath:indexPath];
  139. break;
  140. }
  141. }
  142. #pragma mark - Table view delegate(Video Resolution)
  143. - (UITableViewCell *)videoResolutionTableViewCellForTableView:(UITableView *)tableView
  144. atIndexPath:(NSIndexPath *)indexPath {
  145. NSString *dequeueIdentifier = @"ARDSettingsVideoResolutionViewCellIdentifier";
  146. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dequeueIdentifier];
  147. if (!cell) {
  148. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  149. reuseIdentifier:dequeueIdentifier];
  150. }
  151. NSString *resolution = self.videoResolutionArray[indexPath.row];
  152. cell.textLabel.text = resolution;
  153. if ([resolution isEqualToString:[_settingsModel currentVideoResolutionSettingFromStore]]) {
  154. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  155. } else {
  156. cell.accessoryType = UITableViewCellAccessoryNone;
  157. }
  158. return cell;
  159. }
  160. - (void)tableView:(UITableView *)tableView
  161. disSelectVideoResolutionAtIndex:(NSIndexPath *)indexPath {
  162. [self tableView:tableView
  163. updateListSelectionAtIndexPath:indexPath
  164. inSection:ARDSettingsSectionVideoResolution];
  165. NSString *videoResolution = self.videoResolutionArray[indexPath.row];
  166. [_settingsModel storeVideoResolutionSetting:videoResolution];
  167. }
  168. #pragma mark - Table view delegate(Video Codec)
  169. - (UITableViewCell *)videoCodecTableViewCellForTableView:(UITableView *)tableView
  170. atIndexPath:(NSIndexPath *)indexPath {
  171. NSString *dequeueIdentifier = @"ARDSettingsVideoCodecCellIdentifier";
  172. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dequeueIdentifier];
  173. if (!cell) {
  174. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  175. reuseIdentifier:dequeueIdentifier];
  176. }
  177. RTCVideoCodecInfo *codec = self.videoCodecArray[indexPath.row];
  178. cell.textLabel.text = [codec humanReadableDescription];
  179. if ([codec isEqualToCodecInfo:[_settingsModel currentVideoCodecSettingFromStore]]) {
  180. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  181. } else {
  182. cell.accessoryType = UITableViewCellAccessoryNone;
  183. }
  184. return cell;
  185. }
  186. - (void)tableView:(UITableView *)tableView
  187. didSelectVideoCodecCellAtIndexPath:(NSIndexPath *)indexPath {
  188. [self tableView:tableView
  189. updateListSelectionAtIndexPath:indexPath
  190. inSection:ARDSettingsSectionVideoCodec];
  191. RTCVideoCodecInfo *videoCodec = self.videoCodecArray[indexPath.row];
  192. [_settingsModel storeVideoCodecSetting:videoCodec];
  193. }
  194. #pragma mark - Table view delegate(Bitrate)
  195. - (UITableViewCell *)bitrateTableViewCellForTableView:(UITableView *)tableView
  196. atIndexPath:(NSIndexPath *)indexPath {
  197. NSString *dequeueIdentifier = @"ARDSettingsBitrateCellIdentifier";
  198. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dequeueIdentifier];
  199. if (!cell) {
  200. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  201. reuseIdentifier:dequeueIdentifier];
  202. UITextField *textField = [[UITextField alloc]
  203. initWithFrame:CGRectMake(10, 0, cell.bounds.size.width - 20, cell.bounds.size.height)];
  204. NSString *currentMaxBitrate = [_settingsModel currentMaxBitrateSettingFromStore].stringValue;
  205. textField.text = currentMaxBitrate;
  206. textField.placeholder = @"Enter max bit rate (kbps)";
  207. textField.keyboardType = UIKeyboardTypeNumberPad;
  208. textField.delegate = self;
  209. // Numerical keyboards have no return button, we need to add one manually.
  210. UIToolbar *numberToolbar =
  211. [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
  212. numberToolbar.items = @[
  213. [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
  214. target:nil
  215. action:nil],
  216. [[UIBarButtonItem alloc] initWithTitle:@"Apply"
  217. style:UIBarButtonItemStyleDone
  218. target:self
  219. action:@selector(numberTextFieldDidEndEditing:)]
  220. ];
  221. [numberToolbar sizeToFit];
  222. textField.inputAccessoryView = numberToolbar;
  223. [cell addSubview:textField];
  224. }
  225. return cell;
  226. }
  227. - (void)numberTextFieldDidEndEditing:(id)sender {
  228. [self.view endEditing:YES];
  229. }
  230. - (void)textFieldDidEndEditing:(UITextField *)textField {
  231. NSNumber *bitrateNumber = nil;
  232. if (textField.text.length != 0) {
  233. bitrateNumber = [NSNumber numberWithInteger:textField.text.intValue];
  234. }
  235. [_settingsModel storeMaxBitrateSetting:bitrateNumber];
  236. }
  237. #pragma mark - Table view delegate(Audio settings)
  238. - (UITableViewCell *)audioSettingsTableViewCellForTableView:(UITableView *)tableView
  239. atIndexPath:(NSIndexPath *)indexPath {
  240. NSString *dequeueIdentifier = @"ARDSettingsAudioSettingsCellIdentifier";
  241. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dequeueIdentifier];
  242. if (!cell) {
  243. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  244. reuseIdentifier:dequeueIdentifier];
  245. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  246. UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
  247. switchView.tag = indexPath.row;
  248. [switchView addTarget:self
  249. action:@selector(audioSettingSwitchChanged:)
  250. forControlEvents:UIControlEventValueChanged];
  251. cell.accessoryView = switchView;
  252. }
  253. cell.textLabel.text = [self labelForAudioSettingAtIndexPathRow:indexPath.row];
  254. UISwitch *switchView = (UISwitch *)cell.accessoryView;
  255. switchView.on = [self valueForAudioSettingAtIndexPathRow:indexPath.row];
  256. return cell;
  257. }
  258. - (NSString *)labelForAudioSettingAtIndexPathRow:(NSInteger)setting {
  259. switch (setting) {
  260. case ARDAudioSettingsAudioOnly:
  261. return @"Audio only";
  262. case ARDAudioSettingsCreateAecDump:
  263. return @"Create AecDump";
  264. case ARDAudioSettingsUseManualAudioConfig:
  265. return @"Use manual audio config";
  266. default:
  267. return @"";
  268. }
  269. }
  270. - (BOOL)valueForAudioSettingAtIndexPathRow:(NSInteger)setting {
  271. switch (setting) {
  272. case ARDAudioSettingsAudioOnly:
  273. return [_settingsModel currentAudioOnlySettingFromStore];
  274. case ARDAudioSettingsCreateAecDump:
  275. return [_settingsModel currentCreateAecDumpSettingFromStore];
  276. case ARDAudioSettingsUseManualAudioConfig:
  277. return [_settingsModel currentUseManualAudioConfigSettingFromStore];
  278. default:
  279. return NO;
  280. }
  281. }
  282. - (void)audioSettingSwitchChanged:(UISwitch *)sender {
  283. switch (sender.tag) {
  284. case ARDAudioSettingsAudioOnly: {
  285. [_settingsModel storeAudioOnlySetting:sender.isOn];
  286. break;
  287. }
  288. case ARDAudioSettingsCreateAecDump: {
  289. [_settingsModel storeCreateAecDumpSetting:sender.isOn];
  290. break;
  291. }
  292. case ARDAudioSettingsUseManualAudioConfig: {
  293. [_settingsModel storeUseManualAudioConfigSetting:sender.isOn];
  294. break;
  295. }
  296. default:
  297. break;
  298. }
  299. }
  300. @end
  301. NS_ASSUME_NONNULL_END