Merge fixes and changed for Android AppRTCDemo from internal repo.
- Rename AppRTCDemoActivity to CallActivity and move UI controls to a fragment. - Add option to enable/disable statistics. - Move peer connection and video constraints from URL to peer connection client. - Variable renaming. R=jiayl@webrtc.org, wzh@webrtc.org Review URL: https://webrtc-codereview.appspot.com/33299004 Cr-Commit-Position: refs/heads/master@{#8319} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8319 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
@@ -54,43 +54,43 @@ import org.webrtc.SessionDescription;
|
||||
public class WebSocketRTCClient implements AppRTCClient,
|
||||
WebSocketChannelEvents {
|
||||
private static final String TAG = "WSRTCClient";
|
||||
public static final String GAE_JOIN = "join";
|
||||
private static final String GAE_MESSAGE = "message";
|
||||
private static final String GAE_LEAVE = "leave";
|
||||
private static final String ROOM_JOIN = "join";
|
||||
private static final String ROOM_MESSAGE = "message";
|
||||
private static final String ROOM_LEAVE = "leave";
|
||||
|
||||
private enum ConnectionState {
|
||||
NEW, CONNECTED, CLOSED, ERROR
|
||||
};
|
||||
private enum MessageType {
|
||||
MESSAGE, BYE
|
||||
MESSAGE, LEAVE
|
||||
};
|
||||
private final LooperExecutor executor;
|
||||
private boolean loopback;
|
||||
private boolean initiator;
|
||||
private SignalingEvents events;
|
||||
private WebSocketChannelClient wsClient;
|
||||
private ConnectionState roomState;
|
||||
private String roomUrl;
|
||||
private String postMessageUrl;
|
||||
private String byeMessageUrl;
|
||||
private RoomConnectionParameters connectionParameters;
|
||||
private String messageUrl;
|
||||
private String leaveUrl;
|
||||
|
||||
public WebSocketRTCClient(SignalingEvents events) {
|
||||
public WebSocketRTCClient(SignalingEvents events, LooperExecutor executor) {
|
||||
this.events = events;
|
||||
executor = new LooperExecutor();
|
||||
this.executor = executor;
|
||||
roomState = ConnectionState.NEW;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// AppRTCClient interface implementation.
|
||||
// Asynchronously connect to an AppRTC room URL, e.g.
|
||||
// https://apprtc.appspot.com/register/<room>, retrieve room parameters
|
||||
// and connect to WebSocket server.
|
||||
// Asynchronously connect to an AppRTC room URL using supplied connection
|
||||
// parameters, retrieves room parameters and connect to WebSocket server.
|
||||
@Override
|
||||
public void connectToRoom(final String url, final boolean loopback) {
|
||||
public void connectToRoom(RoomConnectionParameters connectionParameters) {
|
||||
this.connectionParameters = connectionParameters;
|
||||
executor.requestStart();
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
connectToRoomInternal(url, loopback);
|
||||
connectToRoomInternal();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -107,33 +107,32 @@ public class WebSocketRTCClient implements AppRTCClient,
|
||||
}
|
||||
|
||||
// Connects to room - function runs on a local looper thread.
|
||||
private void connectToRoomInternal(String url, boolean loopback) {
|
||||
Log.d(TAG, "Connect to room: " + url);
|
||||
this.loopback = loopback;
|
||||
private void connectToRoomInternal() {
|
||||
String connectionUrl = getConnectionUrl(connectionParameters);
|
||||
Log.d(TAG, "Connect to room: " + connectionUrl);
|
||||
roomState = ConnectionState.NEW;
|
||||
roomUrl = url.substring(0, url.indexOf("/" + GAE_JOIN));
|
||||
// Create WebSocket client.
|
||||
wsClient = new WebSocketChannelClient(executor, this);
|
||||
// Get room parameters.
|
||||
new RoomParametersFetcher(loopback, url,
|
||||
new RoomParametersFetcherEvents() {
|
||||
@Override
|
||||
public void onSignalingParametersReady(
|
||||
final SignalingParameters params) {
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
signalingParametersReady(params);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSignalingParametersError(String description) {
|
||||
reportError(description);
|
||||
}
|
||||
RoomParametersFetcherEvents callbacks = new RoomParametersFetcherEvents() {
|
||||
@Override
|
||||
public void onSignalingParametersReady(
|
||||
final SignalingParameters params) {
|
||||
WebSocketRTCClient.this.executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
WebSocketRTCClient.this.signalingParametersReady(params);
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
@Override
|
||||
public void onSignalingParametersError(String description) {
|
||||
WebSocketRTCClient.this.reportError(description);
|
||||
}
|
||||
};
|
||||
|
||||
new RoomParametersFetcher(connectionParameters.loopback, connectionUrl,
|
||||
null, callbacks).makeRequest();
|
||||
}
|
||||
|
||||
// Disconnect from room and send bye messages - runs on a local looper thread.
|
||||
@@ -141,7 +140,7 @@ public class WebSocketRTCClient implements AppRTCClient,
|
||||
Log.d(TAG, "Disconnect. Room state: " + roomState);
|
||||
if (roomState == ConnectionState.CONNECTED) {
|
||||
Log.d(TAG, "Closing room.");
|
||||
sendPostMessage(MessageType.BYE, byeMessageUrl, "");
|
||||
sendPostMessage(MessageType.LEAVE, leaveUrl, null);
|
||||
}
|
||||
roomState = ConnectionState.CLOSED;
|
||||
if (wsClient != null) {
|
||||
@@ -150,57 +149,53 @@ public class WebSocketRTCClient implements AppRTCClient,
|
||||
}
|
||||
|
||||
// Helper functions to get connection, post message and leave message URLs
|
||||
public static String getGAEConnectionUrl(String roomUrl, String roomId) {
|
||||
return roomUrl + "/" + GAE_JOIN + "/" + roomId;
|
||||
private String getConnectionUrl(
|
||||
RoomConnectionParameters connectionParameters) {
|
||||
return connectionParameters.roomUrl + "/" + ROOM_JOIN + "/"
|
||||
+ connectionParameters.roomId;
|
||||
}
|
||||
|
||||
private String getGAEPostMessageUrl(String roomUrl, String roomId,
|
||||
String clientId) {
|
||||
return roomUrl + "/" + GAE_MESSAGE + "/" + roomId + "/" + clientId;
|
||||
private String getMessageUrl(RoomConnectionParameters connectionParameters,
|
||||
SignalingParameters signalingParameters) {
|
||||
return connectionParameters.roomUrl + "/" + ROOM_MESSAGE + "/"
|
||||
+ connectionParameters.roomId + "/" + signalingParameters.clientId;
|
||||
}
|
||||
|
||||
private String getGAEByeMessageUrl(String roomUrl, String roomId,
|
||||
String clientId) {
|
||||
return roomUrl + "/" + GAE_LEAVE + "/" + roomId + "/" + clientId;
|
||||
private String getLeaveUrl(RoomConnectionParameters connectionParameters,
|
||||
SignalingParameters signalingParameters) {
|
||||
return connectionParameters.roomUrl + "/" + ROOM_LEAVE + "/"
|
||||
+ connectionParameters.roomId + "/" + signalingParameters.clientId;
|
||||
}
|
||||
|
||||
// Callback issued when room parameters are extracted. Runs on local
|
||||
// looper thread.
|
||||
private void signalingParametersReady(final SignalingParameters params) {
|
||||
private void signalingParametersReady(
|
||||
final SignalingParameters signalingParameters) {
|
||||
Log.d(TAG, "Room connection completed.");
|
||||
if (loopback && (!params.initiator || params.offerSdp != null)) {
|
||||
if (connectionParameters.loopback
|
||||
&& (!signalingParameters.initiator
|
||||
|| signalingParameters.offerSdp != null)) {
|
||||
reportError("Loopback room is busy.");
|
||||
return;
|
||||
}
|
||||
if (!loopback && !params.initiator && params.offerSdp == null) {
|
||||
if (!connectionParameters.loopback
|
||||
&& !signalingParameters.initiator
|
||||
&& signalingParameters.offerSdp == null) {
|
||||
Log.w(TAG, "No offer SDP in room response.");
|
||||
}
|
||||
initiator = params.initiator;
|
||||
postMessageUrl = getGAEPostMessageUrl(
|
||||
roomUrl, params.roomId, params.clientId);
|
||||
byeMessageUrl = getGAEByeMessageUrl(
|
||||
roomUrl, params.roomId, params.clientId);
|
||||
initiator = signalingParameters.initiator;
|
||||
messageUrl = getMessageUrl(connectionParameters, signalingParameters);
|
||||
leaveUrl = getLeaveUrl(connectionParameters, signalingParameters);
|
||||
Log.d(TAG, "Message URL: " + messageUrl);
|
||||
Log.d(TAG, "Leave URL: " + leaveUrl);
|
||||
roomState = ConnectionState.CONNECTED;
|
||||
|
||||
// Fire connection and signaling parameters events.
|
||||
events.onConnectedToRoom(params);
|
||||
events.onConnectedToRoom(signalingParameters);
|
||||
|
||||
// Connect to WebSocket server.
|
||||
wsClient.connect(
|
||||
params.wssUrl, params.wssPostUrl, params.roomId, params.clientId);
|
||||
|
||||
// For call receiver get sdp offer and ice candidates
|
||||
// from room parameters and fire corresponding events.
|
||||
if (!params.initiator) {
|
||||
if (params.offerSdp != null) {
|
||||
events.onRemoteDescription(params.offerSdp);
|
||||
}
|
||||
if (params.iceCandidates != null) {
|
||||
for (IceCandidate iceCandidate : params.iceCandidates) {
|
||||
events.onRemoteIceCandidate(iceCandidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
wsClient.connect(signalingParameters.wssUrl, signalingParameters.wssPostUrl,
|
||||
connectionParameters.roomId, signalingParameters.clientId);
|
||||
}
|
||||
|
||||
// Send local offer SDP to the other participant.
|
||||
@@ -216,8 +211,8 @@ public class WebSocketRTCClient implements AppRTCClient,
|
||||
JSONObject json = new JSONObject();
|
||||
jsonPut(json, "sdp", sdp.description);
|
||||
jsonPut(json, "type", "offer");
|
||||
sendPostMessage(MessageType.MESSAGE, postMessageUrl, json.toString());
|
||||
if (loopback) {
|
||||
sendPostMessage(MessageType.MESSAGE, messageUrl, json.toString());
|
||||
if (connectionParameters.loopback) {
|
||||
// In loopback mode rename this offer to answer and route it back.
|
||||
SessionDescription sdpAnswer = new SessionDescription(
|
||||
SessionDescription.Type.fromCanonicalForm("answer"),
|
||||
@@ -234,7 +229,7 @@ public class WebSocketRTCClient implements AppRTCClient,
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (loopback) {
|
||||
if (connectionParameters.loopback) {
|
||||
Log.e(TAG, "Sending answer in loopback mode.");
|
||||
return;
|
||||
}
|
||||
@@ -267,8 +262,8 @@ public class WebSocketRTCClient implements AppRTCClient,
|
||||
reportError("Sending ICE candidate in non connected state.");
|
||||
return;
|
||||
}
|
||||
sendPostMessage(MessageType.MESSAGE, postMessageUrl, json.toString());
|
||||
if (loopback) {
|
||||
sendPostMessage(MessageType.MESSAGE, messageUrl, json.toString());
|
||||
if (connectionParameters.loopback) {
|
||||
events.onRemoteIceCandidate(candidate);
|
||||
}
|
||||
} else {
|
||||
@@ -384,11 +379,11 @@ public class WebSocketRTCClient implements AppRTCClient,
|
||||
// Send SDP or ICE candidate to a room server.
|
||||
private void sendPostMessage(
|
||||
final MessageType messageType, final String url, final String message) {
|
||||
if (messageType == MessageType.BYE) {
|
||||
Log.d(TAG, "C->GAE: " + url);
|
||||
} else {
|
||||
Log.d(TAG, "C->GAE: " + message);
|
||||
String logInfo = url;
|
||||
if (message != null) {
|
||||
logInfo += ". Message: " + message;
|
||||
}
|
||||
Log.d(TAG, "C->GAE: " + logInfo);
|
||||
AsyncHttpURLConnection httpConnection = new AsyncHttpURLConnection(
|
||||
"POST", url, message, new AsyncHttpEvents() {
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user