I have an Asterisk box with a public IP address and two SIP clients behind
the same NAT device(I also have SIP clients behind different NATs). I want
to know is it possible for Asterisk to detect if both clients are behind
the same NAT and use direct media between them and use other options for
clients that are behind different NATs?

By detection I mean is it possible for Asterisk to take a look at the
public IP address of packets and if both packets have the same IP address
it tells the clients to send RTP traffic directly to each other. Is there a
module or piece of code for this behavior in Asterisk??

PS:I assumed each NAT box has a single external IP address, and this
assumption is good for me.

I don’t think Asterisk can detect when phones are behind the same remote NAT.