blob: 0d5ac9a3b91a2765eaa552d86d4a94f4d7dec543 [file] [log] [blame]
Anders Carlsson73119182018-03-15 09:41:03 +01001/*
2 * Copyright 2018 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#import "NADViewController.h"
12
13#import <WebRTC/RTCCameraPreviewView.h>
14#import <WebRTC/RTCCameraVideoCapturer.h>
15#import <WebRTC/RTCEAGLVideoView.h>
16#import <WebRTC/RTCMTLVideoView.h>
17#import <WebRTC/RTCVideoRenderer.h>
18#include <memory>
19
20#include "objccallclient.h"
21
22@interface NADViewController ()
23
24@property(nonatomic) RTCCameraVideoCapturer *capturer;
25@property(nonatomic) RTCCameraPreviewView *localVideoView;
26@property(nonatomic) __kindof UIView<RTCVideoRenderer> *remoteVideoView;
27@property(nonatomic) UIButton *callButton;
28@property(nonatomic) UIButton *hangUpButton;
29
30@end
31
32@implementation NADViewController {
33 std::unique_ptr<webrtc_examples::ObjCCallClient> _call_client;
34
35 UIView *_view;
36}
37
38@synthesize capturer = _capturer;
39@synthesize localVideoView = _localVideoView;
40@synthesize remoteVideoView = _remoteVideoView;
41@synthesize callButton = _callButton;
42@synthesize hangUpButton = _hangUpButton;
43
44#pragma mark - View controller lifecycle
45
46- (void)loadView {
47 _view = [[UIView alloc] initWithFrame:CGRectZero];
48
49#if defined(RTC_SUPPORTS_METAL)
50 _remoteVideoView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero];
51#else
52 _remoteVideoView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectZero];
53#endif
54 _remoteVideoView.translatesAutoresizingMaskIntoConstraints = NO;
55 [_view addSubview:_remoteVideoView];
56
57 _localVideoView = [[RTCCameraPreviewView alloc] initWithFrame:CGRectZero];
58 _localVideoView.translatesAutoresizingMaskIntoConstraints = NO;
59 [_view addSubview:_localVideoView];
60
61 _callButton = [UIButton buttonWithType:UIButtonTypeSystem];
62 _callButton.translatesAutoresizingMaskIntoConstraints = NO;
63 [_callButton setTitle:@"Call" forState:UIControlStateNormal];
64 [_callButton addTarget:self action:@selector(call:) forControlEvents:UIControlEventTouchUpInside];
65 [_view addSubview:_callButton];
66
67 _hangUpButton = [UIButton buttonWithType:UIButtonTypeSystem];
68 _hangUpButton.translatesAutoresizingMaskIntoConstraints = NO;
69 [_hangUpButton setTitle:@"Hang up" forState:UIControlStateNormal];
70 [_hangUpButton addTarget:self
71 action:@selector(hangUp:)
72 forControlEvents:UIControlEventTouchUpInside];
73 [_view addSubview:_hangUpButton];
74
75 UILayoutGuide *margin = _view.layoutMarginsGuide;
76 [_remoteVideoView.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor].active = YES;
77 [_remoteVideoView.topAnchor constraintEqualToAnchor:margin.topAnchor].active = YES;
78 [_remoteVideoView.trailingAnchor constraintEqualToAnchor:margin.trailingAnchor].active = YES;
79 [_remoteVideoView.bottomAnchor constraintEqualToAnchor:margin.bottomAnchor].active = YES;
80
81 [_localVideoView.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor constant:8.0].active =
82 YES;
83 [_localVideoView.topAnchor constraintEqualToAnchor:margin.topAnchor constant:8.0].active = YES;
84 [_localVideoView.widthAnchor constraintEqualToConstant:60].active = YES;
85 [_localVideoView.heightAnchor constraintEqualToConstant:60].active = YES;
86
87 [_callButton.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor constant:8.0].active =
88 YES;
89 [_callButton.bottomAnchor constraintEqualToAnchor:margin.bottomAnchor constant:8.0].active = YES;
90 [_callButton.widthAnchor constraintEqualToConstant:100].active = YES;
91 [_callButton.heightAnchor constraintEqualToConstant:40].active = YES;
92
93 [_hangUpButton.trailingAnchor constraintEqualToAnchor:margin.trailingAnchor constant:8.0].active =
94 YES;
95 [_hangUpButton.bottomAnchor constraintEqualToAnchor:margin.bottomAnchor constant:8.0].active =
96 YES;
97 [_hangUpButton.widthAnchor constraintEqualToConstant:100].active = YES;
98 [_hangUpButton.heightAnchor constraintEqualToConstant:40].active = YES;
99
100 self.view = _view;
101}
102
103- (void)viewDidLoad {
104 [super viewDidLoad];
105
106 self.capturer = [[RTCCameraVideoCapturer alloc] init];
107 self.localVideoView.captureSession = self.capturer.captureSession;
108
109 _call_client.reset(new webrtc_examples::ObjCCallClient());
110
111 // Start capturer.
112 AVCaptureDevice *selectedDevice = nil;
113 NSArray<AVCaptureDevice *> *captureDevices = [RTCCameraVideoCapturer captureDevices];
114 for (AVCaptureDevice *device in captureDevices) {
115 if (device.position == AVCaptureDevicePositionFront) {
116 selectedDevice = device;
117 break;
118 }
119 }
120
121 AVCaptureDeviceFormat *selectedFormat = nil;
122 int targetWidth = 640;
123 int targetHeight = 480;
124 int currentDiff = INT_MAX;
125 NSArray<AVCaptureDeviceFormat *> *formats =
126 [RTCCameraVideoCapturer supportedFormatsForDevice:selectedDevice];
127 for (AVCaptureDeviceFormat *format in formats) {
128 CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions(format.formatDescription);
129 FourCharCode pixelFormat = CMFormatDescriptionGetMediaSubType(format.formatDescription);
130 int diff = abs(targetWidth - dimension.width) + abs(targetHeight - dimension.height);
131 if (diff < currentDiff) {
132 selectedFormat = format;
133 currentDiff = diff;
134 } else if (diff == currentDiff && pixelFormat == [_capturer preferredOutputPixelFormat]) {
135 selectedFormat = format;
136 }
137 }
138
139 [self.capturer startCaptureWithDevice:selectedDevice format:selectedFormat fps:30];
140}
141
142- (void)didReceiveMemoryWarning {
143 [super didReceiveMemoryWarning];
144 // Dispose of any resources that can be recreated.
145}
146
147#pragma mark - Actions
148
149- (IBAction)call:(id)sender {
150 _call_client->Call(self.capturer, self.remoteVideoView);
151}
152
153- (IBAction)hangUp:(id)sender {
154 _call_client->Hangup();
155}
156
157@end