add channels arg to codec

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@227 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale 2005-12-28 20:06:03 +00:00
parent f5a25e1635
commit e7c060b8b9
8 changed files with 55 additions and 22 deletions

View File

@ -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_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_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_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_DECLARE(switch_status) switch_core_codec_encode(switch_codec *codec,
switch_codec *other_codec, switch_codec *other_codec,
void *decoded_data, void *decoded_data,

View File

@ -890,6 +890,7 @@ static switch_status exosip_create_call(eXosip_event_t *event)
dname, dname,
rate, rate,
globals.codec_ms, globals.codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL, NULL,
switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
@ -901,6 +902,7 @@ static switch_status exosip_create_call(eXosip_event_t *event)
dname, dname,
rate, rate,
globals.codec_ms, globals.codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL, NULL,
switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
@ -1052,6 +1054,7 @@ static void handle_answer(eXosip_event_t *event)
dname, dname,
rate, rate,
globals.codec_ms, globals.codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL, NULL,
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
@ -1063,6 +1066,7 @@ static void handle_answer(eXosip_event_t *event)
dname, dname,
rate, rate,
globals.codec_ms, globals.codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE, SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE,
NULL, NULL,
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {

View File

@ -275,6 +275,7 @@ static switch_status iax_set_codec(struct private_object *tech_pvt, struct iax_s
dname, dname,
0, 0,
0, 0,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL, NULL,
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { 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, dname,
0, 0,
0, 0,
1,
SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE, SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE,
NULL, NULL,
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {

View File

@ -71,37 +71,41 @@ void playback_function(switch_core_session *session, char *data)
write_frame.buflen = sizeof(buf); 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; interval = 20;
samples = (fh.samplerate / 50) * fh.channels;
len = samples * 2;
codec_name = "L16"; 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, if (switch_core_codec_init(&codec,
codec_name, codec_name,
8000, fh.samplerate,
interval, interval,
fh.channels,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL, NULL,
pool) == SWITCH_STATUS_SUCCESS) { pool) == SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Raw Codec Activated\n"); switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Raw Codec Activated\n");
write_frame.codec = &codec; write_frame.codec = &codec;
} else { } 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); switch_channel_hangup(channel);
return; 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 */ /* start a thread to absorb incoming audio */
switch_core_service_session(session, &thread_session); switch_core_service_session(session, &thread_session);
ilen = samples; ilen = samples;

View File

@ -703,6 +703,7 @@ static switch_status engage_device(struct private_object *tech_pvt)
"L16", "L16",
sample_rate, sample_rate,
codec_ms, codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL, NULL,
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { 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", "L16",
sample_rate, sample_rate,
codec_ms, codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE, SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE,
NULL, NULL,
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {

View File

@ -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 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. 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; return SWITCH_STATUS_NOOP;
} }
@ -72,7 +73,8 @@ static switch_status switch_raw_decode(switch_codec *codec,
unsigned int *flag) 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; return SWITCH_STATUS_NOOP;
} }
@ -132,6 +134,23 @@ static const switch_codec_implementation raw_32k_implementation = {
/*.destroy = */ switch_raw_destroy /*.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 = { static const switch_codec_implementation raw_16k_implementation = {
/*.samples_per_second = */ 16000, /*.samples_per_second = */ 16000,
/*.bits_per_second = */ 256000, /*.bits_per_second = */ 256000,
@ -146,7 +165,7 @@ static const switch_codec_implementation raw_16k_implementation = {
/*.encode = */ switch_raw_encode, /*.encode = */ switch_raw_encode,
/*.decode = */ switch_raw_decode, /*.decode = */ switch_raw_decode,
/*.destroy = */ switch_raw_destroy, /*.destroy = */ switch_raw_destroy,
/*.next = */ &raw_32k_implementation /*.next = */ &raw_22k_implementation
}; };
static const switch_codec_implementation raw_8k_implementation = { static const switch_codec_implementation raw_8k_implementation = {

View File

@ -207,13 +207,13 @@ static switch_status woomerachan_on_init(switch_core_session *session)
tech_pvt->frame.data = tech_pvt->databuf; 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_console_printf(SWITCH_CHANNEL_CONSOLE, "%s Cannot set read codec\n", switch_channel_get_name(channel));
switch_channel_hangup(channel); switch_channel_hangup(channel);
return SWITCH_STATUS_FALSE; 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_console_printf(SWITCH_CHANNEL_CONSOLE, "%s Cannot set read codec\n", switch_channel_get_name(channel));
switch_channel_hangup(channel); switch_channel_hangup(channel);
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;

View File

@ -202,7 +202,7 @@ SWITCH_DECLARE(switch_status) switch_core_session_set_write_codec(switch_core_se
return SWITCH_STATUS_SUCCESS; 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_interface *codec_interface;
const switch_codec_implementation *iptr, *implementation = NULL; 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) { 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; implementation = iptr;
break; break;
} }