source: gutenbach/debian/lib/sipbmp3-filter @ 3a900ef

debianmacno-cupsnodebathenaweb
Last change on this file since 3a900ef was 3a900ef, checked in by Edward Z. Yang <edwardzyang@…>, 15 years ago

Rewrite to use a config file for Zephyr.

Signed-off-by: Edward Z. Yang <edwardzyang@…>

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