19

I've been googling a way to change codec in Chrome's implementation of WebRTC, but there doesn't seem to be a way.

How can I change the default codec used(audio or video) in a WebRTCpeer connection in Chrome?

Benjamin Trent
  • 7,378
  • 3
  • 31
  • 41
Tony
  • 333
  • 1
  • 2
  • 5
  • Possible duplicate of [Is there a way to choose codecs in WebRTC PeerConnection?](http://stackoverflow.com/questions/21402990/is-there-a-way-to-choose-codecs-in-webrtc-peerconnection) – user Oct 12 '16 at 22:54

2 Answers2

20

Yes, you can change the codec to be anything you want...as long as Chrome supports it. Right now, audio wise, the only supported codecs are PCMA, PCMU, ISAC, and OPUS(the default). For Video you have VP8(also H264 on some systems with FireFox).

To use any of these codecs as default, you must modify your SDP before setting it locally in your peerconnection and sending your offer/answer. I have tested successfully forcing Chrome to send PCMA instead of OPUS by default.

As an example:

Say you have your default audio SDP section that looks like the following(notes are in brackets are are not part of the sdp)

m=audio<media> 49353<port> RTP/SAVPF<proto> 111 103 104 0 8 106 105 13 126 <rtpformats>

c=IN<nettype> IP4<addrtype> 192.168.0.13<address>

a=rtcp:49353<port> IN<nettype> IP4<addresstype> privateIP<connection address>

a=candidate:1204296370 1 udp 2122260223 privateIP 49353 typ host generation 0 <audioIceCandidate>
a=candidate:1204296370 2 udp 2122260223 privateIP 49353 typ host generation 0
a=candidate:155969090 1 tcp 1518280447 privateIP  0 typ host generation 0
a=candidate:155969090 2 tcp 1518280447 privateIP  0 typ host generation 0
a=ice-ufrag:E7VFzFythTIOaQ6X <ice username>
a=ice-pwd:ZMHFqqXEA8JLjItZcRN4FZDJ <ice-password>
a=ice-options:google-ice <iceoptions>
a=fingerprint:sha-256<encryptType> 66:2D:43:3A:31:7B:46:56:50:D7:CC:75:80:79:5D:88:7D:5D:1B:0E:C7:E6:F9:C4:68:6D:51:7F:4B:32:97:A1<print>
a=setup:actpass <dtls setup mode>
a=mid:audio   
a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level <extention map>
a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
a=sendrecv <mediamode>
a=rtcp-mux <says rtcp mux>
a=rtpmap:111 opus/48000/2
a=fmtp:111 minptime=10
a=rtpmap:103 ISAC/16000
a=rtpmap:104 ISAC/32000
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:106 CN/32000
a=rtpmap:105 CN/16000
a=rtpmap:13 CN/8000
a=rtpmap:126 telephone-event/8000
a=maxptime:60

If you wanted to ONLY use PCMA, you would change the m=audio line to the following: m=audio 49353 RTP/SAVPF 8 this way only the PCMA payload is considered. Then you need to remove all the rtpmap lines that do not correspond with that payload, i.e. any a=rtpmap: where the next character is NOT an 8. If you set that modified sdp locally and send it to your peer(and if they SUPPORT PCMA...does not have to be default for them as the negotiation will force PCMA if you only offer it), then PCMA will be your audio codec and not OPUS.

Couple of asides:

  • The SDP I am talking about is the one generated and passed through the success callback of the createOffer and createAnswer functions of the peerconnection
  • This type of idea will work for ADDING codecs that you know are supported by the underlaying systems(H264, SPEEX, etc.). Just make sure to add the payload and the appropriate mappings and options(fmtp is needed for h264 as profiles are important and possibly sprop-parameter-sets).
  • This will work with any appropriately coded WebRTC system, i.e. Firefox, Opera, etc. Not just chrome.
Benjamin Trent
  • 7,378
  • 3
  • 31
  • 41
  • Hi, Benjamin Thanks to your advice, I managed to change audio codec(Opus to PCMA). However, when I tried to change video codec(to h.264), the video shows nothing. Does Chrome support changing video codec? – Tony Nov 22 '14 at 06:26
  • 2
    Chrome does not support H.264 at all currently, Firefox does I believe if the underlying system does(I have seen firefox offer it as a option in its latest releases). – Benjamin Trent Nov 22 '14 at 16:29
  • 2
    @BenjaminTrent: Can you please share the main documentation how to improve Opus or iSac quality? Where to find the SDP manual related to Audio codec please? –  Feb 20 '15 at 22:52
  • 2
    Opus is effected by dynamically changing bitrates, so the browser could be lowering its bitrate [Here is the rfc referencing the RTP payload format of opus](https://tools.ietf.org/html/draft-spittka-payload-rtp-opus-03#section-6.2). That section I linked should give what options there are for the codec. Making it CBR(constant bitrate) and forcing a specific rate that is relatively high may help. – Benjamin Trent Feb 21 '15 at 14:22
  • 3
    If you're going to be removing a=rtpmap lines, also make sure to remove a=rtcp-fb and a=fmtp lines for the same "codec id" or else the browser might complain (for example, if you remove "8" from the m= line then remove a=rtpmap:8, a=rtcp-fb:8 and a=fmtp:8 lines) – ssmith Nov 09 '16 at 21:39
  • @BenjaminTrent Can you give any reference where it says chrome does not support h264 encoding. AFAIK, it is chrome on Android that has no h264 codec – bugwheels94 Apr 04 '18 at 00:24
  • 3
    @bugwheels94 things have changed in the last 3+ years since I wrote that comment :). I think that newer versions of Chrome support H264 on many systems, though I don't know the exact list. I tested H264 in chrome 65 and it worked great. Back when I wrote this comment, H264 in chrome was just starting an experiment phase and was only accessible via a flag in the browser settings. – Benjamin Trent Apr 04 '18 at 13:25
  • @BenjaminTrent do you know what lines I would need to add to add H264 codec to an offer? For some reason Chrome on Android is inconsistent - only sometimes includes it in the offer despite being able to support it. – Andrew May 24 '20 at 03:45
  • @Andrew it's been a while since I have worked on webrtc tech. This Q/A might help you https://stackoverflow.com/a/29434735/1818849 . Note, h264 might not be supported on ALL android devices because not all android devices have hardware accelerated support of h264: https://stackoverflow.com/questions/36766716/how-to-enable-h264-on-android-webrtc – Benjamin Trent May 26 '20 at 11:58
  • Is ```OPUS``` still the default codec in Chrome(android)? – Sujith S Manjavana Jul 07 '22 at 16:21
0

As browsers start to support setCodecPreferences, you can check for the mimetype of the codec you want to use by default to set the codec preference. For example if you want to prefer opus for audio you can check for the "audio/opus" mimetype and set your codec preferences to opus codecs:

let tcvr = pc.getTransceivers()[0];
let codecs = RTCRtpReceiver.getCapabilities('audio').codecs;
let opus_codecs = [];
// iterate over supported codecs and pull out the codecs we want
for(let i = 0; i < codecs.length; i++)
{
   if(codecs[i].mimeType == "audio/opus")
   {
      opus_codecs .push(codecs[i]);
   }
}
// currently not all browsers support setCodecPreferences
if(tcvr.setCodecPreferences != undefined)
{
   tcvr.setCodecPreferences(opus_codecs);
}

Or for video you can fix the codec to vp9:

// note the following should be called before before calling either RTCPeerConnection.createOffer() or createAnswer()
let tcvr = pc.getTransceivers()[0];
let codecs = RTCRtpReceiver.getCapabilities('video').codecs;
let vp9_codecs = [];
// iterate over supported codecs and pull out the codecs we want
for(let i = 0; i < codecs.length; i++)
{
   if(codecs[i].mimeType == "video/VP9")
   {
      vp9_codecs.push(codecs[i]);
   }
}
// currently not all browsers support setCodecPreferences
if(tcvr.setCodecPreferences != undefined)
{
   tcvr.setCodecPreferences(vp9_codecs);
}

Code adapted from this Pericror blog post to force audio/video codecs.

TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65