[b0d9dda] | 1 | #!/usr/athena/bin/perl |
---|
| 2 | # Play the data on STDIN as an audio file |
---|
| 3 | # |
---|
[2e8b589] | 4 | # $Id: gutenbach-filter,v 1.26 2009/02/20 00:27:17 geofft Exp root $ |
---|
| 5 | # $Source: /usr/local/bin/RCS/gutenbach-filter,v $ |
---|
[b0d9dda] | 6 | # |
---|
| 7 | # TODO |
---|
| 8 | # ---- |
---|
| 9 | # Make this structured code. It's a mess. |
---|
| 10 | # Repeat what we just played for EXT files too |
---|
| 11 | # Support HTTP Auth on ogg streams |
---|
| 12 | # License, cleanup and package |
---|
| 13 | # |
---|
| 14 | # Jered Floyd <jered@mit.edu> takes very little credit for this code |
---|
[c711ed9] | 15 | # apparently neither does Quentin Smith <quentin@mit.edu> |
---|
[b0d9dda] | 16 | |
---|
[ba06c7f] | 17 | use Image::ExifTool qw(ImageInfo); |
---|
| 18 | use File::Spec::Functions; |
---|
[908107d] | 19 | use File::Temp qw{tempdir}; |
---|
| 20 | use File::Basename qw(basename); |
---|
[ba06c7f] | 21 | use LWP::UserAgent; |
---|
| 22 | use Data::Dumper; |
---|
[f6c9736] | 23 | use IPC::Open2; |
---|
[ba06c7f] | 24 | |
---|
[32b323f] | 25 | my $zephyr_class = "sipb-test"; |
---|
| 26 | my $host = `hostname`; |
---|
[2e8b589] | 27 | my $queue = "gutenbach"; |
---|
[3a900ef] | 28 | |
---|
| 29 | # Configuration |
---|
[2e8b589] | 30 | my $config_file = "/usr/lib/gutenbach/gutenbach-filter-config.pl"; |
---|
[3a900ef] | 31 | if (-r $config_file) { |
---|
| 32 | # Inline the configuration file |
---|
| 33 | local $/; |
---|
| 34 | my $fh; |
---|
| 35 | open $fh, $config_file; |
---|
| 36 | eval <$fh>; |
---|
| 37 | } |
---|
| 38 | |
---|
[ba06c7f] | 39 | my $ua = new LWP::UserAgent; |
---|
| 40 | |
---|
| 41 | close(STDERR); |
---|
[2e8b589] | 42 | open(STDERR, ">>", "/tmp/gutenbach.log") or warn "Couldn't open log: $!"; |
---|
[ba06c7f] | 43 | |
---|
| 44 | $ENV{"TERM"}="vt100"; |
---|
[b0d9dda] | 45 | |
---|
[148111e] | 46 | print STDERR "STDERR FROM SPOOL FILTER\n"; |
---|
| 47 | |
---|
[b0d9dda] | 48 | # set real uid to be effective uid |
---|
| 49 | $< = $>; |
---|
| 50 | |
---|
| 51 | # Select the correct output device and set the volume |
---|
[ba06c7f] | 52 | #system("amixer -q set Headphone 100\% unmute"); |
---|
[b0d9dda] | 53 | |
---|
| 54 | # The command line we get from lpd is (no spaces between options and args): |
---|
| 55 | # -C lpr -C class |
---|
| 56 | # -A LPRng internal identifier |
---|
| 57 | # -H originating host |
---|
| 58 | # -J lpr -J jobname (default: list of files) |
---|
| 59 | # -L lpr -U username |
---|
| 60 | # -P logname |
---|
| 61 | # -Q queuename (lpr -Q) |
---|
[53d7625] | 62 | # -Z random user-specified options |
---|
[b0d9dda] | 63 | # -a printcap af (accounting file name) |
---|
| 64 | # -d printcap sd entry (spool dir) |
---|
| 65 | # -e print job data file name (currently being processed) |
---|
| 66 | # -h print job originiating host (same as -H) |
---|
| 67 | # -j job number in spool queue |
---|
| 68 | # -k print job control file name |
---|
| 69 | # -l printcap pl (page length) |
---|
| 70 | # -n user name (same as -L) |
---|
| 71 | # -s printcap sf (status file) |
---|
| 72 | # -w printcap pw (page width) |
---|
| 73 | # -x printcap px (page x dimension) |
---|
| 74 | # -y printcap py (page y dimension) |
---|
| 75 | # accounting file name |
---|
| 76 | |
---|
[06133dc] | 77 | printf(STDERR "Got \@ARGV: %s\n", Dumper(\@ARGV)); |
---|
| 78 | |
---|
| 79 | my %opts; |
---|
| 80 | |
---|
| 81 | my @NEWARGV; |
---|
| 82 | |
---|
| 83 | foreach my $arg (@ARGV) { |
---|
| 84 | if ($arg =~ m/^-([a-zA-Z])(.*)$/) { |
---|
| 85 | $opts{$1} = $2; |
---|
| 86 | } else { |
---|
| 87 | push @NEWARGV, @ARGV; |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | @ARGV = @NEWARGV; |
---|
[b0d9dda] | 92 | |
---|
[b011fb0] | 93 | printf(STDERR Dumper(\%opts)); |
---|
| 94 | |
---|
[b0d9dda] | 95 | # Status messages at start of playback |
---|
[3a900ef] | 96 | open(ZEPHYR, '|/usr/athena/bin/zwrite -d -n -c '. $zephyr_class .' -i ' . |
---|
[9daccda] | 97 | $queue.'@'.$host.' -s "SIPB LPR music spooler"'); |
---|
[b0d9dda] | 98 | |
---|
[b5d15a0] | 99 | # For the Now Playing remctl command |
---|
[2e8b589] | 100 | open(STATUS, '>', '/var/run/gutenbach/status') or die("Can't open status file /var/run/gutenbach/status"); |
---|
| 101 | |
---|
| 102 | # For the Now Playing local command |
---|
| 103 | open(CURRENT, '>', '/var/run/gutenbach/current') or die("Can't open status file /var/run/sipbp3/current"); |
---|
[b5d15a0] | 104 | |
---|
[c2f6180] | 105 | print(ZEPHYR "$opts{'n'}\@$opts{'H'} is playing:\n"); |
---|
| 106 | print(STATUS "User: $opts{'n'}\@$opts{'H'}\n"); |
---|
[2e8b589] | 107 | print(CURRENT "$opts{'n'}\@$opts{'H'} is playing:\n"); |
---|
[c2f6180] | 108 | |
---|
[f014bc4] | 109 | # SIGHUP handler |
---|
| 110 | sub clear_status { |
---|
| 111 | # Possible race condition if the previous status is still going |
---|
[2e8b589] | 112 | open(STA, '>', '/var/run/gutenbach/status'); |
---|
[f014bc4] | 113 | close(STA); |
---|
| 114 | open(ZEPH, '|/usr/athena/bin/zwrite -d -n -c '. $zephyr_class .' -i '. |
---|
[45850db] | 115 | $queue.'@'.$host.' -s "SIPB LPR music spooler"'); |
---|
[f014bc4] | 116 | print(ZEPH "Playback aborted.\n"); |
---|
| 117 | close(ZEPH); |
---|
[2e8b589] | 118 | open(CURRENT, '|/usr/athena/bin/zwrite -d -n -c '. $zephyr_class .' -i '. |
---|
| 119 | $queue.'@'.$host.' -s "SIPB LPR music spooler"'); |
---|
| 120 | close(CURRENT); |
---|
[f014bc4] | 121 | die; |
---|
| 122 | } |
---|
| 123 | $SIG{HUP} = \&clear_status; |
---|
| 124 | |
---|
[b0d9dda] | 125 | # So, the file we're currently processing is "-d/-e". |
---|
| 126 | |
---|
[ba06c7f] | 127 | # Read the metadata information from the file. |
---|
| 128 | my ($filepath) = catfile($opts{'d'}, $opts{'e'}); |
---|
| 129 | my ($fileinfo) = ImageInfo($filepath); |
---|
| 130 | my ($magic) = $fileinfo->{FileType}; |
---|
[b0d9dda] | 131 | |
---|
[ba06c7f] | 132 | if ($magic) { |
---|
[c711ed9] | 133 | printf(ZEPHYR "%s file %s\n", $magic, $opts{'J'}); |
---|
[2e8b589] | 134 | printf(CURRENT "%s file %s\n", $magic, $opts{'J'}); |
---|
[b5d15a0] | 135 | printf(STATUS "Filetype: %s\n", $magic); |
---|
| 136 | printf(STATUS "Filename: %s\n", $opts{'J'}); |
---|
| 137 | if (exists $fileinfo->{'Title'}) { |
---|
| 138 | printf(ZEPHYR "\@b{%s}\n", $fileinfo->{'Title'}) if exists $fileinfo->{'Title'}; |
---|
[2e8b589] | 139 | printf(CURRENT "\@b{%s}\n", $fileinfo->{'Title'}) if exists $fileinfo->{'Title'}; |
---|
[b5d15a0] | 140 | printf(STATUS "Title: %s\n", $fileinfo->{'Title'}); |
---|
| 141 | } |
---|
[4cc6b4d] | 142 | foreach my $key (qw/Artist Album AlbumArtist/) { |
---|
[b5d15a0] | 143 | if (exists $fileinfo->{$key}) { |
---|
| 144 | printf(ZEPHYR "%s\n", $fileinfo->{$key}) if exists $fileinfo->{$key}; |
---|
[2e8b589] | 145 | printf(CURRENT "%s\n", $fileinfo->{$key}) if exists $fileinfo->{$key}; |
---|
[b5d15a0] | 146 | printf(STATUS "%s: %s\n", $key, $fileinfo->{$key}); |
---|
| 147 | } |
---|
[ba06c7f] | 148 | } |
---|
[a041c73] | 149 | my $tempdir = tempdir(); |
---|
[6d813c3] | 150 | $opts{'J'} =~ s/_mp3/.mp3/; #awful hack -- geofft |
---|
[a041c73] | 151 | my $newpath = $tempdir . '/' . basename($opts{'J'}); |
---|
| 152 | symlink($filepath, $newpath); |
---|
| 153 | $filepath = $newpath; |
---|
[ba06c7f] | 154 | } |
---|
[7e27cc3] | 155 | elsif ($opts{'C'} eq 'Z') { |
---|
[ba06c7f] | 156 | $filepath = resolve_external_reference($filepath, \%opts); |
---|
[f6c9736] | 157 | if ($filepath =~ m|http://www\.youtube\.com/watch\?v=|) { |
---|
[f1ba976] | 158 | $pid = open2($out, $in, qw{youtube-dl -g}, $filepath); |
---|
| 159 | #$title = <$out>; |
---|
| 160 | $title = ""; |
---|
[f6c9736] | 161 | print ZEPHYR "YouTube video $filepath\n$title"; |
---|
[2e8b589] | 162 | print CURRENT "YouTube video $filepath\n$title"; |
---|
[f6c9736] | 163 | print STATUS "YouTube video $filepath\n$title"; |
---|
| 164 | $filepath = <$out>; |
---|
| 165 | chomp $filepath; |
---|
| 166 | waitpid $pid, 0; |
---|
| 167 | } else { |
---|
| 168 | print STDERR "Resolved external reference to $filepath\n"; |
---|
| 169 | printf(ZEPHYR "%s\n", $filepath); |
---|
[2e8b589] | 170 | printf(CURRENT "%s\n", $filepath); |
---|
[f6c9736] | 171 | printf(STATUS "External: %s\n", $filepath); |
---|
| 172 | } |
---|
[ba06c7f] | 173 | } |
---|
[7e27cc3] | 174 | elsif (-T $filepath) { |
---|
| 175 | split_playlist($filepath, \%opts); |
---|
| 176 | close(ZEPHYR); |
---|
[2e8b589] | 177 | close(CURRENT); |
---|
[b5d15a0] | 178 | close(STATUS); |
---|
[7e27cc3] | 179 | exit 0; |
---|
| 180 | } |
---|
[a041c73] | 181 | |
---|
[7e27cc3] | 182 | #printf(STDERR "Job priority %s\n", $opts{'C'}) if $opts{'C'} eq 'Z'; |
---|
| 183 | #printf(ZEPHYR "Job priority %s\n", $opts{'C'}) if ($opts{'C'} && ($opts{'C'} ne 'A')); |
---|
[ba06c7f] | 184 | close(ZEPHYR); |
---|
[2e8b589] | 185 | close(CURRENT); |
---|
[b5d15a0] | 186 | close(STATUS); |
---|
[ba06c7f] | 187 | play_mplayer_audio($filepath, \%opts); |
---|
[b0d9dda] | 188 | |
---|
[a041c73] | 189 | if ($magic) { |
---|
| 190 | unlink($newpath); |
---|
| 191 | rmdir($tempdir); |
---|
| 192 | } |
---|
| 193 | |
---|
[b0d9dda] | 194 | # Play an external stream reference |
---|
[ba06c7f] | 195 | sub resolve_external_reference { |
---|
[b0d9dda] | 196 | # Retrieve those command line opts. |
---|
[ba06c7f] | 197 | my ($filepath, $opts) = @_; |
---|
[b0d9dda] | 198 | |
---|
| 199 | my $format, $uri, $userpass; |
---|
| 200 | |
---|
[ba06c7f] | 201 | if (<STDIN> =~ /^(\S+)/) { |
---|
| 202 | $uri=$1; |
---|
| 203 | |
---|
[f6c9736] | 204 | if ($uri =~ m|http://www\.youtube\.com/watch\?v=|) { |
---|
| 205 | return $uri; |
---|
| 206 | } |
---|
| 207 | |
---|
[ba06c7f] | 208 | my $response = $ua->head($uri); |
---|
| 209 | |
---|
| 210 | $contenttype=($response->content_type() or "unknown"); |
---|
| 211 | |
---|
| 212 | if ($contenttype eq "audio/mpeg") { $format="MP3" } |
---|
| 213 | elsif ($contenttype eq "application/x-ogg") { $format="OGG" } |
---|
| 214 | elsif ($contenttype eq "application/ogg") { $format="OGG" } |
---|
| 215 | elsif ($contenttype eq "audio/x-scpls") { $format="SHOUTCAST" } |
---|
| 216 | else { |
---|
| 217 | print ZEPHYR |
---|
| 218 | "Unknown Content-Type $contenttype for URI $uri\n"; |
---|
| 219 | } |
---|
| 220 | } else { |
---|
[82d34e6] | 221 | print ZEPHYR "Couldn't read URI for external reference\n"; |
---|
[ba06c7f] | 222 | return $filepath; |
---|
[b281379] | 223 | } |
---|
[b0d9dda] | 224 | |
---|
[ba06c7f] | 225 | if ($format eq "SHOUTCAST") { |
---|
[53d7625] | 226 | print ZEPHYR "Shoutcast playlist...\n"; |
---|
| 227 | #Don't close ZEPHYR yet, will print the name of the stream if available |
---|
[ba06c7f] | 228 | return &get_shoutcast($uri); |
---|
| 229 | } elsif ($format eq "MP3") { |
---|
| 230 | } elsif ($format eq "OGG") { |
---|
[b0d9dda] | 231 | } else { |
---|
| 232 | print ZEPHYR "Unrecognized stream format: $format\n"; |
---|
| 233 | } |
---|
[ba06c7f] | 234 | return $uri; |
---|
[b0d9dda] | 235 | } |
---|
| 236 | |
---|
[7e27cc3] | 237 | sub split_playlist { |
---|
| 238 | my ($file, $opts) = @_; |
---|
| 239 | |
---|
| 240 | my $i = 0; |
---|
| 241 | |
---|
| 242 | while (<STDIN>) { |
---|
| 243 | chomp; |
---|
| 244 | if (/^([^#]\S+)/) { |
---|
| 245 | printf (STDERR "Found line: %s\n", $_); |
---|
[9daccda] | 246 | open(LPR, "|-", 'mit-lpr', '-P'.$queue.'@localhost', '-CZ', '-J'.$opts->{J}); |
---|
[7e27cc3] | 247 | print LPR $1; |
---|
| 248 | close(LPR); |
---|
| 249 | $i++; |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | printf(ZEPHYR "Playlist containing %d valid entries, split into separate jobs.\n", $i); |
---|
| 253 | } |
---|
| 254 | |
---|
[53d7625] | 255 | # Process a Shoutcast playlist |
---|
| 256 | # get_shoutcast(URI) |
---|
| 257 | sub get_shoutcast { |
---|
| 258 | my $uri = shift(@_); |
---|
| 259 | |
---|
[ba06c7f] | 260 | my $response = $ua->get($uri); |
---|
| 261 | |
---|
| 262 | foreach (split("\n", $response->content())) { |
---|
[53d7625] | 263 | if (/^File\d+=(\S+)/) { |
---|
| 264 | push(@uris, $1); |
---|
| 265 | } |
---|
| 266 | if (/^Title\d+=(.+)$/) { |
---|
| 267 | push(@titles, $1); |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | # choose a random server |
---|
| 272 | $server = int(rand scalar(@uris)); |
---|
| 273 | # print the name of the stream if available |
---|
| 274 | print ZEPHYR "$titles[$server]\n"; |
---|
| 275 | return $uris[$server]; |
---|
| 276 | } |
---|
[b0d9dda] | 277 | |
---|
[ba06c7f] | 278 | sub play_mplayer_audio { |
---|
| 279 | my ($filepath, $opts) = @_; |
---|
[b0d9dda] | 280 | |
---|
[ba06c7f] | 281 | # Prepare to write status: |
---|
[3a900ef] | 282 | open(ZEPHYR, '|/usr/athena/bin/zwrite -d -n -c '.$zephyr_class.' -i ' . |
---|
[45850db] | 283 | $queue.'@'.$host.' -s "SIPB LPR music spooler"'); |
---|
[ba06c7f] | 284 | |
---|
| 285 | # fork for mpg123 |
---|
| 286 | my $pid = open(MP3STATUS, "-|"); |
---|
| 287 | unless (defined $pid) { |
---|
| 288 | print ZEPHYR "Couldn't fork: $!\n"; |
---|
| 289 | close(ZEPHYR); |
---|
| 290 | return; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | if ($pid) { #parent |
---|
| 294 | # Check if there were any errors |
---|
| 295 | if ($_ = <MP3STATUS>) { |
---|
| 296 | print ZEPHYR "Playback completed with the following errors:\n"; |
---|
| 297 | print ZEPHYR $_; |
---|
| 298 | while (<MP3STATUS>) { |
---|
| 299 | print ZEPHYR $_; |
---|
| 300 | } |
---|
| 301 | } else { |
---|
| 302 | print ZEPHYR "Playback completed successfully.\n"; |
---|
| 303 | } |
---|
| 304 | close(MP3STATUS) || print ZEPHYR "mplayer exited $?\n"; |
---|
| 305 | |
---|
| 306 | close(ZEPHYR); |
---|
[2e8b589] | 307 | open(STATUS, '>', '/var/run/gutenbach/status'); |
---|
[b5d15a0] | 308 | close(STATUS); |
---|
[ba06c7f] | 309 | } |
---|
[b281379] | 310 | else { # child |
---|
| 311 | # redirect STDERR to STDOUT |
---|
| 312 | open STDERR, '>&STDOUT'; |
---|
[ba06c7f] | 313 | # make sure that mplayer doesn't try to intepret the file as keyboard input |
---|
| 314 | close(STDIN); |
---|
| 315 | open(STDIN, "/dev/null"); |
---|
| 316 | #print STDERR Dumper([qw|/usr/bin/mplayer -nolirc -ao alsa -quiet|, $filepath]); |
---|
[8625400] | 317 | my @args = (qw|/usr/bin/mplayer -vo fbdev2 -zoom -x 1024 -y 768 -framedrop -nolirc -ao alsa -cache 512 -really-quiet |, $filepath); |
---|
[b011fb0] | 318 | #print STDERR "About to exec: ", Dumper([@args]); |
---|
| 319 | exec(@args) || |
---|
[6761cd4] | 320 | die "Couldn't exec"; |
---|
[b0d9dda] | 321 | } |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | # ID3 comments often have useless crap because tools like iTunes were |
---|
| 325 | # written by drooling idiots |
---|
| 326 | sub filter_comment { |
---|
| 327 | my $comment = shift(@_); |
---|
| 328 | |
---|
| 329 | if ($comment =~ /^engiTunes_CDDB/) { |
---|
| 330 | return undef; |
---|
| 331 | } |
---|
| 332 | return $comment; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | |
---|