Index: pjproject/trunk/pjmedia/src/pjmedia/vid_stream.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia/vid_stream.c	(revision 4010)
+++ pjproject/trunk/pjmedia/src/pjmedia/vid_stream.c	(revision 4012)
@@ -118,4 +118,8 @@
     pj_uint32_t		     rtcp_interval; /**< Interval, in timestamp.    */
     pj_bool_t		     initial_rr;    /**< Initial RTCP RR sent	    */
+    pj_bool_t                rtcp_sdes_bye_disabled;/**< Send RTCP SDES/BYE?*/
+    void		    *out_rtcp_pkt;  /**< Outgoing RTCP packet.	    */
+    unsigned		     out_rtcp_pkt_size;
+					    /**< Outgoing RTCP packet size. */
 
     unsigned		     dec_max_size;  /**< Size of decoded/raw picture*/
@@ -169,4 +173,9 @@
     PJ_LOG(4,(sender, "%s: %s [err:%d]", title, errmsg, status));
 }
+
+
+static pj_status_t send_rtcp(pjmedia_vid_stream *stream,
+			     pj_bool_t with_sdes,
+			     pj_bool_t with_bye);
 
 
@@ -400,6 +409,5 @@
 
     /* Send RTCP */
-    pjmedia_rtcp_build_rtcp(&stream->rtcp, &pkt, &pkt_len);
-    pjmedia_transport_send_rtcp(stream->transport, pkt, pkt_len);
+    send_rtcp(stream, PJ_TRUE, PJ_FALSE, PJ_FALSE);
 
 #elif PJMEDIA_STREAM_ENABLE_KA == PJMEDIA_STREAM_KA_USER
@@ -431,4 +439,65 @@
 
 
+static pj_status_t send_rtcp(pjmedia_vid_stream *stream,
+			     pj_bool_t with_sdes,
+			     pj_bool_t with_bye)
+{
+    void *sr_rr_pkt;
+    pj_uint8_t *pkt;
+    int len, max_len;
+    pj_status_t status;
+
+    /* Build RTCP RR/SR packet */
+    pjmedia_rtcp_build_rtcp(&stream->rtcp, &sr_rr_pkt, &len);
+
+    if (with_sdes || with_bye) {
+	pkt = (pj_uint8_t*) stream->out_rtcp_pkt;
+	pj_memcpy(pkt, sr_rr_pkt, len);
+	max_len = stream->out_rtcp_pkt_size;
+    } else {
+	pkt = sr_rr_pkt;
+	max_len = len;
+    }
+
+    /* Build RTCP SDES packet */
+    if (with_sdes) {
+	pjmedia_rtcp_sdes sdes;
+	pj_size_t sdes_len;
+
+	pj_bzero(&sdes, sizeof(sdes));
+	sdes.cname = stream->cname;
+	sdes_len = max_len - len;
+	status = pjmedia_rtcp_build_rtcp_sdes(&stream->rtcp, pkt+len,
+					      &sdes_len, &sdes);
+	if (status != PJ_SUCCESS) {
+	    PJ_PERROR(4,(stream->name.ptr, status,
+        			     "Error generating RTCP SDES"));
+	} else {
+	    len += sdes_len;
+	}
+    }
+
+    /* Build RTCP BYE packet */
+    if (with_bye) {
+	pj_size_t bye_len;
+
+	bye_len = max_len - len;
+	status = pjmedia_rtcp_build_rtcp_bye(&stream->rtcp, pkt+len,
+					     &bye_len, NULL);
+	if (status != PJ_SUCCESS) {
+	    PJ_PERROR(4,(stream->name.ptr, status,
+        			     "Error generating RTCP BYE"));
+	} else {
+	    len += bye_len;
+	}
+    }
+
+    /* Send! */
+    status = pjmedia_transport_send_rtcp(stream->transport, pkt, len);
+
+    return status;
+}
+
+
 /**
  * check_tx_rtcp()
@@ -450,76 +519,14 @@
 
     } else if (timestamp - stream->rtcp_last_tx >= stream->rtcp_interval) {
+	pj_status_t status;
 	
-	void *rtcp_pkt;
-	int len;
-
-	pjmedia_rtcp_build_rtcp(&stream->rtcp, &rtcp_pkt, &len);
-
-	pjmedia_transport_send_rtcp(stream->transport, rtcp_pkt, len);
+	status = send_rtcp(stream, !stream->rtcp_sdes_bye_disabled, PJ_FALSE);
+	if (status != PJ_SUCCESS) {
+	    PJ_PERROR(4,(stream->name.ptr, status,
+        		 "Error sending RTCP"));
+	}
 
 	stream->rtcp_last_tx = timestamp;
     }
-}
-
-/* Build RTCP SDES packet */
-static unsigned create_rtcp_sdes(pjmedia_vid_stream *stream, pj_uint8_t *pkt,
-				 unsigned max_len)
-{
-    pjmedia_rtcp_common hdr;
-    pj_uint8_t *p = pkt;
-
-    /* SDES header */
-    hdr.version = 2;
-    hdr.p = 0;
-    hdr.count = 1;
-    hdr.pt = 202;
-    hdr.length = 2 + (4+stream->cname.slen+3)/4 - 1;
-    if (max_len < (hdr.length << 2)) {
-	pj_assert(!"Not enough buffer for SDES packet");
-	return 0;
-    }
-    hdr.length = pj_htons((pj_uint16_t)hdr.length);
-    hdr.ssrc = stream->enc->rtp.out_hdr.ssrc;
-    pj_memcpy(p, &hdr, sizeof(hdr));
-    p += sizeof(hdr);
-
-    /* CNAME item */
-    *p++ = 1;
-    *p++ = (pj_uint8_t)stream->cname.slen;
-    pj_memcpy(p, stream->cname.ptr, stream->cname.slen);
-    p += stream->cname.slen;
-
-    /* END */
-    *p++ = '\0';
-    *p++ = '\0';
-
-    /* Pad to 32bit */
-    while ((p-pkt) % 4)
-	*p++ = '\0';
-
-    return (p - pkt);
-}
-
-/* Build RTCP BYE packet */
-static unsigned create_rtcp_bye(pjmedia_vid_stream *stream, pj_uint8_t *pkt,
-				unsigned max_len)
-{
-    pjmedia_rtcp_common hdr;
-
-    /* BYE header */
-    hdr.version = 2;
-    hdr.p = 0;
-    hdr.count = 1;
-    hdr.pt = 203;
-    hdr.length = 1;
-    if (max_len < (hdr.length << 2)) {
-	pj_assert(!"Not enough buffer for SDES packet");
-	return 0;
-    }
-    hdr.length = pj_htons((pj_uint16_t)hdr.length);
-    hdr.ssrc = stream->enc->rtp.out_hdr.ssrc;
-    pj_memcpy(pkt, &hdr, sizeof(hdr));
-
-    return sizeof(hdr);
 }
 
@@ -717,25 +724,12 @@
     /* Send RTCP RR and SDES after we receive some RTP packets */
     if (stream->rtcp.received >= 10 && !stream->initial_rr) {
-	void *sr_rr_pkt;
-	pj_uint8_t *pkt;
-	int len;
-
-	/* Build RR or SR */
-	pjmedia_rtcp_build_rtcp(&stream->rtcp, &sr_rr_pkt, &len);
-	pkt = (pj_uint8_t*) stream->enc->buf;
-	pj_memcpy(pkt, sr_rr_pkt, len);
-	pkt += len;
-
-	/* Append SDES */
-	len = create_rtcp_sdes(stream, (pj_uint8_t*)pkt, 
-			       stream->enc->buf_size - len);
-	if (len > 0) {
-	    pkt += len;
-	    len = ((pj_uint8_t*)pkt) - ((pj_uint8_t*)stream->enc->buf);
-	    pjmedia_transport_send_rtcp(stream->transport, 
-					stream->enc->buf, len);
-	}
-
-	stream->initial_rr = PJ_TRUE;
+	status = send_rtcp(stream, !stream->rtcp_sdes_bye_disabled,
+			   PJ_FALSE);
+        if (status != PJ_SUCCESS) {
+            PJ_PERROR(4,(stream->name.ptr, status,
+            	     "Error sending initial RTCP RR"));
+	} else {
+	    stream->initial_rr = PJ_TRUE;
+	}
     }
 }
@@ -1287,5 +1281,5 @@
     pj_pool_t *own_pool = NULL;
     pjmedia_vid_stream *stream;
-    unsigned jb_init, jb_max, jb_min_pre, jb_max_pre, len;
+    unsigned jb_init, jb_max, jb_min_pre, jb_max_pre;
     int frm_ptime, chunks_per_frm;
     pjmedia_video_format_detail *vfd_enc, *vfd_dec;
@@ -1358,4 +1352,5 @@
     stream->rtcp_interval = (PJMEDIA_RTCP_INTERVAL-500 + (pj_rand()%1000)) *
 			    info->codec_info.clock_rate / 1000;
+    stream->rtcp_sdes_bye_disabled = info->rtcp_sdes_bye_disabled;
 
     stream->jb_last_frm = PJMEDIA_JB_NORMAL_FRAME;
@@ -1508,4 +1503,16 @@
     }
 
+    /* Allocate outgoing RTCP buffer, should be enough to hold SR/RR, SDES,
+     * BYE, and XR.
+     */
+    stream->out_rtcp_pkt_size =  sizeof(pjmedia_rtcp_sr_pkt) +
+				 sizeof(pjmedia_rtcp_common) +
+				 (4 + stream->cname.slen) +
+				 32;
+    if (stream->out_rtcp_pkt_size > PJMEDIA_MAX_MTU)
+	stream->out_rtcp_pkt_size = PJMEDIA_MAX_MTU;
+
+    stream->out_rtcp_pkt = pj_pool_alloc(pool, stream->out_rtcp_pkt_size);
+
     /* Only attach transport when stream is ready. */
     status = pjmedia_transport_attach(tp, stream, &info->rem_addr, 
@@ -1519,9 +1526,6 @@
 
     /* Send RTCP SDES */
-    len = create_rtcp_sdes(stream, (pj_uint8_t*)stream->enc->buf, 
-			   stream->enc->buf_size);
-    if (len != 0) {
-	pjmedia_transport_send_rtcp(stream->transport, 
-				    stream->enc->buf, len);
+    if (!stream->rtcp_sdes_bye_disabled) {
+        pjmedia_vid_stream_send_rtcp_sdes(stream);
     }
 
@@ -1579,15 +1583,9 @@
 PJ_DEF(pj_status_t) pjmedia_vid_stream_destroy( pjmedia_vid_stream *stream )
 {
-    unsigned len;
     PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL);
 
-    /* Send RTCP BYE */
-    if (stream->enc && stream->transport) {
-	len = create_rtcp_bye(stream, (pj_uint8_t*)stream->enc->buf,
-			      stream->enc->buf_size);
-	if (len != 0) {
-	    pjmedia_transport_send_rtcp(stream->transport, 
-					stream->enc->buf, len);
-	}
+    /* Send RTCP BYE (also SDES) */
+    if (!stream->rtcp_sdes_bye_disabled) {
+	send_rtcp(stream, PJ_TRUE, PJ_TRUE);
     }
 
@@ -1824,4 +1822,5 @@
 }
 
+
 /*
  * Force stream to send video keyframe.
@@ -1841,3 +1840,31 @@
 
 
+/*
+ * Send RTCP SDES.
+ */
+PJ_DEF(pj_status_t) pjmedia_vid_stream_send_rtcp_sdes(
+						pjmedia_vid_stream *stream)
+{
+    PJ_ASSERT_RETURN(stream, PJ_EINVAL);
+
+    return send_rtcp(stream, PJ_TRUE, PJ_FALSE);
+}
+
+
+/*
+ * Send RTCP BYE.
+ */
+PJ_DEF(pj_status_t) pjmedia_vid_stream_send_rtcp_bye(
+						pjmedia_vid_stream *stream)
+{
+    PJ_ASSERT_RETURN(stream, PJ_EINVAL);
+
+    if (stream->enc && stream->transport) {
+	return send_rtcp(stream, PJ_TRUE, PJ_TRUE);
+    }
+
+    return PJ_SUCCESS;
+}
+
+
 #endif /* PJMEDIA_HAS_VIDEO */
