]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add record overwrite
authorAnthony Minessale <anthm@freeswitch.org>
Wed, 27 Oct 2010 00:34:47 +0000 (19:34 -0500)
committerAnthony Minessale <anthm@freeswitch.org>
Wed, 27 Oct 2010 00:34:47 +0000 (19:34 -0500)
src/include/switch_types.h
src/mod/formats/mod_native_file/mod_native_file.c
src/mod/formats/mod_sndfile/mod_sndfile.c
src/switch_core_file.c
src/switch_ivr_play_say.c

index c3622e31959065cf5906462138f22e7181a8a0fd..272d435068cb707587750773acf913e96e61f08e 100644 (file)
@@ -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;
 
index c72743fa90fe1ee6c969b393876784c6b267003d..272fc5d504e9a2dbb3c146a486b3adf4dbb8b2b1 100644 (file)
@@ -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;
index a0ddba07d0b09688eb5b700999a54cf1a310042a..2b661ed2c2bf905e513b019b431ffb7a69e33149 100644 (file)
@@ -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));
index 7d5df8882a3972665a7e3f4482ea502cd8556733..af3897e48cba4ab8014e10f2dfa6e0a9c43e25bb 100644 (file)
@@ -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)) {
-               return SWITCH_STATUS_FALSE;
+       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 (!fh->file_interface->file_seek) {
+       
+       if (!ok) {
                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;
 }
 
index b1b20ec6fbcc9b88550289812a11ebb85edc57aa..7285b894eaa1740e009df8f57a10e65e6a8badbb 100644 (file)
@@ -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;
        }