; exosip/!myrealm!1000@dest
;srtp:myrealm => ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+; VAD choose one (out is a good choice);
+; vad => in
+; vad => out
+; vad => both
;---- WOOMERA PROTOCOL
;--------------------------------------------------------------------------------
;rtp-ip => my_lan_ip
;ext-rtp-ip => stun:stun.server.com
exten => 1000
+
+; VAD choose one
+; vad => in
+; vad => out
+; vad => both
char *ani;
/*! ANI II (when applicable) */
char *ani2;
+ /*! RDNIS */
+ char *rdnis;
/*! Destination Number */
char *destination_number;
+ /*! channel type */
+ char *source;
/*! channel name */
char *chan_name;
/*! unique id */
char *network_addr,
char *ani,
char *ani2,
+ char *rdnis,
+ char *source,
char *destination_number);
/*!
*/
SWITCH_DECLARE(int) switch_rtp_write(switch_rtp *rtp_session, void *data, uint32_t datalen, uint32_t ts, switch_frame_flag *flags);
+/*!
+ \brief Enable VAD on an RTP Session
+ \param rtp_session the RTP session
+ \param session the core session associated with the RTP session
+ \param codec the codec the channel is currenty using
+ \param flags flags for control
+ \return SWITCH_STAUTS_SUCCESS on success
+*/
+SWITCH_DECLARE(switch_status) switch_rtp_enable_vad(switch_rtp *rtp_session, switch_core_session *session, switch_codec *codec, switch_vad_flag_t flags);
+
+/*!
+ \brief Disable VAD on an RTP Session
+ \param rtp_session the RTP session
+ \return SWITCH_STAUTS_SUCCESS on success
+*/
+SWITCH_DECLARE(switch_status) switch_rtp_disable_vad(switch_rtp *rtp_session);
+
/*!
\brief Write data to a given RTP session
\param rtp_session the RTP session to write to
#define SWITCH_TRUE 1
#define SWITCH_FALSE 0
+/*!
+ \enum switch_vad_flags
+ \brief RTP Related Flags
+<pre>
+ SWITCH_VAD_FLAG_TALKING - Currently Talking
+ SWITCH_VAD_FLAG_EVENTS_TALK - Fire events when talking is detected
+ SWITCH_VAD_FLAG_EVENTS_NOTALK - Fire events when not talking is detected
+</pre>
+ */
+typedef enum {
+ SWITCH_VAD_FLAG_TALKING = ( 1 << 0 ),
+ SWITCH_VAD_FLAG_EVENTS_TALK = ( 1 << 1 ),
+ SWITCH_VAD_FLAG_EVENTS_NOTALK = ( 1 << 2 ),
+} switch_vad_flag_t;
+
+
/*!
\enum switch_rtp_flag_t
\brief RTP Related Flags
<pre>
- SWITCH_RTP_FLAG_NOBLOCK - Do not block
- SWITCH_RTP_FLAG_IO - IO is ready
- SWITCH_RTP_FLAG_USE_TIMER - Timeout Reads and replace with a CNG Frame
- SWITCH_RTP_FLAG_SECURE - Secure RTP
- SWITCH_RTP_FLAG_AUTOADJ - Auto-Adjust the dest based on the source
- SWITCH_RTP_FLAG_RAW_WRITE - Try to forward packets unscathed
- SWITCH_RTP_FLAG_GOOGLEHACK - Convert payload from 102 to 97
+ SWITCH_RTP_FLAG_NOBLOCK - Do not block
+ SWITCH_RTP_FLAG_IO - IO is ready
+ SWITCH_RTP_FLAG_USE_TIMER - Timeout Reads and replace with a CNG Frame
+ SWITCH_RTP_FLAG_TIMER_RECLOCK - Resync the timer to the current clock on slips
+ SWITCH_RTP_FLAG_SECURE - Secure RTP
+ SWITCH_RTP_FLAG_AUTOADJ - Auto-Adjust the dest based on the source
+ SWITCH_RTP_FLAG_RAW_WRITE - Try to forward packets unscathed
+ SWITCH_RTP_FLAG_GOOGLEHACK - Convert payload from 102 to 97
+ SWITCH_RTP_FLAG_VAD - Enable VAD
</pre>
*/
typedef enum {
SWITCH_RTP_FLAG_NOBLOCK = ( 1 << 0),
SWITCH_RTP_FLAG_IO = (1 << 1),
SWITCH_RTP_FLAG_USE_TIMER = (1 << 2),
- SWITCH_RTP_FLAG_SECURE = (1 << 3),
- SWITCH_RTP_FLAG_AUTOADJ = (1 << 4),
- SWITCH_RTP_FLAG_RAW_WRITE = (1 << 5),
- SWITCH_RTP_FLAG_GOOGLEHACK = (1 << 6)
+ SWITCH_RTP_FLAG_TIMER_RECLOCK = (1 << 3),
+ SWITCH_RTP_FLAG_SECURE = (1 << 4),
+ SWITCH_RTP_FLAG_AUTOADJ = (1 << 5),
+ SWITCH_RTP_FLAG_RAW_WRITE = (1 << 6),
+ SWITCH_RTP_FLAG_GOOGLEHACK = (1 << 7),
+ SWITCH_RTP_FLAG_VAD = (1 << 8)
} switch_rtp_flag_t;
/*!
SWITCH_EVENT_SHUTDOWN - The system has been shutdown
SWITCH_EVENT_PUBLISH - Publish
SWITCH_EVENT_UNPUBLISH - UnPublish
+ SWITCH_EVENT_TALK - Talking Detected
+ SWITCH_EVENT_NOTALK - Not Talking Detected
SWITCH_EVENT_ALL - All events at once
</pre>
SWITCH_EVENT_SHUTDOWN,
SWITCH_EVENT_PUBLISH,
SWITCH_EVENT_UNPUBLISH,
+ SWITCH_EVENT_TALK,
+ SWITCH_EVENT_NOTALK,
SWITCH_EVENT_ALL
} switch_event_t;
caller_caller_profile->dialplan,
caller_caller_profile->caller_id_name,
caller_caller_profile->caller_id_number,
- caller_caller_profile->network_addr, NULL, NULL, chan_data);
+ caller_caller_profile->network_addr,
+ NULL,
+ NULL,
+ caller_caller_profile->rdnis,
+ caller_caller_profile->source,
+ chan_data);
skip = 1;
} else if (!strcasecmp(exten_name, "inbound") && switch_channel_test_flag(channel, CF_OUTBOUND)) {
skip = 1;
+ } else if (*exten_name == 's' && *(exten_name+1) == ':') {
+ exten_name += 2;
+ if (strcasecmp(exten_name, caller_profile->source)) {
+ skip = 1;
+ }
}
}
TFLAG_CODEC_READY = (1 << 8),
TFLAG_TRANSPORT = (1 << 9),
TFLAG_ANSWER = (1 << 10),
+ TFLAG_VAD_IN = ( 1 << 11),
+ TFLAG_VAD_OUT = ( 1 << 12),
+ TFLAG_VAD = ( 1 << 13)
} TFLAGS;
typedef enum {
char *extip;
char *lanaddr;
char *exten;
- unsigned int flags;
ldl_handle_t *handle;
+ unsigned int flags;
};
struct private_object {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "RTP ERROR %s\n", err);
switch_channel_hangup(channel);
return -1;
+ } else {
+ uint8_t vad_in = switch_test_flag(tech_pvt, TFLAG_VAD_IN) ? 1 : 0;
+ uint8_t vad_out = switch_test_flag(tech_pvt, TFLAG_VAD_OUT) ? 1 : 0;
+ uint8_t inb = switch_test_flag(tech_pvt, TFLAG_OUTBOUND) ? 0 : 1;
+ switch_rtp_activate_ice(tech_pvt->rtp_session, tech_pvt->remote_user, tech_pvt->local_user);
+ if ((vad_in && inb) || (vad_out && !inb)) {
+ switch_rtp_enable_vad(tech_pvt->rtp_session, tech_pvt->session, &tech_pvt->read_codec, SWITCH_VAD_FLAG_TALKING);
+ switch_set_flag(tech_pvt, TFLAG_VAD);
+ }
}
- switch_rtp_activate_ice(tech_pvt->rtp_session, tech_pvt->remote_user, tech_pvt->local_user);
-
return 0;
}
switch_core_session_add_stream(*new_session, NULL);
if ((tech_pvt = (struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object))) != 0) {
memset(tech_pvt, 0, sizeof(*tech_pvt));
+ tech_pvt->flags |= globals.flags;
+ tech_pvt->flags |= mdl_profile->flags;
channel = switch_core_session_get_channel(*new_session);
switch_core_session_set_private(*new_session, tech_pvt);
tech_pvt->session = *new_session;
globals.codec_rates_last =
switch_separate_string(globals.codec_rates_string, ',', globals.codec_rates, SWITCH_MAX_CODECS);
}
+
} else if (!strcasecmp(cfg.category, "interface")) {
if (!globals.init) {
ldl_global_init(globals.debug);
profile->lanaddr = switch_core_strdup(module_pool, val);
} else if (!strcmp(var, "exten")) {
profile->exten = switch_core_strdup(module_pool, val);
+ } else if (!strcmp(var, "vad")) {
+ if (!strcasecmp(val, "in")) {
+ switch_set_flag(profile, TFLAG_VAD_IN);
+ } else if (!strcasecmp(val, "out")) {
+ switch_set_flag(profile, TFLAG_VAD_OUT);
+ } else if (!strcasecmp(val, "both")) {
+ switch_set_flag(profile, TFLAG_VAD_IN);
+ switch_set_flag(profile, TFLAG_VAD_OUT);
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invald option %s for VAD\n", val);
+ }
}
}
}
switch_core_session_add_stream(session, NULL);
if ((tech_pvt = (struct private_object *) switch_core_session_alloc(session, sizeof(struct private_object))) != 0) {
memset(tech_pvt, 0, sizeof(*tech_pvt));
+ tech_pvt->flags |= globals.flags;
+ tech_pvt->flags |= profile->flags;
channel = switch_core_session_get_channel(session);
switch_core_session_set_private(session, tech_pvt);
tech_pvt->session = session;
tech_pvt->profile = profile;
tech_pvt->local_port = switch_rtp_request_port();
switch_set_flag(tech_pvt, TFLAG_ANSWER);
+
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Hey where is my memory pool?\n");
switch_core_session_destroy(&session);
ldl_session_get_ip(dlsession),
NULL,
NULL,
+ NULL,
+ (char *)modname,
profile->exten)) != 0) {
char name[128];
snprintf(name, sizeof(name), "DingaLing/%s-%04x", tech_pvt->caller_profile->destination_number,
TFLAG_BYE = (1 << 8),
TFLAG_ANS = (1 << 9),
TFLAG_EARLY_MEDIA = (1 << 10),
- TFLAG_SECURE = (1 << 11)
-
+ TFLAG_SECURE = (1 << 11),
+ TFLAG_VAD_IN = ( 1 << 12),
+ TFLAG_VAD_OUT = ( 1 << 13),
+ TFLAG_VAD = ( 1 << 14)
} TFLAGS;
tech_pvt->read_codec.codec_interface->ianacode,
tech_pvt->read_codec.implementation->encoded_bytes_per_frame,
ms,
- SWITCH_RTP_FLAG_NOBLOCK | SWITCH_RTP_FLAG_RAW_WRITE,
+ SWITCH_RTP_FLAG_USE_TIMER | SWITCH_RTP_FLAG_TIMER_RECLOCK | SWITCH_RTP_FLAG_RAW_WRITE,
key,
&err, switch_core_session_get_pool(tech_pvt->session));
if (tech_pvt->rtp_session) {
+ uint8_t vad_in = switch_test_flag(tech_pvt, TFLAG_VAD_IN) ? 1 : 0;
+ uint8_t vad_out = switch_test_flag(tech_pvt, TFLAG_VAD_OUT) ? 1 : 0;
+ uint8_t inb = switch_test_flag(tech_pvt, TFLAG_OUTBOUND) ? 0 : 1;
+
tech_pvt->ssrc = switch_rtp_get_ssrc(tech_pvt->rtp_session);
switch_set_flag(tech_pvt, TFLAG_RTP);
+
+ if ((vad_in && inb) || (vad_out && !inb)) {
+ switch_rtp_enable_vad(tech_pvt->rtp_session, tech_pvt->session, &tech_pvt->read_codec, SWITCH_VAD_FLAG_TALKING);
+ switch_set_flag(tech_pvt, TFLAG_VAD);
+ }
} else {
switch_channel *channel = switch_core_session_get_channel(tech_pvt->session);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "RTP REPORTS ERROR: [%s]\n", err);
if ((tech_pvt =
(struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object))) != 0) {
memset(tech_pvt, 0, sizeof(*tech_pvt));
+ tech_pvt->flags = globals.flags;
channel = switch_core_session_get_channel(*new_session);
switch_core_session_set_private(*new_session, tech_pvt);
tech_pvt->session = *new_session;
switch_core_session_add_stream(session, NULL);
if ((tech_pvt = (struct private_object *) switch_core_session_alloc(session, sizeof(struct private_object))) != 0) {
memset(tech_pvt, 0, sizeof(*tech_pvt));
+ tech_pvt->flags = globals.flags;
channel = switch_core_session_get_channel(session);
switch_core_session_set_private(session, tech_pvt);
tech_pvt->session = session;
tech_pvt->realm = switch_core_session_strdup(session, osip_header_get_value(tedious));
}
-
-
if (!(from = osip_message_get_from(event->request))) {
switch_core_session_destroy(&session);
return SWITCH_STATUS_MEMERR;
displayname,
username,
event->request->from->url->host,
- NULL, NULL, event->request->req_uri->username)) != 0) {
+ NULL,
+ NULL,
+ NULL,
+ (char *)modname,
+ event->request->req_uri->username)) != 0) {
switch_channel_set_caller_profile(channel, tech_pvt->caller_profile);
}
globals.debug = atoi(val);
} else if (!strcmp(var, "port")) {
globals.port = atoi(val);
+ } else if (!strcmp(var, "vad")) {
+ if (!strcasecmp(val, "in")) {
+ switch_set_flag(&globals, TFLAG_VAD_IN);
+ } else if (!strcasecmp(val, "out")) {
+ switch_set_flag(&globals, TFLAG_VAD_OUT);
+ } else if (!strcasecmp(val, "both")) {
+ switch_set_flag(&globals, TFLAG_VAD_IN);
+ switch_set_flag(&globals, TFLAG_VAD_OUT);
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invald option %s for VAD\n", val);
+ }
} else if (!strcmp(var, "ext-rtp-ip")) {
set_global_extrtpip(val);
} else if (!strcmp(var, "rtp-ip")) {
iaxevent->ies.calling_number,
iax_get_peer_ip(iaxevent->session),
iaxevent->ies.calling_ani,
- NULL, iaxevent->ies.called_number)) != 0) {
+ NULL,
+ NULL,
+ (char *)modname,
+ iaxevent->ies.called_number)) != 0) {
char name[128];
snprintf(name, sizeof(name), "IAX/%s-%04x", tech_pvt->caller_profile->destination_number,
rand() & 0xffff);
if ((tech_pvt->caller_profile = switch_caller_profile_new(switch_core_session_get_pool(session),
globals.dialplan,
globals.cid_name,
- globals.cid_num, NULL, NULL, NULL, dest)) != 0) {
+ globals.cid_num, NULL, NULL, NULL, NULL, (char *)modname, dest)) != 0) {
char name[128];
snprintf(name, sizeof(name), "PortAudio/%s-%04x",
tech_pvt->caller_profile->destination_number ? tech_pvt->caller_profile->
event->ring.callingani,
switch_strlen_zero(ani2str) ? NULL : ani2str,
NULL,
+ NULL,
+ (char *)modname,
event->ring.callednum))) {
switch_channel_set_caller_profile(channel, tech_pvt->caller_profile);
}
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
switch_frame *pframe;
+ switch_size_t len;
channel = switch_core_session_get_channel(session);
assert(channel != NULL);
pframe = &tech_pvt->frame;
*frame = pframe;
- pframe->datalen = sizeof(tech_pvt->databuf);
- if (switch_socket_recvfrom(tech_pvt->udpread, tech_pvt->udp_socket, 0, tech_pvt->databuf, &pframe->datalen) == SWITCH_STATUS_SUCCESS) {
+ len = sizeof(tech_pvt->databuf);
+ if (switch_socket_recvfrom(tech_pvt->udpread, tech_pvt->udp_socket, 0, tech_pvt->databuf, &len) == SWITCH_STATUS_SUCCESS) {
+ pframe->datalen = len;
pframe->samples = (int) pframe->datalen / 2;
return SWITCH_STATUS_SUCCESS;
}
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
+ switch_size_t len;
//switch_frame *pframe;
-
+
channel = switch_core_session_get_channel(session);
assert(channel != NULL);
}
//pframe = &tech_pvt->frame;
- if (switch_socket_sendto(tech_pvt->udp_socket, tech_pvt->udpwrite, 0, frame->data, &frame->datalen) == SWITCH_STATUS_SUCCESS) {
+ len = frame->datalen;
+ if (switch_socket_sendto(tech_pvt->udp_socket, tech_pvt->udpwrite, 0, frame->data, &len) == SWITCH_STATUS_SUCCESS) {
+ frame->datalen = len;
return SWITCH_STATUS_SUCCESS;
}
if ((tech_pvt->caller_profile = switch_caller_profile_new(switch_core_session_get_pool(session),
tech_pvt->profile->dialplan,
- cid_name, cid_num, ip, NULL, NULL, exten)) != 0) {
+ cid_name, cid_num, ip, NULL, NULL, NULL, (char *)modname, exten)) != 0) {
char name[128];
snprintf(name, sizeof(name), "Woomera/%s-%04x", tech_pvt->caller_profile->destination_number,
rand() & 0xffff);
char *network_addr = "";
char *ani = "";
char *ani2 = "";
+ char *rdnis = "";
*rval = BOOLEAN_TO_JSVAL( JS_FALSE );
if (argc > 8) {
ani2 = JS_GetStringBytes(JS_ValueToString(cx, argv[8]));
}
+ if (argc > 9) {
+ rdnis = JS_GetStringBytes(JS_ValueToString(cx, argv[9]));
+ }
if (switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "OH OH no pool\n");
return JS_FALSE;
}
- caller_profile = switch_caller_profile_new(pool, dialplan, cid_name, cid_num, network_addr, ani, ani2, dest);
+ caller_profile = switch_caller_profile_new(pool, dialplan, cid_name, cid_num, network_addr, ani, ani2, rdnis, (char *)modname, dest);
if (switch_core_session_outgoing_channel(session, channel_type, caller_profile, &peer_session, pool) == SWITCH_STATUS_SUCCESS) {
jss = switch_core_session_alloc(peer_session, sizeof(*jss));
jss->session = peer_session;
char *caller_id_name,
char *caller_id_number,
char *network_addr,
- char *ani, char *ani2, char *destination_number)
+ char *ani,
+ char *ani2,
+ char *rdnis,
+ char *source,
+ char *destination_number)
{
profile->network_addr = switch_core_strdup(pool, network_addr);
profile->ani = switch_core_strdup(pool, ani);
profile->ani2 = switch_core_strdup(pool, ani2);
+ profile->rdnis = switch_core_strdup(pool, rdnis);
+ profile->source = switch_core_strdup(pool, source);
profile->destination_number = switch_core_strdup(pool, destination_number);
}
}
-SWITCH_DECLARE(switch_caller_profile *) switch_caller_profile_clone(switch_core_session *session,
- switch_caller_profile *tocopy)
+SWITCH_DECLARE(switch_caller_profile *) switch_caller_profile_clone(switch_core_session *session, switch_caller_profile *tocopy)
+
{
switch_caller_profile *profile = NULL;
if ((profile = switch_core_session_alloc(session, sizeof(switch_caller_profile))) != 0) {
profile->ani2 = switch_core_session_strdup(session, tocopy->ani2);
profile->caller_id_number = switch_core_session_strdup(session, tocopy->caller_id_number);
profile->network_addr = switch_core_session_strdup(session, tocopy->network_addr);
+ profile->rdnis = switch_core_session_strdup(session, tocopy->rdnis);
profile->destination_number = switch_core_session_strdup(session, tocopy->destination_number);
profile->uuid = switch_core_session_strdup(session, tocopy->uuid);
+ profile->source = switch_core_session_strdup(session, tocopy->source);
profile->chan_name = switch_core_session_strdup(session, tocopy->chan_name);
}
snprintf(header_name, sizeof(header_name), "%s-Unique-ID", prefix);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, header_name, caller_profile->uuid);
}
+ if (caller_profile->source) {
+ snprintf(header_name, sizeof(header_name), "%s-RDNIS", prefix);
+ switch_event_add_header(event, SWITCH_STACK_BOTTOM, header_name, caller_profile->source);
+ }
+ if (caller_profile->rdnis) {
+ snprintf(header_name, sizeof(header_name), "%s-RDNIS", prefix);
+ switch_event_add_header(event, SWITCH_STACK_BOTTOM, header_name, caller_profile->rdnis);
+ }
if (caller_profile->chan_name) {
snprintf(header_name, sizeof(header_name), "%s-Channel-Name", prefix);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, header_name, caller_profile->chan_name);
uint32_t encoded_data_len,
uint32_t encoded_rate,
void *decoded_data,
- uint32_t *decoded_data_len, uint32_t *decoded_rate, unsigned int *flag)
+ uint32_t *decoded_data_len,
+ uint32_t *decoded_rate,
+ unsigned int *flag)
{
assert(codec != NULL);
"SHUTDOWN",
"PUBLISH",
"UNPUBLISH",
+ "TALK",
+ "NOTALK",
"ALL"
};
char body[SWITCH_RTP_MAX_BUF_LEN];
} rtp_msg_t;
+
+struct switch_rtp_vad_data {
+ switch_core_session *session;
+ switch_codec vad_codec;
+ switch_codec *read_codec;
+ 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;
+};
+
struct switch_rtp {
switch_socket_t *sock;
uint32_t remote_port;
uint8_t stuncount;
switch_buffer *packet_buffer;
+ struct switch_rtp_vad_data vad_data;
};
static int global_init = 0;
switch_rtp_kill_socket(*rtp_session);
switch_socket_close((*rtp_session)->sock);
+ if (switch_test_flag((*rtp_session), SWITCH_RTP_FLAG_VAD)) {
+ switch_rtp_disable_vad(*rtp_session);
+ }
if (switch_test_flag((*rtp_session), SWITCH_RTP_FLAG_SECURE)) {
srtp_dealloc((*rtp_session)->recv_ctx);
srtp_dealloc((*rtp_session)->send_ctx);
rtp_session->recv_msg.header.pt = SWITCH_RTP_CNG_PAYLOAD;
*flags |= SFF_CNG;
/* Set the next waypoint and return a CNG frame */
- rtp_session->next_read += rtp_session->ms_per_packet;
+ if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_TIMER_RECLOCK)) {
+ rtp_session->next_read = switch_time_now() + rtp_session->ms_per_packet;
+ } else {
+ rtp_session->next_read += rtp_session->ms_per_packet;
+ }
*payload_type = SWITCH_RTP_CNG_PAYLOAD;
return SWITCH_RTP_CNG_PAYLOAD;
}
uint8_t packetize = (rtp_session->packet_size > datalen && (payload == rtp_session->payload)) ? 1 : 0;
uint8_t fwd = (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_RAW_WRITE) && (*flags & SFF_RAW_RTP)) ? 1 : 0;
rtp_msg_t *send_msg;
+ uint8_t send = 1;
if (fwd) {
bytes = datalen;
} else {
send_msg = &rtp_session->send_msg;
send_msg->header.pt = payload;
- send_msg->header.m = m;
+ send_msg->header.m = m ? 1 : 0;
if (packetize) {
if (!rtp_session->packet_buffer) {
if (switch_buffer_create(rtp_session->pool, &rtp_session->packet_buffer, rtp_session->packet_size * 2) != SWITCH_STATUS_SUCCESS) {
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_GOOGLEHACK) && rtp_session->send_msg.header.pt == 97) {
rtp_session->recv_msg.header.pt = 102;
}
- switch_socket_sendto(rtp_session->sock, rtp_session->remote_addr, 0, (void*)send_msg, &bytes);
+ if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_VAD)) {
+ int16_t decoded[SWITCH_RECCOMMENDED_BUFFER_SIZE/sizeof(int16_t)];
+ uint32_t rate;
+ uint32_t flags;
+ uint32_t len = sizeof(decoded);
+ send = 0;
+ time_t now = time(NULL);
+
+ 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;
+ //printf("RESCAN\n");
+ }
+
+ if (switch_core_codec_decode(&rtp_session->vad_data.vad_codec,
+ rtp_session->vad_data.read_codec,
+ data,
+ datalen,
+ rtp_session->vad_data.read_codec->implementation->samples_per_second,
+ decoded,
+ &len,
+ &rate,
+ &flags) == SWITCH_STATUS_SUCCESS) {
+
+ double energy = 0;
+ uint32_t x, y = 0, z = len / sizeof(int16_t);
+ uint32_t score = 0;
+
+ if (z) {
+ for (x = 0; x < z; x++) {
+ energy += abs(decoded[y] * 1.0);
+ y += rtp_session->vad_data.read_codec->implementation->number_of_channels;
+ }
+
+ if (++rtp_session->vad_data.start_count < rtp_session->vad_data.start) {
+ send = 1;
+ } else {
+ score = energy / z;
+ 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;
+ rtp_session->vad_data.bg_level += (rtp_session->vad_data.bg_level / 3);
+ //printf("AVG %u\n", rtp_session->vad_data.bg_level);
+ }
+ send = 1;
+ } else {
+ if (score > rtp_session->vad_data.bg_level) {
+ uint32_t diff = score - rtp_session->vad_data.bg_level;
+
+ if (rtp_session->vad_data.hangover_hits) {
+ rtp_session->vad_data.hangover_hits--;
+ }
+
+ if (diff >= rtp_session->vad_data.diff_level || ++rtp_session->vad_data.hangunder_hits >= rtp_session->vad_data.hangunder) {
+ switch_set_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING);
+ send_msg->header.m = 1;
+ rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
+ if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_EVENTS_TALK)) {
+ switch_event *event;
+ if (switch_event_create(&event, SWITCH_EVENT_TALK) == SWITCH_STATUS_SUCCESS) {
+ switch_channel *channel = switch_core_session_get_channel(rtp_session->vad_data.session);
+ switch_channel_event_set_data(channel, event);
+ switch_event_fire(&event);
+ }
+ }
+ }
+ } 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);
+ rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
+ if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_EVENTS_NOTALK)) {
+ switch_event *event;
+ if (switch_event_create(&event, SWITCH_EVENT_NOTALK) == SWITCH_STATUS_SUCCESS) {
+ switch_channel *channel = switch_core_session_get_channel(rtp_session->vad_data.session);
+ switch_channel_event_set_data(channel, event);
+ switch_event_fire(&event);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
+ send = 1;
+ } else {
+ if (++rtp_session->vad_data.cng_count >= rtp_session->vad_data.cng_freq) {
+ rtp_session->send_msg.header.pt = SWITCH_RTP_CNG_PAYLOAD;
+ memset(rtp_session->send_msg.body, 255, SWITCH_RTP_CNG_PAYLOAD);
+ rtp_session->send_msg.header.ts = htonl(rtp_session->vad_data.ts);
+ rtp_session->vad_data.ts++;
+ bytes = SWITCH_RTP_CNG_PAYLOAD;
+ send = 1;
+ rtp_session->vad_data.cng_count = 0;
+ }
+ }
+
+ }
+ } else {
+ return SWITCH_STATUS_GENERR;
+ }
+ }
+
+ if (send) {
+ switch_socket_sendto(rtp_session->sock, rtp_session->remote_addr, 0, (void*)send_msg, &bytes);
+ }
+
if (rtp_session->ice_user) {
if (ice_out(rtp_session) != SWITCH_STATUS_SUCCESS) {
return -1;
}
+
+SWITCH_DECLARE(switch_status) switch_rtp_disable_vad(switch_rtp *rtp_session)
+{
+ if (!switch_test_flag(rtp_session, SWITCH_RTP_FLAG_VAD)) {
+ return SWITCH_STATUS_GENERR;
+ }
+ switch_core_codec_destroy(&rtp_session->vad_data.vad_codec);
+ switch_clear_flag(rtp_session, SWITCH_RTP_FLAG_VAD);
+ return SWITCH_STATUS_SUCCESS;
+}
+
+SWITCH_DECLARE(switch_status) switch_rtp_enable_vad(switch_rtp *rtp_session, switch_core_session *session, switch_codec *codec, switch_vad_flag_t flags)
+{
+ 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));
+
+ if (switch_core_codec_init(&rtp_session->vad_data.vad_codec,
+ codec->codec_interface->iananame,
+ codec->implementation->samples_per_second,
+ codec->implementation->microseconds_per_frame / 1000,
+ codec->implementation->number_of_channels,
+ SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
+ NULL,
+ rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't load codec?\n");
+ return SWITCH_STATUS_FALSE;
+ }
+
+ rtp_session->vad_data.diff_level = 400;
+ rtp_session->vad_data.hangunder = 15;
+ rtp_session->vad_data.hangover = 40;
+ rtp_session->vad_data.bg_len = 20;
+ 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;
+ rtp_session->vad_data.start = 5;
+ rtp_session->vad_data.next_scan = time(NULL);
+ rtp_session->vad_data.scan_freq = 0;
+ switch_set_flag(rtp_session, SWITCH_RTP_FLAG_VAD);
+
+ return SWITCH_STATUS_SUCCESS;
+}
+
SWITCH_DECLARE(int) switch_rtp_write(switch_rtp *rtp_session, void *data, uint32_t datalen, uint32_t ts, switch_frame_flag *flags)
{