]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add padding to cycles on session_record
authorAnthony Minessale <anthony.minessale@gmail.com>
Fri, 5 Jun 2009 19:52:02 +0000 (19:52 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Fri, 5 Jun 2009 19:52:02 +0000 (19:52 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@13648 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_core.h
src/mod/formats/mod_shout/mod_shout.c
src/switch_core_media_bug.c
src/switch_ivr_async.c

index 9d0623de0605996b5db91f95bdde3ef205530452..16063b41a39f38618c70057da57c97cba1bf4787 100644 (file)
@@ -238,7 +238,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_remove_all(_In_ switch_cor
   \param frame the frame to write the data to
   \return the amount of data 
 */
-SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(_In_ switch_media_bug_t *bug, _In_ switch_frame_t *frame);
+SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(_In_ switch_media_bug_t *bug, _In_ switch_frame_t *frame, switch_bool_t fill);
 
 /*!
   \brief Flush the read and write buffers for the bug
index 278abfbfdcd664df855dd252e9b8bfe3faa1bd8a..2eaef91029f0b538da2a83072f279fbe84603ca1 100644 (file)
@@ -1159,7 +1159,7 @@ static switch_bool_t telecast_callback(switch_media_bug_t *bug, void *user_data,
                break;
        case SWITCH_ABC_TYPE_READ_PING:
                if (buffer) {
-                       if (switch_core_media_bug_read(bug, &frame) == SWITCH_STATUS_SUCCESS) {
+                       if (switch_core_media_bug_read(bug, &frame, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
                                switch_buffer_lock(buffer);
                                switch_buffer_write(buffer, frame.data, frame.datalen);
                                switch_buffer_unlock(buffer);
index 06d32fa607064fad07e97ae983852bf89bda1fc8..82b75b7dc324a967ffac6ca60caa2877c84f639c 100644 (file)
@@ -102,7 +102,7 @@ SWITCH_DECLARE(void) switch_core_media_bug_flush(switch_media_bug_t *bug)
        }
 }
 
-SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(switch_media_bug_t *bug, switch_frame_t *frame)
+SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(switch_media_bug_t *bug, switch_frame_t *frame, switch_bool_t fill)
 {
        switch_size_t bytes = 0, datalen = 0;
        int16_t *dp, *fp;
@@ -131,7 +131,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(switch_media_bug_t *b
        
        switch_mutex_lock(bug->read_mutex);
        frame->datalen = (uint32_t) switch_buffer_read(bug->raw_read_buffer, frame->data, bytes);
-       if (frame->datalen < bytes) {
+       if (fill && frame->datalen < bytes) {
                memset(((unsigned char *)frame->data) + frame->datalen, 0, bytes - frame->datalen);
                frame->datalen = bytes;
        }
@@ -141,7 +141,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(switch_media_bug_t *b
                switch_assert(bug->raw_write_buffer);
                switch_mutex_lock(bug->write_mutex);
                datalen = (uint32_t) switch_buffer_read(bug->raw_write_buffer, bug->data, bytes);
-               if (datalen < bytes) {
+               if (fill && datalen < bytes) {
                        memset(((unsigned char *)bug->data) + datalen, 0, bytes - datalen);
                        datalen = bytes;
                }
@@ -154,6 +154,14 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(switch_media_bug_t *b
        rlen = frame->datalen / 2;
        wlen = datalen / 2;
        blen = bytes / 2;
+
+       if (!fill && rlen == 0 && wlen == 0) {
+               frame->datalen = 0;
+               frame->samples = 0;
+               frame->rate = read_impl.actual_samples_per_second;
+               frame->codec = NULL;
+               return SWITCH_STATUS_FALSE;
+       }
        
        if (switch_test_flag(bug, SMBF_STEREO)) {
                for (x = 0; x < blen; x++) {
index 23686196115f2934007d3aef228fd82b5843dd30..163ba2e43b9b1ac036c17b8e21f4010099e0969a 100644 (file)
@@ -424,9 +424,11 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_displace_session(switch_core_session_
        return SWITCH_STATUS_SUCCESS;
 }
 
+#define LEAD_IN 25
 struct record_helper {
        char *file;
        switch_file_handle_t *fh;
+       int lead_in;
 };
 
 static switch_bool_t record_callback(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type)
@@ -447,6 +449,24 @@ static switch_bool_t record_callback(switch_media_bug_t *bug, void *user_data, s
                        switch_channel_set_private(channel, rh->file, NULL);
                        
                        if (rh->fh) {
+                               if (switch_channel_test_flag(channel, CF_ANSWERED) || !switch_core_media_bug_test_flag(bug, SMBF_RECORD_ANSWER_REQ)) {
+                                       switch_size_t len;
+                                       uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
+                                       switch_frame_t frame = { 0 };
+                                       int cnt = LEAD_IN;
+                                       
+                                       frame.data = data;
+                                       frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
+                                       
+                                       for (; cnt; cnt--) {
+                                               if (switch_core_media_bug_read(bug, &frame, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
+                                                       len = (switch_size_t) frame.datalen / 2;
+                                                       switch_core_file_write(rh->fh, data, &len);
+                                               } 
+                                       }
+                                                  
+                               }
+
                                switch_core_file_close(rh->fh);
                                if (rh->fh->samples_out < read_impl.samples_per_second * 3) {
                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Discarding short file %s\n", rh->file);
@@ -457,6 +477,10 @@ static switch_bool_t record_callback(switch_media_bug_t *bug, void *user_data, s
 
                break;
        case SWITCH_ABC_TYPE_READ_PING:
+               if (rh->lead_in) {
+                       rh->lead_in--;
+               } else
+
                if (rh->fh) {
                        switch_size_t len;
                        uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
@@ -465,17 +489,15 @@ static switch_bool_t record_callback(switch_media_bug_t *bug, void *user_data, s
                        frame.data = data;
                        frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
 
-                       if (switch_core_media_bug_read(bug, &frame) == SWITCH_STATUS_SUCCESS) {
-                               int doit = 1;
-                               if (!switch_channel_test_flag(channel, CF_ANSWERED) && switch_core_media_bug_test_flag(bug, SMBF_RECORD_ANSWER_REQ)) {
-                                       doit = 0;
-                               }
-
-                               if (doit) {
+                       if (switch_channel_test_flag(channel, CF_ANSWERED) || !switch_core_media_bug_test_flag(bug, SMBF_RECORD_ANSWER_REQ)) {
+                               int loops = LEAD_IN;
+                               while (switch_core_media_bug_read(bug, &frame, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS && frame.datalen) {
                                        len = (switch_size_t) frame.datalen / 2;
                                        switch_core_file_write(rh->fh, data, &len);
+                                       if (!--loops) break;
                                }
                        }
+                       rh->lead_in = LEAD_IN;
                }
                break;
        case SWITCH_ABC_TYPE_WRITE:
@@ -528,7 +550,7 @@ static switch_bool_t eavesdrop_callback(switch_media_bug_t *bug, void *user_data
                break;
        case SWITCH_ABC_TYPE_READ_PING:
                if (ep->buffer) {                       
-                       if (switch_core_media_bug_read(bug, &frame) == SWITCH_STATUS_SUCCESS) {
+                       if (switch_core_media_bug_read(bug, &frame, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
                                switch_buffer_lock(ep->buffer);
                                switch_buffer_zwrite(ep->buffer, frame.data, frame.datalen);
                                switch_buffer_unlock(ep->buffer);
@@ -949,6 +971,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_session(switch_core_session_t
        rh = switch_core_session_alloc(session, sizeof(*rh));
        rh->fh = fh;
        rh->file = switch_core_session_strdup(session, file);
+       rh->lead_in = LEAD_IN;
 
        if ((status = switch_core_media_bug_add(session, record_callback, rh, to, flags, &bug)) != SWITCH_STATUS_SUCCESS) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error adding media bug for file %s\n", file);
@@ -991,8 +1014,8 @@ static switch_bool_t inband_dtmf_callback(switch_media_bug_t *bug, void *user_da
                                        switch_channel_queue_dtmf(channel, &dtmf);
                                        p++;
                                }
-                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "DTMF DETECTED: [%s]\n", digit_str);
                        }
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "DTMF DETECTED: [%s]\n", digit_str);
                        switch_core_media_bug_set_read_replace_frame(bug, frame);
                }
                break;
@@ -1942,7 +1965,7 @@ static switch_bool_t speech_callback(switch_media_bug_t *bug, void *user_data, s
                break;
        case SWITCH_ABC_TYPE_READ:
                if (sth->ah) {
-                       if (switch_core_media_bug_read(bug, &frame) == SWITCH_STATUS_SUCCESS) {
+                       if (switch_core_media_bug_read(bug, &frame, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
                                if (switch_core_asr_feed(sth->ah, frame.data, frame.datalen, &flags) != SWITCH_STATUS_SUCCESS) {
                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error Feeding Data\n");
                                        return SWITCH_FALSE;