Force g729 codec at endpoint (phone) on outbound SIP trunk

Note: Running FreePBX 2.7, Asterisk 1.6, and I have licensed g729 codec from Digium. Using Polycom 601 phones that support g729 natively.

My goal is the following:
ulaw for extension->extension (works)
ulaw for extension->voicemail (works)
ulaw for extension -> PSTN FXO (works)
ulaw for PSTN FXO -> extensions/voicemail/IVR (works)
g729 for SIP Trunk->extensions/voicemail/IVR (works)

g729 for extension->SIP Trunk (oops: transcodes)

So far everything is working, except outgoing SIP Trunk calls from the extensions–they are being transcoded rather than going g729 end to end (pass-through).

Right now in sip show channels they show up as
ulaw at the extension
and g729 on the trunk

so they’re being transcoded–which wastes my g729 licenses (and I can hear a difference).

Config on SIP Trunk is set to disallow=all and allow=g729.
Config on the extension is to allow whatever (FreePBX default order–probably listing ulaw before g729).
Config on the endpoint (phone) is to ulaw = 1, alaw=2, g729ab=3.

If I disable ulaw/alaw on the endpoint, call connects g729 as expected, but then I would be messing up PSTN/Voicemal/inter-extension calls. If I give g729 priority on the phone, it still connects to the g729 SIP Trunk as ulaw and transcodes it.

How can I get outgoing calls on my SIP trunk to run native g729 from end to end while keeping ulaw for internal calls and calls oven the PSTN? Why is it that a phone that prefers ulaw but allows g729 will force Asterisk to transcode the call, rather than using native g729 like it expressly allows (and how do I fix that)?



Actually it didn’t work at all.

It’s worse. It transcodes the call twice. :stuck_out_tongue:

g729 show licenses shows 2 codec usages per call, not just one. :stuck_out_tongue:

Any more ideas?

Putting code in

as described, worked perfectly!

Thank you so much!

How about this:

In extensions_additional.conf, put something like:

exten => s,1,GotoIf($["${OUT_${DIAL_TRUNK}}" = “IAX2/LinkXXX”]?G729)
exten => s,n,GotoIf($["${OUT_${DIAL_TRUNK}}" = “SIP/VOIP1XXX”]?iLBC)
exten => s,n,MacroExit()

exten => s,n(G729),NoOp(Setting Codec for Trunk)
exten => s,n,set(SIP_CODEC=g729)
exten => s,n,MacroExit()

exten => s,n(iLBC),NoOp(Setting Codec for Trunk)
exten => s,n,set(SIP_CODEC=iLBC)
exten => s,n,MacroExit()

No idea if this is a bright idea, but works for me. I also take no created for this… Please see

According to
"From this I assume we must expect that the custom contexts included in all the extensions_additional.conf contexts will be called after the context in which they are included."

EDIT: See solution above using the predial-hook. In my testing it worked.

In my case I have a IAX trunk that only uses G927, and Aastra IP phones that favor ulaw, but can do g927. This works well as I prefer using ulaw normally, but when I make a call that uses the IAX trunk, the Aastra uses ulaw, and the PBX trans-codes.

By manually inserting Set(SIP_CODE=g729) into the dial plan ( outrt-2 in extensions_additional.conf ) for the Trunk, the phone then uses G729 and all works as expected.

My problem is every attempt I have made to make this change via extensions_custom.conf has failed - This is really beyond my current understanding of Asterisk/FreePBX. I am really not sure at all where I need to insert the Set(SIP_CODE=g729) for only the Trunk in question.

Any suggestions?



Thank you. Yes that is already the case. (I noted that particular setting in my original post.)

Thanks anyway!

Hi Tony,

Did you get anywhere with this? I am faced with the same problem.



No I have not found a solution. At the moment it is still transcoding. It works, but that really bothers me, lowers quality, and uses up licences I’d rather not spend that way, but so far no one’s offered a solution… yet. :slight_smile: Still hoping! :slight_smile:


What do you have in your trunk settings? Try adding an allow line (allow=g729) and a deny all (deny=all)


Just trying again to see if I can get an answer on this. It’s kind of a bummer to have asterisk transcode our outgoing calls, when the phones can do native g.729. Setting g729 as the first allowed codec on the extension makes it do 729 extension to extension which isn’t desired.



I’m testing something similar (FreePBX, Asterisk 1.8.5, Polycom IP430). I haven’t purchased the g729 license because I don’t think I need it if I’m not transcoding. My phone and ITSP both support g729 natively.

Here is what I think is going on. What we’re asking for is, “Please check the second call leg for its preferred codec, and use that codec for both call legs.” However, it seems that once the codec is set for one leg of a call, it doesn’t change. A Senior Product Manager at Digium provides a clue in this thread:

“Once the call leg has been negotiated by Asterisk to use G.729, then Asterisk won’t reinvite the first leg to a different codec just because the second call leg fails for some reason.”

So if the phone starts talking to Asterisk with ulaw, but the trunk is forced to g729, either it will transcode or, with no transcoding license, fail.

I was able to get the phone to use g729 by adding the following lines to sip_general_custom.conf:


Once that was done, g729 has first priority (it’s not even listed in my sip_general_additional.conf). And I can then use pass-through to call my ITSP. Unfortunately:

  • I can’t retrieve voicemail. Maybe following the other suggestion from the link above would solve that: “The best thing you can do is download the prompts in G.729 format and have those played back.”
  • Calls to my PSTN line are g729. (Nice to see that the SPA3102 supports g729!)
  • Calls to other SIP extensions are g729.

So I guess the bottom line is, if you want g729 pass-through, you have to use g729 for all calls: internal, ITSP, and PSTN.

Mark Berry
MCB Systems

With the change to sip_general_custom.conf listed above, and the g729 sounds files extracted, the system is now using g729 on all internal and external calls. Not the mixed solution originally asked for, but at least trunk calls are on the compressed codec. We’ll see how that sounds…

I wrote this up here: Get Asterisk to Use the G.729a Codec.

Update 11/5/2011: I had to set the Preferred Codec of my SPA3102 to G729a for inbound calls. And it seems we still need a transcoder to handle some voicemail and other functions, but the pass-through means far less transcoding. Blog post updated.

Mark Berry
MCB Systems