Restrict codec on subsequent leg, based on incoming call

Looking for help on avoiding transcoding whilst transitioning to PJSIP. We’re running Asterisk 18.14.0 with FreePBX 16.

I would essentially like to be able to duplicating what we have working with chan_sip and chan_iax2, so that we can migrate to PJSIP for the upstream VoIP trunks. We’re essentially after the promises of ACN but disappointed that work on this appears to have stalled in the last 2 years.

I’m unfortunately not a developer and whilst I’ve been able to hack together most of what we need using the dial plan I’m in no position to meaningfully contribute to the source code. I am however very grateful for the contributions of skill and time for those that are able to contribute though!

What we have working
We use chan_sip for upstream trunks and a mixture of PJSIP and IAX2 for downstream extensions. Our dial plan currently sets an inheritable variable (‘_’ prefix) that the outgoing call uses to restrict the codec selection of what the current (inbound) channel is making use of.

Upstream (chan_sip) trunk is configured for both alaw and g729 with a custom context:

context=custom-force-codec
allow=alaw,g729

Incoming calls (from upstream SIP trunk towards extensions):
This is very manual, in that we set nearly all extensions to only support g729, with some allowing both g729 and alaw. We then update the custom context that incoming calls from the upstream provider flow in via to force alaw for those.

[custom-force-codec]
exten => _0115551111,1,Set(SIP_CODEC=alaw)
exten => _0116662222,1,Set(SIP_CODEC=alaw)
exten => _.,1,Set(SIP_CODEC=g729)
exten => _.,n,Goto(from-pstn,${EXTEN},1)
exten => s,1,Goto(from-pstn,s,1)

Incoming calls subsequently work relatively well, herewith an example:

pbx.c: Executing [0115551111@custom-force-codec:1] Set("SIP/AAA-00009e8b", "SIP_CODEC=alaw") in new stack
pbx.c: Executing [0115551111@custom-force-codec:2] Goto("SIP/AAA-00009e8b", "from-pstn,5551111,1") in new stack
pbx_builtins.c: Goto (from-pstn,0115551111,1)

Outgoing calls (from extensions through upstream SIP trunks): We have a custom 'macro-dialout-trunk-predial-hook' context which simply contains: ``` exten => s,n,Set(_SIP_CODEC_OUTBOUND=${CHANNEL(audioreadformat)}) ```

Question time
I’m looking for anything that could provide a bridge gap before ACN becomes a reality. I’m also not sure if ACN will ever support codec negotiation between IAX2 and PJSIP.

The PJSIP_MEDIA_OFFER dialplan function[1] is used to set the codecs for an outgoing PJSIP call leg. You use it in a pre-dial handler. Is that what you’re looking for?

[1] Asterisk 20 Function_PJSIP_MEDIA_OFFER - Asterisk Project - Asterisk Project Wiki

Oh, and there are also configuration options which can control the outgoing call leg[1] despite ACN not being fully implemented. Experiment and see.

[1] asterisk/pjsip.conf.sample at master · asterisk/asterisk · GitHub

Many thanks, from my understanding this requires the dial command to be customised to call a pre-dial handler. Any chance that there are plans for this to use a variable that can be inherited instead, like _SIP_CODEC_OUTBOUND?

Is there anything available to restrict the codecs that Asterisk / PJSIP negotiates for an incoming call before it’s answered? Like setting SIP_CODEC?

Lastly, is there a discussion thread somewhere we I can get an understanding of the progress of ACN? My understanding is that it’s waiting for changes in PJSIP but I did find something which appeared to me that this may already have been done many years ago and that the work ahead needs to happen in Asterisk.

There are no plans to use dialplan variables for such things. The use of dialplan variables predate dialplan functions, which is why it was done that way in the first place. PJSIP instead uses dialplan functions.

I vaguely recall PJSIP_MEDIA_OFFER does work on inbound, but haven’t tested it personally.

There’s no current thread, and it’s never been waiting on changes in PJSIP itself. It’s always been waiting on additional work in Asterisk to take advantage of things. Regulatory changes we’ve had to make and other things took priority over the ACN work.

This is the Asterisk forum; you start with a blank sheet for the Dial application call.

However, there are hints in your question that you are really using FreePBX. FreePBX already uses a pre-dial handler, for alert info, and I’d be surprised if there wasn’t a customisation hook.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.