Jitter buffer functionality has been in Asterisk for quite some time now. However, knowing what jitter is in a voice over IP (VoIP) application and when to use a de-jittering buffer to manage it may still be misunderstood by some.
What is a jitter buffer
In Asterisk, and generally speaking in VoIP, jitter is the divergence of an expected voice packet from its presumed arrival time. For instance, two packets may arrive at the same time, or out of order due to network congestion. These problems can cause audio quality to drop. A jitter buffer then is an intermediary queue that’s used to order packets according to their expected timing values in an attempt to minimize jitter. Using a jitter buffer can potentially improve call quality.
Asterisk implements two types of jitter handling buffers: fixed and adaptive. A fixed buffer always maintains an established queue size, whereas the adaptive buffer queue size grows or shrinks based upon internal adaptation logic.
When/Which to use
You’ll want to use a jitter buffer when having networking issues like packet loss or packets arriving out of order. In the case of out of order packets the Asterisk jitter buffer inserts the packets into the buffer in the correct order. These packets are then passed to the Asterisk core in the expected order ensuring continuous audio playback. In the packet loss case, the jitter buffer “interpolates” the lost frame. This “interpolated” frame is passed to the Asterisk core, which in turn allows individual codecs (those programmed to) to attempt packet loss concealment or correction.
Developer NOTE: codec implementors need keep in mind jitter buffers when writing/modifying a codec module for Asterisk. Asterisk codec modules must work with jitter buffers. At a minimum, a codec module should be able to handle “interpolated” frames, or frames with a datalen equal to zero. Even if this means just ignoring those frames.
Injecting a jitter buffer into the process unfortunately has a negative side effect. Anytime buffering is introduced a delay is too. A large delay can impact call quality, so it is important to configure the jitter buffer appropriately. A fixed buffer always maintains a set delay. This is a good choice if predictability is necessary. An adaptive buffer attempts to minimize the delay by maintaining a smaller buffer, but will grow when needed. It also has the ability to shrink back down, lowering the delay, once any packet timing issues “settle down”. For these reasons the adaptive buffer is usually considered superior to the fixed. And is in all likely hood the one you want to choose when using a jitter buffer in Asterisk.
How to use
There are several ways to attach a jitter buffer to a channel in Asterisk. The easiest, and preferred, way is to use the Asterisk JITTERBUFFER function. This function allows you to add a fixed or adaptive buffer in the dialplan to the read side of a channel. For instance, to add an adaptive jitter buffer with default settings use the following dialplan:
exten => 1,1,Set(JITTERBUFFER(adaptive)=default)
More options and examples can be found here: Function_JITTERBUFFER
A jitter buffer can be added to a local channel also by using the ‘j’ option in dialplan. Note, that you’ll want to also use the ‘n’ option so the channel does not get optimized away:
See Local Channel Modifiers for more information.
Lastly, and since this way is the less preferred way I’ll just briefly mention it. Depending on the channel driver (chan_sip, chan_iax, etc… not chan_pjsip), a jitter buffer can be set to be used within a channel type’s configuration setup (see that channel’s configuration settings for more information). For instance, in chan_sip by setting the appropriate configuration options (jbenabled=yes, jbmaxsize=200, and jbimpl=fixed creates a fixed size buffer) a jitter buffer is added to chan_sip channels when in use.