-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.m
184 lines (144 loc) · 5.26 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
//
// ViewController.m
// CineIOPeerExampleApp
//
// Created by Thomas Shafer on 3/2/15.
// Copyright (c) 2015 cine.io. All rights reserved.
//
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "CinePeerClient.h"
#import "CinePeerClientConfig.h"
#import "CineCall.h"
#import "CineIdentity.h"
#import "RTCMediaStream.h"
#import "RTCEAGLVideoView.h"
#import "MediaStreamAndRenderer.h"
static CGFloat const kLocalViewPadding = 20;
@interface ViewController () <CinePeerClientDelegate>
@property (weak, nonatomic) IBOutlet UIView *videosView;
@property (strong, nonatomic) UIView *videosSubView;
@property (nonatomic, strong) NSMutableArray* videoViews;
@property (nonatomic, strong) CinePeerClient *cinePeerClient;
@end
@implementation ViewController
{
CGSize _localVideoSize;
CGSize _remoteVideoSize;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.videoViews = [[NSMutableArray alloc] init];
[self initializeVideoViews];
// Update with your cine.io api keys
// register for free at https://www.cine.io/
NSString *CINE_IO_PUBLIC_KEY = @"CINE_IO_PUBLIC_KEY";
NSString *CINE_IO_SECRET_KEY = @"CINE_IO_SECRET_KEY";
if ([CINE_IO_PUBLIC_KEY isEqual: @"CINE_IO_PUBLIC_KEY"]){
NSAssert(NO, @"Please set CINE_IO_PUBLIC_KEY to your own public key. You can register for one for free at https://www.cine.io");
}
// Initialize the CinePeerClientConfig class
// We are a delegate of CinePeerClientDelegate
CinePeerClientConfig *config = [[CinePeerClientConfig alloc] initWithPublicKey:CINE_IO_PUBLIC_KEY delegate:self];
// Add the secret key
// This is only necessary for securely identifying
// [config setSecretKey:CINE_IO_SECRET_KEY];
// Create the peer client
self.cinePeerClient = [[CinePeerClient alloc] initWithConfig:config];
// Start up the camera and microphone
[self.cinePeerClient startMediaStream];
// Join a room
NSString *roomName = @"example";
[self.cinePeerClient joinRoom:roomName];
// Uncomment to identify
// NSString *identityName = @"SET-UNIQUE-IDENTITY-HERE";
// CineIdentity *identity = [config generateIdentity:identityName];
// [self.cinePeerClient identify:identity];
//
// Uncomment to call another identity
// NSString *identityToCall = @"SET-UNIQUE-IDENTITY-HERE-TO-CALL";
// [self.cinePeerClient call:identityToCall];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)initializeVideoViews {
self.videosView.hidden = NO;
[self refreshVideoLayout];
}
- (void)refreshVideoLayout {
// TODO: handle rotation.
NSLog(@"Reset video layout");
for(MediaStreamAndRenderer* msr in self.videoViews){
[msr removeVideoRenderer];
}
if (self.videosSubView != nil){
NSLog(@"removing view");
[self.videosSubView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
}
self.videosSubView = [[UIView alloc] initWithFrame:self.videosView.frame];
[self.videosView addSubview:self.videosSubView];
NSUInteger offset = kLocalViewPadding;
for(MediaStreamAndRenderer* msr in self.videoViews){
offset = [self showMediaStream:msr offset:offset];
}
}
- (int)showMediaStream:(MediaStreamAndRenderer *)msr offset:(int)offset
{
NSLog(@"Showing media stream");
RTCEAGLVideoView *renderer = [msr getRenderer];
CGRect videoFrame = renderer.frame;
videoFrame.origin.x = kLocalViewPadding;
videoFrame.origin.y = offset + kLocalViewPadding;
renderer.frame = videoFrame;
[self.videosSubView addSubview:renderer];
offset += videoFrame.size.height + kLocalViewPadding;
return offset;
}
- (MediaStreamAndRenderer *)getMediaStreamAndRendererForPeerConnection:(RTCPeerConnection *)peerConnection
{
MediaStreamAndRenderer *msrToReturn = nil;
for(MediaStreamAndRenderer* msr in self.videoViews){
if ([msr getPeerConnection] == peerConnection)
msrToReturn = msr;
}
return msrToReturn;
}
#pragma mark - CinePeerClientDelegate
- (void) addStream:(RTCMediaStream *)stream peerConnection:(RTCPeerConnection *)peerConnection local:(BOOL)local
{
NSLog(@"Got media stream");
MediaStreamAndRenderer *msr = [[MediaStreamAndRenderer alloc] initWithStream:stream peerConnection:peerConnection bounds:self.videosView.bounds local:local view:self];
[self.videoViews addObject:msr];
[self refreshVideoLayout];
}
- (void)removeStream:(RTCMediaStream *)mediaStream peerConnection:(RTCPeerConnection *)peerConnection local:(BOOL)local;
{
NSLog(@"Remove stream");
MediaStreamAndRenderer *msrToDelete = [self getMediaStreamAndRendererForPeerConnection:peerConnection];
if (msrToDelete != nil){
[msrToDelete cleanup];
[self.videoViews removeObject:msrToDelete];
}
NSLog(@"Did removed");
[self refreshVideoLayout];
}
-(void) handleError:(NSDictionary *)error
{
NSLog(@"ViewController got error: %@", error);
}
- (void) handleCall:(CineCall *)call
{
NSLog(@"ViewController got call");
[call answer];
}
- (void) onCallCancel:(CineCall *)call
{
NSLog(@"ViewController got call cancel");
}
- (void) onCallReject:(CineCall *)call
{
NSLog(@"ViewController got call reject");
}
@end