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