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