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.

262 lines
9.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2015 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 "ARDMainViewController.h"
  11. #import <AVFoundation/AVFoundation.h>
  12. #import <WebRTC/RTCAudioSession.h>
  13. #import <WebRTC/RTCAudioSessionConfiguration.h>
  14. #import <WebRTC/RTCDispatcher.h>
  15. #import <WebRTC/RTCLogging.h>
  16. #import "ARDAppClient.h"
  17. #import "ARDMainView.h"
  18. #import "ARDSettingsModel.h"
  19. #import "ARDSettingsViewController.h"
  20. #import "ARDVideoCallViewController.h"
  21. static NSString *const barButtonImageString = @"ic_settings_black_24dp.png";
  22. // Launch argument to be passed to indicate that the app should start loopback immediatly
  23. static NSString *const loopbackLaunchProcessArgument = @"loopback";
  24. @interface ARDMainViewController () <
  25. ARDMainViewDelegate,
  26. ARDVideoCallViewControllerDelegate,
  27. RTCAudioSessionDelegate>
  28. @property(nonatomic, strong) ARDMainView *mainView;
  29. @property(nonatomic, strong) AVAudioPlayer *audioPlayer;
  30. @end
  31. @implementation ARDMainViewController {
  32. BOOL _useManualAudio;
  33. }
  34. @synthesize mainView = _mainView;
  35. @synthesize audioPlayer = _audioPlayer;
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. if ([[[NSProcessInfo processInfo] arguments] containsObject:loopbackLaunchProcessArgument]) {
  39. [self mainView:nil didInputRoom:@"" isLoopback:YES];
  40. }
  41. }
  42. - (void)loadView {
  43. self.title = @"AppRTC Mobile";
  44. _mainView = [[ARDMainView alloc] initWithFrame:CGRectZero];
  45. _mainView.delegate = self;
  46. self.view = _mainView;
  47. [self addSettingsBarButton];
  48. RTCAudioSessionConfiguration *webRTCConfig =
  49. [RTCAudioSessionConfiguration webRTCConfiguration];
  50. webRTCConfig.categoryOptions = webRTCConfig.categoryOptions |
  51. AVAudioSessionCategoryOptionDefaultToSpeaker;
  52. [RTCAudioSessionConfiguration setWebRTCConfiguration:webRTCConfig];
  53. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  54. [session addDelegate:self];
  55. [self configureAudioSession];
  56. [self setupAudioPlayer];
  57. }
  58. - (void)addSettingsBarButton {
  59. UIBarButtonItem *settingsButton =
  60. [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:barButtonImageString]
  61. style:UIBarButtonItemStylePlain
  62. target:self
  63. action:@selector(showSettings:)];
  64. self.navigationItem.rightBarButtonItem = settingsButton;
  65. }
  66. + (NSString *)loopbackRoomString {
  67. NSString *loopbackRoomString =
  68. [[NSUUID UUID].UUIDString stringByReplacingOccurrencesOfString:@"-" withString:@""];
  69. return loopbackRoomString;
  70. }
  71. #pragma mark - ARDMainViewDelegate
  72. - (void)mainView:(ARDMainView *)mainView didInputRoom:(NSString *)room isLoopback:(BOOL)isLoopback {
  73. if (!room.length) {
  74. if (isLoopback) {
  75. // If this is a loopback call, allow a generated room name.
  76. room = [[self class] loopbackRoomString];
  77. } else {
  78. [self showAlertWithMessage:@"Missing room name."];
  79. return;
  80. }
  81. }
  82. // Trim whitespaces.
  83. NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
  84. NSString *trimmedRoom = [room stringByTrimmingCharactersInSet:whitespaceSet];
  85. // Check that room name is valid.
  86. NSError *error = nil;
  87. NSRegularExpressionOptions options = NSRegularExpressionCaseInsensitive;
  88. NSRegularExpression *regex =
  89. [NSRegularExpression regularExpressionWithPattern:@"\\w+"
  90. options:options
  91. error:&error];
  92. if (error) {
  93. [self showAlertWithMessage:error.localizedDescription];
  94. return;
  95. }
  96. NSRange matchRange =
  97. [regex rangeOfFirstMatchInString:trimmedRoom
  98. options:0
  99. range:NSMakeRange(0, trimmedRoom.length)];
  100. if (matchRange.location == NSNotFound ||
  101. matchRange.length != trimmedRoom.length) {
  102. [self showAlertWithMessage:@"Invalid room name."];
  103. return;
  104. }
  105. ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init];
  106. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  107. session.useManualAudio = [settingsModel currentUseManualAudioConfigSettingFromStore];
  108. session.isAudioEnabled = NO;
  109. // Kick off the video call.
  110. ARDVideoCallViewController *videoCallViewController =
  111. [[ARDVideoCallViewController alloc] initForRoom:trimmedRoom
  112. isLoopback:isLoopback
  113. delegate:self];
  114. videoCallViewController.modalTransitionStyle =
  115. UIModalTransitionStyleCrossDissolve;
  116. [self presentViewController:videoCallViewController
  117. animated:YES
  118. completion:nil];
  119. }
  120. - (void)mainViewDidToggleAudioLoop:(ARDMainView *)mainView {
  121. if (mainView.isAudioLoopPlaying) {
  122. [_audioPlayer stop];
  123. } else {
  124. [_audioPlayer play];
  125. }
  126. mainView.isAudioLoopPlaying = _audioPlayer.playing;
  127. }
  128. #pragma mark - ARDVideoCallViewControllerDelegate
  129. - (void)viewControllerDidFinish:(ARDVideoCallViewController *)viewController {
  130. if (![viewController isBeingDismissed]) {
  131. RTCLog(@"Dismissing VC");
  132. [self dismissViewControllerAnimated:YES completion:^{
  133. [self restartAudioPlayerIfNeeded];
  134. }];
  135. }
  136. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  137. session.isAudioEnabled = NO;
  138. }
  139. #pragma mark - RTCAudioSessionDelegate
  140. - (void)audioSessionDidStartPlayOrRecord:(RTCAudioSession *)session {
  141. // Stop playback on main queue and then configure WebRTC.
  142. [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeMain
  143. block:^{
  144. if (self.mainView.isAudioLoopPlaying) {
  145. RTCLog(@"Stopping audio loop due to WebRTC start.");
  146. [self.audioPlayer stop];
  147. }
  148. RTCLog(@"Setting isAudioEnabled to YES.");
  149. session.isAudioEnabled = YES;
  150. }];
  151. }
  152. - (void)audioSessionDidStopPlayOrRecord:(RTCAudioSession *)session {
  153. // WebRTC is done with the audio session. Restart playback.
  154. [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeMain
  155. block:^{
  156. RTCLog(@"audioSessionDidStopPlayOrRecord");
  157. [self restartAudioPlayerIfNeeded];
  158. }];
  159. }
  160. #pragma mark - Private
  161. - (void)showSettings:(id)sender {
  162. ARDSettingsViewController *settingsController =
  163. [[ARDSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped
  164. settingsModel:[[ARDSettingsModel alloc] init]];
  165. UINavigationController *navigationController =
  166. [[UINavigationController alloc] initWithRootViewController:settingsController];
  167. [self presentViewControllerAsModal:navigationController];
  168. }
  169. - (void)presentViewControllerAsModal:(UIViewController *)viewController {
  170. [self presentViewController:viewController animated:YES completion:nil];
  171. }
  172. - (void)configureAudioSession {
  173. RTCAudioSessionConfiguration *configuration =
  174. [[RTCAudioSessionConfiguration alloc] init];
  175. configuration.category = AVAudioSessionCategoryAmbient;
  176. configuration.categoryOptions = AVAudioSessionCategoryOptionDuckOthers;
  177. configuration.mode = AVAudioSessionModeDefault;
  178. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  179. [session lockForConfiguration];
  180. BOOL hasSucceeded = NO;
  181. NSError *error = nil;
  182. if (session.isActive) {
  183. hasSucceeded = [session setConfiguration:configuration error:&error];
  184. } else {
  185. hasSucceeded = [session setConfiguration:configuration
  186. active:YES
  187. error:&error];
  188. }
  189. if (!hasSucceeded) {
  190. RTCLogError(@"Error setting configuration: %@", error.localizedDescription);
  191. }
  192. [session unlockForConfiguration];
  193. }
  194. - (void)setupAudioPlayer {
  195. NSString *audioFilePath =
  196. [[NSBundle mainBundle] pathForResource:@"mozart" ofType:@"mp3"];
  197. NSURL *audioFileURL = [NSURL URLWithString:audioFilePath];
  198. _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL
  199. error:nil];
  200. _audioPlayer.numberOfLoops = -1;
  201. _audioPlayer.volume = 1.0;
  202. [_audioPlayer prepareToPlay];
  203. }
  204. - (void)restartAudioPlayerIfNeeded {
  205. [self configureAudioSession];
  206. if (_mainView.isAudioLoopPlaying && !self.presentedViewController) {
  207. RTCLog(@"Starting audio loop due to WebRTC end.");
  208. [_audioPlayer play];
  209. }
  210. }
  211. - (void)showAlertWithMessage:(NSString*)message {
  212. UIAlertController *alert =
  213. [UIAlertController alertControllerWithTitle:nil
  214. message:message
  215. preferredStyle:UIAlertControllerStyleAlert];
  216. UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK"
  217. style:UIAlertActionStyleDefault
  218. handler:^(UIAlertAction *action){
  219. }];
  220. [alert addAction:defaultAction];
  221. [self presentViewController:alert animated:YES completion:nil];
  222. }
  223. @end