Changed the streaming to use UDP.
This commit is contained in:
@@ -78,7 +78,7 @@ public class NxtARCore implements ApplicationListener, NetworkConnectionListener
|
||||
fps = new FPSLogger();
|
||||
font = new BitmapFont();
|
||||
|
||||
//Gdx.app.setLogLevel(Application.LOG_NONE);
|
||||
Gdx.app.setLogLevel(Application.LOG_DEBUG);
|
||||
|
||||
camera = new OrthographicCamera(1, h/w);
|
||||
batch = new SpriteBatch();
|
||||
|
@@ -15,17 +15,18 @@
|
||||
*/
|
||||
package ve.ucv.ciens.ccg.nxtar.network;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
import ve.ucv.ciens.ccg.networkdata.VideoFrameDataMessage;
|
||||
import ve.ucv.ciens.ccg.networkdata.VideoStreamingControlMessage;
|
||||
import ve.ucv.ciens.ccg.nxtar.interfaces.NetworkConnectionListener;
|
||||
import ve.ucv.ciens.ccg.nxtar.interfaces.Toaster;
|
||||
import ve.ucv.ciens.ccg.nxtar.network.protocols.VideoStreamingProtocol;
|
||||
import ve.ucv.ciens.ccg.nxtar.utils.ProjectConstants;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
@@ -39,6 +40,7 @@ public class VideoStreamingThread extends Thread {
|
||||
|
||||
private NetworkConnectionListener netListener;
|
||||
private ServerSocket server;
|
||||
private DatagramSocket socket;
|
||||
private Toaster toaster;
|
||||
private ProtocolState_t protocolState;
|
||||
private boolean protocolStarted;
|
||||
@@ -71,6 +73,7 @@ public class VideoStreamingThread extends Thread {
|
||||
|
||||
try{
|
||||
server = new ServerSocket(ProjectConstants.SERVER_TCP_PORT_1);
|
||||
socket = new DatagramSocket(ProjectConstants.SERVER_TCP_PORT_2);
|
||||
}catch(IOException io){
|
||||
Gdx.app.error(TAG, CLASS_NAME + ".VideoStreamingThread() :: Error creating server: " + io.getMessage(), io);
|
||||
}
|
||||
@@ -336,6 +339,77 @@ public class VideoStreamingThread extends Thread {
|
||||
}
|
||||
}
|
||||
|
||||
private int byteArray2Int(byte[] array){
|
||||
int number = 0;
|
||||
for(int i = 0; i < 4; i++){
|
||||
number |= (array[3-i] & 0xff) << (i << 3);
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
private void receiveUdp(){
|
||||
try{
|
||||
int intSize;
|
||||
byte[] size = new byte[4];
|
||||
byte[] data;
|
||||
DatagramPacket packet;
|
||||
VideoFrameDataMessage dataMessage;
|
||||
Object tmpMessage;
|
||||
|
||||
Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Reading message size from socket.");
|
||||
try{
|
||||
packet = new DatagramPacket(size, size.length);
|
||||
socket.receive(packet);
|
||||
}catch(IOException io){
|
||||
Gdx.app.error(TAG, CLASS_NAME + ".receiveUdp() :: IOException receiving size " + io.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Creating buffers.");
|
||||
intSize = byteArray2Int(size);
|
||||
data = new byte[intSize];
|
||||
|
||||
Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Reading message from socket.");
|
||||
try{
|
||||
packet = new DatagramPacket(data, data.length);
|
||||
socket.receive(packet);
|
||||
}catch(IOException io){
|
||||
Gdx.app.error(TAG, CLASS_NAME + ".receiveUdp() :: IOException receiving data " + io.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
||||
|
||||
Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Saving message in monitor.");
|
||||
try{
|
||||
ObjectInputStream ois = new ObjectInputStream(bais);
|
||||
tmpMessage = ois.readObject();
|
||||
|
||||
if(tmpMessage instanceof VideoFrameDataMessage){
|
||||
Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Received a data message.");
|
||||
dataMessage = (VideoFrameDataMessage) tmpMessage;
|
||||
|
||||
Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Received frame dimensions are: " +
|
||||
Integer.toString(dataMessage.imageWidth) + "x" + Integer.toString(dataMessage.imageHeight));
|
||||
frameMonitor.setFrameDimensions(dataMessage.imageWidth, dataMessage.imageHeight);
|
||||
frameMonitor.setNewFrame(dataMessage.data);
|
||||
|
||||
}else{
|
||||
Gdx.app.debug(TAG, CLASS_NAME + ".receiveUdp() :: Received something unknown.");
|
||||
}
|
||||
}catch(IOException io){
|
||||
Gdx.app.error(TAG, CLASS_NAME + ".receiveUdp() :: IOException received deserializing message " + io.getMessage());
|
||||
return;
|
||||
}catch(ClassNotFoundException cn){
|
||||
Gdx.app.error(TAG, CLASS_NAME + ".receiveUdp() :: ClassNotFoundException received " + cn.getMessage());
|
||||
return;
|
||||
}
|
||||
}catch(Exception e){
|
||||
Gdx.app.error(TAG, CLASS_NAME + ".receiveUdp() :: Exception received " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public int getFps(){
|
||||
return fps;
|
||||
}
|
||||
@@ -362,7 +436,9 @@ public class VideoStreamingThread extends Thread {
|
||||
then = System.currentTimeMillis();
|
||||
|
||||
while(!done){
|
||||
receiveImage();
|
||||
//receiveImage();
|
||||
Gdx.app.debug(TAG, CLASS_NAME + ".run() :: Receiving.");
|
||||
receiveUdp();
|
||||
frames++;
|
||||
now = System.currentTimeMillis();
|
||||
delta = now - then;
|
||||
|
Reference in New Issue
Block a user