Index: /pjproject/trunk/pjnath/include/pjnath/config.h
===================================================================
--- /pjproject/trunk/pjnath/include/pjnath/config.h	(revision 2053)
+++ /pjproject/trunk/pjnath/include/pjnath/config.h	(revision 2054)
@@ -245,10 +245,39 @@
 
 /**
+ * The number of bits to represent component IDs. This will affect
+ * the maximum number of components (PJ_ICE_MAX_COMP) value.
+ */
+#ifndef PJ_ICE_COMP_BITS
+#   define PJ_ICE_COMP_BITS			    3
+#endif
+
+
+/**
  * Maximum number of ICE components.
- *
- * Default: 8
- */
-#ifndef PJ_ICE_MAX_COMP
-#   define PJ_ICE_MAX_COMP			    8
+ */
+#define PJ_ICE_MAX_COMP		    (2<<PJ_ICE_COMP_BITS)
+
+
+/**
+ * The number of bits to represent candidate type preference.
+ */
+#ifndef PJ_ICE_CAND_TYPE_PREF_BITS
+#   define PJ_ICE_CAND_TYPE_PREF_BITS		    2
+#endif
+
+
+/**
+ * The number of bits to represent ICE candidate's local preference. The
+ * local preference is used to specify preference among candidates with
+ * the same type, and ICE draft suggests 65535 as the default local 
+ * preference, which means we need 16 bits to represent the value. But 
+ * since we don't have the facility to specify local preference, we'll
+ * just disable this feature and let the preference sorted by the 
+ * type only.
+ *
+ * Default: 0
+ */
+#ifndef PJ_ICE_LOCAL_PREF_BITS
+#   define PJ_ICE_LOCAL_PREF_BITS		    0
 #endif
 
Index: /pjproject/trunk/pjnath/src/pjnath/ice_session.c
===================================================================
--- /pjproject/trunk/pjnath/src/pjnath/ice_session.c	(revision 2053)
+++ /pjproject/trunk/pjnath/src/pjnath/ice_session.c	(revision 2054)
@@ -66,11 +66,20 @@
 };
 
-/* Default ICE session preferences, according to draft-ice */
+/* Candidate type preference */
 static pj_uint8_t cand_type_prefs[4] =
 {
+#if PJ_ICE_CAND_TYPE_PREF_BITS < 8
+    /* Keep it to 2 bits */
+    3,	    /**< PJ_ICE_HOST_PREF	*/
+    1,	    /**< PJ_ICE_SRFLX_PREF.	*/
+    2,	    /**< PJ_ICE_PRFLX_PREF	*/
+    0	    /**< PJ_ICE_RELAYED_PREF	*/
+#else
+    /* Default ICE session preferences, according to draft-ice */
     126,    /**< PJ_ICE_HOST_PREF	*/
     100,    /**< PJ_ICE_SRFLX_PREF.	*/
     110,    /**< PJ_ICE_PRFLX_PREF	*/
     0	    /**< PJ_ICE_RELAYED_PREF	*/
+#endif
 };
 
@@ -216,4 +225,5 @@
 				    const pj_sockaddr *base_addr)
 {
+#if 0
     char buf[64];
     pj_uint32_t val;
@@ -228,4 +238,14 @@
 		     get_type_prefix(type), val);
     pj_strdup2(pool, foundation, buf);
+#else
+    /* Much shorter version, valid for candidates added by
+     * pj_ice_strans.
+     */
+    foundation->ptr = (char*) pj_pool_alloc(pool, 1);
+    *foundation->ptr = (char)get_type_prefix(type);
+    foundation->slen = 1;
+
+    PJ_UNUSED_ARG(base_addr);
+#endif
 }
 
@@ -451,8 +471,12 @@
 					  const pj_uint8_t prefs[4])
 {
+    unsigned i;
     PJ_ASSERT_RETURN(ice && prefs, PJ_EINVAL);
     ice->prefs = (pj_uint8_t*) pj_pool_calloc(ice->pool, PJ_ARRAY_SIZE(prefs),
 					      sizeof(pj_uint8_t));
-    pj_memcpy(ice->prefs, prefs, sizeof(prefs));
+    for (i=0; i<4; ++i) {
+	pj_assert(prefs[i] < (2 << PJ_ICE_CAND_TYPE_PREF_BITS));
+	ice->prefs[i] = prefs[i];
+    }
     return PJ_SUCCESS;
 }
@@ -579,7 +603,25 @@
 				  pj_uint32_t comp_id)
 {
+#if 0
     return ((ice->prefs[type] & 0xFF) << 24) + 
 	   ((local_pref & 0xFFFF)    << 8) +
 	   (((256 - comp_id) & 0xFF) << 0);
+#else
+    enum {
+	type_mask   = ((2 << PJ_ICE_CAND_TYPE_PREF_BITS) - 1),
+	local_mask  = ((2 << PJ_ICE_LOCAL_PREF_BITS) - 1),
+	comp_mask   = ((2 << PJ_ICE_COMP_BITS) - 1),
+
+	comp_shift  = 0,
+	local_shift = (PJ_ICE_COMP_BITS),
+	type_shift  = (comp_shift + local_shift),
+
+	max_comp    = (2<<PJ_ICE_COMP_BITS),
+    };
+
+    return ((ice->prefs[type] & type_mask) << type_shift) + 
+	   ((local_pref & local_mask) << local_shift) +
+	   (((max_comp - comp_id) & comp_mask) << comp_shift);
+#endif
 }
 
@@ -1430,5 +1472,5 @@
 
     /* Add PRIORITY */
-    prio = CALC_CAND_PRIO(ice, PJ_ICE_CAND_TYPE_PRFLX, 65535, 
+    prio = CALC_CAND_PRIO(ice, PJ_ICE_CAND_TYPE_PRFLX, 0, 
 			  lcand->comp_id);
     pj_stun_msg_add_uint_attr(check->tdata->pool, check->tdata->msg, 
Index: /pjproject/trunk/pjnath/src/pjnath/ice_strans.c
===================================================================
--- /pjproject/trunk/pjnath/src/pjnath/ice_strans.c	(revision 2053)
+++ /pjproject/trunk/pjnath/src/pjnath/ice_strans.c	(revision 2054)
@@ -46,8 +46,22 @@
 };
 
-/* Candidate preference default values */
-#define SRFLX_PREF  65535
-#define HOST_PREF   65530
-#define RELAY_PREF  65525
+/* Candidate's local preference values. This is mostly used to
+ * specify preference among candidates with the same type. Since
+ * we don't have the facility to specify that, we'll just set it
+ * all to zero.
+ */
+#define SRFLX_PREF  0
+#define HOST_PREF   0
+#define RELAY_PREF  0
+
+/* The candidate type preference when STUN candidate is used */
+static pj_uint8_t srflx_pref_table[4] = 
+{
+    /* Keep it to 2 bits */
+    1,	/**< PJ_ICE_HOST_PREF	    */
+    2,	/**< PJ_ICE_SRFLX_PREF	    */
+    3,	/**< PJ_ICE_PRFLX_PREF	    */
+    0	/**< PJ_ICE_RELAYED_PREF    */
+};
 
 
@@ -640,5 +654,4 @@
     ice_st->ice->user_data = (void*)ice_st;
 
-#if 0
     /* If default candidate for components are SRFLX one, upload a custom
      * type priority to ICE session so that SRFLX candidates will get
@@ -649,7 +662,6 @@
 	    == PJ_ICE_CAND_TYPE_SRFLX)
     {
-	pj_ice_sess_set_prefs(ice_st->ice, srflx_prio);
-    }
-#endif
+	pj_ice_sess_set_prefs(ice_st->ice, srflx_pref_table);
+    }
 
     /* Add components/candidates */
