Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 110 lines (92 sloc) 2.646 kb
#!/usr/bin/perl
# RPD - Radio Playing Daemon
# Copyright (C) 2010 Magnus Woldrich <trapd00r@trapd00r.se>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
our $APP = 'Radio Playing Daemon';
our $VERSION = 0.4;
use strict;
use RPD::URI qw(geturi listchans);
my $chan = shift // 'moh'; # Masters of Hardcore
my $log = "$ENV{HOME}/.mplayer/rpd.log";
my $fifo = "$ENV{HOME}/.mplayer/mplayer.fifo";
my $pidfile = '/tmp/rpd.pid';
my $player = 'mplayer';
my @playopt = (' -cache 400', '-quiet', '-identify',
'-idle', '-input', "file=$fifo"
);
my $chan_url = undef;
if(-f $pidfile) {
print "RPD is already running, exiting...\n";
exit 1;
}
if(!-p $fifo) {
require POSIX;
POSIX::mkfifo($fifo, 0666) or croak("Cant mkfifo $fifo: $!");
}
if($chan eq '-l' or $chan eq '--list') {
listchans();
exit 0;
}
if(defined(geturi($chan))) {
$chan_url = geturi($chan);
print $chan_url;
play($chan_url);
}
else {
if(!$chan) { # no args
usage();
}
else { # arg is invalid
# We have already shifted of a value from @ARGV
printf("Invalid argument%s\n", (@ARGV > 0) ? 's' : '');
}
}
sub play {
my $station = shift(@_);
croak("Provide a station") unless($station);
daemonize();
exec($player, @playopt, $station);
}
sub usage {
print << "USAGE";
$APP $VERSION
Usage: rpd [OPTIONS] (STATION)
OPTIONS
-l, --list list channels
USAGE
}
sub daemonize {
use POSIX 'setsid';
my $PID = fork();
exit(0) if($PID); # parent
exit(1) if(!defined($PID)); # out of resources
setsid();
$PID = fork();
exit(1) if(!defined($PID));
if($PID) {
waitpid($PID, 0);
unlink($pidfile) and print "\n\nMMMMMMMMMMMMM\n\n";
exit(0);
}
elsif($PID == 0) {
open(my $pid, '>', $pidfile) or croak("Cant create pidfile $pidfile: $!");
print $pid $$;
close($pid);
open(STDOUT, '>', $log);
open(STDERR, '>', '/dev/null');
open(STDIN, '<', '/dev/null');
}
}
Jump to Line
Something went wrong with that request. Please try again.