| 1 | #!/usr/bin/perl |
|---|
| 2 | # Play the data on STDIN as an audio file |
|---|
| 3 | # |
|---|
| 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 $ |
|---|
| 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 |
|---|
| 15 | # apparently neither does Quentin Smith <quentin@mit.edu> |
|---|
| 16 | |
|---|
| 17 | use strict; |
|---|
| 18 | use warnings; |
|---|
| 19 | use Image::ExifTool qw(ImageInfo); |
|---|
| 20 | use File::Spec::Functions; |
|---|
| 21 | use File::Temp qw{tempfile tempdir}; |
|---|
| 22 | use File::Basename qw(basename); |
|---|
| 23 | use LWP::UserAgent; |
|---|
| 24 | use Data::Dumper; |
|---|
| 25 | use IPC::Open2; |
|---|
| 26 | use English; |
|---|
| 27 | |
|---|
| 28 | use vars qw/$zephyr_class $host $queue $mixer $channel/; |
|---|
| 29 | |
|---|
| 30 | require "/usr/lib/gutenbach/config/gutenbach-filter-config.pl" or die "Unable to load configuration"; |
|---|
| 31 | |
|---|
| 32 | my $ua = new LWP::UserAgent; |
|---|
| 33 | |
|---|
| 34 | # This variable contains the pid of the child process (which runs |
|---|
| 35 | # mplayer) once it has been forked, so that we can kill it on SIGTERM |
|---|
| 36 | my $pid; |
|---|
| 37 | |
|---|
| 38 | # Replace STDERR with a log file in /tmp. |
|---|
| 39 | open(CUPS, ">&STDERR") or die "Unable to copy CUPS filehandle"; |
|---|
| 40 | close(STDERR); |
|---|
| 41 | open(STDERR, ">>", "/tmp/gutenbach.log") or warn "Couldn't open log: $!"; |
|---|
| 42 | |
|---|
| 43 | # Set the TERM environment (for the benefit of mplayer?) |
|---|
| 44 | # I don't know why we do this --quentin |
|---|
| 45 | $ENV{"TERM"}="vt100"; |
|---|
| 46 | |
|---|
| 47 | print STDERR "STDERR FROM SPOOL FILTER\n"; |
|---|
| 48 | |
|---|
| 49 | # CUPS provides us with these arguments: |
|---|
| 50 | # |
|---|
| 51 | # argv[1] |
|---|
| 52 | # The job ID |
|---|
| 53 | # argv[2] |
|---|
| 54 | # The user printing the job |
|---|
| 55 | # argv[3] |
|---|
| 56 | # The job name/title |
|---|
| 57 | # argv[4] |
|---|
| 58 | # The number of copies to print |
|---|
| 59 | # argv[5] |
|---|
| 60 | # The options that were provided when the job was submitted |
|---|
| 61 | # argv[6] |
|---|
| 62 | # The file to print (first program only) |
|---|
| 63 | # |
|---|
| 64 | # The scheduler runs one or more of these programs to print any given |
|---|
| 65 | # job. The first filter reads from the print file and writes to the |
|---|
| 66 | # standard output, while the remaining filters read from the standard |
|---|
| 67 | # input and write to the standard output. The backend is the last |
|---|
| 68 | # filter in the chain and writes to the device. |
|---|
| 69 | |
|---|
| 70 | printf(STDERR "Got \@ARGV: %s\n", Dumper(\@ARGV)); |
|---|
| 71 | |
|---|
| 72 | my %arguments = ( |
|---|
| 73 | "job-id" => $ARGV[0], |
|---|
| 74 | user => $ARGV[1], |
|---|
| 75 | "job-title" => $ARGV[2], |
|---|
| 76 | copies => $ARGV[3], |
|---|
| 77 | options => {split(/[= ]/, $ARGV[4])}, |
|---|
| 78 | file => $ARGV[5], |
|---|
| 79 | ); |
|---|
| 80 | |
|---|
| 81 | # If we weren't given a filename, we need to read from stdin. Since |
|---|
| 82 | # mplayer really wants a file, let's write out to a temporary file |
|---|
| 83 | # first. |
|---|
| 84 | if (!$arguments{"file"}) { |
|---|
| 85 | my ($fh, $file) = tempfile("gutenbachXXXXX", UNLINK => 1); # Ask File::Temp for a safe temporary file |
|---|
| 86 | my $buf; |
|---|
| 87 | while (read(STDIN, $buf, 1024*1024)) { # Read 1M at a time and put it in the temporary file |
|---|
| 88 | print $fh $buf; |
|---|
| 89 | } |
|---|
| 90 | close($fh); |
|---|
| 91 | $arguments{"file"} = $file; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | printf(STDERR "Got \%arguments: %s\n", Dumper(\%arguments)); |
|---|
| 95 | |
|---|
| 96 | # Open up a zwrite command to announce the current track. |
|---|
| 97 | my @zwrite_command = (qw(/usr/bin/zwrite -d -n -c), $zephyr_class, "-i", $queue.'@'.$host, "-s", "Gutenbach Music Spooler"); |
|---|
| 98 | |
|---|
| 99 | print STDERR "Invoking @zwrite_command\n"; |
|---|
| 100 | open(ZEPHYR, "|-", @zwrite_command) or die "Couldn't launch zwrite: $!"; |
|---|
| 101 | |
|---|
| 102 | my $status; |
|---|
| 103 | if (exists($arguments{"options"}{"job-originating-host-name"})) { |
|---|
| 104 | print(ZEPHYR $arguments{"user"},"\@",$arguments{"options"}{"job-originating-host-name"}," is playing:\n"); |
|---|
| 105 | $status = "User: ".$arguments{"user"}."\@".$arguments{"options"}{"job-originating-host-name"}; |
|---|
| 106 | } else { |
|---|
| 107 | print(ZEPHYR $arguments{"user"}," is playing:\n"); |
|---|
| 108 | $status = "User: ".$arguments{"user"}; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | # SIGHUP handler, in case we were aborted |
|---|
| 112 | sub clear_status { |
|---|
| 113 | kill 15, $pid if $pid; |
|---|
| 114 | my @zwrite_command = (qw(/usr/bin/zwrite -d -n -c), $zephyr_class, "-i", $queue.'@'.$host, "-s", "Gutenbach Music Spooler"); |
|---|
| 115 | open(ZEPH, "|-", @zwrite_command); |
|---|
| 116 | print(ZEPH "Playback aborted.\n"); |
|---|
| 117 | close(ZEPH); |
|---|
| 118 | |
|---|
| 119 | open(STATUS, ">", "/var/run/gutenbach/status"); |
|---|
| 120 | print(STATUS ""); |
|---|
| 121 | close(STATUS); |
|---|
| 122 | die; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | $SIG{HUP} = \&clear_status; |
|---|
| 126 | $SIG{TERM} = \&clear_status; |
|---|
| 127 | $SIG{INT} = \&clear_status; |
|---|
| 128 | |
|---|
| 129 | # Read the metadata information from the file. |
|---|
| 130 | my ($filepath) = $arguments{"file"}; |
|---|
| 131 | my ($fileinfo) = ImageInfo($filepath); |
|---|
| 132 | my ($magic) = $fileinfo->{FileType}; |
|---|
| 133 | my ($tempdir); |
|---|
| 134 | my ($newpath); |
|---|
| 135 | my ($title); |
|---|
| 136 | |
|---|
| 137 | open(STATUS, ">", "/var/run/gutenbach/status"); |
|---|
| 138 | |
|---|
| 139 | if ($magic) { |
|---|
| 140 | # $magic means that Image::ExifTool was able to identify the type of file |
|---|
| 141 | printf(ZEPHYR "%s file %s\n", $magic, $arguments{"job-title"}); |
|---|
| 142 | #printf(STATUS $arguments{"job-title"}); |
|---|
| 143 | $status .= sprintf(" Filetype: %s.", $magic); |
|---|
| 144 | $status .= sprintf(" Filename: %s.", $arguments{"job-title"}); |
|---|
| 145 | if (exists $fileinfo->{'Title'}) { |
|---|
| 146 | $title = $fileinfo->{'Title'}; |
|---|
| 147 | printf(ZEPHYR "\@b{%s}\n", $fileinfo->{'Title'}) if exists $fileinfo->{'Title'}; |
|---|
| 148 | printf(STATUS "%s\n", $fileinfo->{'Title'}) if exists $fileinfo->{'Title'}; |
|---|
| 149 | $status .= sprintf(" Title: %s.", $fileinfo->{'Title'}); |
|---|
| 150 | } |
|---|
| 151 | foreach my $key (qw/Artist Album AlbumArtist/) { |
|---|
| 152 | if (exists $fileinfo->{$key}) { |
|---|
| 153 | printf(ZEPHYR "%s\n", $fileinfo->{$key}) if exists $fileinfo->{$key}; |
|---|
| 154 | printf(STATUS "%s\n", $fileinfo->{$key}) if exists $fileinfo->{$key}; |
|---|
| 155 | $status .= sprintf(" %s: %s\n", $key, $fileinfo->{$key}); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | $tempdir = tempdir(); |
|---|
| 160 | #awful hack -- geofft |
|---|
| 161 | #== -- quentin |
|---|
| 162 | # This code appears to create a new temporary directory and symlink |
|---|
| 163 | # the job file into the temporary directory under the original |
|---|
| 164 | # filename. I think this is because mplayer sometimes uses the file |
|---|
| 165 | # extension to identify a filetype. |
|---|
| 166 | $newpath = $tempdir . '/' . basename($arguments{"job-title"}); |
|---|
| 167 | symlink($filepath, $newpath); |
|---|
| 168 | $filepath = $newpath; |
|---|
| 169 | } |
|---|
| 170 | elsif ($arguments{copies} == 42) { |
|---|
| 171 | # This is a flag that is set by jobs queued by split_playlist(); it tells us to not try to split the playlist again. |
|---|
| 172 | # Call resolve_external_reference to apply some heuristics to determine the filetype. |
|---|
| 173 | $filepath = resolve_external_reference($filepath, \%arguments); |
|---|
| 174 | if ($filepath =~ m|http://www\.youtube\.com/watch\?v=|) { |
|---|
| 175 | # YouTube URLs are resolved by the youtube-dl command. |
|---|
| 176 | # Launch youtube-dl |
|---|
| 177 | $pid = open(YTDL, "-|", "youtube-dl","-b", "-g", $filepath) or die "Unable to invoke youtube-dl"; |
|---|
| 178 | print ZEPHYR "YouTube video $filepath\n$title"; |
|---|
| 179 | $status .= " YouTube video $filepath. $title."; |
|---|
| 180 | # youtube-dl prints the URL of the flash video, which we pass to mplayer as a filename. |
|---|
| 181 | $filepath = <YTDL>; |
|---|
| 182 | chomp $filepath; |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | } else { # Doesn't appear to be a YouTube URL. |
|---|
| 186 | print STDERR "Resolved external reference to $filepath\n"; |
|---|
| 187 | printf(ZEPHYR "%s\n", $filepath); |
|---|
| 188 | $status .= sprintf(" External: %s\n", $filepath); |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | elsif (-T $filepath) { # If the file appears to be a text file, treat it as a playlist. |
|---|
| 192 | split_playlist($filepath, \%arguments); |
|---|
| 193 | close(ZEPHYR); |
|---|
| 194 | # See http://www.cups.org/documentation.php/api-filter.html#MESSAGES |
|---|
| 195 | print CUPS "NOTICE: $status\n"; |
|---|
| 196 | exit 0; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | close(ZEPHYR); |
|---|
| 200 | close(STATUS); |
|---|
| 201 | print CUPS "NOTICE: $status\n"; |
|---|
| 202 | play_mplayer_audio($filepath, \%arguments); |
|---|
| 203 | |
|---|
| 204 | # Remove the symlink we made earlier for the filetype. |
|---|
| 205 | if ($magic) { |
|---|
| 206 | unlink($newpath); |
|---|
| 207 | rmdir($tempdir); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | # Play an external stream reference |
|---|
| 211 | sub resolve_external_reference { |
|---|
| 212 | # Retrieve those command line opts. |
|---|
| 213 | my ($filepath, $arguments) = @_; |
|---|
| 214 | |
|---|
| 215 | my ($format, $uri, $userpass); |
|---|
| 216 | |
|---|
| 217 | open(FILE, "<", $filepath) or die "Couldn't open spool file"; |
|---|
| 218 | if (<FILE> =~ /^(\S+)/) { |
|---|
| 219 | # Take the leading non-whitespace as a URL |
|---|
| 220 | $uri=$1; |
|---|
| 221 | |
|---|
| 222 | if ($uri =~ m|http://www\.youtube\.com/watch\?v=|) { |
|---|
| 223 | return $uri; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | # Fetch the URL with a HEAD request to get the content type |
|---|
| 227 | my $response = $ua->head($uri); |
|---|
| 228 | |
|---|
| 229 | my $contenttype=($response->content_type() or "unknown"); |
|---|
| 230 | |
|---|
| 231 | if ($contenttype eq "audio/mpeg") { $format="MP3" } |
|---|
| 232 | elsif ($contenttype eq "application/x-ogg") { $format="OGG" } |
|---|
| 233 | elsif ($contenttype eq "application/ogg") { $format="OGG" } |
|---|
| 234 | elsif ($contenttype eq "audio/x-scpls") { $format="SHOUTCAST" } |
|---|
| 235 | else { |
|---|
| 236 | print ZEPHYR |
|---|
| 237 | "Unknown Content-Type $contenttype for URI $uri\n"; |
|---|
| 238 | } |
|---|
| 239 | } else { # Unable to match the URL regex |
|---|
| 240 | print ZEPHYR "Couldn't read URI for external reference\n"; |
|---|
| 241 | # Return the existing path, in the hopes that mplayer knows what to do with it. |
|---|
| 242 | return $filepath; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | if ($format eq "SHOUTCAST") { |
|---|
| 246 | print ZEPHYR "Shoutcast playlist...\n"; |
|---|
| 247 | return get_shoutcast($uri); |
|---|
| 248 | } elsif ($format eq "MP3") { |
|---|
| 249 | } elsif ($format eq "OGG") { |
|---|
| 250 | } else { |
|---|
| 251 | print ZEPHYR "Unrecognized stream format: $format\n"; |
|---|
| 252 | } |
|---|
| 253 | return $uri; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | sub split_playlist { |
|---|
| 257 | my ($file, $arguments) = @_; |
|---|
| 258 | |
|---|
| 259 | my $i = 0; |
|---|
| 260 | |
|---|
| 261 | open(FILE, "<", $filepath) or die "Couldn't open spool file"; |
|---|
| 262 | while (<FILE>) { |
|---|
| 263 | chomp; |
|---|
| 264 | if (/^([^#]\S+)/) { |
|---|
| 265 | printf (STDERR "Found playlist line: %s\n", $_); |
|---|
| 266 | $ENV{CUPS_SERVER}='localhost'; |
|---|
| 267 | open(LP, "|-", "lp", "-d", "$queue", "-n", "42"); #'-#', '42', '-J', $arguments->{"job-title"}, '-o', 'job-priority=100'); |
|---|
| 268 | print LP $1; |
|---|
| 269 | close(LP); |
|---|
| 270 | $i++; |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | printf(ZEPHYR "Playlist containing %d valid entries, split into separate jobs.\n", $i); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | # Process a Shoutcast playlist |
|---|
| 277 | # get_shoutcast(URI) |
|---|
| 278 | sub get_shoutcast { |
|---|
| 279 | my $uri = shift(@_); |
|---|
| 280 | |
|---|
| 281 | my $response = $ua->get($uri); |
|---|
| 282 | my (@titles, @uris); |
|---|
| 283 | |
|---|
| 284 | foreach (split("\n", $response->content())) { |
|---|
| 285 | if (/^File\d+=(\S+)/) { |
|---|
| 286 | push(@uris, $1); |
|---|
| 287 | } |
|---|
| 288 | if (/^Title\d+=(.+)$/) { |
|---|
| 289 | push(@titles, $1); |
|---|
| 290 | } |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | # choose a random server |
|---|
| 294 | my $server = int(rand scalar(@uris)); |
|---|
| 295 | # print the name of the stream if available |
|---|
| 296 | print ZEPHYR "$titles[$server]\n"; |
|---|
| 297 | return $uris[$server]; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | sub play_mplayer_audio { |
|---|
| 301 | my ($filepath, $opts) = @_; |
|---|
| 302 | |
|---|
| 303 | # Open up a zwrite command to show the mplayer output |
|---|
| 304 | my @zwrite_command = (qw(/usr/bin/zwrite -d -n -c), $zephyr_class, "-i", $queue.'@'.$host, "-s", "Gutenbach Music Spooler"); |
|---|
| 305 | |
|---|
| 306 | print STDERR "Invoking (from play_mplayer_audio): @zwrite_command\n"; |
|---|
| 307 | |
|---|
| 308 | # fork for mplayer |
|---|
| 309 | $pid = open(MP3STATUS, "-|"); |
|---|
| 310 | unless (defined $pid) { |
|---|
| 311 | open(ZEPHYR, "|-", @zwrite_command) or die "Couldn't launch zwrite: $!"; |
|---|
| 312 | print ZEPHYR "Couldn't fork: $!\n"; |
|---|
| 313 | close(ZEPHYR); |
|---|
| 314 | return; |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | if ($pid) { #parent |
|---|
| 318 | # Check if there were any errors |
|---|
| 319 | if ($_ = <MP3STATUS>) { |
|---|
| 320 | open(ZEPHYR, "|-", @zwrite_command) or die "Couldn't launch zwrite: $!"; |
|---|
| 321 | print ZEPHYR "Playback completed with the following errors:\n"; |
|---|
| 322 | while (<MP3STATUS>) { |
|---|
| 323 | print ZEPHYR $_; |
|---|
| 324 | } |
|---|
| 325 | close(ZEPHYR); |
|---|
| 326 | } else { |
|---|
| 327 | open(ZEPHYR, "|-", @zwrite_command) or die "Couldn't launch zwrite: $!"; |
|---|
| 328 | print ZEPHYR "Playback completed successfully.\n"; |
|---|
| 329 | close(ZEPHYR); |
|---|
| 330 | open(STATUS, ">", "/var/run/gutenbach/status"); |
|---|
| 331 | print(STATUS ""); |
|---|
| 332 | close(STATUS); |
|---|
| 333 | } |
|---|
| 334 | close(MP3STATUS) || print ZEPHYR "mplayer exited $?\n"; |
|---|
| 335 | } |
|---|
| 336 | else { # child |
|---|
| 337 | # redirect STDERR to STDOUT |
|---|
| 338 | open STDERR, '>&STDOUT'; |
|---|
| 339 | # make sure that mplayer doesn't try to intepret the file as keyboard input |
|---|
| 340 | close(STDIN); |
|---|
| 341 | open(STDIN, "/dev/null"); |
|---|
| 342 | |
|---|
| 343 | my @args = (qw|/usr/bin/mplayer -vo fbdev2 -zoom -x 1024 -y 768 -framedrop -nolirc -cache 512 -ao alsa -really-quiet |, $filepath); |
|---|
| 344 | #pint STDERR "About to exec: ", Dumper([@args]); |
|---|
| 345 | exec(@args) || |
|---|
| 346 | die "Couldn't exec"; |
|---|
| 347 | } |
|---|
| 348 | } |
|---|