2006-04-03 21:00:13 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
2007-04-07 03:07:43 +00:00
|
|
|
* Marcel Barbulescu <marcelbarbulescu@gmail.com>
|
2006-04-03 21:00:13 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* switch_rtp.c -- RTP
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <switch.h>
|
2007-06-20 09:02:30 +00:00
|
|
|
#include <switch_stun.h>
|
2006-04-03 21:00:13 +00:00
|
|
|
#undef PACKAGE_NAME
|
|
|
|
#undef PACKAGE_STRING
|
|
|
|
#undef PACKAGE_TARNAME
|
|
|
|
#undef PACKAGE_VERSION
|
|
|
|
#undef PACKAGE_BUGREPORT
|
2006-10-02 20:49:26 +00:00
|
|
|
#undef VERSION
|
|
|
|
#undef PACKAGE
|
2006-12-15 15:33:43 +00:00
|
|
|
#undef inline
|
2006-04-03 21:00:13 +00:00
|
|
|
#include <datatypes.h>
|
|
|
|
#include <srtp.h>
|
2008-01-16 06:01:53 +00:00
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
#define READ_INC(rtp_session) switch_mutex_lock(rtp_session->read_mutex); rtp_session->reading++
|
|
|
|
#define READ_DEC(rtp_session) switch_mutex_unlock(rtp_session->read_mutex); rtp_session->reading--
|
|
|
|
#define WRITE_INC(rtp_session) switch_mutex_lock(rtp_session->write_mutex); rtp_session->writing++
|
|
|
|
#define WRITE_DEC(rtp_session) switch_mutex_unlock(rtp_session->write_mutex); rtp_session->writing--
|
2008-01-16 06:01:53 +00:00
|
|
|
|
2007-05-04 23:29:35 +00:00
|
|
|
#include "stfu.h"
|
2008-01-16 06:01:53 +00:00
|
|
|
|
2006-04-03 21:00:13 +00:00
|
|
|
#define rtp_header_len 12
|
2006-04-05 20:17:22 +00:00
|
|
|
#define RTP_START_PORT 16384
|
|
|
|
#define RTP_END_PORT 32768
|
2006-04-10 16:11:24 +00:00
|
|
|
#define MASTER_KEY_LEN 30
|
2006-05-05 13:35:33 +00:00
|
|
|
#define RTP_MAGIC_NUMBER 42
|
2008-01-16 06:01:53 +00:00
|
|
|
#define MAX_SRTP_ERRS 10
|
2006-04-05 20:17:22 +00:00
|
|
|
|
2007-04-07 03:07:43 +00:00
|
|
|
static switch_port_t START_PORT = RTP_START_PORT;
|
|
|
|
static switch_port_t END_PORT = RTP_END_PORT;
|
2006-04-05 20:17:22 +00:00
|
|
|
static switch_port_t NEXT_PORT = RTP_START_PORT;
|
|
|
|
static switch_mutex_t *port_lock = NULL;
|
2006-04-03 21:00:13 +00:00
|
|
|
|
|
|
|
typedef srtp_hdr_t rtp_hdr_t;
|
|
|
|
|
2006-05-04 20:19:23 +00:00
|
|
|
#ifdef _MSC_VER
|
2006-05-05 13:35:33 +00:00
|
|
|
#pragma pack(4)
|
2006-05-04 20:19:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma pack()
|
|
|
|
#endif
|
|
|
|
|
2007-12-11 19:15:02 +00:00
|
|
|
static switch_hash_t *alloc_hash = NULL;
|
2006-05-04 20:19:23 +00:00
|
|
|
|
2006-04-03 21:00:13 +00:00
|
|
|
typedef struct {
|
2007-03-29 22:31:56 +00:00
|
|
|
srtp_hdr_t header;
|
|
|
|
char body[SWITCH_RTP_MAX_BUF_LEN];
|
2006-04-03 21:00:13 +00:00
|
|
|
} rtp_msg_t;
|
|
|
|
|
2006-04-21 22:31:08 +00:00
|
|
|
struct switch_rtp_vad_data {
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_core_session_t *session;
|
|
|
|
switch_codec_t vad_codec;
|
|
|
|
switch_codec_t *read_codec;
|
2006-04-21 22:31:08 +00:00
|
|
|
uint32_t bg_level;
|
|
|
|
uint32_t bg_count;
|
|
|
|
uint32_t bg_len;
|
|
|
|
uint32_t diff_level;
|
|
|
|
uint8_t hangunder;
|
|
|
|
uint8_t hangunder_hits;
|
|
|
|
uint8_t hangover;
|
|
|
|
uint8_t hangover_hits;
|
|
|
|
uint8_t cng_freq;
|
|
|
|
uint8_t cng_count;
|
|
|
|
switch_vad_flag_t flags;
|
|
|
|
uint32_t ts;
|
|
|
|
uint8_t start;
|
|
|
|
uint8_t start_count;
|
|
|
|
uint8_t scan_freq;
|
|
|
|
time_t next_scan;
|
|
|
|
};
|
|
|
|
|
2006-05-04 17:51:53 +00:00
|
|
|
struct switch_rtp_rfc2833_data {
|
|
|
|
switch_queue_t *dtmf_queue;
|
|
|
|
char out_digit;
|
|
|
|
unsigned char out_digit_packet[4];
|
|
|
|
unsigned int out_digit_sofar;
|
2008-01-11 21:59:17 +00:00
|
|
|
unsigned int out_digit_sub_sofar;
|
2006-05-04 17:51:53 +00:00
|
|
|
unsigned int out_digit_dur;
|
2007-03-22 20:55:46 +00:00
|
|
|
uint16_t in_digit_seq;
|
2008-01-11 21:59:17 +00:00
|
|
|
uint32_t in_digit_ts;
|
2008-01-11 01:27:24 +00:00
|
|
|
uint32_t timestamp_dtmf;
|
2008-01-11 21:59:17 +00:00
|
|
|
uint16_t last_duration;
|
|
|
|
uint32_t flip;
|
|
|
|
char first_digit;
|
2006-05-04 17:51:53 +00:00
|
|
|
char last_digit;
|
2007-12-22 00:32:20 +00:00
|
|
|
switch_queue_t *dtmf_inqueue;
|
2006-05-04 17:51:53 +00:00
|
|
|
switch_mutex_t *dtmf_mutex;
|
|
|
|
};
|
|
|
|
|
2006-04-03 21:00:13 +00:00
|
|
|
struct switch_rtp {
|
2006-04-04 02:28:11 +00:00
|
|
|
switch_socket_t *sock;
|
2006-04-03 21:00:13 +00:00
|
|
|
|
2006-04-04 02:28:11 +00:00
|
|
|
switch_sockaddr_t *local_addr;
|
2006-04-03 21:00:13 +00:00
|
|
|
rtp_msg_t send_msg;
|
|
|
|
|
2006-04-04 02:28:11 +00:00
|
|
|
switch_sockaddr_t *remote_addr;
|
2006-04-03 21:00:13 +00:00
|
|
|
rtp_msg_t recv_msg;
|
2008-01-16 06:01:53 +00:00
|
|
|
|
2007-05-16 17:47:24 +00:00
|
|
|
uint32_t autoadj_window;
|
|
|
|
uint32_t autoadj_tally;
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2008-01-16 06:01:53 +00:00
|
|
|
srtp_ctx_t *send_ctx;
|
|
|
|
srtp_ctx_t *recv_ctx;
|
|
|
|
srtp_policy_t send_policy;
|
|
|
|
srtp_policy_t recv_policy;
|
|
|
|
uint32_t srtp_errs;
|
|
|
|
|
2006-04-04 04:15:39 +00:00
|
|
|
uint16_t seq;
|
2008-01-12 04:56:20 +00:00
|
|
|
uint32_t ssrc;
|
2007-07-06 16:19:40 +00:00
|
|
|
uint8_t sending_dtmf;
|
2006-04-18 16:50:34 +00:00
|
|
|
switch_payload_t payload;
|
2006-05-04 20:19:23 +00:00
|
|
|
switch_payload_t rpayload;
|
2006-04-29 23:43:28 +00:00
|
|
|
switch_rtp_invalid_handler_t invalid_handler;
|
2006-04-03 21:00:13 +00:00
|
|
|
void *private_data;
|
|
|
|
uint32_t ts;
|
2007-02-12 18:22:30 +00:00
|
|
|
uint32_t last_write_ts;
|
2007-03-17 01:52:05 +00:00
|
|
|
uint32_t last_write_samplecount;
|
2008-01-14 21:51:31 +00:00
|
|
|
uint32_t next_write_samplecount;
|
2006-04-03 21:00:13 +00:00
|
|
|
uint32_t flags;
|
2006-04-29 01:00:52 +00:00
|
|
|
switch_memory_pool_t *pool;
|
2006-04-04 02:28:11 +00:00
|
|
|
switch_sockaddr_t *from_addr;
|
2007-12-11 19:15:02 +00:00
|
|
|
char *rx_host;
|
|
|
|
switch_port_t rx_port;
|
2006-04-04 17:22:06 +00:00
|
|
|
char *ice_user;
|
|
|
|
char *user_ice;
|
2007-03-29 22:31:56 +00:00
|
|
|
char *timer_name;
|
2006-04-04 17:22:06 +00:00
|
|
|
switch_time_t last_stun;
|
2007-03-09 16:30:34 +00:00
|
|
|
uint32_t samples_per_interval;
|
|
|
|
uint32_t conf_samples_per_interval;
|
|
|
|
uint32_t rsamples_per_interval;
|
2006-04-07 17:32:14 +00:00
|
|
|
uint32_t ms_per_packet;
|
2006-04-17 18:25:43 +00:00
|
|
|
uint32_t remote_port;
|
2006-04-04 17:22:06 +00:00
|
|
|
uint8_t stuncount;
|
2006-04-21 22:31:08 +00:00
|
|
|
struct switch_rtp_vad_data vad_data;
|
2006-05-04 17:51:53 +00:00
|
|
|
struct switch_rtp_rfc2833_data dtmf_data;
|
2006-05-27 00:24:32 +00:00
|
|
|
switch_payload_t te;
|
2007-03-07 18:34:22 +00:00
|
|
|
switch_payload_t cng_pt;
|
2006-08-28 23:05:26 +00:00
|
|
|
switch_mutex_t *flag_mutex;
|
2008-01-18 18:14:53 +00:00
|
|
|
switch_mutex_t *read_mutex;
|
|
|
|
switch_mutex_t *write_mutex;
|
2006-09-12 22:23:45 +00:00
|
|
|
switch_timer_t timer;
|
2006-09-14 00:15:03 +00:00
|
|
|
uint8_t ready;
|
2007-03-17 01:52:05 +00:00
|
|
|
uint8_t cn;
|
2006-09-14 00:15:03 +00:00
|
|
|
switch_time_t last_time;
|
2007-05-04 23:29:35 +00:00
|
|
|
stfu_instance_t *jb;
|
2007-10-31 02:12:30 +00:00
|
|
|
uint32_t max_missed_packets;
|
|
|
|
uint32_t missed_count;
|
2007-12-18 17:33:29 +00:00
|
|
|
rtp_msg_t write_msg;
|
2008-01-16 06:01:53 +00:00
|
|
|
switch_rtp_crypto_key_t *crypto_keys[SWITCH_RTP_CRYPTO_MAX];
|
2008-01-18 18:14:53 +00:00
|
|
|
int reading;
|
|
|
|
int writing;
|
2006-04-03 21:00:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int global_init = 0;
|
2008-01-11 04:23:09 +00:00
|
|
|
static int rtp_common_write(switch_rtp_t *rtp_session,
|
|
|
|
rtp_msg_t *send_msg,
|
|
|
|
void *data,
|
|
|
|
uint32_t datalen,
|
|
|
|
switch_payload_t payload,
|
|
|
|
uint32_t timestamp,
|
|
|
|
switch_frame_flag_t *flags);
|
|
|
|
|
2006-04-03 21:00:13 +00:00
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
static switch_status_t ice_out(switch_rtp_t *rtp_session)
|
2006-04-04 17:22:06 +00:00
|
|
|
{
|
2007-12-11 03:57:15 +00:00
|
|
|
uint8_t buf[256] = { 0 };
|
|
|
|
switch_stun_packet_t *packet;
|
|
|
|
unsigned int elapsed;
|
|
|
|
switch_size_t bytes;
|
2008-01-18 18:14:53 +00:00
|
|
|
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
2006-04-04 17:22:06 +00:00
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
WRITE_INC(rtp_session);
|
|
|
|
|
2007-12-11 19:23:57 +00:00
|
|
|
switch_assert(rtp_session != NULL);
|
|
|
|
switch_assert(rtp_session->ice_user != NULL);
|
2006-04-04 17:22:06 +00:00
|
|
|
|
2007-12-11 03:57:15 +00:00
|
|
|
if (rtp_session->stuncount != 0) {
|
|
|
|
rtp_session->stuncount--;
|
2008-01-18 18:14:53 +00:00
|
|
|
goto end;
|
2007-12-11 03:57:15 +00:00
|
|
|
}
|
2006-04-04 17:22:06 +00:00
|
|
|
|
2007-12-11 03:57:15 +00:00
|
|
|
if (rtp_session->last_stun) {
|
|
|
|
elapsed = (unsigned int) ((switch_time_now() - rtp_session->last_stun) / 1000);
|
2006-04-04 17:22:06 +00:00
|
|
|
|
2007-12-11 03:57:15 +00:00
|
|
|
if (elapsed > 30000) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No stun for a long time (PUNT!)\n");
|
2008-01-18 18:14:53 +00:00
|
|
|
status = SWITCH_STATUS_FALSE;
|
|
|
|
goto end;
|
2006-04-04 17:22:06 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-11 03:57:15 +00:00
|
|
|
|
|
|
|
packet = switch_stun_packet_build_header(SWITCH_STUN_BINDING_REQUEST, NULL, buf);
|
|
|
|
switch_stun_packet_attribute_add_username(packet, rtp_session->ice_user, 32);
|
|
|
|
bytes = switch_stun_packet_length(packet);
|
|
|
|
switch_socket_sendto(rtp_session->sock, rtp_session->remote_addr, 0, (void *) packet, &bytes);
|
|
|
|
rtp_session->stuncount = 25;
|
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
end:
|
|
|
|
WRITE_DEC(rtp_session);
|
|
|
|
|
2006-04-04 17:22:06 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 01:00:52 +00:00
|
|
|
static void handle_ice(switch_rtp_t *rtp_session, void *data, switch_size_t len)
|
2006-04-04 17:22:06 +00:00
|
|
|
{
|
|
|
|
switch_stun_packet_t *packet;
|
|
|
|
switch_stun_packet_attribute_t *attr;
|
2007-03-29 22:31:56 +00:00
|
|
|
char username[33] = { 0 };
|
|
|
|
unsigned char buf[512] = { 0 };
|
2007-12-17 23:43:15 +00:00
|
|
|
switch_size_t cpylen = len;
|
2008-01-18 18:14:53 +00:00
|
|
|
|
|
|
|
READ_INC(rtp_session);
|
|
|
|
WRITE_INC(rtp_session);
|
2007-12-17 23:43:15 +00:00
|
|
|
|
|
|
|
if (cpylen > 512) {
|
|
|
|
cpylen = 512;
|
|
|
|
}
|
2006-04-04 17:22:06 +00:00
|
|
|
|
2007-12-17 23:43:15 +00:00
|
|
|
memcpy(buf, data, cpylen);
|
2006-04-04 17:22:06 +00:00
|
|
|
packet = switch_stun_packet_parse(buf, sizeof(buf));
|
|
|
|
rtp_session->last_stun = switch_time_now();
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-04-04 17:22:06 +00:00
|
|
|
switch_stun_packet_first_attribute(packet, attr);
|
|
|
|
|
|
|
|
do {
|
2007-03-29 22:31:56 +00:00
|
|
|
switch (attr->type) {
|
2006-04-04 17:22:06 +00:00
|
|
|
case SWITCH_STUN_ATTR_MAPPED_ADDRESS:
|
|
|
|
if (attr->type) {
|
|
|
|
char ip[16];
|
|
|
|
uint16_t port;
|
|
|
|
switch_stun_packet_attribute_get_mapped_address(attr, ip, &port);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SWITCH_STUN_ATTR_USERNAME:
|
2007-03-29 22:31:56 +00:00
|
|
|
if (attr->type) {
|
2006-04-04 17:22:06 +00:00
|
|
|
switch_stun_packet_attribute_get_username(attr, username, 32);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (switch_stun_packet_next_attribute(attr));
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
if ((packet->header.type == SWITCH_STUN_BINDING_REQUEST) && !strcmp(rtp_session->user_ice, username)) {
|
2007-12-17 23:19:10 +00:00
|
|
|
uint8_t stunbuf[512];
|
2006-04-04 17:22:06 +00:00
|
|
|
switch_stun_packet_t *rpacket;
|
2007-12-06 22:26:37 +00:00
|
|
|
const char *remote_ip;
|
2006-04-04 17:22:06 +00:00
|
|
|
switch_size_t bytes;
|
2006-11-15 03:17:28 +00:00
|
|
|
char ipbuf[25];
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-12-17 23:19:10 +00:00
|
|
|
memset(stunbuf, 0, sizeof(stunbuf));
|
|
|
|
rpacket = switch_stun_packet_build_header(SWITCH_STUN_BINDING_RESPONSE, packet->header.id, stunbuf);
|
2006-04-04 17:22:06 +00:00
|
|
|
switch_stun_packet_attribute_add_username(rpacket, username, 32);
|
2006-11-15 03:17:28 +00:00
|
|
|
remote_ip = switch_get_addr(ipbuf, sizeof(ipbuf), rtp_session->from_addr);
|
2007-12-06 22:26:37 +00:00
|
|
|
switch_stun_packet_attribute_add_binded_address(rpacket, (char *)remote_ip, switch_sockaddr_get_port(rtp_session->from_addr));
|
2006-04-04 17:22:06 +00:00
|
|
|
bytes = switch_stun_packet_length(rpacket);
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_socket_sendto(rtp_session->sock, rtp_session->from_addr, 0, (void *) rpacket, &bytes);
|
2006-04-04 17:22:06 +00:00
|
|
|
}
|
2008-01-18 18:14:53 +00:00
|
|
|
|
|
|
|
READ_DEC(rtp_session);
|
|
|
|
WRITE_DEC(rtp_session);
|
2006-04-04 17:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_init(switch_memory_pool_t *pool)
|
2006-04-03 21:00:13 +00:00
|
|
|
{
|
|
|
|
if (global_init) {
|
|
|
|
return;
|
|
|
|
}
|
2007-12-11 19:15:02 +00:00
|
|
|
switch_core_hash_init(&alloc_hash, pool);
|
2007-03-29 22:31:56 +00:00
|
|
|
srtp_init();
|
|
|
|
switch_mutex_init(&port_lock, SWITCH_MUTEX_NESTED, pool);
|
|
|
|
global_init = 1;
|
2006-04-03 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
2008-01-16 06:01:53 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_get_random(void *buf, uint32_t len)
|
|
|
|
{
|
|
|
|
crypto_get_random(buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-11 19:15:02 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_shutdown(void)
|
|
|
|
{
|
|
|
|
switch_core_hash_destroy(&alloc_hash);
|
|
|
|
}
|
|
|
|
|
2007-04-07 03:07:43 +00:00
|
|
|
SWITCH_DECLARE(switch_port_t) switch_rtp_set_start_port(switch_port_t port)
|
|
|
|
{
|
|
|
|
if (port) {
|
|
|
|
if (port_lock) {
|
|
|
|
switch_mutex_lock(port_lock);
|
|
|
|
}
|
|
|
|
if (NEXT_PORT == START_PORT) {
|
|
|
|
NEXT_PORT = port;
|
|
|
|
}
|
|
|
|
START_PORT = port;
|
|
|
|
if (NEXT_PORT < START_PORT) {
|
|
|
|
NEXT_PORT = START_PORT;
|
|
|
|
}
|
|
|
|
if (port_lock) {
|
|
|
|
switch_mutex_unlock(port_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return START_PORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_port_t) switch_rtp_set_end_port(switch_port_t port)
|
|
|
|
{
|
|
|
|
if (port) {
|
|
|
|
if (port_lock) {
|
|
|
|
switch_mutex_lock(port_lock);
|
|
|
|
}
|
|
|
|
END_PORT = port;
|
|
|
|
if (NEXT_PORT > END_PORT) {
|
|
|
|
NEXT_PORT = START_PORT;
|
|
|
|
}
|
|
|
|
if (port_lock) {
|
|
|
|
switch_mutex_unlock(port_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return END_PORT;
|
|
|
|
}
|
|
|
|
|
2007-12-20 15:41:45 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_release_port(const char *ip, switch_port_t port)
|
2006-04-05 20:17:22 +00:00
|
|
|
{
|
2007-12-11 19:15:02 +00:00
|
|
|
switch_core_port_allocator_t *alloc = NULL;
|
|
|
|
|
2007-12-20 15:41:45 +00:00
|
|
|
if (!ip) {
|
2007-12-11 19:15:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_mutex_lock(port_lock);
|
2007-12-20 15:41:45 +00:00
|
|
|
if ((alloc = switch_core_hash_find(alloc_hash, ip))) {
|
2007-12-13 15:10:55 +00:00
|
|
|
switch_core_port_allocator_free_port(alloc, port);
|
2007-12-11 19:15:02 +00:00
|
|
|
}
|
|
|
|
switch_mutex_unlock(port_lock);
|
|
|
|
|
|
|
|
}
|
2006-04-05 20:17:22 +00:00
|
|
|
|
2007-12-11 19:15:02 +00:00
|
|
|
SWITCH_DECLARE(switch_port_t) switch_rtp_request_port(const char *ip)
|
|
|
|
{
|
|
|
|
switch_port_t port = 0;
|
|
|
|
switch_core_port_allocator_t *alloc = NULL;
|
|
|
|
|
2006-04-05 20:17:22 +00:00
|
|
|
switch_mutex_lock(port_lock);
|
2007-12-11 19:15:02 +00:00
|
|
|
alloc = switch_core_hash_find(alloc_hash, ip);
|
|
|
|
if (!alloc) {
|
|
|
|
if (switch_core_port_allocator_new(START_PORT, END_PORT, SPF_EVEN, &alloc) != SWITCH_STATUS_SUCCESS) {
|
2007-12-13 17:25:33 +00:00
|
|
|
abort();
|
2007-12-11 19:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch_core_hash_insert(alloc_hash, ip, alloc);
|
2006-04-05 20:17:22 +00:00
|
|
|
}
|
2007-12-11 19:15:02 +00:00
|
|
|
|
|
|
|
if (switch_core_port_allocator_request_port(alloc, &port) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
port = 0;
|
|
|
|
}
|
|
|
|
|
2006-04-05 20:17:22 +00:00
|
|
|
switch_mutex_unlock(port_lock);
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2007-05-12 14:48:14 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rtp_set_local_address(switch_rtp_t *rtp_session, const char *host, switch_port_t port, const char **err)
|
2006-04-06 20:56:29 +00:00
|
|
|
{
|
2007-02-17 22:13:19 +00:00
|
|
|
switch_socket_t *new_sock = NULL, *old_sock = NULL;
|
|
|
|
switch_status_t status = SWITCH_STATUS_FALSE;
|
2007-12-17 18:24:26 +00:00
|
|
|
#ifndef WIN32
|
2007-04-20 00:29:18 +00:00
|
|
|
char o[5] = "TEST", i[5] = "";
|
2007-12-04 21:55:48 +00:00
|
|
|
switch_size_t len, ilen = 0;
|
|
|
|
int x;
|
2007-12-17 18:24:26 +00:00
|
|
|
#endif
|
2007-12-04 21:55:48 +00:00
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
WRITE_INC(rtp_session);
|
|
|
|
READ_INC(rtp_session);
|
|
|
|
|
2007-04-20 20:53:11 +00:00
|
|
|
*err = NULL;
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
if (switch_sockaddr_info_get(&rtp_session->local_addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
|
2006-04-06 20:56:29 +00:00
|
|
|
*err = "Local Address Error!";
|
2007-02-17 22:13:19 +00:00
|
|
|
goto done;
|
2006-04-04 02:28:11 +00:00
|
|
|
}
|
|
|
|
|
2006-04-06 20:56:29 +00:00
|
|
|
if (rtp_session->sock) {
|
2006-08-30 19:46:43 +00:00
|
|
|
switch_rtp_kill_socket(rtp_session);
|
2006-04-03 21:00:13 +00:00
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-02-17 22:13:19 +00:00
|
|
|
if (switch_socket_create(&new_sock, AF_INET, SOCK_DGRAM, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
|
2006-04-04 02:28:11 +00:00
|
|
|
*err = "Socket Error!";
|
2007-02-17 22:13:19 +00:00
|
|
|
goto done;
|
2006-04-04 02:28:11 +00:00
|
|
|
}
|
2006-04-03 21:00:13 +00:00
|
|
|
|
2007-02-17 22:13:19 +00:00
|
|
|
if (switch_socket_opt_set(new_sock, SWITCH_SO_REUSEADDR, 1) != SWITCH_STATUS_SUCCESS) {
|
2006-10-06 22:39:49 +00:00
|
|
|
*err = "Socket Error!";
|
2007-02-17 22:13:19 +00:00
|
|
|
goto done;
|
2006-10-06 22:39:49 +00:00
|
|
|
}
|
|
|
|
|
2007-02-17 22:13:19 +00:00
|
|
|
if (switch_socket_bind(new_sock, rtp_session->local_addr) != SWITCH_STATUS_SUCCESS) {
|
2006-04-04 02:28:11 +00:00
|
|
|
*err = "Bind Error!";
|
2007-02-17 22:13:19 +00:00
|
|
|
goto done;
|
2006-04-04 02:28:11 +00:00
|
|
|
}
|
2006-04-03 21:00:13 +00:00
|
|
|
|
2007-12-17 18:24:26 +00:00
|
|
|
#ifndef WIN32
|
2007-04-20 00:29:18 +00:00
|
|
|
len = sizeof(i);
|
2007-12-04 21:55:48 +00:00
|
|
|
switch_socket_opt_set(new_sock, SWITCH_SO_NONBLOCK, TRUE);
|
|
|
|
|
2007-04-20 00:29:18 +00:00
|
|
|
switch_socket_sendto(new_sock, rtp_session->local_addr, 0, (void *) o, &len);
|
|
|
|
|
2007-12-04 21:55:48 +00:00
|
|
|
x = 0;
|
|
|
|
while(!ilen) {
|
2007-12-04 23:06:12 +00:00
|
|
|
switch_status_t status;
|
2007-12-04 21:55:48 +00:00
|
|
|
ilen = len;
|
2007-12-04 23:06:12 +00:00
|
|
|
status = switch_socket_recvfrom(rtp_session->from_addr, new_sock, 0, (void *) i, &ilen);
|
2007-12-04 21:55:48 +00:00
|
|
|
|
|
|
|
if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++x > 500) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch_yield(1000);
|
|
|
|
}
|
|
|
|
switch_socket_opt_set(new_sock, SWITCH_SO_NONBLOCK, FALSE);
|
|
|
|
|
|
|
|
if (!ilen) {
|
|
|
|
*err = "Send myself a packet failed!";
|
2007-04-20 00:29:18 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2007-12-17 18:24:26 +00:00
|
|
|
#endif
|
|
|
|
|
2007-02-17 22:13:19 +00:00
|
|
|
old_sock = rtp_session->sock;
|
|
|
|
rtp_session->sock = new_sock;
|
|
|
|
new_sock = NULL;
|
|
|
|
|
2007-04-19 21:40:50 +00:00
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER) || switch_test_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK)) {
|
2007-03-09 20:44:13 +00:00
|
|
|
switch_socket_opt_set(rtp_session->sock, SWITCH_SO_NONBLOCK, TRUE);
|
2006-08-28 23:05:26 +00:00
|
|
|
switch_set_flag_locked(rtp_session, SWITCH_RTP_FLAG_NOBLOCK);
|
2006-04-07 17:32:14 +00:00
|
|
|
}
|
2007-02-17 22:13:19 +00:00
|
|
|
|
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
|
|
|
*err = "Success";
|
2006-08-28 23:05:26 +00:00
|
|
|
switch_set_flag_locked(rtp_session, SWITCH_RTP_FLAG_IO);
|
2007-02-17 22:13:19 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
done:
|
2007-02-17 22:13:19 +00:00
|
|
|
|
|
|
|
if (new_sock) {
|
|
|
|
switch_socket_close(new_sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_sock) {
|
|
|
|
switch_socket_close(old_sock);
|
|
|
|
}
|
2008-01-18 18:14:53 +00:00
|
|
|
|
|
|
|
WRITE_DEC(rtp_session);
|
|
|
|
READ_DEC(rtp_session);
|
2007-02-17 22:13:19 +00:00
|
|
|
|
|
|
|
return status;
|
2006-04-06 20:56:29 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 02:12:30 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_set_max_missed_packets(switch_rtp_t *rtp_session, uint32_t max)
|
|
|
|
{
|
|
|
|
rtp_session->max_missed_packets = max;
|
|
|
|
}
|
|
|
|
|
2007-05-12 14:48:14 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rtp_set_remote_address(switch_rtp_t *rtp_session, const char *host, switch_port_t port, const char **err)
|
2006-04-06 20:56:29 +00:00
|
|
|
{
|
2008-01-19 02:27:26 +00:00
|
|
|
switch_sockaddr_t *remote_addr;
|
2006-04-06 20:56:29 +00:00
|
|
|
*err = "Success";
|
2008-01-19 02:27:26 +00:00
|
|
|
|
|
|
|
if (switch_sockaddr_info_get(&remote_addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !remote_addr) {
|
2006-04-06 20:56:29 +00:00
|
|
|
*err = "Remote Address Error!";
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2008-01-19 02:27:26 +00:00
|
|
|
switch_mutex_lock(rtp_session->write_mutex);
|
|
|
|
rtp_session->remote_addr = remote_addr;
|
2006-04-17 18:25:43 +00:00
|
|
|
rtp_session->remote_port = port;
|
2008-01-19 02:27:26 +00:00
|
|
|
switch_mutex_unlock(rtp_session->write_mutex);
|
2006-04-17 18:25:43 +00:00
|
|
|
|
2006-04-06 20:56:29 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-01-16 06:01:53 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rtp_add_crypto_key(switch_rtp_t *rtp_session,
|
|
|
|
switch_rtp_crypto_direction_t direction,
|
|
|
|
uint32_t index,
|
|
|
|
switch_rtp_crypto_key_type_t type,
|
|
|
|
unsigned char *key,
|
|
|
|
switch_size_t keylen)
|
|
|
|
{
|
|
|
|
switch_rtp_crypto_key_t *crypto_key;
|
|
|
|
srtp_policy_t *policy;
|
|
|
|
err_status_t stat;
|
2008-01-17 04:16:04 +00:00
|
|
|
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
2008-01-16 06:01:53 +00:00
|
|
|
|
2008-01-17 06:07:17 +00:00
|
|
|
if (direction >= SWITCH_RTP_CRYPTO_MAX || keylen > SWITCH_RTP_MAX_CRYPTO_LEN) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2008-01-16 06:01:53 +00:00
|
|
|
crypto_key = switch_core_alloc(rtp_session->pool, sizeof(*crypto_key));
|
2008-01-17 06:07:17 +00:00
|
|
|
|
2008-01-17 04:16:04 +00:00
|
|
|
if (direction == SWITCH_RTP_CRYPTO_RECV) {
|
|
|
|
policy = &rtp_session->recv_policy;
|
|
|
|
} else {
|
|
|
|
policy = &rtp_session->send_policy;
|
|
|
|
}
|
2008-01-16 06:01:53 +00:00
|
|
|
|
|
|
|
crypto_key->type = type;
|
|
|
|
crypto_key->index = index;
|
|
|
|
memcpy(crypto_key->key, key, keylen);
|
|
|
|
crypto_key->next = rtp_session->crypto_keys[direction];
|
|
|
|
rtp_session->crypto_keys[direction] = crypto_key;
|
|
|
|
|
|
|
|
memset(policy, 0, sizeof(*policy));
|
|
|
|
|
|
|
|
switch(crypto_key->type) {
|
|
|
|
case AES_CM_128_HMAC_SHA1_80:
|
|
|
|
crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtp);
|
|
|
|
break;
|
|
|
|
case AES_CM_128_HMAC_SHA1_32:
|
|
|
|
crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
policy->next = NULL;
|
|
|
|
policy->key = (uint8_t *) crypto_key->key;
|
|
|
|
crypto_policy_set_rtcp_default(&policy->rtcp);
|
|
|
|
policy->rtcp.sec_serv = sec_serv_none;
|
|
|
|
|
|
|
|
policy->rtp.sec_serv = sec_serv_conf_and_auth;
|
|
|
|
switch(direction) {
|
|
|
|
case SWITCH_RTP_CRYPTO_RECV:
|
|
|
|
policy->ssrc.type = ssrc_any_inbound;
|
2008-01-17 04:16:04 +00:00
|
|
|
|
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_RECV)) {
|
|
|
|
switch_set_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_RECV_RESET);
|
|
|
|
} else {
|
|
|
|
if ((stat = srtp_create(&rtp_session->recv_ctx, policy))) {
|
|
|
|
status = SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Activating Secure RTP RECV\n");
|
|
|
|
switch_set_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_RECV);
|
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error allocating srtp [%d]\n", stat);
|
|
|
|
return status;
|
|
|
|
}
|
2008-01-16 06:01:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SWITCH_RTP_CRYPTO_SEND:
|
|
|
|
policy->ssrc.type = ssrc_specific;
|
|
|
|
policy->ssrc.value = rtp_session->ssrc;
|
|
|
|
|
2008-01-17 04:16:04 +00:00
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND)) {
|
|
|
|
switch_set_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND_RESET);
|
|
|
|
} else {
|
|
|
|
if ((stat = srtp_create(&rtp_session->send_ctx, policy))) {
|
|
|
|
status = SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Activating Secure RTP SEND\n");
|
|
|
|
switch_set_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND);
|
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error allocating srtp [%d]\n", stat);
|
|
|
|
return status;
|
|
|
|
}
|
2008-01-16 06:01:53 +00:00
|
|
|
}
|
2008-01-17 04:16:04 +00:00
|
|
|
|
2008-01-16 06:01:53 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rtp_create(switch_rtp_t **new_rtp_session,
|
2006-09-12 22:23:45 +00:00
|
|
|
switch_payload_t payload,
|
2007-03-09 16:30:34 +00:00
|
|
|
uint32_t samples_per_interval,
|
2006-09-12 22:23:45 +00:00
|
|
|
uint32_t ms_per_packet,
|
2008-01-16 06:01:53 +00:00
|
|
|
switch_rtp_flag_t flags,
|
|
|
|
char *timer_name,
|
|
|
|
const char **err,
|
2007-03-30 00:15:25 +00:00
|
|
|
switch_memory_pool_t *pool)
|
2006-04-06 20:56:29 +00:00
|
|
|
{
|
2006-04-29 01:00:52 +00:00
|
|
|
switch_rtp_t *rtp_session = NULL;
|
2006-04-06 20:56:29 +00:00
|
|
|
uint32_t ssrc = rand() & 0xffff;
|
|
|
|
|
|
|
|
*new_rtp_session = NULL;
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-03-09 16:30:34 +00:00
|
|
|
if (samples_per_interval > SWITCH_RTP_MAX_BUF_LEN) {
|
2006-04-07 17:32:14 +00:00
|
|
|
*err = "Packet Size Too Large!";
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2006-04-03 21:00:13 +00:00
|
|
|
if (!(rtp_session = switch_core_alloc(pool, sizeof(*rtp_session)))) {
|
|
|
|
*err = "Memory Error!";
|
2006-04-06 20:56:29 +00:00
|
|
|
return SWITCH_STATUS_MEMERR;
|
2006-04-03 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
2006-04-04 02:28:11 +00:00
|
|
|
rtp_session->pool = pool;
|
2006-05-27 00:24:32 +00:00
|
|
|
rtp_session->te = 101;
|
2008-01-18 18:14:53 +00:00
|
|
|
rtp_session->ready = 1;
|
2006-08-28 23:05:26 +00:00
|
|
|
|
2007-12-05 17:16:50 +00:00
|
|
|
switch_mutex_init(&rtp_session->flag_mutex, SWITCH_MUTEX_NESTED, pool);
|
2008-01-18 18:14:53 +00:00
|
|
|
switch_mutex_init(&rtp_session->read_mutex, SWITCH_MUTEX_NESTED, pool);
|
|
|
|
switch_mutex_init(&rtp_session->write_mutex, SWITCH_MUTEX_NESTED, pool);
|
2007-12-05 17:16:50 +00:00
|
|
|
switch_mutex_init(&rtp_session->dtmf_data.dtmf_mutex, SWITCH_MUTEX_NESTED, pool);
|
2007-12-22 00:32:20 +00:00
|
|
|
switch_queue_create(&rtp_session->dtmf_data.dtmf_queue, 100, rtp_session->pool);
|
|
|
|
switch_queue_create(&rtp_session->dtmf_data.dtmf_inqueue, 100, rtp_session->pool);
|
|
|
|
|
2007-05-16 17:47:24 +00:00
|
|
|
switch_rtp_set_flag(rtp_session, flags);
|
|
|
|
|
2006-04-07 17:32:14 +00:00
|
|
|
/* for from address on recvfrom calls */
|
2007-12-05 17:16:50 +00:00
|
|
|
switch_sockaddr_info_get(&rtp_session->from_addr, NULL, SWITCH_UNSPEC, 0, 0, pool);
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-04-10 19:55:53 +00:00
|
|
|
|
2007-03-17 01:52:05 +00:00
|
|
|
rtp_session->seq = (uint16_t) rand();
|
2008-01-12 04:56:20 +00:00
|
|
|
rtp_session->ssrc = ssrc;
|
2008-01-16 06:01:53 +00:00
|
|
|
rtp_session->send_msg.header.ssrc = htonl(rtp_session->ssrc);
|
2007-03-29 22:31:56 +00:00
|
|
|
rtp_session->send_msg.header.ts = 0;
|
|
|
|
rtp_session->send_msg.header.m = 0;
|
|
|
|
rtp_session->send_msg.header.pt = (switch_payload_t) htonl(payload);
|
2006-04-03 21:00:13 +00:00
|
|
|
rtp_session->send_msg.header.version = 2;
|
2007-03-29 22:31:56 +00:00
|
|
|
rtp_session->send_msg.header.p = 0;
|
|
|
|
rtp_session->send_msg.header.x = 0;
|
|
|
|
rtp_session->send_msg.header.cc = 0;
|
|
|
|
|
2008-01-16 06:01:53 +00:00
|
|
|
rtp_session->recv_msg.header.ssrc = 0;
|
2007-03-29 22:31:56 +00:00
|
|
|
rtp_session->recv_msg.header.ts = 0;
|
|
|
|
rtp_session->recv_msg.header.seq = 0;
|
|
|
|
rtp_session->recv_msg.header.m = 0;
|
|
|
|
rtp_session->recv_msg.header.pt = (switch_payload_t) htonl(payload);
|
2006-04-03 21:00:13 +00:00
|
|
|
rtp_session->recv_msg.header.version = 2;
|
2007-03-29 22:31:56 +00:00
|
|
|
rtp_session->recv_msg.header.p = 0;
|
|
|
|
rtp_session->recv_msg.header.x = 0;
|
|
|
|
rtp_session->recv_msg.header.cc = 0;
|
2006-04-03 21:00:13 +00:00
|
|
|
|
|
|
|
rtp_session->payload = payload;
|
2006-04-07 17:32:14 +00:00
|
|
|
rtp_session->ms_per_packet = ms_per_packet;
|
2007-03-09 16:30:34 +00:00
|
|
|
rtp_session->samples_per_interval = rtp_session->conf_samples_per_interval = samples_per_interval;
|
2007-12-05 17:16:50 +00:00
|
|
|
rtp_session->timer_name = switch_core_strdup(pool, timer_name);
|
2006-04-10 19:55:53 +00:00
|
|
|
|
2006-09-16 20:46:52 +00:00
|
|
|
|
2006-09-21 19:51:39 +00:00
|
|
|
if (!switch_strlen_zero(timer_name)) {
|
2006-09-14 00:15:03 +00:00
|
|
|
switch_set_flag_locked(rtp_session, SWITCH_RTP_FLAG_USE_TIMER);
|
2006-09-12 22:23:45 +00:00
|
|
|
}
|
|
|
|
|
2006-09-21 19:51:39 +00:00
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER) && switch_strlen_zero(timer_name)) {
|
2006-09-16 20:46:52 +00:00
|
|
|
timer_name = "soft";
|
|
|
|
}
|
|
|
|
|
2006-09-21 19:51:39 +00:00
|
|
|
if (!switch_strlen_zero(timer_name)) {
|
2007-12-05 17:16:50 +00:00
|
|
|
if (switch_core_timer_init(&rtp_session->timer, timer_name, ms_per_packet / 1000, samples_per_interval, pool) ==
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Starting timer [%s] %d bytes per %dms\n", timer_name, samples_per_interval,
|
|
|
|
ms_per_packet);
|
2006-09-17 18:03:32 +00:00
|
|
|
} else {
|
|
|
|
memset(&rtp_session->timer, 0, sizeof(rtp_session->timer));
|
2007-03-30 00:13:31 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error starting timer [%s], async RTP disabled\n", timer_name);
|
2006-09-17 18:03:32 +00:00
|
|
|
switch_clear_flag_locked(rtp_session, SWITCH_RTP_FLAG_USE_TIMER);
|
|
|
|
}
|
2006-09-16 20:46:52 +00:00
|
|
|
}
|
|
|
|
|
2006-04-06 20:56:29 +00:00
|
|
|
*new_rtp_session = rtp_session;
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-05-12 14:48:14 +00:00
|
|
|
SWITCH_DECLARE(switch_rtp_t *) switch_rtp_new(const char *rx_host,
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_port_t rx_port,
|
2007-05-12 14:48:14 +00:00
|
|
|
const char *tx_host,
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_port_t tx_port,
|
|
|
|
switch_payload_t payload,
|
|
|
|
uint32_t samples_per_interval,
|
|
|
|
uint32_t ms_per_packet,
|
2008-01-16 06:01:53 +00:00
|
|
|
switch_rtp_flag_t flags,
|
|
|
|
char *timer_name,
|
|
|
|
const char **err,
|
|
|
|
switch_memory_pool_t *pool)
|
2006-04-06 20:56:29 +00:00
|
|
|
{
|
2007-12-13 15:10:55 +00:00
|
|
|
switch_rtp_t *rtp_session = NULL;
|
|
|
|
|
2008-01-16 06:01:53 +00:00
|
|
|
if (switch_rtp_create(&rtp_session, payload, samples_per_interval, ms_per_packet, flags, timer_name, err, pool) != SWITCH_STATUS_SUCCESS) {
|
2007-12-13 15:10:55 +00:00
|
|
|
goto end;
|
2006-04-06 20:56:29 +00:00
|
|
|
}
|
2007-12-13 15:10:55 +00:00
|
|
|
|
2006-04-06 20:56:29 +00:00
|
|
|
if (switch_rtp_set_remote_address(rtp_session, tx_host, tx_port, err) != SWITCH_STATUS_SUCCESS) {
|
2007-12-13 15:10:55 +00:00
|
|
|
rtp_session = NULL;
|
|
|
|
goto end;
|
2006-04-06 20:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_rtp_set_local_address(rtp_session, rx_host, rx_port, err) != SWITCH_STATUS_SUCCESS) {
|
2007-12-13 15:10:55 +00:00
|
|
|
rtp_session = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
|
|
|
if (rtp_session) {
|
2008-01-18 18:14:53 +00:00
|
|
|
rtp_session->ready = 2;
|
2007-12-13 15:10:55 +00:00
|
|
|
rtp_session->rx_host = switch_core_strdup(rtp_session->pool, rx_host);
|
|
|
|
rtp_session->rx_port = rx_port;
|
|
|
|
} else {
|
2007-12-20 15:41:45 +00:00
|
|
|
switch_rtp_release_port(rx_host, rx_port);
|
2006-04-06 20:56:29 +00:00
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-04-03 21:00:13 +00:00
|
|
|
return rtp_session;
|
|
|
|
}
|
|
|
|
|
2006-05-27 00:24:32 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_set_telephony_event(switch_rtp_t *rtp_session, switch_payload_t te)
|
|
|
|
{
|
2007-05-21 14:45:35 +00:00
|
|
|
if (te > 95) {
|
2006-05-27 00:24:32 +00:00
|
|
|
rtp_session->te = te;
|
|
|
|
}
|
|
|
|
}
|
2006-04-06 20:56:29 +00:00
|
|
|
|
2007-03-07 18:34:22 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_set_cng_pt(switch_rtp_t *rtp_session, switch_payload_t pt)
|
|
|
|
{
|
|
|
|
rtp_session->cng_pt = pt;
|
|
|
|
}
|
|
|
|
|
2007-05-04 23:29:35 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rtp_activate_jitter_buffer(switch_rtp_t *rtp_session, uint32_t queue_frames)
|
|
|
|
{
|
|
|
|
rtp_session->jb = stfu_n_init(queue_frames);
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rtp_activate_ice(switch_rtp_t *rtp_session, char *login, char *rlogin)
|
2006-04-04 17:22:06 +00:00
|
|
|
{
|
|
|
|
char ice_user[80];
|
|
|
|
char user_ice[80];
|
|
|
|
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(ice_user, sizeof(ice_user), "%s%s", login, rlogin);
|
|
|
|
switch_snprintf(user_ice, sizeof(user_ice), "%s%s", rlogin, login);
|
2006-04-04 17:22:06 +00:00
|
|
|
rtp_session->ice_user = switch_core_strdup(rtp_session->pool, ice_user);
|
|
|
|
rtp_session->user_ice = switch_core_strdup(rtp_session->pool, user_ice);
|
|
|
|
|
2006-04-05 20:17:22 +00:00
|
|
|
if (rtp_session->ice_user) {
|
|
|
|
if (ice_out(rtp_session) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-04 17:22:06 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 01:00:52 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_kill_socket(switch_rtp_t *rtp_session)
|
2006-04-03 21:00:13 +00:00
|
|
|
{
|
2007-12-11 19:23:57 +00:00
|
|
|
switch_assert(rtp_session != NULL);
|
2006-08-30 19:46:43 +00:00
|
|
|
switch_mutex_lock(rtp_session->flag_mutex);
|
2006-09-14 01:37:55 +00:00
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_IO)) {
|
2008-01-18 18:14:53 +00:00
|
|
|
switch_clear_flag(rtp_session, SWITCH_RTP_FLAG_IO);
|
2007-12-11 19:23:57 +00:00
|
|
|
switch_assert(rtp_session->sock != NULL);
|
2007-03-09 20:44:13 +00:00
|
|
|
switch_socket_shutdown(rtp_session->sock, SWITCH_SHUTDOWN_READWRITE);
|
2006-09-14 01:37:55 +00:00
|
|
|
}
|
2006-08-30 19:46:43 +00:00
|
|
|
switch_mutex_unlock(rtp_session->flag_mutex);
|
2006-04-03 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
2006-08-28 23:05:26 +00:00
|
|
|
SWITCH_DECLARE(uint8_t) switch_rtp_ready(switch_rtp_t *rtp_session)
|
|
|
|
{
|
2008-01-19 02:27:26 +00:00
|
|
|
return (rtp_session != NULL &&
|
|
|
|
switch_test_flag(rtp_session, SWITCH_RTP_FLAG_IO) && rtp_session->sock && rtp_session->remote_addr && rtp_session->ready == 2) ? 1 : 0;
|
2006-08-28 23:05:26 +00:00
|
|
|
}
|
2006-04-03 21:00:13 +00:00
|
|
|
|
2006-04-29 01:00:52 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_destroy(switch_rtp_t **rtp_session)
|
2006-04-03 21:00:13 +00:00
|
|
|
{
|
2007-12-22 00:32:20 +00:00
|
|
|
void *pop;
|
2008-01-18 00:10:23 +00:00
|
|
|
switch_socket_t *sock;
|
2008-01-18 18:14:53 +00:00
|
|
|
int sanity = 0;
|
2007-12-22 00:32:20 +00:00
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
switch_mutex_lock((*rtp_session)->flag_mutex);
|
|
|
|
|
|
|
|
if (!rtp_session || !*rtp_session || !(*rtp_session)->ready) {
|
2006-08-28 23:05:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
(*rtp_session)->ready = 0;
|
|
|
|
|
|
|
|
|
|
|
|
while((*rtp_session)->reading || (*rtp_session)->writing) {
|
|
|
|
switch_yield(10000);
|
|
|
|
if (++sanity > 1000) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_rtp_kill_socket(*rtp_session);
|
|
|
|
|
2007-12-22 00:32:20 +00:00
|
|
|
while(switch_queue_trypop((*rtp_session)->dtmf_data.dtmf_inqueue, &pop) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
free(pop);
|
|
|
|
}
|
|
|
|
|
|
|
|
while(switch_queue_trypop((*rtp_session)->dtmf_data.dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
free(pop);
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-05-04 23:29:35 +00:00
|
|
|
if ((*rtp_session)->jb) {
|
|
|
|
stfu_n_destroy(&(*rtp_session)->jb);
|
|
|
|
}
|
|
|
|
|
2008-01-18 00:10:23 +00:00
|
|
|
sock = (*rtp_session)->sock;
|
2006-08-30 19:46:43 +00:00
|
|
|
(*rtp_session)->sock = NULL;
|
2008-01-18 00:10:23 +00:00
|
|
|
switch_socket_close(sock);
|
|
|
|
|
2006-09-19 20:04:35 +00:00
|
|
|
|
2006-04-21 22:31:08 +00:00
|
|
|
if (switch_test_flag((*rtp_session), SWITCH_RTP_FLAG_VAD)) {
|
|
|
|
switch_rtp_disable_vad(*rtp_session);
|
|
|
|
}
|
2006-08-28 23:05:26 +00:00
|
|
|
|
2008-01-17 04:16:04 +00:00
|
|
|
if (switch_test_flag((*rtp_session), SWITCH_RTP_FLAG_SECURE_SEND)) {
|
|
|
|
switch_mutex_lock((*rtp_session)->flag_mutex);
|
2006-04-10 19:55:53 +00:00
|
|
|
srtp_dealloc((*rtp_session)->send_ctx);
|
2008-01-17 04:16:04 +00:00
|
|
|
(*rtp_session)->send_ctx = NULL;
|
|
|
|
switch_clear_flag((*rtp_session), SWITCH_RTP_FLAG_SECURE_SEND);
|
|
|
|
switch_mutex_unlock((*rtp_session)->flag_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_test_flag((*rtp_session), SWITCH_RTP_FLAG_SECURE_RECV)) {
|
|
|
|
switch_mutex_lock((*rtp_session)->flag_mutex);
|
|
|
|
srtp_dealloc((*rtp_session)->recv_ctx);
|
|
|
|
(*rtp_session)->recv_ctx = NULL;
|
|
|
|
switch_clear_flag((*rtp_session), SWITCH_RTP_FLAG_SECURE_RECV);
|
|
|
|
switch_mutex_unlock((*rtp_session)->flag_mutex);
|
2006-04-10 19:55:53 +00:00
|
|
|
}
|
|
|
|
|
2006-09-12 22:23:45 +00:00
|
|
|
if ((*rtp_session)->timer.timer_interface) {
|
|
|
|
switch_core_timer_destroy(&(*rtp_session)->timer);
|
|
|
|
}
|
|
|
|
|
2007-12-20 15:41:45 +00:00
|
|
|
switch_rtp_release_port((*rtp_session)->rx_host, (*rtp_session)->rx_port);
|
2006-09-19 20:04:35 +00:00
|
|
|
switch_mutex_unlock((*rtp_session)->flag_mutex);
|
2008-01-18 18:14:53 +00:00
|
|
|
|
2006-04-03 21:00:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
SWITCH_DECLARE(switch_socket_t *) switch_rtp_get_rtp_socket(switch_rtp_t *rtp_session)
|
2006-04-03 21:00:13 +00:00
|
|
|
{
|
|
|
|
return rtp_session->sock;
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_set_default_samples_per_interval(switch_rtp_t *rtp_session, uint16_t samples_per_interval)
|
2006-04-07 17:32:14 +00:00
|
|
|
{
|
2007-03-09 16:30:34 +00:00
|
|
|
rtp_session->samples_per_interval = samples_per_interval;
|
2006-04-07 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
2007-03-09 16:30:34 +00:00
|
|
|
SWITCH_DECLARE(uint32_t) switch_rtp_get_default_samples_per_interval(switch_rtp_t *rtp_session)
|
2006-04-07 17:32:14 +00:00
|
|
|
{
|
2007-03-09 16:30:34 +00:00
|
|
|
return rtp_session->samples_per_interval;
|
2006-04-07 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
2006-04-29 01:00:52 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_set_default_payload(switch_rtp_t *rtp_session, switch_payload_t payload)
|
2006-04-06 21:08:30 +00:00
|
|
|
{
|
|
|
|
rtp_session->payload = payload;
|
|
|
|
}
|
|
|
|
|
2006-04-29 01:00:52 +00:00
|
|
|
SWITCH_DECLARE(uint32_t) switch_rtp_get_default_payload(switch_rtp_t *rtp_session)
|
2006-04-06 21:08:30 +00:00
|
|
|
{
|
|
|
|
return rtp_session->payload;
|
|
|
|
}
|
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_set_invald_handler(switch_rtp_t *rtp_session, switch_rtp_invalid_handler_t on_invalid)
|
2006-04-03 21:00:13 +00:00
|
|
|
{
|
|
|
|
rtp_session->invalid_handler = on_invalid;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
|
2006-04-17 18:25:43 +00:00
|
|
|
{
|
2006-08-28 23:05:26 +00:00
|
|
|
switch_set_flag_locked(rtp_session, flags);
|
2007-05-16 17:47:24 +00:00
|
|
|
if (flags & SWITCH_RTP_FLAG_AUTOADJ) {
|
|
|
|
rtp_session->autoadj_window = 20;
|
|
|
|
rtp_session->autoadj_tally = 0;
|
|
|
|
}
|
2006-04-17 18:25:43 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
SWITCH_DECLARE(uint8_t) switch_rtp_test_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
|
2006-04-17 18:25:43 +00:00
|
|
|
{
|
|
|
|
return (uint8_t) switch_test_flag(rtp_session, flags);
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
|
2006-04-17 18:25:43 +00:00
|
|
|
{
|
2006-08-28 23:05:26 +00:00
|
|
|
switch_clear_flag_locked(rtp_session, flags);
|
2006-04-17 18:25:43 +00:00
|
|
|
}
|
2006-04-07 17:32:14 +00:00
|
|
|
|
2006-05-04 17:51:53 +00:00
|
|
|
static void do_2833(switch_rtp_t *rtp_session)
|
|
|
|
{
|
|
|
|
switch_frame_flag_t flags = 0;
|
2007-03-09 16:30:34 +00:00
|
|
|
uint32_t samples = rtp_session->samples_per_interval;
|
2006-05-04 17:51:53 +00:00
|
|
|
|
|
|
|
if (rtp_session->dtmf_data.out_digit_dur > 0) {
|
2008-01-08 22:30:52 +00:00
|
|
|
int x, loops = 1;
|
2006-05-04 17:51:53 +00:00
|
|
|
rtp_session->dtmf_data.out_digit_sofar += samples;
|
2008-01-11 21:59:17 +00:00
|
|
|
rtp_session->dtmf_data.out_digit_sub_sofar += samples;
|
2006-05-04 17:51:53 +00:00
|
|
|
|
2008-01-11 21:59:17 +00:00
|
|
|
if (rtp_session->dtmf_data.out_digit_sub_sofar > 0xFFFF) {
|
|
|
|
rtp_session->dtmf_data.out_digit_sub_sofar = samples;
|
2008-01-08 21:43:09 +00:00
|
|
|
rtp_session->dtmf_data.timestamp_dtmf += 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2006-05-04 17:51:53 +00:00
|
|
|
if (rtp_session->dtmf_data.out_digit_sofar >= rtp_session->dtmf_data.out_digit_dur) {
|
|
|
|
rtp_session->dtmf_data.out_digit_packet[1] |= 0x80;
|
|
|
|
loops = 3;
|
|
|
|
}
|
2008-01-08 22:30:52 +00:00
|
|
|
|
2008-01-11 21:59:17 +00:00
|
|
|
rtp_session->dtmf_data.out_digit_packet[2] = (unsigned char) (rtp_session->dtmf_data.out_digit_sub_sofar >> 8);
|
|
|
|
rtp_session->dtmf_data.out_digit_packet[3] = (unsigned char) rtp_session->dtmf_data.out_digit_sub_sofar;
|
2008-01-08 21:43:09 +00:00
|
|
|
|
2006-05-04 17:51:53 +00:00
|
|
|
for (x = 0; x < loops; x++) {
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_rtp_write_manual(rtp_session,
|
2007-02-13 17:13:57 +00:00
|
|
|
rtp_session->dtmf_data.out_digit_packet,
|
|
|
|
4,
|
|
|
|
0,
|
|
|
|
rtp_session->te,
|
2008-01-10 16:59:23 +00:00
|
|
|
rtp_session->dtmf_data.timestamp_dtmf,
|
2007-03-30 00:15:25 +00:00
|
|
|
&flags);
|
2008-01-11 21:59:17 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Send %s packet for [%c] ts=%u dur=%d/%d/%d seq=%d\n",
|
|
|
|
loops == 1 ? "middle" : "end", rtp_session->dtmf_data.out_digit,
|
|
|
|
rtp_session->dtmf_data.timestamp_dtmf,
|
|
|
|
rtp_session->dtmf_data.out_digit_sofar,
|
|
|
|
rtp_session->dtmf_data.out_digit_sub_sofar,
|
|
|
|
rtp_session->dtmf_data.out_digit_dur,
|
|
|
|
rtp_session->seq);
|
2006-05-04 17:51:53 +00:00
|
|
|
}
|
2007-03-08 22:18:33 +00:00
|
|
|
|
2007-07-06 16:19:40 +00:00
|
|
|
if (loops != 1) {
|
2008-01-11 21:59:17 +00:00
|
|
|
rtp_session->last_write_ts = rtp_session->dtmf_data.timestamp_dtmf + rtp_session->dtmf_data.out_digit_sub_sofar;
|
2007-07-06 16:19:40 +00:00
|
|
|
rtp_session->sending_dtmf = 0;
|
2008-01-11 21:59:17 +00:00
|
|
|
if (rtp_session->timer.interval) {
|
|
|
|
switch_core_timer_check(&rtp_session->timer);
|
|
|
|
rtp_session->last_write_samplecount = rtp_session->timer.samplecount;
|
2008-01-14 21:51:31 +00:00
|
|
|
rtp_session->next_write_samplecount = rtp_session->timer.samplecount + samples * 5;
|
2008-01-11 21:59:17 +00:00
|
|
|
}
|
|
|
|
rtp_session->dtmf_data.out_digit_dur = 0;
|
2007-03-08 22:18:33 +00:00
|
|
|
}
|
2006-05-04 17:51:53 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
if (!rtp_session->dtmf_data.out_digit_dur && rtp_session->dtmf_data.dtmf_queue && switch_queue_size(rtp_session->dtmf_data.dtmf_queue)) {
|
2006-05-04 17:51:53 +00:00
|
|
|
void *pop;
|
|
|
|
|
2008-01-14 21:51:31 +00:00
|
|
|
if (rtp_session->timer.interval) {
|
|
|
|
switch_core_timer_check(&rtp_session->timer);
|
|
|
|
if (rtp_session->timer.samplecount < rtp_session->next_write_samplecount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-04 17:51:53 +00:00
|
|
|
if (switch_queue_trypop(rtp_session->dtmf_data.dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) {
|
2007-12-22 00:32:20 +00:00
|
|
|
switch_dtmf_t *rdigit = pop;
|
2008-01-11 21:59:17 +00:00
|
|
|
int64_t offset;
|
2006-05-05 16:52:19 +00:00
|
|
|
|
2008-01-09 17:20:05 +00:00
|
|
|
rtp_session->sending_dtmf = 1;
|
|
|
|
|
2006-05-04 17:51:53 +00:00
|
|
|
memset(rtp_session->dtmf_data.out_digit_packet, 0, 4);
|
2008-01-08 21:43:09 +00:00
|
|
|
rtp_session->dtmf_data.out_digit_sofar = samples;
|
2008-01-11 21:59:17 +00:00
|
|
|
rtp_session->dtmf_data.out_digit_sub_sofar = samples;
|
2006-05-04 17:51:53 +00:00
|
|
|
rtp_session->dtmf_data.out_digit_dur = rdigit->duration;
|
|
|
|
rtp_session->dtmf_data.out_digit = rdigit->digit;
|
2007-03-29 22:31:56 +00:00
|
|
|
rtp_session->dtmf_data.out_digit_packet[0] = (unsigned char) switch_char_to_rfc2833(rdigit->digit);
|
2006-05-04 17:51:53 +00:00
|
|
|
rtp_session->dtmf_data.out_digit_packet[1] = 7;
|
|
|
|
|
2008-01-11 06:28:50 +00:00
|
|
|
rtp_session->dtmf_data.timestamp_dtmf = rtp_session->last_write_ts + samples;
|
2008-01-11 21:59:17 +00:00
|
|
|
if (rtp_session->timer.interval) {
|
|
|
|
switch_core_timer_check(&rtp_session->timer);
|
|
|
|
offset = rtp_session->timer.samplecount - rtp_session->last_write_samplecount;
|
|
|
|
if (offset > 0) {
|
2008-01-11 22:16:20 +00:00
|
|
|
rtp_session->dtmf_data.timestamp_dtmf = (uint32_t)(rtp_session->dtmf_data.timestamp_dtmf + offset);
|
2008-01-11 21:59:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-08 21:43:09 +00:00
|
|
|
switch_rtp_write_manual(rtp_session,
|
|
|
|
rtp_session->dtmf_data.out_digit_packet,
|
|
|
|
4,
|
|
|
|
switch_test_flag(rtp_session, SWITCH_RTP_FLAG_BUGGY_2833) ? 0 : 1,
|
|
|
|
rtp_session->te,
|
|
|
|
rtp_session->dtmf_data.timestamp_dtmf,
|
2008-01-10 16:59:23 +00:00
|
|
|
&flags);
|
2008-01-08 21:43:09 +00:00
|
|
|
|
2008-01-11 21:59:17 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Send start packet for [%c] ts=%u dur=%d/%d/%d seq=%d\n",
|
|
|
|
rtp_session->dtmf_data.out_digit,
|
|
|
|
rtp_session->dtmf_data.timestamp_dtmf,
|
2008-01-08 21:43:09 +00:00
|
|
|
rtp_session->dtmf_data.out_digit_sofar,
|
2008-01-11 21:59:17 +00:00
|
|
|
rtp_session->dtmf_data.out_digit_sub_sofar,
|
|
|
|
rtp_session->dtmf_data.out_digit_dur,
|
2008-01-08 21:43:09 +00:00
|
|
|
rtp_session->seq);
|
2008-01-11 21:59:17 +00:00
|
|
|
|
2006-05-04 17:51:53 +00:00
|
|
|
free(rdigit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_type, switch_frame_flag_t *flags)
|
2006-04-03 21:00:13 +00:00
|
|
|
{
|
2006-11-16 18:48:53 +00:00
|
|
|
switch_size_t bytes = 0;
|
2006-04-29 23:43:28 +00:00
|
|
|
switch_status_t status;
|
2006-09-16 20:46:52 +00:00
|
|
|
uint8_t check = 1;
|
2007-05-05 01:31:54 +00:00
|
|
|
stfu_frame_t *jb_frame;
|
2008-01-18 18:14:53 +00:00
|
|
|
int ret = -1;
|
2007-05-09 19:44:15 +00:00
|
|
|
|
2006-09-16 20:46:52 +00:00
|
|
|
if (!rtp_session->timer.interval) {
|
|
|
|
rtp_session->last_time = switch_time_now();
|
|
|
|
}
|
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
READ_INC(rtp_session);
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
while (switch_rtp_ready(rtp_session)) {
|
|
|
|
bytes = sizeof(rtp_msg_t);
|
2007-03-30 00:13:31 +00:00
|
|
|
status = switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock, 0, (void *) &rtp_session->recv_msg, &bytes);
|
2008-02-05 21:09:52 +00:00
|
|
|
|
2006-09-16 20:46:52 +00:00
|
|
|
if (!SWITCH_STATUS_IS_BREAK(status) && rtp_session->timer.interval) {
|
2006-09-12 22:23:45 +00:00
|
|
|
switch_core_timer_step(&rtp_session->timer);
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2008-01-17 04:16:04 +00:00
|
|
|
if (bytes && switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_RECV)) {
|
2008-01-16 06:01:53 +00:00
|
|
|
int sbytes = (int) bytes;
|
2008-01-17 04:16:04 +00:00
|
|
|
err_status_t stat = 0;
|
|
|
|
|
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_RECV_RESET)) {
|
|
|
|
switch_clear_flag_locked(rtp_session, SWITCH_RTP_FLAG_SECURE_RECV_RESET);
|
|
|
|
srtp_dealloc(rtp_session->recv_ctx);
|
|
|
|
rtp_session->recv_ctx = NULL;
|
|
|
|
if ((stat = srtp_create(&rtp_session->recv_ctx, &rtp_session->recv_policy))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP RECV\n");
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
2008-01-17 04:16:04 +00:00
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "RE-Activating Secure RTP RECV\n");
|
|
|
|
rtp_session->srtp_errs = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-01-16 06:01:53 +00:00
|
|
|
|
|
|
|
stat = srtp_unprotect(rtp_session->recv_ctx, &rtp_session->recv_msg.header, &sbytes);
|
2008-01-17 04:16:04 +00:00
|
|
|
|
|
|
|
|
2008-01-16 06:01:53 +00:00
|
|
|
if (stat && rtp_session->recv_msg.header.pt != rtp_session->te && rtp_session->recv_msg.header.pt != rtp_session->cng_pt) {
|
|
|
|
if (++rtp_session->srtp_errs >= MAX_SRTP_ERRS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
|
|
|
|
"error: srtp unprotection failed with code %d%s\n", stat,
|
|
|
|
stat == err_status_replay_fail ? " (replay check failed)" : stat == err_status_auth_fail ? " (auth check failed)" : "");
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
2008-01-16 06:01:53 +00:00
|
|
|
} else {
|
|
|
|
sbytes = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rtp_session->srtp_errs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = sbytes;
|
|
|
|
}
|
|
|
|
|
2007-05-05 01:31:54 +00:00
|
|
|
if (rtp_session->jb && bytes && rtp_session->recv_msg.header.pt == rtp_session->payload) {
|
|
|
|
if (rtp_session->recv_msg.header.m) {
|
|
|
|
stfu_n_reset(rtp_session->jb);
|
|
|
|
}
|
2007-05-04 23:29:35 +00:00
|
|
|
|
|
|
|
stfu_n_eat(rtp_session->jb, ntohl(rtp_session->recv_msg.header.ts), rtp_session->recv_msg.body, bytes - rtp_header_len);
|
2007-05-05 01:31:54 +00:00
|
|
|
if ((jb_frame = stfu_n_read_a_frame(rtp_session->jb))) {
|
|
|
|
memcpy(rtp_session->recv_msg.body, jb_frame->data, jb_frame->dlen);
|
|
|
|
if (jb_frame->plc) {
|
2007-05-04 23:29:35 +00:00
|
|
|
*flags |= SFF_PLC;
|
2007-05-05 01:31:54 +00:00
|
|
|
}
|
|
|
|
bytes = jb_frame->dlen + rtp_header_len;
|
|
|
|
rtp_session->recv_msg.header.ts = htonl(jb_frame->ts);
|
2007-05-04 23:29:35 +00:00
|
|
|
} else {
|
|
|
|
bytes = 0;
|
|
|
|
continue;
|
2007-05-05 01:31:54 +00:00
|
|
|
}
|
2007-05-04 23:29:35 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 18:34:22 +00:00
|
|
|
if (!bytes && switch_test_flag(rtp_session, SWITCH_RTP_FLAG_BREAK)) {
|
2006-08-28 23:05:26 +00:00
|
|
|
switch_clear_flag_locked(rtp_session, SWITCH_RTP_FLAG_BREAK);
|
2006-12-18 21:40:04 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
memset(&rtp_session->recv_msg.body, 0, 2);
|
2007-03-07 18:34:22 +00:00
|
|
|
rtp_session->recv_msg.body[0] = 127;
|
2007-03-29 22:31:56 +00:00
|
|
|
rtp_session->recv_msg.header.pt = SWITCH_RTP_CNG_PAYLOAD;
|
|
|
|
*flags |= SFF_CNG;
|
|
|
|
/* Return a CNG frame */
|
|
|
|
*payload_type = SWITCH_RTP_CNG_PAYLOAD;
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = 2 + rtp_header_len;
|
|
|
|
goto end;
|
2006-05-04 01:15:38 +00:00
|
|
|
}
|
|
|
|
|
2006-04-10 18:09:02 +00:00
|
|
|
if (bytes < 0) {
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = (int) bytes;
|
|
|
|
goto end;
|
2007-03-08 23:19:50 +00:00
|
|
|
}
|
|
|
|
|
2006-09-16 20:46:52 +00:00
|
|
|
if (rtp_session->timer.interval) {
|
2007-03-29 22:31:56 +00:00
|
|
|
check = (uint8_t) (switch_core_timer_check(&rtp_session->timer) == SWITCH_STATUS_SUCCESS);
|
|
|
|
|
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_AUTO_CNG) &&
|
2007-03-30 00:13:31 +00:00
|
|
|
rtp_session->timer.samplecount >= (rtp_session->last_write_samplecount + (rtp_session->samples_per_interval * 50))) {
|
2008-01-16 18:09:09 +00:00
|
|
|
uint8_t data[10] = { 0 };
|
2007-12-17 23:19:10 +00:00
|
|
|
switch_frame_flag_t frame_flags = SFF_NONE;
|
2007-03-17 01:52:05 +00:00
|
|
|
data[0] = 65;
|
|
|
|
rtp_session->cn++;
|
2008-01-18 00:10:23 +00:00
|
|
|
switch_mutex_lock(rtp_session->flag_mutex);
|
2008-01-16 18:09:09 +00:00
|
|
|
rtp_common_write(rtp_session, NULL, (void *) data, 2, rtp_session->cng_pt, 0, &frame_flags);
|
2008-01-18 00:10:23 +00:00
|
|
|
switch_mutex_unlock(rtp_session->flag_mutex);
|
2007-03-07 18:34:22 +00:00
|
|
|
}
|
2006-09-16 20:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (check) {
|
2006-05-05 16:52:19 +00:00
|
|
|
do_2833(rtp_session);
|
2007-10-31 02:12:30 +00:00
|
|
|
if (!bytes && rtp_session->max_missed_packets) {
|
|
|
|
if (++rtp_session->missed_count >= rtp_session->max_missed_packets) {
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = -2;
|
|
|
|
goto end;
|
2007-10-31 02:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-05 01:31:54 +00:00
|
|
|
|
|
|
|
if (rtp_session->jb && (jb_frame = stfu_n_read_a_frame(rtp_session->jb))) {
|
|
|
|
memcpy(rtp_session->recv_msg.body, jb_frame->data, jb_frame->dlen);
|
|
|
|
if (jb_frame->plc) {
|
|
|
|
*flags |= SFF_PLC;
|
|
|
|
}
|
|
|
|
bytes = jb_frame->dlen + rtp_header_len;
|
|
|
|
rtp_session->recv_msg.header.ts = htonl(jb_frame->ts);
|
2008-02-05 21:09:52 +00:00
|
|
|
} else if (!bytes && switch_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) { /* We're late! We're Late! */
|
2007-10-31 02:12:30 +00:00
|
|
|
uint8_t *data = (uint8_t *) rtp_session->recv_msg.body;
|
|
|
|
|
2006-05-05 16:52:19 +00:00
|
|
|
if (!switch_test_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK) && status == SWITCH_STATUS_BREAK) {
|
|
|
|
switch_yield(1000);
|
|
|
|
continue;
|
|
|
|
}
|
2007-05-05 01:31:54 +00:00
|
|
|
|
2007-04-07 01:57:54 +00:00
|
|
|
memset(data, 0, 2);
|
|
|
|
data[0] = 65;
|
|
|
|
|
|
|
|
rtp_session->recv_msg.header.pt = (uint32_t) rtp_session->cng_pt ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD;
|
2006-05-10 03:23:05 +00:00
|
|
|
*flags |= SFF_CNG;
|
2007-04-08 16:16:00 +00:00
|
|
|
*payload_type = (switch_payload_t)rtp_session->recv_msg.header.pt;
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = 2 + rtp_header_len;
|
|
|
|
goto end;
|
2007-04-07 01:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes && rtp_session->recv_msg.header.version != 2) {
|
|
|
|
uint8_t *data = (uint8_t *) rtp_session->recv_msg.body;
|
|
|
|
if (rtp_session->recv_msg.header.version == 0 && rtp_session->ice_user) {
|
|
|
|
handle_ice(rtp_session, (void *) &rtp_session->recv_msg, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rtp_session->invalid_handler) {
|
|
|
|
rtp_session->invalid_handler(rtp_session, rtp_session->sock, (void *) &rtp_session->recv_msg, bytes, rtp_session->from_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(data, 0, 2);
|
|
|
|
data[0] = 65;
|
|
|
|
|
|
|
|
rtp_session->recv_msg.header.pt = (uint32_t) rtp_session->cng_pt ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD;
|
|
|
|
*flags |= SFF_CNG;
|
2007-04-08 16:16:00 +00:00
|
|
|
*payload_type = (switch_payload_t)rtp_session->recv_msg.header.pt;
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = 2 + rtp_header_len;
|
|
|
|
goto end;
|
2007-04-07 01:57:54 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 02:12:30 +00:00
|
|
|
if (bytes > 0) {
|
|
|
|
rtp_session->missed_count = 0;
|
|
|
|
}
|
2007-05-04 23:29:35 +00:00
|
|
|
|
2006-04-10 17:25:59 +00:00
|
|
|
if (status == SWITCH_STATUS_BREAK || bytes == 0) {
|
2006-09-20 20:25:26 +00:00
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_DATAWAIT)) {
|
2007-06-23 03:26:12 +00:00
|
|
|
goto do_continue;
|
2006-09-20 20:25:26 +00:00
|
|
|
}
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = 0;
|
|
|
|
goto end;
|
2006-04-10 17:25:59 +00:00
|
|
|
}
|
2007-04-07 01:57:54 +00:00
|
|
|
|
2007-05-16 17:47:24 +00:00
|
|
|
if (bytes && switch_test_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ) && switch_sockaddr_get_port(rtp_session->from_addr)) {
|
2007-12-06 22:26:37 +00:00
|
|
|
const char *tx_host;
|
|
|
|
const char *old_host;
|
2007-04-07 01:57:54 +00:00
|
|
|
char bufa[30], bufb[30];
|
|
|
|
tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr);
|
|
|
|
old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
|
2007-05-16 17:47:24 +00:00
|
|
|
if ((switch_sockaddr_get_port(rtp_session->from_addr) != rtp_session->remote_port) || strcmp(tx_host, old_host)) {
|
2007-04-07 01:57:54 +00:00
|
|
|
const char *err;
|
|
|
|
uint32_t old = rtp_session->remote_port;
|
|
|
|
|
|
|
|
if (!switch_strlen_zero(tx_host) && switch_sockaddr_get_port(rtp_session->from_addr) > 0) {
|
2007-05-16 17:47:24 +00:00
|
|
|
if (++rtp_session->autoadj_tally >= 10) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
|
|
|
|
"Auto Changing port from %s:%u to %s:%u\n", old_host, old, tx_host,
|
|
|
|
switch_sockaddr_get_port(rtp_session->from_addr));
|
|
|
|
switch_rtp_set_remote_address(rtp_session, tx_host, switch_sockaddr_get_port(rtp_session->from_addr), &err);
|
|
|
|
}
|
2006-06-09 00:40:04 +00:00
|
|
|
}
|
2006-04-18 16:40:18 +00:00
|
|
|
}
|
2006-05-04 20:19:23 +00:00
|
|
|
}
|
|
|
|
|
2007-05-16 17:47:24 +00:00
|
|
|
if (rtp_session->autoadj_window) {
|
|
|
|
if (--rtp_session->autoadj_window == 0) {
|
|
|
|
switch_clear_flag_locked(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-07 01:57:54 +00:00
|
|
|
if (bytes && rtp_session->cng_pt && rtp_session->recv_msg.header.pt == rtp_session->cng_pt) {
|
2007-06-23 03:26:12 +00:00
|
|
|
goto do_continue;
|
2007-04-07 01:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_GOOGLEHACK) && rtp_session->recv_msg.header.pt == 102) {
|
|
|
|
rtp_session->recv_msg.header.pt = 97;
|
2006-04-03 21:00:13 +00:00
|
|
|
}
|
2006-04-07 17:32:14 +00:00
|
|
|
|
2007-04-07 01:57:54 +00:00
|
|
|
rtp_session->rpayload = (switch_payload_t) rtp_session->recv_msg.header.pt;
|
|
|
|
|
2008-01-11 21:59:17 +00:00
|
|
|
/* RFC2833 ... like all RFC RE: VoIP, guarenteed to drive you to insanity!
|
|
|
|
We know the real rules here, but if we enforce them, it's an interop nightmare so,
|
|
|
|
we put up with as much as we can so we don't have to deal with being punished for
|
|
|
|
doing it right. Nice guys finish last!
|
|
|
|
*/
|
|
|
|
if (!switch_test_flag(rtp_session, SWITCH_RTP_FLAG_PASS_RFC2833) && rtp_session->recv_msg.header.pt == rtp_session->te) {
|
2006-05-05 16:03:15 +00:00
|
|
|
unsigned char *packet = (unsigned char *) rtp_session->recv_msg.body;
|
2008-01-11 21:59:17 +00:00
|
|
|
int end = packet[1] & 0x80 ? 1 : 0;
|
2008-01-11 22:16:20 +00:00
|
|
|
uint16_t duration = (packet[2] << 8) + packet[3];
|
2006-05-05 16:03:15 +00:00
|
|
|
char key = switch_rfc2833_to_char(packet[0]);
|
2007-03-29 22:31:56 +00:00
|
|
|
uint16_t in_digit_seq = ntohs((uint16_t) rtp_session->recv_msg.header.seq);
|
2008-01-11 21:59:17 +00:00
|
|
|
|
2007-03-22 20:55:46 +00:00
|
|
|
if (in_digit_seq > rtp_session->dtmf_data.in_digit_seq) {
|
2008-01-11 21:59:17 +00:00
|
|
|
uint32_t ts = htonl(rtp_session->recv_msg.header.ts);
|
|
|
|
//int m = rtp_session->recv_msg.header.m;
|
|
|
|
|
2007-03-22 20:55:46 +00:00
|
|
|
rtp_session->dtmf_data.in_digit_seq = in_digit_seq;
|
2008-01-11 21:59:17 +00:00
|
|
|
|
|
|
|
//printf("%c %u %u %u\n", key, in_digit_seq, ts, duration);
|
|
|
|
|
|
|
|
if (rtp_session->dtmf_data.last_duration > duration && ts == rtp_session->dtmf_data.in_digit_ts) {
|
|
|
|
rtp_session->dtmf_data.flip++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end) {
|
|
|
|
if (rtp_session->dtmf_data.in_digit_ts) {
|
2007-12-22 00:32:20 +00:00
|
|
|
switch_dtmf_t dtmf = { key, duration };
|
2008-01-11 21:59:17 +00:00
|
|
|
|
|
|
|
if (ts > rtp_session->dtmf_data.in_digit_ts) {
|
|
|
|
dtmf.duration += (ts - rtp_session->dtmf_data.in_digit_ts);
|
|
|
|
}
|
|
|
|
if (rtp_session->dtmf_data.flip) {
|
|
|
|
dtmf.duration += rtp_session->dtmf_data.flip * 0xFFFF;
|
|
|
|
rtp_session->dtmf_data.flip = 0;
|
|
|
|
//printf("you're welcome!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
//printf("done digit=%c ts=%u start_ts=%u dur=%u ddur=%u\n",
|
|
|
|
//dtmf.digit, ts, rtp_session->dtmf_data.in_digit_ts, duration, dtmf.duration);
|
2007-12-22 00:32:20 +00:00
|
|
|
switch_rtp_queue_rfc2833_in(rtp_session, &dtmf);
|
2007-04-26 21:57:50 +00:00
|
|
|
switch_set_flag_locked(rtp_session, SWITCH_RTP_FLAG_BREAK);
|
2008-01-11 21:59:17 +00:00
|
|
|
rtp_session->dtmf_data.last_digit = rtp_session->dtmf_data.first_digit;
|
2007-03-22 20:55:46 +00:00
|
|
|
}
|
2008-01-11 21:59:17 +00:00
|
|
|
rtp_session->dtmf_data.in_digit_ts = 0;
|
|
|
|
} else if (!rtp_session->dtmf_data.in_digit_ts) {
|
|
|
|
rtp_session->dtmf_data.in_digit_ts = ts;
|
|
|
|
rtp_session->dtmf_data.first_digit = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtp_session->dtmf_data.last_duration = duration;
|
2007-02-10 23:16:34 +00:00
|
|
|
|
2006-05-04 17:51:53 +00:00
|
|
|
}
|
2007-06-23 03:26:12 +00:00
|
|
|
goto do_continue;
|
2006-05-04 17:51:53 +00:00
|
|
|
}
|
2006-05-05 16:03:15 +00:00
|
|
|
break;
|
2007-06-23 03:26:12 +00:00
|
|
|
|
|
|
|
do_continue:
|
2008-01-03 21:34:44 +00:00
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
if (rtp_session->ms_per_packet) {
|
|
|
|
switch_yield((rtp_session->ms_per_packet / 1000) * 750);
|
|
|
|
} else {
|
|
|
|
switch_yield(1000);
|
|
|
|
}
|
2006-05-04 17:51:53 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
*payload_type = (switch_payload_t) rtp_session->recv_msg.header.pt;
|
2006-05-04 17:51:53 +00:00
|
|
|
|
2006-04-06 19:50:53 +00:00
|
|
|
if (*payload_type == SWITCH_RTP_CNG_PAYLOAD) {
|
|
|
|
*flags |= SFF_CNG;
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-05-04 17:51:53 +00:00
|
|
|
if (bytes > 0) {
|
|
|
|
do_2833(rtp_session);
|
|
|
|
}
|
2006-05-05 13:35:33 +00:00
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = (int) bytes;
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
|
|
|
READ_DEC(rtp_session);
|
|
|
|
|
|
|
|
return ret;
|
2006-04-03 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
2006-05-04 17:51:53 +00:00
|
|
|
SWITCH_DECLARE(switch_size_t) switch_rtp_has_dtmf(switch_rtp_t *rtp_session)
|
|
|
|
{
|
2007-03-07 23:24:09 +00:00
|
|
|
switch_size_t has = 0;
|
2006-05-04 17:51:53 +00:00
|
|
|
|
2007-03-07 23:24:09 +00:00
|
|
|
if (switch_rtp_ready(rtp_session)) {
|
|
|
|
switch_mutex_lock(rtp_session->dtmf_data.dtmf_mutex);
|
2007-12-22 00:32:20 +00:00
|
|
|
has = switch_queue_size(rtp_session->dtmf_data.dtmf_inqueue);
|
2007-03-07 23:24:09 +00:00
|
|
|
switch_mutex_unlock(rtp_session->dtmf_data.dtmf_mutex);
|
|
|
|
}
|
2006-05-04 17:51:53 +00:00
|
|
|
|
|
|
|
return has;
|
|
|
|
}
|
|
|
|
|
2007-12-22 00:32:20 +00:00
|
|
|
SWITCH_DECLARE(switch_size_t) switch_rtp_dequeue_dtmf(switch_rtp_t *rtp_session, switch_dtmf_t *dtmf)
|
|
|
|
{
|
|
|
|
switch_size_t bytes = 0;
|
|
|
|
switch_dtmf_t *_dtmf = NULL;
|
|
|
|
void *pop;
|
|
|
|
|
2007-03-07 23:24:09 +00:00
|
|
|
if (!switch_rtp_ready(rtp_session)) {
|
2007-12-22 00:32:20 +00:00
|
|
|
return bytes;
|
2007-03-07 23:24:09 +00:00
|
|
|
}
|
2006-05-04 17:51:53 +00:00
|
|
|
|
|
|
|
switch_mutex_lock(rtp_session->dtmf_data.dtmf_mutex);
|
2007-12-22 00:32:20 +00:00
|
|
|
if (switch_queue_trypop(rtp_session->dtmf_data.dtmf_inqueue, &pop) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
_dtmf = (switch_dtmf_t *) pop;
|
|
|
|
*dtmf = *_dtmf;
|
|
|
|
bytes++;
|
2006-05-04 17:51:53 +00:00
|
|
|
}
|
|
|
|
switch_mutex_unlock(rtp_session->dtmf_data.dtmf_mutex);
|
|
|
|
|
2007-12-22 00:32:20 +00:00
|
|
|
return bytes;
|
2006-05-04 17:51:53 +00:00
|
|
|
}
|
|
|
|
|
2007-12-22 00:32:20 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rtp_queue_rfc2833(switch_rtp_t *rtp_session, const switch_dtmf_t *dtmf)
|
2006-05-04 17:51:53 +00:00
|
|
|
{
|
2007-12-22 00:32:20 +00:00
|
|
|
|
|
|
|
switch_dtmf_t *rdigit;
|
2006-05-04 17:51:53 +00:00
|
|
|
|
2007-03-07 23:24:09 +00:00
|
|
|
if (!switch_rtp_ready(rtp_session)) {
|
2007-12-22 00:32:20 +00:00
|
|
|
return SWITCH_STATUS_FALSE;
|
2007-03-07 23:24:09 +00:00
|
|
|
}
|
2006-05-04 17:51:53 +00:00
|
|
|
|
2007-12-22 00:32:20 +00:00
|
|
|
if ((rdigit = malloc(sizeof(*rdigit))) != 0) {
|
|
|
|
*rdigit = *dtmf;
|
2008-01-14 21:27:40 +00:00
|
|
|
if ((switch_queue_trypush(rtp_session->dtmf_data.dtmf_queue, rdigit)) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
free(rdigit);
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2007-12-22 00:32:20 +00:00
|
|
|
} else {
|
|
|
|
abort();
|
2006-05-04 17:51:53 +00:00
|
|
|
}
|
2007-12-22 00:32:20 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2006-05-04 17:51:53 +00:00
|
|
|
}
|
|
|
|
|
2007-12-22 00:32:20 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rtp_queue_rfc2833_in(switch_rtp_t *rtp_session, const switch_dtmf_t *dtmf)
|
2006-05-04 17:51:53 +00:00
|
|
|
{
|
2007-12-22 00:32:20 +00:00
|
|
|
switch_dtmf_t *rdigit;
|
2006-05-04 17:51:53 +00:00
|
|
|
|
2007-03-07 23:24:09 +00:00
|
|
|
if (!switch_rtp_ready(rtp_session)) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2007-12-22 00:32:20 +00:00
|
|
|
if ((rdigit = malloc(sizeof(*rdigit))) != 0) {
|
|
|
|
*rdigit = *dtmf;
|
2008-01-14 21:27:40 +00:00
|
|
|
if ((switch_queue_trypush(rtp_session->dtmf_data.dtmf_inqueue, rdigit)) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
free(rdigit);
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2007-12-22 00:32:20 +00:00
|
|
|
} else {
|
|
|
|
abort();
|
2006-05-04 17:51:53 +00:00
|
|
|
}
|
2007-12-22 00:32:20 +00:00
|
|
|
|
2006-05-04 17:51:53 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rtp_read(switch_rtp_t *rtp_session, void *data, uint32_t * datalen,
|
|
|
|
switch_payload_t *payload_type, switch_frame_flag_t *flags)
|
2006-04-03 21:00:13 +00:00
|
|
|
{
|
2007-03-07 23:24:09 +00:00
|
|
|
int bytes = 0;
|
2006-04-03 21:00:13 +00:00
|
|
|
|
2007-03-07 23:24:09 +00:00
|
|
|
if (!switch_rtp_ready(rtp_session)) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = rtp_common_read(rtp_session, payload_type, flags);
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-04-12 20:48:47 +00:00
|
|
|
if (bytes < 0) {
|
2006-04-10 18:28:46 +00:00
|
|
|
*datalen = 0;
|
2007-10-31 13:08:45 +00:00
|
|
|
return bytes == -2 ? SWITCH_STATUS_TIMEOUT : SWITCH_STATUS_GENERR;
|
2006-05-04 01:15:38 +00:00
|
|
|
} else if (bytes == 0) {
|
|
|
|
*datalen = 0;
|
|
|
|
return SWITCH_STATUS_BREAK;
|
2006-04-18 00:24:52 +00:00
|
|
|
} else {
|
|
|
|
bytes -= rtp_header_len;
|
2006-04-03 21:00:13 +00:00
|
|
|
}
|
2006-04-10 18:28:46 +00:00
|
|
|
|
|
|
|
*datalen = bytes;
|
|
|
|
|
2006-04-07 17:32:14 +00:00
|
|
|
memcpy(data, rtp_session->recv_msg.body, bytes);
|
2006-04-10 18:28:46 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2006-04-07 17:32:14 +00:00
|
|
|
}
|
2006-04-03 21:00:13 +00:00
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rtp_zerocopy_read_frame(switch_rtp_t *rtp_session, switch_frame_t *frame)
|
2006-04-18 00:24:52 +00:00
|
|
|
{
|
2007-03-07 23:24:09 +00:00
|
|
|
int bytes = 0;
|
|
|
|
|
|
|
|
if (!switch_rtp_ready(rtp_session)) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = rtp_common_read(rtp_session, &frame->payload, &frame->flags);
|
2007-04-19 21:40:50 +00:00
|
|
|
|
2006-04-18 00:24:52 +00:00
|
|
|
frame->data = rtp_session->recv_msg.body;
|
|
|
|
frame->packet = &rtp_session->recv_msg;
|
|
|
|
frame->packetlen = bytes;
|
|
|
|
frame->source = __FILE__;
|
|
|
|
frame->flags |= SFF_RAW_RTP;
|
2007-10-23 21:32:17 +00:00
|
|
|
if (frame->payload == rtp_session->te) {
|
|
|
|
frame->flags |= SFF_RFC2833;
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
frame->timestamp = ntohl(rtp_session->recv_msg.header.ts);
|
2007-04-19 22:04:03 +00:00
|
|
|
frame->seq = (uint16_t)ntohs((u_short)rtp_session->recv_msg.header.seq);
|
2007-04-19 21:40:50 +00:00
|
|
|
frame->ssrc = ntohl(rtp_session->recv_msg.header.ssrc);
|
|
|
|
frame->m = rtp_session->recv_msg.header.m ? SWITCH_TRUE : SWITCH_FALSE;
|
2006-04-18 00:24:52 +00:00
|
|
|
|
|
|
|
if (bytes < 0) {
|
|
|
|
frame->datalen = 0;
|
2007-10-31 13:08:45 +00:00
|
|
|
return bytes == -2 ? SWITCH_STATUS_TIMEOUT : SWITCH_STATUS_GENERR;
|
2006-04-18 16:20:47 +00:00
|
|
|
} else if (bytes == 0) {
|
|
|
|
frame->datalen = 0;
|
|
|
|
return SWITCH_STATUS_BREAK;
|
2006-04-18 00:24:52 +00:00
|
|
|
} else {
|
|
|
|
bytes -= rtp_header_len;
|
|
|
|
}
|
2007-04-19 21:40:50 +00:00
|
|
|
|
2006-04-18 00:24:52 +00:00
|
|
|
frame->datalen = bytes;
|
2007-04-19 21:40:50 +00:00
|
|
|
|
2006-04-18 00:24:52 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-05-04 17:51:53 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rtp_zerocopy_read(switch_rtp_t *rtp_session,
|
2007-03-30 00:13:31 +00:00
|
|
|
void **data, uint32_t * datalen, switch_payload_t *payload_type, switch_frame_flag_t *flags)
|
2006-04-07 17:32:14 +00:00
|
|
|
{
|
2007-03-07 23:24:09 +00:00
|
|
|
int bytes = 0;
|
|
|
|
|
|
|
|
if (!switch_rtp_ready(rtp_session)) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = rtp_common_read(rtp_session, payload_type, flags);
|
2006-04-10 18:09:02 +00:00
|
|
|
*data = rtp_session->recv_msg.body;
|
|
|
|
|
2006-04-12 20:48:47 +00:00
|
|
|
if (bytes < 0) {
|
2006-04-10 18:28:46 +00:00
|
|
|
*datalen = 0;
|
|
|
|
return SWITCH_STATUS_GENERR;
|
2006-04-18 00:24:52 +00:00
|
|
|
} else {
|
|
|
|
bytes -= rtp_header_len;
|
2006-04-03 21:00:13 +00:00
|
|
|
}
|
2006-04-10 18:09:02 +00:00
|
|
|
|
2006-04-10 18:28:46 +00:00
|
|
|
*datalen = bytes;
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2006-04-07 17:32:14 +00:00
|
|
|
}
|
2006-04-04 17:22:06 +00:00
|
|
|
|
2008-01-11 04:23:09 +00:00
|
|
|
static int rtp_common_write(switch_rtp_t *rtp_session,
|
|
|
|
rtp_msg_t *send_msg,
|
|
|
|
void *data,
|
|
|
|
uint32_t datalen,
|
|
|
|
switch_payload_t payload,
|
|
|
|
uint32_t timestamp,
|
|
|
|
switch_frame_flag_t *flags)
|
2006-04-07 17:32:14 +00:00
|
|
|
{
|
|
|
|
switch_size_t bytes;
|
2006-04-21 22:31:08 +00:00
|
|
|
uint8_t send = 1;
|
2008-01-11 04:23:09 +00:00
|
|
|
uint32_t this_ts = 0;
|
2008-01-18 18:14:53 +00:00
|
|
|
int ret;
|
2006-04-10 16:56:11 +00:00
|
|
|
|
2007-03-07 23:24:09 +00:00
|
|
|
if (!switch_rtp_ready(rtp_session)) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
WRITE_INC(rtp_session);
|
|
|
|
|
2008-01-11 04:23:09 +00:00
|
|
|
if (send_msg) {
|
2006-04-18 00:24:52 +00:00
|
|
|
bytes = datalen;
|
2007-12-17 23:19:10 +00:00
|
|
|
if (flags && *flags & SFF_RFC2833) {
|
2007-10-23 21:32:17 +00:00
|
|
|
send_msg->header.pt = rtp_session->te;
|
|
|
|
}
|
2008-01-28 17:45:12 +00:00
|
|
|
data = send_msg->body;
|
|
|
|
datalen -= rtp_header_len;
|
2006-04-18 00:24:52 +00:00
|
|
|
} else {
|
2007-03-17 01:52:05 +00:00
|
|
|
uint8_t m = 0;
|
2007-10-23 21:32:17 +00:00
|
|
|
|
|
|
|
if (*flags & SFF_RFC2833) {
|
|
|
|
payload = rtp_session->te;
|
|
|
|
}
|
2007-06-11 22:12:10 +00:00
|
|
|
|
2008-01-11 04:23:09 +00:00
|
|
|
send_msg = &rtp_session->send_msg;
|
|
|
|
send_msg->header.pt = payload;
|
|
|
|
|
|
|
|
if (timestamp) {
|
|
|
|
rtp_session->ts = (uint32_t) timestamp;
|
|
|
|
} else if (rtp_session->timer.timer_interface) {
|
|
|
|
rtp_session->ts = rtp_session->timer.samplecount;
|
2008-01-23 16:23:01 +00:00
|
|
|
if (rtp_session->ts <= rtp_session->last_write_ts) {
|
|
|
|
rtp_session->ts = rtp_session->last_write_ts + rtp_session->samples_per_interval;
|
|
|
|
}
|
2008-01-11 04:23:09 +00:00
|
|
|
} else {
|
|
|
|
rtp_session->ts += rtp_session->samples_per_interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtp_session->send_msg.header.ts = htonl(rtp_session->ts);
|
|
|
|
|
|
|
|
if ((rtp_session->ts > (rtp_session->last_write_ts + (rtp_session->samples_per_interval * 10)))
|
2007-06-11 22:12:10 +00:00
|
|
|
|| rtp_session->ts == rtp_session->samples_per_interval) {
|
2007-03-17 01:52:05 +00:00
|
|
|
m++;
|
|
|
|
}
|
2007-06-11 22:12:10 +00:00
|
|
|
|
2007-03-17 01:52:05 +00:00
|
|
|
if (rtp_session->cn && payload != rtp_session->cng_pt) {
|
|
|
|
rtp_session->cn = 0;
|
2007-06-11 22:12:10 +00:00
|
|
|
m++;
|
2007-03-17 01:52:05 +00:00
|
|
|
}
|
2006-04-21 22:31:08 +00:00
|
|
|
send_msg->header.m = m ? 1 : 0;
|
2008-01-11 16:01:07 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
memcpy(send_msg->body, data, datalen);
|
|
|
|
bytes = datalen + rtp_header_len;
|
2006-04-03 21:00:13 +00:00
|
|
|
}
|
2006-04-18 00:24:52 +00:00
|
|
|
|
2008-01-16 06:01:53 +00:00
|
|
|
send_msg->header.ssrc = htonl(rtp_session->ssrc);
|
2006-04-03 21:00:13 +00:00
|
|
|
|
2006-04-19 20:38:02 +00:00
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_GOOGLEHACK) && rtp_session->send_msg.header.pt == 97) {
|
|
|
|
rtp_session->recv_msg.header.pt = 102;
|
|
|
|
}
|
2006-12-13 22:58:32 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_VAD) &&
|
2007-04-07 01:57:54 +00:00
|
|
|
rtp_session->recv_msg.header.pt == rtp_session->vad_data.read_codec->implementation->ianacode) {
|
|
|
|
|
|
|
|
int16_t decoded[SWITCH_RECOMMENDED_BUFFER_SIZE / sizeof(int16_t)] = {0};
|
|
|
|
uint32_t rate = 0;
|
2007-12-17 23:19:10 +00:00
|
|
|
uint32_t codec_flags = 0;
|
2006-04-21 22:31:08 +00:00
|
|
|
uint32_t len = sizeof(decoded);
|
2008-01-11 00:43:49 +00:00
|
|
|
time_t now = switch_timestamp(NULL);
|
2006-04-24 14:53:54 +00:00
|
|
|
send = 0;
|
2006-12-13 22:58:32 +00:00
|
|
|
|
2006-04-21 22:31:08 +00:00
|
|
|
if (rtp_session->vad_data.scan_freq && rtp_session->vad_data.next_scan <= now) {
|
|
|
|
rtp_session->vad_data.bg_count = rtp_session->vad_data.bg_level = 0;
|
|
|
|
rtp_session->vad_data.next_scan = now + rtp_session->vad_data.scan_freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_core_codec_decode(&rtp_session->vad_data.vad_codec,
|
|
|
|
rtp_session->vad_data.read_codec,
|
|
|
|
data,
|
|
|
|
datalen,
|
2007-10-19 15:31:02 +00:00
|
|
|
rtp_session->vad_data.read_codec->implementation->actual_samples_per_second,
|
2007-12-17 23:19:10 +00:00
|
|
|
decoded, &len, &rate, &codec_flags) == SWITCH_STATUS_SUCCESS) {
|
2006-04-21 22:31:08 +00:00
|
|
|
|
2006-04-24 14:53:54 +00:00
|
|
|
uint32_t energy = 0;
|
2006-04-21 22:31:08 +00:00
|
|
|
uint32_t x, y = 0, z = len / sizeof(int16_t);
|
|
|
|
uint32_t score = 0;
|
2007-11-24 21:48:25 +00:00
|
|
|
int divisor = 0;
|
2006-04-21 22:31:08 +00:00
|
|
|
if (z) {
|
2007-11-24 21:48:25 +00:00
|
|
|
|
|
|
|
if (!(divisor = rtp_session->vad_data.read_codec->implementation->actual_samples_per_second / 8000)) {
|
|
|
|
divisor = 1;
|
|
|
|
}
|
|
|
|
|
2006-04-21 22:31:08 +00:00
|
|
|
for (x = 0; x < z; x++) {
|
2006-04-24 14:53:54 +00:00
|
|
|
energy += abs(decoded[y]);
|
2006-04-21 22:31:08 +00:00
|
|
|
y += rtp_session->vad_data.read_codec->implementation->number_of_channels;
|
|
|
|
}
|
2007-04-07 01:57:54 +00:00
|
|
|
|
2006-04-21 22:31:08 +00:00
|
|
|
if (++rtp_session->vad_data.start_count < rtp_session->vad_data.start) {
|
|
|
|
send = 1;
|
|
|
|
} else {
|
2007-11-24 21:48:25 +00:00
|
|
|
score = (energy / (z / divisor));
|
2006-04-21 22:31:08 +00:00
|
|
|
if (score && (rtp_session->vad_data.bg_count < rtp_session->vad_data.bg_len)) {
|
|
|
|
rtp_session->vad_data.bg_level += score;
|
|
|
|
if (++rtp_session->vad_data.bg_count == rtp_session->vad_data.bg_len) {
|
|
|
|
rtp_session->vad_data.bg_level /= rtp_session->vad_data.bg_len;
|
|
|
|
}
|
|
|
|
send = 1;
|
|
|
|
} else {
|
2007-06-09 19:01:43 +00:00
|
|
|
if (score > rtp_session->vad_data.bg_level && !switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
|
2006-04-21 22:31:08 +00:00
|
|
|
uint32_t diff = score - rtp_session->vad_data.bg_level;
|
|
|
|
|
|
|
|
if (rtp_session->vad_data.hangover_hits) {
|
|
|
|
rtp_session->vad_data.hangover_hits--;
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
if (diff >= rtp_session->vad_data.diff_level || ++rtp_session->vad_data.hangunder_hits >= rtp_session->vad_data.hangunder) {
|
2007-06-09 19:01:43 +00:00
|
|
|
|
2006-04-21 22:31:08 +00:00
|
|
|
switch_set_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING);
|
|
|
|
send_msg->header.m = 1;
|
2007-03-30 00:13:31 +00:00
|
|
|
rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
|
2006-04-21 22:31:08 +00:00
|
|
|
if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_EVENTS_TALK)) {
|
2006-04-29 01:00:52 +00:00
|
|
|
switch_event_t *event;
|
2006-04-21 22:31:08 +00:00
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_TALK) == SWITCH_STATUS_SUCCESS) {
|
2008-01-28 07:26:10 +00:00
|
|
|
switch_channel_event_set_data(switch_core_session_get_channel(rtp_session->vad_data.session), event);
|
2006-04-21 22:31:08 +00:00
|
|
|
switch_event_fire(&event);
|
|
|
|
}
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
}
|
2006-04-21 22:31:08 +00:00
|
|
|
} else {
|
|
|
|
if (rtp_session->vad_data.hangunder_hits) {
|
|
|
|
rtp_session->vad_data.hangunder_hits--;
|
|
|
|
}
|
|
|
|
if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
|
|
|
|
if (++rtp_session->vad_data.hangover_hits >= rtp_session->vad_data.hangover) {
|
|
|
|
switch_clear_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING);
|
2007-03-30 00:13:31 +00:00
|
|
|
rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
|
2006-04-21 22:31:08 +00:00
|
|
|
if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_EVENTS_NOTALK)) {
|
2006-04-29 01:00:52 +00:00
|
|
|
switch_event_t *event;
|
2006-04-21 22:31:08 +00:00
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_NOTALK) == SWITCH_STATUS_SUCCESS) {
|
2008-01-28 07:26:10 +00:00
|
|
|
switch_channel_event_set_data(switch_core_session_get_channel(rtp_session->vad_data.session), event);
|
2006-04-21 22:31:08 +00:00
|
|
|
switch_event_fire(&event);
|
2007-03-29 22:31:56 +00:00
|
|
|
}
|
2006-04-21 22:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-04-21 22:31:08 +00:00
|
|
|
if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
|
|
|
|
send = 1;
|
2007-04-07 01:57:54 +00:00
|
|
|
}
|
2006-04-21 22:31:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
2006-04-21 22:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-11 05:42:50 +00:00
|
|
|
this_ts = ntohl(send_msg->header.ts);
|
|
|
|
|
2008-01-18 00:10:23 +00:00
|
|
|
if (!switch_rtp_ready(rtp_session) || rtp_session->sending_dtmf || !this_ts) {
|
2008-01-11 04:23:09 +00:00
|
|
|
send = 0;
|
|
|
|
}
|
2007-04-07 01:57:54 +00:00
|
|
|
|
2008-01-11 04:23:09 +00:00
|
|
|
if (send) {
|
2008-01-11 16:01:07 +00:00
|
|
|
send_msg->header.seq = htons(++rtp_session->seq);
|
2008-01-11 04:23:09 +00:00
|
|
|
|
2008-01-16 06:01:53 +00:00
|
|
|
|
2008-01-17 04:16:04 +00:00
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND)) {
|
2008-01-16 06:01:53 +00:00
|
|
|
int sbytes = (int) bytes;
|
|
|
|
err_status_t stat;
|
|
|
|
|
2008-01-17 04:16:04 +00:00
|
|
|
|
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND_RESET)) {
|
|
|
|
switch_clear_flag_locked(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND_RESET);
|
|
|
|
srtp_dealloc(rtp_session->send_ctx);
|
|
|
|
rtp_session->send_ctx = NULL;
|
|
|
|
if ((stat = srtp_create(&rtp_session->send_ctx, &rtp_session->send_policy))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP SEND\n");
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
2008-01-17 04:16:04 +00:00
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "RE-Activating Secure RTP SEND\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-16 06:01:53 +00:00
|
|
|
stat = srtp_protect(rtp_session->send_ctx, &send_msg->header, &sbytes);
|
|
|
|
if (stat) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error: srtp protection failed with code %d\n", stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = sbytes;
|
|
|
|
}
|
|
|
|
|
2008-01-11 04:23:09 +00:00
|
|
|
if (switch_socket_sendto(rtp_session->sock, rtp_session->remote_addr, 0, (void *) send_msg, &bytes) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
rtp_session->seq--;
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
2008-01-11 01:27:24 +00:00
|
|
|
}
|
2008-01-11 04:23:09 +00:00
|
|
|
|
2007-03-17 01:52:05 +00:00
|
|
|
if (rtp_session->timer.interval) {
|
|
|
|
switch_core_timer_check(&rtp_session->timer);
|
|
|
|
rtp_session->last_write_samplecount = rtp_session->timer.samplecount;
|
|
|
|
}
|
2008-01-11 04:23:09 +00:00
|
|
|
rtp_session->last_write_ts = this_ts;
|
2006-04-21 22:31:08 +00:00
|
|
|
}
|
|
|
|
|
2006-04-07 17:32:14 +00:00
|
|
|
if (rtp_session->ice_user) {
|
|
|
|
if (ice_out(rtp_session) != SWITCH_STATUS_SUCCESS) {
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
2006-04-07 17:32:14 +00:00
|
|
|
}
|
2006-04-06 19:50:53 +00:00
|
|
|
}
|
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = (int) bytes;
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
|
|
|
WRITE_DEC(rtp_session);
|
|
|
|
|
|
|
|
return ret;
|
2006-04-03 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rtp_disable_vad(switch_rtp_t *rtp_session)
|
2006-04-21 22:31:08 +00:00
|
|
|
{
|
2007-03-07 23:24:09 +00:00
|
|
|
|
|
|
|
if (!switch_rtp_ready(rtp_session)) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2006-04-21 22:31:08 +00:00
|
|
|
if (!switch_test_flag(rtp_session, SWITCH_RTP_FLAG_VAD)) {
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
switch_core_codec_destroy(&rtp_session->vad_data.vad_codec);
|
2006-08-28 23:05:26 +00:00
|
|
|
switch_clear_flag_locked(rtp_session, SWITCH_RTP_FLAG_VAD);
|
2006-04-21 22:31:08 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:15:25 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_rtp_enable_vad(switch_rtp_t *rtp_session, switch_core_session_t *session, switch_codec_t *codec,
|
|
|
|
switch_vad_flag_t flags)
|
2006-04-21 22:31:08 +00:00
|
|
|
{
|
2007-03-07 23:24:09 +00:00
|
|
|
if (!switch_rtp_ready(rtp_session)) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2006-04-21 22:31:08 +00:00
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_VAD)) {
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
memset(&rtp_session->vad_data, 0, sizeof(rtp_session->vad_data));
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-04-21 22:31:08 +00:00
|
|
|
if (switch_core_codec_init(&rtp_session->vad_data.vad_codec,
|
2006-06-23 20:14:29 +00:00
|
|
|
codec->implementation->iananame,
|
2006-10-09 02:24:43 +00:00
|
|
|
NULL,
|
2006-04-21 22:31:08 +00:00
|
|
|
codec->implementation->samples_per_second,
|
|
|
|
codec->implementation->microseconds_per_frame / 1000,
|
|
|
|
codec->implementation->number_of_channels,
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
|
2006-04-21 22:31:08 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't load codec?\n");
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2007-04-07 01:57:54 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Activate VAD codec %s %dms\n", codec->implementation->iananame,
|
|
|
|
codec->implementation->microseconds_per_frame / 1000);
|
2006-04-21 22:31:08 +00:00
|
|
|
rtp_session->vad_data.diff_level = 400;
|
|
|
|
rtp_session->vad_data.hangunder = 15;
|
|
|
|
rtp_session->vad_data.hangover = 40;
|
2006-04-22 16:25:33 +00:00
|
|
|
rtp_session->vad_data.bg_len = 5;
|
|
|
|
rtp_session->vad_data.bg_count = 5;
|
|
|
|
rtp_session->vad_data.bg_level = 300;
|
2006-04-21 22:31:08 +00:00
|
|
|
rtp_session->vad_data.read_codec = codec;
|
|
|
|
rtp_session->vad_data.session = session;
|
|
|
|
rtp_session->vad_data.flags = flags;
|
|
|
|
rtp_session->vad_data.cng_freq = 50;
|
|
|
|
rtp_session->vad_data.ts = 1;
|
2006-04-22 16:25:33 +00:00
|
|
|
rtp_session->vad_data.start = 0;
|
2008-01-11 00:43:49 +00:00
|
|
|
rtp_session->vad_data.next_scan = switch_timestamp(NULL);
|
2006-04-21 22:31:08 +00:00
|
|
|
rtp_session->vad_data.scan_freq = 0;
|
2006-08-28 23:05:26 +00:00
|
|
|
switch_set_flag_locked(rtp_session, SWITCH_RTP_FLAG_VAD);
|
2006-04-22 16:12:03 +00:00
|
|
|
switch_set_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_CNG);
|
2006-04-21 22:31:08 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-01-11 04:23:09 +00:00
|
|
|
SWITCH_DECLARE(int) switch_rtp_write_frame(switch_rtp_t *rtp_session, switch_frame_t *frame)
|
2006-04-18 16:20:47 +00:00
|
|
|
{
|
2007-03-07 23:24:09 +00:00
|
|
|
uint8_t fwd = 0;
|
2008-01-11 04:23:09 +00:00
|
|
|
void *data = NULL;
|
|
|
|
uint32_t len, ts = 0;
|
2007-03-07 21:21:42 +00:00
|
|
|
switch_payload_t payload;
|
2008-01-11 04:23:09 +00:00
|
|
|
rtp_msg_t *send_msg = NULL;
|
2006-04-18 16:20:47 +00:00
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
if (!switch_rtp_ready(rtp_session) || !rtp_session->remote_addr) {
|
2007-03-07 23:24:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fwd = (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_RAW_WRITE) && switch_test_flag(frame, SFF_RAW_RTP)) ? 1 : 0;
|
|
|
|
|
2007-12-11 19:23:57 +00:00
|
|
|
switch_assert(frame != NULL);
|
2007-03-07 21:21:42 +00:00
|
|
|
|
|
|
|
if (switch_test_flag(frame, SFF_CNG)) {
|
|
|
|
payload = rtp_session->cng_pt;
|
|
|
|
} else {
|
2007-03-10 00:43:53 +00:00
|
|
|
payload = rtp_session->payload;
|
2007-03-07 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2007-04-19 21:40:50 +00:00
|
|
|
if (switch_test_flag(frame, SFF_RTP_HEADER)) {
|
|
|
|
return switch_rtp_write_manual(rtp_session, frame->data, frame->datalen, frame->m, frame->payload,
|
2008-01-10 16:59:23 +00:00
|
|
|
(uint32_t)(frame->timestamp), &frame->flags);
|
2007-04-19 21:40:50 +00:00
|
|
|
}
|
|
|
|
|
2007-04-05 19:29:48 +00:00
|
|
|
if (fwd) {
|
2008-01-11 04:23:09 +00:00
|
|
|
send_msg = frame->packet;
|
2006-04-18 16:20:47 +00:00
|
|
|
len = frame->packetlen;
|
2008-01-11 04:23:09 +00:00
|
|
|
ts = 0;
|
2006-04-18 16:20:47 +00:00
|
|
|
} else {
|
|
|
|
data = frame->data;
|
|
|
|
len = frame->datalen;
|
2008-01-20 11:00:07 +00:00
|
|
|
ts = (uint32_t)frame->timestamp;
|
2006-04-18 16:20:47 +00:00
|
|
|
}
|
|
|
|
|
2008-01-11 04:23:09 +00:00
|
|
|
return rtp_common_write(rtp_session, send_msg, data, len, payload, ts, &frame->flags);
|
2006-04-18 16:20:47 +00:00
|
|
|
}
|
|
|
|
|
2007-02-13 17:13:57 +00:00
|
|
|
SWITCH_DECLARE(int) switch_rtp_write_manual(switch_rtp_t *rtp_session,
|
|
|
|
void *data,
|
2007-04-19 22:04:03 +00:00
|
|
|
uint32_t datalen,
|
2008-01-10 16:59:23 +00:00
|
|
|
uint8_t m, switch_payload_t payload, uint32_t ts, switch_frame_flag_t *flags)
|
2006-04-03 21:00:13 +00:00
|
|
|
{
|
2007-03-08 22:18:33 +00:00
|
|
|
switch_size_t bytes;
|
2008-01-18 18:14:53 +00:00
|
|
|
int ret = -1;
|
2006-04-03 21:00:13 +00:00
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
if (!switch_rtp_ready(rtp_session) || !rtp_session->remote_addr || datalen > SWITCH_RTP_MAX_BUF_LEN) {
|
2007-03-07 23:24:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
WRITE_INC(rtp_session);
|
2007-12-17 23:43:15 +00:00
|
|
|
|
2007-12-18 17:33:29 +00:00
|
|
|
rtp_session->write_msg = rtp_session->send_msg;
|
2008-01-11 16:01:07 +00:00
|
|
|
rtp_session->write_msg.header.seq = htons(++rtp_session->seq);
|
2007-12-18 17:33:29 +00:00
|
|
|
rtp_session->write_msg.header.ts = htonl(ts);
|
|
|
|
rtp_session->write_msg.header.pt = payload;
|
|
|
|
rtp_session->write_msg.header.m = m ? 1 : 0;
|
|
|
|
memcpy(rtp_session->write_msg.body, data, datalen);
|
2006-04-03 21:00:13 +00:00
|
|
|
|
2007-03-08 22:18:33 +00:00
|
|
|
bytes = rtp_header_len + datalen;
|
|
|
|
|
2008-01-17 04:16:04 +00:00
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND)) {
|
2007-03-29 22:31:56 +00:00
|
|
|
int sbytes = (int) bytes;
|
2007-03-08 22:18:33 +00:00
|
|
|
err_status_t stat;
|
|
|
|
|
2008-01-17 04:16:04 +00:00
|
|
|
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND_RESET)) {
|
|
|
|
switch_clear_flag_locked(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND_RESET);
|
|
|
|
srtp_dealloc(rtp_session->send_ctx);
|
|
|
|
rtp_session->send_ctx = NULL;
|
|
|
|
if ((stat = srtp_create(&rtp_session->send_ctx, &rtp_session->send_policy))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP SEND\n");
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
2008-01-17 04:16:04 +00:00
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "RE-Activating Secure RTP SEND\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-18 17:33:29 +00:00
|
|
|
stat = srtp_protect(rtp_session->send_ctx, &rtp_session->write_msg.header, &sbytes);
|
2007-03-08 22:18:33 +00:00
|
|
|
if (stat) {
|
2007-03-30 00:13:31 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error: srtp protection failed with code %d\n", stat);
|
2007-03-08 22:18:33 +00:00
|
|
|
}
|
|
|
|
bytes = sbytes;
|
|
|
|
}
|
|
|
|
|
2007-12-18 17:33:29 +00:00
|
|
|
if (switch_socket_sendto(rtp_session->sock, rtp_session->remote_addr, 0, (void *) &rtp_session->write_msg, &bytes) != SWITCH_STATUS_SUCCESS) {
|
2008-01-10 03:10:58 +00:00
|
|
|
rtp_session->seq--;
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
2007-03-08 22:18:33 +00:00
|
|
|
}
|
2008-01-10 03:10:58 +00:00
|
|
|
|
2008-01-11 16:01:07 +00:00
|
|
|
rtp_session->last_write_ts = ts;
|
2008-01-10 03:10:58 +00:00
|
|
|
|
2008-01-18 18:14:53 +00:00
|
|
|
ret = (int) bytes;
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
|
|
|
WRITE_DEC(rtp_session);
|
|
|
|
|
|
|
|
return ret;
|
2006-04-03 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
2006-04-29 01:00:52 +00:00
|
|
|
SWITCH_DECLARE(uint32_t) switch_rtp_get_ssrc(switch_rtp_t *rtp_session)
|
2006-04-03 21:00:13 +00:00
|
|
|
{
|
|
|
|
return rtp_session->send_msg.header.ssrc;
|
|
|
|
}
|
|
|
|
|
2006-04-29 01:00:52 +00:00
|
|
|
SWITCH_DECLARE(void) switch_rtp_set_private(switch_rtp_t *rtp_session, void *private_data)
|
2006-04-03 21:00:13 +00:00
|
|
|
{
|
|
|
|
rtp_session->private_data = private_data;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
SWITCH_DECLARE(void *) switch_rtp_get_private(switch_rtp_t *rtp_session)
|
2006-04-03 21:00:13 +00:00
|
|
|
{
|
|
|
|
return rtp_session->private_data;
|
|
|
|
}
|
2006-11-27 22:30:48 +00:00
|
|
|
|
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
2008-02-03 22:14:57 +00:00
|
|
|
* indent-tabs-mode:t
|
2006-11-27 22:30:48 +00:00
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
|
|
|
|
*/
|