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:
Local/101@mycontext/nj
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.
13 Responses
Great, Kevin and thanks
Thanks for taking the time to write this! It helped immensely when using sip through openvpn. Thank you thank you thank you!
Can this be added to the b-leg for Chan SIP or PJSIP?
We have a few WiFi phones in office and would love better quality in the event of packet loss / jitter.
You can add a jitter buffer to the outbound channel by using a pre-dial handler:
; outbound pre-dial handler
exten => predial_outbound,1,NoOp()
same => n,Set(JITTERBUFFER(adaptive)=default)
same => n,Return()
exten => 101,1,NoOp()
same => n,Dial(PJSIP/${EXTEN},20,b(default^predial_outbound^1))
same => n,Hangup()
Hi Kevin, looks good but should could you let me know what file / context this should go into?
I install and service VOIP systems.When looking at voice quality stats I often see Jitter Buffer Average Depth and Max. Depth. I know that this is telling me the average use of the adaptive buffer. The max depth is usually in the 20 to 50 range but sometimes I have seen as much as 200. At what point does the use of the buffer become unacceptable? I’m sure ideally I would not want to see it at all. What is the best way to isolate the issue?
Thank you so much ! I have lost hours playing with codecs with no success before reading your input. Now the outgoing audio quality is very good. Before it was scratchy, uncomfortable.
The dialplan examples I mentioned go into the extensions.conf file, and can be put into any context (or the one needing access to a given dialplan section).
Ideally, yes you wouldn’t want to use a buffer at all due to the potential of talk delay. However, depending on network conditions usually a slight delay is preferred to garbled audio. As to the other questions I am not sure. Those are dependent upon local conditions. If you haven’t already done so I’d suggest posting your question(s) at the community forum ( https://community.asterisk.org/). It’s much more suited for that kind of discussion.
Hi Kevin,
I put this in extensions.conf as well as extensions_custom.conf and it didn’t seem to do anything, so I added a debug statement that I never see in asterisk/full
same => n,Verbose(1,jitter buffer test 2)
Which I never see… Was expecting it on an outbound call from a voip phone (hence the reason for adding a receive jitter buffer).
Hi,
I’m still interested in in getting a receive jitter buffer working on the receive side from the devices out in the field.
Haven’t had any luck with a dial plan or adding it to sip_general_post.conf for the old style Chan sip extensions.
I set it to 4000 ms to test, dial *43 and here my voice immediately.
Any help is appreciated.
Hi Kevin,
As far as not seeing an expected delay that might depend which type of buffer you are using, and what kind of network conditions you are experiencing. An adaptive buffer with little to no “jitter” would have a small, if barely noticeable, delay.
If you are still having trouble, and if you haven’t already done so I’d suggest posting your question(s) over on the Asterisk forums (https://community.asterisk.org/). It’s much more conducive to these types of questions/discussions. You’re also more likely to get a quicker response from someone there as well. Thanks!
Did you enable verbose logging in Asterisk? Also may mean that that priority for the given extension in your context is never executed/reached.