-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcam_proxy.pl
151 lines (116 loc) · 3.25 KB
/
cam_proxy.pl
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/perl
# this is the part of surveillance scripts
# This particular is a camera proxy handler.
use warnings;
use strict;
use Getopt::Long;
use Carp;
#these are from config file:
my %cams;
my %o; # options
# local vars:
my $debug;
my $log;
my $logfile;
my $config_path = 'cam_proxy.config'; # default
my $show_help = 0;
####################################################
GetOptions (
'c=s' => \$config_path,
'd' => \$debug,
'h' => \$show_help,
'l=i' => \$log,
) or die "GetOptions: $!";
load_config( $config_path );
if ( $#ARGV < 0 || $show_help ) # must be camera id after options
{
help(0);
}
my $cam_id = $ARGV[0];
exists $cams{ $cam_id } or report_and_exit( "!ERROR! invalid camID: $cam_id" );
$cams{ $cam_id }->{ 'proto' } ne 'rtsp' and report_and_exit( "!ERROR! camera: $cam_id is not rtsp" );
if ( ! exists( $cams{ $cam_id }->{ 'proxy port' } ) || ! defined( $cams{ $cam_id }->{ 'proxy port' } ) )
{
report_and_exit( "!ERROR! camera: $cam_id bad port" );
}
#live555ProxyServer [-v|-V] [-t|-T <http-port>] [-p <rtspServer-port>] [-u <username> <password>] [-R] [-U <username-for-REGISTER> <password-for-REGISTER>] <rtsp-url-1> ... <rtsp-url-n>
my $cmd = '/bin/live555ProxyServer -t -p ' . $cams{ $cam_id }->{ 'proxy port' };
if ( $log == 1 )
{
$cmd .= ' -v';
}
elsif ( $log > 1 )
{
$cmd .= ' -V';
}
$cmd .= " '" . $cams{ $cam_id }->{ 'source' } . "'";
$log > 0 and $cmd .= " > '" . $o{ 'records base dir' } . '/cam_proxy_' . $cam_id . '-' . get_times( 'dt' ) . '.log\' 2>&1';
print 'Starting: ', $cmd, "\n";
$debug and exit(0);
system $cmd;
exit($!);
#####################################
sub help
{
print "Use: cam_proxy.pl [options] <camera id>\n";
print "Options:\n\t-c <config file path>\n\t-d - debug\n\t-l - log verbosity level\n\t";
print "Known camera IDs: ", join(', ', keys %cams), "\n";
exit(1);
}
#############################################################
sub report_and_exit
{
if ( $debug )
{
print join "\n", @_;
}
else
{
open M, '|-', '/sbin/sendmail ' . $o{ 'whine email' } or croak "sendmail: $!/$?";
print M "From: cam_proxy for '$cam_id'\n";
print M "To: " . $o{ 'whine email' } . "\n";
print M "\n\n" . join("\n", @_);
if ( open( L, '<', $log) )
{
print M "\nLog file follows:\n";
while(<L>)
{
print M $_;
}
close L;
}
close M;
if ( $log )
{
open L, '>>', $logfile or croak "$logfile: $!";
print L join("\n", @_);
close L;
}
}
exit($? > $! ? $? : $!);
}
#############################################################
sub load_config
{
my $file = $_[0];
if ( ! -r $file )
{
$file =~ m?/? and print "Can't open config: $file" and help();
$file = dirname(__FILE__) . '/' . $_[0];
-r $file or $file = '/etc/smarthome/cams/' . $_[0];
}
$debug and print "+ Loading config: $file\n";
#unless ($return = do $file)
#{
# croak "couldn't parse $file: $@" if $@;
# croak "couldn't do $file: $!" unless defined $return;
# croak "couldn't run $file" unless $return;
#}
my $cfg;
open C, '<', $file or die "config file: '$file': $!";
sysread( C, $cfg, 999999 ) or die "config is empty?";
close C;
eval $cfg;
defined( $debug ) or $debug = 0;
defined( $log ) or $log = 0;
}