私は Node.js でバイナリ ストリームを実験してきましたが、驚いたことに、node-radio-stream を使用して Shoutcast ストリームを取得し、それをチャンク エンコーディングを使用して HTML5 要素にプッシュする実際の動作デモがあります。ただし、Safariでのみ機能します。
ここに私のサーバーコードがあります:
var radio = require("radio-stream");
var http = require('http');
var url = "http://67.205.85.183:7714";
var stream = radio.createReadStream(url);
var clients = [];
stream.on("connect", function() {
console.error("Radio Stream connected!");
console.error(stream.headers);
});
// When a chunk of data is received on the stream, push it to all connected clients
stream.on("data", function (chunk) {
if (clients.length > 0){
for (client in clients){
clients[client].write(chunk);
};
}
});
// When a 'metadata' event happens, usually a new song is starting.
stream.on("metadata", function(title) {
console.error(title);
});
// Listen on a web port and respond with a chunked response header.
var server = http.createServer(function(req, res){
res.writeHead(200,{
"Content-Type": "audio/mpeg",
'Transfer-Encoding': 'chunked'
});
// Add the response to the clients array to receive streaming
clients.push(res);
console.log('Client connected; streaming');
});
server.listen("8000", "127.0.0.1");
console.log('Server running at http://127.0.0.1:8000');
私のクライアントコードは単純です:
<audio controls src="http://localhost:8000/"></audio>
これは Mac の Safari 5 では問題なく動作しますが、Chrome や Firefox では何もしないようです。何か案は?
エンコーディングの問題、または部分的に実装された HTML5 機能を含む可能性のある候補...