mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-07-23 20:51:58 +00:00
IVR groundwork (move playback into the core and add timing option)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@447 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
@@ -45,6 +45,18 @@ extern "C" {
|
||||
#define SWITCH_MAX_CODECS 30
|
||||
|
||||
|
||||
/*!
|
||||
\enum switch_ivr_option_t
|
||||
\brief Possible options related to ivr functions
|
||||
<pre>
|
||||
SWITCH_IVR_OPTION_SYNC - synchronous (do everyting in the forground)
|
||||
|
||||
</pre>
|
||||
*/
|
||||
typedef enum {
|
||||
SWITCH_IVR_OPTION_SYNC = (1 << 0)
|
||||
} switch_ivr_option_t;
|
||||
|
||||
/*!
|
||||
\enum switch_core_session_message_t
|
||||
\brief Possible types of messages for inter-session communication
|
||||
@@ -354,6 +366,7 @@ typedef switch_status (*switch_waitfor_read_hook)(switch_core_session *, int, in
|
||||
typedef switch_status (*switch_waitfor_write_hook)(switch_core_session *, int, int);
|
||||
typedef switch_status (*switch_send_dtmf_hook)(switch_core_session *, char *);
|
||||
typedef switch_status (*switch_api_function)(char *in, char *out, size_t outlen);
|
||||
typedef switch_status (*switch_dtmf_callback_function)(switch_core_session *session, char *dtmf);
|
||||
|
||||
/* things we don't deserve to know about */
|
||||
|
||||
|
||||
@@ -30,151 +30,47 @@
|
||||
*
|
||||
*/
|
||||
#include <switch.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <switch_ivr.h>
|
||||
|
||||
|
||||
|
||||
static const char modname[] = "mod_playback";
|
||||
|
||||
/*
|
||||
dtmf handler function you can hook up to be executed when a digit is dialed during playback
|
||||
if you return anything but SWITCH_STATUS_SUCCESS the playback will stop.
|
||||
*/
|
||||
static switch_status on_dtmf(switch_core_session *session, char *dtmf)
|
||||
{
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Digits %s\n", dtmf);
|
||||
|
||||
if (*dtmf == '*') {
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void playback_function(switch_core_session *session, char *data)
|
||||
{
|
||||
switch_channel *channel;
|
||||
short buf[960];
|
||||
char dtmf[128];
|
||||
int interval = 0, samples = 0;
|
||||
size_t len = 0, ilen = 0;
|
||||
switch_frame write_frame;
|
||||
switch_timer timer;
|
||||
switch_core_thread_session thread_session;
|
||||
switch_codec codec;
|
||||
switch_memory_pool *pool = switch_core_session_get_pool(session);
|
||||
switch_file_handle fh;
|
||||
char *codec_name;
|
||||
int x;
|
||||
int stream_id;
|
||||
char *timer_name = NULL;
|
||||
char *file_name = NULL;
|
||||
|
||||
file_name = switch_core_session_strdup(session, data);
|
||||
|
||||
if ((timer_name = strchr(file_name, ' '))) {
|
||||
*timer_name++ = '\0';
|
||||
}
|
||||
|
||||
channel = switch_core_session_get_channel(session);
|
||||
assert(channel != NULL);
|
||||
assert(channel != NULL);
|
||||
|
||||
if (switch_core_file_open(&fh,
|
||||
data,
|
||||
SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT,
|
||||
switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
|
||||
if (switch_ivr_play_file(session, file_name, timer_name, on_dtmf) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_channel_hangup(channel);
|
||||
return;
|
||||
}
|
||||
|
||||
switch_channel_answer(channel);
|
||||
|
||||
write_frame.data = buf;
|
||||
write_frame.buflen = sizeof(buf);
|
||||
|
||||
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "OPEN FILE %s %dhz %d channels\n", data, fh.samplerate, fh.channels);
|
||||
|
||||
interval = 20;
|
||||
samples = (fh.samplerate / 50) * fh.channels;
|
||||
len = samples * 2;
|
||||
|
||||
codec_name = "L16";
|
||||
|
||||
if (switch_core_codec_init(&codec,
|
||||
codec_name,
|
||||
fh.samplerate,
|
||||
interval,
|
||||
fh.channels,
|
||||
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
|
||||
NULL, pool) == SWITCH_STATUS_SUCCESS) {
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Raw Codec Activated\n");
|
||||
write_frame.codec = &codec;
|
||||
} else {
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Raw Codec Activation Failed %s@%dhz %d channels %dms\n",
|
||||
codec_name, fh.samplerate, fh.channels, interval);
|
||||
switch_core_file_close(&fh);
|
||||
switch_channel_hangup(channel);
|
||||
return;
|
||||
}
|
||||
|
||||
if (switch_core_timer_init(&timer, "soft", interval, samples, pool) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "setup timer failed!\n");
|
||||
switch_core_codec_destroy(&codec);
|
||||
switch_core_file_close(&fh);
|
||||
switch_channel_hangup(channel);
|
||||
return;
|
||||
}
|
||||
write_frame.rate = fh.samplerate;
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "setup timer success %d bytes per %d ms!\n", len, interval);
|
||||
|
||||
/* start a thread to absorb incoming audio */
|
||||
for (stream_id = 0; stream_id < switch_core_session_get_stream_count(session); stream_id++) {
|
||||
switch_core_service_session(session, &thread_session, stream_id);
|
||||
}
|
||||
ilen = samples;
|
||||
while (switch_channel_get_state(channel) == CS_EXECUTE) {
|
||||
int done = 0;
|
||||
|
||||
if (switch_channel_has_dtmf(channel)) {
|
||||
switch_channel_dequeue_dtmf(channel, dtmf, sizeof(dtmf));
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "DTMF [%s]\n", dtmf);
|
||||
|
||||
switch (*dtmf) {
|
||||
case '*':
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch_core_file_read(&fh, buf, &ilen);
|
||||
|
||||
if (ilen <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
write_frame.datalen = ilen * 2;
|
||||
write_frame.samples = (int) ilen;
|
||||
#ifdef SWAP_LINEAR
|
||||
switch_swap_linear(write_frame.data, (int) write_frame.datalen / 2);
|
||||
#endif
|
||||
|
||||
for (stream_id = 0; stream_id < switch_core_session_get_stream_count(session); stream_id++) {
|
||||
if (switch_core_session_write_frame(session, &write_frame, -1, stream_id) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Bad Write\n");
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ((x = switch_core_timer_next(&timer)) < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "done playing file\n");
|
||||
switch_core_file_close(&fh);
|
||||
|
||||
//switch_core_session_kill_channel(session, SWITCH_SIG_KILL);
|
||||
|
||||
switch_core_timer_destroy(&timer);
|
||||
|
||||
switch_core_codec_destroy(&codec);
|
||||
|
||||
//switch_channel_hangup(channel);
|
||||
|
||||
/* End the audio absorbing thread */
|
||||
switch_core_thread_session_end(&thread_session);
|
||||
|
||||
|
||||
}
|
||||
|
||||
static const switch_application_interface playback_application_interface = {
|
||||
|
||||
Reference in New Issue
Block a user