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