Sync Android AppRTCDemo with internal repo.
- Fixed some Lint warnings. - Switch to OPUS by default. - Add check to WebSocket connection that public methods are called on correct thread. R=jiayl@webrtc.org, wzh@webrtc.org Review URL: https://webrtc-codereview.appspot.com/36669004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@8032 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
@@ -26,6 +26,10 @@
|
||||
*/
|
||||
package org.appspot.apprtc;
|
||||
|
||||
import org.appspot.apprtc.util.AsyncHttpURLConnection;
|
||||
import org.appspot.apprtc.util.AsyncHttpURLConnection.AsyncHttpEvents;
|
||||
import org.appspot.apprtc.util.LooperExecutor;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import de.tavendo.autobahn.WebSocket.WebSocketConnectionObserver;
|
||||
@@ -39,15 +43,12 @@ import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import org.appspot.apprtc.util.AsyncHttpURLConnection;
|
||||
import org.appspot.apprtc.util.AsyncHttpURLConnection.AsyncHttpEvents;
|
||||
import org.appspot.apprtc.util.LooperExecutor;
|
||||
|
||||
/**
|
||||
* WebSocket client implementation.
|
||||
* All public methods should be called from a looper executor thread
|
||||
* passed in constructor.
|
||||
* All events are issued on the same thread.
|
||||
*
|
||||
* <p>All public methods should be called from a looper executor thread
|
||||
* passed in a constructor, otherwise exception will be thrown.
|
||||
* All events are dispatched on the same thread.
|
||||
*/
|
||||
|
||||
public class WebSocketChannelClient {
|
||||
@@ -66,10 +67,10 @@ public class WebSocketChannelClient {
|
||||
private boolean closeEvent;
|
||||
// WebSocket send queue. Messages are added to the queue when WebSocket
|
||||
// client is not registered and are consumed in register() call.
|
||||
private LinkedList<String> wsSendQueue;
|
||||
private final LinkedList<String> wsSendQueue;
|
||||
|
||||
/**
|
||||
* WebSocketConnectionState is the names of possible WS connection states.
|
||||
* Possible WebSocket connection states.
|
||||
*/
|
||||
public enum WebSocketConnectionState {
|
||||
NEW, CONNECTED, REGISTERED, CLOSED, ERROR
|
||||
@@ -77,7 +78,7 @@ public class WebSocketChannelClient {
|
||||
|
||||
/**
|
||||
* Callback interface for messages delivered on WebSocket.
|
||||
* All events are invoked from UI thread.
|
||||
* All events are dispatched from a looper executor thread.
|
||||
*/
|
||||
public interface WebSocketChannelEvents {
|
||||
public void onWebSocketOpen();
|
||||
@@ -102,6 +103,7 @@ public class WebSocketChannelClient {
|
||||
|
||||
public void connect(final String wsUrl, final String postUrl,
|
||||
final String roomID, final String clientID) {
|
||||
checkIfCalledOnValidThread();
|
||||
if (state != WebSocketConnectionState.NEW) {
|
||||
Log.e(TAG, "WebSocket is already connected.");
|
||||
return;
|
||||
@@ -125,6 +127,7 @@ public class WebSocketChannelClient {
|
||||
}
|
||||
|
||||
public void register() {
|
||||
checkIfCalledOnValidThread();
|
||||
if (state != WebSocketConnectionState.CONNECTED) {
|
||||
Log.w(TAG, "WebSocket register() in state " + state);
|
||||
return;
|
||||
@@ -138,28 +141,25 @@ public class WebSocketChannelClient {
|
||||
ws.sendTextMessage(json.toString());
|
||||
state = WebSocketConnectionState.REGISTERED;
|
||||
// Send any previously accumulated messages.
|
||||
synchronized (wsSendQueue) {
|
||||
for (String sendMessage : wsSendQueue) {
|
||||
send(sendMessage);
|
||||
}
|
||||
wsSendQueue.clear();
|
||||
for (String sendMessage : wsSendQueue) {
|
||||
send(sendMessage);
|
||||
}
|
||||
wsSendQueue.clear();
|
||||
} catch (JSONException e) {
|
||||
reportError("WebSocket register JSON error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void send(String message) {
|
||||
checkIfCalledOnValidThread();
|
||||
switch (state) {
|
||||
case NEW:
|
||||
case CONNECTED:
|
||||
// Store outgoing messages and send them after websocket client
|
||||
// is registered.
|
||||
Log.d(TAG, "WS ACC: " + message);
|
||||
synchronized (wsSendQueue) {
|
||||
wsSendQueue.add(message);
|
||||
return;
|
||||
}
|
||||
wsSendQueue.add(message);
|
||||
return;
|
||||
case ERROR:
|
||||
case CLOSED:
|
||||
Log.e(TAG, "WebSocket send() in error or closed state : " + message);
|
||||
@@ -181,15 +181,14 @@ public class WebSocketChannelClient {
|
||||
}
|
||||
|
||||
// This call can be used to send WebSocket messages before WebSocket
|
||||
// connection is opened. However for now this way of sending messages
|
||||
// is not used until possible race condition of arriving ice candidates
|
||||
// send through websocket before SDP answer sent through http post will be
|
||||
// resolved.
|
||||
// connection is opened.
|
||||
public void post(String message) {
|
||||
checkIfCalledOnValidThread();
|
||||
sendWSSMessage("POST", message);
|
||||
}
|
||||
|
||||
public void disconnect(boolean waitForComplete) {
|
||||
checkIfCalledOnValidThread();
|
||||
Log.d(TAG, "Disonnect WebSocket. State: " + state);
|
||||
if (state == WebSocketConnectionState.REGISTERED) {
|
||||
send("{\"type\": \"bye\"}");
|
||||
@@ -209,9 +208,10 @@ public class WebSocketChannelClient {
|
||||
// sending any pending messages to deleted looper thread.
|
||||
if (waitForComplete) {
|
||||
synchronized (closeEventLock) {
|
||||
if (!closeEvent) {
|
||||
while (!closeEvent) {
|
||||
try {
|
||||
closeEventLock.wait(CLOSE_TIMEOUT);
|
||||
break;
|
||||
} catch (InterruptedException e) {
|
||||
Log.e(TAG, "Wait error: " + e.toString());
|
||||
}
|
||||
@@ -242,17 +242,26 @@ public class WebSocketChannelClient {
|
||||
AsyncHttpURLConnection httpConnection = new AsyncHttpURLConnection(
|
||||
method, postUrl, message, new AsyncHttpEvents() {
|
||||
@Override
|
||||
public void OnHttpError(String errorMessage) {
|
||||
public void onHttpError(String errorMessage) {
|
||||
reportError("WS " + method + " error: " + errorMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void OnHttpComplete(String response) {
|
||||
public void onHttpComplete(String response) {
|
||||
}
|
||||
});
|
||||
httpConnection.send();
|
||||
}
|
||||
|
||||
// Helper method for debugging purposes. Ensures that WebSocket method is
|
||||
// called on a looper thread.
|
||||
private void checkIfCalledOnValidThread() {
|
||||
if (!executor.checkOnLooperThread()) {
|
||||
throw new IllegalStateException(
|
||||
"WebSocket method is not called on valid thread");
|
||||
}
|
||||
}
|
||||
|
||||
private class WebSocketObserver implements WebSocketConnectionObserver {
|
||||
@Override
|
||||
public void onOpen() {
|
||||
|
||||
Reference in New Issue
Block a user