2013-07-10 02:45:36 +02:00
|
|
|
/*
|
|
|
|
* libjingle
|
|
|
|
* Copyright 2013, Google Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2014-05-31 00:26:06 +02:00
|
|
|
#if !defined(__has_feature) || !__has_feature(objc_arc)
|
|
|
|
#error "This file requires ARC support."
|
|
|
|
#endif
|
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
#import "APPRTCAppClient.h"
|
|
|
|
|
|
|
|
#import <dispatch/dispatch.h>
|
|
|
|
|
|
|
|
#import "GAEChannelClient.h"
|
2013-08-01 20:29:45 +02:00
|
|
|
#import "RTCICEServer.h"
|
2014-03-10 21:41:22 +01:00
|
|
|
#import "RTCMediaConstraints.h"
|
2014-05-31 00:26:06 +02:00
|
|
|
#import "RTCPair.h"
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-05-31 00:26:06 +02:00
|
|
|
@implementation APPRTCAppClient {
|
|
|
|
dispatch_queue_t _backgroundQueue;
|
2014-06-07 00:29:10 +02:00
|
|
|
GAEChannelClient* _gaeChannel;
|
|
|
|
NSURL* _postMessageURL;
|
|
|
|
BOOL _verboseLogging;
|
|
|
|
__weak id<GAEMessageHandler> _messageHandler;
|
2014-05-31 00:26:06 +02:00
|
|
|
}
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-06-07 00:29:10 +02:00
|
|
|
- (instancetype)initWithDelegate:(id<APPRTCAppClientDelegate>)delegate
|
|
|
|
messageHandler:(id<GAEMessageHandler>)handler {
|
2013-07-10 02:45:36 +02:00
|
|
|
if (self = [super init]) {
|
2014-05-31 00:26:06 +02:00
|
|
|
_delegate = delegate;
|
2014-03-25 01:11:56 +01:00
|
|
|
_messageHandler = handler;
|
2014-04-30 02:17:47 +02:00
|
|
|
_backgroundQueue = dispatch_queue_create("RTCBackgroundQueue",
|
|
|
|
DISPATCH_QUEUE_SERIAL);
|
2013-07-10 02:45:36 +02:00
|
|
|
// Uncomment to see Request/Response logging.
|
2013-08-01 20:29:45 +02:00
|
|
|
// _verboseLogging = YES;
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2014-03-25 01:11:56 +01:00
|
|
|
- (void)connectToRoom:(NSURL*)url {
|
2014-06-07 00:29:10 +02:00
|
|
|
NSString* urlString =
|
|
|
|
[[url absoluteString] stringByAppendingString:@"&t=json"];
|
|
|
|
NSURL* requestURL = [NSURL URLWithString:urlString];
|
|
|
|
NSURLRequest* request = [NSURLRequest requestWithURL:requestURL];
|
|
|
|
[self sendURLRequest:request
|
|
|
|
completionHandler:^(NSError* error,
|
|
|
|
NSHTTPURLResponse* httpResponse,
|
|
|
|
NSData* responseData) {
|
|
|
|
int statusCode = [httpResponse statusCode];
|
|
|
|
[self logVerbose:[NSString stringWithFormat:
|
|
|
|
@"Response received\nURL\n%@\nStatus [%d]\nHeaders\n%@",
|
|
|
|
[httpResponse URL],
|
|
|
|
statusCode,
|
|
|
|
[httpResponse allHeaderFields]]];
|
|
|
|
NSAssert(statusCode == 200,
|
|
|
|
@"Invalid response of %d received while connecting to: %@",
|
|
|
|
statusCode,
|
|
|
|
urlString);
|
|
|
|
if (statusCode != 200) {
|
2014-04-30 02:17:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-06-07 00:29:10 +02:00
|
|
|
[self handleResponseData:responseData
|
|
|
|
forRoomRequest:request];
|
|
|
|
}];
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-06-07 00:29:10 +02:00
|
|
|
- (void)sendData:(NSData*)data {
|
|
|
|
NSParameterAssert([data length] > 0);
|
|
|
|
NSString* message = [NSString stringWithUTF8String:[data bytes]];
|
|
|
|
[self logVerbose:[NSString stringWithFormat:@"Send message:\n%@", message]];
|
|
|
|
if (!_postMessageURL) {
|
|
|
|
return;
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
2014-03-25 01:11:56 +01:00
|
|
|
NSMutableURLRequest* request =
|
2014-06-07 00:29:10 +02:00
|
|
|
[NSMutableURLRequest requestWithURL:_postMessageURL];
|
2013-07-10 02:45:36 +02:00
|
|
|
request.HTTPMethod = @"POST";
|
|
|
|
[request setHTTPBody:data];
|
2014-06-07 00:29:10 +02:00
|
|
|
[self sendURLRequest:request
|
|
|
|
completionHandler:^(NSError* error,
|
|
|
|
NSHTTPURLResponse* httpResponse,
|
|
|
|
NSData* responseData) {
|
|
|
|
int status = [httpResponse statusCode];
|
|
|
|
NSString* response = [responseData length] > 0 ?
|
|
|
|
[NSString stringWithUTF8String:[responseData bytes]] :
|
|
|
|
nil;
|
|
|
|
NSAssert(status == 200,
|
|
|
|
@"Bad response [%d] to message: %@\n\n%@",
|
|
|
|
status,
|
|
|
|
message,
|
|
|
|
response);
|
|
|
|
}];
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-06-07 00:29:10 +02:00
|
|
|
#pragma mark - Private
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-06-07 00:29:10 +02:00
|
|
|
- (void)logVerbose:(NSString*)message {
|
|
|
|
if (_verboseLogging) {
|
|
|
|
NSLog(@"%@", message);
|
|
|
|
}
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-06-07 00:29:10 +02:00
|
|
|
- (void)handleResponseData:(NSData*)responseData
|
|
|
|
forRoomRequest:(NSURLRequest*)request {
|
|
|
|
NSDictionary* roomJSON = [self parseJSONData:responseData];
|
|
|
|
[self logVerbose:[NSString stringWithFormat:@"Room JSON:\n%@", roomJSON]];
|
|
|
|
NSParameterAssert(roomJSON);
|
|
|
|
if (roomJSON[@"error"]) {
|
|
|
|
NSArray* errorMessages = roomJSON[@"error_messages"];
|
|
|
|
NSMutableString* message = [NSMutableString string];
|
|
|
|
for (NSString* errorMessage in errorMessages) {
|
|
|
|
[message appendFormat:@"%@\n", errorMessage];
|
|
|
|
}
|
2014-05-31 00:26:06 +02:00
|
|
|
[self.delegate appClient:self didErrorWithMessage:message];
|
2013-07-10 02:45:36 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-06-07 00:29:10 +02:00
|
|
|
NSString* pcConfig = roomJSON[@"pc_config"];
|
|
|
|
NSData* pcConfigData = [pcConfig dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSDictionary* pcConfigJSON = [self parseJSONData:pcConfigData];
|
|
|
|
[self logVerbose:[NSString stringWithFormat:@"PCConfig JSON:\n%@",
|
|
|
|
pcConfigJSON]];
|
|
|
|
NSParameterAssert(pcConfigJSON);
|
|
|
|
|
|
|
|
NSArray* iceServers = [self parseICEServersForPCConfigJSON:pcConfigJSON];
|
|
|
|
[self requestTURNServerForICEServers:iceServers
|
|
|
|
turnServerUrl:roomJSON[@"turn_url"]];
|
|
|
|
|
|
|
|
_initiator = [roomJSON[@"initiator"] boolValue];
|
|
|
|
[self logVerbose:[NSString stringWithFormat:@"Initiator: %d", _initiator]];
|
|
|
|
_postMessageURL = [self parsePostMessageURLForRoomJSON:roomJSON
|
|
|
|
request:request];
|
|
|
|
[self logVerbose:[NSString stringWithFormat:@"POST message URL:\n%@",
|
|
|
|
_postMessageURL]];
|
|
|
|
_videoConstraints = [self parseVideoConstraintsForRoomJSON:roomJSON];
|
|
|
|
[self logVerbose:[NSString stringWithFormat:@"Media constraints:\n%@",
|
|
|
|
_videoConstraints]];
|
|
|
|
NSString* token = roomJSON[@"token"];
|
|
|
|
[self logVerbose:
|
|
|
|
[NSString stringWithFormat:@"About to open GAE with token: %@",
|
|
|
|
token]];
|
|
|
|
_gaeChannel =
|
|
|
|
[[GAEChannelClient alloc] initWithToken:token
|
|
|
|
delegate:_messageHandler];
|
|
|
|
}
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-06-07 00:29:10 +02:00
|
|
|
- (NSDictionary*)parseJSONData:(NSData*)data {
|
|
|
|
NSError* error = nil;
|
2014-03-25 01:11:56 +01:00
|
|
|
NSDictionary* json =
|
2014-06-07 00:29:10 +02:00
|
|
|
[NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
2013-07-10 02:45:36 +02:00
|
|
|
NSAssert(!error, @"Unable to parse. %@", error.localizedDescription);
|
2014-06-07 00:29:10 +02:00
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray*)parseICEServersForPCConfigJSON:(NSDictionary*)pcConfigJSON {
|
|
|
|
NSMutableArray* result = [NSMutableArray array];
|
|
|
|
NSArray* iceServers = pcConfigJSON[@"iceServers"];
|
|
|
|
for (NSDictionary* iceServer in iceServers) {
|
|
|
|
NSString* url = iceServer[@"urls"];
|
|
|
|
NSString* username = pcConfigJSON[@"username"];
|
|
|
|
NSString* credential = iceServer[@"credential"];
|
|
|
|
username = username ? username : @"";
|
|
|
|
credential = credential ? credential : @"";
|
|
|
|
[self logVerbose:[NSString stringWithFormat:@"url [%@] - credential [%@]",
|
|
|
|
url,
|
|
|
|
credential]];
|
|
|
|
RTCICEServer* server =
|
2013-08-01 20:29:45 +02:00
|
|
|
[[RTCICEServer alloc] initWithURI:[NSURL URLWithString:url]
|
2013-09-05 23:49:58 +02:00
|
|
|
username:username
|
2013-07-10 02:45:36 +02:00
|
|
|
password:credential];
|
2014-06-07 00:29:10 +02:00
|
|
|
[result addObject:server];
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
2014-06-07 00:29:10 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSURL*)parsePostMessageURLForRoomJSON:(NSDictionary*)roomJSON
|
|
|
|
request:(NSURLRequest*)request {
|
|
|
|
NSString* requestUrl = [[request URL] absoluteString];
|
|
|
|
NSRange queryRange = [requestUrl rangeOfString:@"?"];
|
|
|
|
NSString* baseUrl = [requestUrl substringToIndex:queryRange.location];
|
|
|
|
NSString* roomKey = roomJSON[@"room_key"];
|
|
|
|
NSParameterAssert([roomKey length] > 0);
|
|
|
|
NSString* me = roomJSON[@"me"];
|
|
|
|
NSParameterAssert([me length] > 0);
|
|
|
|
NSString* postMessageUrl =
|
|
|
|
[NSString stringWithFormat:@"%@/message?r=%@&u=%@", baseUrl, roomKey, me];
|
|
|
|
return [NSURL URLWithString:postMessageUrl];
|
|
|
|
}
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-06-07 00:29:10 +02:00
|
|
|
- (RTCMediaConstraints*)parseVideoConstraintsForRoomJSON:
|
|
|
|
(NSDictionary*)roomJSON {
|
|
|
|
NSString* mediaConstraints = roomJSON[@"media_constraints"];
|
|
|
|
RTCMediaConstraints* constraints = nil;
|
|
|
|
if ([mediaConstraints length] > 0) {
|
|
|
|
NSData* constraintsData =
|
|
|
|
[mediaConstraints dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSDictionary* constraintsJSON = [self parseJSONData:constraintsData];
|
|
|
|
id video = constraintsJSON[@"video"];
|
2014-05-31 00:26:06 +02:00
|
|
|
if ([video isKindOfClass:[NSDictionary class]]) {
|
|
|
|
NSDictionary* mandatory = video[@"mandatory"];
|
|
|
|
NSMutableArray* mandatoryContraints =
|
|
|
|
[NSMutableArray arrayWithCapacity:[mandatory count]];
|
|
|
|
[mandatory enumerateKeysAndObjectsUsingBlock:^(
|
|
|
|
id key, id obj, BOOL* stop) {
|
|
|
|
[mandatoryContraints addObject:[[RTCPair alloc] initWithKey:key
|
|
|
|
value:obj]];
|
|
|
|
}];
|
|
|
|
// TODO(tkchin): figure out json formats for optional constraints.
|
2014-06-07 00:29:10 +02:00
|
|
|
constraints =
|
2014-05-31 00:26:06 +02:00
|
|
|
[[RTCMediaConstraints alloc]
|
|
|
|
initWithMandatoryConstraints:mandatoryContraints
|
|
|
|
optionalConstraints:nil];
|
|
|
|
} else if ([video isKindOfClass:[NSNumber class]] && [video boolValue]) {
|
2014-06-07 00:29:10 +02:00
|
|
|
constraints = [[RTCMediaConstraints alloc] init];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return constraints;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)requestTURNServerWithUrl:(NSString*)turnServerUrl
|
|
|
|
completionHandler:
|
|
|
|
(void (^)(RTCICEServer* turnServer))completionHandler {
|
|
|
|
NSURL* turnServerURL = [NSURL URLWithString:turnServerUrl];
|
|
|
|
NSMutableURLRequest* request =
|
|
|
|
[NSMutableURLRequest requestWithURL:turnServerURL];
|
|
|
|
[request addValue:@"Mozilla/5.0" forHTTPHeaderField:@"user-agent"];
|
|
|
|
[request addValue:@"https://apprtc.appspot.com"
|
|
|
|
forHTTPHeaderField:@"origin"];
|
|
|
|
[self sendURLRequest:request
|
|
|
|
completionHandler:^(NSError* error,
|
|
|
|
NSHTTPURLResponse* response,
|
|
|
|
NSData* responseData) {
|
|
|
|
if (error) {
|
|
|
|
NSLog(@"Unable to get TURN server.");
|
|
|
|
completionHandler(nil);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSDictionary* json = [self parseJSONData:responseData];
|
|
|
|
NSString* username = json[@"username"];
|
|
|
|
NSString* password = json[@"password"];
|
|
|
|
NSArray* uris = json[@"uris"];
|
|
|
|
NSParameterAssert([uris count] > 0);
|
|
|
|
RTCICEServer* turnServer =
|
|
|
|
[[RTCICEServer alloc] initWithURI:[NSURL URLWithString:uris[0]]
|
|
|
|
username:username
|
|
|
|
password:password];
|
|
|
|
completionHandler(turnServer);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)requestTURNServerForICEServers:(NSArray*)iceServers
|
|
|
|
turnServerUrl:(NSString*)turnServerUrl {
|
|
|
|
BOOL isTurnPresent = NO;
|
|
|
|
for (RTCICEServer* iceServer in iceServers) {
|
|
|
|
if ([[iceServer.URI scheme] isEqualToString:@"turn"]) {
|
|
|
|
isTurnPresent = YES;
|
|
|
|
break;
|
2014-03-10 21:41:22 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-07 00:29:10 +02:00
|
|
|
if (!isTurnPresent) {
|
|
|
|
[self requestTURNServerWithUrl:turnServerUrl
|
|
|
|
completionHandler:^(RTCICEServer* turnServer) {
|
|
|
|
NSArray* servers = iceServers;
|
|
|
|
if (turnServer) {
|
|
|
|
servers = [servers arrayByAddingObject:turnServer];
|
|
|
|
}
|
|
|
|
NSLog(@"ICE servers:\n%@", servers);
|
|
|
|
[self.delegate appClient:self didReceiveICEServers:servers];
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
NSLog(@"ICE servers:\n%@", iceServers);
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[self.delegate appClient:self didReceiveICEServers:iceServers];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-03-10 21:41:22 +01:00
|
|
|
|
2014-06-07 00:29:10 +02:00
|
|
|
- (void)sendURLRequest:(NSURLRequest*)request
|
|
|
|
completionHandler:(void (^)(NSError* error,
|
|
|
|
NSHTTPURLResponse* httpResponse,
|
|
|
|
NSData* responseData))completionHandler {
|
|
|
|
dispatch_async(_backgroundQueue, ^{
|
|
|
|
NSError* error = nil;
|
|
|
|
NSURLResponse* response = nil;
|
|
|
|
NSData* responseData = [NSURLConnection sendSynchronousRequest:request
|
|
|
|
returningResponse:&response
|
|
|
|
error:&error];
|
|
|
|
NSParameterAssert(!response ||
|
|
|
|
[response isKindOfClass:[NSHTTPURLResponse class]]);
|
|
|
|
if (error) {
|
|
|
|
NSLog(@"Failed URL request for:%@\nError:%@", request, error);
|
|
|
|
}
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
|
|
|
|
completionHandler(error, httpResponse, responseData);
|
|
|
|
});
|
|
|
|
});
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|