Index: pjproject/trunk/pjmedia/src/pjmedia/stream.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia/stream.c	(revision 452)
+++ pjproject/trunk/pjmedia/src/pjmedia/stream.c	(revision 457)
@@ -57,5 +57,4 @@
     void		   *out_pkt;	    /**< Output buffer.		    */
     pjmedia_rtp_session	    rtp;	    /**< RTP session.		    */
-    char		    last_frm_type;  /**< Last frame type from jb    */
 };
 
@@ -91,6 +90,11 @@
     pjmedia_codec_param	     codec_param;   /**< Codec param.		    */
     unsigned		     frame_size;    /**< Size of encoded base frame.*/
+    pj_bool_t		     is_streaming;  /**< Currently streaming?. This
+						 is used to put RTP marker
+						 bit.			    */
+
     pj_mutex_t		    *jb_mutex;
     pjmedia_jbuf	    *jb;	    /**< Jitter buffer.		    */
+    char		     jb_last_frm;   /**< Last frame type from jb    */
 
     pjmedia_rtcp_session     rtcp;	    /**< RTCP for incoming RTP.	    */
@@ -210,5 +214,5 @@
 	     * the frame. 
 	     */
-	    if (frame_type != channel->last_frm_type) {
+	    if (frame_type != stream->jb_last_frm) {
 		pjmedia_jb_state jb_state;
 
@@ -244,7 +248,8 @@
 		pjmedia_zero_samples(p_out_samp + samples_count,
 				     samples_required - samples_count);
+		samples_count = samples_required;
 	    }
 
-	    channel->last_frm_type = frame_type;
+	    stream->jb_last_frm = frame_type;
 	    break;
 
@@ -277,5 +282,5 @@
 		} while (samples_count < samples_required);
 
-		if (channel->last_frm_type != frame_type) {
+		if (stream->jb_last_frm != frame_type) {
 		    PJ_LOG(5,(stream->port.info.name.ptr, 
 			      "Jitter buffer is bufferring with plc (prefetch=%d)",
@@ -288,4 +293,5 @@
 		pjmedia_zero_samples(p_out_samp + samples_count,
 				     samples_required - samples_count);
+		samples_count = samples_required;
 		PJ_LOG(5,(stream->port.info.name.ptr, 
 			  "Jitter buffer is bufferring (prefetch=%d)..", 
@@ -293,5 +299,5 @@
 	    }
 
-	    channel->last_frm_type = frame_type;
+	    stream->jb_last_frm = frame_type;
 	    break;
 
@@ -318,5 +324,5 @@
 	}
 
-	channel->last_frm_type = frame_type;
+	stream->jb_last_frm = frame_type;
     }
 
@@ -438,8 +444,6 @@
     struct pjmedia_frame frame_out;
     unsigned ts_len;
-    pj_bool_t has_tx;
     void *rtphdr;
     int rtphdrlen;
-    pj_ssize_t sent;
 
 
@@ -454,7 +458,6 @@
     /* Init frame_out buffer. */
     frame_out.buf = ((char*)channel->out_pkt) + sizeof(pjmedia_rtp_hdr);
-
-    /* Make compiler happy */
     frame_out.size = 0;
+
 
     /* If we have DTMF digits in the queue, transmit the digits. 
@@ -463,5 +466,4 @@
     if (stream->tx_dtmf_count) {
 
-	has_tx = PJ_TRUE;
 	create_dtmf_payload(stream, &frame_out);
 
@@ -475,6 +477,4 @@
     } else if (frame->type != PJMEDIA_FRAME_TYPE_NONE) {
 	unsigned ts, samples_per_frame;
-
-	has_tx = PJ_TRUE;
 
 	/* Repeatedly call encode if there are multiple frames to be
@@ -490,10 +490,13 @@
 	    unsigned bytes_per_sample, max_size;
 
+	    /* Nb of bytes in PCM sample */
 	    bytes_per_sample = stream->codec_param.info.pcm_bits_per_sample/8;
 
+	    /* Split original PCM input frame into base frame size */
 	    tmp_in_frame.buf = ((char*)frame->buf) + ts * bytes_per_sample;
 	    tmp_in_frame.size = samples_per_frame * bytes_per_sample;
 	    tmp_in_frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
 
+	    /* Set output frame position */
 	    tmp_out_frame.buf = ((char*)frame_out.buf) + frame_out.size;
 
@@ -501,4 +504,5 @@
 		       frame_out.size;
 
+	    /* Encode! */
 	    status = stream->codec->op->encode( stream->codec, &tmp_in_frame, 
 						max_size, &tmp_out_frame);
@@ -509,8 +513,17 @@
 	    }
 
+	    /* tmp_out_frame.size may be zero for silence frame. */
 	    frame_out.size += tmp_out_frame.size;
+
+	    /* Stop processing next PCM frame when encode() returns either 
+	     * CNG frame or NULL frame.
+	     */
+	    if (tmp_out_frame.type!=PJMEDIA_FRAME_TYPE_AUDIO || 
+		tmp_out_frame.size==0) 
+	    {
+		break;
+	    }
+
 	}
-
-	//printf("p"); fflush(stdout);
 
 	/* Encapsulate. */
@@ -523,5 +536,4 @@
 
 	/* Just update RTP session's timestamp. */
-	has_tx = PJ_FALSE;
 	status = pjmedia_rtp_encode_rtp( &channel->rtp, 
 					 0, 0, 
@@ -547,21 +559,32 @@
 
     /* Do nothing if we have nothing to transmit */
-    if (!has_tx)
+    if (frame_out.size == 0) {
+	if (stream->is_streaming) {
+	    PJ_LOG(5,(stream->port.info.name.ptr,"Starting silence"));
+	    stream->is_streaming = PJ_FALSE;
+	}
 	return PJ_SUCCESS;
-
-    if (rtphdrlen != sizeof(pjmedia_rtp_hdr)) {
-	/* We don't support RTP with extended header yet. */
-	PJ_TODO(SUPPORT_SENDING_RTP_WITH_EXTENDED_HEADER);
-	return PJ_SUCCESS;
-    }
-
+    }
+
+
+    /* Copy RTP header to the beginning of packet */
     pj_memcpy(channel->out_pkt, rtphdr, sizeof(pjmedia_rtp_hdr));
 
 
-    /* Send. */
-    sent = frame_out.size+sizeof(pjmedia_rtp_hdr);
-
+    /* Set RTP marker bit if currently not streaming */
+    if (stream->is_streaming == PJ_FALSE) {
+	pjmedia_rtp_hdr *rtp = channel->out_pkt;
+
+	rtp->m = 1;
+	PJ_LOG(5,(stream->port.info.name.ptr,"Start talksprut.."));
+    }
+
+    stream->is_streaming = PJ_TRUE;
+
+    /* Send the RTP packet to the transport. */
     (*stream->transport->op->send_rtp)(stream->transport,
-				       channel->out_pkt, sent);
+				       channel->out_pkt, 
+				       frame_out.size + 
+					    sizeof(pjmedia_rtp_hdr));
 
 
