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