diff --git a/src/include/private/switch_core_pvt.h b/src/include/private/switch_core_pvt.h index 7e3659b09d..ff0a6189b9 100644 --- a/src/include/private/switch_core_pvt.h +++ b/src/include/private/switch_core_pvt.h @@ -196,6 +196,8 @@ struct switch_media_bug { switch_codec_implementation_t read_impl; switch_codec_implementation_t write_impl; uint32_t record_frame_size; + uint32_t record_pre_buffer_count; + uint32_t record_pre_buffer_max; switch_frame_t *ping_frame; struct switch_media_bug *next; }; diff --git a/src/include/switch_core.h b/src/include/switch_core.h index 2558e1bb19..517e13ed94 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -270,6 +270,8 @@ SWITCH_DECLARE(void) switch_core_media_bug_flush(_In_ switch_media_bug_t *bug); */ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_flush_all(_In_ switch_core_session_t *session); +SWITCH_DECLARE(switch_status_t) switch_core_media_bug_set_pre_buffer_framecount(switch_media_bug_t *bug, uint32_t framecount); + ///\} ///\defgroup pa1 Port Allocation diff --git a/src/switch_core_media_bug.c b/src/switch_core_media_bug.c index c01d823963..0397aa666b 100644 --- a/src/switch_core_media_bug.c +++ b/src/switch_core_media_bug.c @@ -112,6 +112,9 @@ SWITCH_DECLARE(void *) switch_core_media_bug_get_user_data(switch_media_bug_t *b SWITCH_DECLARE(void) switch_core_media_bug_flush(switch_media_bug_t *bug) { + + bug->record_pre_buffer_count = 0; + if (bug->raw_read_buffer) { switch_mutex_lock(bug->read_mutex); switch_buffer_zero(bug->raw_read_buffer); @@ -144,6 +147,13 @@ SWITCH_DECLARE(void) switch_core_media_bug_inuse(switch_media_bug_t *bug, switch } } +SWITCH_DECLARE(switch_status_t) switch_core_media_bug_set_pre_buffer_framecount(switch_media_bug_t *bug, uint32_t framecount) +{ + bug->record_pre_buffer_max = framecount; + + return SWITCH_STATUS_SUCCESS; +} + 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; @@ -188,6 +198,11 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(switch_media_bug_t *b do_write = switch_buffer_inuse(bug->raw_write_buffer); switch_mutex_unlock(bug->write_mutex); } + + if (bug->record_frame_size && bug->record_pre_buffer_max && (do_read || do_write) && bug->record_pre_buffer_count < bug->record_pre_buffer_max) { + bug->record_pre_buffer_count++; + return SWITCH_STATUS_FALSE; + } if (bug->record_frame_size) { if ((do_read && do_read < bug->record_frame_size) || (do_write && do_write < bug->record_frame_size)) { @@ -212,7 +227,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(switch_media_bug_t *b bug->record_frame_size = do_read; } } - + fill_read = !do_read; fill_write = !do_write; @@ -220,6 +235,14 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(switch_media_bug_t *b return SWITCH_STATUS_FALSE; } + if (do_read && do_read > SWITCH_RECOMMENDED_BUFFER_SIZE) { + do_read = 1280; + } + + if (do_write && do_write > SWITCH_RECOMMENDED_BUFFER_SIZE) { + do_write = 1280; + } + if (do_read) { switch_mutex_lock(bug->read_mutex); frame->datalen = (uint32_t) switch_buffer_read(bug->raw_read_buffer, frame->data, do_read); diff --git a/src/switch_ivr_async.c b/src/switch_ivr_async.c index 7b6cd1271c..9971ab68e0 100644 --- a/src/switch_ivr_async.c +++ b/src/switch_ivr_async.c @@ -1765,6 +1765,16 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_session(switch_core_session_t return status; } + if ((p = switch_channel_get_variable(channel, "RECORD_PRE_BUFFER_FRAMES"))) { + int tmp = atoi(p); + + if (tmp > 0) { + switch_core_media_bug_set_pre_buffer_framecount(bug, tmp); + } + } else { + switch_core_media_bug_set_pre_buffer_framecount(bug, 25); + } + switch_channel_set_private(channel, file, bug); return SWITCH_STATUS_SUCCESS;