Index: pjproject/trunk/pjmedia/include/pjmedia/codec.h
===================================================================
--- pjproject/trunk/pjmedia/include/pjmedia/codec.h	(revision 1984)
+++ pjproject/trunk/pjmedia/include/pjmedia/codec.h	(revision 1985)
@@ -254,4 +254,5 @@
        unsigned	   channel_cnt;		/**< Channel count.		    */
        pj_uint32_t avg_bps;		/**< Average bandwidth in bits/sec  */
+       pj_uint32_t max_bps;		/**< Maximum bandwidth in bits/sec  */
        pj_uint16_t frm_ptime;		/**< Decoder frame ptime in msec.   */
        pj_uint16_t enc_ptime;		/**< Encoder ptime, or zero if it's
Index: pjproject/trunk/pjmedia/src/pjmedia-codec/g722.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia-codec/g722.c	(revision 1984)
+++ pjproject/trunk/pjmedia/src/pjmedia-codec/g722.c	(revision 1985)
@@ -271,4 +271,5 @@
     attr->info.channel_cnt = 1;
     attr->info.avg_bps = 64000;
+    attr->info.max_bps = 64000;
     attr->info.pcm_bits_per_sample = 16;
     attr->info.frm_ptime = PTIME;
Index: pjproject/trunk/pjmedia/src/pjmedia-codec/gsm.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia-codec/gsm.c	(revision 1984)
+++ pjproject/trunk/pjmedia/src/pjmedia-codec/gsm.c	(revision 1985)
@@ -265,4 +265,5 @@
     attr->info.channel_cnt = 1;
     attr->info.avg_bps = 13200;
+    attr->info.max_bps = 13200;
     attr->info.pcm_bits_per_sample = 16;
     attr->info.frm_ptime = 20;
Index: pjproject/trunk/pjmedia/src/pjmedia-codec/ilbc.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia-codec/ilbc.c	(revision 1984)
+++ pjproject/trunk/pjmedia/src/pjmedia-codec/ilbc.c	(revision 1985)
@@ -265,4 +265,5 @@
     attr->info.channel_cnt = 1;
     attr->info.avg_bps = ilbc_factory.bps;
+    attr->info.max_bps = 15200;
     attr->info.pcm_bits_per_sample = 16;
     attr->info.frm_ptime = (short)ilbc_factory.mode;
Index: pjproject/trunk/pjmedia/src/pjmedia-codec/l16.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia-codec/l16.c	(revision 1984)
+++ pjproject/trunk/pjmedia/src/pjmedia-codec/l16.c	(revision 1985)
@@ -243,4 +243,5 @@
     attr->info.channel_cnt = id->channel_cnt;
     attr->info.avg_bps = id->clock_rate * id->channel_cnt * 16;
+    attr->info.max_bps = attr->info.avg_bps;
     attr->info.pcm_bits_per_sample = 16;
 
Index: pjproject/trunk/pjmedia/src/pjmedia-codec/speex_codec.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia-codec/speex_codec.c	(revision 1984)
+++ pjproject/trunk/pjmedia/src/pjmedia-codec/speex_codec.c	(revision 1985)
@@ -122,4 +122,5 @@
     int		     framesize;		/* Frame size for current mode.	    */
     int		     bitrate;		/* Bit rate for current mode.	    */
+    int		     max_bitrate;	/* Max bit rate for current mode.   */
 };
 
@@ -161,7 +162,7 @@
 	return PJMEDIA_CODEC_EFAILED;
 
-    /* We have to get maximum bitrate, so let's set maximum quality */
-    tmp = 10;
-    speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);
+    /* Set the quality */
+    if (p->quality != -1)
+	speex_encoder_ctl(state, SPEEX_SET_QUALITY, &p->quality);
 
     /* Sampling rate. */
@@ -179,9 +180,14 @@
     speex_encoder_ctl(state, SPEEX_GET_FRAME_SIZE, &p->samples_per_frame);
 
-    /* Now get the the averate bitrate */
+    /* Now get the average bitrate */
     speex_encoder_ctl(state, SPEEX_GET_BITRATE, &p->bitrate);
 
     /* Calculate framesize. */
     p->framesize = p->bitrate * 20 / 1000;
+
+    /* Now get the maximum bitrate by using maximum quality (=10) */
+    tmp = 10;
+    speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);
+    speex_encoder_ctl(state, SPEEX_GET_BITRATE, &p->max_bitrate);
 
     /* Destroy encoder. */
@@ -238,5 +244,5 @@
 	((options & PJMEDIA_SPEEX_NO_NB) == 0);
     spx_factory.speex_param[PARAM_NB].pt = PJMEDIA_RTP_PT_SPEEX_NB;
-    spx_factory.speex_param[PARAM_NB].mode = &speex_nb_mode;
+    spx_factory.speex_param[PARAM_NB].mode = speex_lib_get_mode(SPEEX_MODEID_NB);
     spx_factory.speex_param[PARAM_NB].clock_rate = 8000;
     spx_factory.speex_param[PARAM_NB].quality = quality;
@@ -246,5 +252,5 @@
 	((options & PJMEDIA_SPEEX_NO_WB) == 0);
     spx_factory.speex_param[PARAM_WB].pt = PJMEDIA_RTP_PT_SPEEX_WB;
-    spx_factory.speex_param[PARAM_WB].mode = &speex_wb_mode;
+    spx_factory.speex_param[PARAM_WB].mode = speex_lib_get_mode(SPEEX_MODEID_WB);
     spx_factory.speex_param[PARAM_WB].clock_rate = 16000;
     spx_factory.speex_param[PARAM_WB].quality = quality;
@@ -254,5 +260,5 @@
 	((options & PJMEDIA_SPEEX_NO_UWB) == 0);
     spx_factory.speex_param[PARAM_UWB].pt = PJMEDIA_RTP_PT_SPEEX_UWB;
-    spx_factory.speex_param[PARAM_UWB].mode = &speex_uwb_mode;
+    spx_factory.speex_param[PARAM_UWB].mode = speex_lib_get_mode(SPEEX_MODEID_UWB);
     spx_factory.speex_param[PARAM_UWB].clock_rate = 32000;
     spx_factory.speex_param[PARAM_UWB].quality = quality;
@@ -438,8 +444,10 @@
 	attr->info.clock_rate = spx_factory.speex_param[PARAM_NB].clock_rate;
 	attr->info.avg_bps = spx_factory.speex_param[PARAM_NB].bitrate;
+	attr->info.max_bps = spx_factory.speex_param[PARAM_NB].max_bitrate;
 
     } else if (id->clock_rate <= 16000) {
 	attr->info.clock_rate = spx_factory.speex_param[PARAM_WB].clock_rate;
 	attr->info.avg_bps = spx_factory.speex_param[PARAM_WB].bitrate;
+	attr->info.max_bps = spx_factory.speex_param[PARAM_WB].max_bitrate;
 
     } else {
@@ -447,4 +455,5 @@
 	attr->info.clock_rate = spx_factory.speex_param[PARAM_UWB].clock_rate;
 	attr->info.avg_bps = spx_factory.speex_param[PARAM_UWB].bitrate;
+	attr->info.max_bps = spx_factory.speex_param[PARAM_UWB].max_bitrate;
     }
 
Index: pjproject/trunk/pjmedia/src/pjmedia/codec.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia/codec.c	(revision 1984)
+++ pjproject/trunk/pjmedia/src/pjmedia/codec.c	(revision 1985)
@@ -386,6 +386,11 @@
 
 	    status = (*factory->op->default_attr)(factory, info, param);
-	    if (status == PJ_SUCCESS)
+	    if (status == PJ_SUCCESS) {
+		/* Check for invalid max_bps. */
+		if (param->info.max_bps < param->info.avg_bps)
+		    param->info.max_bps = param->info.avg_bps;
+
 		return PJ_SUCCESS;
+	    }
 
 	}
Index: pjproject/trunk/pjmedia/src/pjmedia/g711.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia/g711.c	(revision 1984)
+++ pjproject/trunk/pjmedia/src/pjmedia/g711.c	(revision 1985)
@@ -250,4 +250,5 @@
     attr->info.channel_cnt = 1;
     attr->info.avg_bps = G711_BPS;
+    attr->info.max_bps = G711_BPS;
     attr->info.pcm_bits_per_sample = 16;
     attr->info.frm_ptime = PTIME;
Index: pjproject/trunk/pjmedia/src/pjmedia/stream.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia/stream.c	(revision 1984)
+++ pjproject/trunk/pjmedia/src/pjmedia/stream.c	(revision 1985)
@@ -1264,7 +1264,7 @@
 
     channel->out_pkt_size = sizeof(pjmedia_rtp_hdr) + 
-			    stream->codec_param.info.avg_bps/8 * 
+			    stream->codec_param.info.max_bps * 
 			    PJMEDIA_MAX_FRAME_DURATION_MS / 
-			    1000;
+			    8 / 1000;
 
     if (channel->out_pkt_size > PJMEDIA_MAX_MTU)
@@ -1374,4 +1374,8 @@
     }
 
+    /* Check for invalid max_bps. */
+    if (stream->codec_param.info.max_bps < stream->codec_param.info.avg_bps)
+	stream->codec_param.info.max_bps = stream->codec_param.info.avg_bps;
+
     /* Check for invalid frame per packet. */
     if (stream->codec_param.setting.frm_per_pkt < 1)
@@ -1385,8 +1389,13 @@
 					  stream->codec_param.setting.frm_per_pkt /
 					  1000;
-    stream->port.info.bytes_per_frame = stream->codec_param.info.avg_bps/8 * 
+    stream->port.info.bytes_per_frame = stream->codec_param.info.max_bps * 
 					stream->codec_param.info.frm_ptime *
 					stream->codec_param.setting.frm_per_pkt /
-					1000;
+					8 / 1000;
+    if ((stream->codec_param.info.max_bps * stream->codec_param.info.frm_ptime *
+	stream->codec_param.setting.frm_per_pkt) % 8000 != 0)
+    {
+	++stream->port.info.bytes_per_frame;
+    }
 
     /* Open the codec: */
@@ -1442,9 +1451,12 @@
     }
 
-    /* Get the frame size: */
-
-    stream->frame_size = ((stream->codec_param.info.avg_bps + 7) / 8) * 
-			  stream->codec_param.info.frm_ptime / 1000;
-
+    /* Get the frame size */
+    stream->frame_size = stream->codec_param.info.max_bps * 
+			 stream->codec_param.info.frm_ptime / 8 / 1000;
+    if ((stream->codec_param.info.max_bps * stream->codec_param.info.frm_ptime) 
+	% 8000 != 0)
+    {
+	++stream->frame_size;
+    }
 
 #if defined(PJMEDIA_HANDLE_G722_MPEG_BUG) && (PJMEDIA_HANDLE_G722_MPEG_BUG!=0)
