From e7c060b8b9ce2ec073545675d878d084606ac561 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 28 Dec 2005 20:06:03 +0000 Subject: [PATCH] add channels arg to codec git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@227 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_core.h | 2 +- src/mod/mod_exosip/mod_exosip.c | 4 +++ src/mod/mod_iaxchan/mod_iaxchan.c | 2 ++ src/mod/mod_playback/mod_playback.c | 34 +++++++++++++---------- src/mod/mod_portaudio/mod_portaudio.c | 2 ++ src/mod/mod_rawaudio/mod_rawaudio.c | 23 +++++++++++++-- src/mod/mod_woomerachan/mod_woomerachan.c | 4 +-- src/switch_core.c | 6 ++-- 8 files changed, 55 insertions(+), 22 deletions(-) diff --git a/src/include/switch_core.h b/src/include/switch_core.h index fdf3e19795..824c8a9c91 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -101,7 +101,7 @@ SWITCH_DECLARE(switch_status) switch_core_session_add_event_hook_kill_channel(sw SWITCH_DECLARE(switch_status) switch_core_session_add_event_hook_waitfor_read(switch_core_session *session, switch_waitfor_read_hook waitfor_read); SWITCH_DECLARE(switch_status) switch_core_session_add_event_hook_waitfor_write(switch_core_session *session, switch_waitfor_write_hook waitfor_write); SWITCH_DECLARE(switch_status) switch_core_session_add_event_hook_send_dtmf(switch_core_session *session, switch_send_dtmf_hook send_dtmf); -SWITCH_DECLARE(switch_status) switch_core_codec_init(switch_codec *codec, char *codec_name, int rate, int ms, switch_codec_flag flags, const switch_codec_settings *codec_settings, switch_memory_pool *pool); +SWITCH_DECLARE(switch_status) switch_core_codec_init(switch_codec *codec, char *codec_name, int rate, int ms, int channels, switch_codec_flag flags, const switch_codec_settings *codec_settings, switch_memory_pool *pool); SWITCH_DECLARE(switch_status) switch_core_codec_encode(switch_codec *codec, switch_codec *other_codec, void *decoded_data, diff --git a/src/mod/mod_exosip/mod_exosip.c b/src/mod/mod_exosip/mod_exosip.c index ee9e17c308..59bd5529ae 100644 --- a/src/mod/mod_exosip/mod_exosip.c +++ b/src/mod/mod_exosip/mod_exosip.c @@ -890,6 +890,7 @@ static switch_status exosip_create_call(eXosip_event_t *event) dname, rate, globals.codec_ms, + 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { @@ -901,6 +902,7 @@ static switch_status exosip_create_call(eXosip_event_t *event) dname, rate, globals.codec_ms, + 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { @@ -1052,6 +1054,7 @@ static void handle_answer(eXosip_event_t *event) dname, rate, globals.codec_ms, + 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { @@ -1063,6 +1066,7 @@ static void handle_answer(eXosip_event_t *event) dname, rate, globals.codec_ms, + 1, SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { diff --git a/src/mod/mod_iaxchan/mod_iaxchan.c b/src/mod/mod_iaxchan/mod_iaxchan.c index 24b46dbd62..7ddfbc7a81 100644 --- a/src/mod/mod_iaxchan/mod_iaxchan.c +++ b/src/mod/mod_iaxchan/mod_iaxchan.c @@ -275,6 +275,7 @@ static switch_status iax_set_codec(struct private_object *tech_pvt, struct iax_s dname, 0, 0, + 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { @@ -285,6 +286,7 @@ static switch_status iax_set_codec(struct private_object *tech_pvt, struct iax_s dname, 0, 0, + 1, SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { diff --git a/src/mod/mod_playback/mod_playback.c b/src/mod/mod_playback/mod_playback.c index 2287c42775..aac2922181 100644 --- a/src/mod/mod_playback/mod_playback.c +++ b/src/mod/mod_playback/mod_playback.c @@ -71,37 +71,41 @@ void playback_function(switch_core_session *session, char *data) write_frame.buflen = sizeof(buf); - switch_console_printf(SWITCH_CHANNEL_CONSOLE, "OPEN FILE %s\n", data); + switch_console_printf(SWITCH_CHANNEL_CONSOLE, "OPEN FILE %s %dkhz %d channels\n", data, fh.samplerate, fh.channels); - samples = fh.samplerate / 50; - len = samples * 2; interval = 20; - + samples = (fh.samplerate / 50) * fh.channels; + len = samples * 2; + codec_name = "L16"; - if (switch_core_timer_init(&timer, "soft", interval, samples, pool) != SWITCH_STATUS_SUCCESS) { - switch_console_printf(SWITCH_CHANNEL_CONSOLE, "setup timer failed!\n"); - switch_channel_hangup(channel); - return; - } - - switch_console_printf(SWITCH_CHANNEL_CONSOLE, "setup timer success %d bytes per %d ms!\n", len, interval); - if (switch_core_codec_init(&codec, codec_name, - 8000, + fh.samplerate, interval, + fh.channels, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, pool) == SWITCH_STATUS_SUCCESS) { switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Raw Codec Activated\n"); write_frame.codec = &codec; } else { - switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Raw Codec Activation Failed\n"); + switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Raw Codec Activation Failed %s@%dhz %d\n", codec_name, fh.samplerate, interval); + switch_core_file_close(&fh); switch_channel_hangup(channel); return; } + if (switch_core_timer_init(&timer, "soft", interval, samples, pool) != SWITCH_STATUS_SUCCESS) { + switch_console_printf(SWITCH_CHANNEL_CONSOLE, "setup timer failed!\n"); + switch_core_codec_destroy(&codec); + switch_core_file_close(&fh); + switch_channel_hangup(channel); + return; + } + + switch_console_printf(SWITCH_CHANNEL_CONSOLE, "setup timer success %d bytes per %d ms!\n", len, interval); + /* start a thread to absorb incoming audio */ switch_core_service_session(session, &thread_session); ilen = samples; @@ -130,7 +134,7 @@ void playback_function(switch_core_session *session, char *data) if (ilen <= 0) { break; } - + write_frame.datalen = ilen * 2; write_frame.samples = ilen; #ifdef SWAP_LINEAR diff --git a/src/mod/mod_portaudio/mod_portaudio.c b/src/mod/mod_portaudio/mod_portaudio.c index 1d727e09a4..a784851824 100644 --- a/src/mod/mod_portaudio/mod_portaudio.c +++ b/src/mod/mod_portaudio/mod_portaudio.c @@ -703,6 +703,7 @@ static switch_status engage_device(struct private_object *tech_pvt) "L16", sample_rate, codec_ms, + 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { @@ -713,6 +714,7 @@ static switch_status engage_device(struct private_object *tech_pvt) "L16", sample_rate, codec_ms, + 1, SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { diff --git a/src/mod/mod_rawaudio/mod_rawaudio.c b/src/mod/mod_rawaudio/mod_rawaudio.c index ca67c03b57..cf6923c84f 100644 --- a/src/mod/mod_rawaudio/mod_rawaudio.c +++ b/src/mod/mod_rawaudio/mod_rawaudio.c @@ -59,6 +59,7 @@ static switch_status switch_raw_encode(switch_codec *codec, TBD look at other_codec to determine the original format of the data and determine if we need to resample in the event the audio is the same format but different implementations. */ + printf("encode %d %d->%d\n", decoded_data_len, other_codec->implementation->bytes_per_frame, codec->implementation->bytes_per_frame); return SWITCH_STATUS_NOOP; } @@ -72,7 +73,8 @@ static switch_status switch_raw_decode(switch_codec *codec, unsigned int *flag) { - //printf("decode %d %d->%d\n", encoded_data_len, other_codec->implementation->bytes_per_frame, codec->implementation->bytes_per_frame); + printf("decode %d %d->%d\n", encoded_data_len, other_codec->implementation->bytes_per_frame, codec->implementation->bytes_per_frame); + return SWITCH_STATUS_NOOP; } @@ -132,6 +134,23 @@ static const switch_codec_implementation raw_32k_implementation = { /*.destroy = */ switch_raw_destroy }; +static const switch_codec_implementation raw_22k_implementation = { + /*.samples_per_second = */ 22050, + /*.bits_per_second = */ 352800, + /*.microseconds_per_frame = */ 20000, + /*.samples_per_frame = */ 441, + /*.bytes_per_frame = */ 882, + /*.encoded_bytes_per_frame = */ 882, + /*.number_of_channels = */ 1, + /*.pref_frames_per_packet = */ 1, + /*.max_frames_per_packet = */ 1, + /*.init = */ switch_raw_init, + /*.encode = */ switch_raw_encode, + /*.decode = */ switch_raw_decode, + /*.destroy = */ switch_raw_destroy, + /*.next = */ &raw_32k_implementation +}; + static const switch_codec_implementation raw_16k_implementation = { /*.samples_per_second = */ 16000, /*.bits_per_second = */ 256000, @@ -146,7 +165,7 @@ static const switch_codec_implementation raw_16k_implementation = { /*.encode = */ switch_raw_encode, /*.decode = */ switch_raw_decode, /*.destroy = */ switch_raw_destroy, - /*.next = */ &raw_32k_implementation + /*.next = */ &raw_22k_implementation }; static const switch_codec_implementation raw_8k_implementation = { diff --git a/src/mod/mod_woomerachan/mod_woomerachan.c b/src/mod/mod_woomerachan/mod_woomerachan.c index d7ec4ca481..ce4679934c 100644 --- a/src/mod/mod_woomerachan/mod_woomerachan.c +++ b/src/mod/mod_woomerachan/mod_woomerachan.c @@ -207,13 +207,13 @@ static switch_status woomerachan_on_init(switch_core_session *session) tech_pvt->frame.data = tech_pvt->databuf; - if (switch_core_codec_init(&tech_pvt->read_codec, "L16", 8000, 30, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { + if (switch_core_codec_init(&tech_pvt->read_codec, "L16", 8000, 30, 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s Cannot set read codec\n", switch_channel_get_name(channel)); switch_channel_hangup(channel); return SWITCH_STATUS_FALSE; } - if (switch_core_codec_init(&tech_pvt->write_codec, "L16", 8000, 30, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { + if (switch_core_codec_init(&tech_pvt->write_codec, "L16", 8000, 30, 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s Cannot set read codec\n", switch_channel_get_name(channel)); switch_channel_hangup(channel); return SWITCH_STATUS_FALSE; diff --git a/src/switch_core.c b/src/switch_core.c index 81fa086401..ccb9c58d64 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -202,7 +202,7 @@ SWITCH_DECLARE(switch_status) switch_core_session_set_write_codec(switch_core_se return SWITCH_STATUS_SUCCESS; } -SWITCH_DECLARE(switch_status) switch_core_codec_init(switch_codec *codec, char *codec_name, int rate, int ms, switch_codec_flag flags, const switch_codec_settings *codec_settings, switch_memory_pool *pool) +SWITCH_DECLARE(switch_status) switch_core_codec_init(switch_codec *codec, char *codec_name, int rate, int ms, int channels, switch_codec_flag flags, const switch_codec_settings *codec_settings, switch_memory_pool *pool) { const switch_codec_interface *codec_interface; const switch_codec_implementation *iptr, *implementation = NULL; @@ -218,7 +218,9 @@ SWITCH_DECLARE(switch_status) switch_core_codec_init(switch_codec *codec, char * } for(iptr = codec_interface->implementations; iptr; iptr = iptr->next) { - if ((!rate || rate == iptr->samples_per_second) && (!ms || ms == (iptr->microseconds_per_frame / 1000))) { + if ((!rate || rate == iptr->samples_per_second) && + (!ms || ms == (iptr->microseconds_per_frame / 1000)) && + (!channels || channels == iptr->number_of_channels)) { implementation = iptr; break; }