File: webcam.cgi

package info (click to toggle)
xawtv 3.107-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,204 kB
  • sloc: ansic: 46,254; perl: 749; sh: 422; cpp: 184; makefile: 126; xml: 74
file content (66 lines) | stat: -rw-r--r-- 1,362 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/perl
#
# send a movie (well, sort of) to the browser.  Works with netscape only,
# other browsers get a static image.  Uses serverpush, i.e. sends the frames
# as multipart/x-mixed-replace elements.
#
# (c) 1999 Gerd Knorr <kraxel@goldbach.in-berlin.de>
#
use strict;

# config
my $IMAGE  = "$ENV{DOCUMENT_ROOT}/images/webcam.jpeg";
my $MAXSEC = 600;	# 10 min timeout

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

undef $/;
$|=1;

my $BO = "wrdlbrmpft";

my $serverpush = ($ENV{HTTP_USER_AGENT} =~ /^Mozilla/ &&
		  $ENV{HTTP_USER_AGENT} !~ /[Cc]ompatible/);

my $start = time;
my @st = stat($IMAGE) or die "stat $IMAGE: $!";
my $mtime = $st[9];

if ($serverpush) {
	print "Content-Type: multipart/x-mixed-replace; boundary=\"$BO\"\r\n";
	print "\r\n";
	print "\r\n--$BO\r\n";
}

for (;;) {
	# read image
	open IMG, "<$IMAGE";
	my $image = <IMG>;
	close IMG;

	# send it
	print  "Content-Type: image/jpeg\r\n";
	printf "Content-Length: %d\r\n",length($image);
	print  "\r\n";
	print $image;
	last unless $serverpush;

	# send multipart border
	if (time - $start > $MAXSEC) {
		print "\r\n--$BO--\r\n";
		last;
	} else {
		print "\r\n--$BO\r\n";
	}

	# wait until there is a new image
	foreach my $i (1 .. $MAXSEC) {
		sleep 1;
		@st = stat($IMAGE);
		if ($st[9] != $mtime) {
			$mtime = $st[9];
			last;
		}
	}
}
exit;