From cfae7d1581b28aba24a1e9fb755fafcdf5d5d1db Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Tue, 30 May 2006 17:19:01 +0000 Subject: [PATCH] fix sample goodies git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@1525 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_core.h | 6 ++++++ src/include/switch_types.h | 6 ++++-- src/switch_core.c | 40 +++++++++++++++++++++++++++----------- src/switch_ivr.c | 30 +++++++++++++++++++++++----- 4 files changed, 64 insertions(+), 18 deletions(-) diff --git a/src/include/switch_core.h b/src/include/switch_core.h index f1dc58960d..4903516f9c 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -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); +/*! + \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 \param session the session to write to diff --git a/src/include/switch_types.h b/src/include/switch_types.h index d3c33dbb47..6b185ddd4c 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -229,7 +229,8 @@ typedef enum { SWITCH_STATUS_GENERR - A general Error SWITCH_STATUS_INUSE - An indication that requested resource is in use 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 */ typedef enum { @@ -245,7 +246,8 @@ typedef enum { SWITCH_STATUS_GENERR, SWITCH_STATUS_INUSE, SWITCH_STATUS_BREAK, - SWITCH_STATUS_SOCKERR + SWITCH_STATUS_SOCKERR, + SWITCH_STATUS_MORE_DATA } switch_status_t; diff --git a/src/switch_core.c b/src/switch_core.c index 08287391ff..08032bf612 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -1203,6 +1203,17 @@ static switch_status_t perform_write(switch_core_session_t *session, switch_fram 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, 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) { short *data = write_frame->data; - session->write_resampler->from_len = - switch_short_to_float(data, session->write_resampler->from, (int) write_frame->datalen / 2); + session->write_resampler->from_len = 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) switch_resample_process(session->write_resampler, session->write_resampler->from, session->write_resampler->from_len, session->write_resampler->to, 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->datalen = session->write_resampler->to_len * 2; + write_frame->datalen = write_frame->samples * 2; 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, write_frame->datalen, session->write_codec->implementation->bytes_per_frame); if ((status = - switch_buffer_create(session->pool, &session->raw_write_buffer, - bytes)) != SWITCH_STATUS_SUCCESS) { + switch_buffer_create(session->pool, &session->raw_write_buffer, bytes)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Write Buffer Failed!\n"); return status; } } + 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); return SWITCH_STATUS_MEMERR; } } + if (perfect) { - enc_frame = write_frame; 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.datalen, &session->enc_write_frame.rate, &flag); - + switch (status) { case SWITCH_STATUS_RESAMPLE: 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); uint32_t bytes = session->write_codec->implementation->bytes_per_frame; switch_size_t frames = (used / bytes); - + status = SWITCH_STATUS_SUCCESS; - if (frames) { + if (!frames) { + return status; + } else { switch_size_t x; for (x = 0; x < frames; x++) { if ((session->raw_write_frame.datalen = (uint32_t) switch_buffer_read(session->raw_write_buffer, session->raw_write_frame.data, bytes)) != 0) { - enc_frame = &session->raw_write_frame; session->raw_write_frame.rate = session->write_codec->implementation->samples_per_second; session->enc_write_frame.datalen = session->enc_write_frame.buflen; diff --git a/src/switch_ivr.c b/src/switch_ivr.c index 9069408827..3bca6bd888 100644 --- a/src/switch_ivr.c +++ b/src/switch_ivr.c @@ -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_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER); + switch_core_session_reset(session); 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", codec_name, fh->samplerate, fh->channels, read_codec->implementation->microseconds_per_frame / 1000); switch_core_file_close(fh); + switch_core_session_reset(session); 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_file_close(fh); - + switch_core_session_reset(session); return status; } @@ -261,7 +263,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess short abuf[960]; char dtmf[128]; uint32_t interval = 0, samples = 0; - uint32_t len = 0, ilen = 0; + uint32_t ilen = 0; switch_size_t olen = 0; switch_frame_t write_frame = {0}; 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_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER); + switch_core_session_reset(session); 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); interval = read_codec->implementation->microseconds_per_frame / 1000; - samples = read_codec->implementation->bytes_per_frame / 2; - len = samples * 2; + + codec_name = "L16"; 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", codec_name, fh->samplerate, fh->channels, interval); switch_core_file_close(fh); + switch_core_session_reset(session); return SWITCH_STATUS_GENERR; } + samples = codec.implementation->bytes_per_frame / 2; + if (timer_name) { + uint32_t len; + + len = samples * 2; 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_core_codec_destroy(&codec); switch_core_file_close(fh); + switch_core_session_reset(session); return SWITCH_STATUS_GENERR; } 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 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"); done = 1; break; @@ -447,6 +461,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess break; } } + if (timer_name) { if (switch_core_timer_next(&timer) < 0) { 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_session_reset(session); return status; } @@ -525,6 +541,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_speak_text(switch_core_session_t *ses &flags, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid TTS module!\n"); + switch_core_session_reset(session); 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); flags = 0; switch_core_speech_close(&sh, &flags); + switch_core_session_reset(session); 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); flags = 0; switch_core_speech_close(&sh, &flags); + switch_core_session_reset(session); return SWITCH_STATUS_GENERR; } 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_session_reset(session); return status; }