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.

438 lines
14 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2014 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 "APPRTCViewController.h"
  11. #import <AVFoundation/AVFoundation.h>
  12. #import <WebRTC/RTCMTLNSVideoView.h>
  13. #import <WebRTC/RTCNSGLVideoView.h>
  14. #import <WebRTC/RTCVideoTrack.h>
  15. #import "ARDAppClient.h"
  16. #import "ARDCaptureController.h"
  17. #import "ARDSettingsModel.h"
  18. static NSUInteger const kContentWidth = 900;
  19. static NSUInteger const kRoomFieldWidth = 200;
  20. static NSUInteger const kActionItemHeight = 30;
  21. static NSUInteger const kBottomViewHeight = 200;
  22. @class APPRTCMainView;
  23. @protocol APPRTCMainViewDelegate
  24. - (void)appRTCMainView:(APPRTCMainView*)mainView
  25. didEnterRoomId:(NSString*)roomId
  26. loopback:(BOOL)isLoopback;
  27. @end
  28. @interface APPRTCMainView : NSView
  29. @property(nonatomic, weak) id<APPRTCMainViewDelegate> delegate;
  30. @property(nonatomic, readonly) NSView<RTCVideoRenderer>* localVideoView;
  31. @property(nonatomic, readonly) NSView<RTCVideoRenderer>* remoteVideoView;
  32. @property(nonatomic, readonly) NSTextView* logView;
  33. - (void)displayLogMessage:(NSString*)message;
  34. @end
  35. @interface APPRTCMainView () <NSTextFieldDelegate, RTCNSGLVideoViewDelegate>
  36. @end
  37. @implementation APPRTCMainView {
  38. NSScrollView* _scrollView;
  39. NSView* _actionItemsView;
  40. NSButton* _connectButton;
  41. NSButton* _loopbackButton;
  42. NSTextField* _roomField;
  43. CGSize _localVideoSize;
  44. CGSize _remoteVideoSize;
  45. }
  46. @synthesize delegate = _delegate;
  47. @synthesize localVideoView = _localVideoView;
  48. @synthesize remoteVideoView = _remoteVideoView;
  49. @synthesize logView = _logView;
  50. - (void)displayLogMessage:(NSString *)message {
  51. dispatch_async(dispatch_get_main_queue(), ^{
  52. self.logView.string = [NSString stringWithFormat:@"%@%@\n", self.logView.string, message];
  53. NSRange range = NSMakeRange(self.logView.string.length, 0);
  54. [self.logView scrollRangeToVisible:range];
  55. });
  56. }
  57. #pragma mark - Private
  58. - (instancetype)initWithFrame:(NSRect)frame {
  59. if (self = [super initWithFrame:frame]) {
  60. [self setupViews];
  61. }
  62. return self;
  63. }
  64. + (BOOL)requiresConstraintBasedLayout {
  65. return YES;
  66. }
  67. - (void)updateConstraints {
  68. NSParameterAssert(
  69. _roomField != nil &&
  70. _scrollView != nil &&
  71. _remoteVideoView != nil &&
  72. _localVideoView != nil &&
  73. _actionItemsView!= nil &&
  74. _connectButton != nil &&
  75. _loopbackButton != nil);
  76. [self removeConstraints:[self constraints]];
  77. NSDictionary* viewsDictionary =
  78. NSDictionaryOfVariableBindings(_roomField,
  79. _scrollView,
  80. _remoteVideoView,
  81. _localVideoView,
  82. _actionItemsView,
  83. _connectButton,
  84. _loopbackButton);
  85. NSSize remoteViewSize = [self remoteVideoViewSize];
  86. NSDictionary* metrics = @{
  87. @"remoteViewWidth" : @(remoteViewSize.width),
  88. @"remoteViewHeight" : @(remoteViewSize.height),
  89. @"kBottomViewHeight" : @(kBottomViewHeight),
  90. @"localViewHeight" : @(remoteViewSize.height / 3),
  91. @"localViewWidth" : @(remoteViewSize.width / 3),
  92. @"kRoomFieldWidth" : @(kRoomFieldWidth),
  93. @"kActionItemHeight" : @(kActionItemHeight)
  94. };
  95. // Declare this separately to avoid compiler warning about splitting string
  96. // within an NSArray expression.
  97. NSString* verticalConstraintLeft =
  98. @"V:|-[_remoteVideoView(remoteViewHeight)]-[_scrollView(kBottomViewHeight)]-|";
  99. NSString* verticalConstraintRight =
  100. @"V:|-[_remoteVideoView(remoteViewHeight)]-[_actionItemsView(kBottomViewHeight)]-|";
  101. NSArray* constraintFormats = @[
  102. verticalConstraintLeft,
  103. verticalConstraintRight,
  104. @"H:|-[_remoteVideoView(remoteViewWidth)]-|",
  105. @"V:|-[_localVideoView(localViewHeight)]",
  106. @"H:|-[_localVideoView(localViewWidth)]",
  107. @"H:|-[_scrollView(==_actionItemsView)]-[_actionItemsView]-|"
  108. ];
  109. NSArray* actionItemsConstraints = @[
  110. @"H:|-[_roomField(kRoomFieldWidth)]-[_loopbackButton(kRoomFieldWidth)]",
  111. @"H:|-[_connectButton(kRoomFieldWidth)]",
  112. @"V:|-[_roomField(kActionItemHeight)]-[_connectButton(kActionItemHeight)]",
  113. @"V:|-[_loopbackButton(kActionItemHeight)]",
  114. ];
  115. [APPRTCMainView addConstraints:constraintFormats
  116. toView:self
  117. viewsDictionary:viewsDictionary
  118. metrics:metrics];
  119. [APPRTCMainView addConstraints:actionItemsConstraints
  120. toView:_actionItemsView
  121. viewsDictionary:viewsDictionary
  122. metrics:metrics];
  123. [super updateConstraints];
  124. }
  125. #pragma mark - Constraints helper
  126. + (void)addConstraints:(NSArray*)constraints toView:(NSView*)view
  127. viewsDictionary:(NSDictionary*)viewsDictionary
  128. metrics:(NSDictionary*)metrics {
  129. for (NSString* constraintFormat in constraints) {
  130. NSArray* constraints =
  131. [NSLayoutConstraint constraintsWithVisualFormat:constraintFormat
  132. options:0
  133. metrics:metrics
  134. views:viewsDictionary];
  135. for (NSLayoutConstraint* constraint in constraints) {
  136. [view addConstraint:constraint];
  137. }
  138. }
  139. }
  140. #pragma mark - Control actions
  141. - (void)startCall:(id)sender {
  142. NSString* roomString = _roomField.stringValue;
  143. // Generate room id for loopback options.
  144. if (_loopbackButton.intValue && [roomString isEqualToString:@""]) {
  145. roomString = [NSUUID UUID].UUIDString;
  146. roomString = [roomString stringByReplacingOccurrencesOfString:@"-" withString:@""];
  147. }
  148. [self.delegate appRTCMainView:self
  149. didEnterRoomId:roomString
  150. loopback:_loopbackButton.intValue];
  151. [self setNeedsUpdateConstraints:YES];
  152. }
  153. #pragma mark - RTCNSGLVideoViewDelegate
  154. - (void)videoView:(RTCNSGLVideoView*)videoView
  155. didChangeVideoSize:(NSSize)size {
  156. if (videoView == _remoteVideoView) {
  157. _remoteVideoSize = size;
  158. } else if (videoView == _localVideoView) {
  159. _localVideoSize = size;
  160. } else {
  161. return;
  162. }
  163. [self setNeedsUpdateConstraints:YES];
  164. }
  165. #pragma mark - Private
  166. - (void)setupViews {
  167. NSParameterAssert([[self subviews] count] == 0);
  168. _logView = [[NSTextView alloc] initWithFrame:NSZeroRect];
  169. [_logView setMinSize:NSMakeSize(0, kBottomViewHeight)];
  170. [_logView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
  171. [_logView setVerticallyResizable:YES];
  172. [_logView setAutoresizingMask:NSViewWidthSizable];
  173. NSTextContainer* textContainer = [_logView textContainer];
  174. NSSize containerSize = NSMakeSize(kContentWidth, FLT_MAX);
  175. [textContainer setContainerSize:containerSize];
  176. [textContainer setWidthTracksTextView:YES];
  177. [_logView setEditable:NO];
  178. [self setupActionItemsView];
  179. _scrollView = [[NSScrollView alloc] initWithFrame:NSZeroRect];
  180. [_scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
  181. [_scrollView setHasVerticalScroller:YES];
  182. [_scrollView setDocumentView:_logView];
  183. [self addSubview:_scrollView];
  184. // NOTE (daniela): Ignoring Clang diagonstic here.
  185. // We're performing run time check to make sure class is available on runtime.
  186. // If not we're providing sensible default.
  187. #pragma clang diagnostic push
  188. #pragma clang diagnostic ignored "-Wpartial-availability"
  189. if ([RTCMTLNSVideoView class] && [RTCMTLNSVideoView isMetalAvailable]) {
  190. _remoteVideoView = [[RTCMTLNSVideoView alloc] initWithFrame:NSZeroRect];
  191. _localVideoView = [[RTCMTLNSVideoView alloc] initWithFrame:NSZeroRect];
  192. }
  193. #pragma clang diagnostic pop
  194. if (_remoteVideoView == nil) {
  195. NSOpenGLPixelFormatAttribute attributes[] = {
  196. NSOpenGLPFADoubleBuffer,
  197. NSOpenGLPFADepthSize, 24,
  198. NSOpenGLPFAOpenGLProfile,
  199. NSOpenGLProfileVersion3_2Core,
  200. 0
  201. };
  202. NSOpenGLPixelFormat* pixelFormat =
  203. [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  204. RTCNSGLVideoView* remote =
  205. [[RTCNSGLVideoView alloc] initWithFrame:NSZeroRect pixelFormat:pixelFormat];
  206. remote.delegate = self;
  207. _remoteVideoView = remote;
  208. RTCNSGLVideoView* local =
  209. [[RTCNSGLVideoView alloc] initWithFrame:NSZeroRect pixelFormat:pixelFormat];
  210. local.delegate = self;
  211. _localVideoView = local;
  212. }
  213. [_remoteVideoView setTranslatesAutoresizingMaskIntoConstraints:NO];
  214. [self addSubview:_remoteVideoView];
  215. [_localVideoView setTranslatesAutoresizingMaskIntoConstraints:NO];
  216. [self addSubview:_localVideoView];
  217. }
  218. - (void)setupActionItemsView {
  219. _actionItemsView = [[NSView alloc] initWithFrame:NSZeroRect];
  220. [_actionItemsView setTranslatesAutoresizingMaskIntoConstraints:NO];
  221. [self addSubview:_actionItemsView];
  222. _roomField = [[NSTextField alloc] initWithFrame:NSZeroRect];
  223. [_roomField setTranslatesAutoresizingMaskIntoConstraints:NO];
  224. [[_roomField cell] setPlaceholderString: @"Enter AppRTC room id"];
  225. [_actionItemsView addSubview:_roomField];
  226. [_roomField setEditable:YES];
  227. _connectButton = [[NSButton alloc] initWithFrame:NSZeroRect];
  228. [_connectButton setTranslatesAutoresizingMaskIntoConstraints:NO];
  229. _connectButton.title = @"Start call";
  230. _connectButton.bezelStyle = NSRoundedBezelStyle;
  231. _connectButton.target = self;
  232. _connectButton.action = @selector(startCall:);
  233. [_actionItemsView addSubview:_connectButton];
  234. _loopbackButton = [[NSButton alloc] initWithFrame:NSZeroRect];
  235. [_loopbackButton setTranslatesAutoresizingMaskIntoConstraints:NO];
  236. _loopbackButton.title = @"Loopback";
  237. [_loopbackButton setButtonType:NSSwitchButton];
  238. [_actionItemsView addSubview:_loopbackButton];
  239. }
  240. - (NSSize)remoteVideoViewSize {
  241. if (!_remoteVideoView.bounds.size.width) {
  242. return NSMakeSize(kContentWidth, 0);
  243. }
  244. NSInteger width = MAX(_remoteVideoView.bounds.size.width, kContentWidth);
  245. NSInteger height = (width/16) * 9;
  246. return NSMakeSize(width, height);
  247. }
  248. @end
  249. @interface APPRTCViewController ()
  250. <ARDAppClientDelegate, APPRTCMainViewDelegate>
  251. @property(nonatomic, readonly) APPRTCMainView* mainView;
  252. @end
  253. @implementation APPRTCViewController {
  254. ARDAppClient* _client;
  255. RTCVideoTrack* _localVideoTrack;
  256. RTCVideoTrack* _remoteVideoTrack;
  257. ARDCaptureController* _captureController;
  258. }
  259. - (void)dealloc {
  260. [self disconnect];
  261. }
  262. - (void)viewDidAppear {
  263. [super viewDidAppear];
  264. [self displayUsageInstructions];
  265. }
  266. - (void)loadView {
  267. APPRTCMainView* view = [[APPRTCMainView alloc] initWithFrame:NSZeroRect];
  268. [view setTranslatesAutoresizingMaskIntoConstraints:NO];
  269. view.delegate = self;
  270. self.view = view;
  271. }
  272. - (void)windowWillClose:(NSNotification*)notification {
  273. [self disconnect];
  274. }
  275. #pragma mark - Usage
  276. - (void)displayUsageInstructions {
  277. [self.mainView displayLogMessage:
  278. @"To start call:\n"
  279. @"• Enter AppRTC room id (not neccessary for loopback)\n"
  280. @"• Start call"];
  281. }
  282. #pragma mark - ARDAppClientDelegate
  283. - (void)appClient:(ARDAppClient *)client
  284. didChangeState:(ARDAppClientState)state {
  285. switch (state) {
  286. case kARDAppClientStateConnected:
  287. [self.mainView displayLogMessage:@"Client connected."];
  288. break;
  289. case kARDAppClientStateConnecting:
  290. [self.mainView displayLogMessage:@"Client connecting."];
  291. break;
  292. case kARDAppClientStateDisconnected:
  293. [self.mainView displayLogMessage:@"Client disconnected."];
  294. [self resetUI];
  295. _client = nil;
  296. break;
  297. }
  298. }
  299. - (void)appClient:(ARDAppClient *)client
  300. didChangeConnectionState:(RTCIceConnectionState)state {
  301. }
  302. - (void)appClient:(ARDAppClient*)client
  303. didCreateLocalCapturer:(RTCCameraVideoCapturer*)localCapturer {
  304. _captureController =
  305. [[ARDCaptureController alloc] initWithCapturer:localCapturer
  306. settings:[[ARDSettingsModel alloc] init]];
  307. [_captureController startCapture];
  308. }
  309. - (void)appClient:(ARDAppClient *)client
  310. didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
  311. _localVideoTrack = localVideoTrack;
  312. [_localVideoTrack addRenderer:self.mainView.localVideoView];
  313. }
  314. - (void)appClient:(ARDAppClient *)client
  315. didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
  316. _remoteVideoTrack = remoteVideoTrack;
  317. [_remoteVideoTrack addRenderer:self.mainView.remoteVideoView];
  318. }
  319. - (void)appClient:(ARDAppClient *)client
  320. didError:(NSError *)error {
  321. [self showAlertWithMessage:[NSString stringWithFormat:@"%@", error]];
  322. [self disconnect];
  323. }
  324. - (void)appClient:(ARDAppClient *)client
  325. didGetStats:(NSArray *)stats {
  326. }
  327. #pragma mark - APPRTCMainViewDelegate
  328. - (void)appRTCMainView:(APPRTCMainView*)mainView
  329. didEnterRoomId:(NSString*)roomId
  330. loopback:(BOOL)isLoopback {
  331. if ([roomId isEqualToString:@""]) {
  332. [self.mainView displayLogMessage:@"Missing room id"];
  333. return;
  334. }
  335. [self disconnect];
  336. ARDAppClient* client = [[ARDAppClient alloc] initWithDelegate:self];
  337. [client connectToRoomWithId:roomId
  338. settings:[[ARDSettingsModel alloc] init] // Use default settings.
  339. isLoopback:isLoopback];
  340. _client = client;
  341. }
  342. #pragma mark - Private
  343. - (APPRTCMainView*)mainView {
  344. return (APPRTCMainView*)self.view;
  345. }
  346. - (void)showAlertWithMessage:(NSString*)message {
  347. dispatch_async(dispatch_get_main_queue(), ^{
  348. NSAlert* alert = [[NSAlert alloc] init];
  349. [alert setMessageText:message];
  350. [alert runModal];
  351. });
  352. }
  353. - (void)resetUI {
  354. [_remoteVideoTrack removeRenderer:self.mainView.remoteVideoView];
  355. [_localVideoTrack removeRenderer:self.mainView.localVideoView];
  356. _remoteVideoTrack = nil;
  357. _localVideoTrack = nil;
  358. [self.mainView.remoteVideoView renderFrame:nil];
  359. [self.mainView.localVideoView renderFrame:nil];
  360. }
  361. - (void)disconnect {
  362. [self resetUI];
  363. [_captureController stopCapture];
  364. _captureController = nil;
  365. [_client disconnect];
  366. }
  367. @end