diff --git a/src/include/switch_rtp.h b/src/include/switch_rtp.h index 241d7297f5..93ff687124 100644 --- a/src/include/switch_rtp.h +++ b/src/include/switch_rtp.h @@ -70,10 +70,26 @@ SWITCH_DECLARE(void) switch_rtp_init(switch_memory_pool *pool); SWITCH_DECLARE(switch_port_t) switch_rtp_request_port(void); /*! - \brief prepare a new RTP session handle - \param rx_ip the local address + \brief create a new RTP session handle + \param new_rtp_session a poiter to aim at the new session + \param payload the IANA payload number + \param flags flags to control behaviour + \param err a pointer to resolve error messages + \param pool a memory pool to use for the session + \return the new RTP session or NULL on failure +*/ +SWITCH_DECLARE(switch_status)switch_rtp_create(switch_rtp **new_rtp_session, + int payload, + switch_rtp_flag_t flags, + const char **err, + switch_memory_pool *pool); + + +/*! + \brief prepare a new RTP session handle and fully initilize it + \param rx_host the local address \param rx_port the local port - \param tx_ip the remote address + \param tx_host the remote address \param tx_port the remote port \param payload the IANA payload number \param flags flags to control behaviour @@ -81,14 +97,35 @@ SWITCH_DECLARE(switch_port_t) switch_rtp_request_port(void); \param pool a memory pool to use for the session \return the new RTP session or NULL on failure */ -SWITCH_DECLARE(switch_rtp *)switch_rtp_new(char *rx_ip, - switch_port_t rx_port, - char *tx_ip, - switch_port_t tx_port, - int payload, - switch_rtp_flag_t flags, - const char **err, - switch_memory_pool *pool); +SWITCH_DECLARE(switch_rtp *)switch_rtp_new(char *rx_host, + switch_port_t rx_port, + char *tx_host, + switch_port_t tx_port, + int payload, + switch_rtp_flag_t flags, + const char **err, + switch_memory_pool *pool); + + +/*! + \brief Assign a remote address to the RTP session + \param rtp_session an RTP session to assign the remote address to + \param host the ip or fqhn of the remote address + \param port the remote port + \param err pointer for error messages +*/ +SWITCH_DECLARE(switch_status) switch_rtp_set_remote_address(switch_rtp *rtp_session, char *host, switch_port_t port, const char **err); + +/*! + \brief Assign a local address to the RTP session + \param rtp_session an RTP session to assign the local address to + \param host the ip or fqhn of the local address + \param port the local port + \param err pointer for error messages + \note this call also binds the RTP session's socket to the new address +*/ +SWITCH_DECLARE(switch_status) switch_rtp_set_local_address(switch_rtp *rtp_session, char *host, switch_port_t port, const char **err); + /*! \brief Kill the socket on an existing RTP session \param rtp_session an RTP session to kill the socket of @@ -114,13 +151,6 @@ SWITCH_DECLARE(switch_status) switch_rtp_activate_ice(switch_rtp *rtp_session, c */ SWITCH_DECLARE(switch_socket_t *)switch_rtp_get_rtp_socket(switch_rtp *rtp_session); -/*! - \brief Activate a given RTP session - \param rtp_session the RTP session to activate - \return 0 -*/ -SWITCH_DECLARE(uint32_t) switch_rtp_start(switch_rtp *rtp_session); - /*! \brief Set a callback function to execute when an invalid RTP packet is encountered \param rtp_session the RTP session diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 77a5235745..7cae7d1efd 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -182,6 +182,7 @@ typedef enum { SWITCH_STATUS_GENERR - A general Error SWITCH_STATUS_INUSE - An indication that requested resource is in use SWITCH_STATUS_BREAK - A non-fatal break of an operation + SWITCH_STATUS_SOCKERR - A socket error */ typedef enum { @@ -197,6 +198,7 @@ typedef enum { SWITCH_STATUS_GENERR, SWITCH_STATUS_INUSE, SWITCH_STATUS_BREAK, + SWITCH_STATUS_SOCKERR } switch_status; /*! diff --git a/src/mod/endpoints/mod_dingaling/mod_dingaling.c b/src/mod/endpoints/mod_dingaling/mod_dingaling.c index 5d7b3d9686..4d0ad53e86 100644 --- a/src/mod/endpoints/mod_dingaling/mod_dingaling.c +++ b/src/mod/endpoints/mod_dingaling/mod_dingaling.c @@ -1278,7 +1278,6 @@ static ldl_status handle_signalling(ldl_handle_t *handle, ldl_session_t *dlsessi } switch_set_flag(tech_pvt, TFLAG_RTP_READY); switch_rtp_activate_ice(tech_pvt->rtp_session, tech_pvt->remote_user, tech_pvt->local_user); - switch_rtp_start(tech_pvt->rtp_session); } return LDL_STATUS_SUCCESS; diff --git a/src/mod/endpoints/mod_exosip/mod_exosip.c b/src/mod/endpoints/mod_exosip/mod_exosip.c index 2e22469160..9e06309e78 100644 --- a/src/mod/endpoints/mod_exosip/mod_exosip.c +++ b/src/mod/endpoints/mod_exosip/mod_exosip.c @@ -480,7 +480,6 @@ static void activate_rtp(struct private_object *tech_pvt) if (tech_pvt->rtp_session) { tech_pvt->ssrc = switch_rtp_get_ssrc(tech_pvt->rtp_session); - switch_rtp_start(tech_pvt->rtp_session); switch_set_flag(tech_pvt, TFLAG_RTP); } else { switch_channel *channel = switch_core_session_get_channel(tech_pvt->session); diff --git a/src/switch_rtp.c b/src/switch_rtp.c index 64576fe10f..72796476fb 100644 --- a/src/switch_rtp.c +++ b/src/switch_rtp.c @@ -193,52 +193,65 @@ SWITCH_DECLARE(switch_port_t) switch_rtp_request_port(void) return port; } -SWITCH_DECLARE(switch_rtp *)switch_rtp_new(char *rx_ip, - switch_port_t rx_port, - char *tx_ip, - switch_port_t tx_port, - int payload, - switch_rtp_flag_t flags, - const char **err, - switch_memory_pool *pool) + +SWITCH_DECLARE(switch_status) switch_rtp_set_local_address(switch_rtp *rtp_session, char *host, switch_port_t port, const char **err) +{ + *err = "Success"; + + if (switch_sockaddr_info_get(&rtp_session->local_addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) { + *err = "Local Address Error!"; + return SWITCH_STATUS_FALSE; + } + + if (rtp_session->sock) { + switch_socket_close(rtp_session->sock); + rtp_session->sock = NULL; + } + + if (switch_socket_create(&rtp_session->sock, AF_INET, SOCK_DGRAM, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) { + *err = "Socket Error!"; + return SWITCH_STATUS_SOCKERR; + } + + if (switch_socket_bind(rtp_session->sock, rtp_session->local_addr) != SWITCH_STATUS_SUCCESS) { + *err = "Bind Error!"; + return SWITCH_STATUS_FALSE; + } + + switch_set_flag(rtp_session, SWITCH_RTP_FLAG_IO); + return SWITCH_STATUS_SUCCESS; +} + +SWITCH_DECLARE(switch_status) switch_rtp_set_remote_address(switch_rtp *rtp_session, char *host, switch_port_t port, const char **err) +{ + *err = "Success"; + + if (switch_sockaddr_info_get(&rtp_session->remote_addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) { + *err = "Remote Address Error!"; + return SWITCH_STATUS_FALSE; + } + + return SWITCH_STATUS_SUCCESS; +} + +SWITCH_DECLARE(switch_status) switch_rtp_create(switch_rtp **new_rtp_session, + int payload, + switch_rtp_flag_t flags, + const char **err, + switch_memory_pool *pool) { - switch_socket_t *sock; switch_rtp *rtp_session = NULL; - switch_sockaddr_t *rx_addr; - switch_sockaddr_t *tx_addr; srtp_policy_t policy; char key[MAX_KEY_LEN]; uint32_t ssrc = rand() & 0xffff; - - if (switch_sockaddr_info_get(&rx_addr, rx_ip, SWITCH_UNSPEC, rx_port, 0, pool) != SWITCH_STATUS_SUCCESS) { - *err = "RX Address Error!"; - return NULL; - } - - if (switch_sockaddr_info_get(&tx_addr, tx_ip, SWITCH_UNSPEC, tx_port, 0, pool) != SWITCH_STATUS_SUCCESS) { - *err = "TX Address Error!"; - return NULL; - } - - if (switch_socket_create(&sock, AF_INET, SOCK_DGRAM, 0, pool) != SWITCH_STATUS_SUCCESS) { - *err = "Socket Error!"; - return NULL; - } - - if (switch_socket_bind(sock, rx_addr) != SWITCH_STATUS_SUCCESS) { - *err = "Bind Error!"; - return NULL; - } - + *new_rtp_session = NULL; + if (!(rtp_session = switch_core_alloc(pool, sizeof(*rtp_session)))) { *err = "Memory Error!"; - return NULL; + return SWITCH_STATUS_MEMERR; } - rtp_session->sock = sock; - rtp_session->local_addr = rx_addr; - rtp_session->remote_addr = tx_addr; rtp_session->pool = pool; switch_sockaddr_info_get(&rtp_session->from_addr, NULL, SWITCH_UNSPEC, 0, 0, rtp_session->pool); @@ -288,9 +301,38 @@ SWITCH_DECLARE(switch_rtp *)switch_rtp_new(char *rx_ip, srtp_create(&rtp_session->recv_ctx, &policy); srtp_create(&rtp_session->send_ctx, &policy); + *new_rtp_session = rtp_session; + + return SWITCH_STATUS_SUCCESS; +} + +SWITCH_DECLARE(switch_rtp *)switch_rtp_new(char *rx_host, + switch_port_t rx_port, + char *tx_host, + switch_port_t tx_port, + int payload, + switch_rtp_flag_t flags, + const char **err, + switch_memory_pool *pool) +{ + switch_rtp *rtp_session; + + if (switch_rtp_create(&rtp_session, payload, flags, err, pool) != SWITCH_STATUS_SUCCESS) { + return NULL; + } + + if (switch_rtp_set_remote_address(rtp_session, tx_host, tx_port, err) != SWITCH_STATUS_SUCCESS) { + return NULL; + } + + if (switch_rtp_set_local_address(rtp_session, rx_host, rx_port, err) != SWITCH_STATUS_SUCCESS) { + return NULL; + } + return rtp_session; } + SWITCH_DECLARE(switch_status) switch_rtp_activate_ice(switch_rtp *rtp_session, char *login, char *rlogin) { char ice_user[80]; @@ -461,12 +503,6 @@ SWITCH_DECLARE(int) switch_rtp_write_payload(switch_rtp *rtp_session, void *data return (int)bytes; } -SWITCH_DECLARE(uint32_t) switch_rtp_start(switch_rtp *rtp_session) -{ - switch_set_flag(rtp_session, SWITCH_RTP_FLAG_IO); - return 0; -} - SWITCH_DECLARE(uint32_t) switch_rtp_get_ssrc(switch_rtp *rtp_session) { return rtp_session->send_msg.header.ssrc;