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