More ticket #399: added PJSUA API to retrieve the remote NAT type

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@1533 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjsip/include/pjsua-lib/pjsua.h b/pjsip/include/pjsua-lib/pjsua.h
index d076c66..695e2c8 100644
--- a/pjsip/include/pjsua-lib/pjsua.h
+++ b/pjsip/include/pjsua-lib/pjsua.h
@@ -989,13 +989,13 @@
     pj_str_t	    stun_relay_host;
 
     /**
-     * Include local endpoint's NAT type in the SDP to assist troubleshooting.
-     * The valid values are:
-     *	- 0: no information will be added in SDP.
+     * Support for adding and parsing NAT type in the SDP to assist 
+     * troubleshooting. The valid values are:
+     *	- 0: no information will be added in SDP, and parsing is disabled.
      *	- 1: only the NAT type number is added.
      *	- 2: add both NAT type number and name.
      *
-     * Default: 2
+     * Default: 1
      */
     int		    nat_type_in_sdp;
 
@@ -1365,6 +1365,8 @@
  *			PJ_SUCCESS and \a type will be set to the correct
  *			value. Other return values indicate error and
  *			\a type will be set to PJ_STUN_NAT_TYPE_ERR_UNKNOWN.
+ *
+ * @see pjsua_call_get_rem_nat_type()
  */
 PJ_DECL(pj_status_t) pjsua_get_nat_type(pj_stun_nat_type *type);
 
@@ -2731,6 +2733,30 @@
 
 
 /**
+ * Get the NAT type of remote's endpoint. This is a proprietary feature
+ * of PJSUA-LIB which sends its NAT type in the SDP when \a nat_type_in_sdp
+ * is set in #pjsua_config.
+ *
+ * This function can only be called after SDP has been received from remote,
+ * which means for incoming call, this function can be called as soon as
+ * call is received as long as incoming call contains SDP, and for outgoing
+ * call, this function can be called only after SDP is received (normally in
+ * 200/OK response to INVITE). As a general case, application should call 
+ * this function after or in \a on_call_media_state() callback.
+ *
+ * @param call_id	Call identification.
+ * @param p_type	Pointer to store the NAT type. Application can then
+ *			retrieve the string description of the NAT type
+ *			by calling pj_stun_get_nat_name().
+ *
+ * @return		PJ_SUCCESS on success.
+ *
+ * @see pjsua_get_nat_type(), nat_type_in_sdp
+ */
+PJ_DECL(pj_status_t) pjsua_call_get_rem_nat_type(pjsua_call_id call_id,
+						 pj_stun_nat_type *p_type);
+
+/**
  * Send response to incoming INVITE request. Depending on the status
  * code specified as parameter, this function may send provisional
  * response, establish the call, or terminate the call.
diff --git a/pjsip/include/pjsua-lib/pjsua_internal.h b/pjsip/include/pjsua-lib/pjsua_internal.h
index 530dc89..004c1ca 100644
--- a/pjsip/include/pjsua-lib/pjsua_internal.h
+++ b/pjsip/include/pjsua-lib/pjsua_internal.h
@@ -52,6 +52,7 @@
     pjmedia_transport	*med_tp;    /**< Media transport.		    */
     pj_timer_entry	 refresh_tm;/**< Timer to send re-INVITE.	    */
     pj_timer_entry	 hangup_tm; /**< Timer to hangup call.		    */
+    pj_stun_nat_type	 rem_nat_type; /**< NAT type of remote endpoint.    */
 
     char    last_text_buf_[128];    /**< Buffer for last_text.		    */
 
diff --git a/pjsip/src/pjsua-lib/pjsua_call.c b/pjsip/src/pjsua-lib/pjsua_call.c
index 220e2b1..f1fe320 100644
--- a/pjsip/src/pjsua-lib/pjsua_call.c
+++ b/pjsip/src/pjsua-lib/pjsua_call.c
@@ -98,6 +98,7 @@
     call->conn_time.msec = 0;
     call->res_time.sec = 0;
     call->res_time.msec = 0;
+    call->rem_nat_type = PJ_STUN_NAT_TYPE_UNKNOWN;
 }
 
 
@@ -429,6 +430,24 @@
 }
 
 
+/* Get the NAT type information in remote's SDP */
+static void update_remote_nat_type(pjsua_call *call, 
+				   const pjmedia_sdp_session *sdp)
+{
+    const pjmedia_sdp_attr *xnat;
+
+    xnat = pjmedia_sdp_attr_find2(sdp->attr_count, sdp->attr, "X-nat", NULL);
+    if (xnat) {
+	call->rem_nat_type = (pj_stun_nat_type) (xnat->value.ptr[0] - '0');
+    } else {
+	call->rem_nat_type = PJ_STUN_NAT_TYPE_UNKNOWN;
+    }
+
+    PJ_LOG(5,(THIS_FILE, "Call %d: remote NAT type is %d (%s)", call->index,
+	      call->rem_nat_type, pj_stun_get_nat_name(call->rem_nat_type)));
+}
+
+
 /**
  * Handle incoming INVITE request.
  * Called by pjsua_core.c
@@ -658,6 +677,13 @@
 	return PJ_TRUE;
     }
 
+    /* Update NAT type of remote endpoint */
+    if (pjsua_var.ua_cfg.nat_type_in_sdp) {
+	const pjmedia_sdp_session *remote_sdp;
+
+	if (pjmedia_sdp_neg_get_neg_remote(inv->neg, &remote_sdp)==PJ_SUCCESS)
+	    update_remote_nat_type(call, remote_sdp);
+    }
 
     /* Create and attach pjsua_var data to the dialog: */
     call->inv = inv;
@@ -1023,6 +1049,21 @@
 
 
 /*
+ * Get remote's NAT type.
+ */
+PJ_DEF(pj_status_t) pjsua_call_get_rem_nat_type(pjsua_call_id call_id,
+						pj_stun_nat_type *p_type)
+{
+    PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
+		     PJ_EINVAL);
+    PJ_ASSERT_RETURN(p_type != NULL, PJ_EINVAL);
+
+    *p_type = pjsua_var.calls[call_id].rem_nat_type;
+    return PJ_SUCCESS;
+}
+
+
+/*
  * Send response to incoming INVITE request.
  */
 PJ_DEF(pj_status_t) pjsua_call_answer( pjsua_call_id call_id, 
@@ -2267,7 +2308,6 @@
 	return;
     }
 
-
     status = pjmedia_sdp_neg_get_active_remote(call->inv->neg, &remote_sdp);
     if (status != PJ_SUCCESS) {
 	pjsua_perror(THIS_FILE, 
@@ -2278,6 +2318,12 @@
 	return;
     }
 
+    /* Update remote's NAT type */
+    if (pjsua_var.ua_cfg.nat_type_in_sdp) {
+	update_remote_nat_type(call, remote_sdp);
+    }
+
+    /* Update media channel with the new SDP */
     status = pjsua_media_channel_update(call->index, local_sdp, remote_sdp);
     if (status != PJ_SUCCESS) {
 	pjsua_perror(THIS_FILE, "Unable to create media session", 
diff --git a/pjsip/src/pjsua-lib/pjsua_core.c b/pjsip/src/pjsua-lib/pjsua_core.c
index a60301a..2cfca8d 100644
--- a/pjsip/src/pjsua-lib/pjsua_core.c
+++ b/pjsip/src/pjsua-lib/pjsua_core.c
@@ -86,7 +86,7 @@
 
     cfg->max_calls = 4;
     cfg->thread_cnt = 1;
-    cfg->nat_type_in_sdp = 2;
+    cfg->nat_type_in_sdp = 1;
 }
 
 PJ_DEF(void) pjsua_config_dup(pj_pool_t *pool,