Update ice server provider response format in iOS AppRTCMobile

BUG=webrtc:5963

Review-Url: https://codereview.webrtc.org/2627523004
Cr-Commit-Position: refs/heads/master@{#16050}
This commit is contained in:
kthelgason 2017-01-13 05:59:46 -08:00 committed by Commit bot
parent a40672a120
commit cc882afdf7
7 changed files with 99 additions and 118 deletions

View File

@ -192,8 +192,6 @@ if (is_ios || (is_mac && target_cpu != "x86")) {
"objc/AppRTCMobile/ARDAppEngineClient.m",
"objc/AppRTCMobile/ARDBitrateTracker.h",
"objc/AppRTCMobile/ARDBitrateTracker.m",
"objc/AppRTCMobile/ARDCEODTURNClient.h",
"objc/AppRTCMobile/ARDCEODTURNClient.m",
"objc/AppRTCMobile/ARDJoinResponse+Internal.h",
"objc/AppRTCMobile/ARDJoinResponse.h",
"objc/AppRTCMobile/ARDJoinResponse.m",
@ -208,7 +206,9 @@ if (is_ios || (is_mac && target_cpu != "x86")) {
"objc/AppRTCMobile/ARDSignalingMessage.m",
"objc/AppRTCMobile/ARDStatsBuilder.h",
"objc/AppRTCMobile/ARDStatsBuilder.m",
"objc/AppRTCMobile/ARDTURNClient+Internal.h",
"objc/AppRTCMobile/ARDTURNClient.h",
"objc/AppRTCMobile/ARDTURNClient.m",
"objc/AppRTCMobile/ARDWebSocketChannel.h",
"objc/AppRTCMobile/ARDWebSocketChannel.m",
"objc/AppRTCMobile/RTCIceCandidate+JSON.h",

View File

@ -23,7 +23,7 @@
#import "WebRTC/RTCTracing.h"
#import "ARDAppEngineClient.h"
#import "ARDCEODTURNClient.h"
#import "ARDTURNClient+Internal.h"
#import "ARDJoinResponse.h"
#import "ARDMessageResponse.h"
#import "ARDSDPUtils.h"
@ -33,12 +33,7 @@
#import "RTCIceCandidate+JSON.h"
#import "RTCSessionDescription+JSON.h"
static NSString * const kARDDefaultSTUNServerUrl =
@"stun:stun.l.google.com:19302";
// TODO(tkchin): figure out a better username for CEOD statistics.
static NSString * const kARDTurnRequestUrl =
@"https://computeengineondemand.appspot.com"
@"/turn?username=iapprtc&key=4080218913";
static NSString * const kARDIceServerRequestUrl = @"https://appr.tc/params";
static NSString * const kARDAppClientErrorDomain = @"ARDAppClient";
static NSInteger const kARDAppClientErrorUnknown = -1;
@ -133,21 +128,15 @@ static int const kKbpsMultiplier = 1000;
@synthesize shouldUseLevelControl = _shouldUseLevelControl;
- (instancetype)init {
if (self = [super init]) {
_roomServerClient = [[ARDAppEngineClient alloc] init];
NSURL *turnRequestURL = [NSURL URLWithString:kARDTurnRequestUrl];
_turnClient = [[ARDCEODTURNClient alloc] initWithURL:turnRequestURL];
[self configure];
}
return self;
return [self initWithDelegate:nil];
}
- (instancetype)initWithDelegate:(id<ARDAppClientDelegate>)delegate {
if (self = [super init]) {
_roomServerClient = [[ARDAppEngineClient alloc] init];
_delegate = delegate;
NSURL *turnRequestURL = [NSURL URLWithString:kARDTurnRequestUrl];
_turnClient = [[ARDCEODTURNClient alloc] initWithURL:turnRequestURL];
NSURL *turnRequestURL = [NSURL URLWithString:kARDIceServerRequestUrl];
_turnClient = [[ARDTURNClient alloc] initWithURL:turnRequestURL];
[self configure];
}
return self;
@ -176,7 +165,7 @@ static int const kKbpsMultiplier = 1000;
- (void)configure {
_factory = [[RTCPeerConnectionFactory alloc] init];
_messageQueue = [NSMutableArray array];
_iceServers = [NSMutableArray arrayWithObject:[self defaultSTUNServer]];
_iceServers = [NSMutableArray array];
_fileLogger = [[RTCFileLogger alloc] init];
[_fileLogger start];
}
@ -814,12 +803,6 @@ static int const kKbpsMultiplier = 1000;
return constraints;
}
- (RTCIceServer *)defaultSTUNServer {
return [[RTCIceServer alloc] initWithURLStrings:@[kARDDefaultSTUNServerUrl]
username:@""
credential:@""];
}
#pragma mark - Errors
+ (NSError *)errorForJoinResultType:(ARDJoinResultType)resultType {

View File

@ -1,66 +0,0 @@
/*
* Copyright 2014 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#import "ARDCEODTURNClient.h"
#import "ARDUtilities.h"
#import "RTCIceServer+JSON.h"
// TODO(tkchin): move this to a configuration object.
static NSString *kTURNOriginURLString = @"https://apprtc.appspot.com";
static NSString *kARDCEODTURNClientErrorDomain = @"ARDCEODTURNClient";
static NSInteger kARDCEODTURNClientErrorBadResponse = -1;
@implementation ARDCEODTURNClient {
NSURL *_url;
}
- (instancetype)initWithURL:(NSURL *)url {
NSParameterAssert([url absoluteString].length);
if (self = [super init]) {
_url = url;
}
return self;
}
- (void)requestServersWithCompletionHandler:
(void (^)(NSArray *turnServers,
NSError *error))completionHandler {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:_url];
// We need to set origin because TURN provider whitelists requests based on
// origin.
[request addValue:@"Mozilla/5.0" forHTTPHeaderField:@"user-agent"];
[request addValue:kTURNOriginURLString forHTTPHeaderField:@"origin"];
[NSURLConnection sendAsyncRequest:request
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
NSArray *turnServers = [NSArray array];
if (error) {
completionHandler(nil, error);
return;
}
NSDictionary *dict = [NSDictionary dictionaryWithJSONData:data];
turnServers = @[ [RTCIceServer serverFromCEODJSONDictionary:dict] ];
if (!turnServers) {
NSError *responseError =
[[NSError alloc] initWithDomain:kARDCEODTURNClientErrorDomain
code:kARDCEODTURNClientErrorBadResponse
userInfo:@{
NSLocalizedDescriptionKey: @"Bad TURN response.",
}];
completionHandler(turnServers, responseError);
return;
}
completionHandler(turnServers, nil);
}];
}
@end

View File

@ -10,8 +10,7 @@
#import "ARDTURNClient.h"
// Requests TURN server urls from compute engine on demand.
@interface ARDCEODTURNClient : NSObject <ARDTURNClient>
@interface ARDTURNClient : NSObject <ARDTURNClient>
- (instancetype)initWithURL:(NSURL *)url;

View File

@ -0,0 +1,86 @@
/*
* Copyright 2014 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#import "ARDTURNClient+Internal.h"
#import "ARDUtilities.h"
#import "RTCIceServer+JSON.h"
// TODO(tkchin): move this to a configuration object.
static NSString *kTURNRefererURLString = @"https://appr.tc";
static NSString *kARDTURNClientErrorDomain = @"ARDTURNClient";
static NSInteger kARDTURNClientErrorBadResponse = -1;
@implementation ARDTURNClient {
NSURL *_url;
}
- (instancetype)initWithURL:(NSURL *)url {
NSParameterAssert([url absoluteString].length);
if (self = [super init]) {
_url = url;
}
return self;
}
- (void)requestServersWithCompletionHandler:
(void (^)(NSArray *turnServers, NSError *error))completionHandler {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:_url];
[NSURLConnection sendAsyncRequest:request
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
completionHandler(nil, error);
return;
}
NSDictionary *responseDict = [NSDictionary dictionaryWithJSONData:data];
NSString *iceServerUrl = responseDict[@"ice_server_url"];
[self makeTurnServerRequestToURL:[NSURL URLWithString:iceServerUrl]
WithCompletionHandler:completionHandler];
}];
}
#pragma mark - Private
- (void)makeTurnServerRequestToURL:(NSURL *)url
WithCompletionHandler:(void (^)(NSArray *turnServers,
NSError *error))completionHandler {
NSMutableURLRequest *iceServerRequest = [NSMutableURLRequest requestWithURL:url];
iceServerRequest.HTTPMethod = @"POST";
[iceServerRequest addValue:kTURNRefererURLString forHTTPHeaderField:@"referer"];
[NSURLConnection sendAsyncRequest:iceServerRequest
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if (error) {
completionHandler(nil, error);
return;
}
NSDictionary *turnResponseDict = [NSDictionary dictionaryWithJSONData:data];
NSMutableArray *turnServers = [NSMutableArray array];
[turnResponseDict[@"iceServers"] enumerateObjectsUsingBlock:
^(NSDictionary *obj, NSUInteger idx, BOOL *stop){
[turnServers addObject:[RTCIceServer serverFromJSONDictionary:obj]];
}];
if (!turnServers) {
NSError *responseError =
[[NSError alloc] initWithDomain:kARDTURNClientErrorDomain
code:kARDTURNClientErrorBadResponse
userInfo:@{
NSLocalizedDescriptionKey: @"Bad TURN response.",
}];
completionHandler(nil, responseError);
return;
}
completionHandler(turnServers, nil);
}];
}
@end

View File

@ -13,7 +13,5 @@
@interface RTCIceServer (JSON)
+ (RTCIceServer *)serverFromJSONDictionary:(NSDictionary *)dictionary;
// CEOD provides different JSON, and this parses that.
+ (RTCIceServer *)serverFromCEODJSONDictionary:(NSDictionary *)dictionary;
@end

View File

@ -10,34 +10,15 @@
#import "RTCIceServer+JSON.h"
static NSString const *kRTCICEServerUsernameKey = @"username";
static NSString const *kRTCICEServerPasswordKey = @"password";
static NSString const *kRTCICEServerUrisKey = @"uris";
static NSString const *kRTCICEServerUrlKey = @"urls";
static NSString const *kRTCICEServerCredentialKey = @"credential";
@implementation RTCIceServer (JSON)
+ (RTCIceServer *)serverFromJSONDictionary:(NSDictionary *)dictionary {
NSString *url = dictionary[kRTCICEServerUrlKey];
NSString *username = dictionary[kRTCICEServerUsernameKey];
NSString *credential = dictionary[kRTCICEServerCredentialKey];
username = username ? username : @"";
credential = credential ? credential : @"";
return [[RTCIceServer alloc] initWithURLStrings:@[url]
NSArray *turnUrls = dictionary[@"urls"];
NSString *username = dictionary[@"username"] ?: @"";
NSString *credential = dictionary[@"credential"] ?: @"";
return [[RTCIceServer alloc] initWithURLStrings:turnUrls
username:username
credential:credential];
}
+ (RTCIceServer *)serverFromCEODJSONDictionary:(NSDictionary *)dictionary {
NSString *username = dictionary[kRTCICEServerUsernameKey];
NSString *password = dictionary[kRTCICEServerPasswordKey];
NSArray *uris = dictionary[kRTCICEServerUrisKey];
RTCIceServer *server =
[[RTCIceServer alloc] initWithURLStrings:uris
username:username
credential:password];
return server;
}
@end