blob: 5b035e3c33c49ef794894eacaf03cd31df347493 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2013, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#import "APPRTCAppClient.h"
29
30#import <dispatch/dispatch.h>
31
32#import "GAEChannelClient.h"
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000033#import "RTCICEServer.h"
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000034#import "APPRTCAppDelegate.h"
35#import "RTCMediaConstraints.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37@interface APPRTCAppClient ()
38
fischman@webrtc.orgd0f4c212013-08-20 22:16:55 +000039@property(nonatomic, strong) dispatch_queue_t backgroundQueue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040@property(nonatomic, copy) NSString *baseURL;
41@property(nonatomic, strong) GAEChannelClient *gaeChannel;
42@property(nonatomic, copy) NSString *postMessageUrl;
43@property(nonatomic, copy) NSString *pcConfig;
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000044@property(nonatomic, strong) NSMutableString *roomHtml;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045@property(atomic, strong) NSMutableArray *sendQueue;
46@property(nonatomic, copy) NSString *token;
47
48@property(nonatomic, assign) BOOL verboseLogging;
49
50@end
51
52@implementation APPRTCAppClient
53
fischman@webrtc.org9ca93a82013-10-29 00:14:15 +000054@synthesize ICEServerDelegate = _ICEServerDelegate;
55@synthesize messageHandler = _messageHandler;
56
57@synthesize backgroundQueue = _backgroundQueue;
58@synthesize baseURL = _baseURL;
59@synthesize gaeChannel = _gaeChannel;
60@synthesize postMessageUrl = _postMessageUrl;
61@synthesize pcConfig = _pcConfig;
62@synthesize roomHtml = _roomHtml;
63@synthesize sendQueue = _sendQueue;
64@synthesize token = _token;
65@synthesize verboseLogging = _verboseLogging;
fischman@webrtc.org82387e42014-02-10 18:47:11 +000066@synthesize initiator = _initiator;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000067@synthesize videoConstraints = _videoConstraints;
fischman@webrtc.org9ca93a82013-10-29 00:14:15 +000068
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069- (id)init {
70 if (self = [super init]) {
71 _backgroundQueue = dispatch_queue_create("RTCBackgroundQueue", NULL);
72 _sendQueue = [NSMutableArray array];
73 // Uncomment to see Request/Response logging.
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000074 // _verboseLogging = YES;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 }
76 return self;
77}
78
79#pragma mark - Public methods
80
81- (void)connectToRoom:(NSURL *)url {
82 NSURLRequest *request = [self getRequestFromUrl:url];
83 [NSURLConnection connectionWithRequest:request delegate:self];
84}
85
86- (void)sendData:(NSData *)data {
87 @synchronized(self) {
88 [self maybeLogMessage:@"Send message"];
89 [self.sendQueue addObject:[data copy]];
90 }
91 [self requestQueueDrainInBackground];
92}
93
94#pragma mark - Internal methods
95
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000096- (NSString*)findVar:(NSString*)name
97 strippingQuotes:(BOOL)strippingQuotes {
98 NSError* error;
99 NSString* pattern =
100 [NSString stringWithFormat:@".*\n *var %@ = ([^\n]*);\n.*", name];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 NSRegularExpression *regexp =
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000102 [NSRegularExpression regularExpressionWithPattern:pattern
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 options:0
104 error:&error];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000105 NSAssert(!error, @"Unexpected error compiling regex: ",
106 error.localizedDescription);
107
108 NSRange fullRange = NSMakeRange(0, [self.roomHtml length]);
109 NSArray *matches =
110 [regexp matchesInString:self.roomHtml options:0 range:fullRange];
111 if ([matches count] != 1) {
112 [self showMessage:[NSString stringWithFormat:@"%d matches for %@ in %@",
113 [matches count], name, self.roomHtml]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 return nil;
115 }
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000116 NSRange matchRange = [matches[0] rangeAtIndex:1];
117 NSString* value = [self.roomHtml substringWithRange:matchRange];
118 if (strippingQuotes) {
119 NSAssert([value length] > 2,
120 @"Can't strip quotes from short string: [%@]", value);
121 NSAssert(([value characterAtIndex:0] == '\'' &&
122 [value characterAtIndex:[value length] - 1] == '\''),
123 @"Can't strip quotes from unquoted string: [%@]", value);
124 value = [value substringWithRange:NSMakeRange(1, [value length] - 2)];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 }
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000126 return value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127}
128
129- (NSURLRequest *)getRequestFromUrl:(NSURL *)url {
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000130 self.roomHtml = [NSMutableString stringWithCapacity:20000];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 NSString *path =
132 [NSString stringWithFormat:@"https:%@", [url resourceSpecifier]];
133 NSURLRequest *request =
134 [NSURLRequest requestWithURL:[NSURL URLWithString:path]];
135 return request;
136}
137
138- (void)maybeLogMessage:(NSString *)message {
139 if (self.verboseLogging) {
140 NSLog(@"%@", message);
141 }
142}
143
144- (void)requestQueueDrainInBackground {
145 dispatch_async(self.backgroundQueue, ^(void) {
146 // TODO(hughv): This can block the UI thread. Fix.
147 @synchronized(self) {
148 if ([self.postMessageUrl length] < 1) {
149 return;
150 }
151 for (NSData *data in self.sendQueue) {
152 NSString *url = [NSString stringWithFormat:@"%@/%@",
153 self.baseURL,
154 self.postMessageUrl];
155 [self sendData:data withUrl:url];
156 }
157 [self.sendQueue removeAllObjects];
158 }
159 });
160}
161
162- (void)sendData:(NSData *)data withUrl:(NSString *)url {
163 NSMutableURLRequest *request =
164 [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
165 request.HTTPMethod = @"POST";
166 [request setHTTPBody:data];
167 NSURLResponse *response;
168 NSError *error;
169 NSData *responseData = [NSURLConnection sendSynchronousRequest:request
170 returningResponse:&response
171 error:&error];
172 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
173 int status = [httpResponse statusCode];
174 NSAssert(status == 200,
175 @"Bad response [%d] to message: %@\n\n%@",
176 status,
177 [NSString stringWithUTF8String:[data bytes]],
178 [NSString stringWithUTF8String:[responseData bytes]]);
179}
180
181- (void)showMessage:(NSString *)message {
182 NSLog(@"%@", message);
183 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Unable to join"
184 message:message
185 delegate:nil
186 cancelButtonTitle:@"OK"
187 otherButtonTitles:nil];
188 [alertView show];
189}
190
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000191- (void)updateICEServers:(NSMutableArray *)ICEServers
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 withTurnServer:(NSString *)turnServerUrl {
193 if ([turnServerUrl length] < 1) {
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000194 [self.ICEServerDelegate onICEServers:ICEServers];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 return;
196 }
197 dispatch_async(self.backgroundQueue, ^(void) {
198 NSMutableURLRequest *request = [NSMutableURLRequest
199 requestWithURL:[NSURL URLWithString:turnServerUrl]];
200 [request addValue:@"Mozilla/5.0" forHTTPHeaderField:@"user-agent"];
201 [request addValue:@"https://apprtc.appspot.com"
202 forHTTPHeaderField:@"origin"];
203 NSURLResponse *response;
204 NSError *error;
205 NSData *responseData = [NSURLConnection sendSynchronousRequest:request
206 returningResponse:&response
207 error:&error];
208 if (!error) {
209 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData
210 options:0
211 error:&error];
212 NSAssert(!error, @"Unable to parse. %@", error.localizedDescription);
213 NSString *username = json[@"username"];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 NSString *password = json[@"password"];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000215 NSArray* uris = json[@"uris"];
216 for (int i = 0; i < [uris count]; ++i) {
217 NSString *turnServer = [uris objectAtIndex:i];
218 RTCICEServer *ICEServer =
219 [[RTCICEServer alloc] initWithURI:[NSURL URLWithString:turnServer]
220 username:username
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 password:password];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000222 NSLog(@"Added ICE Server: %@", ICEServer);
223 [ICEServers addObject:ICEServer];
224 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 } else {
226 NSLog(@"Unable to get TURN server. Error: %@", error.description);
227 }
228
229 dispatch_async(dispatch_get_main_queue(), ^(void) {
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000230 [self.ICEServerDelegate onICEServers:ICEServers];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 });
232 });
233}
234
235#pragma mark - NSURLConnectionDataDelegate methods
236
237- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
238 NSString *roomHtml = [NSString stringWithUTF8String:[data bytes]];
239 [self maybeLogMessage:
240 [NSString stringWithFormat:@"Received %d chars", [roomHtml length]]];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000241 [self.roomHtml appendString:roomHtml];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242}
243
244- (void)connection:(NSURLConnection *)connection
245 didReceiveResponse:(NSURLResponse *)response {
246 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
247 int statusCode = [httpResponse statusCode];
248 [self maybeLogMessage:
249 [NSString stringWithFormat:
250 @"Response received\nURL\n%@\nStatus [%d]\nHeaders\n%@",
251 [httpResponse URL],
252 statusCode,
253 [httpResponse allHeaderFields]]];
254 NSAssert(statusCode == 200, @"Invalid response of %d received.", statusCode);
255}
256
257- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
258 [self maybeLogMessage:[NSString stringWithFormat:@"finished loading %d chars",
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000259 [self.roomHtml length]]];
260 NSRegularExpression* fullRegex =
261 [NSRegularExpression regularExpressionWithPattern:@"room is full"
262 options:0
263 error:nil];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000264 if ([fullRegex
265 numberOfMatchesInString:self.roomHtml
266 options:0
267 range:NSMakeRange(0, [self.roomHtml length])]) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 [self showMessage:@"Room full"];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000269 APPRTCAppDelegate *ad =
270 (APPRTCAppDelegate *)[[UIApplication sharedApplication] delegate];
271 [ad closeVideoUI];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 return;
273 }
274
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000275
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 NSString *fullUrl = [[[connection originalRequest] URL] absoluteString];
277 NSRange queryRange = [fullUrl rangeOfString:@"?"];
278 self.baseURL = [fullUrl substringToIndex:queryRange.location];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000279 [self maybeLogMessage:
280 [NSString stringWithFormat:@"Base URL: %@", self.baseURL]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281
fischman@webrtc.org82387e42014-02-10 18:47:11 +0000282 self.initiator = [[self findVar:@"initiator" strippingQuotes:NO] boolValue];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000283 self.token = [self findVar:@"channelToken" strippingQuotes:YES];
284 if (!self.token)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 return;
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000286 [self maybeLogMessage:[NSString stringWithFormat:@"Token: %@", self.token]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000288 NSString* roomKey = [self findVar:@"roomKey" strippingQuotes:YES];
289 NSString* me = [self findVar:@"me" strippingQuotes:YES];
290 if (!roomKey || !me)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 self.postMessageUrl =
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000293 [NSString stringWithFormat:@"/message?r=%@&u=%@", roomKey, me];
294 [self maybeLogMessage:[NSString stringWithFormat:@"POST message URL: %@",
295 self.postMessageUrl]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000297 NSString* pcConfig = [self findVar:@"pcConfig" strippingQuotes:NO];
298 if (!pcConfig)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 [self maybeLogMessage:
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000301 [NSString stringWithFormat:@"PC Config JSON: %@", pcConfig]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000303 NSString *turnServerUrl = [self findVar:@"turnUrl" strippingQuotes:YES];
304 if (turnServerUrl) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 [self maybeLogMessage:
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000306 [NSString stringWithFormat:@"TURN server request URL: %@",
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 turnServerUrl]];
308 }
309
310 NSError *error;
311 NSData *pcData = [pcConfig dataUsingEncoding:NSUTF8StringEncoding];
312 NSDictionary *json =
313 [NSJSONSerialization JSONObjectWithData:pcData options:0 error:&error];
314 NSAssert(!error, @"Unable to parse. %@", error.localizedDescription);
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000315 NSArray *servers = [json objectForKey:@"iceServers"];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000316 NSMutableArray *ICEServers = [NSMutableArray array];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 for (NSDictionary *server in servers) {
braveyao@webrtc.orgeaadeca2014-02-26 04:16:02 +0000318 NSString *url = [server objectForKey:@"urls"];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000319 NSString *username = json[@"username"];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 NSString *credential = [server objectForKey:@"credential"];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000321 if (!username) {
322 username = @"";
323 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 if (!credential) {
325 credential = @"";
326 }
327 [self maybeLogMessage:
328 [NSString stringWithFormat:@"url [%@] - credential [%@]",
329 url,
330 credential]];
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000331 RTCICEServer *ICEServer =
332 [[RTCICEServer alloc] initWithURI:[NSURL URLWithString:url]
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000333 username:username
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334 password:credential];
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +0000335 NSLog(@"Added ICE Server: %@", ICEServer);
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000336 [ICEServers addObject:ICEServer];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 }
fischman@webrtc.org1bc19542013-08-01 18:29:45 +0000338 [self updateICEServers:ICEServers withTurnServer:turnServerUrl];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000340 NSString* mc = [self findVar:@"mediaConstraints" strippingQuotes:NO];
341 if (mc) {
342 error = nil;
343 NSData *mcData = [mc dataUsingEncoding:NSUTF8StringEncoding];
344 json =
345 [NSJSONSerialization JSONObjectWithData:mcData options:0 error:&error];
346 NSAssert(!error, @"Unable to parse. %@", error.localizedDescription);
347 if ([[json objectForKey:@"video"] boolValue]) {
348 self.videoConstraints = [[RTCMediaConstraints alloc] init];
349 }
350 }
351
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 [self maybeLogMessage:
353 [NSString stringWithFormat:@"About to open GAE with token: %@",
354 self.token]];
355 self.gaeChannel =
356 [[GAEChannelClient alloc] initWithToken:self.token
357 delegate:self.messageHandler];
358}
359
360@end