Index: pjproject/trunk/pjmedia/src/pjmedia/stream.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia/stream.c	(revision 1415)
+++ pjproject/trunk/pjmedia/src/pjmedia/stream.c	(revision 1416)
@@ -63,5 +63,5 @@
 {
     int		    event;
-    pj_uint32_t	    start_ts;
+    pj_uint32_t	    duration;
 };
 
@@ -374,48 +374,45 @@
  */
 static void create_dtmf_payload(pjmedia_stream *stream, 
-			  struct pjmedia_frame *frame_out)
+			        struct pjmedia_frame *frame_out,
+				int *first, int *last)
 {
     pjmedia_rtp_dtmf_event *event;
     struct dtmf *digit = &stream->tx_dtmf_buf[0];
-    unsigned duration;
     pj_uint32_t cur_ts;
 
     pj_assert(sizeof(pjmedia_rtp_dtmf_event) == 4);
+
+    *first = *last = 0;
 
     event = (pjmedia_rtp_dtmf_event*) frame_out->buf;
     cur_ts = pj_ntohl(stream->enc->rtp.out_hdr.ts);
-    duration = cur_ts - digit->start_ts;
-
-    if (duration == 0) {
+
+    if (digit->duration == 0) {
 	PJ_LOG(5,(stream->port.info.name.ptr, "Sending DTMF digit id %c", 
 		  digitmap[digit->event]));
-	duration = stream->port.info.samples_per_frame;
-	digit->start_ts = cur_ts - duration;
-    }
+	*first = 1;
+    }
+
+    digit->duration += stream->port.info.samples_per_frame;
 
     event->event = (pj_uint8_t)digit->event;
     event->e_vol = 10;
-    event->duration = pj_htons((pj_uint16_t)duration);
-
-    if (duration >= PJMEDIA_DTMF_DURATION) {
+    event->duration = pj_htons((pj_uint16_t)digit->duration);
+
+
+    if (digit->duration >= PJMEDIA_DTMF_DURATION) {
+
 	event->e_vol |= 0x80;
+	*last = 1;
 
 	/* Prepare next digit. */
 	pj_mutex_lock(stream->jb_mutex);
+
 	pj_array_erase(stream->tx_dtmf_buf, sizeof(stream->tx_dtmf_buf[0]),
 		       stream->tx_dtmf_count, 0);
 	--stream->tx_dtmf_count;
 
-	stream->tx_dtmf_buf[0].start_ts = cur_ts;
 	pj_mutex_unlock(stream->jb_mutex);
-
-	if (stream->tx_dtmf_count) {
-	    PJ_LOG(5,(stream->port.info.name.ptr,
-		      "Sending DTMF digit id %c", 
-		      digitmap[stream->tx_dtmf_buf[0].event]));
-	}
-
-    }
-
+    }
 
     frame_out->size = 4;
@@ -526,5 +523,5 @@
     void *rtphdr;
     int rtphdrlen;
-
+    int inc_timestamp = 0;
 
     /* Don't do anything if stream is paused */
@@ -555,13 +552,25 @@
      */
     if (stream->tx_dtmf_count) {
-
-	create_dtmf_payload(stream, &frame_out);
-
-	/* Encapsulate. */
+	int first=0, last=0;
+
+	create_dtmf_payload(stream, &frame_out, &first, &last);
+
+	/* Encapsulate into RTP packet. Note that:
+         *  - RTP marker should be set on the beginning of a new event
+	 *  - RTP timestamp is constant for the same packet. 
+         */
 	status = pjmedia_rtp_encode_rtp( &channel->rtp, 
-					 stream->tx_event_pt, 0, 
-					 frame_out.size, ts_len, 
+					 stream->tx_event_pt, first, 
+					 frame_out.size, (first?ts_len:0), 
 					 (const void**)&rtphdr, 
 					 &rtphdrlen);
+
+	if (last) {
+	    /* This is the last packet for the event. 
+	     * Increment the RTP timestamp of the RTP session, for next
+	     * RTP packets.
+	     */
+	    inc_timestamp = PJMEDIA_DTMF_DURATION - ts_len;
+	}
 
     } else if (frame->type != PJMEDIA_FRAME_TYPE_NONE) {
@@ -667,4 +676,12 @@
     pj_memcpy(channel->out_pkt, rtphdr, sizeof(pjmedia_rtp_hdr));
 
+    /* Special case for DTMF: timestamp remains constant for
+     * the same event, and is only updated after a complete event
+     * has been transmitted.
+     */
+    if (inc_timestamp) {
+	pjmedia_rtp_encode_rtp( &channel->rtp, stream->tx_event_pt, 0,
+				0, inc_timestamp, NULL, NULL);
+    }
 
     /* Set RTP marker bit if currently not streaming */
@@ -1577,4 +1594,5 @@
 
 	    stream->tx_dtmf_buf[stream->tx_dtmf_count+i].event = pt;
+	    stream->tx_dtmf_buf[stream->tx_dtmf_count+i].duration = 0;
 	}
 
@@ -1582,17 +1600,6 @@
 	    goto on_return;
 
-	/* Init start_ts and end_ts only for the first digit.
-	 * Subsequent digits are initialized on the fly.
-	 */
-	if (stream->tx_dtmf_count ==0) {
-	    pj_uint32_t start_ts;
-
-	    start_ts = pj_ntohl(stream->enc->rtp.out_hdr.ts);
-	    stream->tx_dtmf_buf[0].start_ts = start_ts;
-	}
-
 	/* Increment digit count only if all digits are valid. */
 	stream->tx_dtmf_count += digit_char->slen;
-
     }
 
