I'm trying to check if a webm Icecast stream is on. For HLS streams I use axios.head method like this:
try {
let res = await axios.head(url);
return /2\d\d/.test('' + res.status);
} catch (err) {
console.log('err', url, err);
return false;
}
And it works fine. But in the process of debugging I learned that Icecast doesn't support HEAD method.
OPTIONS method also fails for Icecast streams ( I get net::ERR_FAILED). I can't do a get to a stream as it would get the whole stream. I just want to make sure that it's active.
How can I check that a webm stream from Icecast exists ?
EDIT
Let me explain a bit futher. This is a generic stream site. The user informs several live streams that he/she wants to see and their time. These streams can be anywhere in the web. At the start time, the user goes to a player page and plays the stream if it's active.
The "if it's active" part is what I'm trying to to here.
Icecast doesn't support CORS properly, so at the moment there isn't a good way to do this via Fetch or XHR.
However, it seems to me that all you need to do is attempt to play the stream. Then, see if an error occurs.
const audio = new Audio('https://example.com/stream');
audio.addEventListener('error', () => {
// Stream didn't load, so in all probability it's not online!
});
audio.play(); // This needs to happen on a trusted event, like a click handler, due to autoplay policy