source: debian/lib/gutenbach @ 3778176

debianmacno-cupsnodebathenaweb
Last change on this file since 3778176 was 3778176, checked in by Jessica B. Hamrick <jhamrick@…>, 14 years ago

CUPS on Jaunty doesn't send hostname info, so check to see if it is null first

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