Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c	(revision 412)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c	(revision 422)
@@ -80,11 +80,7 @@
 #if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
     pjsua.clock_rate = 44100;
-#else
-    pjsua.clock_rate = 8000;
 #endif
     pjsua.complexity = -1;
     pjsua.quality = 4;
-    pjsua.has_wb = 0;
-    pjsua.has_uwb = 0;
 
 
@@ -621,174 +617,91 @@
 static pj_status_t init_media(void)
 {
+    int i;
     unsigned options;
+    unsigned clock_rate;
+    unsigned samples_per_frame;
+    pj_str_t codec_id;
     pj_status_t status;
 
-    /* If user doesn't specify any codecs, register all of them. */
-    if (pjsua.codec_cnt == 0) {
-
+    /* Register all codecs */
 #if PJMEDIA_HAS_SPEEX_CODEC
-	unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB;
-
-	/* Register speex. */
-	if (pjsua.has_wb)
-	    option &= ~PJMEDIA_SPEEX_NO_WB;
-	if (pjsua.has_uwb)
-	    option &= ~PJMEDIA_SPEEX_NO_UWB;
-
-	status = pjmedia_codec_speex_init(pjsua.med_endpt, option, 
-					  pjsua.quality, pjsua.complexity );
-	if (status != PJ_SUCCESS) {
-	    pjsua_perror(THIS_FILE, "Error initializing Speex codec",
-		         status);
-	    return status;
-	}
-
-	pjsua.codec_arg[pjsua.codec_cnt] = pj_str("speex");
-	pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_speex_deinit;
-	pjsua.codec_cnt++;
+    /* Register speex. */
+    status = pjmedia_codec_speex_init(pjsua.med_endpt, 
+				      PJMEDIA_SPEEX_NO_UWB,
+				      pjsua.quality, pjsua.complexity );
+    if (status != PJ_SUCCESS) {
+	pjsua_perror(THIS_FILE, "Error initializing Speex codec",
+		     status);
+	return status;
+    }
+
+    /* Set "speex/16000/1" to have highest priority */
+    codec_id = pj_str("speex/16000/1");
+    pjmedia_codec_mgr_set_codec_priority( 
+	pjmedia_endpt_get_codec_mgr(pjsua.med_endpt),
+	&codec_id, 
+	PJMEDIA_CODEC_PRIO_HIGHEST);
+
 #endif /* PJMEDIA_HAS_SPEEX_CODEC */
 
 #if PJMEDIA_HAS_GSM_CODEC
-	/* Register GSM */
-	status = pjmedia_codec_gsm_init(pjsua.med_endpt);
-	if (status != PJ_SUCCESS) {
-	    pjsua_perror(THIS_FILE, "Error initializing GSM codec",
-		         status);
-	    return status;
-	}
-
-	pjsua.codec_arg[pjsua.codec_cnt] = pj_str("gsm");
-	pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_gsm_deinit;
-	pjsua.codec_cnt++;
+    /* Register GSM */
+    status = pjmedia_codec_gsm_init(pjsua.med_endpt);
+    if (status != PJ_SUCCESS) {
+	pjsua_perror(THIS_FILE, "Error initializing GSM codec",
+		     status);
+	return status;
+    }
 #endif /* PJMEDIA_HAS_GSM_CODEC */
 
+#if PJMEDIA_HAS_G711_CODEC
+    /* Register PCMA and PCMU */
+    status = pjmedia_codec_g711_init(pjsua.med_endpt);
+    if (status != PJ_SUCCESS) {
+	pjsua_perror(THIS_FILE, "Error initializing G711 codec",
+		     status);
+	return status;
+    }
+#endif	/* PJMEDIA_HAS_G711_CODEC */
+
 #if PJMEDIA_HAS_L16_CODEC
-	/* Register L16 */
-	status = pjmedia_codec_l16_init(pjsua.med_endpt, 0);
-	if (status != PJ_SUCCESS) {
-	    pjsua_perror(THIS_FILE, "Error initializing L16 codec",
-		         status);
-	    return status;
-	}
-
-	pjsua.codec_arg[pjsua.codec_cnt] = pj_str("l16");
-	pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_l16_deinit;
-	pjsua.codec_cnt++;
-#endif /* PJMEDIA_HAS_L16_CODEC */
-
-
-#if PJMEDIA_HAS_G711_CODEC
-	/* Register PCMA and PCMU */
-	status = pjmedia_codec_g711_init(pjsua.med_endpt);
-	if (status != PJ_SUCCESS) {
-	    pjsua_perror(THIS_FILE, "Error initializing G711 codec",
-		         status);
-	    return status;
-	}
-
-	pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcmu");
-	pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit;
-	pjsua.codec_cnt++;
-	pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcma");
-	pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit;
-	pjsua.codec_cnt++;
-#endif	/* PJMEDIA_HAS_G711_CODEC */
-
-    } else {
-
-	/* If user specifies the exact codec to be used, then create only
-	 * those codecs.
-	 */
-	int i;
-
-	for (i=0; i<pjsua.codec_cnt; ++i) {
+    /* Register L16 family codecs, but disable all */
+    status = pjmedia_codec_l16_init(pjsua.med_endpt, 0);
+    if (status != PJ_SUCCESS) {
+	pjsua_perror(THIS_FILE, "Error initializing L16 codecs",
+		     status);
+	return status;
+    }
+
+    /* Disable ALL L16 codecs */
+    codec_id = pj_str("L16");
+    pjmedia_codec_mgr_set_codec_priority( 
+	pjmedia_endpt_get_codec_mgr(pjsua.med_endpt),
+	&codec_id, 
+	PJMEDIA_CODEC_PRIO_DISABLED);
+
+#endif	/* PJMEDIA_HAS_L16_CODEC */
+
+
+    /* If user specifies the exact codec to be used, then disable all codecs
+     * and only enable those specific codecs.
+     */
+    if (pjsua.codec_cnt != 0) {
+	codec_id = pj_str("");
+	pjmedia_codec_mgr_set_codec_priority( 
+	    pjmedia_endpt_get_codec_mgr(pjsua.med_endpt),
+	    &codec_id, 
+	    PJMEDIA_CODEC_PRIO_DISABLED);
+    }
+
 	
-	    if (0) {
-		/* Dummy */
-	    }
-#if PJMEDIA_HAS_SPEEX_CODEC
-	    /* Is it speex? */
-	    else if (!pj_stricmp2(&pjsua.codec_arg[i], "speex")) {
-
-		unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB;
-
-		/* Register speex. */
-		if (pjsua.has_wb)
-		    option &= ~(PJMEDIA_SPEEX_NO_WB);
-		if (pjsua.has_uwb)
-		    option &= ~(PJMEDIA_SPEEX_NO_UWB);
-
-		status = pjmedia_codec_speex_init(pjsua.med_endpt, option,
-						  -1, -1);
-		if (status != PJ_SUCCESS) {
-		    pjsua_perror(THIS_FILE, "Error initializing Speex codec",
-			         status);
-		    return status;
-		}
-
-		pjsua.codec_deinit[i] = &pjmedia_codec_speex_deinit;
-	    }
-#endif	/* PJMEDIA_HAS_SPEEX_CODEC */
-
-#if PJMEDIA_HAS_GSM_CODEC
-	    /* Is it gsm? */
-	    else if (!pj_stricmp2(&pjsua.codec_arg[i], "gsm")) {
-
-		status = pjmedia_codec_gsm_init(pjsua.med_endpt);
-		if (status != PJ_SUCCESS) {
-		    pjsua_perror(THIS_FILE, "Error initializing GSM codec",
-			         status);
-		    return status;
-		}
-
-		pjsua.codec_deinit[i] = &pjmedia_codec_gsm_deinit;
-
-	    }
-#endif	/* PJMEDIA_HAS_GSM_CODEC */
-
-#if PJMEDIA_HAS_L16_CODEC
-	    /* Is it l16? */
-	    else if (!pj_stricmp2(&pjsua.codec_arg[i], "l16")) {
-
-		status = pjmedia_codec_l16_init(pjsua.med_endpt, 0);
-		if (status != PJ_SUCCESS) {
-		    pjsua_perror(THIS_FILE, "Error initializing L16 codec",
-			         status);
-		    return status;
-		}
-
-		pjsua.codec_deinit[i] = &pjmedia_codec_l16_deinit;
-
-		pjsua.clock_rate = 44100;
-	    }
-#endif	/* PJMEDIA_HAS_L16_CODEC */
-
-#if PJMEDIA_HAS_G711_CODEC
-	    /* Is it pcma/pcmu? */
-	    else if (!pj_stricmp2(&pjsua.codec_arg[i], "pcmu") ||
-		     !pj_stricmp2(&pjsua.codec_arg[i], "pcma"))
-	    {
-
-		status = pjmedia_codec_g711_init(pjsua.med_endpt);
-		if (status != PJ_SUCCESS) {
-		    pjsua_perror(THIS_FILE, "Error initializing G711 codec",
-			         status);
-		    return status;
-		}
-
-		pjsua.codec_deinit[i] = &pjmedia_codec_g711_deinit;
-
-	    }
-#endif	/* PJMEDIA_HAS_G711_CODEC */
-
-	    /* Don't know about this codec... */
-	    else {
-
-		PJ_LOG(1,(THIS_FILE, "Error: unsupported codecs %s",
-			  pjsua.codec_arg[i].ptr));
-		return PJMEDIA_CODEC_EUNSUP;
-	    }
-	}
-    }
+
+    for (i=0; i<pjsua.codec_cnt; ++i) {
+	pjmedia_codec_mgr_set_codec_priority( 
+	    pjmedia_endpt_get_codec_mgr(pjsua.med_endpt),
+	    &pjsua.codec_arg[i], 
+	    PJMEDIA_CODEC_PRIO_NEXT_HIGHER);
+    }
+
 
     /* Init options for conference bridge. */
@@ -798,10 +711,12 @@
 
     /* Init conference bridge. */
-
+    clock_rate = pjsua.clock_rate ? pjsua.clock_rate : 16000;
+    samples_per_frame = clock_rate * 20 / 1000;
     status = pjmedia_conf_create(pjsua.pool, 
 				 pjsua.max_calls+PJSUA_CONF_MORE_PORTS, 
-				 pjsua.clock_rate, 
+				 clock_rate, 
 				 1, /* mono */
-				 pjsua.clock_rate * 20 / 1000, 16, 
+				 samples_per_frame, 
+				 16, 
 				 options,
 				 &pjsua.mconf);
@@ -814,7 +729,7 @@
 
     /* Add NULL port to the bridge. */
-    status = pjmedia_null_port_create( pjsua.pool, pjsua.clock_rate, 
+    status = pjmedia_null_port_create( pjsua.pool, clock_rate, 
 				       1, /* mono */
-				       pjsua.clock_rate * 20 / 1000, 16,
+				       samples_per_frame, 16,
 				       &pjsua.null_port);
     pjmedia_conf_add_port( pjsua.mconf, pjsua.pool, pjsua.null_port, 
@@ -1128,7 +1043,19 @@
 
     /* Shutdown all codecs: */
-    for (i = pjsua.codec_cnt-1; i >= 0; --i) {
-	(*pjsua.codec_deinit[i])();
-    }
+#if PJMEDIA_HAS_SPEEX_CODEC
+    pjmedia_codec_speex_deinit();
+#endif /* PJMEDIA_HAS_SPEEX_CODEC */
+
+#if PJMEDIA_HAS_GSM_CODEC
+    pjmedia_codec_gsm_deinit();
+#endif /* PJMEDIA_HAS_GSM_CODEC */
+
+#if PJMEDIA_HAS_G711_CODEC
+    pjmedia_codec_g711_deinit();
+#endif	/* PJMEDIA_HAS_G711_CODEC */
+
+#if PJMEDIA_HAS_L16_CODEC
+    pjmedia_codec_l16_deinit();
+#endif	/* PJMEDIA_HAS_L16_CODEC */
 
     /* Destroy media endpoint. */
