mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-02-24 18:41:57 +00:00
258 lines
6.6 KiB
C
258 lines
6.6 KiB
C
|
/*
|
||
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
||
|
* Copyright (C) 2005-2011, Anthony Minessale II <anthm@freeswitch.org>
|
||
|
*
|
||
|
* Version: MPL 1.1
|
||
|
*
|
||
|
* The contents of this file are subject to the Mozilla Public License Version
|
||
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
||
|
* the License. You may obtain a copy of the License at
|
||
|
* http://www.mozilla.org/MPL/
|
||
|
*
|
||
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
||
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||
|
* for the specific language governing rights and limitations under the
|
||
|
* License.
|
||
|
*
|
||
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / ISAC codec module
|
||
|
*
|
||
|
* The Initial Developer of the Original Code is
|
||
|
* Anthony Minessale II <anthm@freeswitch.org>
|
||
|
* Portions created by the Initial Developer are Copyright (C)
|
||
|
* the Initial Developer. All Rights Reserved.
|
||
|
*
|
||
|
* Contributor(s):
|
||
|
* Anthony Minessale II <anthm@freeswitch.org>
|
||
|
*
|
||
|
*
|
||
|
* mod_iSAC.c -- iSAC Codec Module
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include <switch.h>
|
||
|
#include "isac.h"
|
||
|
|
||
|
SWITCH_MODULE_LOAD_FUNCTION(mod_iSAC_codec_load);
|
||
|
SWITCH_MODULE_DEFINITION(mod_iSAC, mod_iSAC_codec_load, NULL, NULL);
|
||
|
|
||
|
struct iSAC_context {
|
||
|
ISACStruct *ISAC_main_inst;
|
||
|
};
|
||
|
|
||
|
static switch_status_t switch_iSAC_init(switch_codec_t *codec, switch_codec_flag_t flags, const switch_codec_settings_t *codec_settings)
|
||
|
{
|
||
|
uint32_t encoding, decoding;
|
||
|
WebRtc_Word16 err;
|
||
|
struct iSAC_context *context = NULL;
|
||
|
|
||
|
encoding = (flags & SWITCH_CODEC_FLAG_ENCODE);
|
||
|
decoding = (flags & SWITCH_CODEC_FLAG_DECODE);
|
||
|
|
||
|
if (!(encoding || decoding)) {
|
||
|
return SWITCH_STATUS_FALSE;
|
||
|
}
|
||
|
|
||
|
if (!(context = switch_core_alloc(codec->memory_pool, sizeof(*context)))) {
|
||
|
return SWITCH_STATUS_FALSE;
|
||
|
}
|
||
|
|
||
|
codec->private_info = context;
|
||
|
|
||
|
err = WebRtcIsac_Create(&context->ISAC_main_inst);
|
||
|
|
||
|
if (err < 0) return SWITCH_STATUS_FALSE;
|
||
|
|
||
|
WebRtcIsac_SetEncSampRate(context->ISAC_main_inst, codec->implementation->actual_samples_per_second / 1000);
|
||
|
WebRtcIsac_SetDecSampRate(context->ISAC_main_inst, codec->implementation->actual_samples_per_second / 1000);
|
||
|
|
||
|
if (encoding) {
|
||
|
if (WebRtcIsac_EncoderInit(context->ISAC_main_inst, 0) < 0) {
|
||
|
return SWITCH_STATUS_FALSE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (decoding) {
|
||
|
if (WebRtcIsac_DecoderInit(context->ISAC_main_inst) < 0) {
|
||
|
return SWITCH_STATUS_FALSE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (codec->implementation->actual_samples_per_second == 16000) {
|
||
|
if (WebRtcIsac_ControlBwe(context->ISAC_main_inst, 0, codec->implementation->microseconds_per_packet / 1000, 0) < 0) {
|
||
|
return SWITCH_STATUS_FALSE;
|
||
|
}
|
||
|
} else {
|
||
|
if (WebRtcIsac_Control(context->ISAC_main_inst, codec->implementation->actual_samples_per_second / 1000,
|
||
|
codec->implementation->microseconds_per_packet / 1000) < 0) {
|
||
|
return SWITCH_STATUS_FALSE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (WebRtcIsac_SetMaxPayloadSize(context->ISAC_main_inst, SWITCH_RECOMMENDED_BUFFER_SIZE) < 0) {
|
||
|
return SWITCH_STATUS_FALSE;
|
||
|
}
|
||
|
|
||
|
if (WebRtcIsac_SetMaxRate(context->ISAC_main_inst, codec->implementation->bits_per_second) < 0) {
|
||
|
return SWITCH_STATUS_FALSE;
|
||
|
}
|
||
|
|
||
|
|
||
|
return SWITCH_STATUS_SUCCESS;
|
||
|
}
|
||
|
|
||
|
static switch_status_t switch_iSAC_encode(switch_codec_t *codec, switch_codec_t *other_codec,
|
||
|
void *decoded_data,
|
||
|
uint32_t decoded_data_len,
|
||
|
uint32_t decoded_rate,
|
||
|
void *encoded_data,
|
||
|
uint32_t *encoded_data_len,
|
||
|
uint32_t *encoded_rate,
|
||
|
unsigned int *flag)
|
||
|
{
|
||
|
struct iSAC_context *context = codec->private_info;
|
||
|
WebRtc_Word16 len = 0, *in, *out;
|
||
|
int rise = (codec->implementation->actual_samples_per_second / 100);
|
||
|
|
||
|
in = decoded_data;
|
||
|
out = encoded_data;
|
||
|
|
||
|
while(len == 0) {
|
||
|
len = WebRtcIsac_Encode(context->ISAC_main_inst, in, out);
|
||
|
in += rise;
|
||
|
}
|
||
|
|
||
|
if (len < 0) {
|
||
|
return SWITCH_STATUS_GENERR;
|
||
|
}
|
||
|
|
||
|
*encoded_data_len = (uint32_t) len;
|
||
|
|
||
|
return SWITCH_STATUS_SUCCESS;
|
||
|
}
|
||
|
|
||
|
static switch_status_t switch_iSAC_decode(switch_codec_t *codec,
|
||
|
switch_codec_t *other_codec,
|
||
|
void *encoded_data,
|
||
|
uint32_t encoded_data_len,
|
||
|
uint32_t encoded_rate,
|
||
|
void *decoded_data,
|
||
|
uint32_t *decoded_data_len,
|
||
|
uint32_t *decoded_rate,
|
||
|
unsigned int *flag)
|
||
|
{
|
||
|
struct iSAC_context *context = codec->private_info;
|
||
|
WebRtc_Word16 len, speechType[1];
|
||
|
|
||
|
if ((*flag & SFF_PLC)) {
|
||
|
len = WebRtcIsac_DecodePlc(context->ISAC_main_inst, decoded_data, 1);
|
||
|
} else {
|
||
|
len = WebRtcIsac_Decode(context->ISAC_main_inst, encoded_data, encoded_data_len, decoded_data, speechType);
|
||
|
}
|
||
|
|
||
|
if (len < 0) {
|
||
|
*decoded_data_len = 0;
|
||
|
return SWITCH_STATUS_GENERR;
|
||
|
}
|
||
|
|
||
|
*decoded_data_len = (uint32_t) len * 2;
|
||
|
|
||
|
return SWITCH_STATUS_SUCCESS;
|
||
|
}
|
||
|
|
||
|
static switch_status_t switch_iSAC_destroy(switch_codec_t *codec)
|
||
|
{
|
||
|
struct iSAC_context *context = codec->private_info;
|
||
|
|
||
|
WebRtcIsac_Free(context->ISAC_main_inst);
|
||
|
|
||
|
return SWITCH_STATUS_SUCCESS;
|
||
|
}
|
||
|
|
||
|
|
||
|
SWITCH_MODULE_LOAD_FUNCTION(mod_iSAC_codec_load)
|
||
|
{
|
||
|
switch_codec_interface_t *codec_interface;
|
||
|
|
||
|
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
|
||
|
|
||
|
SWITCH_ADD_CODEC(codec_interface, "iSAC"); /* 8.0kbit */
|
||
|
|
||
|
switch_core_codec_add_implementation(pool, codec_interface,
|
||
|
SWITCH_CODEC_TYPE_AUDIO,
|
||
|
99,
|
||
|
"iSAC",
|
||
|
NULL,
|
||
|
16000,
|
||
|
16000,
|
||
|
53400,
|
||
|
30000,
|
||
|
480,
|
||
|
960,
|
||
|
0,
|
||
|
1,
|
||
|
3,
|
||
|
switch_iSAC_init,
|
||
|
switch_iSAC_encode,
|
||
|
switch_iSAC_decode,
|
||
|
switch_iSAC_destroy);
|
||
|
|
||
|
|
||
|
|
||
|
switch_core_codec_add_implementation(pool, codec_interface,
|
||
|
SWITCH_CODEC_TYPE_AUDIO,
|
||
|
99,
|
||
|
"iSAC",
|
||
|
NULL,
|
||
|
16000,
|
||
|
16000,
|
||
|
53400,
|
||
|
60000,
|
||
|
960,
|
||
|
1920,
|
||
|
0,
|
||
|
1,
|
||
|
6,
|
||
|
switch_iSAC_init,
|
||
|
switch_iSAC_encode,
|
||
|
switch_iSAC_decode,
|
||
|
switch_iSAC_destroy);
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
switch_core_codec_add_implementation(pool, codec_interface,
|
||
|
SWITCH_CODEC_TYPE_AUDIO,
|
||
|
99,
|
||
|
"iSAC",
|
||
|
NULL,
|
||
|
32000,
|
||
|
32000,
|
||
|
160000,
|
||
|
30000,
|
||
|
960,
|
||
|
1920,
|
||
|
0,
|
||
|
1,
|
||
|
6,
|
||
|
switch_iSAC_init,
|
||
|
switch_iSAC_encode,
|
||
|
switch_iSAC_decode,
|
||
|
switch_iSAC_destroy);
|
||
|
|
||
|
|
||
|
|
||
|
/* indicate that the module should continue to be loaded */
|
||
|
return SWITCH_STATUS_SUCCESS;
|
||
|
}
|
||
|
|
||
|
/* For Emacs:
|
||
|
* Local Variables:
|
||
|
* mode:c
|
||
|
* indent-tabs-mode:t
|
||
|
* tab-width:4
|
||
|
* c-basic-offset:4
|
||
|
* End:
|
||
|
* For VIM:
|
||
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
|
||
|
*/
|