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}
diff --git a/webrtc/examples/BUILD.gn b/webrtc/examples/BUILD.gn
index ca4f706..cf0c195 100644
--- a/webrtc/examples/BUILD.gn
+++ b/webrtc/examples/BUILD.gn
@@ -192,8 +192,6 @@
       "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 @@
       "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",
diff --git a/webrtc/examples/objc/AppRTCMobile/ARDAppClient.m b/webrtc/examples/objc/AppRTCMobile/ARDAppClient.m
index 40a2be7..ef21ab6 100644
--- a/webrtc/examples/objc/AppRTCMobile/ARDAppClient.m
+++ b/webrtc/examples/objc/AppRTCMobile/ARDAppClient.m
@@ -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 @@
 @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 @@
 - (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 @@
   return constraints;
 }
 
-- (RTCIceServer *)defaultSTUNServer {
-  return [[RTCIceServer alloc] initWithURLStrings:@[kARDDefaultSTUNServerUrl]
-                                         username:@""
-                                       credential:@""];
-}
-
 #pragma mark - Errors
 
 + (NSError *)errorForJoinResultType:(ARDJoinResultType)resultType {
diff --git a/webrtc/examples/objc/AppRTCMobile/ARDCEODTURNClient.m b/webrtc/examples/objc/AppRTCMobile/ARDCEODTURNClient.m
deleted file mode 100644
index 5be3335..0000000
--- a/webrtc/examples/objc/AppRTCMobile/ARDCEODTURNClient.m
+++ /dev/null
@@ -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
diff --git a/webrtc/examples/objc/AppRTCMobile/ARDCEODTURNClient.h b/webrtc/examples/objc/AppRTCMobile/ARDTURNClient+Internal.h
similarity index 80%
rename from webrtc/examples/objc/AppRTCMobile/ARDCEODTURNClient.h
rename to webrtc/examples/objc/AppRTCMobile/ARDTURNClient+Internal.h
index 9b136aa..3a579f8 100644
--- a/webrtc/examples/objc/AppRTCMobile/ARDCEODTURNClient.h
+++ b/webrtc/examples/objc/AppRTCMobile/ARDTURNClient+Internal.h
@@ -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;
 
diff --git a/webrtc/examples/objc/AppRTCMobile/ARDTURNClient.m b/webrtc/examples/objc/AppRTCMobile/ARDTURNClient.m
new file mode 100644
index 0000000..05f5cd9
--- /dev/null
+++ b/webrtc/examples/objc/AppRTCMobile/ARDTURNClient.m
@@ -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
diff --git a/webrtc/examples/objc/AppRTCMobile/RTCIceServer+JSON.h b/webrtc/examples/objc/AppRTCMobile/RTCIceServer+JSON.h
index 69fb432..dbc702d 100644
--- a/webrtc/examples/objc/AppRTCMobile/RTCIceServer+JSON.h
+++ b/webrtc/examples/objc/AppRTCMobile/RTCIceServer+JSON.h
@@ -13,7 +13,5 @@
 @interface RTCIceServer (JSON)
 
 + (RTCIceServer *)serverFromJSONDictionary:(NSDictionary *)dictionary;
-// CEOD provides different JSON, and this parses that.
-+ (RTCIceServer *)serverFromCEODJSONDictionary:(NSDictionary *)dictionary;
 
 @end
diff --git a/webrtc/examples/objc/AppRTCMobile/RTCIceServer+JSON.m b/webrtc/examples/objc/AppRTCMobile/RTCIceServer+JSON.m
index 0bc31bb..912b521 100644
--- a/webrtc/examples/objc/AppRTCMobile/RTCIceServer+JSON.m
+++ b/webrtc/examples/objc/AppRTCMobile/RTCIceServer+JSON.m
@@ -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