From 17ac6ba9c63d117956e48178f86866019ba32001 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Tue, 26 Oct 2010 19:34:47 -0500 Subject: [PATCH] add record overwrite --- src/include/switch_types.h | 3 +- .../formats/mod_native_file/mod_native_file.c | 2 +- src/mod/formats/mod_sndfile/mod_sndfile.c | 4 +- src/switch_core_file.c | 39 +++++++++++++------ src/switch_ivr_play_say.c | 18 +++++++++ 5 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/include/switch_types.h b/src/include/switch_types.h index c3622e3195..272d435068 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -1307,7 +1307,8 @@ typedef enum { SWITCH_FILE_CALLBACK = (1 << 12), SWITCH_FILE_DONE = (1 << 13), SWITCH_FILE_BUFFER_DONE = (1 << 14), - SWITCH_FILE_WRITE_APPEND = (1 << 15) + SWITCH_FILE_WRITE_APPEND = (1 << 15), + SWITCH_FILE_WRITE_OVER = (1 << 16) } switch_file_flag_enum_t; typedef uint32_t switch_file_flag_t; diff --git a/src/mod/formats/mod_native_file/mod_native_file.c b/src/mod/formats/mod_native_file/mod_native_file.c index c72743fa90..272fc5d504 100644 --- a/src/mod/formats/mod_native_file/mod_native_file.c +++ b/src/mod/formats/mod_native_file/mod_native_file.c @@ -58,7 +58,7 @@ static switch_status_t native_file_file_open(switch_file_handle_t *handle, const if (switch_test_flag(handle, SWITCH_FILE_FLAG_WRITE)) { flags |= SWITCH_FOPEN_WRITE | SWITCH_FOPEN_CREATE; - if (switch_test_flag(handle, SWITCH_FILE_WRITE_APPEND)) { + if (switch_test_flag(handle, SWITCH_FILE_WRITE_APPEND) || switch_test_flag(handle, SWITCH_FILE_WRITE_OVER)) { flags |= SWITCH_FOPEN_READ; } else { flags |= SWITCH_FOPEN_TRUNCATE; diff --git a/src/mod/formats/mod_sndfile/mod_sndfile.c b/src/mod/formats/mod_sndfile/mod_sndfile.c index a0ddba07d0..2b661ed2c2 100644 --- a/src/mod/formats/mod_sndfile/mod_sndfile.c +++ b/src/mod/formats/mod_sndfile/mod_sndfile.c @@ -82,7 +82,7 @@ static switch_status_t sndfile_file_open(switch_file_handle_t *handle, const cha } if (switch_test_flag(handle, SWITCH_FILE_FLAG_WRITE)) { - if (switch_test_flag(handle, SWITCH_FILE_WRITE_APPEND)) { + if (switch_test_flag(handle, SWITCH_FILE_WRITE_APPEND) || switch_test_flag(handle, SWITCH_FILE_WRITE_OVER)) { mode += SFM_RDWR; } else { mode += SFM_WRITE; @@ -208,6 +208,8 @@ static switch_status_t sndfile_file_open(switch_file_handle_t *handle, const cha if (switch_test_flag(handle, SWITCH_FILE_WRITE_APPEND)) { handle->pos = sf_seek(context->handle, 0, SEEK_END); + } else if (switch_test_flag(handle, SWITCH_FILE_WRITE_OVER)) { + handle->pos = sf_seek(context->handle, 0, SEEK_SET); } else { sf_count_t frames = 0; sf_command(context->handle, SFC_FILE_TRUNCATE, &frames, sizeof(frames)); diff --git a/src/switch_core_file.c b/src/switch_core_file.c index 7d5df8882a..af3897e48c 100644 --- a/src/switch_core_file.c +++ b/src/switch_core_file.c @@ -407,39 +407,56 @@ SWITCH_DECLARE(switch_status_t) switch_core_file_write(switch_file_handle_t *fh, SWITCH_DECLARE(switch_status_t) switch_core_file_seek(switch_file_handle_t *fh, unsigned int *cur_pos, int64_t samples, int whence) { - size_t bytes = 0; switch_status_t status; - + int ok = 1; + switch_assert(fh != NULL); switch_assert(fh->file_interface != NULL); - if (!switch_test_flag(fh, SWITCH_FILE_OPEN) || !switch_test_flag(fh, SWITCH_FILE_FLAG_READ)) { + if (!switch_test_flag(fh, SWITCH_FILE_OPEN) || !fh->file_interface->file_seek) { + ok = 0; + } else if (switch_test_flag(fh, SWITCH_FILE_FLAG_WRITE)) { + if (!(switch_test_flag(fh, SWITCH_FILE_WRITE_APPEND) || switch_test_flag(fh, SWITCH_FILE_WRITE_OVER))) { + ok = 0; + } + } else if (!switch_test_flag(fh, SWITCH_FILE_FLAG_READ)) { + ok = 0; + } + + if (!ok) { return SWITCH_STATUS_FALSE; } - - if (!fh->file_interface->file_seek) { - return SWITCH_STATUS_FALSE; - } - + if (fh->buffer) { - bytes += switch_buffer_inuse(fh->buffer); switch_buffer_zero(fh->buffer); } if (fh->pre_buffer) { - bytes += switch_buffer_inuse(fh->pre_buffer); switch_buffer_zero(fh->pre_buffer); } if (whence == SWITCH_SEEK_CUR) { - samples -= bytes / sizeof(int16_t); + unsigned int cur = 0; + + if (switch_test_flag(fh, SWITCH_FILE_FLAG_WRITE)) { + fh->file_interface->file_seek(fh, &cur, fh->samples_out, SEEK_SET); + } else { + fh->file_interface->file_seek(fh, &cur, fh->offset_pos, SEEK_SET); + } } switch_set_flag(fh, SWITCH_FILE_SEEK); status = fh->file_interface->file_seek(fh, cur_pos, samples, whence); + if (samples) { fh->offset_pos = *cur_pos; + + if (switch_test_flag(fh, SWITCH_FILE_FLAG_WRITE)) { + fh->samples_out = *cur_pos; + } } + + return status; } diff --git a/src/switch_ivr_play_say.c b/src/switch_ivr_play_say.c index b1b20ec6fb..7285b894ea 100644 --- a/src/switch_ivr_play_say.c +++ b/src/switch_ivr_play_say.c @@ -364,6 +364,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se time_t start = 0; uint32_t org_silence_hits = 0; int asis = 0; + int32_t sample_start = 0; int waste_resources = 0, fill_cng = 0; switch_codec_implementation_t read_impl = { 0 }; switch_frame_t write_frame = { 0 }; @@ -405,6 +406,11 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se fh->channels = read_impl.number_of_channels; fh->native_rate = read_impl.actual_samples_per_second; + if (fh->samples > 0) { + sample_start = fh->samples; + fh->samples = 0; + } + if ((vval = switch_channel_get_variable(channel, "record_sample_rate"))) { int tmp = 0; @@ -497,6 +503,10 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se file_flags |= SWITCH_FILE_WRITE_APPEND; } + if (switch_test_flag(fh, SWITCH_FILE_WRITE_OVER) || ((p = switch_channel_get_variable(channel, "RECORD_WRITE_OVER")) && switch_true(p))) { + file_flags |= SWITCH_FILE_WRITE_OVER; + } + if (!fh->prefix) { fh->prefix = prefix; } @@ -507,6 +517,14 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se return SWITCH_STATUS_GENERR; } + if (sample_start > 0) { + uint32_t pos = 0; + switch_core_file_seek(fh, &pos, sample_start, SEEK_SET); + switch_clear_flag(fh, SWITCH_FILE_SEEK); + fh->samples = 0; + } + + if (switch_test_flag(fh, SWITCH_FILE_NATIVE)) { asis = 1; }