| 1 | package App::Pimpd::Player; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | BEGIN { | ||
| 5 | use Exporter; | ||
| 6 | use vars qw(@ISA @EXPORT); | ||
| 7 | @ISA = qw(Exporter); | ||
| 8 | @EXPORT = qw( | ||
| 9 | play | ||
| 10 | stop | ||
| 11 | player_init | ||
| 12 | player_daemonize | ||
| 13 | player_destruct | ||
| 14 | ); | ||
| 15 | } | ||
| 16 | |||
| 17 | my $DEBUG = $ENV{PIMPD2_DEBUG}; | ||
| 18 | |||
| 19 | use Carp; | ||
| 20 | use App::Pimpd; | ||
| 21 | |||
| 22 | # NOTE To config | ||
| 23 | my $pidfile_pimpd = '/tmp/pimpd2.pid'; | ||
| 24 | my $pidfile_player = '/tmp/pimpd2-player.pid'; | ||
| 25 | my $player_tmp_log = '/tmp/pimpd2.log'; | ||
| 26 | |||
| 27 | my $cmdline = player_cmdline(); | ||
| 28 | |||
| 29 | sub player_init { | ||
| 30 | if(@_) { | ||
| 31 | $cmdline = "@_"; | ||
| 32 | } | ||
| 33 | my $fails = 0; | ||
| 34 | |||
| 35 | # Not playing! | ||
| 36 | if(! -e $player_tmp_log) { | ||
| 37 | player_daemonize($player_tmp_log); | ||
| 38 | exec($cmdline); | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | open(my $fh, '<', $player_tmp_log); | ||
| 42 | while(<$fh>) { | ||
| 43 | if(/Exiting\.\.\. \(End of file\)/m) { | ||
| 44 | $fails++; | ||
| 45 | # fulhack. Time::HiRes | ||
| 46 | select(undef, undef, undef, 0.50); | ||
| 47 | # 10s of trying | ||
| 48 | if($fails == 20) { | ||
| 49 | last; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | close($fh); | ||
| 54 | } | ||
| 55 | if($fails == 20) { | ||
| 56 | unlink($player_tmp_log); | ||
| 57 | #player_destruct(); | ||
| 58 | return 0; | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | player_daemonize($player_tmp_log); | ||
| 62 | exec($cmdline); | ||
| 63 | } | ||
| 64 | return 0; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | sub player_daemonize { | ||
| 69 | my $daemon_log = shift // '/dev/null'; | ||
| 70 | use POSIX 'setsid'; | ||
| 71 | my $PID = fork(); | ||
| 72 | exit(0) if($PID); #parent | ||
| 73 | exit(1) if(!defined($PID)); # out of resources | ||
| 74 | |||
| 75 | setsid(); | ||
| 76 | $PID = fork(); | ||
| 77 | exit(1) if(!defined($PID)); | ||
| 78 | |||
| 79 | if($PID) { # parent | ||
| 80 | open(my $fh, '>', $pidfile_pimpd) or confess($!); | ||
| 81 | print $fh $$; | ||
| 82 | close($fh); | ||
| 83 | |||
| 84 | waitpid($PID, 0); | ||
| 85 | #unlink($pidfile); # remove the lock when child have confessd | ||
| 86 | |||
| 87 | # Child have confessd/returned. | ||
| 88 | # This means that MPD is in a state where it's not sending any data | ||
| 89 | # We try to reconnect 20 times with a delay, and if the stream is still | ||
| 90 | # down, we exit. See player_init() | ||
| 91 | |||
| 92 | player_init(); | ||
| 93 | exit(0); | ||
| 94 | } | ||
| 95 | elsif($PID == 0) { # child | ||
| 96 | open(my $fh, '>', "$pidfile_player") or confess("pidfile $pidfile_player: $!"); | ||
| 97 | print $fh $$; | ||
| 98 | close($fh); | ||
| 99 | open(STDOUT, '>>', $daemon_log) unless $ENV{DEBUG}; | ||
| 100 | open(STDERR, '>', '/dev/null') unless $ENV{DEBUG}; | ||
| 101 | open(STDIN, '<', '/dev/null') unless $ENV{DEBUG}; | ||
| 102 | } | ||
| 103 | return 0; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub play { | ||
| 107 | player_destruct(); # FIXME | ||
| 108 | $mpd->play; | ||
| 109 | player_init(@_); | ||
| 110 | |||
| 111 | #if(player_init() == 1) { | ||
| 112 | # $mpd->play; | ||
| 113 | #} | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub stop { | ||
| 118 | $mpd->stop; | ||
| 119 | #unlink($player_tmp_log); | ||
| 120 | player_destruct(); | ||
| 121 | |||
| 122 | return; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub player_destruct { | ||
| 126 | open(my $fh, '<', $pidfile_pimpd) or return 1; # for now | ||
| 127 | my $pimpd_player = <$fh>; | ||
| 128 | close($fh); | ||
| 129 | |||
| 130 | if(kill(9, $pimpd_player)) { | ||
| 131 | unlink($player_tmp_log); | ||
| 132 | #printf("%s %s\n", fg('bold', $pimpd_player), 'terminated'); | ||
| 133 | } | ||
| 134 | |||
| 135 | open(my $fh, '<', $pidfile_player) or confess($!); | ||
| 136 | my $pimpd_target = <$fh>; | ||
| 137 | close($fh); | ||
| 138 | |||
| 139 | if(kill(9, $pimpd_target)) { | ||
| 140 | #printf("%s %s\n", fg('bold', $pimpd_target, 'terminated')); | ||
| 141 | } | ||
| 142 | |||
| 143 | if(kill(9, $pimpd_target+1)) { | ||
| 144 | #printf("%s %s\n", fg('bold', $pimpd_target + 1), 'terminated'); | ||
| 145 | } | ||
| 146 | return 0; | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | 1; | ||
| 151 | |||
| 152 | __END__ |