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