Limiting outgoing leg to several codecs

The subject may sound like a typical question but the key word here is ‘several’.

I have the following setup: Asterisk servers behind kamailio(openser) SIP proxies which acts as registrars and load balancers. Asterisk’s functions are handling of severals trunks to CISCO equipment (AS5300 PSTN gateway and CallManager 4.1); dispatching of calls based on specific user permissions logic.

The whole setup is based upon RealTime.

A typical call in this setup is passing following steps:

client1 -> kamailio -> Asterisk -> kamailio -> client2


client1 -> CallManager -> Asterisk -> kamailio -> client2

The problem here consist of few major issues:

  1. CallManager’s SIP trunk is able of alaw/ulaw codecs only.
  2. Some broken clients on incoming calls replies with all codecs they’re capable of even if they’re limited to a single codec.
  3. Asterisk doesn’t use user’s RealTime configuration when sending call to a trunk.

Now imagine a client2 being using low band (say - EDGE) internet connection and connecting using ulaw codec on an incoming call. For sure, conversation becomes impossible due to choppy or completely disappearing sound.

Searching documentation and Internet I have found a channel variable SIP_CODEC (or, SIP_CODEC_OUTBOUND as of this ticket). But this variable defines only one codec and this becomes a big trouble since I don’t know which particular application would be used by client2 and what codecs it supports.

The way I would like to do it is to manually fetch ‘allow’ field for the user being dialed and tell asterisk somehow that these are the only codecs it should use or, if not possible, disconnect the call. Something of the kind:

exten => s,1,ChanIsAvail(SIP/PROXY1&SIP/PROXY2)
exten => s,n,Fetch_Allowed_Codecs_Here
; Say, we're getting variable ALLOWED_CODECS=speex,ilbc,gsm
exten => s,n,Dial(${AVAILORIGCHAN}/${ARG1})

Do I have any chance of implementing this functionality?