I need to have real-time streaming audio from one client to a server to several listener clients.
Currently, I have a recording from a client that is running and transmitting audio through socket.io to the server. The server receives this data and must transmit audio (also via socket.io?) To clients who want to listen to this stream. This should be as real as possible (minimize delay).
I use GetUserMedia to record a microphone (browser compatibility is not important here). I want clients to use the HTML5 audio tag to listen to the stream. The data received on the server are chunks (currently packed in 700), packed in blob with type audio / wav.
This is my code to send to the server:
mediaRecorder.ondataavailable = function(e) { this.chunks.push(e.data); if (this.chunks.length >= 700) { this.sendData(this.chunks); this.chunks = []; } }; mediaRecorder.sendData = function(buffer) { blob = new Blob(buffer, { 'type' : 'audio/wav' }); socket.emit('voice', blob); }
On the server, I can send pieces to the client just like this:
socket.on('voice', function(blob) { socket.broadcast.emit('voice', blob); });
On the client, I can play like this:
var audio = document.createElement('audio'); socket.on('voice', function(arrayBuffer) { var blob = new Blob([arrayBuffer], { 'type' : 'audio/wav' }); audio.src = window.URL.createObjectURL(blob); audio.play(); });
This works for the first drop of pieces that I am sending, but you are not allowed to change to audio.src to a new URL source so that this is not a working solution.
I think I need to create some kind of stream on the server that I can put in the HTML5 audio tag on the listening clients, but I don’t know how to do it. The resulting droplets with pieces should be added to this stream in real time.
What is the best way to do this? Am I doing this right from the client microphone to the server?