Streaming music over WebRTC cutting in and out

Viewed 1125

We would like to be able to play music in another tab (say YouTube, Spotify, Soundcloud, etc) and then stream that over a WebRTC connection to other peers.

We are doing this through the screenshare and it's mostly working, but the music will sometimes cut in and out for the listeners, giving it a choppy sound. In other words, it sounds smooth to the person sending it (ie sharing it from the originating URL), but it sounds choppy to the on the receiving side of the WebRTC connection.

Any thoughts on what might be causing this? Is this a buffering issue? If so, is it more likely buffering on the sending or the receiving side?

Thanks so much for any help!

1 Answers

WebRTC favors low latency over quality, with the goal of ensuring you can have normal speech communication. To do this, a lot of things happen to your audio:

  • Playback rate is constantly changed. If playback gets behind, the rate speeds up. If it's too far ahead, it slows down.
  • There is a very small buffer, creating more opportunities for the playback buffer run dry.
  • If packets are lost, the audio for their time is simply discarded... skipped over. Playback isn't likely to buffer a bit and then continue.
  • When audio is lost, a bit of a trail-off is synthesized. This is fine for speech, but sounds bad for music.

On the media capture end, there are also audio "enhancements" designed for dealing with bad webcam microphones which can sometimes be applied to other mediastreams if configured incorrectly. These include:

  • Echo cancellation
  • Noise reduction
  • Automatic gain control

Finally, it's usually the case that audio bitrates are quite low by default. You'll usually have to munge the SDP if you want stereo high quality audio.

All this to say, WebRTC might not be the right choice for you if you are concerned with quality. I often resort to the MediaRecorder API.

Related