Asterisk behavior with directmedia=yes and nat=yes


I have two cisco phones .

phone A is behind NAT and phone-B is in public netwrok.

I configured directmedia=yes.

Now, when I try to call Phone-B to A,

Phone-B sends INVITE to asterisk.

asterisk sends INVITE to A(with its own media IP and port).
A sends back 200 OK.
asterisk sends 200 OK to B.
both asterisk and phone-B sends RTP packets to A.

question #1: here is why asterisk is sending 200 OK to B even before it is sending RE-INVITE with phone-B media ?
Is this expected ? I feel that B should receive 200 OK only after asterisk sends re-INVITE with B’s media.

Now, asterisk sends RE-INVITE with Phone-B media
A sends back 200 OK to asterisk.

question #2: What settings I should make to not create media session between asterisk and A.


It does it that way because that is how it does it.

There is an option that will try and establish direct media on the initial exchange, but it used to be buggy.

You stop directmedia by using directmedia=no on at least one side - typically the NATted side.

You do not configure a NATted connection using nat=, you use externip/externaddr/stunaddr.

nat=yes is deprecated in version 11.

Hi David,

Thanks for the reply.

One thing I am not clear is why should B send RTP packet to A, even before asterisk sends B’s media info to A ?

This will impact the pinhole behavior in NAT device rite ?


“rite” is not a valid word here.

You will need to provide a trace of the SDP and RTP exchanges, as I can’t really work out exactly what you are doing from your description.

Hi David,

I am new to SIP and Asterisk, so excuse me for any blunt questions.

Let me explain in detail what I am observing.

My Setup:

Phone A( ---- NAT ---- SWITCH------Phone-B(

I have settings nat=yes for A and nat=no for B.
Now when I enable directmedia=yes and try to call from B to A.

  1. B sends INVITE to Asterisk with SDP
  2. Asterisk sends INVITE to A with Asterisk SDP
  3. A replies 200 OK to Asterisk with its SDP(
  4. Asterisk sends this 200 OK to B with A’s SDP( info.
  5. Asterisk sends RTP packet to A’s SDP
  6. Now B sends RTP packet to A’s SDP but it fails due to lack of pinhole.
  7. Asterisk sends INVITE to A with B’s SDP info
  8. A replies 200 OK with same SDP as it sent earlier.
  9. Asterisk again sends this 200 OK to B

Now, the issue at step 6 will be solved if we keep step 4 after step 8.

I just want to understand why step 4 is kept after 3 and not after 8 ?, which logically makes sense.

Again sorry for any stupid questions :cry:

Normally you would set directmedia to no for B.

NAT and SIP fit together rather uncomfortably, so you are going to need a detailed understanding of the individual nat= options (nat=yes is deprecated, probably for this reason, from version 11), and how the router and phone handle such environments. In particular, routers that use SIPALG are known to cause problems. I know neither enough about your system, nor the subtleties of the NAT workrounds in Asterisk, to say much more than don’t try for direct media.

You might want to look into (from the documentation for the trunk version):

[quote];directrtpsetup=yes ; Enable the new experimental direct RTP setup. This sets up
; the call directly with media peer-2-peer without re-invites.
; Will not work for video and cases where the callee sends
; RTP payloads and fmtp headers in the 200 OK that does not match the
; callers INVITE. This will also fail if directmedia is enabled when
; the device is actually behind NAT.

But you should note that this has been classed as experimental for many years.