Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: webrtc/examples/androidapp/src/org/appspot/apprtc/RoomParametersFetcher.java

Issue 2377003002: Format all Java in WebRTC. (Closed)
Patch Set: Rebase. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 * SignalingParameters are extracted. 51 * SignalingParameters are extracted.
52 */ 52 */
53 void onSignalingParametersReady(final SignalingParameters params); 53 void onSignalingParametersReady(final SignalingParameters params);
54 54
55 /** 55 /**
56 * Callback for room parameters extraction error. 56 * Callback for room parameters extraction error.
57 */ 57 */
58 void onSignalingParametersError(final String description); 58 void onSignalingParametersError(final String description);
59 } 59 }
60 60
61 public RoomParametersFetcher(String roomUrl, String roomMessage, 61 public RoomParametersFetcher(
62 final RoomParametersFetcherEvents events) { 62 String roomUrl, String roomMessage, final RoomParametersFetcherEvents even ts) {
63 this.roomUrl = roomUrl; 63 this.roomUrl = roomUrl;
64 this.roomMessage = roomMessage; 64 this.roomMessage = roomMessage;
65 this.events = events; 65 this.events = events;
66 } 66 }
67 67
68 public void makeRequest() { 68 public void makeRequest() {
69 Log.d(TAG, "Connecting to room: " + roomUrl); 69 Log.d(TAG, "Connecting to room: " + roomUrl);
70 httpConnection = new AsyncHttpURLConnection( 70 httpConnection =
71 "POST", roomUrl, roomMessage, 71 new AsyncHttpURLConnection("POST", roomUrl, roomMessage, new AsyncHttpEv ents() {
72 new AsyncHttpEvents() {
73 @Override 72 @Override
74 public void onHttpError(String errorMessage) { 73 public void onHttpError(String errorMessage) {
75 Log.e(TAG, "Room connection error: " + errorMessage); 74 Log.e(TAG, "Room connection error: " + errorMessage);
76 events.onSignalingParametersError(errorMessage); 75 events.onSignalingParametersError(errorMessage);
77 } 76 }
78 77
79 @Override 78 @Override
80 public void onHttpComplete(String response) { 79 public void onHttpComplete(String response) {
81 roomHttpResponseParse(response); 80 roomHttpResponseParse(response);
82 } 81 }
(...skipping 24 matching lines...) Expand all
107 iceCandidates = new LinkedList<IceCandidate>(); 106 iceCandidates = new LinkedList<IceCandidate>();
108 String messagesString = roomJson.getString("messages"); 107 String messagesString = roomJson.getString("messages");
109 JSONArray messages = new JSONArray(messagesString); 108 JSONArray messages = new JSONArray(messagesString);
110 for (int i = 0; i < messages.length(); ++i) { 109 for (int i = 0; i < messages.length(); ++i) {
111 String messageString = messages.getString(i); 110 String messageString = messages.getString(i);
112 JSONObject message = new JSONObject(messageString); 111 JSONObject message = new JSONObject(messageString);
113 String messageType = message.getString("type"); 112 String messageType = message.getString("type");
114 Log.d(TAG, "GAE->C #" + i + " : " + messageString); 113 Log.d(TAG, "GAE->C #" + i + " : " + messageString);
115 if (messageType.equals("offer")) { 114 if (messageType.equals("offer")) {
116 offerSdp = new SessionDescription( 115 offerSdp = new SessionDescription(
117 SessionDescription.Type.fromCanonicalForm(messageType), 116 SessionDescription.Type.fromCanonicalForm(messageType), message. getString("sdp"));
118 message.getString("sdp"));
119 } else if (messageType.equals("candidate")) { 117 } else if (messageType.equals("candidate")) {
120 IceCandidate candidate = new IceCandidate( 118 IceCandidate candidate = new IceCandidate(
121 message.getString("id"), 119 message.getString("id"), message.getInt("label"), message.getStr ing("candidate"));
122 message.getInt("label"),
123 message.getString("candidate"));
124 iceCandidates.add(candidate); 120 iceCandidates.add(candidate);
125 } else { 121 } else {
126 Log.e(TAG, "Unknown message: " + messageString); 122 Log.e(TAG, "Unknown message: " + messageString);
127 } 123 }
128 } 124 }
129 } 125 }
130 Log.d(TAG, "RoomId: " + roomId + ". ClientId: " + clientId); 126 Log.d(TAG, "RoomId: " + roomId + ". ClientId: " + clientId);
131 Log.d(TAG, "Initiator: " + initiator); 127 Log.d(TAG, "Initiator: " + initiator);
132 Log.d(TAG, "WSS url: " + wssUrl); 128 Log.d(TAG, "WSS url: " + wssUrl);
133 Log.d(TAG, "WSS POST url: " + wssPostUrl); 129 Log.d(TAG, "WSS POST url: " + wssPostUrl);
(...skipping 12 matching lines...) Expand all
146 if (!isTurnPresent) { 142 if (!isTurnPresent) {
147 LinkedList<PeerConnection.IceServer> turnServers = 143 LinkedList<PeerConnection.IceServer> turnServers =
148 requestTurnServers(roomJson.getString("ice_server_url")); 144 requestTurnServers(roomJson.getString("ice_server_url"));
149 for (PeerConnection.IceServer turnServer : turnServers) { 145 for (PeerConnection.IceServer turnServer : turnServers) {
150 Log.d(TAG, "TurnServer: " + turnServer); 146 Log.d(TAG, "TurnServer: " + turnServer);
151 iceServers.add(turnServer); 147 iceServers.add(turnServer);
152 } 148 }
153 } 149 }
154 150
155 SignalingParameters params = new SignalingParameters( 151 SignalingParameters params = new SignalingParameters(
156 iceServers, initiator, 152 iceServers, initiator, clientId, wssUrl, wssPostUrl, offerSdp, iceCand idates);
157 clientId, wssUrl, wssPostUrl,
158 offerSdp, iceCandidates);
159 events.onSignalingParametersReady(params); 153 events.onSignalingParametersReady(params);
160 } catch (JSONException e) { 154 } catch (JSONException e) {
161 events.onSignalingParametersError( 155 events.onSignalingParametersError("Room JSON parsing error: " + e.toString ());
162 "Room JSON parsing error: " + e.toString());
163 } catch (IOException e) { 156 } catch (IOException e) {
164 events.onSignalingParametersError("Room IO error: " + e.toString()); 157 events.onSignalingParametersError("Room IO error: " + e.toString());
165 } 158 }
166 } 159 }
167 160
168 // Requests & returns a TURN ICE Server based on a request URL. Must be run 161 // Requests & returns a TURN ICE Server based on a request URL. Must be run
169 // off the main thread! 162 // off the main thread!
170 private LinkedList<PeerConnection.IceServer> requestTurnServers(String url) 163 private LinkedList<PeerConnection.IceServer> requestTurnServers(String url)
171 throws IOException, JSONException { 164 throws IOException, JSONException {
172 LinkedList<PeerConnection.IceServer> turnServers = 165 LinkedList<PeerConnection.IceServer> turnServers = new LinkedList<PeerConnec tion.IceServer>();
173 new LinkedList<PeerConnection.IceServer>();
174 Log.d(TAG, "Request TURN from: " + url); 166 Log.d(TAG, "Request TURN from: " + url);
175 HttpURLConnection connection = 167 HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnecti on();
176 (HttpURLConnection) new URL(url).openConnection();
177 connection.setDoOutput(true); 168 connection.setDoOutput(true);
178 connection.setRequestProperty("REFERER", "https://appr.tc"); 169 connection.setRequestProperty("REFERER", "https://appr.tc");
179 connection.setConnectTimeout(TURN_HTTP_TIMEOUT_MS); 170 connection.setConnectTimeout(TURN_HTTP_TIMEOUT_MS);
180 connection.setReadTimeout(TURN_HTTP_TIMEOUT_MS); 171 connection.setReadTimeout(TURN_HTTP_TIMEOUT_MS);
181 int responseCode = connection.getResponseCode(); 172 int responseCode = connection.getResponseCode();
182 if (responseCode != 200) { 173 if (responseCode != 200) {
183 throw new IOException("Non-200 response when requesting TURN server from " 174 throw new IOException("Non-200 response when requesting TURN server from " + url + " : "
184 + url + " : " + connection.getHeaderField(null)); 175 + connection.getHeaderField(null));
185 } 176 }
186 InputStream responseStream = connection.getInputStream(); 177 InputStream responseStream = connection.getInputStream();
187 String response = drainStream(responseStream); 178 String response = drainStream(responseStream);
188 connection.disconnect(); 179 connection.disconnect();
189 Log.d(TAG, "TURN response: " + response); 180 Log.d(TAG, "TURN response: " + response);
190 JSONObject responseJSON = new JSONObject(response); 181 JSONObject responseJSON = new JSONObject(response);
191 JSONArray iceServers = responseJSON.getJSONArray("iceServers"); 182 JSONArray iceServers = responseJSON.getJSONArray("iceServers");
192 for (int i = 0; i < iceServers.length(); ++i) { 183 for (int i = 0; i < iceServers.length(); ++i) {
193 JSONObject server = iceServers.getJSONObject(i); 184 JSONObject server = iceServers.getJSONObject(i);
194 JSONArray turnUrls = server.getJSONArray("urls"); 185 JSONArray turnUrls = server.getJSONArray("urls");
195 String username = 186 String username = server.has("username") ? server.getString("username") : "";
196 server.has("username") ? server.getString("username") : ""; 187 String credential = server.has("credential") ? server.getString("credentia l") : "";
197 String credential =
198 server.has("credential") ? server.getString("credential") : "";
199 for (int j = 0; j < turnUrls.length(); j++) { 188 for (int j = 0; j < turnUrls.length(); j++) {
200 String turnUrl = turnUrls.getString(j); 189 String turnUrl = turnUrls.getString(j);
201 turnServers.add(new PeerConnection.IceServer(turnUrl, username, 190 turnServers.add(new PeerConnection.IceServer(turnUrl, username, credenti al));
202 credential));
203 } 191 }
204 } 192 }
205 return turnServers; 193 return turnServers;
206 } 194 }
207 195
208 // Return the list of ICE servers described by a WebRTCPeerConnection 196 // Return the list of ICE servers described by a WebRTCPeerConnection
209 // configuration string. 197 // configuration string.
210 private LinkedList<PeerConnection.IceServer> iceServersFromPCConfigJSON( 198 private LinkedList<PeerConnection.IceServer> iceServersFromPCConfigJSON(String pcConfig)
211 String pcConfig) throws JSONException { 199 throws JSONException {
212 JSONObject json = new JSONObject(pcConfig); 200 JSONObject json = new JSONObject(pcConfig);
213 JSONArray servers = json.getJSONArray("iceServers"); 201 JSONArray servers = json.getJSONArray("iceServers");
214 LinkedList<PeerConnection.IceServer> ret = 202 LinkedList<PeerConnection.IceServer> ret = new LinkedList<PeerConnection.Ice Server>();
215 new LinkedList<PeerConnection.IceServer>();
216 for (int i = 0; i < servers.length(); ++i) { 203 for (int i = 0; i < servers.length(); ++i) {
217 JSONObject server = servers.getJSONObject(i); 204 JSONObject server = servers.getJSONObject(i);
218 String url = server.getString("urls"); 205 String url = server.getString("urls");
219 String credential = 206 String credential = server.has("credential") ? server.getString("credentia l") : "";
220 server.has("credential") ? server.getString("credential") : "";
221 ret.add(new PeerConnection.IceServer(url, "", credential)); 207 ret.add(new PeerConnection.IceServer(url, "", credential));
222 } 208 }
223 return ret; 209 return ret;
224 } 210 }
225 211
226 // Return the contents of an InputStream as a String. 212 // Return the contents of an InputStream as a String.
227 private static String drainStream(InputStream in) { 213 private static String drainStream(InputStream in) {
228 Scanner s = new Scanner(in).useDelimiter("\\A"); 214 Scanner s = new Scanner(in).useDelimiter("\\A");
229 return s.hasNext() ? s.next() : ""; 215 return s.hasNext() ? s.next() : "";
230 } 216 }
231
232 } 217 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698