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.

247 lines
8.1 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 "ARDVideoCallViewController.h"
  11. #import <WebRTC/RTCAudioSession.h>
  12. #import <WebRTC/RTCCameraVideoCapturer.h>
  13. #import <WebRTC/RTCDispatcher.h>
  14. #import <WebRTC/RTCLogging.h>
  15. #import <WebRTC/RTCMediaConstraints.h>
  16. #import "ARDAppClient.h"
  17. #import "ARDCaptureController.h"
  18. #import "ARDFileCaptureController.h"
  19. #import "ARDSettingsModel.h"
  20. #import "ARDVideoCallView.h"
  21. @interface ARDVideoCallViewController () <ARDAppClientDelegate,
  22. ARDVideoCallViewDelegate,
  23. RTCAudioSessionDelegate>
  24. @property(nonatomic, strong) RTCVideoTrack *remoteVideoTrack;
  25. @property(nonatomic, readonly) ARDVideoCallView *videoCallView;
  26. @property(nonatomic, assign) AVAudioSessionPortOverride portOverride;
  27. @end
  28. @implementation ARDVideoCallViewController {
  29. ARDAppClient *_client;
  30. RTCVideoTrack *_remoteVideoTrack;
  31. ARDCaptureController *_captureController;
  32. ARDFileCaptureController *_fileCaptureController NS_AVAILABLE_IOS(10);
  33. }
  34. @synthesize videoCallView = _videoCallView;
  35. @synthesize remoteVideoTrack = _remoteVideoTrack;
  36. @synthesize delegate = _delegate;
  37. @synthesize portOverride = _portOverride;
  38. - (instancetype)initForRoom:(NSString *)room
  39. isLoopback:(BOOL)isLoopback
  40. delegate:(id<ARDVideoCallViewControllerDelegate>)delegate {
  41. if (self = [super init]) {
  42. ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init];
  43. _delegate = delegate;
  44. _client = [[ARDAppClient alloc] initWithDelegate:self];
  45. [_client connectToRoomWithId:room settings:settingsModel isLoopback:isLoopback];
  46. }
  47. return self;
  48. }
  49. - (void)loadView {
  50. _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero];
  51. _videoCallView.delegate = self;
  52. _videoCallView.statusLabel.text =
  53. [self statusTextForState:RTCIceConnectionStateNew];
  54. self.view = _videoCallView;
  55. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  56. [session addDelegate:self];
  57. }
  58. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  59. return UIInterfaceOrientationMaskAll;
  60. }
  61. #pragma mark - ARDAppClientDelegate
  62. - (void)appClient:(ARDAppClient *)client
  63. didChangeState:(ARDAppClientState)state {
  64. switch (state) {
  65. case kARDAppClientStateConnected:
  66. RTCLog(@"Client connected.");
  67. break;
  68. case kARDAppClientStateConnecting:
  69. RTCLog(@"Client connecting.");
  70. break;
  71. case kARDAppClientStateDisconnected:
  72. RTCLog(@"Client disconnected.");
  73. [self hangup];
  74. break;
  75. }
  76. }
  77. - (void)appClient:(ARDAppClient *)client
  78. didChangeConnectionState:(RTCIceConnectionState)state {
  79. RTCLog(@"ICE state changed: %ld", (long)state);
  80. __weak ARDVideoCallViewController *weakSelf = self;
  81. dispatch_async(dispatch_get_main_queue(), ^{
  82. ARDVideoCallViewController *strongSelf = weakSelf;
  83. strongSelf.videoCallView.statusLabel.text =
  84. [strongSelf statusTextForState:state];
  85. });
  86. }
  87. - (void)appClient:(ARDAppClient *)client
  88. didCreateLocalCapturer:(RTCCameraVideoCapturer *)localCapturer {
  89. _videoCallView.localVideoView.captureSession = localCapturer.captureSession;
  90. ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init];
  91. _captureController =
  92. [[ARDCaptureController alloc] initWithCapturer:localCapturer settings:settingsModel];
  93. [_captureController startCapture];
  94. }
  95. - (void)appClient:(ARDAppClient *)client
  96. didCreateLocalFileCapturer:(RTCFileVideoCapturer *)fileCapturer {
  97. #if defined(__IPHONE_11_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0)
  98. if (@available(iOS 10, *)) {
  99. _fileCaptureController = [[ARDFileCaptureController alloc] initWithCapturer:fileCapturer];
  100. [_fileCaptureController startCapture];
  101. }
  102. #endif
  103. }
  104. - (void)appClient:(ARDAppClient *)client
  105. didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
  106. }
  107. - (void)appClient:(ARDAppClient *)client
  108. didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
  109. self.remoteVideoTrack = remoteVideoTrack;
  110. __weak ARDVideoCallViewController *weakSelf = self;
  111. dispatch_async(dispatch_get_main_queue(), ^{
  112. ARDVideoCallViewController *strongSelf = weakSelf;
  113. strongSelf.videoCallView.statusLabel.hidden = YES;
  114. });
  115. }
  116. - (void)appClient:(ARDAppClient *)client
  117. didGetStats:(NSArray *)stats {
  118. _videoCallView.statsView.stats = stats;
  119. [_videoCallView setNeedsLayout];
  120. }
  121. - (void)appClient:(ARDAppClient *)client
  122. didError:(NSError *)error {
  123. NSString *message =
  124. [NSString stringWithFormat:@"%@", error.localizedDescription];
  125. [self hangup];
  126. [self showAlertWithMessage:message];
  127. }
  128. #pragma mark - ARDVideoCallViewDelegate
  129. - (void)videoCallViewDidHangup:(ARDVideoCallView *)view {
  130. [self hangup];
  131. }
  132. - (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view {
  133. // TODO(tkchin): Rate limit this so you can't tap continously on it.
  134. // Probably through an animation.
  135. [_captureController switchCamera];
  136. }
  137. - (void)videoCallViewDidChangeRoute:(ARDVideoCallView *)view {
  138. AVAudioSessionPortOverride override = AVAudioSessionPortOverrideNone;
  139. if (_portOverride == AVAudioSessionPortOverrideNone) {
  140. override = AVAudioSessionPortOverrideSpeaker;
  141. }
  142. [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeAudioSession
  143. block:^{
  144. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  145. [session lockForConfiguration];
  146. NSError *error = nil;
  147. if ([session overrideOutputAudioPort:override error:&error]) {
  148. self.portOverride = override;
  149. } else {
  150. RTCLogError(@"Error overriding output port: %@",
  151. error.localizedDescription);
  152. }
  153. [session unlockForConfiguration];
  154. }];
  155. }
  156. - (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view {
  157. _client.shouldGetStats = YES;
  158. _videoCallView.statsView.hidden = NO;
  159. }
  160. #pragma mark - RTCAudioSessionDelegate
  161. - (void)audioSession:(RTCAudioSession *)audioSession
  162. didDetectPlayoutGlitch:(int64_t)totalNumberOfGlitches {
  163. RTCLog(@"Audio session detected glitch, total: %lld", totalNumberOfGlitches);
  164. }
  165. #pragma mark - Private
  166. - (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
  167. if (_remoteVideoTrack == remoteVideoTrack) {
  168. return;
  169. }
  170. [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView];
  171. _remoteVideoTrack = nil;
  172. [_videoCallView.remoteVideoView renderFrame:nil];
  173. _remoteVideoTrack = remoteVideoTrack;
  174. [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView];
  175. }
  176. - (void)hangup {
  177. self.remoteVideoTrack = nil;
  178. _videoCallView.localVideoView.captureSession = nil;
  179. [_captureController stopCapture];
  180. _captureController = nil;
  181. [_fileCaptureController stopCapture];
  182. _fileCaptureController = nil;
  183. [_client disconnect];
  184. [_delegate viewControllerDidFinish:self];
  185. }
  186. - (NSString *)statusTextForState:(RTCIceConnectionState)state {
  187. switch (state) {
  188. case RTCIceConnectionStateNew:
  189. case RTCIceConnectionStateChecking:
  190. return @"Connecting...";
  191. case RTCIceConnectionStateConnected:
  192. case RTCIceConnectionStateCompleted:
  193. case RTCIceConnectionStateFailed:
  194. case RTCIceConnectionStateDisconnected:
  195. case RTCIceConnectionStateClosed:
  196. case RTCIceConnectionStateCount:
  197. return nil;
  198. }
  199. }
  200. - (void)showAlertWithMessage:(NSString*)message {
  201. UIAlertController *alert =
  202. [UIAlertController alertControllerWithTitle:nil
  203. message:message
  204. preferredStyle:UIAlertControllerStyleAlert];
  205. UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK"
  206. style:UIAlertActionStyleDefault
  207. handler:^(UIAlertAction *action){
  208. }];
  209. [alert addAction:defaultAction];
  210. [self presentViewController:alert animated:YES completion:nil];
  211. }
  212. @end