mod_spandsp: add start_tone_detect/stop_tone_detect app and api commands for tone and cadence detection (MODAPP-378)
This commit is contained in:
parent
e098d8757e
commit
a6e6514799
|
@ -0,0 +1,73 @@
|
|||
<configuration name="spandsp.conf" description="Tone detector descriptors">
|
||||
<descriptors>
|
||||
|
||||
<!-- These tones are defined in Annex to ITU Operational Bulletin No. 781 - 1.II.2003 -->
|
||||
<!-- Various Tones Used in National Networks (According to ITU-T Recommendation E.180)(03/1998) -->
|
||||
|
||||
<!-- North America -->
|
||||
<descriptor name="1">
|
||||
<tone name="CED_TONE">
|
||||
<element freq1="2100" freq2="0" min="500" max="0"/>
|
||||
</tone>
|
||||
<tone name="SIT">
|
||||
<element freq1="950" freq2="0" min="256" max="400"/>
|
||||
<element freq1="1400" freq2="0" min="256" max="400"/>
|
||||
<element freq1="1800" freq2="0" min="256" max="400"/>
|
||||
</tone>
|
||||
<tone name="REORDER_TONE">
|
||||
<element freq1="480" freq2="620" min="224" max="272"/>
|
||||
<element freq1="0" freq2="0" min="224" max="272"/>
|
||||
</tone>
|
||||
<tone name="BUSY_TONE">
|
||||
<element freq1="480" freq2="620" min="464" max="516"/>
|
||||
<element freq1="0" freq2="0" min="464" max="516"/>
|
||||
</tone>
|
||||
</descriptor>
|
||||
|
||||
<!-- United Kingdom -->
|
||||
<descriptor name="44">
|
||||
<tone name="CED_TONE">
|
||||
<element freq1="2100" freq2="0" min="500" max="0"/>
|
||||
</tone>
|
||||
<tone name="SIT">
|
||||
<element freq1="950" freq2="0" min="256" max="400"/>
|
||||
<element freq1="1400" freq2="0" min="256" max="400"/>
|
||||
<element freq1="1800" freq2="0" min="256" max="400"/>
|
||||
</tone>
|
||||
<tone name="REORDER_TONE">
|
||||
<element freq1="400" freq2="0" min="368" max="416"/>
|
||||
<element freq1="0" freq2="0" min="336" max="368"/>
|
||||
<element freq1="400" freq2="0" min="256" max="288"/>
|
||||
<element freq1="0" freq2="0" min="512" max="544"/>
|
||||
</tone>
|
||||
<tone name="BUSY_TONE">
|
||||
<element freq1="400" freq2="0" min="352" max="384"/>
|
||||
<element freq1="0" freq2="0" min="352" max="384"/>
|
||||
<element freq1="400" freq2="0" min="352" max="384"/>
|
||||
<element freq1="0" freq2="0" min="352" max="384"/>
|
||||
</tone>
|
||||
</descriptor>
|
||||
|
||||
<!-- Germany -->
|
||||
<descriptor name="49">
|
||||
<tone name="CED_TONE">
|
||||
<element freq1="2100" freq2="0" min="500" max="0"/>
|
||||
</tone>
|
||||
<tone name="SIT">
|
||||
<element freq1="900" freq2="0" min="256" max="400"/>
|
||||
<element freq1="1400" freq2="0" min="256" max="400"/>
|
||||
<element freq1="1800" freq2="0" min="256" max="400"/>
|
||||
</tone>
|
||||
<tone name="REORDER_TONE">
|
||||
<element freq1="425" freq2="0" min="224" max="272"/>
|
||||
<element freq1="0" freq2="0" min="224" max="272"/>
|
||||
</tone>
|
||||
<tone name="BUSY_TONE">
|
||||
<element freq1="425" freq2="0" min="464" max="516"/>
|
||||
<element freq1="0" freq2="0" min="464" max="516"/>
|
||||
</tone>
|
||||
</descriptor>
|
||||
</descriptors>
|
||||
|
||||
</configuration>
|
||||
|
|
@ -29,6 +29,7 @@
|
|||
* Brian West <brian@freeswitch.org>
|
||||
* Steve Underwood <steveu@coppice.org>
|
||||
* Antonio Gallo <agx@linux.it>
|
||||
* Christopher M. Rienzo <chris@rienzo.net>
|
||||
* mod_spandsp.c -- Module implementing spandsp fax, dsp, and codec functionality
|
||||
*
|
||||
*/
|
||||
|
@ -98,9 +99,91 @@ SWITCH_STANDARD_APP(t38_gateway_function)
|
|||
switch_ivr_tone_detect_session(session, "t38", "1100.0", "rw", timeout, 1, data, NULL, t38_gateway_start);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start tone detector application
|
||||
*
|
||||
* @param data the command string
|
||||
*/
|
||||
SWITCH_STANDARD_APP(start_tone_detect_app)
|
||||
{
|
||||
switch_channel_t *channel;
|
||||
if (!session) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No session\n");
|
||||
return;
|
||||
}
|
||||
channel = switch_core_session_get_channel(session);
|
||||
if (zstr(data)) {
|
||||
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "-ERR missing descriptor name");
|
||||
} else if (callprogress_detector_start(session, data) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "-ERR failed to start tone detector");
|
||||
} else {
|
||||
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "+OK started");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start tone detector API
|
||||
*/
|
||||
SWITCH_STANDARD_API(start_tone_detect_api)
|
||||
{
|
||||
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
||||
|
||||
if (zstr(cmd)) {
|
||||
stream->write_function(stream, "-ERR missing descriptor name\n");
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
if (!session) {
|
||||
stream->write_function(stream, "-ERR no session\n");
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
status = callprogress_detector_start(session, cmd);
|
||||
if (status == SWITCH_STATUS_SUCCESS) {
|
||||
stream->write_function(stream, "+OK started\n");
|
||||
} else {
|
||||
stream->write_function(stream, "-ERR failed to start tone detector\n");
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop tone detector application
|
||||
*
|
||||
* @param data the command string
|
||||
*/
|
||||
SWITCH_STANDARD_APP(stop_tone_detect_app)
|
||||
{
|
||||
switch_channel_t *channel;
|
||||
if (!session) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No session\n");
|
||||
return;
|
||||
}
|
||||
channel = switch_core_session_get_channel(session);
|
||||
callprogress_detector_stop(session);
|
||||
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "+OK stopped");
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop tone detector API
|
||||
*/
|
||||
SWITCH_STANDARD_API(stop_tone_detect_api)
|
||||
{
|
||||
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
||||
if (!session) {
|
||||
stream->write_function(stream, "-ERR no session\n");
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
callprogress_detector_stop(session);
|
||||
stream->write_function(stream, "+OK stopped\n");
|
||||
return status;
|
||||
}
|
||||
|
||||
SWITCH_MODULE_LOAD_FUNCTION(mod_spandsp_init)
|
||||
{
|
||||
switch_application_interface_t *app_interface;
|
||||
switch_api_interface_t *api_interface;
|
||||
|
||||
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
|
||||
|
||||
|
@ -115,8 +198,17 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_spandsp_init)
|
|||
SWITCH_ADD_APP(app_interface, "spandsp_stop_dtmf", "stop inband dtmf", "Stop detecting inband dtmf.", stop_dtmf_session_function, "", SAF_NONE);
|
||||
SWITCH_ADD_APP(app_interface, "spandsp_start_dtmf", "Detect dtmf", "Detect inband dtmf on the session", dtmf_session_function, "", SAF_MEDIA_TAP);
|
||||
|
||||
SWITCH_ADD_APP(app_interface, "start_tone_detect", "Start background tone detection with cadence", "", start_tone_detect_app, "[name]", SAF_NONE);
|
||||
SWITCH_ADD_APP(app_interface, "stop_tone_detect", "Stop background tone detection with cadence", "", stop_tone_detect_app, "", SAF_NONE);
|
||||
SWITCH_ADD_API(api_interface, "start_tone_detect", "Start background tone detection with cadence", start_tone_detect_api, "[name]");
|
||||
SWITCH_ADD_API(api_interface, "stop_tone_detect", "Stop backbground tone detection with cadence", stop_tone_detect_api, "");
|
||||
|
||||
|
||||
mod_spandsp_fax_load(pool);
|
||||
mod_spandsp_codecs_load(module_interface, pool);
|
||||
if (mod_spandsp_dsp_load(module_interface, pool) != SWITCH_STATUS_SUCCESS) {
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
if ((switch_event_bind_removable(modname, SWITCH_EVENT_RELOADXML, NULL, event_handler, NULL, &NODE) != SWITCH_STATUS_SUCCESS)) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind our reloadxml handler!\n");
|
||||
|
@ -133,6 +225,7 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_spandsp_shutdown)
|
|||
switch_event_unbind(&NODE);
|
||||
|
||||
mod_spandsp_fax_shutdown();
|
||||
mod_spandsp_dsp_shutdown();
|
||||
|
||||
return SWITCH_STATUS_UNLOAD;
|
||||
}
|
||||
|
|
|
@ -52,8 +52,13 @@ typedef enum {
|
|||
FUNCTION_GW
|
||||
} mod_spandsp_fax_application_mode_t;
|
||||
|
||||
void mod_spandsp_fax_shutdown(void);
|
||||
void mod_spandsp_fax_load(switch_memory_pool_t *pool);
|
||||
switch_status_t mod_spandsp_codecs_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool);
|
||||
switch_status_t mod_spandsp_dsp_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool);
|
||||
|
||||
void mod_spandsp_fax_shutdown(void);
|
||||
void mod_spandsp_dsp_shutdown(void);
|
||||
|
||||
void mod_spandsp_fax_event_handler(switch_event_t *event);
|
||||
void mod_spandsp_fax_process_fax(switch_core_session_t *session, const char *data, mod_spandsp_fax_application_mode_t app_mode);
|
||||
switch_bool_t t38_gateway_start(switch_core_session_t *session, const char *app, const char *data);
|
||||
|
@ -61,4 +66,5 @@ switch_bool_t t38_gateway_start(switch_core_session_t *session, const char *app,
|
|||
switch_status_t spandsp_stop_inband_dtmf_session(switch_core_session_t *session);
|
||||
switch_status_t spandsp_inband_dtmf_session(switch_core_session_t *session);
|
||||
|
||||
switch_status_t mod_spandsp_codecs_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool);
|
||||
switch_status_t callprogress_detector_start(switch_core_session_t *session, const char *name);
|
||||
switch_status_t callprogress_detector_stop(switch_core_session_t *session);
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
* Anthony Minessale II <anthm@freeswitch.org>
|
||||
* Steve Underwood <steveu@coppice.org>
|
||||
* Antonio Gallo <agx@linux.it>
|
||||
* Christopher M. Rienzo <chris@rienzo.net>
|
||||
* mod_spandsp_dsp.c -- dsp applications provided by SpanDSP
|
||||
*
|
||||
*/
|
||||
|
@ -148,6 +149,481 @@ switch_status_t spandsp_inband_dtmf_session(switch_core_session_t *session)
|
|||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* private channel data */
|
||||
#define TONE_PRIVATE "mod_tone_detect_bug"
|
||||
|
||||
/**
|
||||
* Module global variables
|
||||
*/
|
||||
struct globals {
|
||||
/** Memory pool */
|
||||
switch_memory_pool_t *pool;
|
||||
/** Call progress tones mapped by descriptor name */
|
||||
switch_hash_t *tones;
|
||||
/** Default debug level */
|
||||
int debug;
|
||||
};
|
||||
typedef struct globals globals_t;
|
||||
static globals_t globals;
|
||||
|
||||
/******************************************************************************
|
||||
* TONE DETECTION WITH CADENCE
|
||||
*/
|
||||
|
||||
#define MAX_TONES 32
|
||||
|
||||
/**
|
||||
* Tone descriptor
|
||||
*
|
||||
* Defines a set of tones to look for
|
||||
*/
|
||||
struct tone_descriptor {
|
||||
/** The name of this descriptor set */
|
||||
const char *name;
|
||||
|
||||
/** Describes the tones to watch */
|
||||
super_tone_rx_descriptor_t *spandsp_tone_descriptor;
|
||||
|
||||
/** The mapping of tone id to key */
|
||||
const char *tone_keys[MAX_TONES];
|
||||
};
|
||||
typedef struct tone_descriptor tone_descriptor_t;
|
||||
|
||||
static switch_status_t tone_descriptor_create(tone_descriptor_t **descriptor, const char *name, switch_memory_pool_t *memory_pool);
|
||||
static int tone_descriptor_add_tone(tone_descriptor_t *descriptor, const char *name);
|
||||
static switch_status_t tone_descriptor_add_tone_element(tone_descriptor_t *descriptor, int tone_id, int freq1, int freq2, int min, int max);
|
||||
|
||||
/**
|
||||
* Tone detector
|
||||
*
|
||||
* Performs detection for the tones described by the descriptor.
|
||||
*/
|
||||
struct tone_detector {
|
||||
/** The tones to look for */
|
||||
tone_descriptor_t *descriptor;
|
||||
|
||||
/** The detector */
|
||||
super_tone_rx_state_t *spandsp_detector;
|
||||
|
||||
/** The detected tone */
|
||||
int detected_tone;
|
||||
|
||||
/** The debug level */
|
||||
int debug;
|
||||
};
|
||||
typedef struct tone_detector tone_detector_t;
|
||||
|
||||
static switch_status_t tone_detector_create(tone_detector_t **detector, tone_descriptor_t *descriptor, switch_memory_pool_t *memory_pool);
|
||||
static switch_bool_t tone_detector_process_buffer(tone_detector_t *detector, void *data, unsigned int len, const char **key);
|
||||
static void tone_detector_destroy(tone_detector_t *detector);
|
||||
|
||||
static switch_bool_t callprogress_detector_process_buffer(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type);
|
||||
|
||||
/**
|
||||
* Allocate the tone descriptor
|
||||
*
|
||||
* @param descriptor the descriptor to create
|
||||
* @param name the descriptor name
|
||||
* @param memory_pool the pool to use
|
||||
* @return SWITCH_STATUS_SUCCESS if successful
|
||||
*/
|
||||
static switch_status_t tone_descriptor_create(tone_descriptor_t **descriptor, const char *name, switch_memory_pool_t *memory_pool)
|
||||
{
|
||||
tone_descriptor_t *ldescriptor = NULL;
|
||||
ldescriptor = switch_core_alloc(memory_pool, sizeof(tone_descriptor_t));
|
||||
if (!ldescriptor) {
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
memset(ldescriptor, 0, sizeof(tone_descriptor_t));
|
||||
ldescriptor->name = switch_core_strdup(memory_pool, name);
|
||||
ldescriptor->spandsp_tone_descriptor = super_tone_rx_make_descriptor(NULL);
|
||||
*descriptor = ldescriptor;
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a tone to the tone descriptor
|
||||
*
|
||||
* @param descriptor the tone descriptor
|
||||
* @param key the tone key - this will be returned by the detector upon match
|
||||
* @return the tone ID
|
||||
*/
|
||||
static int tone_descriptor_add_tone(tone_descriptor_t *descriptor, const char *key)
|
||||
{
|
||||
int id = super_tone_rx_add_tone(descriptor->spandsp_tone_descriptor);
|
||||
if (id >= MAX_TONES) {
|
||||
return -1;
|
||||
}
|
||||
descriptor->tone_keys[id] = key;
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a tone element to the tone descriptor
|
||||
*
|
||||
* @param descriptor the tone descriptor
|
||||
* @param tone_id the tone ID
|
||||
* @param freq1 the first frequency (0 if none)
|
||||
* @param freq2 the second frequency (0 if none)
|
||||
* @param min the minimum tone duration in ms
|
||||
* @param max the maximum tone duration in ms
|
||||
* @return SWITCH_STATUS_SUCCESS if successful
|
||||
*/
|
||||
static switch_status_t tone_descriptor_add_tone_element(tone_descriptor_t *descriptor, int tone_id, int freq1, int freq2, int min, int max)
|
||||
{
|
||||
if (super_tone_rx_add_element(descriptor->spandsp_tone_descriptor, tone_id, freq1, freq2, min, max) == 0) {
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process tone report callback from spandsp
|
||||
*
|
||||
* @param user_data the tone_detector
|
||||
* @param code the detected tone
|
||||
* @param level unused
|
||||
* @param delay unused
|
||||
*/
|
||||
static void tone_report_callback(void *user_data, int code, int level, int delay)
|
||||
{
|
||||
tone_detector_t *detector = (tone_detector_t *)user_data;
|
||||
if (detector->debug > 0) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Tone report: code = %d, level = %d, delay = %d\n", code, level, delay);
|
||||
}
|
||||
detector->detected_tone = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process tone segment report from spandsp (for debugging)
|
||||
*
|
||||
* @param user_data the tone_detector
|
||||
* @param f1 the first frequency of the segment
|
||||
* @param f2 the second frequency of the segment
|
||||
* @param duration the duration of the segment
|
||||
*/
|
||||
static void tone_segment_callback(void *user_data, int f1, int f2, int duration)
|
||||
{
|
||||
tone_detector_t *detector = (tone_detector_t *)user_data;
|
||||
if (detector->debug > 1) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Tone segment: f1 = %d, f2 = %d, duration = %d\n", f1, f2, duration);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate the tone detector
|
||||
*
|
||||
* @param detector the detector to create
|
||||
* @param descriptor the descriptor to use
|
||||
* @param memory_pool the pool to use
|
||||
* @return SWITCH_STATUS_SUCCESS if successful
|
||||
*/
|
||||
static switch_status_t tone_detector_create(tone_detector_t **detector, tone_descriptor_t *descriptor, switch_memory_pool_t *memory_pool)
|
||||
{
|
||||
tone_detector_t *ldetector = NULL;
|
||||
ldetector = switch_core_alloc(memory_pool, sizeof(tone_detector_t));
|
||||
if (!ldetector) {
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
memset(ldetector, 0, sizeof(tone_detector_t));
|
||||
ldetector->descriptor = descriptor;
|
||||
ldetector->debug = globals.debug;
|
||||
*detector = ldetector;
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize detector. Call when media bug starts detection
|
||||
*
|
||||
* @param detector the detector to initialize
|
||||
*/
|
||||
static void tone_detector_init(tone_detector_t *detector)
|
||||
{
|
||||
detector->spandsp_detector = super_tone_rx_init(NULL, detector->descriptor->spandsp_tone_descriptor, tone_report_callback, detector);
|
||||
super_tone_rx_segment_callback(detector->spandsp_detector, tone_segment_callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the buffer looking for tones
|
||||
*
|
||||
* @param data the data to process
|
||||
* @param len the amount of data to process
|
||||
* @param key the found tone key
|
||||
* @return SWITCH_TRUE if a tone was found
|
||||
*/
|
||||
static switch_bool_t tone_detector_process_buffer(tone_detector_t *detector, void *data, unsigned int len, const char **key)
|
||||
{
|
||||
detector->detected_tone = -1;
|
||||
super_tone_rx(detector->spandsp_detector, data, len);
|
||||
if (detector->detected_tone != -1) {
|
||||
*key = detector->descriptor->tone_keys[detector->detected_tone];
|
||||
return SWITCH_TRUE;
|
||||
}
|
||||
return SWITCH_FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the tone detector
|
||||
* @param detector the detector to destroy
|
||||
*/
|
||||
static void tone_detector_destroy(tone_detector_t *detector)
|
||||
{
|
||||
if (detector) {
|
||||
if (detector->spandsp_detector) {
|
||||
super_tone_rx_release(detector->spandsp_detector);
|
||||
super_tone_rx_free(detector->spandsp_detector);
|
||||
detector->spandsp_detector = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start call progress detection
|
||||
*
|
||||
* @param session the session to detect
|
||||
* @param name of the descriptor to use
|
||||
* @return SWITCH_STATUS_SUCCESS if successful
|
||||
*/
|
||||
switch_status_t callprogress_detector_start(switch_core_session_t *session, const char *name)
|
||||
{
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
tone_detector_t *detector = NULL;
|
||||
tone_descriptor_t *descriptor = NULL;
|
||||
switch_media_bug_t *bug = NULL;
|
||||
|
||||
/* are we already running? */
|
||||
bug = switch_channel_get_private(channel, TONE_PRIVATE);
|
||||
if (bug) {
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
/* find the tone descriptor with the matching name and create the detector */
|
||||
descriptor = switch_core_hash_find(globals.tones, name);
|
||||
if (!descriptor) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "(%s) no tone descriptor defined with name '%s'. Update configuration. \n", switch_channel_get_name(channel), name);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
tone_detector_create(&detector, descriptor, switch_core_session_get_pool(session));
|
||||
|
||||
/* start listening for tones */
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "(%s) Starting tone detection for '%s'\n", switch_channel_get_name(channel), name);
|
||||
switch_core_media_bug_add(session, "spandsp_tone_detect", NULL,
|
||||
callprogress_detector_process_buffer, detector, 0 /* stop time */, SMBF_READ_STREAM, &bug);
|
||||
if (!bug) {
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
switch_channel_set_private(channel, TONE_PRIVATE, bug);
|
||||
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a buffer of audio data for call progress tones
|
||||
*
|
||||
* @param bug the session's media bug
|
||||
* @param user_data the detector
|
||||
* @param type the type of data available from the bug
|
||||
* @return SWITCH_TRUE
|
||||
*/
|
||||
static switch_bool_t callprogress_detector_process_buffer(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type)
|
||||
{
|
||||
uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
|
||||
switch_frame_t frame = { 0 };
|
||||
tone_detector_t *detector = (tone_detector_t *)user_data;
|
||||
switch_core_session_t *session = switch_core_media_bug_get_session(bug);
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
|
||||
frame.data = data;
|
||||
frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
|
||||
|
||||
switch(type) {
|
||||
case SWITCH_ABC_TYPE_INIT:
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "(%s) initializing tone detector\n", switch_channel_get_name(channel));
|
||||
tone_detector_init(detector);
|
||||
break;
|
||||
case SWITCH_ABC_TYPE_READ:
|
||||
{
|
||||
const char *detected_tone = NULL;
|
||||
if (!detector->spandsp_detector) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "(%s) detector is destroyed\n", switch_channel_get_name(channel));
|
||||
return SWITCH_FALSE;
|
||||
}
|
||||
if (switch_core_media_bug_read(bug, &frame, SWITCH_TRUE) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "(%s) error reading frame\n", switch_channel_get_name(channel));
|
||||
return SWITCH_FALSE;
|
||||
}
|
||||
tone_detector_process_buffer(detector, frame.data, frame.samples, &detected_tone);
|
||||
if (detected_tone) {
|
||||
switch_event_t *event = NULL;
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "(%s) DETECTED TONE: %s\n", switch_channel_get_name(channel), detected_tone);
|
||||
if (switch_event_create(&event, SWITCH_EVENT_DETECTED_TONE) == SWITCH_STATUS_SUCCESS) {
|
||||
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Detected-Tone", detected_tone);
|
||||
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Unique-ID", switch_core_session_get_uuid(session));
|
||||
switch_event_fire(&event);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SWITCH_ABC_TYPE_WRITE:
|
||||
break;
|
||||
case SWITCH_ABC_TYPE_WRITE_REPLACE:
|
||||
break;
|
||||
case SWITCH_ABC_TYPE_READ_REPLACE:
|
||||
break;
|
||||
case SWITCH_ABC_TYPE_READ_PING:
|
||||
break;
|
||||
case SWITCH_ABC_TYPE_CLOSE:
|
||||
if (detector->spandsp_detector) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "(%s) destroying tone detector\n", switch_channel_get_name(channel));
|
||||
tone_detector_destroy(detector);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return SWITCH_TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop call progress detection
|
||||
* @param session the session to stop
|
||||
* @return SWITCH_STATUS_SUCCESS if successful
|
||||
*/
|
||||
switch_status_t callprogress_detector_stop(switch_core_session_t *session)
|
||||
{
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
switch_media_bug_t *bug = switch_channel_get_private(channel, TONE_PRIVATE);
|
||||
if (bug) {
|
||||
switch_core_media_bug_close(&bug);
|
||||
switch_channel_set_private(channel, TONE_PRIVATE, NULL);
|
||||
}
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process configuration file
|
||||
*/
|
||||
static switch_status_t do_config(void)
|
||||
{
|
||||
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
||||
switch_xml_t cfg = NULL, xml = NULL, callprogress = NULL, xdescriptor = NULL;
|
||||
if (!(xml = switch_xml_open_cfg("spandsp.conf", &cfg, NULL))) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Could not open tone_detect.conf\n");
|
||||
status = SWITCH_STATUS_FALSE;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* TODO make configuration param */
|
||||
globals.debug = 1;
|
||||
|
||||
/* Configure call progress detector */
|
||||
if ((callprogress = switch_xml_child(cfg, "descriptors"))) {
|
||||
for (xdescriptor = switch_xml_child(callprogress, "descriptor"); xdescriptor; xdescriptor = switch_xml_next(xdescriptor)) {
|
||||
const char *name = switch_xml_attr(xdescriptor, "name");
|
||||
const char *tone_name = NULL;
|
||||
switch_xml_t tone = NULL, element = NULL;
|
||||
tone_descriptor_t *descriptor = NULL;
|
||||
|
||||
/* create descriptor */
|
||||
if (zstr(name)) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Missing <descriptor> name\n");
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Adding tone_descriptor: %s\n", name);
|
||||
if (tone_descriptor_create(&descriptor, name, globals.pool) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Unable to allocate tone_descriptor: %s\n", name);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
switch_core_hash_insert(globals.tones, name, descriptor);
|
||||
|
||||
/* add tones to descriptor */
|
||||
for (tone = switch_xml_child(xdescriptor, "tone"); tone; tone = switch_xml_next(tone)) {
|
||||
int id = 0;
|
||||
tone_name = switch_xml_attr(tone, "name");
|
||||
if (zstr(tone_name)) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Missing <tone> name for <descriptor> %s\n", name);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
id = tone_descriptor_add_tone(descriptor, tone_name);
|
||||
if (id == -1) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Unable to add tone_descriptor: %s, tone: %s. (too many tones)\n", name, tone_name);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Adding tone_descriptor: %s, tone: %s(%d)\n", name, tone_name, id);}
|
||||
/* add elements to tone */
|
||||
for (element = switch_xml_child(tone, "element"); element; element = switch_xml_next(element)) {
|
||||
const char *freq1_attr = switch_xml_attr(element, "freq1");
|
||||
const char *freq2_attr = switch_xml_attr(element, "freq2");
|
||||
const char *min_attr = switch_xml_attr(element, "min");
|
||||
const char *max_attr = switch_xml_attr(element, "max");
|
||||
int freq1, freq2, min, max;
|
||||
if (zstr(freq1_attr)) {
|
||||
freq1 = 0;
|
||||
} else {
|
||||
freq1 = atoi(freq1_attr);
|
||||
}
|
||||
if (zstr(freq2_attr)) {
|
||||
freq2 = 0;
|
||||
} else {
|
||||
freq2 = atoi(freq2_attr);
|
||||
}
|
||||
if (zstr(min_attr)) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Missing min in <element> of <descriptor> %s <tone> %s(%d)\n", name, tone_name, id);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
min = atoi(min_attr);
|
||||
if (zstr(max_attr)) {
|
||||
max = 0;
|
||||
} else {
|
||||
max = atoi(max_attr);
|
||||
}
|
||||
/* check params */
|
||||
if ((freq1 < 0 || freq2 < 0 || min < 0 || max < 0) || (freq1 == 0 && min == 0 && max == 0)) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid element param.\n");
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Adding tone_descriptor: %s, tone: %s(%d), element (%d, %d, %d, %d)\n", name, tone_name, id, freq1, freq2, min, max);
|
||||
tone_descriptor_add_tone_element(descriptor, id, freq1, freq2, min, max);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
done:
|
||||
if (xml) {
|
||||
switch_xml_free(xml);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when FreeSWITCH loads the module
|
||||
*/
|
||||
switch_status_t mod_spandsp_dsp_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool)
|
||||
{
|
||||
memset(&globals, 0, sizeof(globals_t));
|
||||
globals.pool = pool;
|
||||
|
||||
switch_core_hash_init(&globals.tones, globals.pool);
|
||||
if (do_config() != SWITCH_STATUS_SUCCESS) {
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
/* indicate that the module should continue to be loaded */
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when FreeSWITCH stops the module
|
||||
*/
|
||||
void mod_spandsp_dsp_shutdown(void)
|
||||
{
|
||||
switch_core_hash_destroy(&globals.tones);
|
||||
}
|
||||
|
||||
|
||||
/* For Emacs:
|
||||
* Local Variables:
|
||||
* mode:c
|
||||
|
|
Loading…
Reference in New Issue