Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Node.js to stream audio to html5 audio tag and bypass iOS limitation

I'm currently working on a project that use HTML5 audio and we have problems with the limitations on iOS devices (mostly multiple audio channels limitation).

I was thinking that it may be possible to control the sounds server-side using node.js. The client would connect to a single audio url and the node server will stream live sounds trough this single channel doing all the mixing and transitions live on the server.

Is it technically possible? Can you parse audio on node.js and do some live mixing? Do you think the server will explode with more than 5 simultaneous clients?

Thanks

like image 898
David Mongeau-Petitpas Avatar asked Jun 07 '12 02:06

David Mongeau-Petitpas


People also ask

What is <HTML5> audio?

HTML5 defines a new element which specifies a standard way to embed an audio file on a web page using the <audio> element. Your browser does not support the audio tag.

How to embed audio file in HTML5?

HTML5 defines a new element which specifies a standard way to embed an audio file on a web page using the <audio> element. Your browser does not support the audio tag. The above <audio> element adds audio controls like play, pause and volume automatically on the web page.

How to record audio from a binaryjs stream?

First add the BinaryJS library. Now start a connection. When ready, create a write stream. Going back to our custom node let's send the audio to the stream. Everything should be ready on the client side now. Our recorderProcess function is called for each audio chunk, and each is sent to the server. But we aren't ready yet!

How to play audio from HTML markup in JavaScript?

Look at the HTML Markup and its output in a browser. Type sample audio url having .mp3 exetension and click on play button. Audio Tag not Supported. Now to apply the action in the above HTML Markup, we need to write some JavaScript. Find the JavaScript methods below: // Global variable to track current file name. // Check for audio element support.


1 Answers

Yes, this is entirely possible, but as Sam pointed out, buffering is an issue. There is significant delay that builds up over several points:

  • Getting control commands from client
  • Mixing raw PCM audio into an internal buffer
  • Encoding data with the correct codec (requires you to do this in chunks)
  • Sending audio data
  • Buffering client-side
  • Playback buffer on client-side

Add this all up, and even if you get it working very quickly, you're still looking at a couple seconds delay.

If you do decide to go this route, you'll need an external application (or write your own Node extension) to do the mixing and encoding for you. Sox is probably a decent choice, and FFMPEG includes many codecs for your use. You can use both over STDIO.

Regarding server load... this is not a light-weight kind of process, but you would have to profile it to see how bad it really is. I suggest you make other attempts at playing multiple audio streams at once client-side before attempting something like this.

like image 104
Brad Avatar answered Nov 01 '22 14:11

Brad