Javascript Voice SDK
Twilio supports Opus and PCMU for our Voice SDKs which includes the Javascript SDK for browser-based VoIP applications and the android and iOS SDKs for mobile applications.
Which codec should you use?
The Opus codec has many advantages over PCMU and is recommended for most applications. Opus is the default codec for Mobile SDKs.
Network bandwidth requirements for the Voice SDKs
The following table lists the network requirements to deliver reasonable audio quality.
Bandwidth (Uplink/Downlink) |
Opus*: 40kbps / 40kbps PCMU: 100kbps / 100kbps |
Latency (RTT) |
< 200ms |
Jitter |
< 30ms |
Packet Loss |
< 3% |
* Opus codec is available from Voice JavaScript SDK version 1.7 and Mobile Voice SDKs 3.x
The Opus bandwidth requirements listed above are the default settings for Opus. Opus codec supports bandwidth control by allowing you to specify how much bandwidth it should use.
More info on the OPUS codec
Opus codec supports bandwidth control by allowing you to specify how much bandwidth it should use.
The parameter to use with Opus to control the bandwidth is maxAverageBitrate
.
The `maxAverageBitrate` parameter indicates the maximum average audio bitrate to use, in bits per second (bps) . By default, the setting is not used. If you specify 0, then the setting is not used. Any positive integer is allowed, but values outside the range 6000 to 510000 are ignored and treated as 0. The recommended bitrate for speech is between 8000 and 40000 bps.
How to use the Opus codec in your web/browser VoIP app (Javascript SDK)
To use Opus in your web application, use the following snippet when either instantiating the Twilio.Device
or updating the Twilio.Device
’s options with .updateOptions(options)
:
const device = new Twilio.Device(token, { codecPreferences: ['opus', 'pcmu']});
// or
const device = new Twilio.Device(token);
device.updateOptions({ codecPreferences: ['opus', 'pcmu']});
To set a custom bandwidth (16kbps), use the following snippet:
const device = new Twilio.Device(token, { codecPreferences: ['opus', 'pcmu'], maxAverageBitrate: 16000} );
// or
const device = new Twilio.Device(token);
device.updateOptions({ codecPreferences: ['opus', 'pcmu'], maxAverageBitrate: 16000});
Reference information:
https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#deviceoptions