From 91621f807b6860a0dd7850ad95cf74e7e7db4557 Mon Sep 17 00:00:00 2001 From: "fischman@webrtc.org" Date: Thu, 5 Jun 2014 20:31:41 +0000 Subject: 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/talk@6345 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../src/org/appspot/apprtc/AppRTCClient.java | 67 +++++++--------------- 1 file changed, 20 insertions(+), 47 deletions(-) (limited to 'examples/android/src') diff --git a/examples/android/src/org/appspot/apprtc/AppRTCClient.java b/examples/android/src/org/appspot/apprtc/AppRTCClient.java index 86460bc..9c42e27 100644 --- a/examples/android/src/org/appspot/apprtc/AppRTCClient.java +++ b/examples/android/src/org/appspot/apprtc/AppRTCClient.java @@ -215,6 +215,8 @@ public class AppRTCClient { } try { return getParametersForRoomUrl(urls[0]); + } catch (JSONException e) { + throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } @@ -231,33 +233,26 @@ public class AppRTCClient { iceServersObserver.onIceServers(appRTCSignalingParameters.iceServers); } - // Fetches |url| and fishes the signaling parameters out of the HTML via - // 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). + // Fetches |url| and fishes the signaling parameters out of the JSON. private AppRTCSignalingParameters getParametersForRoomUrl(String url) - throws IOException { - final Pattern fullRoomPattern = Pattern.compile( - ".*\n *Sorry, this room is full\\..*"); - - String roomHtml = - drainStream((new URL(url)).openConnection().getInputStream()); - - Matcher fullRoomMatcher = fullRoomPattern.matcher(roomHtml); - if (fullRoomMatcher.find()) { - throw new IOException("Room is full!"); + throws IOException, JSONException { + url = url + "&t=json"; + JSONObject roomJson = new JSONObject( + drainStream((new URL(url)).openConnection().getInputStream())); + + if (roomJson.has("error")) { + JSONArray errors = roomJson.getJSONArray("error_messages"); + throw new IOException(errors.toString()); } String gaeBaseHref = url.substring(0, url.indexOf('?')); - String token = getVarValue(roomHtml, "channelToken", true); + String token = roomJson.getString("token"); String postMessageUrl = "/message?r=" + - getVarValue(roomHtml, "roomKey", true) + "&u=" + - getVarValue(roomHtml, "me", true); - boolean initiator = getVarValue(roomHtml, "initiator", false).equals("1"); + roomJson.getString("room_key") + "&u=" + + roomJson.getString("me"); + boolean initiator = roomJson.getInt("initiator") == 1; LinkedList iceServers = - iceServersFromPCConfigJSON(getVarValue(roomHtml, "pcConfig", false)); + iceServersFromPCConfigJSON(roomJson.getString("pc_config")); boolean isTurnPresent = false; for (PeerConnection.IceServer server : iceServers) { @@ -267,21 +262,20 @@ public class AppRTCClient { } } if (!isTurnPresent) { - iceServers.add( - requestTurnServer(getVarValue(roomHtml, "turnUrl", true))); + iceServers.add(requestTurnServer(roomJson.getString("turn_url"))); } MediaConstraints pcConstraints = constraintsFromJSON( - getVarValue(roomHtml, "pcConstraints", false)); + roomJson.getString("pc_constraints")); addDTLSConstraintIfMissing(pcConstraints); Log.d(TAG, "pcConstraints: " + pcConstraints); MediaConstraints videoConstraints = constraintsFromJSON( getAVConstraints("video", - getVarValue(roomHtml, "mediaConstraints", false))); + roomJson.getString("media_constraints"))); Log.d(TAG, "videoConstraints: " + videoConstraints); MediaConstraints audioConstraints = constraintsFromJSON( getAVConstraints("audio", - getVarValue(roomHtml, "mediaConstraints", false))); + roomJson.getString("media_constraints"))); Log.d(TAG, "audioConstraints: " + audioConstraints); 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 // off the main thread! private PeerConnection.IceServer requestTurnServer(String url) { -- cgit v1.2.3