From 893c7210a2b33d87d7356bbd321a15b781754d25 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Fri, 22 Sep 2006 02:10:27 +0000 Subject: [PATCH] more optimizationification git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@2775 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/mod/timers/mod_softtimer/mod_softtimer.c | 12 +++++++---- src/switch_buffer.c | 19 +++++++++++++---- src/switch_core.c | 22 ++++++++++++-------- src/switch_ivr.c | 8 ++++--- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/mod/timers/mod_softtimer/mod_softtimer.c b/src/mod/timers/mod_softtimer/mod_softtimer.c index 03daa0e167..11d5609c36 100644 --- a/src/mod/timers/mod_softtimer/mod_softtimer.c +++ b/src/mod/timers/mod_softtimer/mod_softtimer.c @@ -155,8 +155,12 @@ SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_mod return SWITCH_STATUS_SUCCESS; } +/* I cant resist setting this to 10ms, we dont even run anything smaller than 20ms so this is already + twice the granularity we need, we'll change it if we need anything smaller +*/ - +#define STEP_MS 10 +#define STEP_MIC 10000 SWITCH_MOD_DECLARE(switch_status_t) switch_module_runtime(void) { switch_time_t reference = switch_time_now(); @@ -169,13 +173,13 @@ SWITCH_MOD_DECLARE(switch_status_t) switch_module_runtime(void) globals.RUNNING = 1; while(globals.RUNNING == 1) { - reference += 1000; + reference += STEP_MIC; while (switch_time_now() < reference) { - switch_yield(1000); + switch_yield(STEP_MIC); } - current_ms++; + current_ms += STEP_MS; for (x = 0; x < MAX_ELEMENTS; x++) { int i = x, index; diff --git a/src/switch_buffer.c b/src/switch_buffer.c index e812154ff3..0bcc7775c6 100644 --- a/src/switch_buffer.c +++ b/src/switch_buffer.c @@ -42,6 +42,7 @@ struct switch_buffer { switch_byte_t *data; switch_byte_t *head; switch_size_t used; + switch_size_t actually_used; switch_size_t datalen; switch_size_t max_len; switch_size_t blocksize; @@ -146,7 +147,7 @@ SWITCH_DECLARE(switch_size_t) switch_buffer_toss(switch_buffer_t *buffer, switch memmove(buffer->data, buffer->data + reading, reading); buffer->head = buffer->data; buffer->used -= reading; - + buffer->actually_used = buffer->used; return buffer->used; } @@ -177,7 +178,7 @@ SWITCH_DECLARE(switch_size_t) switch_buffer_read(switch_buffer_t *buffer, void * SWITCH_DECLARE(switch_size_t) switch_buffer_write(switch_buffer_t *buffer, void *data, switch_size_t datalen) { - switch_size_t freespace; + switch_size_t freespace, actual_freespace; assert(buffer != NULL); assert(data != NULL); @@ -187,13 +188,22 @@ SWITCH_DECLARE(switch_size_t) switch_buffer_write(switch_buffer_t *buffer, void return buffer->used; } + actual_freespace = buffer->datalen - buffer->actually_used; + + if (actual_freespace < datalen) { + memmove(buffer->data, buffer->head, buffer->used); + buffer->head = buffer->data; + buffer->actually_used = buffer->used; + } + freespace = buffer->datalen - buffer->used; + /* if (buffer->data != buffer->head) { memmove(buffer->data, buffer->head, buffer->used); buffer->head = buffer->data; } - + */ if (switch_test_flag(buffer, SWITCH_BUFFER_FLAG_DYNAMIC)) { if (freespace < datalen) { switch_size_t new_size, new_block_size; @@ -218,8 +228,9 @@ SWITCH_DECLARE(switch_size_t) switch_buffer_write(switch_buffer_t *buffer, void if (freespace < datalen) { return 0; } else { - memcpy(buffer->data + buffer->used, data, datalen); + memcpy(buffer->head + buffer->used, data, datalen); buffer->used += datalen; + buffer->actually_used += datalen; } //if (buffer->id == 4) printf("%u i %d = %d\n", buffer->id, (uint32_t)datalen, (uint32_t)buffer->used); diff --git a/src/switch_core.c b/src/switch_core.c index e41584b789..1a1af4e570 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -52,6 +52,8 @@ #define SWITCH_EVENT_QUEUE_LEN 256 #define SWITCH_SQL_QUEUE_LEN 2000 +#define SWITCH_BUFFER_BLOCK_FRAMES 25 +#define SWITCH_BUFFER_START_FRAMES 50 struct switch_media_bug { switch_buffer_t *raw_write_buffer; @@ -253,10 +255,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_add(switch_core_session_t bug->user_data = user_data; bug->session = session; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Attaching BUG to %s\n", switch_channel_get_name(session->channel)); - bytes = session->read_codec->implementation->bytes_per_frame * 2; - switch_buffer_create_dynamic(&bug->raw_read_buffer, bytes, bytes, MAX_BUG_BUFFER); - bytes = session->write_codec->implementation->bytes_per_frame * 2; - switch_buffer_create_dynamic(&bug->raw_write_buffer, bytes, bytes, MAX_BUG_BUFFER); + bytes = session->read_codec->implementation->bytes_per_frame; + switch_buffer_create_dynamic(&bug->raw_read_buffer, bytes * SWITCH_BUFFER_BLOCK_FRAMES, bytes * SWITCH_BUFFER_START_FRAMES, MAX_BUG_BUFFER); + bytes = session->write_codec->implementation->bytes_per_frame; + switch_buffer_create_dynamic(&bug->raw_write_buffer, bytes * SWITCH_BUFFER_BLOCK_FRAMES, bytes * SWITCH_BUFFER_START_FRAMES, MAX_BUG_BUFFER); switch_mutex_init(&bug->read_mutex, SWITCH_MUTEX_NESTED, session->pool); switch_mutex_init(&bug->write_mutex, SWITCH_MUTEX_NESTED, session->pool); @@ -1701,9 +1703,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi perfect = TRUE; } else { if (!session->raw_read_buffer) { - switch_size_t bytes = session->read_codec->implementation->bytes_per_frame * 2; + switch_size_t bytes = session->read_codec->implementation->bytes_per_frame; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Engaging Read Buffer at %u bytes\n", bytes); - switch_buffer_create_dynamic(&session->raw_read_buffer, bytes, bytes, 0); + switch_buffer_create_dynamic(&session->raw_read_buffer, bytes * SWITCH_BUFFER_BLOCK_FRAMES, bytes * SWITCH_BUFFER_START_FRAMES, 0); } if (!switch_buffer_write(session->raw_read_buffer, read_frame->data, read_frame->datalen)) { status = SWITCH_STATUS_MEMERR; @@ -1932,13 +1934,15 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess perfect = TRUE; } else { if (!session->raw_write_buffer) { - switch_size_t bytes = session->write_codec->implementation->bytes_per_frame * 2; + switch_size_t bytes = session->write_codec->implementation->bytes_per_frame; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Engaging Write Buffer at %u bytes to accomodate %u->%u\n", bytes, write_frame->datalen, session->write_codec->implementation->bytes_per_frame); - if ((status = - switch_buffer_create_dynamic(&session->raw_write_buffer, bytes, bytes, 0)) != SWITCH_STATUS_SUCCESS) { + if ((status =switch_buffer_create_dynamic(&session->raw_write_buffer, + bytes * SWITCH_BUFFER_BLOCK_FRAMES, + bytes * SWITCH_BUFFER_START_FRAMES, + 0)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Write Buffer Failed!\n"); return status; } diff --git a/src/switch_ivr.c b/src/switch_ivr.c index 633f6345a6..fcdc38c57c 100644 --- a/src/switch_ivr.c +++ b/src/switch_ivr.c @@ -580,8 +580,10 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_session(switch_core_session_t return SWITCH_STATUS_SUCCESS; } -#define FILE_STARTSAMPLES 512 * 128 -#define FILE_BLOCKSIZE 1024 +#define FILE_STARTSAMPLES 512 * 64 +#define FILE_BLOCKSIZE 1024 * 8 +#define FILE_BUFSIZE 1024 * 64 + SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *session, switch_file_handle_t *fh, char *file, @@ -679,7 +681,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess interval = read_codec->implementation->microseconds_per_frame / 1000; if (!fh->audio_buffer) { - switch_buffer_create_dynamic(&fh->audio_buffer, FILE_BLOCKSIZE, (FILE_STARTSAMPLES * sizeof(int16_t)) + FILE_BLOCKSIZE, 0); + switch_buffer_create_dynamic(&fh->audio_buffer, FILE_BLOCKSIZE, FILE_BUFSIZE, 0); } codec_name = "L16";