refactor record code in the core

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12357 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale 2009-03-02 19:30:41 +00:00
parent 34f5a778ca
commit 2246250584
2 changed files with 70 additions and 87 deletions

View File

@ -104,105 +104,88 @@ 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)
{ {
uint32_t bytes = 0; switch_size_t bytes = 0, datalen = 0;
uint32_t datalen = 0;
int16_t *dp, *fp; int16_t *dp, *fp;
uint32_t x; uint32_t x;
size_t rlen = 0; size_t rlen = 0;
size_t wlen = 0; size_t wlen = 0;
uint32_t blen; uint32_t blen;
size_t rdlen = 0;
uint32_t maxlen;
switch_codec_implementation_t read_impl = {0}; switch_codec_implementation_t read_impl = {0};
int16_t *tp;
switch_core_session_get_read_impl(bug->session, &read_impl); switch_core_session_get_read_impl(bug->session, &read_impl);
if (bug->raw_read_buffer) { bytes = read_impl.decoded_bytes_per_packet;
rlen = switch_buffer_inuse(bug->raw_read_buffer);
}
if (bug->raw_write_buffer) { if (frame->buflen < bytes) {
wlen = switch_buffer_inuse(bug->raw_write_buffer); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s frame buffer too small!\n", switch_channel_get_name(bug->session->channel));
}
if ((bug->raw_read_buffer && bug->raw_write_buffer) && (!rlen && !wlen)) {
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
maxlen = SWITCH_RECOMMENDED_BUFFER_SIZE > frame->buflen ? frame->buflen : SWITCH_RECOMMENDED_BUFFER_SIZE; if (!(bug->raw_read_buffer && bug->raw_write_buffer)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%sBuffer Error\n", switch_channel_get_name(bug->session->channel));
if ((rdlen = rlen > wlen ? wlen : rlen) > maxlen) {
rdlen = maxlen;
} }
if (!rdlen) {
rdlen = maxlen;
}
frame->datalen = 0; frame->datalen = 0;
if (rlen) { switch_mutex_lock(bug->read_mutex);
switch_mutex_lock(bug->read_mutex); frame->datalen = (uint32_t) switch_buffer_read(bug->raw_read_buffer, frame->data, bytes);
if (frame->datalen < bytes) {
frame->datalen = (uint32_t) switch_buffer_read(bug->raw_read_buffer, frame->data, rdlen); memset(((unsigned char *)frame->data) + frame->datalen, 0, bytes - frame->datalen);
switch_mutex_unlock(bug->read_mutex); frame->datalen = bytes;
} }
switch_mutex_unlock(bug->read_mutex);
if (wlen) {
switch_mutex_lock(bug->write_mutex); switch_mutex_lock(bug->write_mutex);
datalen = (uint32_t) switch_buffer_read(bug->raw_write_buffer, bug->data, rdlen); datalen = (uint32_t) switch_buffer_read(bug->raw_write_buffer, bug->data, bytes);
switch_mutex_unlock(bug->write_mutex); if (datalen < bytes) {
memset(((unsigned char *)bug->data) + datalen, 0, bytes - datalen);
datalen = bytes;
} }
switch_mutex_unlock(bug->write_mutex);
tp = bug->tmp;
bytes = (datalen > frame->datalen) ? datalen : frame->datalen; dp = (int16_t *) bug->data;
switch_assert(bytes <= maxlen); fp = (int16_t *) frame->data;
rlen = frame->datalen / 2;
if (bytes) { wlen = datalen / 2;
int16_t *tp = bug->tmp; blen = bytes / 2;
dp = (int16_t *) bug->data; if (switch_test_flag(bug, SMBF_STEREO)) {
fp = (int16_t *) frame->data; for (x = 0; x < blen; x++) {
rlen = frame->datalen / 2; if (x < rlen) {
wlen = datalen / 2; *(tp++) = *(fp + x);
blen = bytes / 2; } else {
*(tp++) = 0;
if (switch_test_flag(bug, SMBF_STEREO)) {
for (x = 0; x < blen; x++) {
if (x < rlen) {
*(tp++) = *(fp + x);
} else {
*(tp++) = 0;
}
if (x < wlen) {
*(tp++) = *(dp + x);
} else {
*(tp++) = 0;
}
} }
memcpy(frame->data, bug->tmp, bytes * 2); if (x < wlen) {
} else { *(tp++) = *(dp + x);
for (x = 0; x < blen; x++) { } else {
int32_t z = 0; *(tp++) = 0;
if (x < rlen) {
z += (int32_t) *(fp + x);
}
if (x < wlen) {
z += (int32_t) *(dp + x);
}
switch_normalize_to_16bit(z);
*(fp + x) = (int16_t) z;
} }
} }
memcpy(frame->data, bug->tmp, bytes * 2);
} else {
for (x = 0; x < blen; x++) {
int32_t z = 0;
frame->datalen = bytes; if (x < rlen) {
frame->samples = bytes / sizeof(int16_t); z += (int32_t) *(fp + x);
frame->rate = read_impl.actual_samples_per_second; }
if (x < wlen) {
return SWITCH_STATUS_SUCCESS; z += (int32_t) *(dp + x);
}
switch_normalize_to_16bit(z);
*(fp + x) = (int16_t) z / 2;
}
} }
return SWITCH_STATUS_FALSE; frame->datalen = bytes;
frame->samples = bytes / sizeof(int16_t);
frame->rate = read_impl.actual_samples_per_second;
frame->codec = NULL;
return SWITCH_STATUS_SUCCESS;
} }
#define MAX_BUG_BUFFER 1024 * 512 #define MAX_BUG_BUFFER 1024 * 512

View File

@ -413,11 +413,6 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_displace_session(switch_core_session_
static switch_bool_t record_callback(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type) static switch_bool_t record_callback(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type)
{ {
switch_file_handle_t *fh = (switch_file_handle_t *) user_data; switch_file_handle_t *fh = (switch_file_handle_t *) user_data;
uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
switch_frame_t frame = { 0 };
frame.data = data;
frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
switch (type) { switch (type) {
case SWITCH_ABC_TYPE_INIT: case SWITCH_ABC_TYPE_INIT:
@ -427,11 +422,16 @@ static switch_bool_t record_callback(switch_media_bug_t *bug, void *user_data, s
switch_core_file_close(fh); switch_core_file_close(fh);
} }
break; break;
case SWITCH_ABC_TYPE_READ: case SWITCH_ABC_TYPE_READ_PING:
if (fh) { if (fh) {
switch_size_t len; switch_size_t len;
switch_core_session_t *session = switch_core_media_bug_get_session(bug); switch_core_session_t *session = switch_core_media_bug_get_session(bug);
switch_channel_t *channel = switch_core_session_get_channel(session); switch_channel_t *channel = switch_core_session_get_channel(session);
uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
switch_frame_t frame = { 0 };
frame.data = data;
frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
if (switch_core_media_bug_read(bug, &frame) == SWITCH_STATUS_SUCCESS) { if (switch_core_media_bug_read(bug, &frame) == SWITCH_STATUS_SUCCESS) {
int doit = 1; int doit = 1;
@ -441,7 +441,7 @@ static switch_bool_t record_callback(switch_media_bug_t *bug, void *user_data, s
if (doit) { if (doit) {
len = (switch_size_t) frame.datalen / 2; len = (switch_size_t) frame.datalen / 2;
switch_core_file_write(fh, frame.data, &len); switch_core_file_write(fh, data, &len);
} }
} }
} }
@ -482,7 +482,7 @@ static switch_bool_t eavesdrop_callback(switch_media_bug_t *bug, void *user_data
struct eavesdrop_pvt *ep = (struct eavesdrop_pvt *) user_data; struct eavesdrop_pvt *ep = (struct eavesdrop_pvt *) user_data;
uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE]; uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
switch_frame_t frame = { 0 }; switch_frame_t frame = { 0 };
frame.data = data; frame.data = data;
frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE; frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
@ -494,7 +494,7 @@ static switch_bool_t eavesdrop_callback(switch_media_bug_t *bug, void *user_data
case SWITCH_ABC_TYPE_WRITE: case SWITCH_ABC_TYPE_WRITE:
break; break;
case SWITCH_ABC_TYPE_READ_PING: case SWITCH_ABC_TYPE_READ_PING:
if (ep->buffer) { if (ep->buffer) {
if (switch_core_media_bug_read(bug, &frame) == SWITCH_STATUS_SUCCESS) { if (switch_core_media_bug_read(bug, &frame) == SWITCH_STATUS_SUCCESS) {
switch_buffer_lock(ep->buffer); switch_buffer_lock(ep->buffer);
switch_buffer_zwrite(ep->buffer, frame.data, frame.datalen); switch_buffer_zwrite(ep->buffer, frame.data, frame.datalen);
@ -821,7 +821,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_session(switch_core_session_t
switch_media_bug_t *bug; switch_media_bug_t *bug;
switch_status_t status; switch_status_t status;
time_t to = 0; time_t to = 0;
switch_media_bug_flag_t flags = SMBF_READ_STREAM | SMBF_WRITE_STREAM; switch_media_bug_flag_t flags = SMBF_READ_STREAM | SMBF_WRITE_STREAM | SMBF_READ_PING;
uint8_t channels; uint8_t channels;
switch_codec_implementation_t read_impl = {0}; switch_codec_implementation_t read_impl = {0};
switch_core_session_get_read_impl(session, &read_impl); switch_core_session_get_read_impl(session, &read_impl);