[b0d9dda] | 1 | #!/usr/athena/bin/perl |
---|
| 2 | # Play the data on STDIN as an audio file |
---|
| 3 | # |
---|
[6d813c3] | 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 $ |
---|
[b0d9dda] | 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 |
---|
[c711ed9] | 15 | # apparently neither does Quentin Smith <quentin@mit.edu> |
---|
[b0d9dda] | 16 | |
---|
[ba06c7f] | 17 | use Image::ExifTool qw(ImageInfo); |
---|
| 18 | use File::Spec::Functions; |
---|
[908107d] | 19 | use File::Temp qw{tempdir}; |
---|
| 20 | use File::Basename qw(basename); |
---|
[ba06c7f] | 21 | use LWP::UserAgent; |
---|
| 22 | use Data::Dumper; |
---|
| 23 | |
---|
[3a900ef] | 24 | my $zephyr_class = "sipb-auto"; |
---|
| 25 | my $host = "zsr"; |
---|
| 26 | |
---|
| 27 | # Configuration |
---|
| 28 | my $config_file = "/etc/sipbmp3-filter-config.pl"; |
---|
| 29 | if (-r $config_file) { |
---|
| 30 | # Inline the configuration file |
---|
| 31 | local $/; |
---|
| 32 | my $fh; |
---|
| 33 | open $fh, $config_file; |
---|
| 34 | eval <$fh>; |
---|
| 35 | } |
---|
| 36 | |
---|
[ba06c7f] | 37 | my $ua = new LWP::UserAgent; |
---|
| 38 | |
---|
| 39 | close(STDERR); |
---|
| 40 | open(STDERR, ">>", "/tmp/sipbmp3.log") or warn "Couldn't open log: $!"; |
---|
| 41 | |
---|
| 42 | $ENV{"TERM"}="vt100"; |
---|
[b0d9dda] | 43 | |
---|
[148111e] | 44 | print STDERR "STDERR FROM SPOOL FILTER\n"; |
---|
| 45 | |
---|
[b0d9dda] | 46 | # set real uid to be effective uid |
---|
| 47 | $< = $>; |
---|
| 48 | |
---|
| 49 | # Select the correct output device and set the volume |
---|
[ba06c7f] | 50 | #system("amixer -q set Headphone 100\% unmute"); |
---|
[b0d9dda] | 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) |
---|
[53d7625] | 60 | # -Z random user-specified options |
---|
[b0d9dda] | 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 | |
---|
[06133dc] | 75 | printf(STDERR "Got \@ARGV: %s\n", Dumper(\@ARGV)); |
---|
| 76 | |
---|
| 77 | my %opts; |
---|
| 78 | |
---|
| 79 | my @NEWARGV; |
---|
| 80 | |
---|
| 81 | foreach 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; |
---|
[b0d9dda] | 90 | |
---|
[b011fb0] | 91 | printf(STDERR Dumper(\%opts)); |
---|
| 92 | |
---|
[b0d9dda] | 93 | # Status messages at start of playback |
---|
[3a900ef] | 94 | open(ZEPHYR, '|/usr/athena/bin/zwrite -d -n -c '. $zephyr_class .' -i ' . |
---|
| 95 | 'sipbmp3@'.$host.' -s "SIPB LPR music spooler"'); |
---|
[c711ed9] | 96 | print(ZEPHYR "$opts{'n'}\@$opts{'H'} is playing:\n"); |
---|
[b0d9dda] | 97 | |
---|
| 98 | # So, the file we're currently processing is "-d/-e". |
---|
| 99 | |
---|
[ba06c7f] | 100 | # Read the metadata information from the file. |
---|
| 101 | my ($filepath) = catfile($opts{'d'}, $opts{'e'}); |
---|
| 102 | my ($fileinfo) = ImageInfo($filepath); |
---|
| 103 | my ($magic) = $fileinfo->{FileType}; |
---|
[b0d9dda] | 104 | |
---|
[ba06c7f] | 105 | if ($magic) { |
---|
[c711ed9] | 106 | printf(ZEPHYR "%s file %s\n", $magic, $opts{'J'}); |
---|
[de4ce86] | 107 | printf(ZEPHYR "\@b{%s}\n", $fileinfo->{'Title'}) if exists $fileinfo->{'Title'}; |
---|
[4cc6b4d] | 108 | foreach my $key (qw/Artist Album AlbumArtist/) { |
---|
[c711ed9] | 109 | printf(ZEPHYR "%s\n", $fileinfo->{$key}) if exists $fileinfo->{$key}; |
---|
[ba06c7f] | 110 | } |
---|
[a041c73] | 111 | my $tempdir = tempdir(); |
---|
[6d813c3] | 112 | $opts{'J'} =~ s/_mp3/.mp3/; #awful hack -- geofft |
---|
[a041c73] | 113 | my $newpath = $tempdir . '/' . basename($opts{'J'}); |
---|
| 114 | symlink($filepath, $newpath); |
---|
| 115 | $filepath = $newpath; |
---|
[ba06c7f] | 116 | } |
---|
[7e27cc3] | 117 | elsif ($opts{'C'} eq 'Z') { |
---|
[ba06c7f] | 118 | $filepath = resolve_external_reference($filepath, \%opts); |
---|
[b011fb0] | 119 | print STDERR "Resolved external reference to $filepath\n"; |
---|
[73cfabc] | 120 | printf(ZEPHYR "%s\n", $filepath); |
---|
[b011fb0] | 121 | close(ZEPHYR); |
---|
[ba06c7f] | 122 | } |
---|
[7e27cc3] | 123 | elsif (-T $filepath) { |
---|
| 124 | split_playlist($filepath, \%opts); |
---|
| 125 | close(ZEPHYR); |
---|
| 126 | exit 0; |
---|
| 127 | } |
---|
[a041c73] | 128 | |
---|
[7e27cc3] | 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')); |
---|
[ba06c7f] | 131 | close(ZEPHYR); |
---|
| 132 | play_mplayer_audio($filepath, \%opts); |
---|
[b0d9dda] | 133 | |
---|
[a041c73] | 134 | if ($magic) { |
---|
| 135 | unlink($newpath); |
---|
| 136 | rmdir($tempdir); |
---|
| 137 | } |
---|
| 138 | |
---|
[b0d9dda] | 139 | # Play an external stream reference |
---|
[ba06c7f] | 140 | sub resolve_external_reference { |
---|
[b0d9dda] | 141 | # Retrieve those command line opts. |
---|
[ba06c7f] | 142 | my ($filepath, $opts) = @_; |
---|
[b0d9dda] | 143 | |
---|
| 144 | my $format, $uri, $userpass; |
---|
| 145 | |
---|
[ba06c7f] | 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 { |
---|
[82d34e6] | 162 | print ZEPHYR "Couldn't read URI for external reference\n"; |
---|
[ba06c7f] | 163 | return $filepath; |
---|
[b281379] | 164 | } |
---|
[b0d9dda] | 165 | |
---|
[ba06c7f] | 166 | if ($format eq "SHOUTCAST") { |
---|
[53d7625] | 167 | print ZEPHYR "Shoutcast playlist...\n"; |
---|
| 168 | #Don't close ZEPHYR yet, will print the name of the stream if available |
---|
[ba06c7f] | 169 | return &get_shoutcast($uri); |
---|
| 170 | } elsif ($format eq "MP3") { |
---|
| 171 | } elsif ($format eq "OGG") { |
---|
[b0d9dda] | 172 | } else { |
---|
| 173 | print ZEPHYR "Unrecognized stream format: $format\n"; |
---|
| 174 | } |
---|
[ba06c7f] | 175 | return $uri; |
---|
[b0d9dda] | 176 | } |
---|
| 177 | |
---|
[7e27cc3] | 178 | sub 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 | |
---|
[53d7625] | 196 | # Process a Shoutcast playlist |
---|
| 197 | # get_shoutcast(URI) |
---|
| 198 | sub get_shoutcast { |
---|
| 199 | my $uri = shift(@_); |
---|
| 200 | |
---|
[ba06c7f] | 201 | my $response = $ua->get($uri); |
---|
| 202 | |
---|
| 203 | foreach (split("\n", $response->content())) { |
---|
[53d7625] | 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 | } |
---|
[b0d9dda] | 218 | |
---|
[ba06c7f] | 219 | sub play_mplayer_audio { |
---|
| 220 | my ($filepath, $opts) = @_; |
---|
[b0d9dda] | 221 | |
---|
[ba06c7f] | 222 | # Prepare to write status: |
---|
[3a900ef] | 223 | open(ZEPHYR, '|/usr/athena/bin/zwrite -d -n -c '.$zephyr_class.' -i ' . |
---|
| 224 | 'sipbmp3@'.$host.' -s "SIPB LPR music spooler"'); |
---|
[ba06c7f] | 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 | } |
---|
[b281379] | 249 | else { # child |
---|
| 250 | # redirect STDERR to STDOUT |
---|
| 251 | open STDERR, '>&STDOUT'; |
---|
[ba06c7f] | 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]); |
---|
[a041c73] | 256 | my @args = (qw|/usr/bin/mplayer -vo null -nolirc -ao alsa -cache 512 -really-quiet|, $filepath); |
---|
[b011fb0] | 257 | #print STDERR "About to exec: ", Dumper([@args]); |
---|
| 258 | exec(@args) || |
---|
[6761cd4] | 259 | die "Couldn't exec"; |
---|
[b0d9dda] | 260 | } |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | # ID3 comments often have useless crap because tools like iTunes were |
---|
| 264 | # written by drooling idiots |
---|
| 265 | sub filter_comment { |
---|
| 266 | my $comment = shift(@_); |
---|
| 267 | |
---|
| 268 | if ($comment =~ /^engiTunes_CDDB/) { |
---|
| 269 | return undef; |
---|
| 270 | } |
---|
| 271 | return $comment; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | |
---|