AppRTCDemo(android): remove HTML/regex hackery in favor of JSON struct.

BUG=3407
R=glaznev@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/16619006

git-svn-id: http://webrtc.googlecode.com/svn/trunk@6345 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
fischman@webrtc.org 2014-06-05 20:31:41 +00:00
parent 65d61c3924
commit 130fa64d4c

View File

@ -215,6 +215,8 @@ public class AppRTCClient {
} }
try { try {
return getParametersForRoomUrl(urls[0]); return getParametersForRoomUrl(urls[0]);
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -231,33 +233,26 @@ public class AppRTCClient {
iceServersObserver.onIceServers(appRTCSignalingParameters.iceServers); iceServersObserver.onIceServers(appRTCSignalingParameters.iceServers);
} }
// Fetches |url| and fishes the signaling parameters out of the HTML via // Fetches |url| and fishes the signaling parameters out of the JSON.
// regular expressions.
//
// TODO(fischman): replace this hackery with a dedicated JSON-serving URL in
// apprtc so that this isn't necessary (here and in other future apps that
// want to interop with apprtc).
private AppRTCSignalingParameters getParametersForRoomUrl(String url) private AppRTCSignalingParameters getParametersForRoomUrl(String url)
throws IOException { throws IOException, JSONException {
final Pattern fullRoomPattern = Pattern.compile( url = url + "&t=json";
".*\n *Sorry, this room is full\\..*"); JSONObject roomJson = new JSONObject(
drainStream((new URL(url)).openConnection().getInputStream()));
String roomHtml = if (roomJson.has("error")) {
drainStream((new URL(url)).openConnection().getInputStream()); JSONArray errors = roomJson.getJSONArray("error_messages");
throw new IOException(errors.toString());
Matcher fullRoomMatcher = fullRoomPattern.matcher(roomHtml);
if (fullRoomMatcher.find()) {
throw new IOException("Room is full!");
} }
String gaeBaseHref = url.substring(0, url.indexOf('?')); String gaeBaseHref = url.substring(0, url.indexOf('?'));
String token = getVarValue(roomHtml, "channelToken", true); String token = roomJson.getString("token");
String postMessageUrl = "/message?r=" + String postMessageUrl = "/message?r=" +
getVarValue(roomHtml, "roomKey", true) + "&u=" + roomJson.getString("room_key") + "&u=" +
getVarValue(roomHtml, "me", true); roomJson.getString("me");
boolean initiator = getVarValue(roomHtml, "initiator", false).equals("1"); boolean initiator = roomJson.getInt("initiator") == 1;
LinkedList<PeerConnection.IceServer> iceServers = LinkedList<PeerConnection.IceServer> iceServers =
iceServersFromPCConfigJSON(getVarValue(roomHtml, "pcConfig", false)); iceServersFromPCConfigJSON(roomJson.getString("pc_config"));
boolean isTurnPresent = false; boolean isTurnPresent = false;
for (PeerConnection.IceServer server : iceServers) { for (PeerConnection.IceServer server : iceServers) {
@ -267,21 +262,20 @@ public class AppRTCClient {
} }
} }
if (!isTurnPresent) { if (!isTurnPresent) {
iceServers.add( iceServers.add(requestTurnServer(roomJson.getString("turn_url")));
requestTurnServer(getVarValue(roomHtml, "turnUrl", true)));
} }
MediaConstraints pcConstraints = constraintsFromJSON( MediaConstraints pcConstraints = constraintsFromJSON(
getVarValue(roomHtml, "pcConstraints", false)); roomJson.getString("pc_constraints"));
addDTLSConstraintIfMissing(pcConstraints); addDTLSConstraintIfMissing(pcConstraints);
Log.d(TAG, "pcConstraints: " + pcConstraints); Log.d(TAG, "pcConstraints: " + pcConstraints);
MediaConstraints videoConstraints = constraintsFromJSON( MediaConstraints videoConstraints = constraintsFromJSON(
getAVConstraints("video", getAVConstraints("video",
getVarValue(roomHtml, "mediaConstraints", false))); roomJson.getString("media_constraints")));
Log.d(TAG, "videoConstraints: " + videoConstraints); Log.d(TAG, "videoConstraints: " + videoConstraints);
MediaConstraints audioConstraints = constraintsFromJSON( MediaConstraints audioConstraints = constraintsFromJSON(
getAVConstraints("audio", getAVConstraints("audio",
getVarValue(roomHtml, "mediaConstraints", false))); roomJson.getString("media_constraints")));
Log.d(TAG, "audioConstraints: " + audioConstraints); Log.d(TAG, "audioConstraints: " + audioConstraints);
return new AppRTCSignalingParameters( return new AppRTCSignalingParameters(
@ -369,27 +363,6 @@ public class AppRTCClient {
} }
} }
// Scan |roomHtml| for declaration & assignment of |varName| and return its
// value, optionally stripping outside quotes if |stripQuotes| requests it.
private String getVarValue(
String roomHtml, String varName, boolean stripQuotes)
throws IOException {
final Pattern pattern = Pattern.compile(
".*\n *var " + varName + " = ([^\n]*);\n.*");
Matcher matcher = pattern.matcher(roomHtml);
if (!matcher.find()) {
throw new IOException("Missing " + varName + " in HTML: " + roomHtml);
}
String varValue = matcher.group(1);
if (matcher.find()) {
throw new IOException("Too many " + varName + " in HTML: " + roomHtml);
}
if (stripQuotes) {
varValue = varValue.substring(1, varValue.length() - 1);
}
return varValue;
}
// Requests & returns a TURN ICE Server based on a request URL. Must be run // Requests & returns a TURN ICE Server based on a request URL. Must be run
// off the main thread! // off the main thread!
private PeerConnection.IceServer requestTurnServer(String url) { private PeerConnection.IceServer requestTurnServer(String url) {