commiting recent changes
[iceplex.git] / yashout
1 #!/usr/bin/perl
2 #!/usr/bin/perl -w
3 # (the Shout library causes warnings if we use -w)
4 #
5 # yashout
6 #   Usage: yashout filename mountpoint [ port ]
7 #
8 # Copyright (c) 2003 Ivan Kohler
9 # All rights reserved.
10 # This program is free software; you can redistribute it and/or modify it under
11 # the same terms as Perl itself.
12
13 use strict;
14 use subs qw(daemonize);
15 use POSIX qw(:sys_wait_h setsid);
16 use Shout;
17
18 my $encoder_password = 'haze420';
19
20 #read options from commandline
21 my($filename, $mountpoint, $port) = @ARGV;
22
23 my $conn = new Shout (
24   ip          => 'localhost',
25   port        => $port || 8000,
26   mount       => $mountpoint,
27   password    => $encoder_password,
28   icy_compat  => 0,
29   dumpfile    => undef,
30   name        => 'Fonestream audio',
31   url         => 'http://www.fonestream.com/',
32   genre       => 'talk',
33   description => 'The easiest way to webcast.',
34   bitrate     => 16,
35   ispublic    => 0,
36 );
37 $conn->connect or die "Failed to connect: ". $conn->error;
38
39 open(SFILE,"</home/ivan/plexmp3/silence.mp3") or die "Can't open silence.mp3: $!";
40 open(FILE,"<$filename") or die "Can't open $filename: $!";
41
42 daemonize(); #fork/disconnect so plex.pls will return to client
43
44 if ( 0 ) {
45
46   #send silence header to server
47   my( $sbuffer, $sbytes ) = ( '', 0 );
48   while ( ( $sbytes = read( SFILE, $sbuffer, 4096 ) ) > 0 ) {
49     $conn->sendData( $sbuffer ) && next;
50     # error handling???
51     print STDERR "Error while sending: ", $conn->error, "\n";
52     last;
53   } continue {
54     $conn->sleep;
55   }
56
57 }
58
59 sleep 10;
60
61 #spool file to server
62 my( $buffer, $bytes ) = ( '', 0 );
63 while ( ( $bytes = read( FILE, $buffer, 4096 ) ) > 0 ) {
64   $conn->sendData( $buffer ) && next;
65   # error handling???
66   print STDERR "Error while sending: ", $conn->error, "\n";
67   last;
68 } continue {
69   $conn->sleep;
70 }
71
72 $conn->disconnect;
73
74 sub daemonize {
75   chdir "/" or die "Can't chdir to /: $!";
76   open STDIN, '/dev/null'   or die "Can't read /dev/null: $!";
77   defined(my $pid = fork) or die "Can't fork: $!";
78   if ( $pid ) {
79     #print "yashout started with pid $pid\n"; #logging to $log_file\n";
80     #exit unless $pid_file;
81     #my $pidfh = new IO::File ">$pid_file" or exit;
82     #print $pidfh "$pid\n";
83     exit;
84   }
85   open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
86   setsid                    or die "Can't start a new session: $!";
87   open STDERR, '>&STDOUT'   or die "Can't dup stdout: $!";
88
89   #$SIG{__DIE__} = \&_die;
90   #$SIG{__WARN__} = \&_logmsg;
91
92 }
93
94