I'm trying to extract icecast metadata from streams. I have code that works for some streams and not for others. The issue is that some streams don't return the icymetaint value and that's where the code gets lost.
I can't get the icymetaint header from this stream: http://radio.hbr1.com:19800/tronic.ogg
But when I put it in VLC media player it shows the meta just fine. So what exactly am I missing here? What other ways are there for an icecast stream to transmit metdata? The stream version is Icecast 2.3.3
This is code inside a class to retrieve the metadata and headers:
public function GetDataFromStream($parsedUrl)
{
$returnData = array();
$addr = $parsedUrl['host'];
$addr = gethostbyname($addr);
$sock = fsockopen($addr, $parsedUrl['port'], $errno, $errstr, 5);
$path = isset($parsedUrl['path'])?$parsedUrl['path']:'/';
if ($sock)
{
$request = 'GET '. $path .' HTTP/1.0' . CRLF .
'Host: ' . $parsedUrl['host'] . CRLF .
'Connection: Close' . CRLF .
'User-Agent: ' . $this->useragent . CRLF .
'Accept: */*' . CRLF .
'icy-metadata: 1'.CRLF.
'icy-prebuffer: 65536'.CRLF.
(isset($parsedUrl['user']) ? 'Authorization: Basic ' .
base64_encode($parsedUrl['user'] . ':' . $parsedUrl['pass']) . CRLF : '').
'X-TipOfTheDay: Winamp "Classic" rulez all of them.' . CRLF . CRLF;
if (fwrite($sock, $request))
{
$theaders = $line = '';
while (!feof($sock))
{
$line = fgets($sock, 4096);
if('' == trim($line))
break;
$theaders .= $line;
}
$theaders = explode(CRLF, $theaders);
foreach ($theaders as $header)
{
$t = explode(':', $header);
if (isset($t[0]) && trim($t[0]) != '')
{
$name = preg_replace('/[^a-z][^a-z0-9]*/i','', strtolower(trim($t[0])));
array_shift($t);
$value = trim(implode(':', $t));
if ($value != '')
{
if (is_numeric($value))
$this->headers[$name] = (int)$value;
else
$this->headers[$name] = $value;
}
}
}
if (isset($this->headers['icymetaint']))
{
$metainterval = $this->headers['icymetaint'];
$intervals = 0;
$metadata = '';
while(1)
{
$data = '';
while(!feof($sock))
{
$data .= fgetc($sock);
if (strlen($data) >= $metainterval)
break;
}
$len = join(unpack('c', fgetc($sock))) * 16;
if ($len > 0)
{
$metadata = str_replace("\0", '', fread($sock, $len));
break;
}
else
{
$intervals++;
if ($intervals > 100) break;
}
}
$metarr = explode(';', $metadata);
foreach ($metarr as $meta)
{
$t = explode('=', $meta);
if (isset($t[0]) && trim($t[0]) != '')
{
$name = preg_replace('/[^a-z][^a-z0-9]*/i','', strtolower(trim($t[0])));
array_shift($t);
$value = trim(implode('=', $t));
if (substr($value, 0, 1) == '"' || substr($value, 0, 1) == "'")
$value = substr($value, 1);
if (substr($value, -1) == '"' || substr($value, -1) == "'")
$value = substr($value, 0, -1);
if ($value != '')
{
$tmp = &$this->metadata;
$tmp[$name] = $value;
}
}
}
$this->valid = true;
}
else
{
$this->valid = false;
}
fclose($sock);
}
else
echo 'unable to write.';
}
else
//echo 'no socket '.$errno.' - '.$errstr.'.';
;
}