fix sample goodies

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@1525 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale 2006-05-30 17:19:01 +00:00
parent ac8a3e43ea
commit cfae7d1581
4 changed files with 64 additions and 18 deletions

View File

@ -432,6 +432,12 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_queue_event(switch_core_sess
*/ */
SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_session_t *session, switch_frame_t **frame, int timeout, int stream_id); SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_session_t *session, switch_frame_t **frame, int timeout, int stream_id);
/*!
\brief Reset the buffers and resampler on a session
\param session the session to reset
*/
SWITCH_DECLARE(void) switch_core_session_reset(switch_core_session_t *session);
/*! /*!
\brief Write a frame to a session \brief Write a frame to a session
\param session the session to write to \param session the session to write to

View File

@ -229,7 +229,8 @@ typedef enum {
SWITCH_STATUS_GENERR - A general Error SWITCH_STATUS_GENERR - A general Error
SWITCH_STATUS_INUSE - An indication that requested resource is in use SWITCH_STATUS_INUSE - An indication that requested resource is in use
SWITCH_STATUS_BREAK - A non-fatal break of an operation SWITCH_STATUS_BREAK - A non-fatal break of an operation
SWITCH_STATUS_SOCKERR - A socket error SWITCH_STATUS_SOCKERR - A socket error
SWITCH_STATUS_MORE_DATA - Need More Data
</pre> </pre>
*/ */
typedef enum { typedef enum {
@ -245,7 +246,8 @@ typedef enum {
SWITCH_STATUS_GENERR, SWITCH_STATUS_GENERR,
SWITCH_STATUS_INUSE, SWITCH_STATUS_INUSE,
SWITCH_STATUS_BREAK, SWITCH_STATUS_BREAK,
SWITCH_STATUS_SOCKERR SWITCH_STATUS_SOCKERR,
SWITCH_STATUS_MORE_DATA
} switch_status_t; } switch_status_t;

View File

@ -1203,6 +1203,17 @@ static switch_status_t perform_write(switch_core_session_t *session, switch_fram
return status; return status;
} }
SWITCH_DECLARE(void) switch_core_session_reset(switch_core_session_t *session)
{
/* sweep theese under the rug, they wont be leaked they will be reclaimed
when the session ends.
*/
session->raw_write_buffer = NULL;
session->raw_read_buffer = NULL;
session->read_resampler = NULL;
session->write_resampler = NULL;
}
SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_session_t *session, switch_frame_t *frame, SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_session_t *session, switch_frame_t *frame,
int timeout, int stream_id) int timeout, int stream_id)
{ {
@ -1284,15 +1295,21 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess
if (session->write_resampler) { if (session->write_resampler) {
short *data = write_frame->data; short *data = write_frame->data;
session->write_resampler->from_len = session->write_resampler->from_len = write_frame->datalen / 2;
switch_short_to_float(data, session->write_resampler->from, (int) write_frame->datalen / 2); switch_short_to_float(data, session->write_resampler->from, session->write_resampler->from_len);
session->write_resampler->to_len = (uint32_t) session->write_resampler->to_len = (uint32_t)
switch_resample_process(session->write_resampler, session->write_resampler->from, switch_resample_process(session->write_resampler, session->write_resampler->from,
session->write_resampler->from_len, session->write_resampler->to, session->write_resampler->from_len, session->write_resampler->to,
session->write_resampler->to_size, 0); session->write_resampler->to_size, 0);
switch_float_to_short(session->write_resampler->to, data, write_frame->datalen * 2);
switch_float_to_short(session->write_resampler->to, data, session->write_resampler->to_len);
write_frame->samples = session->write_resampler->to_len; write_frame->samples = session->write_resampler->to_len;
write_frame->datalen = session->write_resampler->to_len * 2; write_frame->datalen = write_frame->samples * 2;
write_frame->rate = session->write_resampler->to_rate; write_frame->rate = session->write_resampler->to_rate;
} }
@ -1308,20 +1325,20 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess
bytes, bytes,
write_frame->datalen, session->write_codec->implementation->bytes_per_frame); write_frame->datalen, session->write_codec->implementation->bytes_per_frame);
if ((status = if ((status =
switch_buffer_create(session->pool, &session->raw_write_buffer, switch_buffer_create(session->pool, &session->raw_write_buffer, bytes)) != SWITCH_STATUS_SUCCESS) {
bytes)) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Write Buffer Failed!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Write Buffer Failed!\n");
return status; return status;
} }
} }
if (!(switch_buffer_write(session->raw_write_buffer, write_frame->data, write_frame->datalen))) { if (!(switch_buffer_write(session->raw_write_buffer, write_frame->data, write_frame->datalen))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Write Buffer %u bytes Failed!\n", write_frame->datalen); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Write Buffer %u bytes Failed!\n", write_frame->datalen);
return SWITCH_STATUS_MEMERR; return SWITCH_STATUS_MEMERR;
} }
} }
if (perfect) { if (perfect) {
enc_frame = write_frame; enc_frame = write_frame;
session->enc_write_frame.datalen = session->enc_write_frame.buflen; session->enc_write_frame.datalen = session->enc_write_frame.buflen;
@ -1333,7 +1350,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess
session->enc_write_frame.data, session->enc_write_frame.data,
&session->enc_write_frame.datalen, &session->enc_write_frame.datalen,
&session->enc_write_frame.rate, &flag); &session->enc_write_frame.rate, &flag);
switch (status) { switch (status) {
case SWITCH_STATUS_RESAMPLE: case SWITCH_STATUS_RESAMPLE:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "fixme 2\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "fixme 2\n");
@ -1357,14 +1374,15 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess
switch_size_t used = switch_buffer_inuse(session->raw_write_buffer); switch_size_t used = switch_buffer_inuse(session->raw_write_buffer);
uint32_t bytes = session->write_codec->implementation->bytes_per_frame; uint32_t bytes = session->write_codec->implementation->bytes_per_frame;
switch_size_t frames = (used / bytes); switch_size_t frames = (used / bytes);
status = SWITCH_STATUS_SUCCESS; status = SWITCH_STATUS_SUCCESS;
if (frames) { if (!frames) {
return status;
} else {
switch_size_t x; switch_size_t x;
for (x = 0; x < frames; x++) { for (x = 0; x < frames; x++) {
if ((session->raw_write_frame.datalen = (uint32_t) if ((session->raw_write_frame.datalen = (uint32_t)
switch_buffer_read(session->raw_write_buffer, session->raw_write_frame.data, bytes)) != 0) { switch_buffer_read(session->raw_write_buffer, session->raw_write_frame.data, bytes)) != 0) {
enc_frame = &session->raw_write_frame; enc_frame = &session->raw_write_frame;
session->raw_write_frame.rate = session->write_codec->implementation->samples_per_second; session->raw_write_frame.rate = session->write_codec->implementation->samples_per_second;
session->enc_write_frame.datalen = session->enc_write_frame.buflen; session->enc_write_frame.datalen = session->enc_write_frame.buflen;

View File

@ -186,6 +186,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se
SWITCH_FILE_FLAG_WRITE | SWITCH_FILE_DATA_SHORT, SWITCH_FILE_FLAG_WRITE | SWITCH_FILE_DATA_SHORT,
switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER); switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
switch_core_session_reset(session);
return SWITCH_STATUS_GENERR; return SWITCH_STATUS_GENERR;
} }
@ -206,6 +207,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Raw Codec Activation Failed %s@%uhz %u channels %dms\n", switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Raw Codec Activation Failed %s@%uhz %u channels %dms\n",
codec_name, fh->samplerate, fh->channels, read_codec->implementation->microseconds_per_frame / 1000); codec_name, fh->samplerate, fh->channels, read_codec->implementation->microseconds_per_frame / 1000);
switch_core_file_close(fh); switch_core_file_close(fh);
switch_core_session_reset(session);
return SWITCH_STATUS_GENERR; return SWITCH_STATUS_GENERR;
} }
@ -245,7 +247,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se
switch_core_session_set_read_codec(session, read_codec); switch_core_session_set_read_codec(session, read_codec);
switch_core_file_close(fh); switch_core_file_close(fh);
switch_core_session_reset(session);
return status; return status;
} }
@ -261,7 +263,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess
short abuf[960]; short abuf[960];
char dtmf[128]; char dtmf[128];
uint32_t interval = 0, samples = 0; uint32_t interval = 0, samples = 0;
uint32_t len = 0, ilen = 0; uint32_t ilen = 0;
switch_size_t olen = 0; switch_size_t olen = 0;
switch_frame_t write_frame = {0}; switch_frame_t write_frame = {0};
switch_timer_t timer; switch_timer_t timer;
@ -287,6 +289,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess
SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT,
switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER); switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
switch_core_session_reset(session);
return SWITCH_STATUS_GENERR; return SWITCH_STATUS_GENERR;
} }
@ -298,8 +301,8 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "OPEN FILE %s %uhz %u channels\n", file, fh->samplerate, fh->channels); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "OPEN FILE %s %uhz %u channels\n", file, fh->samplerate, fh->channels);
interval = read_codec->implementation->microseconds_per_frame / 1000; interval = read_codec->implementation->microseconds_per_frame / 1000;
samples = read_codec->implementation->bytes_per_frame / 2;
len = samples * 2;
codec_name = "L16"; codec_name = "L16";
if (switch_core_codec_init(&codec, if (switch_core_codec_init(&codec,
@ -315,14 +318,21 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Raw Codec Activation Failed %s@%uhz %u channels %dms\n", switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Raw Codec Activation Failed %s@%uhz %u channels %dms\n",
codec_name, fh->samplerate, fh->channels, interval); codec_name, fh->samplerate, fh->channels, interval);
switch_core_file_close(fh); switch_core_file_close(fh);
switch_core_session_reset(session);
return SWITCH_STATUS_GENERR; return SWITCH_STATUS_GENERR;
} }
samples = codec.implementation->bytes_per_frame / 2;
if (timer_name) { if (timer_name) {
uint32_t len;
len = samples * 2;
if (switch_core_timer_init(&timer, timer_name, interval, samples, pool) != SWITCH_STATUS_SUCCESS) { if (switch_core_timer_init(&timer, timer_name, interval, samples, pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setup timer failed!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setup timer failed!\n");
switch_core_codec_destroy(&codec); switch_core_codec_destroy(&codec);
switch_core_file_close(fh); switch_core_file_close(fh);
switch_core_session_reset(session);
return SWITCH_STATUS_GENERR; return SWITCH_STATUS_GENERR;
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "setup timer success %u bytes per %d ms!\n", len, interval); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "setup timer success %u bytes per %d ms!\n", len, interval);
@ -437,8 +447,12 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess
#endif #endif
for (stream_id = 0; stream_id < switch_core_session_get_stream_count(session); stream_id++) { for (stream_id = 0; stream_id < switch_core_session_get_stream_count(session); stream_id++) {
status = switch_core_session_write_frame(session, &write_frame, -1, stream_id);
if (switch_core_session_write_frame(session, &write_frame, -1, stream_id) != SWITCH_STATUS_SUCCESS) { if (status == SWITCH_STATUS_MORE_DATA) {
status = SWITCH_STATUS_SUCCESS;
continue;
} else if (status != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Bad Write\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Bad Write\n");
done = 1; done = 1;
break; break;
@ -447,6 +461,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess
break; break;
} }
} }
if (timer_name) { if (timer_name) {
if (switch_core_timer_next(&timer) < 0) { if (switch_core_timer_next(&timer) < 0) {
break; break;
@ -474,6 +489,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess
switch_core_timer_destroy(&timer); switch_core_timer_destroy(&timer);
} }
switch_core_session_reset(session);
return status; return status;
} }
@ -525,6 +541,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_speak_text(switch_core_session_t *ses
&flags, &flags,
switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid TTS module!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid TTS module!\n");
switch_core_session_reset(session);
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
@ -556,6 +573,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_speak_text(switch_core_session_t *ses
codec_name, rate, interval); codec_name, rate, interval);
flags = 0; flags = 0;
switch_core_speech_close(&sh, &flags); switch_core_speech_close(&sh, &flags);
switch_core_session_reset(session);
return SWITCH_STATUS_GENERR; return SWITCH_STATUS_GENERR;
} }
@ -565,6 +583,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_speak_text(switch_core_session_t *ses
switch_core_codec_destroy(&codec); switch_core_codec_destroy(&codec);
flags = 0; flags = 0;
switch_core_speech_close(&sh, &flags); switch_core_speech_close(&sh, &flags);
switch_core_session_reset(session);
return SWITCH_STATUS_GENERR; return SWITCH_STATUS_GENERR;
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "setup timer success %u bytes per %d ms!\n", len, interval); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "setup timer success %u bytes per %d ms!\n", len, interval);
@ -687,6 +706,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_speak_text(switch_core_session_t *ses
switch_core_timer_destroy(&timer); switch_core_timer_destroy(&timer);
} }
switch_core_session_reset(session);
return status; return status;
} }