source: gutenbach/debian/lib/sipbmp3-filter @ a041c73

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

r1.25 broke playing non-files. Fix that.

  • 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.26 2009-02-20 00:27:17 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    my $tempdir = tempdir();
99    my $newpath = $tempdir . '/' . basename($opts{'J'});
100    symlink($filepath, $newpath);
101    $filepath = $newpath;
102}
103elsif ($opts{'C'} eq 'Z') {
104    $filepath = resolve_external_reference($filepath, \%opts);
105    print STDERR "Resolved external reference to $filepath\n";
106    printf(ZEPHYR "%s\n", $filepath);
107    close(ZEPHYR);
108}
109elsif (-T $filepath) {
110    split_playlist($filepath, \%opts);
111    close(ZEPHYR);
112    exit 0;
113}
114
115#printf(STDERR "Job priority %s\n", $opts{'C'}) if $opts{'C'} eq 'Z';
116#printf(ZEPHYR "Job priority %s\n", $opts{'C'}) if ($opts{'C'} && ($opts{'C'} ne 'A'));
117close(ZEPHYR);
118play_mplayer_audio($filepath, \%opts);
119
120if ($magic) {
121    unlink($newpath);
122    rmdir($tempdir);
123}
124
125# Play an external stream reference
126sub resolve_external_reference {
127    # Retrieve those command line opts.
128    my ($filepath, $opts) = @_;
129
130    my $format, $uri, $userpass;
131
132    if (<STDIN> =~ /^(\S+)/) {
133        $uri=$1;
134
135        my $response = $ua->head($uri);
136       
137        $contenttype=($response->content_type() or "unknown");
138       
139        if ($contenttype eq "audio/mpeg") { $format="MP3" }
140        elsif ($contenttype eq "application/x-ogg") { $format="OGG" }
141        elsif ($contenttype eq "application/ogg") { $format="OGG" }
142        elsif ($contenttype eq "audio/x-scpls") { $format="SHOUTCAST" }
143        else {
144            print ZEPHYR
145                "Unknown Content-Type $contenttype for URI $uri\n";
146        }
147    } else {
148        print ZEPHYR "Couldn't read URI for external reference\n";
149        return $filepath;
150    }
151
152    if ($format eq "SHOUTCAST") {
153        print ZEPHYR "Shoutcast playlist...\n";
154        #Don't close ZEPHYR yet, will print the name of the stream if available
155        return &get_shoutcast($uri);
156    } elsif ($format eq "MP3") {
157    } elsif ($format eq "OGG") {
158    } else {
159      print ZEPHYR "Unrecognized stream format: $format\n";
160    }
161    return $uri;
162}
163
164sub split_playlist {
165    my ($file, $opts) = @_;
166
167    my $i = 0;
168   
169    while (<STDIN>) {
170        chomp;
171        if (/^([^#]\S+)/) {
172            printf (STDERR "Found line: %s\n", $_);
173            open(LPR, "|-", qw/mit-lpr -Psipbmp3@localhost -CZ/, '-J'.$opts->{J});
174            print LPR $1;
175            close(LPR);
176        $i++;
177        }
178    }
179    printf(ZEPHYR "Playlist containing %d valid entries, split into separate jobs.\n", $i);
180}
181
182# Process a Shoutcast playlist
183# get_shoutcast(URI)
184sub get_shoutcast {
185  my $uri = shift(@_);
186 
187  my $response = $ua->get($uri);
188
189  foreach (split("\n", $response->content())) {
190      if (/^File\d+=(\S+)/) {
191          push(@uris, $1);
192      }
193      if (/^Title\d+=(.+)$/) {
194          push(@titles, $1);
195      }
196  }
197 
198  # choose a random server
199  $server = int(rand scalar(@uris));
200  # print the name of the stream if available
201  print ZEPHYR "$titles[$server]\n";
202  return $uris[$server];
203}
204
205sub play_mplayer_audio {
206    my ($filepath, $opts) = @_;
207
208    # Prepare to write status:
209    open(ZEPHYR, '|/usr/athena/bin/zwrite -d -n -c sipb-auto -i ' .
210         'sipbmp3@zsr -s "SIPB LPR music spooler"');
211   
212    # fork for mpg123
213    my $pid = open(MP3STATUS, "-|");
214    unless (defined $pid) {
215        print ZEPHYR "Couldn't fork: $!\n";
216        close(ZEPHYR);
217        return;
218    }
219   
220    if ($pid) { #parent
221        # Check if there were any errors
222        if ($_ = <MP3STATUS>) {
223            print ZEPHYR "Playback completed with the following errors:\n";
224            print ZEPHYR $_;
225            while (<MP3STATUS>) {
226                print ZEPHYR $_;
227            }
228        } else {
229            print ZEPHYR "Playback completed successfully.\n";
230        }
231        close(MP3STATUS) || print ZEPHYR "mplayer exited $?\n";
232       
233        close(ZEPHYR);
234    }
235  else { # child
236      # redirect STDERR to STDOUT
237      open STDERR, '>&STDOUT';
238      # make sure that mplayer doesn't try to intepret the file as keyboard input
239      close(STDIN);
240      open(STDIN, "/dev/null");
241      #print STDERR Dumper([qw|/usr/bin/mplayer -nolirc -ao alsa -quiet|, $filepath]);
242      my @args = (qw|/usr/bin/mplayer -vo null -nolirc -ao alsa -cache 512 -really-quiet|, $filepath);
243      #print STDERR "About to exec: ", Dumper([@args]);
244      exec(@args) ||
245          die "Couldn't exec";
246  }
247}
248
249# ID3 comments often have useless crap because tools like iTunes were
250# written by drooling idiots
251sub filter_comment {
252  my $comment = shift(@_);
253
254  if ($comment =~ /^engiTunes_CDDB/) {
255    return undef;
256  }
257  return $comment;
258}
259
260
Note: See TracBrowser for help on using the repository browser.