/
ViewController.m
executable file
·395 lines (351 loc) · 16.9 KB
/
ViewController.m
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
//
// ViewController.m
// WCSApiExample
//
// Created by user on 24/11/2015.
// Copyright © 2015 user. All rights reserved.
//
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <FPWCSApi2/FPWCSApi2.h>
#import "WCSUtil.h"
@interface ViewController ()
@end
@implementation ViewController {
FPWCSApi2Session *_session;
FPWCSApi2Stream *_localStream;
FPWCSApi2Stream *_remoteStream;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setupViews];
[self setupLayout];
}
- (void)start {
if (!_session || [_session getStatus] != kFPWCSSessionStatusEstablished || ![[_session getServerUrl] isEqualToString:_urlInput.text]) {
if (_session && ![[_session getServerUrl] isEqualToString:_urlInput.text]) {
[_session on:kFPWCSSessionStatusDisconnected callback:^(FPWCSApi2Session *session){}];
[_session on:kFPWCSSessionStatusFailed callback:^(FPWCSApi2Session *session){}];
[_session disconnect];
}
FPWCSApi2SessionOptions *options = [[FPWCSApi2SessionOptions alloc] init];
options.urlServer = _urlInput.text;
options.appKey = @"defaultApp";
NSError *error;
_session = [FPWCSApi2 createSession:options error:&error];
if (!_session) {
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Failed to connect"
message:error.localizedDescription
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self onStopped];
}];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
return;
}
[_session on:kFPWCSSessionStatusEstablished callback:^(FPWCSApi2Session *session){
[self startStreaming];
}];
[_session on:kFPWCSSessionStatusDisconnected callback:^(FPWCSApi2Session *session){
[self onStopped];
}];
[_session on:kFPWCSSessionStatusFailed callback:^(FPWCSApi2Session *session){
[self onStopped];
}];
[_session connect];
} else {
[self startStreaming];
}
}
- (void)startStreaming {
FPWCSApi2StreamOptions *options = [[FPWCSApi2StreamOptions alloc] init];
options.name = [self getStreamName];
options.display = _videoView.local;
options.constraints = [_localControl toMediaConstraints];
NSError *error;
_localStream = [_session createStream:options error:&error];
if (!_localStream) {
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Failed to publish"
message:error.localizedDescription
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self onStopped];
}];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
return;
}
//initial mute
if (_localControl.muteAudio.control.isOn) {
[_localStream muteAudio];
}
if (_localControl.muteVideo.control.isOn) {
[_localStream muteVideo];
}
[_localStream on:kFPWCSStreamStatusPublishing callback:^(FPWCSApi2Stream *stream){
[self startPlaying];
}];
[_localStream on:kFPWCSStreamStatusUnpublished callback:^(FPWCSApi2Stream *stream){
[self onStopped];
}];
[_localStream on:kFPWCSStreamStatusFailed callback:^(FPWCSApi2Stream *stream){
[self onStopped];
}];
if(![_localStream publish:&error]) {
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Failed to publish"
message:error.localizedDescription
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self onStopped];
}];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
}
}
- (void)startPlaying {
FPWCSApi2StreamOptions *options = [[FPWCSApi2StreamOptions alloc] init];
options.name = [_localStream getName];
options.display = _videoView.remote;
options.constraints = [_remoteControl toMediaConstraints];
NSError *error;
_remoteStream = [_session createStream:options error:&error];
if (!_remoteStream) {
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Failed to play"
message:error.localizedDescription
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self onStopped];
}];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
return;
}
[_remoteStream on:kFPWCSStreamStatusPlaying callback:^(FPWCSApi2Stream *stream){
[self onStarted];
}];
[_remoteStream on:kFPWCSStreamStatusStopped callback:^(FPWCSApi2Stream *rStream){
[_localStream stop:nil];
}];
[_remoteStream on:kFPWCSStreamStatusFailed callback:^(FPWCSApi2Stream *rStream){
if (_localStream && [_localStream getStatus] == kFPWCSStreamStatusPublishing) {
[_localStream stop:nil];
}
}];
if(![_remoteStream play:&error]) {
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Failed to play"
message:error.localizedDescription
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (_localStream && [_localStream getStatus] == kFPWCSStreamStatusPublishing) {
[_localStream stop:nil];
}
}];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
}
}
- (void)onStarted {
[_startButton setTitle:@"Stop" forState:UIControlStateNormal];
_startButton.userInteractionEnabled = YES;
_startButton.alpha = 1;
}
- (void)onStopped {
[_startButton setTitle:@"Start" forState:UIControlStateNormal];
_startButton.userInteractionEnabled = YES;
_startButton.alpha = 1;
_urlInput.userInteractionEnabled = YES;
if (_localStream) {
[FPWCSApi2 releaseLocalMedia:_videoView.local];
}
}
- (void)startButton:(UIButton *)button {
button.userInteractionEnabled = NO;
button.alpha = 0.5;
_urlInput.userInteractionEnabled = NO;
if ([button.titleLabel.text isEqualToString:@"Stop"]) {
if (_remoteStream) {
NSError *error;
[_remoteStream stop:&error];
} else {
NSLog(@"No remote stream, failed to stop");
}
} else {
//start
[self start];
}
}
- (void)localSettingsButton:(UIButton *)button {
[_localControl show];
}
- (void)remoteSettingsButton:(UIButton *)button {
[_remoteControl show];
}
- (void)controlValueChanged:(id)sender {
if (sender == _localControl.muteAudio.control) {
if (_localStream) {
if (_localControl.muteAudio.control.isOn) {
[_localStream muteAudio];
} else {
[_localStream unmuteAudio];
}
}
} else if (sender == _localControl.muteVideo.control) {
if (_localStream) {
if (_localControl.muteVideo.control.isOn) {
[_localStream muteVideo];
} else {
[_localStream unmuteVideo];
}
}
}
}
//user interface views and layout
- (void)setupViews {
_scrollView = [[UIScrollView alloc] init];
_scrollView.translatesAutoresizingMaskIntoConstraints = NO;
_scrollView.scrollEnabled = YES;
_startButton = [WCSViewUtil createButton:@"Start"];
[_startButton addTarget:self action:@selector(startButton:) forControlEvents:UIControlEventTouchUpInside];
_localSettingsButton = [WCSViewUtil createButton:@"Local settings"];
[_localSettingsButton addTarget:self action:@selector(localSettingsButton:) forControlEvents:UIControlEventTouchUpInside];
_remoteSettingsButton = [WCSViewUtil createButton:@"Remote settings"];
[_remoteSettingsButton addTarget:self action:@selector(remoteSettingsButton:) forControlEvents:UIControlEventTouchUpInside];
_settingsButtonContainer = [[UIView alloc] init];
_settingsButtonContainer.translatesAutoresizingMaskIntoConstraints = NO;
_urlInput = [WCSViewUtil createTextField:self];
_urlInput.text = @"ws://192.168.88.234:8080";
_videoView = [[WCSDoubleVideoView alloc] init];
_localControl = [[WCSLocalVideoControlView alloc] init];
[_localControl.muteAudio.control addTarget:self action:@selector(controlValueChanged:) forControlEvents:UIControlEventValueChanged];
[_localControl.muteVideo.control addTarget:self action:@selector(controlValueChanged:) forControlEvents:UIControlEventValueChanged];
_remoteControl = [[WCSRemoteVideoControlView alloc] init];
_contentView = [[UIView alloc] init];
_contentView.translatesAutoresizingMaskIntoConstraints = NO;
[_contentView addSubview:_startButton];
[_settingsButtonContainer addSubview:_localSettingsButton];
[_settingsButtonContainer addSubview:_remoteSettingsButton];
[_contentView addSubview:_settingsButtonContainer];
[_contentView addSubview:_urlInput];
[_contentView addSubview:_videoView];
[_scrollView addSubview:_contentView];
[self.view addSubview:_scrollView];
[self.view addSubview:_localControl];
[self.view addSubview:_remoteControl];
}
- (void)setupLayout {
NSDictionary *views = @{
@"start": _startButton,
@"localSettings": _localSettingsButton,
@"remoteSettings": _remoteSettingsButton,
@"settings": _settingsButtonContainer,
@"urlInput": _urlInput,
@"videoView": _videoView,
@"content": _contentView,
@"localControl": _localControl,
@"remoteControl": _remoteControl,
@"scroll": _scrollView
};
NSDictionary *metrics = @{
@"height": @30,
@"vSpacing": @30,
@"hSpacing": @10
};
void (^setConstraint)(UIView*, NSString*, NSLayoutFormatOptions) = ^(UIView *view, NSString *constraint, NSLayoutFormatOptions options) {
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraint options:options metrics:metrics views:views]];
};
setConstraint(_startButton, @"V:[start(height)]", 0);
setConstraint(_contentView, @"H:|[start]|", 0);
setConstraint(_localSettingsButton, @"V:[localSettings(height)]", 0);
setConstraint(_remoteSettingsButton, @"V:[remoteSettings(height)]", 0);
[_settingsButtonContainer addConstraint:[NSLayoutConstraint
constraintWithItem:_localSettingsButton
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:_settingsButtonContainer
attribute:NSLayoutAttributeLeft
multiplier:1
constant:0.0f]];
[_settingsButtonContainer addConstraint:[NSLayoutConstraint
constraintWithItem:_localSettingsButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:_settingsButtonContainer
attribute:NSLayoutAttributeWidth
multiplier:0.48
constant:0.0f]];
[_settingsButtonContainer addConstraint:[NSLayoutConstraint
constraintWithItem:_remoteSettingsButton
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:_settingsButtonContainer
attribute:NSLayoutAttributeRight
multiplier:1
constant:0.0f]];
[_settingsButtonContainer addConstraint:[NSLayoutConstraint
constraintWithItem:_remoteSettingsButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:_settingsButtonContainer
attribute:NSLayoutAttributeWidth
multiplier:0.48
constant:0.0f]];
setConstraint(_settingsButtonContainer, @"V:|[localSettings]|", 0);
setConstraint(_settingsButtonContainer, @"V:|[remoteSettings]|", 0);
setConstraint(_settingsButtonContainer, @"V:[settings(height)]", 0);
setConstraint(_contentView, @"H:|[settings]|", 0);
setConstraint(_urlInput, @"V:[urlInput(height)]", 0);
setConstraint(_contentView, @"H:|[urlInput]|", 0);
setConstraint(_contentView, @"H:|[videoView]|", 0);
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_videoView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0]];
setConstraint(_contentView, @"V:|[videoView]-[settings]-vSpacing-[urlInput]-vSpacing-[start]-vSpacing-|", 0);
setConstraint(_scrollView, @"V:|[content]|", 0);
setConstraint(self.view, @"H:|[scroll]|", 0);
setConstraint(self.view, @"H:|-hSpacing-[content]-hSpacing-|", 0);
setConstraint(self.view, @"V:|-vSpacing-[scroll]|", 0);
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:-20]];
setConstraint(self.view, @"V:|-vSpacing-[localControl]|", 0);
setConstraint(self.view, @"V:|-vSpacing-[remoteControl]|", 0);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[[WCSKeyboardTracker sharedInstance] update:textField];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[[WCSKeyboardTracker sharedInstance] update];
}
- (NSString *)getStreamName {
NSArray *split = [_urlInput.text componentsSeparatedByString:@"/"];
if (split.count > 3 && ((NSString *)(split[3])).length > 0) {
return split[3];
}
return [[[NSUUID UUID] UUIDString] substringToIndex:8];
}
@end