Index: channels/chan_pjsip.c
===================================================================
--- channels/chan_pjsip.c	(revision 431537)
+++ channels/chan_pjsip.c	(working copy)
@@ -545,7 +545,7 @@
 	int exists;
 
 	/* If we only needed this DSP for fax detection purposes we can just drop it now */
-	if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND) {
+	if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND || session->endpoint->dtmf == AST_SIP_DTMF_AUTO) {
 		ast_dsp_set_features(session->dsp, DSP_FEATURE_DIGIT_DETECT);
 	} else {
 		ast_dsp_free(session->dsp);
@@ -1476,6 +1476,14 @@
 		}
 
 		ast_rtp_instance_dtmf_begin(media->rtp, digit);
+                break;
+	case AST_SIP_DTMF_AUTO:
+                       if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_INBAND)) {
+                        return -1;
+                }
+
+                ast_rtp_instance_dtmf_begin(media->rtp, digit);
+                break;
 	case AST_SIP_DTMF_NONE:
 		break;
 	case AST_SIP_DTMF_INBAND:
@@ -1579,6 +1587,15 @@
 		}
 
 		ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
+                break;
+        case AST_SIP_DTMF_AUTO:
+                if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_INBAND)) {
+                        return -1;
+                }
+
+                ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
+                break;
+
 	case AST_SIP_DTMF_NONE:
 		break;
 	case AST_SIP_DTMF_INBAND:
Index: include/asterisk/res_pjsip.h
===================================================================
--- include/asterisk/res_pjsip.h	(revision 431537)
+++ include/asterisk/res_pjsip.h	(working copy)
@@ -252,6 +252,8 @@
 	AST_SIP_DTMF_INBAND,
 	/*! Use SIP INFO DTMF (blech) */
 	AST_SIP_DTMF_INFO,
+	/*! Use SIP 4733 if supported by the other side or INBAND if not */
+	AST_SIP_DTMF_AUTO,
 };
 
 /*!
Index: res/res_pjsip_sdp_rtp.c
===================================================================
--- res/res_pjsip_sdp_rtp.c	(revision 431537)
+++ res/res_pjsip_sdp_rtp.c	(working copy)
@@ -123,7 +123,7 @@
 		ice->stop(session_media->rtp);
 	}
 
-	if (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733) {
+	if (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733 || session->endpoint->dtmf == AST_SIP_DTMF_AUTO) {	
 		ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833);
 		ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_DTMF, 1);
 	} else if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND) {
@@ -143,13 +143,14 @@
 	return 0;
 }
 
-static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp_media *stream, struct ast_rtp_codecs *codecs)
+static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp_media *stream, struct ast_rtp_codecs *codecs,
+       struct ast_sip_session_media *session_media)
 {
 	pjmedia_sdp_attr *attr;
 	pjmedia_sdp_rtpmap *rtpmap;
 	pjmedia_sdp_fmtp fmtp;
 	struct ast_format *format;
-	int i, num = 0;
+	int i, num = 0, tel_event = 0;;
 	char name[256];
 	char media[20];
 	char fmt_param[256];
@@ -171,6 +172,9 @@
 		}
 
 		ast_copy_pj_str(name, &rtpmap->enc_name, sizeof(name));
+                if (strcmp(name,"telephone-event") == 0) {
+                        tel_event++;
+                }
 		ast_copy_pj_str(media, (pj_str_t*)&stream->desc.media, sizeof(media));
 		ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, NULL, pj_strtoul(&stream->desc.fmt[i]),
 							     media, name, 0, rtpmap->clock_rate);
@@ -196,6 +200,9 @@
 			}
 		}
 	}
+        if (tel_event==0) {
+                ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
+        }
 
 	/* Get the packetization, if it exists */
 	if ((attr = pjmedia_sdp_media_find_attr2(stream, "ptime", NULL))) {
@@ -234,7 +241,7 @@
 	}
 
 	/* get the capabilities on the peer */
-	get_codecs(session, stream, &codecs);
+	get_codecs(session, stream, &codecs,  session_media);
 	ast_rtp_codecs_payload_formats(&codecs, peer, &fmts);
 
 	/* get the joint capabilities between peer and endpoint */
@@ -947,7 +954,7 @@
 	pj_str_t stmp;
 	pjmedia_sdp_attr *attr;
 	int index = 0;
-	int noncodec = (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733) ? AST_RTP_DTMF : 0;
+	int noncodec = (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733 || session->endpoint->dtmf == AST_SIP_DTMF_AUTO) ? AST_RTP_DTMF : 0;
 	int min_packet_size = 0, max_packet_size = 0;
 	int rtp_code;
 	RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
Index: res/res_pjsip/pjsip_configuration.c
===================================================================
--- res/res_pjsip/pjsip_configuration.c	(revision 431537)
+++ res/res_pjsip/pjsip_configuration.c	(working copy)
@@ -109,6 +109,8 @@
 		endpoint->dtmf = AST_SIP_DTMF_INBAND;
 	} else if (!strcasecmp(var->value, "info")) {
 		endpoint->dtmf = AST_SIP_DTMF_INFO;
+        } else if (!strcasecmp(var->value, "auto")) {
+                endpoint->dtmf = AST_SIP_DTMF_AUTO;
 	} else if (!strcasecmp(var->value, "none")) {
 		endpoint->dtmf = AST_SIP_DTMF_NONE;
 	} else {
@@ -129,6 +131,8 @@
 		*buf = "inband"; break;
 	case AST_SIP_DTMF_INFO :
 		*buf = "info"; break;
+       case AST_SIP_DTMF_AUTO :
+                *buf = "auto"; break;
 	default:
 		*buf = "none";
 	}
Index: res/res_pjsip.c
===================================================================
--- res/res_pjsip.c	(revision 431537)
+++ res/res_pjsip.c	(working copy)
@@ -208,6 +208,10 @@
 							<enum name="info">
 								<para>DTMF is sent as SIP INFO packets.</para>
 							</enum>
+                                                        <enum name="auto">
+                                                                <para>DTMF is sent as RFC-2833 if the other side supports it or as INBAND if not.</para>
+                                                        </enum>
+
 						</enumlist>
 					</description>
 				</configOption>
Index: res/res_pjsip_session.c
===================================================================
--- res/res_pjsip_session.c	(revision 431537)
+++ res/res_pjsip_session.c	(working copy)
@@ -1104,7 +1104,7 @@
 	session->inv_session = inv_session;
 	session->req_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
 
-	if (endpoint->dtmf == AST_SIP_DTMF_INBAND) {
+	 if (endpoint->dtmf == AST_SIP_DTMF_INBAND || endpoint->dtmf == AST_SIP_DTMF_AUTO) {
 		dsp_features |= DSP_FEATURE_DIGIT_DETECT;
 	}
 
