Add DCHECK to ensure that NetEq's packet buffer is not empty

This DCHECK ensures that one packet was inserted after the buffer was
flushed.

R=kwiberg@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/30169004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@7719 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
henrik.lundin@webrtc.org
2014-11-19 13:02:24 +00:00
parent 2176db343c
commit 6f6ef72950
17 changed files with 1211 additions and 324 deletions

View File

@@ -0,0 +1,216 @@
/*
* libjingle
* Copyright 2014, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.appspot.apprtc;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
import de.tavendo.autobahn.WebSocket.WebSocketConnectionObserver;
import java.net.URI;
import java.net.URISyntaxException;
import org.json.JSONException;
import org.json.JSONObject;
/**
* WebSocket client implementation.
* For proper synchronization all methods should be called from UI thread
* and all WebSocket events are delivered on UI thread as well.
*/
public class WebSocketChannelClient {
private final String TAG = "WSChannelRTCClient";
private final WebSocketChannelEvents events;
private final Handler uiHandler;
private WebSocketConnection ws;
private WebSocketObserver wsObserver;
private URI serverURI;
private WebSocketConnectionState state;
public enum WebSocketConnectionState {
NEW, CONNECTED, REGISTERED, CLOSED, ERROR
};
/**
* Callback interface for messages delivered on WebSocket.
* All events are invoked from UI thread.
*/
public interface WebSocketChannelEvents {
public void onWebSocketOpen();
public void onWebSocketMessage(final String message);
public void onWebSocketClose();
public void onWebSocketError(final String description);
}
public WebSocketChannelClient(WebSocketChannelEvents events) {
this.events = events;
uiHandler = new Handler(Looper.getMainLooper());
state = WebSocketConnectionState.NEW;
}
public WebSocketConnectionState getState() {
return state;
}
public void connect(String url) {
if (state != WebSocketConnectionState.NEW) {
Log.e(TAG, "WebSocket is already connected.");
return;
}
Log.d(TAG, "Connecting WebSocket to: " + url);
ws = new WebSocketConnection();
wsObserver = new WebSocketObserver();
try {
serverURI = new URI(url);
ws.connect(serverURI, wsObserver);
} catch (URISyntaxException e) {
reportError("URI error: " + e.getMessage());
} catch (WebSocketException e) {
reportError("WebSocket connection error: " + e.getMessage());
}
}
public void register(String roomId, String clientId) {
if (state != WebSocketConnectionState.CONNECTED) {
Log.w(TAG, "WebSocket register() in state " + state);
return;
}
JSONObject json = new JSONObject();
try {
json.put("cmd", "register");
json.put("roomid", roomId);
json.put("clientid", clientId);
Log.d(TAG, "WS SEND: " + json.toString());
ws.sendTextMessage(json.toString());
state = WebSocketConnectionState.REGISTERED;
} catch (JSONException e) {
reportError("WebSocket register JSON error: " + e.getMessage());
}
}
public void send(String message) {
if (state != WebSocketConnectionState.REGISTERED) {
Log.e(TAG, "WebSocket send() in non registered state : " + message);
return;
}
JSONObject json = new JSONObject();
try {
json.put("cmd", "send");
json.put("msg", message);
message = json.toString();
Log.d(TAG, "WS SEND: " + message);
ws.sendTextMessage(message);
} catch (JSONException e) {
reportError("WebSocket send JSON error: " + e.getMessage());
}
}
public void disconnect() {
Log.d(TAG, "Disonnect WebSocket. State: " + state);
if (state == WebSocketConnectionState.REGISTERED) {
send("{\"type\": \"bye\"}");
state = WebSocketConnectionState.CONNECTED;
}
// TODO(glaznev): send DELETE to http WebSocket server once send()
// will switch to http POST.
// Close WebSocket in CONNECTED or ERROR states only.
if (state == WebSocketConnectionState.CONNECTED ||
state == WebSocketConnectionState.ERROR) {
state = WebSocketConnectionState.CLOSED;
ws.disconnect();
}
}
private void reportError(final String errorMessage) {
Log.e(TAG, errorMessage);
uiHandler.post(new Runnable() {
public void run() {
if (state != WebSocketConnectionState.ERROR) {
state = WebSocketConnectionState.ERROR;
events.onWebSocketError(errorMessage);
}
}
});
}
private class WebSocketObserver implements WebSocketConnectionObserver {
@Override
public void onOpen() {
Log.d(TAG, "WebSocket connection opened to: " + serverURI.toString());
uiHandler.post(new Runnable() {
public void run() {
state = WebSocketConnectionState.CONNECTED;
events.onWebSocketOpen();
}
});
}
@Override
public void onClose(WebSocketCloseNotification code, String reason) {
Log.d(TAG, "WebSocket connection closed. Code: " + code +
". Reason: " + reason);
uiHandler.post(new Runnable() {
public void run() {
if (state != WebSocketConnectionState.CLOSED) {
state = WebSocketConnectionState.CLOSED;
events.onWebSocketClose();
}
}
});
}
@Override
public void onTextMessage(String payload) {
Log.d(TAG, "WS GET: " + payload);
final String message = payload;
uiHandler.post(new Runnable() {
public void run() {
if (state == WebSocketConnectionState.CONNECTED ||
state == WebSocketConnectionState.REGISTERED) {
events.onWebSocketMessage(message);
}
}
});
}
@Override
public void onRawTextMessage(byte[] payload) {
}
@Override
public void onBinaryMessage(byte[] payload) {
}
}
}