Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up SDP for High quality Opus audio

I have been trying to transmit some high quality audio stream through WebRTC. Opus, the main advertised codec seems perfect since it can support up to 510kbit/s, way more than needed. The problem is, setting up the Webrtc SDP is way less obvious than it seems. Thanks to Muaz Khan great work, I have been able to force it to 128kbit/s. Basically the code looks like that:

 function setBandwidth(sdp) {
 var sdpLines = sdp.split('\r\n');


// Find opus payload.
var opusIndex = findLine(sdpLines, 'a=rtpmap', 'opus/48000');
var opusPayload;
if (opusIndex) {
    opusPayload = '109';
}
sdpLines[opusIndex]='a=rtpmap:'+opusPayload+' opus/48000/2';


   var mediaIndex = findLine(sdpLines, 'm=audio');
sdpLines[mediaIndex]=(sdpLines[mediaIndex].slice(0,(sdpLines[mediaIndex].indexOf("RTP/SAVPF")+10))).concat(opusPayload); 
 var abIndex = findLine(sdpLines, 'a=mid:');
  sdpLines[abIndex]='a=mid:audio\r\nb=AS:300000';

// Find the payload in fmtp line.
  var fmtpLineIndex = findLine(sdpLines, 'a=fmtp:' + opusPayload.toString());

if (fmtpLineIndex == null) {
    sdpLines[opusIndex] = sdpLines[opusIndex].concat('\r\n'+'a=fmtp:' + opusPayload.toString()+ ' minptime=10; useinbandfec=1; maxaveragebitrate='+128*1024+'; stereo=1; sprop-stereo=1 ; cbr=1');
     sdp = sdpLines.join('\r\n');

    return sdp;
}

// Append stereo=1 to fmtp line.
// added maxaveragebitrate here; about 50 kbits/s
// added stereo=1 here for stereo audio
// x-google-min-bitrate=50; x-google-max-bitrate=50
sdpLines[fmtpLineIndex] = sdpLines[fmtpLineIndex].concat('; maxaveragebitrate='+128*1024+'; stereo=1; sprop-stereo=1 ; cbr=1');


sdp = sdpLines.join('\r\n');
return sdp;
}

So now everything is set, both firefox and chrome display the right value for sender and receiver, the communication opens, the music is played!

adding answer-sdp v=0
o=mozilla...THIS_IS_SDPARTA-42.0 502631676322875352 0 IN IP4 0.0.0.0
s=-
t=0 0
a=fingerprint:sha-256.....
a=ice-options:trickle
a=msid-semantic:WMS *
m=audio 9 RTP/SAVPF 109 
c=IN IP4 0.0.0.0
a=recvonly
a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level
a=ice-pwd:c56d106030599efe08cfa2a4f9b3ad5a
a=ice-ufrag:93982a76
a=mid:audio
b=AS:300000
a=rtcp-mux
a=rtpmap:109 opus/48000/2
a=fmtp:109 minptime=10; useinbandfec=1; maxaveragebitrate=131072; stereo=1; sprop-stereo=1 ; cbr=1
a=setup:active
a=ssrc:1948755120 cname:{208483df-13c9-e347-ba4a-c71604df3ad9}

But the quality is terrible. Chrome shows about 30kbit/s on chrome://webrtc-internals/ and the sound is heavily distorted with variable volume... Any leads on the issue?

like image 435
Ben Banks Avatar asked Nov 11 '15 11:11

Ben Banks


People also ask

What bitrate should I use for Opus?

Opus supports bitrates from 6 kbps to 510 kbps for typical stereo audio sources (and a maximum of around 255 kbps per channel for multichannel audio), with the 'sweet spot' for music and general audio around 30 kbps (mono) and 40–100 kbps (stereo).

How much bandwidth does Opus use?

The Opus codec requires 6kbps to 500kbps. The average bandwidth may be taken as 42 kbps.

Why is Opus codec so good?

Opus is a totally open, royalty-free, highly versatile audio codec. Opus is unmatched for interactive speech and music transmission over the Internet, but is also intended for storage and streaming applications. Open, highly-versatile… and royalty-free.

Is Opus lossy or lossless?

Opus is a lossy audio codec that has some significant advantages over other lossy codecs such as MP3 or AAC.


1 Answers

I created a SDP parser. You feed the SDP description, get a JSON object and then serialize it again.

This way it's much easier to process the SDP as an object than as bulk text.

like image 101
Adrian Ber Avatar answered Sep 24 '22 02:09

Adrian Ber