Live audio via socket.io 1.0

Like from socket.io

Binary streaming

Starting from 1.0, you can send any drops back and forth: image, audio, video.

Now I wonder if this cannot be the solution to something that I have been trying to achieve lately.

I was really looking for a way to transfer live audio from (A - that is, microphone input ...) to all clients connected to my site. Is something like this possible? I was messing around with WebRTC examples ( https://www.webrtc-experiment.com/ ), but I was not able to manage the target for several connected clients.

My idea is about something like getUserMedia or any other sound source (PCM, whatever ..) on side A, cut into pieces and provided to the client and played back, for example, using the html5 audio element or something else. I need to do this as much as possible in real time, no cry / ice services were fast enough (indeed, they are not suitable for my problem, they are designed to be used in this way), and I do not care about the sound quality. Cross-platform compatibility would be awesome.

Is this possible? Using socket.io as a way to provide this data to clients?

I would be very grateful for any link, hint or source that could help me with this. Thank you very much.

+6
source share
3 answers

This example shows how to use MediaRecorder to download sound, and then forward it using socket.io . This code will only be broadcast after you call mediaRecorder.stop() . You can select the broadcast inside ondataavailable . If you do, you can pass timeslice to mediaRecorder.start() so that it does not run ondataavailable so often.

This decision is not really lively, but I think it will help people who come back and find this question.

Client code

 var constraints = { audio: true }; navigator.mediaDevices.getUserMedia(constraints).then(function(mediaStream) { var mediaRecorder = new MediaRecorder(mediaStream); mediaRecorder.onstart = function(e) { this.chunks = []; }; mediaRecorder.ondataavailable = function(e) { this.chunks.push(e.data); }; mediaRecorder.onstop = function(e) { var blob = new Blob(this.chunks, { 'type' : 'audio/ogg; codecs=opus' }); socket.emit('radio', blob); }; // Start recording mediaRecorder.start(); // Stop recording after 5 seconds and broadcast it to server setTimeout(function() { mediaRecorder.stop() }, 5000); }); // When the client receives a voice message it will play the sound socket.on('voice', function(arrayBuffer) { var blob = new Blob([arrayBuffer], { 'type' : 'audio/ogg; codecs=opus' }); var audio = document.createElement('audio'); audio.src = window.URL.createObjectURL(blob); audio.play(); }); 

Server code

 socket.on('radio', function(blob) { // can choose to broadcast it to whoever you want socket.broadcast.emit('voice', blob); }); 
+3
source

you can see this demo https://github.com/LingyuCoder/SkyRTC-demo hope for help

0
source

In Client Code, you can write setInterval () instead of setTimeout (), and then call mediaRecorder.start () recursively so that every 5 seconds a drop will be emitted continuously.

 setInterval(function() { mediaRecorder.stop() mediaRecorder.start() }, 5000); 

Client code

 var constraints = { audio: true }; navigator.mediaDevices.getUserMedia(constraints).then(function(mediaStream) { var mediaRecorder = new MediaRecorder(mediaStream); mediaRecorder.onstart = function(e) { this.chunks = []; }; mediaRecorder.ondataavailable = function(e) { this.chunks.push(e.data); }; mediaRecorder.onstop = function(e) { var blob = new Blob(this.chunks, { 'type' : 'audio/ogg; codecs=opus' }); socket.emit('radio', blob); }; // Start recording mediaRecorder.start(); // Stop recording after 5 seconds and broadcast it to server setInterval(function() { mediaRecorder.stop() mediaRecorder.start() }, 5000); }); // When the client receives a voice message it will play the sound socket.on('voice', function(arrayBuffer) { var blob = new Blob([arrayBuffer], { 'type' : 'audio/ogg; codecs=opus' }); var audio = document.createElement('audio'); audio.src = window.URL.createObjectURL(blob); audio.play(); }); 

Server code

 socket.on('voice', function(blob) { // can choose to broadcast it to whoever you want socket.broadcast.emit('voice', blob); }); 
0
source

Source: https://habr.com/ru/post/972618/


All Articles