提交 02099953 authored 作者: Michael Jerris's avatar Michael Jerris

MODFORM-21 add a bit more error checking to mod_shout write function

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@10538 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 1a598b6d
...@@ -902,12 +902,22 @@ static switch_status_t shout_file_read(switch_file_handle_t *handle, void *data, ...@@ -902,12 +902,22 @@ static switch_status_t shout_file_read(switch_file_handle_t *handle, void *data,
static switch_status_t shout_file_write(switch_file_handle_t *handle, void *data, size_t *len) static switch_status_t shout_file_write(switch_file_handle_t *handle, void *data, size_t *len)
{ {
shout_context_t *context = handle->private_info; shout_context_t *context;
unsigned char mp3buf[8192] = ""; unsigned char mp3buf[8192] = "";
int rlen; int rlen;
int16_t *audio = data; int16_t *audio = data;
int nsamples = *len; int nsamples = *len;
if (!handle) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error no handle\n");
return SWITCH_STATUS_FALSE;
}
if (!(context = handle->private_info)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error no context\n");
return SWITCH_STATUS_FALSE;
}
if (context->shout && !context->shout_init) { if (context->shout && !context->shout_init) {
context->shout_init++; context->shout_init++;
if (shout_open(context->shout) != SHOUTERR_SUCCESS) { if (shout_open(context->shout) != SHOUTERR_SUCCESS) {
...@@ -918,7 +928,7 @@ static switch_status_t shout_file_write(switch_file_handle_t *handle, void *data ...@@ -918,7 +928,7 @@ static switch_status_t shout_file_write(switch_file_handle_t *handle, void *data
launch_write_stream_thread(context); launch_write_stream_thread(context);
} }
if (handle->handler) { if (handle->handler && context->audio_mutex) {
switch_mutex_lock(context->audio_mutex); switch_mutex_lock(context->audio_mutex);
if (context->audio_buffer) { if (context->audio_buffer) {
if (!switch_buffer_write(context->audio_buffer, data, (nsamples * sizeof(int16_t) * handle->channels))) { if (!switch_buffer_write(context->audio_buffer, data, (nsamples * sizeof(int16_t) * handle->channels))) {
...@@ -928,47 +938,50 @@ static switch_status_t shout_file_write(switch_file_handle_t *handle, void *data ...@@ -928,47 +938,50 @@ static switch_status_t shout_file_write(switch_file_handle_t *handle, void *data
} else { } else {
context->err++; context->err++;
} }
switch_mutex_unlock(context->audio_mutex); switch_mutex_unlock(context->audio_mutex);
if (context->err) { if (context->err) {
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
} else {
if (!context->lame_ready) { handle->sample_count += *len;
lame_init_params(context->gfp); return SWITCH_STATUS_SUCCESS;
lame_print_config(context->gfp); }
context->lame_ready = 1;
}
if (handle->channels == 2) { if (!context->lame_ready) {
int16_t l[4096] = { 0 }; lame_init_params(context->gfp);
int16_t r[4096] = { 0 }; lame_print_config(context->gfp);
int i, j = 0; context->lame_ready = 1;
}
for (i = 0; i < nsamples; i++) { if (handle->channels == 2) {
l[i] = audio[j++]; int16_t l[4096] = { 0 };
r[i] = audio[j++]; int16_t r[4096] = { 0 };
} int i, j = 0;
if ((rlen = lame_encode_buffer(context->gfp, l, r, nsamples, mp3buf, sizeof(mp3buf))) < 0) { for (i = 0; i < nsamples; i++) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MP3 encode error %d!\n", rlen); l[i] = audio[j++];
return SWITCH_STATUS_FALSE; r[i] = audio[j++];
} }
} else if (handle->channels == 1) { if ((rlen = lame_encode_buffer(context->gfp, l, r, nsamples, mp3buf, sizeof(mp3buf))) < 0) {
if ((rlen = lame_encode_buffer(context->gfp, audio, NULL, nsamples, mp3buf, sizeof(mp3buf))) < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MP3 encode error %d!\n", rlen);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MP3 encode error %d!\n", rlen); return SWITCH_STATUS_FALSE;
return SWITCH_STATUS_FALSE;
}
} else {
rlen = 0;
} }
if (rlen) { } else if (handle->channels == 1) {
int ret = fwrite(mp3buf, 1, rlen, context->fp); if ((rlen = lame_encode_buffer(context->gfp, audio, NULL, nsamples, mp3buf, sizeof(mp3buf))) < 0) {
if (ret < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MP3 encode error %d!\n", rlen);
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
} else {
rlen = 0;
}
if (rlen) {
int ret = fwrite(mp3buf, 1, rlen, context->fp);
if (ret < 0) {
return SWITCH_STATUS_FALSE;
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论