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