diff --git a/src/switch_core_media_bug.c b/src/switch_core_media_bug.c index 82b75b7dc3..6d47806f41 100644 --- a/src/switch_core_media_bug.c +++ b/src/switch_core_media_bug.c @@ -104,7 +104,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_bool_t fill) { - switch_size_t bytes = 0, datalen = 0; + switch_size_t bytes = 0, datalen = 0, ttl = 0; int16_t *dp, *fp; uint32_t x; size_t rlen = 0; @@ -126,11 +126,13 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(switch_media_bug_t *b switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s Buffer Error\n", switch_channel_get_name(bug->session->channel)); return SWITCH_STATUS_FALSE; } - + + frame->flags = 0; frame->datalen = 0; switch_mutex_lock(bug->read_mutex); frame->datalen = (uint32_t) switch_buffer_read(bug->raw_read_buffer, frame->data, bytes); + ttl += frame->datalen; if (fill && frame->datalen < bytes) { memset(((unsigned char *)frame->data) + frame->datalen, 0, bytes - frame->datalen); frame->datalen = bytes; @@ -141,6 +143,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); + ttl += datalen; if (fill && datalen < bytes) { memset(((unsigned char *)bug->data) + datalen, 0, bytes - datalen); datalen = bytes; @@ -192,6 +195,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(switch_media_bug_t *b } } + if (!ttl) { + switch_set_flag(frame, SFF_CNG); + } + frame->datalen = bytes; frame->samples = bytes / sizeof(int16_t); frame->rate = read_impl.actual_samples_per_second; diff --git a/src/switch_ivr_async.c b/src/switch_ivr_async.c index 90692ad60b..6dfbe5624f 100644 --- a/src/switch_ivr_async.c +++ b/src/switch_ivr_async.c @@ -486,11 +486,12 @@ static switch_bool_t record_callback(switch_media_bug_t *bug, void *user_data, s frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE; 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; + if (switch_test_flag((&frame), SFF_CNG)) { + break; + } } } rh->lead_in = LEAD_IN;