2013-12-13 09:19:49 -04:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2013 Miguel Angel Astor Romero
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
2013-11-05 11:44:26 -04:30
|
|
|
package ve.ucv.ciens.ccg.nxtcam.network;
|
|
|
|
|
|
2013-12-17 17:03:02 -04:30
|
|
|
import java.io.ByteArrayOutputStream;
|
2013-11-05 11:44:26 -04:30
|
|
|
import java.io.IOException;
|
2013-12-17 17:03:02 -04:30
|
|
|
import java.io.ObjectOutputStream;
|
2014-01-15 14:49:58 -04:30
|
|
|
import java.net.DatagramPacket;
|
|
|
|
|
import java.net.DatagramSocket;
|
2013-11-05 11:44:26 -04:30
|
|
|
import java.net.InetAddress;
|
|
|
|
|
import java.net.Socket;
|
2014-01-15 14:49:58 -04:30
|
|
|
import java.net.UnknownHostException;
|
2013-11-05 11:44:26 -04:30
|
|
|
|
2014-01-07 18:12:47 -04:30
|
|
|
import ve.ucv.ciens.ccg.networkdata.VideoFrameDataMessage;
|
2013-11-05 11:44:26 -04:30
|
|
|
import ve.ucv.ciens.ccg.nxtcam.camera.CameraImageMonitor;
|
2013-11-06 11:49:11 -04:30
|
|
|
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
|
2013-11-05 11:44:26 -04:30
|
|
|
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
|
2013-12-17 17:03:02 -04:30
|
|
|
import android.graphics.ImageFormat;
|
|
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.graphics.YuvImage;
|
2013-11-05 11:44:26 -04:30
|
|
|
|
2014-01-07 18:12:47 -04:30
|
|
|
public class VideoStreamingThread extends Thread{
|
2013-11-05 11:44:26 -04:30
|
|
|
private final String TAG = "IM_THREAD";
|
2014-01-07 18:12:47 -04:30
|
|
|
private final String CLASS_NAME = VideoStreamingThread.class.getSimpleName();
|
2013-11-05 11:44:26 -04:30
|
|
|
|
2014-02-11 17:52:29 -04:30
|
|
|
private boolean pause, done;
|
2013-11-05 11:44:26 -04:30
|
|
|
private Object threadPauseMonitor;
|
|
|
|
|
private CameraImageMonitor camMonitor;
|
|
|
|
|
private Socket socket;
|
2014-01-15 14:49:58 -04:30
|
|
|
DatagramSocket udpSocket;
|
2014-01-07 18:12:47 -04:30
|
|
|
private String serverIp;
|
|
|
|
|
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
2013-11-05 11:44:26 -04:30
|
|
|
|
2014-01-07 18:12:47 -04:30
|
|
|
public VideoStreamingThread(String serverIp){
|
2014-01-08 15:21:37 -04:30
|
|
|
super("Video Streaming Thread");
|
2013-11-06 11:49:11 -04:30
|
|
|
this.serverIp = serverIp;
|
2013-11-05 11:44:26 -04:30
|
|
|
done = false;
|
2014-02-11 17:52:29 -04:30
|
|
|
pause = false;
|
2013-11-05 11:44:26 -04:30
|
|
|
threadPauseMonitor = new Object();
|
|
|
|
|
socket = null;
|
|
|
|
|
camMonitor = CameraImageMonitor.getInstance();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-07 18:12:47 -04:30
|
|
|
public void run(){
|
2014-01-20 12:59:51 -04:30
|
|
|
|
2014-01-15 14:49:58 -04:30
|
|
|
try{
|
|
|
|
|
udpSocket = new DatagramSocket();
|
2014-02-11 17:52:29 -04:30
|
|
|
|
2014-01-15 14:49:58 -04:30
|
|
|
}catch(IOException io){
|
|
|
|
|
Logger.log_e(TAG, CLASS_NAME + ".run() :: IOException received creating socket " + io.getMessage());
|
|
|
|
|
System.exit(1);
|
|
|
|
|
}
|
2014-01-07 18:12:47 -04:30
|
|
|
|
|
|
|
|
|
2014-02-11 17:52:29 -04:30
|
|
|
while(!done){
|
|
|
|
|
|
|
|
|
|
synchronized (threadPauseMonitor) {
|
|
|
|
|
while(pause){
|
|
|
|
|
try{ threadPauseMonitor.wait(); }catch(InterruptedException ie){ };
|
|
|
|
|
}
|
2014-01-07 18:12:47 -04:30
|
|
|
}
|
2014-02-11 17:52:29 -04:30
|
|
|
|
|
|
|
|
sendUdp();
|
|
|
|
|
try{
|
|
|
|
|
sleep(50L);
|
|
|
|
|
}catch(InterruptedException ie){}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-07 18:12:47 -04:30
|
|
|
|
|
|
|
|
Logger.log_d(TAG, CLASS_NAME + ".run() :: Thread finish reached.");
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-15 14:49:58 -04:30
|
|
|
private byte[] int2ByteArray(int integer){
|
|
|
|
|
int shift;
|
|
|
|
|
byte[] array = new byte[4];
|
|
|
|
|
for(int i = 0; i < 4; i++){
|
|
|
|
|
shift = i << 3;
|
|
|
|
|
array[3 - i] = (byte)((integer & (0xff << shift)) >>> shift);
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
2014-01-20 12:59:51 -04:30
|
|
|
|
2014-01-15 14:49:58 -04:30
|
|
|
private void sendUdp(){
|
|
|
|
|
int bufferSize;
|
|
|
|
|
byte[] image;
|
|
|
|
|
byte[] buffer;
|
|
|
|
|
byte[] size;
|
|
|
|
|
DatagramPacket packet;
|
|
|
|
|
VideoFrameDataMessage message;
|
|
|
|
|
Rect imageSize;
|
|
|
|
|
YuvImage yuvImage;
|
2014-01-20 12:59:51 -04:30
|
|
|
|
2014-01-15 14:49:58 -04:30
|
|
|
image = camMonitor.getImageData();
|
|
|
|
|
imageSize = camMonitor.getImageParameters();
|
|
|
|
|
|
|
|
|
|
yuvImage = new YuvImage(image, ImageFormat.NV21, imageSize.width(), imageSize.height(), null);
|
2014-03-18 18:02:58 -04:30
|
|
|
yuvImage.compressToJpeg(imageSize, 90, outputStream);
|
2014-01-20 12:59:51 -04:30
|
|
|
|
2014-01-15 14:49:58 -04:30
|
|
|
message = new VideoFrameDataMessage();
|
|
|
|
|
message.data = outputStream.toByteArray();
|
|
|
|
|
message.imageWidth = imageSize.width();
|
|
|
|
|
message.imageHeight = imageSize.height();
|
2014-01-20 12:59:51 -04:30
|
|
|
|
2014-01-15 14:49:58 -04:30
|
|
|
outputStream.reset();
|
2014-01-20 12:59:51 -04:30
|
|
|
|
2014-01-15 14:49:58 -04:30
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
|
|
|
|
oos.writeObject(message);
|
|
|
|
|
oos.flush();
|
|
|
|
|
oos.reset();
|
|
|
|
|
}catch(IOException io){
|
|
|
|
|
Logger.log_e(TAG, CLASS_NAME + ".sendUdp() :: IOException received while serializing." + io.getMessage());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer = baos.toByteArray();
|
|
|
|
|
baos.reset();
|
|
|
|
|
bufferSize = buffer.length;
|
|
|
|
|
size = int2ByteArray(bufferSize);
|
|
|
|
|
|
|
|
|
|
try{
|
2014-02-10 15:24:34 -04:30
|
|
|
packet = new DatagramPacket(size, 4, InetAddress.getByName(serverIp), ProjectConstants.VIDEO_STREAMING_PORT);
|
2014-01-15 14:49:58 -04:30
|
|
|
udpSocket.send(packet);
|
|
|
|
|
|
2014-02-10 15:24:34 -04:30
|
|
|
packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(serverIp), ProjectConstants.VIDEO_STREAMING_PORT);
|
2014-01-15 14:49:58 -04:30
|
|
|
udpSocket.send(packet);
|
|
|
|
|
|
|
|
|
|
}catch(UnknownHostException uo){
|
|
|
|
|
Logger.log_e(TAG, CLASS_NAME + ".sendUdp() :: UnknownHostException received " + uo.getMessage());
|
|
|
|
|
return;
|
|
|
|
|
}catch(IOException io){
|
|
|
|
|
Logger.log_e(TAG, CLASS_NAME + ".sendUdp() :: IOException buffer size is " + Integer.toString(buffer.length));
|
|
|
|
|
Logger.log_e(TAG, CLASS_NAME + ".sendUdp() :: IOException received while sending " + io.getMessage());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-20 12:59:51 -04:30
|
|
|
|
2013-11-06 11:49:11 -04:30
|
|
|
public void disconnect(){
|
|
|
|
|
if(socket != null && socket.isConnected()){
|
|
|
|
|
try{
|
2013-12-18 10:54:47 -04:30
|
|
|
Logger.log_d(TAG, CLASS_NAME + ".disconnect() :: Closing socket.");
|
2013-11-06 11:49:11 -04:30
|
|
|
socket.close();
|
|
|
|
|
}catch (IOException io) {
|
2013-11-21 10:13:38 -04:30
|
|
|
Logger.log_e(TAG, CLASS_NAME + ".connectToServer() :: " + io.getMessage());
|
2013-11-06 11:49:11 -04:30
|
|
|
}
|
2013-11-05 11:44:26 -04:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized void finish(){
|
|
|
|
|
done = true;
|
2013-11-21 10:13:38 -04:30
|
|
|
Logger.log_i(TAG, CLASS_NAME + ".finish() :: Finishing thread.");
|
2013-11-05 11:44:26 -04:30
|
|
|
}
|
|
|
|
|
|
2014-02-11 17:52:29 -04:30
|
|
|
public void pauseThread(){
|
|
|
|
|
synchronized (threadPauseMonitor) {
|
|
|
|
|
pause = true;
|
2013-12-17 17:03:02 -04:30
|
|
|
}
|
2013-11-21 10:13:38 -04:30
|
|
|
Logger.log_d(TAG, CLASS_NAME + ".pauseThread() :: Pausing thread.");
|
2013-11-05 11:44:26 -04:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized void resumeThread(){
|
2013-11-21 10:13:38 -04:30
|
|
|
Logger.log_d(TAG, CLASS_NAME + ".resumeThread() :: Resuming thread.");
|
2013-11-05 11:44:26 -04:30
|
|
|
synchronized (threadPauseMonitor) {
|
2014-02-11 17:52:29 -04:30
|
|
|
pause = false;
|
2013-11-05 11:44:26 -04:30
|
|
|
threadPauseMonitor.notifyAll();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isConnected(){
|
2013-11-06 11:49:11 -04:30
|
|
|
if(socket != null && socket.isConnected())
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2013-11-05 11:44:26 -04:30
|
|
|
}
|
|
|
|
|
}
|