Index: pjproject/branches/pjproject-0.5-stable/pjmedia/src/pjmedia/sdp_neg.c
===================================================================
--- pjproject/branches/pjproject-0.5-stable/pjmedia/src/pjmedia/sdp_neg.c	(revision 1069)
+++ pjproject/branches/pjproject-0.5-stable/pjmedia/src/pjmedia/sdp_neg.c	(revision 1071)
@@ -32,4 +32,5 @@
 {
     pjmedia_sdp_neg_state state;	    /**< Negotiator state.	     */
+    pj_bool_t		  prefer_remote_codec_order;
     pj_bool_t		  has_remote_answer;
     pj_bool_t		  answer_was_remote;
@@ -87,4 +88,5 @@
 
     neg->state = PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER;
+    neg->prefer_remote_codec_order = PJMEDIA_SDP_NEG_PREFER_REMOTE_CODEC_ORDER;
     neg->initial_sdp = pjmedia_sdp_session_clone(pool, local);
     neg->neg_local_sdp = pjmedia_sdp_session_clone(pool, local);
@@ -120,4 +122,5 @@
     PJ_ASSERT_RETURN(neg != NULL, PJ_ENOMEM);
 
+    neg->prefer_remote_codec_order = PJMEDIA_SDP_NEG_PREFER_REMOTE_CODEC_ORDER;
     neg->neg_remote_sdp = pjmedia_sdp_session_clone(pool, remote);
 
@@ -140,4 +143,18 @@
     return PJ_SUCCESS;
 }
+
+
+/*
+ * Set codec order preference.
+ */
+PJ_DEF(pj_status_t)
+pjmedia_sdp_neg_set_prefer_remote_codec_order(pjmedia_sdp_neg *neg,
+					      pj_bool_t prefer_remote)
+{
+    PJ_ASSERT_RETURN(neg, PJ_EINVAL);
+    neg->prefer_remote_codec_order = prefer_remote;
+    return PJ_SUCCESS;
+}
+
 
 /*
@@ -668,5 +685,6 @@
 static pj_status_t match_offer(pj_pool_t *pool,
 			       const pjmedia_sdp_media *offer,
-			       const pjmedia_sdp_media *local,
+			       const pjmedia_sdp_media *preanswer,
+			       const pjmedia_sdp_media *orig_local,
 			       pjmedia_sdp_media **p_answer)
 {
@@ -711,10 +729,10 @@
 
 		/* Find matching codec in local descriptor. */
-		for (j=0; j<local->desc.fmt_count; ++j) {
+		for (j=0; j<preanswer->desc.fmt_count; ++j) {
 		    unsigned p;
-		    p = pj_strtoul(&local->desc.fmt[j]);
-		    if (p == pt && pj_isdigit(*local->desc.fmt[j].ptr)) {
+		    p = pj_strtoul(&preanswer->desc.fmt[j]);
+		    if (p == pt && pj_isdigit(*preanswer->desc.fmt[j].ptr)) {
 			found_matching_codec = 1;
-			pt_answer[pt_answer_count++] = local->desc.fmt[j];
+			pt_answer[pt_answer_count++] = preanswer->desc.fmt[j];
 			break;
 		    }
@@ -754,7 +772,7 @@
 		 * encoding name and clock rate.
 		 */
-		for (j=0; j<local->desc.fmt_count; ++j) {
-		    a = pjmedia_sdp_media_find_attr2(local, "rtpmap", 
-						     &local->desc.fmt[j]);
+		for (j=0; j<preanswer->desc.fmt_count; ++j) {
+		    a = pjmedia_sdp_media_find_attr2(preanswer, "rtpmap", 
+						     &preanswer->desc.fmt[j]);
 		    if (a) {
 			pjmedia_sdp_rtpmap lr;
@@ -774,5 +792,5 @@
 			    else
 				found_matching_telephone_event = 1;
-			    pt_answer[pt_answer_count++] = local->desc.fmt[j];
+			    pt_answer[pt_answer_count++] = preanswer->desc.fmt[j];
 			    break;
 			}
@@ -792,9 +810,9 @@
 		continue;
 
-	    for (j=0; j<local->desc.fmt_count; ++j) {
-		if (!pj_strcmp(&offer->desc.fmt[i], &local->desc.fmt[j])) {
+	    for (j=0; j<preanswer->desc.fmt_count; ++j) {
+		if (!pj_strcmp(&offer->desc.fmt[i], &preanswer->desc.fmt[j])) {
 		    /* Match */
 		    found_matching_other = 1;
-		    pt_answer[pt_answer_count++] = local->desc.fmt[j];
+		    pt_answer[pt_answer_count++] = preanswer->desc.fmt[j];
 		    break;
 		}
@@ -824,5 +842,5 @@
      * to suit the offer.
      */
-    answer = pjmedia_sdp_media_clone(pool, local);
+    answer = pjmedia_sdp_media_clone(pool, orig_local);
     for (i=0; i<pt_answer_count; ++i) {
 	unsigned j;
@@ -868,4 +886,5 @@
 /* Create complete answer for remote's offer. */
 static pj_status_t create_answer( pj_pool_t *pool,
+				  pj_bool_t prefer_remote_codec_order,
 				  const pjmedia_sdp_session *initial,
 				  const pjmedia_sdp_session *offer,
@@ -915,5 +934,10 @@
 	    {
 		/* See if it has matching codec. */
-		status = match_offer(pool, om, im, &am);
+		if (prefer_remote_codec_order) {
+		    status = match_offer(pool, om, im, im, &am);
+		} else {
+		    status = match_offer(pool, im, om, im, &am);
+		}
+
 		if (status == PJ_SUCCESS) {
 		    /* Mark media as used. */
@@ -992,5 +1016,6 @@
 	pjmedia_sdp_session *answer = NULL;
 
-	status = create_answer(pool, neg->neg_local_sdp, neg->neg_remote_sdp,
+	status = create_answer(pool, neg->prefer_remote_codec_order, 
+			       neg->neg_local_sdp, neg->neg_remote_sdp,
 			       &answer);
 	if (status == PJ_SUCCESS) {
