Monday, April 25, 2011

DCS-950G streaming in Linux

Yesterday I got IPcam DCS-950G. I wanted to connect camera to Motion but it did not get stream. I had to use stupid IE only. I was angry and looking for solution. I found people just as disappointed as I am. I decided to solve this problem.

I went to D-Link and study specification. After that I downgrade camera to DCS-950G_ A1_Firmware_v1.00 because the camera with this old firmware does not need admin authentication for streaming. It's so convenient. I do not have to send with each request the password.

I wanted to write a Groovy script, but I wanted to control the camera from the router, which I do not have Java. That's why I chose Bash.

After hours of testing, I had written a working script:

#!/bin/bash
#

WEBCAM_IP="192.168.0.20"

#######################################################################

#init
{ exec 3<>/dev/tcp/${WEBCAM_IP}/5000; echo -n "ARAGORN_INIT" >&3; cat <&3> /dev/null; } &

sleep 2

#start
exec 3<>/dev/tcp/${WEBCAM_IP}/5001
echo -n "ARAGORN_START#255055293901165#0#1#0" >&3

#data
while :
do
 
    head -c 40 <&3 > header.hex
    BYTES=( `hexdump -v -s 24 -n 4 -e ' 1/1 "%02X " ' header.hex` )      
    HEX="${BYTES[3]}${BYTES[2]}${BYTES[1]}${BYTES[0]}"   
    DATA_LENGTH=`echo "obase=10; ibase=16; ${HEX}" | bc`  
    head -v -c "${DATA_LENGTH}" <&3
                                
done


With MPlayer I can play the stream directly from the camera. Mplayer takes around 5% CPU on a single camera. 
./dcs-950G.bash | mplayer - -demuxer mpeg4es


With ffmpeg I got every second  actual jpeg for Motion.