Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Video Surveillance: Configuring the Client Machine

Learn how to configure the client machine (the one to which the camera is attached), to read the stream from your camera and make it accessible from a remote location via network or internet.


advertisement

This is the first part of a tutorial series regarding video surveillance that will teach you how to set up a remote video surveillance system. In this particular article, you will learn how to configure the client machine (the one to which the camera is attached), to read the stream from your camera and make it accessible from a remote location via network or internet.

Note that there are many software solutions (both free and paid) that can read the camera stream and output it to an IP address, but in this tutorial, I will show you how to create a program in Java that does the same thing.

Required Java Libraries

There are many Java libraries that can be used to stream the video from the camera. The simplest one to use is vlcj, which can be downloaded from here. After downloading the zip file, you will have to include external libraries in your Eclipse or NetBeans project. To do that, right click on your project, select properties, then Java build path, and then the libraries tab. Click the Add external JARs button, browse to the folder where you saved the files, and select the all the JAR files extracted from the zip file.



Vlcj is actually a bridge between java and VLC media player. That means that you will have to install VLC media player and use its two files (libvlc.dll and libvlccore.dll). You could also use VLC media player's interface to stream a video, but if you need some customizations, creating your own program is a good solution.

Creating a Video Streaming Program

Streaming a video is actually sending the output to an IP address and a port on it, so that someone can connect to that IP address and port and view the video. Thanks to vlcj library, the code for stream output is quite simple:

public class WebCam {

    public static void main(String[] args) throws Exception {
                // Add the path to libvlc.dll and libvlccore.dll, so Java can find them
    	NativeLibrary.addSearchPath("libvlc", "C:/Program files/VideoLAN/VLC/");
    	
	// Select a device to capture video from. See more info below.
    	String media = "dshow://";
        
	// Stream options – in this example, the stream would be available
                // on http://192.168.1.100:5555
String options = formatStreamOptions("192.168.1.100", 5555);

System.out.println("Streaming '" + media + "' to '" + options + "'");

MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(args);
HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
mediaPlayer.playMedia(media, options);

Thread.currentThread().join();
    }

    private static String formatStreamOptions(String serverAddress, int serverPort) {
    	StringBuilder sb = new StringBuilder(60);
        
   	sb.append(":dshow-adev=none :dshow-vdev= my-camera-name :dshow-aspect-ratio=4\\:3 :live-caching=200");        
   	sb.append(":sout=#duplicate{dst=std{access=http,mux=ts,");
   	sb.append("dst=");
  	sb.append(serverAddress);
  	sb.append(':');
   	sb.append(serverPort);
  	sb.append("}}");
    	return sb.toString();
    }

}

Let's explain the code. First, Java sometimes cannot find the VLC files, so you need to add the path to them. After that, you have to select a device from which to capture video. However, device name is not used there, but a media resource locator (MRL). MRL is used to uniquely identify a media resource. Your device is usually a default device and can be accessed with "dshow://", but in case that does not work, read more about using MRL here and here. The options string represents VLC commands that will be used.

The above example is using HTTP to stream the video. However, other protocols, such as RTSP (Real-time Streaming Protocol), can also be used. The program that uses RTSP to stream the video would look like this:

public class WebCam {

    public static void main(String[] args) throws Exception {
    	// Add the path to libvlc.dll and libvlccore.dll, so Java can find them
    	NativeLibrary.addSearchPath("libvlc", "C:/Program files/VideoLAN/VLC/");

	// Select a device to capture video from. See more info below.
   	String media = "dshow://";

	// Stream options – in this example, the stream would be available
                // on rtsp://192.168.1.100:8435
String options = formatStreamOptions("192.168.1.100", 8435, "demo");

System.out.println("Streaming '" + media + "' to '" + options + "'");

MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(args);

HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
mediaPlayer.playMedia(media,
            options,
            ":no-sout-rtp-sap",
            ":no-sout-standard-sap",
            ":sout-all",
            ":sout-keep"
        );

        Thread.currentThread().join();
    }

    private static String formatStreamOptions(String serverAddress, int serverPort, String id) {
        StringBuilder sb = new StringBuilder(60);
        sb.append(":sout=#rtp{sdp=rtsp://@");
        sb.append(serverAddress);
        sb.append(':');
        sb.append(serverPort);
        sb.append('/');
        sb.append(id);
        sb.append("}");
        return sb.toString();
    }
}

To be continued…

In the next two parts of this tutorial, you will learn how to:



   
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap