OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 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 |
11 package org.appspot.apprtc; | 11 package org.appspot.apprtc; |
12 | 12 |
13 import android.util.Log; | 13 import android.util.Log; |
14 | 14 |
15 import org.appspot.apprtc.util.LooperExecutor; | |
16 import org.json.JSONArray; | 15 import org.json.JSONArray; |
17 import org.json.JSONException; | 16 import org.json.JSONException; |
18 import org.json.JSONObject; | 17 import org.json.JSONObject; |
19 import org.webrtc.IceCandidate; | 18 import org.webrtc.IceCandidate; |
20 import org.webrtc.PeerConnection; | 19 import org.webrtc.PeerConnection; |
21 import org.webrtc.SessionDescription; | 20 import org.webrtc.SessionDescription; |
22 | 21 |
23 import java.util.LinkedList; | 22 import java.util.LinkedList; |
| 23 import java.util.concurrent.ExecutorService; |
| 24 import java.util.concurrent.Executors; |
24 import java.util.regex.Matcher; | 25 import java.util.regex.Matcher; |
25 import java.util.regex.Pattern; | 26 import java.util.regex.Pattern; |
26 | 27 |
27 /** | 28 /** |
28 * Implementation of AppRTCClient that uses direct TCP connection as the signali
ng channel. | 29 * Implementation of AppRTCClient that uses direct TCP connection as the signali
ng channel. |
29 * This eliminates the need for an external server. This class does not support
loopback | 30 * This eliminates the need for an external server. This class does not support
loopback |
30 * connections. | 31 * connections. |
31 */ | 32 */ |
32 public class DirectRTCClient implements AppRTCClient, TCPChannelClient.TCPChanne
lEvents { | 33 public class DirectRTCClient implements AppRTCClient, TCPChannelClient.TCPChanne
lEvents { |
33 private static final String TAG = "DirectRTCClient"; | 34 private static final String TAG = "DirectRTCClient"; |
(...skipping 11 matching lines...) Expand all Loading... |
45 // IPv6 without [] | 46 // IPv6 without [] |
46 + "((([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4})?::(([0-9a-fA-F]{1,4}:)*[0-9a-
fA-F]{1,4})?)|" | 47 + "((([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4})?::(([0-9a-fA-F]{1,4}:)*[0-9a-
fA-F]{1,4})?)|" |
47 + "(([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4})|" | 48 + "(([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4})|" |
48 // Literals | 49 // Literals |
49 + "localhost" | 50 + "localhost" |
50 + ")" | 51 + ")" |
51 // Optional port number | 52 // Optional port number |
52 + "(:(\\d+))?" | 53 + "(:(\\d+))?" |
53 ); | 54 ); |
54 | 55 |
55 private final LooperExecutor executor; | 56 private final ExecutorService executor; |
56 private final SignalingEvents events; | 57 private final SignalingEvents events; |
57 private TCPChannelClient tcpClient; | 58 private TCPChannelClient tcpClient; |
58 private RoomConnectionParameters connectionParameters; | 59 private RoomConnectionParameters connectionParameters; |
59 | 60 |
60 private enum ConnectionState { | 61 private enum ConnectionState { |
61 NEW, CONNECTED, CLOSED, ERROR | 62 NEW, CONNECTED, CLOSED, ERROR |
62 }; | 63 }; |
63 | 64 |
64 // All alterations of the room state should be done from inside the looper thr
ead. | 65 // All alterations of the room state should be done from inside the looper thr
ead. |
65 private ConnectionState roomState; | 66 private ConnectionState roomState; |
66 | 67 |
67 public DirectRTCClient(SignalingEvents events, LooperExecutor looperExecutor)
{ | 68 public DirectRTCClient(SignalingEvents events) { |
68 this.events = events; | 69 this.events = events; |
69 executor = looperExecutor; | |
70 | 70 |
71 executor.requestStart(); | 71 executor = Executors.newSingleThreadExecutor(); |
72 roomState = ConnectionState.NEW; | 72 roomState = ConnectionState.NEW; |
73 } | 73 } |
74 | 74 |
75 /** | 75 /** |
76 * Connects to the room, roomId in connectionsParameters is required. roomId m
ust be a valid | 76 * Connects to the room, roomId in connectionsParameters is required. roomId m
ust be a valid |
77 * IP address matching IP_PATTERN. | 77 * IP address matching IP_PATTERN. |
78 */ | 78 */ |
79 @Override | 79 @Override |
80 public void connectToRoom(RoomConnectionParameters connectionParameters) { | 80 public void connectToRoom(RoomConnectionParameters connectionParameters) { |
81 this.connectionParameters = connectionParameters; | 81 this.connectionParameters = connectionParameters; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 * | 141 * |
142 * Runs on the looper thread. | 142 * Runs on the looper thread. |
143 */ | 143 */ |
144 private void disconnectFromRoomInternal() { | 144 private void disconnectFromRoomInternal() { |
145 roomState = ConnectionState.CLOSED; | 145 roomState = ConnectionState.CLOSED; |
146 | 146 |
147 if (tcpClient != null) { | 147 if (tcpClient != null) { |
148 tcpClient.disconnect(); | 148 tcpClient.disconnect(); |
149 tcpClient = null; | 149 tcpClient = null; |
150 } | 150 } |
| 151 executor.shutdown(); |
151 } | 152 } |
152 | 153 |
153 @Override | 154 @Override |
154 public void sendOfferSdp(final SessionDescription sdp) { | 155 public void sendOfferSdp(final SessionDescription sdp) { |
155 executor.execute(new Runnable() { | 156 executor.execute(new Runnable() { |
156 @Override | 157 @Override |
157 public void run() { | 158 public void run() { |
158 if (roomState != ConnectionState.CONNECTED) { | 159 if (roomState != ConnectionState.CONNECTED) { |
159 reportError("Sending offer SDP in non connected state."); | 160 reportError("Sending offer SDP in non connected state."); |
160 return; | 161 return; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 reportError("Unexpected TCP message: " + msg); | 289 reportError("Unexpected TCP message: " + msg); |
289 } | 290 } |
290 } catch (JSONException e) { | 291 } catch (JSONException e) { |
291 reportError("TCP message JSON parsing error: " + e.toString()); | 292 reportError("TCP message JSON parsing error: " + e.toString()); |
292 } | 293 } |
293 } | 294 } |
294 | 295 |
295 @Override | 296 @Override |
296 public void onTCPError(String description) { | 297 public void onTCPError(String description) { |
297 reportError("TCP connection error: " + description); | 298 reportError("TCP connection error: " + description); |
298 executor.requestStop(); | |
299 } | 299 } |
300 | 300 |
301 @Override | 301 @Override |
302 public void onTCPClose() { | 302 public void onTCPClose() { |
303 events.onChannelClose(); | 303 events.onChannelClose(); |
304 executor.requestStop(); | |
305 } | 304 } |
306 | 305 |
307 // -------------------------------------------------------------------- | 306 // -------------------------------------------------------------------- |
308 // Helper functions. | 307 // Helper functions. |
309 private void reportError(final String errorMessage) { | 308 private void reportError(final String errorMessage) { |
310 Log.e(TAG, errorMessage); | 309 Log.e(TAG, errorMessage); |
311 executor.execute(new Runnable() { | 310 executor.execute(new Runnable() { |
312 @Override | 311 @Override |
313 public void run() { | 312 public void run() { |
314 if (roomState != ConnectionState.ERROR) { | 313 if (roomState != ConnectionState.ERROR) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 return json; | 345 return json; |
347 } | 346 } |
348 | 347 |
349 // Converts a JSON candidate to a Java object. | 348 // Converts a JSON candidate to a Java object. |
350 private static IceCandidate toJavaCandidate(JSONObject json) throws JSONExcept
ion { | 349 private static IceCandidate toJavaCandidate(JSONObject json) throws JSONExcept
ion { |
351 return new IceCandidate(json.getString("id"), | 350 return new IceCandidate(json.getString("id"), |
352 json.getInt("label"), | 351 json.getInt("label"), |
353 json.getString("candidate")); | 352 json.getString("candidate")); |
354 } | 353 } |
355 } | 354 } |
OLD | NEW |