Pjsip migration - SIP_CODEC and SIP_CODEC_OUTBOUND


I’ve been struggling the last few days trying to start using pjsip in favor of chan_sip. Here is where I am stuck

Currently, for example, if I want to prevent transcoding between two endpoints that share g722, I can set the following:

exten => _3XX,1,Set(SIP_CODEC=g722)
exten => _3XX,n,Set(__SIP_CODEC_OUTBOUND=g722)

And the call will connect using g722 on BOTH endpoints

The only replacement i’ve found so far is:

exten => _3XX,1,Set(PJSIP_MEDIA_OFFER(audio)=!all,g722)
exten => _3XX,n,Set(PJSIP_SEND_SESSION_REFRESH()=invite)
exten => _3XX,n,Dial(PJSIP/${EXTEN},30)

Unfortunately, only the caller will be forced to g722. The callee will remain on whatever codec is first in the preferred list

Is there any workaround to this? Asterisk 13.19. I’ve searched for hours and only found others complaining of the same with no official solution

Thank you

Have you tried using a pre-dial handler[1] to also set the media offer on the outgoing channel?

[1] https://wiki.asterisk.org/wiki/display/AST/Pre-Dial+Handlers

Thank you. I did not, up until now, and it worked successfully once I added it in.

exten => _2XX,n,Dial(PJSIP/${EXTEN},,b(out-pre-dial^${EXTEN}^1))

Now to push my luck. If I want to send the call to an AGI instead of directly dialing the remote extension, is it possible to set up a pre-dial handler without the Dial command but with something else like the Set command?

1 Like

I don’t understand what you mean. A pre-dial handler is invoked by Dial() and is meant to be a quick dialplan application run. Are you saying you want to dial something and send it elsewhere? Like an Originate from dialplan? If so there’s an application for that…

Thanks for your quick response

Just to clarify, this is how I expect the finalized script to look:

exten => _2XX,1,Set(PJSIP_MEDIA_OFFER(audio)=!all,ulaw)
exten => _2XX,n,Set(PJSIP_SEND_SESSION_REFRESH()=invite)
exten => _2XX,n,AGI(agi://IPaddress)
exten => _2XX,n,Hangup()

The AGI is encrypted so I can’t edit anything within it - but it will send the call to ${EXTEN} without an issue

Any way to get the pre-dial handler to notify the ${EXTEN} but without the Dial cmd? Maybe we can trick asterisk somehow

No. You have to pass the option to Dial in order to have it execute the pre-dial handler.

I understand. Thank you so much for your help.

Hello Jades & JColp,

Sorry to break in on your thread but I’m a little stuck.
Jades, could you post your working configuration so I can duplicate?

I have FreePBX 14 / Asterisk 13, ulaw is the first codec in Admin / Advanced SIP Settings / Codec order.

To get G722 between extensions, I have the following config giving partially good results (my extensions start with 4…) in:
exten => _4XXX,1,Set(PJSIP_MEDIA_OFFER(audio)=!all,g722)
same => n,Set(PJSIP_SEND_SESSION_REFRESH()=invite)
same => n,Dial(PJSIP/${EXTEN},b(callee-pre-dial^${EXTEN}^1))
same => n,Dial(PJSIP/${EXTEN},30)

exten => _4XXX,1,Set(PJSIP_MEDIA_OFFER(audio)=!all,g722)
same => n,Return()

I get good call quality on both ends of the call (Grandstream Wave on Android talking to Jitsi Desktop on Windows). Both ends report they are using G722.
However, 2 issues:

  1. ‘pjsip show channelstats’ shows the caller with ulaw and callee with g722. As if the destination is switched to G722 but the calling extension remains at ulaw.
  2. The CPU on the Asterisk server jumps, indicating it is doing transcoding for some reason. If I temporarily move G722 to the top of my SIP Codecs in FreePBX, calls between extensions are not transcoded and CPU utilization stays low. So Asterisk is transcoding and I want to avoid this.

I don’t understand what is going on - or what I am doing wrong. As I said, it looks like the 2 extensions are talking G722 to each other but Asterisk still thinks the caller is on ulaw.

Any suggestions?



Your code looks right, and I also have the same issue where asterisk shows the wrong codec. I just assumed it was a bug or design and just left it as is. I am able to differentiate how g711 vs g722 sounds like, plus both endpoints are confirming that the in-use codec is g722, so I am confident that it what you are seeing in the CLI doesn’t even matter. I also noticed the same behavior in chan_sip btw, which is what eased my concerned even more so.

Hi Jades,

Thanks for getting back to me and confirming the code snippet looks good.
I am getting good call quality with g722 but I am also seeing increased CPU utilization on the Asterisk server which indicates it is doing transcoding.

If I force all communication to g722 by changing the preferred codec in FreePBX / Advanced SIP Settings / General SIP Settings and move g722 to the top, I get the g722 call quality but without the increased CPU utilization i.e. no transcoding.

I’m concerned with CPU load on the server if I have a lot of extensions talking to each other and Asterisk is doing (unnecessary) transcoding on each channel.

Are you also seeing increased CPU utilization with g722 between extensions using the /etc/asterisk/extensions_override_freepbx.conf customization?

Thanks again,


If you provide the SDP using “pjsip set logger on” then it can be shown what is actually being negotiated.

Hi Jerry,

I see almost exactly the same amount of CPU usage when making a call with the above script vs making an outside call where no script is involved and g711 is automatically negotiated.

Also, I highly doubt any transcoding is done. You’d be able to tell in the quality of the phone call if transcoding is being performed. I could be wrong about this statement, but I know what a transcoded call would sound like, so I’m pretty confident about my response.