Index: pjproject/trunk/pjmedia/src/pjmedia/endpoint.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia/endpoint.c	(revision 6103)
+++ pjproject/trunk/pjmedia/src/pjmedia/endpoint.c	(revision 6105)
@@ -421,7 +421,7 @@
     unsigned televent_num = 0;
     unsigned televent_clockrates[8];
+#endif
     unsigned used_pt_num = 0;
     unsigned used_pt[PJMEDIA_MAX_SDP_FMT];
-#endif
 
     PJ_UNUSED_ARG(options);
@@ -430,4 +430,12 @@
     PJ_ASSERT_RETURN(endpt->codec_mgr.codec_cnt <= PJMEDIA_MAX_SDP_FMT,
 		     PJ_ETOOMANY);
+
+    /* Insert PJMEDIA_RTP_PT_TELEPHONE_EVENTS as used PT */
+#if defined(PJMEDIA_RTP_PT_TELEPHONE_EVENTS) && \
+	    PJMEDIA_RTP_PT_TELEPHONE_EVENTS != 0
+    if (endpt->has_telephone_event) {
+	used_pt[used_pt_num++] = PJMEDIA_RTP_PT_TELEPHONE_EVENTS;
+    }
+#endif
 
     /* Create and init basic SDP media */
@@ -445,4 +453,5 @@
 	pjmedia_codec_param codec_param;
 	pj_str_t *fmt;
+	unsigned pt;
 
 	if (endpt->codec_mgr.codec_desc[i].prio == PJMEDIA_CODEC_PRIO_DISABLED)
@@ -453,7 +462,35 @@
 					    &codec_param);
 	fmt = &m->desc.fmt[m->desc.fmt_count++];
+	pt = codec_info->pt;
+
+	/* Rearrange dynamic payload type to make sure it is inside 96-127
+	 * range and not being used by other codec/tel-event.
+	 */
+	if (pt >= 96) {
+	    unsigned pt_check = 96;
+	    unsigned j = 0;
+	    while (j < used_pt_num && pt_check <= 127) {
+		if (pt_check==used_pt[j]) {
+		    pt_check++;
+		    j = 0;
+		} else {
+		    j++;
+		}
+	    }
+	    if (pt_check > 127) {
+		/* No more available PT */
+		PJ_LOG(4,(THIS_FILE, "Warning: no available dynamic "
+			  "payload type for audio codec"));
+		break;
+	    }
+	    pt = pt_check;
+	}
+
+	/* Take a note of used dynamic PT */
+	if (pt >= 96)
+	    used_pt[used_pt_num++] = pt;
 
 	fmt->ptr = (char*) pj_pool_alloc(pool, 8);
-	fmt->slen = pj_utoa(codec_info->pt, fmt->ptr);
+	fmt->slen = pj_utoa(pt, fmt->ptr);
 
 	rtpmap.pt = *fmt;
@@ -461,5 +498,5 @@
 
 #if defined(PJMEDIA_HANDLE_G722_MPEG_BUG) && (PJMEDIA_HANDLE_G722_MPEG_BUG != 0)
-	if (codec_info->pt == PJMEDIA_RTP_PT_G722)
+	if (pt == PJMEDIA_RTP_PT_G722)
 	    rtpmap.clock_rate = 8000;
 	else
@@ -488,5 +525,5 @@
 	}
 
-	if (codec_info->pt >= 96 || pjmedia_add_rtpmap_for_static_pt) {
+	if (pt >= 96 || pjmedia_add_rtpmap_for_static_pt) {
 	    pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr);
 	    m->attr[m->attr_count++] = attr;
@@ -504,5 +541,5 @@
 					MAX_FMTP_STR_LEN - buf_len,
 					"%d",
-					codec_info->pt);
+					pt);
 
 	    for (ii = 0; ii < dec_fmtp->cnt; ++ii) {
@@ -549,15 +586,9 @@
 	    max_bitrate = codec_param.info.max_bps;
 
-	/* List clock rate & channel count of audio codecs for generating
-	 * telephone-event later.
-	 */
+	/* List clock rate of audio codecs for generating telephone-event */
 #if defined(PJMEDIA_RTP_PT_TELEPHONE_EVENTS) && \
 	    PJMEDIA_RTP_PT_TELEPHONE_EVENTS != 0
-	{
+	if (endpt->has_telephone_event) {
 	    unsigned j;
-
-	    /* Take a note of used dynamic PT */
-	    if (codec_info->pt >= 96)
-		used_pt[used_pt_num++] = codec_info->pt;
 
 	    for (j=0; j<televent_num; ++j) {
@@ -584,18 +615,46 @@
 	    char buf[160];
 	    unsigned j = 0;
-	    unsigned pt = PJMEDIA_RTP_PT_TELEPHONE_EVENTS;
+	    unsigned pt;
 
 	    /* Find PT for this tel-event */
-	    while (j < used_pt_num && pt <= 127) {
-		if (pt == used_pt[j]) {
-		    pt++;
+	    if (i == 0) {
+		/* First telephony-event always uses preconfigured PT
+		 * PJMEDIA_RTP_PT_TELEPHONE_EVENTS.
+		 */
+		pt = PJMEDIA_RTP_PT_TELEPHONE_EVENTS;
+	    } else {
+		/* Otherwise, find any free PT slot, starting from
+		 * (PJMEDIA_RTP_PT_TELEPHONE_EVENTS + 1).
+		 */
+		pt = PJMEDIA_RTP_PT_TELEPHONE_EVENTS + 1;
+		while (j < used_pt_num && pt <= 127) {
+		    if (pt == used_pt[j]) {
+			pt++;
+			j = 0;
+		    } else {
+			j++;
+		    }
+		}
+		if (pt > 127) {
+		    /* Not found? Find more, but now starting from 96 */
+		    pt = 96;
 		    j = 0;
-		} else {
-		    j++;
+		    while (j < used_pt_num &&
+			   pt < PJMEDIA_RTP_PT_TELEPHONE_EVENTS)
+		    {
+			if (pt == used_pt[j]) {
+			    pt++;
+			    j = 0;
+			} else {
+			    j++;
+			}
+		    }
+		    if (pt >= PJMEDIA_RTP_PT_TELEPHONE_EVENTS) {
+			/* No more available PT */
+			PJ_LOG(4,(THIS_FILE, "Warning: no available dynamic "
+				  "payload type for telephone-event"));
+			break;
+		    }
 		}
-	    }
-	    if (pt > 127) {
-		/* No more available PT */
-		break;
 	    }
 	    used_pt[used_pt_num++] = pt;
