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