source: gutenbach/debian/lib/sipbmp3-filter @ ba06c7f

debianmacno-cupsnodebathenaweb
Last change on this file since ba06c7f was ba06c7f, checked in by root <root>, 16 years ago

Use mplayer for all audio playing

  • Property mode set to 100755
File size: 5.3 KB
RevLine 
[b0d9dda]1#!/usr/athena/bin/perl
2# Play the data on STDIN as an audio file
3#
[ba06c7f]4# $Id: sipbmp3-filter,v 1.16 2008-09-27 09:00:41 root 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
15
16use Getopt::Std;
17
[ba06c7f]18use Image::ExifTool qw(ImageInfo);
19use File::Spec::Functions;
20use LWP::UserAgent;
21use Data::Dumper;
22
23my $ua = new LWP::UserAgent;
24
25close(STDERR);
26open(STDERR, ">>", "/tmp/sipbmp3.log") or warn "Couldn't open log: $!";
27
28$ENV{"TERM"}="vt100";
[b0d9dda]29
[148111e]30print STDERR "STDERR FROM SPOOL FILTER\n";
31
[b0d9dda]32# set real uid to be effective uid
33$< = $>;
34
35# Select the correct output device and set the volume
[ba06c7f]36#system("amixer -q set Headphone 100\% unmute");
[b0d9dda]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)
[53d7625]46#  -Z random user-specified options
[b0d9dda]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
62getopt('ACFHJLPQRZacdefhijklnprswxy', \%opts);
63
64# Status messages at start of playback
[6761cd4]65open(ZEPHYR, '|/usr/athena/bin/zwrite -d -n -c sipb-auto -i ' .
[ba06c7f]66  'sipbmp3@zsr -s "SIPB LPR music spooler"');
[b0d9dda]67print(ZEPHYR "User $opts{'n'} on host $opts{'H'} is playing:\n");
68
69# So, the file we're currently processing is "-d/-e".
70
[ba06c7f]71# Read the metadata information from the file.
72my ($filepath) = catfile($opts{'d'}, $opts{'e'});
73my ($fileinfo) = ImageInfo($filepath);
74my ($magic) = $fileinfo->{FileType};
[b0d9dda]75
[ba06c7f]76if ($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}
82elsif (-T $filepath) {
83    $filepath = resolve_external_reference($filepath, \%opts);
84}
85close(ZEPHYR);
86play_mplayer_audio($filepath, \%opts);
[b0d9dda]87
88# Play an external stream reference
[ba06c7f]89sub resolve_external_reference {
[b0d9dda]90    # Retrieve those command line opts.
[ba06c7f]91    my ($filepath, $opts) = @_;
[b0d9dda]92
93    my $format, $uri, $userpass;
94
[ba06c7f]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 {
[82d34e6]112        print ZEPHYR "Couldn't read URI for external reference\n";
113        close(ZEPHYR);
[ba06c7f]114        return $filepath;
[b281379]115    }
[b0d9dda]116
[ba06c7f]117    if ($format eq "SHOUTCAST") {
[53d7625]118        print ZEPHYR "Shoutcast playlist...\n";
119        #Don't close ZEPHYR yet, will print the name of the stream if available
[ba06c7f]120        return &get_shoutcast($uri);
121    } elsif ($format eq "MP3") {
122    } elsif ($format eq "OGG") {
[b0d9dda]123    } else {
124      print ZEPHYR "Unrecognized stream format: $format\n";
125    }
[ba06c7f]126    return $uri;
[b0d9dda]127}
128
[53d7625]129# Process a Shoutcast playlist
130# get_shoutcast(URI)
131sub get_shoutcast {
132  my $uri = shift(@_);
133 
[ba06c7f]134  my $response = $ua->get($uri);
135
136  foreach (split("\n", $response->content())) {
[53d7625]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}
[b0d9dda]151
[ba06c7f]152sub play_mplayer_audio {
153    my ($filepath, $opts) = @_;
[b0d9dda]154
[ba06c7f]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    }
[b281379]182  else { # child
183      # redirect STDERR to STDOUT
184      open STDERR, '>&STDOUT';
[ba06c7f]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) ||
[6761cd4]190          die "Couldn't exec";
[b0d9dda]191  }
192}
193
194# ID3 comments often have useless crap because tools like iTunes were
195# written by drooling idiots
196sub filter_comment {
197  my $comment = shift(@_);
198
199  if ($comment =~ /^engiTunes_CDDB/) {
200    return undef;
201  }
202  return $comment;
203}
204
205
Note: See TracBrowser for help on using the repository browser.