From 93c46290dcf20a704b36e66208543ff2bc36e23b Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Fri, 6 Jun 2008 14:51:48 +0000 Subject: More ticket #485: added TURN support in PJSUA-LIB API git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1990 74dad513-b988-da41-8d7b-12977e46ad98 --- pjsip/include/pjsua-lib/pjsua.h | 37 ++++-- pjsip/include/pjsua-lib/pjsua_internal.h | 1 + pjsip/src/pjsua-lib/pjsua_core.c | 3 +- pjsip/src/pjsua-lib/pjsua_media.c | 194 ++++++++++++++++++++----------- 4 files changed, 155 insertions(+), 80 deletions(-) (limited to 'pjsip') diff --git a/pjsip/include/pjsua-lib/pjsua.h b/pjsip/include/pjsua-lib/pjsua.h index 9f42b9ce..9e9ee2a2 100644 --- a/pjsip/include/pjsua-lib/pjsua.h +++ b/pjsip/include/pjsua-lib/pjsua.h @@ -1028,8 +1028,8 @@ typedef struct pjsua_config /** * Specify domain name to be resolved with DNS SRV resolution to get the - * address of the STUN servers. Alternatively application may specify - * \a stun_host and \a stun_relay_host instead. + * address of the STUN server. Alternatively application may specify + * \a stun_host instead. * * If DNS SRV resolution failed for this domain, then DNS A resolution * will be performed only if \a stun_host is specified. @@ -1042,11 +1042,6 @@ typedef struct pjsua_config */ pj_str_t stun_host; - /** - * Specify STUN relay server to be used. - */ - pj_str_t stun_relay_host; - /** * Support for adding and parsing NAT type in the SDP to assist * troubleshooting. The valid values are: @@ -3886,9 +3881,33 @@ struct pjsua_media_config pj_bool_t enable_ice; /** - * Enable ICE media relay. + * Disable ICE host candidates. + */ + pj_bool_t ice_no_host_cands; + + /** + * Enable TURN relay candidate in ICE. + */ + pj_bool_t enable_turn; + + /** + * Specify TURN domain name or host name, in in "DOMAIN:PORT" or + * "HOST:PORT" format. + */ + pj_str_t turn_server; + + /** + * Specify the connection type to be used to the TURN server. Valid + * values are PJ_TURN_TP_UDP or PJ_TURN_TP_TCP. + * + * Default: PJ_TURN_TP_UDP + */ + pj_turn_tp_type turn_conn_type; + + /** + * Specify the credential to authenticate with the TURN server. */ - pj_bool_t enable_relay; + pj_stun_auth_cred turn_auth_cred; }; diff --git a/pjsip/include/pjsua-lib/pjsua_internal.h b/pjsip/include/pjsua-lib/pjsua_internal.h index df981ed8..dfb9f84a 100644 --- a/pjsip/include/pjsua-lib/pjsua_internal.h +++ b/pjsip/include/pjsua-lib/pjsua_internal.h @@ -52,6 +52,7 @@ typedef struct pjsua_call pjsip_evsub *xfer_sub; /**< Xfer server subscription, if this call was triggered by xfer. */ pjmedia_transport *med_tp; /**< Current media transport. */ + pj_status_t med_tp_st; /**< Media transport status. */ pjmedia_transport *med_orig; /**< Original media transport */ pj_timer_entry refresh_tm;/**< Timer to send re-INVITE. */ pj_timer_entry hangup_tm; /**< Timer to hangup call. */ diff --git a/pjsip/src/pjsua-lib/pjsua_core.c b/pjsip/src/pjsua-lib/pjsua_core.c index 82dc4d65..a50ff433 100644 --- a/pjsip/src/pjsua-lib/pjsua_core.c +++ b/pjsip/src/pjsua-lib/pjsua_core.c @@ -113,7 +113,6 @@ PJ_DEF(void) pjsua_config_dup(pj_pool_t *pool, pj_strdup_with_null(pool, &dst->user_agent, &src->user_agent); pj_strdup_with_null(pool, &dst->stun_domain, &src->stun_domain); pj_strdup_with_null(pool, &dst->stun_host, &src->stun_host); - pj_strdup_with_null(pool, &dst->stun_relay_host, &src->stun_relay_host); } PJ_DEF(void) pjsua_msg_data_init(pjsua_msg_data *msg_data) @@ -172,6 +171,8 @@ PJ_DEF(void) pjsua_media_config_default(pjsua_media_config *cfg) cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE; cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN; cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1; + + cfg->turn_conn_type = PJ_TURN_TP_UDP; } diff --git a/pjsip/src/pjsua-lib/pjsua_media.c b/pjsip/src/pjsua-lib/pjsua_media.c index 90b39460..a43ce4cb 100644 --- a/pjsip/src/pjsua-lib/pjsua_media.c +++ b/pjsip/src/pjsua-lib/pjsua_media.c @@ -36,6 +36,15 @@ static pj_uint16_t next_rtp_port; static void close_snd_dev(void); +static void pjsua_media_config_dup(pj_pool_t *pool, + pjsua_media_config *dst, + const pjsua_media_config *src) +{ + pj_memcpy(dst, src, sizeof(*src)); + pj_strdup(pool, &dst->turn_server, &src->turn_server); + pj_stun_auth_cred_dup(pool, &dst->turn_auth_cred, &src->turn_auth_cred); +} + /** * Init media subsystems. */ @@ -49,7 +58,7 @@ pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg) PJ_UNUSED_ARG(codec_id); /* Copy configuration */ - pj_memcpy(&pjsua_var.media_cfg, cfg, sizeof(*cfg)); + pjsua_media_config_dup(pjsua_var.pool, &pjsua_var.media_cfg, cfg); /* Normalize configuration */ if (pjsua_var.media_cfg.snd_clock_rate == 0) { @@ -581,46 +590,81 @@ on_error: /* This callback is called when ICE negotiation completes */ -static void on_ice_complete(pjmedia_transport *tp, pj_status_t result) +static void on_ice_complete(pjmedia_transport *tp, + pj_ice_strans_op op, + pj_status_t result) { - unsigned id, c; + unsigned id; pj_bool_t found = PJ_FALSE; - /* We're only interested with failure case */ - if (result == PJ_SUCCESS) - return; - /* Find call which has this media transport */ PJSUA_LOCK(); - for (id=0, c=0; idinv) { - ++c; - - if (call->med_tp == tp) { - call->media_st = PJSUA_CALL_MEDIA_ERROR; - call->media_dir = PJMEDIA_DIR_NONE; - found = PJ_TRUE; - break; - } + for (id=0; idptr = host_port->ptr; + host->slen = (str_port.ptr - host->ptr); + str_port.ptr++; + str_port.slen = host_port->slen - host->slen - 1; + iport = (int)pj_strtoul(&str_port); + if (iport < 1 || iport > 65535) + return PJ_EINVAL; + *port = (pj_uint16_t)iport; + } else { + *host = *host_port; + *port = 0; + } + + return PJ_SUCCESS; +} + /* Create ICE media transports (when ice is enabled) */ -static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg) +static pj_status_t create_ice_media_transports(void) { + char stunip[PJ_INET6_ADDRSTRLEN]; + pj_ice_strans_cfg ice_cfg; unsigned i; - pj_sockaddr_in addr; pj_status_t status; /* Make sure STUN server resolution has completed */ @@ -630,27 +674,57 @@ static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg) return status; } - pj_sockaddr_in_init(&addr, 0, (pj_uint16_t)cfg->port); + /* Create ICE stream transport configuration */ + pj_ice_strans_cfg_default(&ice_cfg); + pj_stun_config_init(&ice_cfg.stun_cfg, &pjsua_var.cp.factory, 0, + pjsip_endpt_get_ioqueue(pjsua_var.endpt), + pjsip_endpt_get_timer_heap(pjsua_var.endpt)); + + ice_cfg.af = pj_AF_INET(); + ice_cfg.resolver = pjsua_var.resolver; + + /* Configure STUN settings */ + if (pj_sockaddr_has_addr(&pjsua_var.stun_srv)) { + pj_sockaddr_print(&pjsua_var.stun_srv, stunip, sizeof(stunip), 0); + ice_cfg.stun.server = pj_str(stunip); + ice_cfg.stun.port = pj_sockaddr_get_port(&pjsua_var.stun_srv); + } + ice_cfg.stun.no_host_cands = pjsua_var.media_cfg.ice_no_host_cands; + + /* Configure TURN settings */ + if (pjsua_var.media_cfg.enable_turn) { + status = parse_host_port(&pjsua_var.media_cfg.turn_server, + &ice_cfg.turn.server, + &ice_cfg.turn.port); + if (status != PJ_SUCCESS || ice_cfg.turn.server.slen == 0) { + PJ_LOG(1,(THIS_FILE, "Invalid TURN server setting")); + return PJ_EINVAL; + } + if (ice_cfg.turn.port == 0) + ice_cfg.turn.port = 3479; + ice_cfg.turn.conn_type = pjsua_var.media_cfg.turn_conn_type; + pj_memcpy(&ice_cfg.turn.auth_cred, + &pjsua_var.media_cfg.turn_auth_cred, + sizeof(ice_cfg.turn.auth_cred)); + } /* Create each media transport */ for (i=0; imed_tp); + //see ticket #525 + //pjmedia_transport_media_stop(call->med_tp); if (call->conf_slot != PJSUA_INVALID_ID) { pjmedia_conf_remove_port(pjsua_var.mconf, call->conf_slot); -- cgit v1.2.3