Asterisk Codec negotation process


I have again a question regarding the codecs negotiation in Asterisk.
I’m using Asterisk 1.8.20.

I have Peer 1 (allow=gsm,alaw) and Peer2 (allow=gsm,alaw).
I have directmedia=no, nat=yes on both peers 1 and 2.

If peer 1 is calling with gsm codec enabled. Everything is OK:
Peer1 – GSM – [Asterisk] – GSM – Peer2

Now if peer1 doesn’t support (on the phone) the codec GSM (disabled or unsupported), then asterisk will chose alaw for peer1 and he will chose gsm for peer2

Peer1 – alaw – [Asterisk] – GSM – Peer2

It results transcoding on Asterisk

Is there a way to avoid this and to say to asterisk to use alaw with peer2 in this case, in place of GSM ?

Thank you
Renaud Dubois

Are you sure the problem is not that peer 2 is ignoring the preferred codec?

I believe that Asterisk will prefer the incoming first choice on the outgoing leg, in preference to the first listed codec, but I could be wrong, but it will still allow both codecs.

If you want to prevent the second leg using GSM at all, you can set a channel variable, before Dial, to limit the codec choices, but if it is offered the codec it can choose to use that to send to Asterisk and Asterisk cannot stop it.

Note that, if the second phone only supports GSM, the call will fail if you restrict the codec to exclude it.

The forward and return link codecs need not be the same, so Asterisk might not transcode forwards, but be forced to do so backwards because peer 2 selected the GSM option.

Asterisk does not know that peer 2 supports G.711 at the time it sends the outgoing invite, so it can’t safely restrict the codec choice.

Dear David55,
Thank you for your answer.
I don’t think it’s a problem with the peer2.
The peer2 is in fact another Asterisk 1.6 (small appliance - A098050401 in the example below).
On the (Asterisk) peer2 side, it’s also written allow=gsm,alaw

I see that transcoding occurs using the command “sip show channels”.
localhostCLI> sip show channels
Peer User/ANR Call ID Format Hold Last Message Expiry Peer
xxxxxx 123456789 4737f4d510e5ad1 0x2 (gsm) No Tx: ACK A098050401
xxxxxx phone2a 1612023569 0x8 (alaw) No Rx: ACK phone2a
2 active SIP dialogs

Can you give me a bit more info about the channel variable that I could use before the Dial to limit codec choice ?

Thank you

See … +Variables

exten => _[0-9].,n,Set(SIP_CODEC_INBOUND=alaw)
exten => _[0-9].,n,Set(SIP_CODEC_OUTBOUND=alaw)