Started fixing the video streaming code.

This commit is contained in:
2014-01-07 18:12:47 -04:30
parent 21a97c0307
commit f843700235
6 changed files with 116 additions and 64 deletions

View File

@@ -24,16 +24,15 @@
android:minSdkVersion="11" android:minSdkVersion="11"
android:targetSdkVersion="19" /> android:targetSdkVersion="19" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.bluetooth" android:required="true" />
<uses-feature android:name="android.hardware.wifi" android:required="true" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.CAMERA" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
@@ -43,8 +42,8 @@
android:allowBackup="true" android:allowBackup="true"
android:icon="@drawable/ic_launcher" android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:screenOrientation="landscape"
android:screenOrientation="landscape" > android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity <activity
android:name="ve.ucv.ciens.ccg.nxtcam.MainActivity" android:name="ve.ucv.ciens.ccg.nxtcam.MainActivity"
android:label="@string/app_name" > android:label="@string/app_name" >
@@ -64,4 +63,4 @@
</activity> </activity>
</application> </application>
</manifest> </manifest>

View File

@@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package ve.ucv.ciens.ccg.nxtcam.network.protocols; package ve.ucv.ciens.ccg.networkdata;
import java.io.Serializable; import java.io.Serializable;
public final class ImageDataMessage implements Serializable{ public final class VideoFrameDataMessage implements Serializable{
private static final long serialVersionUID = 9989L; private static final long serialVersionUID = 9989L;
public static final int magicNumber = 0x10; public static final int magicNumber = 0x10;
@@ -25,7 +25,7 @@ public final class ImageDataMessage implements Serializable{
public int imageHeight; public int imageHeight;
public byte[] data; public byte[] data;
public ImageDataMessage(){ public VideoFrameDataMessage(){
imageWidth = -1; imageWidth = -1;
imageHeight = -1; imageHeight = -1;
data = null; data = null;

View File

@@ -13,17 +13,17 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package ve.ucv.ciens.ccg.nxtcam.network.protocols; package ve.ucv.ciens.ccg.networkdata;
import java.io.Serializable; import java.io.Serializable;
public final class ImageTransferProtocolMessage implements Serializable{ public final class VideoStreamingControlMessage implements Serializable{
private static final long serialVersionUID = 8898L; private static final long serialVersionUID = 8898L;
public static final int magicNumber = 0x20; public static final int magicNumber = 0x20;
public byte message; public byte message;
public ImageTransferProtocolMessage(){ public VideoStreamingControlMessage(){
message = -1; message = -1;
} }
} }

View File

@@ -16,7 +16,7 @@
package ve.ucv.ciens.ccg.nxtcam; package ve.ucv.ciens.ccg.nxtcam;
import ve.ucv.ciens.ccg.nxtcam.camera.CameraPreview; import ve.ucv.ciens.ccg.nxtcam.camera.CameraPreview;
import ve.ucv.ciens.ccg.nxtcam.network.ImageTransferThread; import ve.ucv.ciens.ccg.nxtcam.network.VideoStreamingThread;
import ve.ucv.ciens.ccg.nxtcam.network.LCPThread; import ve.ucv.ciens.ccg.nxtcam.network.LCPThread;
import ve.ucv.ciens.ccg.nxtcam.utils.Logger; import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants; import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
@@ -39,7 +39,7 @@ public class CamActivity extends Activity{
private Camera hwCamera; private Camera hwCamera;
private CameraPreview cPreview; private CameraPreview cPreview;
private CameraSetupTask camSetupTask; private CameraSetupTask camSetupTask;
private ImageTransferThread imThread; private VideoStreamingThread imThread;
private LCPThread botThread; private LCPThread botThread;
private String serverIp; private String serverIp;
@@ -54,7 +54,7 @@ public class CamActivity extends Activity{
Intent intent = getIntent(); Intent intent = getIntent();
serverIp = intent.getStringExtra("address"); serverIp = intent.getStringExtra("address");
imThread = new ImageTransferThread(serverIp); imThread = new VideoStreamingThread(serverIp);
imThread.start(); imThread.start();
} }

View File

@@ -22,31 +22,33 @@ import java.io.ObjectOutputStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import ve.ucv.ciens.ccg.networkdata.VideoFrameDataMessage;
import ve.ucv.ciens.ccg.networkdata.VideoStreamingControlMessage;
import ve.ucv.ciens.ccg.nxtcam.camera.CameraImageMonitor; import ve.ucv.ciens.ccg.nxtcam.camera.CameraImageMonitor;
import ve.ucv.ciens.ccg.nxtcam.network.protocols.ImageDataMessage; import ve.ucv.ciens.ccg.nxtcam.network.protocols.VideoStreamingProtocol;
import ve.ucv.ciens.ccg.nxtcam.network.protocols.ImageTransferProtocol;
import ve.ucv.ciens.ccg.nxtcam.network.protocols.ImageTransferProtocolMessage;
import ve.ucv.ciens.ccg.nxtcam.utils.Logger; import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants; import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
import android.graphics.ImageFormat; import android.graphics.ImageFormat;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.YuvImage; import android.graphics.YuvImage;
public class ImageTransferThread extends Thread{ public class VideoStreamingThread extends Thread{
private final String TAG = "IM_THREAD"; private final String TAG = "IM_THREAD";
private final String CLASS_NAME = ImageTransferThread.class.getSimpleName(); private final String CLASS_NAME = VideoStreamingThread.class.getSimpleName();
private enum thread_state_t {WAIT_FOR_ACK, WAIT_FOR_READY, CAN_SEND, END_STREAM}; private enum ProtocolState_t {WAIT_FOR_ACK, WAIT_FOR_READY, CAN_SEND, END_STREAM};
private boolean pause, done; private boolean pause, done;
private Object threadPauseMonitor; private Object threadPauseMonitor;
private CameraImageMonitor camMonitor; private CameraImageMonitor camMonitor;
private Socket socket; private Socket socket;
private ObjectOutputStream writer; private ObjectOutputStream writer;
private ObjectInputStream reader; private String serverIp; private ObjectInputStream reader;
private thread_state_t threadState; private String serverIp;
private ProtocolState_t protocolState;
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
public ImageTransferThread(String serverIp){ public VideoStreamingThread(String serverIp){
this.serverIp = serverIp; this.serverIp = serverIp;
pause = false; pause = false;
done = false; done = false;
@@ -55,14 +57,14 @@ public class ImageTransferThread extends Thread{
writer = null; writer = null;
reader = null; reader = null;
camMonitor = CameraImageMonitor.getInstance(); camMonitor = CameraImageMonitor.getInstance();
threadState = thread_state_t.WAIT_FOR_READY; protocolState = ProtocolState_t.WAIT_FOR_READY;
} }
public void run(){ /*public void run(){
byte[] image; byte[] image;
Object auxiliary; Object tmpMessage;
ImageTransferProtocolMessage simpleMessage; VideoStreamingControlMessage controlMessage;
ImageDataMessage imageMessage; VideoFrameDataMessage dataMessage;
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
connectToServer(); connectToServer();
@@ -73,12 +75,12 @@ public class ImageTransferThread extends Thread{
}else{ }else{
while(!done){ while(!done){
// checkPause(); // checkPause();
switch(threadState){ switch(protocolState){
case WAIT_FOR_READY: case WAIT_FOR_READY:
Logger.log_d(TAG, CLASS_NAME + ".run() :: Reading message from server. State is WAIT_FOR_READY."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Reading message from server. State is WAIT_FOR_READY.");
auxiliary = readMessage(); tmpMessage = readMessage();
if(!validateImageTransferProtocolMessage(auxiliary)){ if(!validateImageTransferProtocolMessage(tmpMessage)){
// If the message received is not valid then send an UNRECOGNIZED message to the server. // If the message received is not valid then send an UNRECOGNIZED message to the server.
Logger.log_d(TAG, CLASS_NAME + ".run() :: Received an unrecognized protocol message. State WAIT_FOR_READY."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Received an unrecognized protocol message. State WAIT_FOR_READY.");
Logger.log_d(TAG, CLASS_NAME + ".run() :: Sending UNRECOGNIZED message to server."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Sending UNRECOGNIZED message to server.");
@@ -86,24 +88,24 @@ public class ImageTransferThread extends Thread{
}else{ }else{
// Else if the message passed the validity check then proceed to the next protocol state. // Else if the message passed the validity check then proceed to the next protocol state.
simpleMessage = (ImageTransferProtocolMessage)auxiliary; controlMessage = (VideoStreamingControlMessage)tmpMessage;
if(simpleMessage.message == ImageTransferProtocol.FLOW_CONTROL_CONTINUE){ if(controlMessage.message == VideoStreamingProtocol.FLOW_CONTROL_CONTINUE){
Logger.log_d(TAG, CLASS_NAME + ".run() :: Received FLOW_CONTROL_CONTINUE from the server."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Received FLOW_CONTROL_CONTINUE from the server.");
Logger.log_d(TAG, CLASS_NAME + ".run() :: Transitioning from WAIT_FOR_READY to CAN_SEND."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Transitioning from WAIT_FOR_READY to CAN_SEND.");
threadState = thread_state_t.CAN_SEND; protocolState = ProtocolState_t.CAN_SEND;
}else if(simpleMessage.message == ImageTransferProtocol.STREAM_CONTROL_END){ }else if(controlMessage.message == VideoStreamingProtocol.STREAM_CONTROL_END){
Logger.log_d(TAG, CLASS_NAME + ".run() :: Received STREAM_CONTROL_END from the server."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Received STREAM_CONTROL_END from the server.");
Logger.log_d(TAG, CLASS_NAME + ".run() :: Transitioning from WAIT_FOR_READY to END_STREAM."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Transitioning from WAIT_FOR_READY to END_STREAM.");
threadState = thread_state_t.END_STREAM; protocolState = ProtocolState_t.END_STREAM;
} }
} }
break; break;
case WAIT_FOR_ACK: case WAIT_FOR_ACK:
Logger.log_d(TAG, CLASS_NAME + ".run() :: Reading message from server. State is WAIT_FOR_ACK."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Reading message from server. State is WAIT_FOR_ACK.");
auxiliary = readMessage(); tmpMessage = readMessage();
if(!validateImageTransferProtocolMessage(auxiliary)){ if(!validateImageTransferProtocolMessage(tmpMessage)){
// If the message received is not valid then send an UNRECOGNIZED message to the server. // If the message received is not valid then send an UNRECOGNIZED message to the server.
Logger.log_d(TAG, CLASS_NAME + ".run() :: Received an unrecognized protocol message. State WAIT_FOR_ACK."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Received an unrecognized protocol message. State WAIT_FOR_ACK.");
Logger.log_d(TAG, CLASS_NAME + ".run() :: Sending UNRECOGNIZED message to server."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Sending UNRECOGNIZED message to server.");
@@ -111,17 +113,17 @@ public class ImageTransferThread extends Thread{
}else{ }else{
// Else if the message passed the validity check then proceed to the next protocol state. // Else if the message passed the validity check then proceed to the next protocol state.
simpleMessage = (ImageTransferProtocolMessage)auxiliary; controlMessage = (VideoStreamingControlMessage)tmpMessage;
if(simpleMessage.message == ImageTransferProtocol.ACK_SEND_NEXT){ if(controlMessage.message == VideoStreamingProtocol.ACK_SEND_NEXT){
Logger.log_d(TAG, CLASS_NAME + ".run() :: Received ACK_SEND_NEXT from the server."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Received ACK_SEND_NEXT from the server.");
Logger.log_d(TAG, CLASS_NAME + ".run() :: Transitioning from WAIT_FOR_ACK to CAN_SEND."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Transitioning from WAIT_FOR_ACK to CAN_SEND.");
threadState = thread_state_t.CAN_SEND; protocolState = ProtocolState_t.CAN_SEND;
}else if(simpleMessage.message == ImageTransferProtocol.ACK_WAIT){ }else if(controlMessage.message == VideoStreamingProtocol.ACK_WAIT){
Logger.log_d(TAG, CLASS_NAME + ".run() :: Received ACK_WAIT from the server."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Received ACK_WAIT from the server.");
Logger.log_d(TAG, CLASS_NAME + ".run() :: Transitioning from WAIT_FOR_ACK to WAIT_FOR_READY."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Transitioning from WAIT_FOR_ACK to WAIT_FOR_READY.");
threadState = thread_state_t.WAIT_FOR_READY; protocolState = ProtocolState_t.WAIT_FOR_READY;
}else if(simpleMessage.message == ImageTransferProtocol.STREAM_CONTROL_END){ }else if(controlMessage.message == VideoStreamingProtocol.STREAM_CONTROL_END){
threadState = thread_state_t.END_STREAM; protocolState = ProtocolState_t.END_STREAM;
} }
} }
break; break;
@@ -139,15 +141,15 @@ public class ImageTransferThread extends Thread{
// Prepare the message for sending. // Prepare the message for sending.
Logger.log_d(TAG, CLASS_NAME + ".run() :: Building message."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Building message.");
imageMessage = new ImageDataMessage(); dataMessage = new VideoFrameDataMessage();
imageMessage.imageWidth = imageSize.width(); dataMessage.imageWidth = imageSize.width();
imageMessage.imageHeight = imageSize.height(); dataMessage.imageHeight = imageSize.height();
imageMessage.data = outputStream.toByteArray(); dataMessage.data = outputStream.toByteArray();
// Send the message. // Send the message.
try{ try{
Logger.log_d(TAG, CLASS_NAME + ".run() :: Sending message."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Sending message.");
writer.writeObject(imageMessage); writer.writeObject(dataMessage);
}catch(IOException io){ }catch(IOException io){
Logger.log_e(TAG, CLASS_NAME + ".run() :: Error sending image to the server: " + io.getMessage()); Logger.log_e(TAG, CLASS_NAME + ".run() :: Error sending image to the server: " + io.getMessage());
} }
@@ -157,12 +159,12 @@ public class ImageTransferThread extends Thread{
yuvImage = null; yuvImage = null;
image = null; image = null;
outputStream.reset(); outputStream.reset();
imageMessage = null; dataMessage = null;
imageSize = null; imageSize = null;
Logger.log_d(TAG, CLASS_NAME + ".run() :: Image data successfuly sent."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Image data successfuly sent.");
Logger.log_d(TAG, CLASS_NAME + ".run() :: Transitioning from CAN_SEND to WAIT_FOR_ACK."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Transitioning from CAN_SEND to WAIT_FOR_ACK.");
threadState = thread_state_t.WAIT_FOR_ACK; protocolState = ProtocolState_t.WAIT_FOR_ACK;
break; break;
case END_STREAM: case END_STREAM:
@@ -175,6 +177,57 @@ public class ImageTransferThread extends Thread{
} }
} }
Logger.log_d(TAG, CLASS_NAME + ".run() :: Thread finish reached."); Logger.log_d(TAG, CLASS_NAME + ".run() :: Thread finish reached.");
}*/
public void run(){
connectToServer();
if(!socket.isConnected()){
Logger.log_e(TAG, CLASS_NAME + ".run() :: Not connected to a server. Finishing thread.");
return;
}else{
while(!done){
sendImage();
}
}
Logger.log_d(TAG, CLASS_NAME + ".run() :: Thread finish reached.");
}
private void sendImage(){
byte[] image;
YuvImage yuvImage;
VideoFrameDataMessage message;
Rect imageSize;
image = camMonitor.getImageData();
imageSize = camMonitor.getImageParameters();
// Compress the image as Jpeg.
Logger.log_d(TAG, CLASS_NAME + ".sendImage() :: Compressing image.");
yuvImage = new YuvImage(image, ImageFormat.NV21, imageSize.width(), imageSize.height(), null);
yuvImage.compressToJpeg(imageSize, 90, outputStream);
Logger.log_d(TAG, CLASS_NAME + ".sendImage() :: Building message.");
message = new VideoFrameDataMessage();
message.data = outputStream.toByteArray();
message.imageWidth = imageSize.width();
message.imageHeight = imageSize.height();
try{
Logger.log_d(TAG, CLASS_NAME + ".sendImage() :: Sending message.");
writer.writeObject(message);
writer.flush();
Logger.log_e(TAG, CLASS_NAME + ".sendImage() :: Message sent successfully: ");
}catch(IOException io){
Logger.log_e(TAG, CLASS_NAME + ".sendImage() :: Error sending image to the server: " + io.getMessage());
}finally{
Logger.log_d(TAG, CLASS_NAME + ".sendImage() :: Cleaning.");
outputStream.reset();
}
} }
private void connectToServer(){ private void connectToServer(){
@@ -215,35 +268,35 @@ public class ImageTransferThread extends Thread{
} }
private Object readMessage(){ private Object readMessage(){
Object auxiliary; Object tmpMessage;
// Read a message from the server stream. // Read a message from the server stream.
try{ try{
auxiliary = reader.readObject(); tmpMessage = reader.readObject();
}catch(IOException io){ }catch(IOException io){
Logger.log_e(TAG, CLASS_NAME + ".run() :: IOException when reading in WAIT_FOR_READY state."); Logger.log_e(TAG, CLASS_NAME + ".run() :: IOException when reading in WAIT_FOR_READY state.");
auxiliary = null; tmpMessage = null;
return null; return null;
}catch(ClassNotFoundException cn){ }catch(ClassNotFoundException cn){
Logger.log_e(TAG, CLASS_NAME + ".run() :: ClassNotFoundException when reading in WAIT_FOR_READY state."); Logger.log_e(TAG, CLASS_NAME + ".run() :: ClassNotFoundException when reading in WAIT_FOR_READY state.");
auxiliary = null; tmpMessage = null;
return null; return null;
} }
return auxiliary; return tmpMessage;
} }
private boolean validateImageTransferProtocolMessage(Object message){ private boolean validateImageTransferProtocolMessage(Object message){
if(message != null && message instanceof ImageTransferProtocolMessage) if(message != null && message instanceof VideoStreamingControlMessage)
return true; return true;
else else
return false; return false;
} }
private void sendUnrecognizedMessage(){ private void sendUnrecognizedMessage(){
ImageTransferProtocolMessage message = new ImageTransferProtocolMessage(); VideoStreamingControlMessage message = new VideoStreamingControlMessage();
message.message = ImageTransferProtocol.UNRECOGNIZED; message.message = VideoStreamingProtocol.UNRECOGNIZED;
try{ try{
writer.writeObject(message); writer.writeObject(message);

View File

@@ -15,7 +15,7 @@
*/ */
package ve.ucv.ciens.ccg.nxtcam.network.protocols; package ve.ucv.ciens.ccg.nxtcam.network.protocols;
public final class ImageTransferProtocol{ public final class VideoStreamingProtocol{
public static final byte STREAM_CONTROL_END = 0x10; public static final byte STREAM_CONTROL_END = 0x10;
public static final byte ACK_SEND_NEXT = 0x20; public static final byte ACK_SEND_NEXT = 0x20;
public static final byte ACK_WAIT = 0x30; public static final byte ACK_WAIT = 0x30;