Le programme enregistre un flux video au format asf en provenance d'une camera IP de surveillance en cas de déclenchement d'une alarme. Le programme utilise les API du serveur embarqué de la camera. On peut géré jusqu'à 5 cameras
Source / Exemple :
// Video stream recording from IP camera in case of alarm
//
// Copyright (C) 2011 Jean Francois Quinet
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// This Class manages all the threads for cameras defined in params
// one camera is defined by the IP address and the port number separated by :
// up to 5 cameras are supported
//
//
// The main thread launchs one thread per camera and wait for one connection on port 8089
// To stop all threads connect via telnet on this port and send the word: stop
//
// The child thread start with the Status method which checks the alarm status and calls
// the StreamRecord method in case of alarm. The file is sized to keep about one minutes of
// video stream
//
// The file camera.log keeps a trace of major events
//
// adapt user id and password for access camera to your needs
//
import java.io.*;
import java.net.*;
import java.net.Socket;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.Date;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
public class Camera_Record
{
static String camera;
static long attente;
static int alarm;
static String camid;
static int i;
static String cam[];
public static void main (String args[] )
{
// open socket serveur
ServerSocket socserv=null;
WriteLog.W_log("Camera main thread initialised");
try
{
int listport=8089;
int maxcon=2;
socserv = new ServerSocket(listport,maxcon);
}
catch (IOException e)
{
WriteLog.W_log("open socket server error : " + e);
}
// Check command line paramaters
if (args.length < 1 | args.length > 5 )
{
WriteLog.W_log("Invalid number of paramaters");
System.exit(1);
}
else
{
// Check for existence of : in paramater
int nbArgs = args.length;
cam = args;
Thread[] st=new Thread[5];
for (i=0; i<nbArgs; i++)
{
if (args[i].indexOf(":") == -1)
{
WriteLog.W_log("Invalid paramater : syntax ip address:port number");
}
st[i] = new Thread()
{
public void run()
{
int j = i-1;
Status(cam[j],3000l);
}
};
st[i].setName("status"+i);
st[i].start();
}
}
StringBuffer stop = new StringBuffer("start");
while (stop.toString().equals("start"))
{
Socket clisoc = null;
try
{
// accept connection from client
clisoc = socserv.accept();
BufferedReader inclisoc = new BufferedReader(
new InputStreamReader(clisoc.getInputStream()));
int l = stop.length();
stop.delete(0,l);
int n;
String lineclisoc = " ";
lineclisoc = inclisoc.readLine();
stop.append(lineclisoc);
if (stop.toString().equals("stop"))
{ // if client send stop close connection and end all threads
inclisoc.close();
clisoc.close();
socserv.close();
WriteLog.W_log("stop command received");
System.exit(0);
}
else
{ // else close connection and wait for a new one
inclisoc.close();
clisoc.close();
WriteLog.W_log("wrong command received: " + stop);
l = stop.length();
stop.replace(0,l,"start");
}
}
catch (SocketException e )
{
WriteLog.W_log("Server Socket error : " + e);
}
catch (IOException e )
{
WriteLog.W_log("Server I/O error : " + e);
}
}
}
public static void Status (String camera, long attente)
{
String host = camera.substring(0, camera.indexOf(":") );
String portnb = camera.substring(camera.indexOf(":") +1, camera.length());
int port = (Integer.valueOf(portnb)).intValue();
while (true) // infinite loop
{
try
{
// Create a connection to server Socket
Socket s = new Socket(host, port);
// Create input and output streams to socket
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(s.getOutputStream())),
true);
// Write CGI command to socket output
out.println("GET /get_status.cgi HTTP/1.0\r\n\r\n");
// Read response from socket
String line = in.readLine();
while (line != null)
{
if (line.indexOf("alarm_status") != -1)
{
String walarm = line.substring(17, 18);
alarm = (Integer.valueOf(walarm)).intValue();
}
else
{
if (line.indexOf("alias") != -1)
{
camid = line.substring(11, 22);
}
}
// Read next line
line = in.readLine();
}
// Terminate connection
out.close();
in.close();
s.close();
// alarm status on => video recording
if (alarm != 0)
{
WriteLog.W_log(Thread.currentThread().getName()+ " "+camid+ " alarm on");
StreamRecord record = new StreamRecord(camid,host,port);
}
else
{
Thread.sleep(attente);
}
}
catch (InterruptedException e ) {}
catch (SocketException e )
{
WriteLog.W_log(host + port + "Socket error : " + e);
}
catch (UnknownHostException e )
{
WriteLog.W_log(host + port + "Invalid host");
}
catch (IOException e )
{
WriteLog.W_log(host + port + "I/O error : " + e);
}
} // end while
}
static class WriteLog
{
public static void W_log (String msg)
{
PrintWriter log=null;
try
{
log = new PrintWriter(new FileWriter("camera.log", true));
Date date = new Date();
log.println(date+" "+msg+"\n"); // except Unix & Linux OS add LF char \n
log.close();
}
catch (IOException e ) {}
}
}
static class StreamRecord
{
public StreamRecord (String cam, String host, int port)
{
int filesize=12044772; // filesize
int bytesRead;
int current = 0;
long time = System.currentTimeMillis();
String filename = cam + "_" + time + ".asf";
try
{
// Create a connection to server Socket
Socket s = new Socket(host, port);
// Create input and output streams to socket
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(s.getOutputStream())),
true);
// Write CGI command to socket output
out.println("GET /videostream.asf?user=xxxxxx&pwd=xxxxxx&resolution=32 HTTP/1.0\r\n\r\n");
// receive file
byte [] mybytearray = new byte [filesize];
InputStream in = s.getInputStream();
FileOutputStream fout = new FileOutputStream(filename);
BufferedOutputStream bos = new BufferedOutputStream(fout);
// discard first bytes
in.skip(160);
bytesRead = in.read(mybytearray,0,mybytearray.length);
current = bytesRead;
//
while (current < 12044770)
{
bytesRead =
in.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
}
bos.write(mybytearray, 0 , current);
bos.flush();
bos.close();
s.close();
}
catch (SocketException e )
{
WriteLog.W_log(host + port + "StreamRecord Socket error : " + e);
}
catch (UnknownHostException e )
{
WriteLog.W_log(host + port + "StreamRecord Invalid host");
}
catch (IOException e )
{
WriteLog.W_log(host + port + "StreamRecord I/O error : " + e);
}
}
}
}
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.