SkipHuffman has asked for the wisdom of the Perl Monks concerning the following question:

I would like to control the sound card on a remote PC through a perl script on a web server.

Basically I have an old PC hooked up to my household sound system in a media closet. I can SSH to it and run mplayer to play a podcast or mp3 music over my home sound system. Works great. But I would like to make it a little more spouse and mother in law friendly by adding a web interface.

I built a little script, see below. But when I run this through a web server, it looks like it is running, but no sound comes out. I am guessing that the sound card cannot be accessed by the web server. How do I break through this?

#!/usr/bin/perl -wT use CGI qw(:cgi); use CGI::Carp qw(fatalsToBrowser); use CGI::Carp qw(fatalsToBrowser); $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin'; # my $input = param('input'); # Hardcoded file because I am lazy. Add input later. my $input = '/share/Music/podcasts/071115/sd20071114.mp3'; print "Content-type: text/html\n\n"; print "<center><h2>$input</h2></center>"; system "/usr/bin/mplayer $input"

Skip

Replies are listed 'Best First'.
Re: Sharing Sound card / Remote Media PC.
by Sixtease (Friar) on Nov 15, 2007 at 15:57 UTC
    Maybe try adding the user apache to audio group? Or some such?

      Thank you, I added the users "apache" and "nobody" to my audio group and all seems well. (edit: This was on my Mandrade development system. I had to add user "www-data" as well for the Debian media server.)

      Skip
Re: Sharing Sound card / Remote Media PC.
by erroneousBollock (Curate) on Nov 15, 2007 at 15:59 UTC
    Your sound card's device node under /dev will be owned by a group called audio (or similar).

    Add your web-server's user (eg: www-data, or nobody) to that group.

    -David

      Thank you, that sounds right. But...

      I have added the "nobody" id and the "apache" id to the group "audio" which owns /dev/audio (which is a link to /dev/sound/audio, which is owned by root.) But it seems to make no difference.

      I can play my script from the command line, either locally or through ssh, but not through a web form. (useless web form below).

      mplayer is running, I can see the feedback on the web browser, but no sound.

      <html> <head> <title> Test file </title> </head> <body> <h1>Test file</h1> <p>test data <form action="/cgi-bin/PlayPod.pl"> <input type="submit" value="play pod"> </form> </body> </html>

      Skip
        Two things:

        Are you sure the webserver is running as nobody?

        Does PlayPod.pl know how to start the music and give control back to the initiating terminal (in this case the web-server via fork) ?

        -David

Re: Sharing Sound card / Remote Media PC.
by amarquis (Curate) on Nov 15, 2007 at 16:44 UTC

    Side note: Thank you for giving me an excellent idea of something to do with this retired PC.

    Edit: In regards to your reply above, what user owns the instance of MPlayer that your script starts up?

      Works very nicely. One suggestion, make sure the audio is working properly before putting the machine in the closet. I spent a week thinking I was hunting down a connection problem between the PC and the sound system, when the problem was an upgrade I had made to the PC which broke the audio. It is a whole lot easier to debug on the workbench than up on a ladder.

      Skip
Re: Sharing Sound card / Remote Media PC.
by shmem (Chancellor) on Nov 16, 2007 at 01:14 UTC

    Just to enhance your interface a bit...

    I have this one running on a linux box for some years. Plays mp3s via The Music Player Daemon, handles audio CDROMs and runs as a plain CGI with hand-crufted AJAX stuff. I did this before knowing about CGI::Ajax ;-)

    Ah, and the rationale was that mpd clients for Windows were very unstable at that time, and mpd didn't handle audio CDs (at that time; does it now?). Don't beat me too much, it's a hack, but heck, it does its job.

    HTML::Writer is here at PerlMonks. There are some bits of german in this cgi script, but they should be clear from context - and there's babelfish, also... ;-P

    Have fun.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Sharing Sound card / Remote Media PC.
by dk (Chaplain) on Nov 19, 2007 at 11:01 UTC
    Run mplayer with -v option, so it tells what sound output (if any) it uses. Mplayer has a nasty habit of silently not playing the video/audio/subtitles/whatever it cannot access.

    Also, note that mplayer shouldn't be really running from under cgi script, as 1) apache will terminate it sooner or later 2) other web users might want to stop it and run another track (not a problem if you have only one terminal though :) A controlling daemon that runs mplayer and accepts commands from cgi would be the proper solution, but that's probably too heavyweight for your situation.

      Thanks. I got it working acceptably well. Though a daemon would improve things.

      Something I discovered. Don't try to use -quiet or -really-quiet both make it more complex to control the system. Without those, the listener can just close the browser window to turn off the sound. Also with those switches, mplayer fails to move from one cut to another if given a wildcard on the command line.

      Skip