diff options
author | Benny Prijono <bennylp@teluu.com> | 2008-06-06 14:51:48 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2008-06-06 14:51:48 +0000 |
commit | 93c46290dcf20a704b36e66208543ff2bc36e23b (patch) | |
tree | e9257512192783ec3163602dfd5c502d7bf85342 /pjsip | |
parent | 5ac557ce4cbeeca1747e14fbf2769d77d23c332d (diff) |
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
Diffstat (limited to 'pjsip')
-rw-r--r-- | pjsip/include/pjsua-lib/pjsua.h | 37 | ||||
-rw-r--r-- | pjsip/include/pjsua-lib/pjsua_internal.h | 1 | ||||
-rw-r--r-- | pjsip/src/pjsua-lib/pjsua_core.c | 3 | ||||
-rw-r--r-- | pjsip/src/pjsua-lib/pjsua_media.c | 194 |
4 files changed, 155 insertions, 80 deletions
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. @@ -1043,11 +1043,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: * - 0: no information will be added in SDP, and parsing is disabled. @@ -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; id<PJSUA_MAX_CALLS && c<pjsua_var.call_cnt; ++id) { - pjsua_call *call = &pjsua_var.calls[id]; - if (call->inv) { - ++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; id<pjsua_var.ua_cfg.max_calls; ++id) { + if (pjsua_var.calls[id].med_tp == tp || + pjsua_var.calls[id].med_orig == tp) + { + found = PJ_TRUE; + break; } } PJSUA_UNLOCK(); - if (found && pjsua_var.ua_cfg.cb.on_call_media_state) { - pjsua_var.ua_cfg.cb.on_call_media_state(id); + if (!found) + return; + + switch (op) { + case PJ_ICE_STRANS_OP_INIT: + pjsua_var.calls[id].med_tp_st = result; + break; + case PJ_ICE_STRANS_OP_NEGOTIATION: + if (result != PJ_SUCCESS) { + pjsua_var.calls[id].media_st = PJSUA_CALL_MEDIA_ERROR; + pjsua_var.calls[id].media_dir = PJMEDIA_DIR_NONE; + + if (pjsua_var.ua_cfg.cb.on_call_media_state) { + pjsua_var.ua_cfg.cb.on_call_media_state(id); + } + } + break; } } +/* Parse "HOST:PORT" format */ +static pj_status_t parse_host_port(const pj_str_t *host_port, + pj_str_t *host, pj_uint16_t *port) +{ + pj_str_t str_port; + + str_port.ptr = pj_strchr(host_port, ':'); + if (str_port.ptr != NULL) { + int iport; + + host->ptr = 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; i<pjsua_var.ua_cfg.max_calls; ++i) { - pj_ice_strans_comp comp; pjmedia_ice_cb ice_cb; - int next_port; char name[32]; -#if PJMEDIA_ADVERTISE_RTCP - enum { COMP_CNT=2 }; -#else - enum { COMP_CNT=1 }; -#endif + unsigned comp_cnt; pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb)); ice_cb.on_ice_complete = &on_ice_complete; - pj_ansi_snprintf(name, sizeof(name), "icetp%02d", i); - - status = pjmedia_ice_create(pjsua_var.med_endpt, name, COMP_CNT, - &pjsua_var.stun_cfg, &ice_cb, + pjsua_var.calls[i].med_tp_st = PJ_EPENDING; + + comp_cnt = 1; + if (PJMEDIA_ADVERTISE_RTCP) + ++comp_cnt; + + status = pjmedia_ice_create(pjsua_var.med_endpt, name, comp_cnt, + &ice_cfg, &ice_cb, &pjsua_var.calls[i].med_tp); if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Unable to create ICE media transport", @@ -658,6 +732,19 @@ static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg) goto on_error; } + /* Wait until transport is initialized, or time out */ + PJSUA_UNLOCK(); + while (pjsua_var.calls[i].med_tp_st == PJ_EPENDING) { + pjsua_handle_events(100); + } + PJSUA_LOCK(); + if (pjsua_var.calls[i].med_tp_st != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Error initializing ICE media transport", + pjsua_var.calls[i].med_tp_st); + status = pjsua_var.calls[i].med_tp_st; + goto on_error; + } + pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp, PJMEDIA_DIR_ENCODING, pjsua_var.media_cfg.tx_drop_pct); @@ -665,40 +752,6 @@ static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg) pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp, PJMEDIA_DIR_DECODING, pjsua_var.media_cfg.rx_drop_pct); - - status = pjmedia_ice_start_init(pjsua_var.calls[i].med_tp, 0, &addr, - &pjsua_var.stun_srv.ipv4, NULL); - if (status != PJ_SUCCESS) { - pjsua_perror(THIS_FILE, "Error starting ICE transport", - status); - goto on_error; - } - - pjmedia_ice_get_comp(pjsua_var.calls[i].med_tp, 1, &comp); - next_port = pj_ntohs(comp.local_addr.ipv4.sin_port); - next_port += 2; - addr.sin_port = pj_htons((pj_uint16_t)next_port); - } - - /* Wait until all ICE transports are ready */ - for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { - - /* Wait until interface status is PJ_SUCCESS */ - for (;;) { - status = pjmedia_ice_get_init_status(pjsua_var.calls[i].med_tp); - if (status == PJ_EPENDING) - pjsua_handle_events(100); - else - break; - } - - if (status != PJ_SUCCESS) { - pjsua_perror(THIS_FILE, - "Error adding STUN address to ICE media transport", - status); - goto on_error; - } - } return PJ_SUCCESS; @@ -744,7 +797,7 @@ PJ_DEF(pj_status_t) pjsua_media_transports_create( pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg); if (pjsua_var.media_cfg.enable_ice) { - status = create_ice_media_transports(&cfg); + status = create_ice_media_transports(); } else { status = create_udp_media_transports(&cfg); } @@ -896,7 +949,8 @@ static void stop_media_session(pjsua_call_id call_id) { pjsua_call *call = &pjsua_var.calls[call_id]; - pjmedia_transport_media_stop(call->med_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); |