From d9c4b269b646f0f57f8b9cabb82d9100d5088281 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 20 Feb 2013 13:07:44 -0600 Subject: [PATCH] add params to soundfiles and implement ;timeout=N to set a particular timeout in milliseconds especially usefule with streams that never end naturally like local_stream:// --- src/include/switch_module_interfaces.h | 1 + src/switch_core_file.c | 31 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/include/switch_module_interfaces.h b/src/include/switch_module_interfaces.h index d9fa1b2abf..650d16eb97 100644 --- a/src/include/switch_module_interfaces.h +++ b/src/include/switch_module_interfaces.h @@ -351,6 +351,7 @@ struct switch_file_handle { char *file_path; char *spool_path; const char *prefix; + int max_samples; }; /*! \brief Abstract interface to an asr module */ diff --git a/src/switch_core_file.c b/src/switch_core_file.c index 1cf60b5583..d72e1938ba 100644 --- a/src/switch_core_file.c +++ b/src/switch_core_file.c @@ -47,6 +47,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_file_open(const char *file, char *rhs = NULL; const char *spool_path = NULL; int is_stream = 0; + char *fp = NULL, *params = NULL; + int to = 0; if (switch_test_flag(fh, SWITCH_FILE_OPEN)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Handle already open\n"); @@ -76,6 +78,25 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_file_open(const char *file, switch_set_flag(fh, SWITCH_FILE_FLAG_FREE_POOL); } + if (strchr(file_path, ';')) { + char *timeout; + + fp = switch_core_strdup(fh->memory_pool, file_path); + file_path = fp; + + if ((params = strchr(fp, ';'))) { + *params++ = '\0'; + + if ((timeout = switch_find_parameter(params, "timeout", fh->memory_pool))) { + if ((to = atoi(timeout)) < 1) { + to = 0; + } + } + + } + } + + if (switch_directory_exists(file_path, fh->memory_pool) == SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "File [%s] is a directory not a file.\n", file_path); status = SWITCH_STATUS_GENERR; @@ -175,6 +196,11 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_file_open(const char *file, switch_goto_status(status, fail); } + if (to) { + fh->max_samples = (fh->samplerate / 1000) * to; + } + + if ((flags & SWITCH_FILE_FLAG_READ)) { fh->native_rate = fh->samplerate; } else { @@ -226,6 +252,11 @@ SWITCH_DECLARE(switch_status_t) switch_core_file_read(switch_file_handle_t *fh, top: + if (fh->max_samples > 0 && fh->samples_in >= fh->max_samples) { + *len = 0; + return SWITCH_STATUS_FALSE; + } + if (fh->buffer && switch_buffer_inuse(fh->buffer) >= *len * 2) { *len = switch_buffer_read(fh->buffer, data, orig_len * 2) / 2; return SWITCH_STATUS_SUCCESS;