1

What I intend to achieve, is a page that when a client is to connect, the page is to constantly read from a local ice-cast server (http://127.0.0.1:8000/stream.mp3), and echo the stream back to the client, from there, the client can be play it back in a basic audio tag.

<?php
header("Content-Transfer-Encoding: binary"); 
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-Disposition: attachment; filename="stream.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
print file_get_contents("http://127.0.0.1:443/stream.mp3");

With this code it only eats up ram and returns nothing useful to the client, I'm thinking something along the lines of waiting until a megabyte buffer is full, then echoing it to the client. But idk, so yeah.

Please note that I'm not that experienced with php. Thanks!

LaptonSpark
  • 21
  • 1
  • 5

1 Answers1

2

file_get_contents attempts to read a stream up to the end, and since you're trying to read from a broadcast server there will be no end.

If HTML5 is an option, the following may work.

<audio autoplay>
  <source src="http://127.0.0.1:443/stream.mp3" type="audio/mpeg">      
</audio>

Alternative solution:

<?php
ob_start();
header("Content-Transfer-Encoding: binary"); 
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-Disposition: attachment; filename="stream.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
$handle = fopen("http://127.0.0.1:443/stream.mp3");

while (($data = fread($handle, $bufferSize)) { //Buffer size needs to be large enough to not break audio up while getting the next part
      echo $data;
      ob_flush();
      flush();
      set_time_limit(30); // Reset the script execution time to prevent timeouts since this page will probably never terminate. 
}
apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • Sorry, but I intend to host ice-cast locally, and have the http server port forwarded. And then the clients connecting to the http server will only be allowed to access ../stream.mp3 somehow, without access to any other part of ice-cast's interface. – LaptonSpark Mar 30 '16 at 11:13
  • I've noted down an alternative (not sure if it works, haven't tested it, but I don't see why it wouldn't). IIS Express unfortunately does not support reverse-proxying (according to http://stackoverflow.com/questions/7492965/can-iis-express-support-reverse-proxies) – apokryfos Mar 30 '16 at 11:29
  • PLease share solution to this and tag me – Elated Coder Nov 29 '17 at 15:11