1acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org/*
2a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis *  Copyright 2014 The WebRTC Project Authors. All rights reserved.
3acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org *
4a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis *  Use of this source code is governed by a BSD-style license
5a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis *  that can be found in the LICENSE file in the root of the source
6a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis *  tree. An additional intellectual property rights grant can be found
7a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis *  in the file PATENTS.  All contributing project authors may
8a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis *  be found in the AUTHORS file in the root of the source tree.
9acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org */
10acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
11acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org#import "APPRTCViewController.h"
12acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
13acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org#import <AVFoundation/AVFoundation.h>
1487776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org#import "ARDAppClient.h"
15acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org#import "RTCNSGLVideoView.h"
168125744a5f95101d5f3ce376c1248727c68b142ftkchin@webrtc.org#import "RTCVideoTrack.h"
17acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
18acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.orgstatic NSUInteger const kContentWidth = 1280;
19acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.orgstatic NSUInteger const kContentHeight = 720;
20acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.orgstatic NSUInteger const kRoomFieldWidth = 80;
21acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.orgstatic NSUInteger const kLogViewHeight = 280;
22acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
23acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@class APPRTCMainView;
24acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@protocol APPRTCMainViewDelegate
25acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
26acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (void)appRTCMainView:(APPRTCMainView*)mainView
27acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org        didEnterRoomId:(NSString*)roomId;
28acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
29acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@end
30acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
31acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@interface APPRTCMainView : NSView
32acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
33acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@property(nonatomic, weak) id<APPRTCMainViewDelegate> delegate;
34acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@property(nonatomic, readonly) RTCNSGLVideoView* localVideoView;
35acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@property(nonatomic, readonly) RTCNSGLVideoView* remoteVideoView;
36acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
37acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (void)displayLogMessage:(NSString*)message;
38acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
39acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@end
40acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
41acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@interface APPRTCMainView () <NSTextFieldDelegate, RTCNSGLVideoViewDelegate>
42acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@end
43acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@implementation APPRTCMainView  {
44acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSScrollView* _scrollView;
45acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSTextField* _roomLabel;
46acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSTextField* _roomField;
47acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSTextView* _logView;
48acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  RTCNSGLVideoView* _localVideoView;
49acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  RTCNSGLVideoView* _remoteVideoView;
50acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  CGSize _localVideoSize;
51acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  CGSize _remoteVideoSize;
52acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
53acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
54acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org+ (BOOL)requiresConstraintBasedLayout {
55acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  return YES;
56acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
57acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
58acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (instancetype)initWithFrame:(NSRect)frame {
59acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  if (self = [super initWithFrame:frame]) {
60acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    [self setupViews];
61acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  }
62acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  return self;
63acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
64acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
65acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (void)updateConstraints {
66acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSParameterAssert(
67acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org      _roomField != nil && _scrollView != nil && _remoteVideoView != nil);
68acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [self removeConstraints:[self constraints]];
69acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSDictionary* viewsDictionary =
70acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org      NSDictionaryOfVariableBindings(_roomLabel,
71acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org                                     _roomField,
72acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org                                     _scrollView,
73acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org                                     _remoteVideoView);
74acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
75acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSSize remoteViewSize = [self remoteVideoViewSize];
76acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSDictionary* metrics = @{
77acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    @"kLogViewHeight" : @(kLogViewHeight),
78acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    @"kRoomFieldWidth" : @(kRoomFieldWidth),
79acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    @"remoteViewWidth" : @(remoteViewSize.width),
80acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    @"remoteViewHeight" : @(remoteViewSize.height),
81acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  };
82acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  // Declare this separately to avoid compiler warning about splitting string
83acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  // within an NSArray expression.
84acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSString* verticalConstraint =
85acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org      @"V:|-[_roomLabel]-[_roomField]-[_scrollView(kLogViewHeight)]"
86acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org       "-[_remoteVideoView(remoteViewHeight)]-|";
87acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSArray* constraintFormats = @[
88acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org      verticalConstraint,
89acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org      @"|-[_roomLabel]",
90acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org      @"|-[_roomField(kRoomFieldWidth)]",
91acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org      @"|-[_scrollView(remoteViewWidth)]-|",
92acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org      @"|-[_remoteVideoView(remoteViewWidth)]-|",
93acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  ];
94acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  for (NSString* constraintFormat in constraintFormats) {
95acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    NSArray* constraints =
96acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org        [NSLayoutConstraint constraintsWithVisualFormat:constraintFormat
97acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org                                                options:0
98acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org                                                metrics:metrics
99acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org                                                  views:viewsDictionary];
100acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    for (NSLayoutConstraint* constraint in constraints) {
101acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org      [self addConstraint:constraint];
102acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    }
103acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  }
104acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [super updateConstraints];
105acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
106acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
107acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (void)displayLogMessage:(NSString*)message {
108acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  _logView.string =
109acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org      [NSString stringWithFormat:@"%@%@\n", _logView.string, message];
110acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSRange range = NSMakeRange([_logView.string length], 0);
111acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_logView scrollRangeToVisible:range];
112acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
113acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
114acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org#pragma mark - NSControl delegate
115acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
116acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (void)controlTextDidEndEditing:(NSNotification*)notification {
117acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSDictionary* userInfo = [notification userInfo];
118acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSInteger textMovement = [userInfo[@"NSTextMovement"] intValue];
119acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  if (textMovement == NSReturnTextMovement) {
120acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    [self.delegate appRTCMainView:self didEnterRoomId:_roomField.stringValue];
121acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  }
122acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
123acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
124acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org#pragma mark - RTCNSGLVideoViewDelegate
125acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
126acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (void)videoView:(RTCNSGLVideoView*)videoView
127acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    didChangeVideoSize:(NSSize)size {
128acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  if (videoView == _remoteVideoView) {
129acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    _remoteVideoSize = size;
130acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  } else if (videoView == _localVideoView) {
131acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    _localVideoSize = size;
132acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  } else {
133acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    return;
134acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  }
135acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [self setNeedsUpdateConstraints:YES];
136acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
137acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
138acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org#pragma mark - Private
139acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
140acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (void)setupViews {
141acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSParameterAssert([[self subviews] count] == 0);
142acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
143acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  _roomLabel = [[NSTextField alloc] initWithFrame:NSZeroRect];
144acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_roomLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
145acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_roomLabel setBezeled:NO];
146acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_roomLabel setDrawsBackground:NO];
147acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_roomLabel setEditable:NO];
148acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_roomLabel setStringValue:@"Enter AppRTC room id:"];
149acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [self addSubview:_roomLabel];
150acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
151acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  _roomField = [[NSTextField alloc] initWithFrame:NSZeroRect];
152acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_roomField setTranslatesAutoresizingMaskIntoConstraints:NO];
153acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [self addSubview:_roomField];
154acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_roomField setEditable:YES];
155acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_roomField setDelegate:self];
156acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
157acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  _logView = [[NSTextView alloc] initWithFrame:NSZeroRect];
158acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_logView setMinSize:NSMakeSize(0, kLogViewHeight)];
159acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_logView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
160acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_logView setVerticallyResizable:YES];
161acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_logView setAutoresizingMask:NSViewWidthSizable];
162acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSTextContainer* textContainer = [_logView textContainer];
163acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSSize containerSize = NSMakeSize(kContentWidth, FLT_MAX);
164acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [textContainer setContainerSize:containerSize];
165acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [textContainer setWidthTracksTextView:YES];
166acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_logView setEditable:NO];
167acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
168acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  _scrollView = [[NSScrollView alloc] initWithFrame:NSZeroRect];
169acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
170acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_scrollView setHasVerticalScroller:YES];
171acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_scrollView setDocumentView:_logView];
172acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [self addSubview:_scrollView];
173acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
174acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSOpenGLPixelFormatAttribute attributes[] = {
175acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    NSOpenGLPFADoubleBuffer,
176acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    NSOpenGLPFADepthSize, 24,
177acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    NSOpenGLPFAOpenGLProfile,
178acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    NSOpenGLProfileVersion3_2Core,
179acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    0
180acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  };
181acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSOpenGLPixelFormat* pixelFormat =
182acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org      [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
183acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  _remoteVideoView = [[RTCNSGLVideoView alloc] initWithFrame:NSZeroRect
184acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org                                                 pixelFormat:pixelFormat];
185acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [_remoteVideoView setTranslatesAutoresizingMaskIntoConstraints:NO];
186acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  _remoteVideoView.delegate = self;
187acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [self addSubview:_remoteVideoView];
188acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
189acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  // TODO(tkchin): create local video view.
190acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  // https://code.google.com/p/webrtc/issues/detail?id=3417.
191acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
192acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
193acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (NSSize)remoteVideoViewSize {
194acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  if (_remoteVideoSize.width > 0 && _remoteVideoSize.height > 0) {
195acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    return _remoteVideoSize;
196acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  } else {
197acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org    return NSMakeSize(kContentWidth, kContentHeight);
198acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  }
199acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
200acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
201acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (NSSize)localVideoViewSize {
202acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  return NSZeroSize;
203acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
204acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
205acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@end
206acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
207acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@interface APPRTCViewController ()
20887776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org    <ARDAppClientDelegate, APPRTCMainViewDelegate>
209acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@property(nonatomic, readonly) APPRTCMainView* mainView;
210acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@end
211acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
212acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@implementation APPRTCViewController {
21387776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org  ARDAppClient* _client;
2148125744a5f95101d5f3ce376c1248727c68b142ftkchin@webrtc.org  RTCVideoTrack* _localVideoTrack;
2158125744a5f95101d5f3ce376c1248727c68b142ftkchin@webrtc.org  RTCVideoTrack* _remoteVideoTrack;
216acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
217acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
218acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (void)dealloc {
219acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [self disconnect];
220acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
221acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
222acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (void)loadView {
223acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  APPRTCMainView* view = [[APPRTCMainView alloc] initWithFrame:NSZeroRect];
224acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [view setTranslatesAutoresizingMaskIntoConstraints:NO];
225acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  view.delegate = self;
226acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  self.view = view;
227acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
228acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
229acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (void)windowWillClose:(NSNotification*)notification {
230acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [self disconnect];
231acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
232acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
23387776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org#pragma mark - ARDAppClientDelegate
23487776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org
23587776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org- (void)appClient:(ARDAppClient *)client
23687776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org    didChangeState:(ARDAppClientState)state {
23787776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org  switch (state) {
23887776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org    case kARDAppClientStateConnected:
23987776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org      NSLog(@"Client connected.");
24087776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org      break;
24187776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org    case kARDAppClientStateConnecting:
24287776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org      NSLog(@"Client connecting.");
24387776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org      break;
24487776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org    case kARDAppClientStateDisconnected:
24587776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org      NSLog(@"Client disconnected.");
24687776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org      [self resetUI];
24787776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org      _client = nil;
24887776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org      break;
24987776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org  }
25087776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org}
251acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
25287776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org- (void)appClient:(ARDAppClient *)client
2533a63a3c35d93616606c23d27583b83a198b94a3etkchin@webrtc.org    didChangeConnectionState:(RTCICEConnectionState)state {
2543a63a3c35d93616606c23d27583b83a198b94a3etkchin@webrtc.org}
2553a63a3c35d93616606c23d27583b83a198b94a3etkchin@webrtc.org
2563a63a3c35d93616606c23d27583b83a198b94a3etkchin@webrtc.org- (void)appClient:(ARDAppClient *)client
25787776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org    didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
2588125744a5f95101d5f3ce376c1248727c68b142ftkchin@webrtc.org  _localVideoTrack = localVideoTrack;
259acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
260acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
26187776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org- (void)appClient:(ARDAppClient *)client
26287776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org    didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
2638125744a5f95101d5f3ce376c1248727c68b142ftkchin@webrtc.org  _remoteVideoTrack = remoteVideoTrack;
2648125744a5f95101d5f3ce376c1248727c68b142ftkchin@webrtc.org  [_remoteVideoTrack addRenderer:self.mainView.remoteVideoView];
265acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
266acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
26787776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org- (void)appClient:(ARDAppClient *)client
26887776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org         didError:(NSError *)error {
26987776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org  [self showAlertWithMessage:[NSString stringWithFormat:@"%@", error]];
270acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [self disconnect];
271acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
272acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
273d33258098b99068064e0d4a8cfc6566dec8e881eZeke Chin- (void)appClient:(ARDAppClient *)client
274d33258098b99068064e0d4a8cfc6566dec8e881eZeke Chin      didGetStats:(NSArray *)stats {
275d33258098b99068064e0d4a8cfc6566dec8e881eZeke Chin}
276d33258098b99068064e0d4a8cfc6566dec8e881eZeke Chin
277acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org#pragma mark - APPRTCMainViewDelegate
278acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
279acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (void)appRTCMainView:(APPRTCMainView*)mainView
280acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org        didEnterRoomId:(NSString*)roomId {
28187776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org  [_client disconnect];
28287776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org  ARDAppClient *client = [[ARDAppClient alloc] initWithDelegate:self];
283913e645e10bd3a060add29ac634a7d28f82744f6haysc  [client connectToRoomWithId:roomId isLoopback:NO isAudioOnly:NO];
28487776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org  _client = client;
285acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
286acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
287acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org#pragma mark - Private
288acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
289acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (APPRTCMainView*)mainView {
290acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  return (APPRTCMainView*)self.view;
291acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
292acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
293acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org- (void)showAlertWithMessage:(NSString*)message {
294acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  NSAlert* alert = [[NSAlert alloc] init];
295acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [alert setMessageText:message];
296acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org  [alert runModal];
297acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
298acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
29987776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org- (void)resetUI {
3008125744a5f95101d5f3ce376c1248727c68b142ftkchin@webrtc.org  [_remoteVideoTrack removeRenderer:self.mainView.remoteVideoView];
3018125744a5f95101d5f3ce376c1248727c68b142ftkchin@webrtc.org  _remoteVideoTrack = nil;
3028125744a5f95101d5f3ce376c1248727c68b142ftkchin@webrtc.org  [self.mainView.remoteVideoView renderFrame:nil];
30387776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org}
30487776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org
30587776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org- (void)disconnect {
30687776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org  [self resetUI];
30787776a893546e1a503b679172aa68055fd634f7btkchin@webrtc.org  [_client disconnect];
308acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org}
309acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org
310acca675bcf8fefd1d1985ea3d0fb8f9ea65f5d4atkchin@webrtc.org@end
311