#!/usr/bin/perl
# Live video CGI script
# Written 28/03/96 by Andrew Pam
# (c) 1996 Serious Cybernetics
# http://www.sericyb.com.au/sc/

$temp = "/tmp/";	# Temporary directory

# Signal handler: clean up and exit
sub handler
{
	# Clean up temporary files
	unlink "$temp$$-00000.rgb", "$temp$$.jpg";
	exit(0);
}

# Initialisation
unlink "$temp$$-00000.rgb";
$SIG{"PIPE"} = "handler";
undef $/;	# process entire files
$| = 1;		# unbuffer output
if ($spush = $ENV{"HTTP_USER_AGENT"} =~ /^Mozilla/)
{
	$zoom = "-z 1/4";	# zoom to 1/4 size (160x120)
	print "Content-Type: multipart/x-mixed-replace;boundary=boundary\n";
	print "\n--boundary\n";
}
else
{
	$zoom = "-z 1/2";	# zoom to 1/2 size (320x240)
}

LOOP:
print "Content-Type: image/jpeg\n";
system "/usr/sbin/vidtomem $zoom -f $temp$$";
# note that dmconvert outputs a newline at this point!
system "/usr/sbin/dmconvert -f jfif $temp$$-00000.rgb $temp$$.jpg";

open(IMAGE, "$temp$$.jpg");
print <IMAGE>;
close(IMAGE);
print "\n--boundary\n" if $spush;

# Clean up temporary files
unlink "$temp$$-00000.rgb", "$temp$$.jpg";

if ($spush)
{
	sleep 1;	# limit max bandwidth
	goto LOOP;
}
