I got bored, so I decided to mess around with my webserver. I noticed I had the song "The Ballad of Bilbo Baggins" by Leonard Nimoy on my server. So, I made a simple one-line script that opened that file using readfile(). I used Winamp's built-in networking feature to read it as a streaming server; lo-and-behold, it opened it and was actually streaming it (I watched as data accumulated).
I then wrote a short little loop that picked a random file off of my MP3 drive, and then opened that. I also toyed around with the Content-Disposition and got it to stream the filename however I pleased. I came up with this code as the server:

<?php
$path = "e:\\";
$files = array();
$dir_handle = @opendir($path) or die("Unable to open $path");
while ($file = readdir($dir_handle)) {
  $files[$i] = $file;
  $i ++;
}
closedir($dir_handle);
$num = rand(0,count($files));
$file = $files[$num];
$file = ereg_replace(".mp3","",$file);
$streamname = $file." [Mordecai's Sever]";
header("Content-type: audio/mpeg");
header("Content-Disposition: attachment; filename=$streamname");
readfile("e:\\$file.mp3");
unset($files);
?>

My question, to anyone who may know, is how would one go about streaming from a certain part of a file (much like ShoutCast), or perhaps multiple files, one after the other. Currently, Winamp disconnects after the file is done, and reconnects, as it is the only "song" in the playlist.
This works fine, but it opens the file each time from the beginning. I don't really care where this goes, but I want to know how to make a sort of ShoutCast server. 🙂

Ideas, anyone?

Edit: For some odd reason, the MB stripped the escaped slashes. All the code is bug-free.

    i dont know if there is any code in php for starting in middle of the song, but in real player script you can do what you want to do. i mean you can stop or start at the middle of the file and loop again and again, also you can stream multiple files using real player script.

      use fseek() and fread() rather than readfile(). i don't know how you'd get this offset from the winamp client though.

        like you said, this is fine for just the one file being played from start to finish... if you want a shoutcast type program, you're going to need to have to make your own server program... this program is constantly keeping track of what mp3 is playing and where the filepointer is, and every time a user wants to listen to the stream, winamp gets that information and plays the music from that point...

        shoutcast's server program actually does two things...

        first, it constantly recieves the mp3 stream from whatever DJ is connected to it using the DSP plugin for winamp (installed on the DJ's machine). Then, it forwards that information to any user that requests it, again through the DSP plugin.

        Now your code is kind of confusing to me... how does winamp's built-in networking feature work?

          It reads the file as fast as it is sent into the buffer, and when it gets EOF, it disconnects. I can make it function almost as well as a ShoutCast server now. I don't really see a point in it, though. 😉

            It seems to stop streaming and disconnects after about 45-50 seconds of streamed data. From one file, it sent 786188 bytes of data before it disconnected.

            I'll mess around with it some more, I don't know exactly why it's doing that, though.

              Originally posted by Mordecai
              It seems to stop streaming and disconnects after about 45-50 seconds of streamed data. From one file, it sent 786188 bytes of data before it disconnected.

              I'll mess around with it some more, I don't know exactly why it's doing that, though.

              Assuming that the "45-50 seconds" refers to runtime and not play time, then the first thing that creeps into my teeny tiny is that it breaks down as PHP: 30 seconds, WinAmp: 15-20 seconds. Then PHP times out 'cos it's exceeded its time limit.

                Originally posted by Weedpacket
                Then PHP times out 'cos it's exceeded its time limit.

                That's exactly what I figure, and fear. Winamp only reads a certain ammount of data into its buffer to save memory, so it takes longer for it to read the stream. PHP can only output it as fast as it is being retrieved, so I'm assuming there's 15-20 seconds of buffer time and 30 seconds of PHP reading. Also, I know that PHP can only use 8MB of memory per script, and I'm almost certain that this script comes close. Do you (or anyone) think that if I unset the variable after it's sent that it will run too slowly?

                  Okay; keeping in mind that I haven't actually dealt with this sort of thing before...

                  First off, you can change PHP's timeout with [man]set_time_limit[/man] - that may help.

                  Second ... well, I'm not too sure about how PHP buffers stuff from readfile() (and I suspect the server's buffering strategy might play a role also); perhaps if you could use some system utility to monitor memory usage while you're testing, you can see if PHP is using too much. If that is the case, then I might suggest replacing the readfile with something a bit lower-level: fopen(), and until feof(), fread() a chunk, echo it and flush(). Then repeat for the next chunk. That should give you a bit more control over memory consumption and buffering at least at the PHP end.

                    Originally posted by Weedpacket
                    I might suggest replacing the readfile with something a bit lower-level: fopen(), and until feof(), fread() a chunk, echo it and flush(). Then repeat for the next chunk. That should give you a bit more control over memory consumption and buffering at least at the PHP end.

                    Yeah, I'm already using fopen() and other file functions rather than readfile(). It's being read in 128Kb chunks and then echoed to the server. Since I rewrote over the variable, the script only used about 128Kb, so it's fine.

                    One weird thing I've noticed is that when I try to use my startup mp3, it doesn't actually play it, but skips over it... it's really weird. It's not like the script is sending an EOF to Winamp and disconnecting, it just ignores the file and doesn't play anything at all.

                      4 months later

                      I'm working on a script to broadcast music via PHP to a ShoutCast DNAS. I must get a realtime converter in it aswell to make it stream using the right bitrate all along, but I'll come to that later. The issue I have now is that I don't know how much or how often to send data to the server.

                      The questions are: Does any of you know in which rate to send data and are any of you interested in helping out with this project?

                        Write a Reply...