FS-9039: [avmd] Use FS enumeration

Now initialization function returns switch_status_t.
This commit is contained in:
Piotr Gregor 2016-04-09 19:50:41 +01:00
parent 07b200cdd4
commit 68c2c5d485
1 changed files with 71 additions and 85 deletions

View File

@ -57,13 +57,13 @@
#define SAMPLES_PER_MS(r, m) ((r) / (1000/(m))) #define SAMPLES_PER_MS(r, m) ((r) / (1000/(m)))
/*! Minimum beep length */ /*! Minimum beep length */
#define BEEP_TIME (2) #define BEEP_TIME (2)
/*! How often to evaluate the output of desa2 in ms */ /*! How often to evaluate the output of DESA-2 in ms */
#define SINE_TIME (2*0.125) #define SINE_TIME (2*0.125)
/*! How long in samples does desa2 results get evaluated */ /*! How long in samples does DESA-2 results get evaluated */
#define SINE_LEN(r) SAMPLES_PER_MS((r), SINE_TIME) #define SINE_LEN(r) SAMPLES_PER_MS((r), SINE_TIME)
/*! How long in samples is the minimum beep length */ /*! How long in samples is the minimum beep length */
#define BEEP_LEN(r) SAMPLES_PER_MS((r), BEEP_TIME) #define BEEP_LEN(r) SAMPLES_PER_MS((r), BEEP_TIME)
/*! Number of points in desa2 sample */ /*! Number of points in DESA-2 sample */
#define P (5) #define P (5)
/*! Guesstimate frame length in ms */ /*! Guesstimate frame length in ms */
#define FRAME_TIME (20) #define FRAME_TIME (20)
@ -83,7 +83,7 @@
* for 8kHz audio. All the frequencies above 0.25 sampling rate * for 8kHz audio. All the frequencies above 0.25 sampling rate
* will be aliased to some frequency below that threshold. * will be aliased to some frequency below that threshold.
* This is not a problem here as we are interested in detection * This is not a problem here as we are interested in detection
* of any constant amplitude anf frequency sine wave instead * of any constant amplitude and frequency sine wave instead
* of detection of particular frequency. * of detection of particular frequency.
*/ */
#define MAX_FREQUENCY (2500.0) #define MAX_FREQUENCY (2500.0)
@ -152,16 +152,14 @@ typedef struct {
static void avmd_process(avmd_session_t *session, switch_frame_t *frame); static void avmd_process(avmd_session_t *session, switch_frame_t *frame);
static switch_bool_t avmd_callback(switch_media_bug_t * bug, static switch_bool_t avmd_callback(switch_media_bug_t * bug,
void *user_data, switch_abc_type_t type); void *user_data, switch_abc_type_t type);
static int init_avmd_session_data(avmd_session_t *avmd_session,
switch_core_session_t *fs_session);
/*! \brief The avmd session data initialization function. /*! \brief The avmd session data initialization function.
* @author Eric des Courtis * @author Eric des Courtis
* @param avmd_session A reference to a avmd session. * @param avmd_session A reference to a avmd session.
* @param fs_session A reference to a FreeSWITCH session. * @param fs_session A reference to a FreeSWITCH session.
*/ */
static int init_avmd_session_data(avmd_session_t *avmd_session, static switch_status_t
init_avmd_session_data(avmd_session_t *avmd_session,
switch_core_session_t *fs_session) switch_core_session_t *fs_session)
{ {
size_t buf_sz; size_t buf_sz;
@ -173,7 +171,7 @@ static int init_avmd_session_data(avmd_session_t *avmd_session,
(size_t)FRAME_LEN(avmd_session->rate), (size_t)FRAME_LEN(avmd_session->rate),
fs_session); fs_session);
if (avmd_session->b.buf == NULL) { if (avmd_session->b.buf == NULL) {
return -1; return SWITCH_STATUS_MEMERR;
} }
avmd_session->session = fs_session; avmd_session->session = fs_session;
avmd_session->pos = 0; avmd_session->pos = 0;
@ -187,21 +185,21 @@ static int init_avmd_session_data(avmd_session_t *avmd_session,
buf_sz = BEEP_LEN((uint32_t)avmd_session->rate) / (uint32_t)SINE_LEN(avmd_session->rate); buf_sz = BEEP_LEN((uint32_t)avmd_session->rate) / (uint32_t)SINE_LEN(avmd_session->rate);
if (buf_sz < 1) { if (buf_sz < 1) {
return -2; return SWITCH_STATUS_MORE_DATA;
} }
INIT_SMA_BUFFER(&avmd_session->sma_b, buf_sz, fs_session); INIT_SMA_BUFFER(&avmd_session->sma_b, buf_sz, fs_session);
if (avmd_session->sma_b.data == NULL) { if (avmd_session->sma_b.data == NULL) {
return -3; return SWITCH_STATUS_FALSE;
} }
memset(avmd_session->sma_b.data, 0, sizeof(BUFF_TYPE) * buf_sz); memset(avmd_session->sma_b.data, 0, sizeof(BUFF_TYPE) * buf_sz);
INIT_SMA_BUFFER(&avmd_session->sqa_b, buf_sz, fs_session); INIT_SMA_BUFFER(&avmd_session->sqa_b, buf_sz, fs_session);
if (avmd_session->sqa_b.data == NULL) { if (avmd_session->sqa_b.data == NULL) {
return -4; return SWITCH_STATUS_FALSE;
} }
memset(avmd_session->sqa_b.data, 0, sizeof(BUFF_TYPE) * buf_sz); memset(avmd_session->sqa_b.data, 0, sizeof(BUFF_TYPE) * buf_sz);
return 0; return SWITCH_STATUS_SUCCESS;
} }
@ -391,11 +389,10 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_avmd_load)
*/ */
SWITCH_STANDARD_APP(avmd_start_function) SWITCH_STANDARD_APP(avmd_start_function)
{ {
int res; switch_media_bug_t *bug;
switch_media_bug_t *bug; switch_status_t status;
switch_status_t status; switch_channel_t *channel;
switch_channel_t *channel; avmd_session_t *avmd_session;
avmd_session_t *avmd_session;
switch_media_bug_flag_t flags = 0; switch_media_bug_flag_t flags = 0;
if (session == NULL) { if (session == NULL) {
@ -426,34 +423,30 @@ SWITCH_STANDARD_APP(avmd_start_function)
avmd_session = (avmd_session_t *)switch_core_session_alloc( avmd_session = (avmd_session_t *)switch_core_session_alloc(
session, sizeof(avmd_session_t)); session, sizeof(avmd_session_t));
res = init_avmd_session_data(avmd_session, session); status = init_avmd_session_data(avmd_session, session);
if (res != 0) { if (status != SWITCH_STATUS_SUCCESS) {
switch (res) { switch (status) {
case -1: case SWITCH_STATUS_MEMERR:
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session),
SWITCH_LOG_ERROR, "Failed to init avmd session." SWITCH_LOG_ERROR, "Failed to init avmd session."
" Buffer error!\n"); " Buffer error!\n");
break; break;
case -2: case SWITCH_STATUS_MORE_DATA:
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session),
SWITCH_LOG_ERROR, "Failed to init avmd session." SWITCH_LOG_ERROR, "Failed to init avmd session."
" SMA buffer size is 0!\n"); " SMA buffer size is 0!\n");
break; break;
case -3: case SWITCH_STATUS_FALSE:
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session),
SWITCH_LOG_ERROR, "Failed to init avmd session." SWITCH_LOG_ERROR, "Failed to init avmd session."
" SMA buffer error\n"); " SMA buffers error\n");
break;
case -4:
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session),
SWITCH_LOG_ERROR, "Failed to init avmd session."
" SMA sqa buffer error\n");
break; break;
default: default:
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session),
SWITCH_LOG_ERROR, "Failed to init avmd session." SWITCH_LOG_ERROR, "Failed to init avmd session."
" Unknown error\n"); " Unknown error\n");
break; break;
} }
return; return;
} }
@ -534,24 +527,23 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_avmd_shutdown)
*/ */
SWITCH_STANDARD_API(avmd_api_main) SWITCH_STANDARD_API(avmd_api_main)
{ {
int res; switch_media_bug_t *bug;
switch_core_session_t *fs_session = NULL; avmd_session_t *avmd_session;
switch_media_bug_t *bug; switch_channel_t *channel;
avmd_session_t *avmd_session; int argc;
switch_channel_t *channel; char *argv[AVMD_PARAMS];
switch_status_t status; char *ccmd = NULL;
int argc; char *uuid, *uuid_dup;
char *argv[AVMD_PARAMS]; char *command;
char *ccmd = NULL;
char *uuid, *uuid_dup;
char *command;
switch_core_media_flag_t flags = 0; switch_core_media_flag_t flags = 0;
switch_status_t status = SWITCH_STATUS_SUCCESS;
switch_core_session_t *fs_session = NULL;
/* No command? Display usage */ /* No command? Display usage */
if (zstr(cmd)) { if (zstr(cmd)) {
stream->write_function(stream, "-ERR, bad command!\n" stream->write_function(stream, "-ERR, bad command!\n"
"-USAGE: %s\n\n", AVMD_SYNTAX); "-USAGE: %s\n\n", AVMD_SYNTAX);
return SWITCH_STATUS_SUCCESS; goto end;
} }
/* Duplicated contents of original string */ /* Duplicated contents of original string */
@ -587,7 +579,7 @@ SWITCH_STANDARD_API(avmd_api_main)
channel = switch_core_session_get_channel(fs_session); channel = switch_core_session_get_channel(fs_session);
if (channel == NULL) { if (channel == NULL) {
stream->write_function(stream, "-ERR, no channel for FreeSWITCH session [%s]!" stream->write_function(stream, "-ERR, no channel for FreeSWITCH session [%s]!"
"\n Please report this to the developers.\n\n", uuid); "\n Please report this to the developers\n\n", uuid);
goto end; goto end;
} }
@ -602,7 +594,7 @@ SWITCH_STANDARD_API(avmd_api_main)
switch_core_media_bug_remove(fs_session, &bug); switch_core_media_bug_remove(fs_session, &bug);
switch_safe_free(ccmd); switch_safe_free(ccmd);
#ifdef AVMD_REPORT_STATUS #ifdef AVMD_REPORT_STATUS
stream->write_function(stream, "+OK\n [%s] [%s] stopped.\n\n", stream->write_function(stream, "+OK\n [%s] [%s] stopped\n\n",
uuid_dup, switch_channel_get_name(channel)); uuid_dup, switch_channel_get_name(channel));
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session), SWITCH_LOG_INFO, switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session), SWITCH_LOG_INFO,
"Avmd on channel [%s] stopped!\n", switch_channel_get_name(channel)); "Avmd on channel [%s] stopped!\n", switch_channel_get_name(channel));
@ -623,7 +615,7 @@ SWITCH_STANDARD_API(avmd_api_main)
#ifdef AVMD_OUTBOUND_CHANNEL #ifdef AVMD_OUTBOUND_CHANNEL
if (SWITCH_CALL_DIRECTION_OUTBOUND != switch_channel_direction(channel)) { if (SWITCH_CALL_DIRECTION_OUTBOUND != switch_channel_direction(channel)) {
stream->write_function(stream, "-ERR, channel for FreeSWITCH session [%s]" stream->write_function(stream, "-ERR, channel for FreeSWITCH session [%s]"
"\n is not outbound.\n\n", uuid); "\n is not outbound\n\n", uuid);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session), SWITCH_LOG_WARNING, switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session), SWITCH_LOG_WARNING,
"Channel [%s] is not outbound!\n", switch_channel_get_name(channel)); "Channel [%s] is not outbound!\n", switch_channel_get_name(channel));
} else { } else {
@ -633,7 +625,7 @@ SWITCH_STANDARD_API(avmd_api_main)
#ifdef AVMD_INBOUND_CHANNEL #ifdef AVMD_INBOUND_CHANNEL
if (SWITCH_CALL_DIRECTION_INBOUND != switch_channel_direction(channel)) { if (SWITCH_CALL_DIRECTION_INBOUND != switch_channel_direction(channel)) {
stream->write_function(stream, "-ERR, channel for FreeSWITCH session [%s]" stream->write_function(stream, "-ERR, channel for FreeSWITCH session [%s]"
"\n is not inbound.\n\n", uuid); "\n is not inbound\n\n", uuid);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session), SWITCH_LOG_WARNING, switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session), SWITCH_LOG_WARNING,
"Channel [%s] is not inbound!\n", switch_channel_get_name(channel)); "Channel [%s] is not inbound!\n", switch_channel_get_name(channel));
} else { } else {
@ -671,37 +663,30 @@ SWITCH_STANDARD_API(avmd_api_main)
/* Allocate memory attached to this FreeSWITCH session for /* Allocate memory attached to this FreeSWITCH session for
* use in the callback routine and to store state information */ * use in the callback routine and to store state information */
avmd_session = (avmd_session_t *) switch_core_session_alloc( avmd_session = (avmd_session_t *) switch_core_session_alloc(
fs_session, sizeof(avmd_session_t)); fs_session, sizeof(avmd_session_t));
status = init_avmd_session_data(avmd_session, fs_session);
res = init_avmd_session_data(avmd_session, fs_session); if (status != SWITCH_STATUS_SUCCESS) {
if (res != 0) {
stream->write_function(stream, "-ERR, failed to initialize avmd session\n" stream->write_function(stream, "-ERR, failed to initialize avmd session\n"
" for FreeSWITCH session [%s]\n", uuid); " for FreeSWITCH session [%s]\n", uuid);
switch (res) { switch (status) {
case -1: case SWITCH_STATUS_MEMERR:
stream->write_function(stream, "-ERR, buffer error\n\n"); stream->write_function(stream, "-ERR, buffer error\n\n");
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session), switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session),
SWITCH_LOG_ERROR, "Failed to init avmd session." SWITCH_LOG_ERROR, "Failed to init avmd session."
" Buffer error!\n"); " Buffer error!\n");
break; break;
case -2: case SWITCH_STATUS_MORE_DATA:
stream->write_function(stream, "-ERR, SMA buffer size is 0\n\n"); stream->write_function(stream, "-ERR, SMA buffer size is 0\n\n");
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session), switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session),
SWITCH_LOG_ERROR, "Failed to init avmd session." SWITCH_LOG_ERROR, "Failed to init avmd session."
" SMA buffer size is 0!\n"); " SMA buffer size is 0!\n");
break; break;
case -3: case SWITCH_STATUS_FALSE:
stream->write_function(stream, "-ERR, SMA buffer error\n\n"); stream->write_function(stream, "-ERR, SMA buffer error\n\n");
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session), switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session),
SWITCH_LOG_ERROR, "Failed to init avmd session." SWITCH_LOG_ERROR, "Failed to init avmd session."
" SMA buffer error\n"); " SMA buffers error\n");
break;
case -4:
stream->write_function(stream, "-ERR, SMA sqa buffer error\n\n");
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session),
SWITCH_LOG_ERROR, "Failed to init avmd session."
" SMA sqa buffer error\n");
break; break;
default: default:
stream->write_function(stream, "-ERR, unknown error\n\n"); stream->write_function(stream, "-ERR, unknown error\n\n");
@ -744,6 +729,7 @@ SWITCH_STANDARD_API(avmd_api_main)
uuid, switch_channel_get_name(channel)); uuid, switch_channel_get_name(channel));
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session), SWITCH_LOG_INFO, switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fs_session), SWITCH_LOG_INFO,
"Avmd on channel [%s] started!\n", switch_channel_get_name(channel)); "Avmd on channel [%s] started!\n", switch_channel_get_name(channel));
switch_assert(status == SWITCH_STATUS_SUCCESS);
#endif #endif
end: end:
@ -765,38 +751,38 @@ end:
static void avmd_process(avmd_session_t *session, switch_frame_t *frame) static void avmd_process(avmd_session_t *session, switch_frame_t *frame)
{ {
int res; int res;
switch_event_t *event; switch_event_t *event;
switch_status_t status; switch_status_t status;
switch_event_t *event_copy; switch_event_t *event_copy;
switch_channel_t *channel; switch_channel_t *channel;
circ_buffer_t *b; circ_buffer_t *b;
size_t pos; size_t pos;
double omega; double omega;
#ifdef AVMD_DEBUG #ifdef AVMD_DEBUG
double f; double f;
#endif #endif
double v; double v;
double sma_digital_freq; double sma_digital_freq;
uint32_t sine_len_i; uint32_t sine_len_i;
char buf[AVMD_CHAR_BUF_LEN]; char buf[AVMD_CHAR_BUF_LEN];
int sample_to_skip_n = AVMD_SAMLPE_TO_SKIP_N; int sample_to_skip_n = AVMD_SAMLPE_TO_SKIP_N;
size_t sample_n = 0; size_t sample_n = 0;
b = &session->b; b = &session->b;
/* If beep has already been detected skip the CPU heavy stuff */ /* If beep has already been detected skip the CPU heavy stuff */
if (session->state.beep_state == BEEP_DETECTED) return; if (session->state.beep_state == BEEP_DETECTED) return;
/* Precompute values used heavily in the inner loop */ /* Precompute values used heavily in the inner loop */
sine_len_i = (uint32_t) SINE_LEN(session->rate); sine_len_i = (uint32_t) SINE_LEN(session->rate);
//sine_len = (double)sine_len_i; //sine_len = (double)sine_len_i;
//beep_len_i = BEEP_LEN(session->rate); //beep_len_i = BEEP_LEN(session->rate);
channel = switch_core_session_get_channel(session->session); channel = switch_core_session_get_channel(session->session);
/* Insert frame of 16 bit samples into buffer */ /* Insert frame of 16 bit samples into buffer */
INSERT_INT16_FRAME(b, (int16_t *)(frame->data), frame->samples); INSERT_INT16_FRAME(b, (int16_t *)(frame->data), frame->samples);
session->sample_count += frame->samples; session->sample_count += frame->samples;
/* INNER LOOP -- OPTIMIZATION TARGET */ /* INNER LOOP -- OPTIMIZATION TARGET */
@ -886,7 +872,7 @@ static void avmd_process(avmd_session_t *session, switch_frame_t *frame)
} }
/* DECISION */ /* DECISION */
/* If variance is less than threshold /* If variance is less than threshold
* and we have at least two estimates * and we have at least two estimates
* then we have detection */ * then we have detection */
#ifdef AVMD_REQUIRE_CONTINUOUS_STREAK #ifdef AVMD_REQUIRE_CONTINUOUS_STREAK