source: gutenbach/debian/lib/sipbmp3-filter @ 908107d

debianmacno-cupsnodebathenaweb
Last change on this file since 908107d was 908107d, checked in by geofft <geofft>, 16 years ago

Provide the right extension to mplayer 'cause it gets filetype detection wrong

  • Property mode set to 100755
File size: 6.7 KB
Line 
1#!/usr/athena/bin/perl
2# Play the data on STDIN as an audio file
3#
4# $Id: sipbmp3-filter,v 1.25 2009-02-09 00:07:49 geofft Exp $
5# $Source: /tmp/tmp.UFBNno9997/RCS/sipbmp3-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 Image::ExifTool qw(ImageInfo);
18use File::Spec::Functions;
19use File::Temp qw{tempdir};
20use File::Basename qw(basename);
21use LWP::UserAgent;
22use Data::Dumper;
23
24my $ua = new LWP::UserAgent;
25
26close(STDERR);
27open(STDERR, ">>", "/tmp/sipbmp3.log") or warn "Couldn't open log: $!";
28
29$ENV{"TERM"}="vt100";
30
31print STDERR "STDERR FROM SPOOL FILTER\n";
32
33# set real uid to be effective uid
34$< = $>;
35
36# Select the correct output device and set the volume
37#system("amixer -q set Headphone 100\% unmute");
38
39# The command line we get from lpd is (no spaces between options and args):
40#  -C lpr -C class
41#  -A LPRng internal identifier
42#  -H originating host
43#  -J lpr -J jobname (default: list of files)
44#  -L lpr -U username
45#  -P logname
46#  -Q queuename (lpr -Q)
47#  -Z random user-specified options
48#  -a printcap af (accounting file name)
49#  -d printcap sd entry (spool dir)
50#  -e print job data file name (currently being processed)
51#  -h print job originiating host (same as -H)
52#  -j job number in spool queue
53#  -k print job control file name
54#  -l printcap pl (page length)
55#  -n user name (same as -L)
56#  -s printcap sf (status file)
57#  -w printcap pw (page width)
58#  -x printcap px (page x dimension)
59#  -y printcap py (page y dimension)
60# accounting file name
61
62printf(STDERR "Got \@ARGV: %s\n", Dumper(\@ARGV));
63
64my %opts;
65
66my @NEWARGV;
67
68foreach my $arg (@ARGV) {
69  if ($arg =~ m/^-([a-zA-Z])(.*)$/) {
70    $opts{$1} = $2;
71  } else {
72    push @NEWARGV, @ARGV;
73  }
74}
75
76@ARGV = @NEWARGV;
77
78printf(STDERR Dumper(\%opts));
79
80# Status messages at start of playback
81open(ZEPHYR, '|/usr/athena/bin/zwrite -d -n -c sipb-auto -i ' .
82  'sipbmp3@zsr -s "SIPB LPR music spooler"');
83print(ZEPHYR "$opts{'n'}\@$opts{'H'} is playing:\n");
84
85# So, the file we're currently processing is "-d/-e".
86
87# Read the metadata information from the file.
88my ($filepath) = catfile($opts{'d'}, $opts{'e'});
89my ($fileinfo) = ImageInfo($filepath);
90my ($magic) = $fileinfo->{FileType};
91
92if ($magic) {
93    printf(ZEPHYR "%s file %s\n", $magic, $opts{'J'});
94    printf(ZEPHYR "\@b{%s}\n", $fileinfo->{'Title'}) if exists $fileinfo->{'Title'};
95    foreach my $key (qw/Artist Album AlbumArtist/) {
96        printf(ZEPHYR "%s\n", $fileinfo->{$key}) if exists $fileinfo->{$key};
97    }
98}
99elsif ($opts{'C'} eq 'Z') {
100    $filepath = resolve_external_reference($filepath, \%opts);
101    print STDERR "Resolved external reference to $filepath\n";
102    printf(ZEPHYR "%s\n", $filepath);
103    close(ZEPHYR);
104}
105elsif (-T $filepath) {
106    split_playlist($filepath, \%opts);
107    close(ZEPHYR);
108    exit 0;
109}
110#printf(STDERR "Job priority %s\n", $opts{'C'}) if $opts{'C'} eq 'Z';
111#printf(ZEPHYR "Job priority %s\n", $opts{'C'}) if ($opts{'C'} && ($opts{'C'} ne 'A'));
112close(ZEPHYR);
113play_mplayer_audio($filepath, \%opts);
114
115# Play an external stream reference
116sub resolve_external_reference {
117    # Retrieve those command line opts.
118    my ($filepath, $opts) = @_;
119
120    my $format, $uri, $userpass;
121
122    if (<STDIN> =~ /^(\S+)/) {
123        $uri=$1;
124
125        my $response = $ua->head($uri);
126       
127        $contenttype=($response->content_type() or "unknown");
128       
129        if ($contenttype eq "audio/mpeg") { $format="MP3" }
130        elsif ($contenttype eq "application/x-ogg") { $format="OGG" }
131        elsif ($contenttype eq "application/ogg") { $format="OGG" }
132        elsif ($contenttype eq "audio/x-scpls") { $format="SHOUTCAST" }
133        else {
134            print ZEPHYR
135                "Unknown Content-Type $contenttype for URI $uri\n";
136        }
137    } else {
138        print ZEPHYR "Couldn't read URI for external reference\n";
139        return $filepath;
140    }
141
142    if ($format eq "SHOUTCAST") {
143        print ZEPHYR "Shoutcast playlist...\n";
144        #Don't close ZEPHYR yet, will print the name of the stream if available
145        return &get_shoutcast($uri);
146    } elsif ($format eq "MP3") {
147    } elsif ($format eq "OGG") {
148    } else {
149      print ZEPHYR "Unrecognized stream format: $format\n";
150    }
151    return $uri;
152}
153
154sub split_playlist {
155    my ($file, $opts) = @_;
156
157    my $i = 0;
158   
159    while (<STDIN>) {
160        chomp;
161        if (/^([^#]\S+)/) {
162            printf (STDERR "Found line: %s\n", $_);
163            open(LPR, "|-", qw/mit-lpr -Psipbmp3@localhost -CZ/, '-J'.$opts->{J});
164            print LPR $1;
165            close(LPR);
166        $i++;
167        }
168    }
169    printf(ZEPHYR "Playlist containing %d valid entries, split into separate jobs.\n", $i);
170}
171
172# Process a Shoutcast playlist
173# get_shoutcast(URI)
174sub get_shoutcast {
175  my $uri = shift(@_);
176 
177  my $response = $ua->get($uri);
178
179  foreach (split("\n", $response->content())) {
180      if (/^File\d+=(\S+)/) {
181          push(@uris, $1);
182      }
183      if (/^Title\d+=(.+)$/) {
184          push(@titles, $1);
185      }
186  }
187 
188  # choose a random server
189  $server = int(rand scalar(@uris));
190  # print the name of the stream if available
191  print ZEPHYR "$titles[$server]\n";
192  return $uris[$server];
193}
194
195sub play_mplayer_audio {
196    my ($filepath, $opts) = @_;
197
198    # Prepare to write status:
199    open(ZEPHYR, '|/usr/athena/bin/zwrite -d -n -c sipb-auto -i ' .
200         'sipbmp3@zsr -s "SIPB LPR music spooler"');
201   
202    # fork for mpg123
203    my $pid = open(MP3STATUS, "-|");
204    unless (defined $pid) {
205        print ZEPHYR "Couldn't fork: $!\n";
206        close(ZEPHYR);
207        return;
208    }
209   
210    my $tempdir = tempdir();
211    my $newpath = $tempdir . '/' . basename($opts{'J'});
212    symlink($filepath, $newpath);
213    if ($pid) { #parent
214        # Check if there were any errors
215        if ($_ = <MP3STATUS>) {
216            print ZEPHYR "Playback completed with the following errors:\n";
217            print ZEPHYR $_;
218            while (<MP3STATUS>) {
219                print ZEPHYR $_;
220            }
221        } else {
222            print ZEPHYR "Playback completed successfully.\n";
223        }
224        close(MP3STATUS) || print ZEPHYR "mplayer exited $?\n";
225       
226        close(ZEPHYR);
227        unlink($newpath);
228        rmdir($tempdir);
229    }
230  else { # child
231      # redirect STDERR to STDOUT
232      open STDERR, '>&STDOUT';
233      # make sure that mplayer doesn't try to intepret the file as keyboard input
234      close(STDIN);
235      open(STDIN, "/dev/null");
236      #print STDERR Dumper([qw|/usr/bin/mplayer -nolirc -ao alsa -quiet|, $filepath]);
237      my @args = (qw|/usr/bin/mplayer -vo null -nolirc -ao alsa -cache 512 -really-quiet|, $newpath);
238      #print STDERR "About to exec: ", Dumper([@args]);
239      exec(@args) ||
240          die "Couldn't exec";
241  }
242}
243
244# ID3 comments often have useless crap because tools like iTunes were
245# written by drooling idiots
246sub filter_comment {
247  my $comment = shift(@_);
248
249  if ($comment =~ /^engiTunes_CDDB/) {
250    return undef;
251  }
252  return $comment;
253}
254
255
Note: See TracBrowser for help on using the repository browser.