mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-11-02 01:25:11 +00:00
Add rate to frames and a bunch of evil resample code
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@240 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
1 parent
0fbf3f5af0
commit
e98104d109
17 files changed
+265
-111
No files matched your search
@@ -79,8 +79,10 @@ static switch_status switch_gsm_encode(switch_codec *codec,
|
||||
switch_codec *other_codec,
|
||||
void *decoded_data,
|
||||
size_t decoded_data_len,
|
||||
int decoded_rate,
|
||||
void *encoded_data,
|
||||
size_t *encoded_data_len,
|
||||
int *encoded_rate,
|
||||
unsigned int *flag)
|
||||
{
|
||||
struct gsm_context *context = codec->private;
|
||||
@@ -117,8 +119,10 @@ static switch_status switch_gsm_decode(switch_codec *codec,
|
||||
switch_codec *other_codec,
|
||||
void *encoded_data,
|
||||
size_t encoded_data_len,
|
||||
int encoded_rate,
|
||||
void *decoded_data,
|
||||
size_t *decoded_data_len,
|
||||
int *decoded_rate,
|
||||
unsigned int *flag)
|
||||
{
|
||||
struct gsm_context *context = codec->private;
|
||||
|
||||
@@ -74,8 +74,10 @@ static switch_status switch_g729_encode(switch_codec *codec,
|
||||
switch_codec *other_codec,
|
||||
void *decoded_data,
|
||||
size_t decoded_data_len,
|
||||
int decoded_rate,
|
||||
void *encoded_data,
|
||||
size_t *encoded_data_len,
|
||||
int *encoded_rate,
|
||||
unsigned int *flag)
|
||||
{
|
||||
struct g729_context *context = codec->private;
|
||||
@@ -112,8 +114,10 @@ static switch_status switch_g729_decode(switch_codec *codec,
|
||||
switch_codec *other_codec,
|
||||
void *encoded_data,
|
||||
size_t encoded_data_len,
|
||||
int encoded_rate,
|
||||
void *decoded_data,
|
||||
size_t *decoded_data_len,
|
||||
int *decoded_rate,
|
||||
unsigned int *flag)
|
||||
{
|
||||
struct g729_context *context = codec->private;
|
||||
|
||||
@@ -911,6 +911,7 @@ static switch_status exosip_create_call(eXosip_event_t *event)
|
||||
return SWITCH_STATUS_FALSE;
|
||||
} else {
|
||||
int ms;
|
||||
tech_pvt->read_frame.rate = rate;
|
||||
switch_set_flag(tech_pvt, TFLAG_USING_CODEC);
|
||||
ms = tech_pvt->write_codec.implementation->microseconds_per_frame / 1000;
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Inbound Codec %s/%d %d ms\n", dname, rate, ms);
|
||||
@@ -1075,6 +1076,7 @@ static void handle_answer(eXosip_event_t *event)
|
||||
return;
|
||||
} else {
|
||||
int ms;
|
||||
tech_pvt->read_frame.rate = rate;
|
||||
switch_set_flag(tech_pvt, TFLAG_USING_CODEC);
|
||||
ms = tech_pvt->write_codec.implementation->microseconds_per_frame / 1000;
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Outbound Codec %s/%d %d ms\n", dname, rate, ms);
|
||||
|
||||
@@ -55,8 +55,10 @@ static switch_status switch_g711u_encode(switch_codec *codec,
|
||||
switch_codec *other_codec,
|
||||
void *decoded_data,
|
||||
size_t decoded_data_len,
|
||||
int decoded_rate,
|
||||
void *encoded_data,
|
||||
size_t *encoded_data_len,
|
||||
int *encoded_rate,
|
||||
unsigned int *flag)
|
||||
{
|
||||
short *dbuf;
|
||||
@@ -79,8 +81,10 @@ static switch_status switch_g711u_decode(switch_codec *codec,
|
||||
switch_codec *other_codec,
|
||||
void *encoded_data,
|
||||
size_t encoded_data_len,
|
||||
int encoded_rate,
|
||||
void *decoded_data,
|
||||
size_t *decoded_data_len,
|
||||
int *decoded_rate,
|
||||
unsigned int *flag)
|
||||
{
|
||||
short *dbuf;
|
||||
@@ -129,8 +133,10 @@ static switch_status switch_g711a_encode(switch_codec *codec,
|
||||
switch_codec *other_codec,
|
||||
void *decoded_data,
|
||||
size_t decoded_data_len,
|
||||
int decoded_rate,
|
||||
void *encoded_data,
|
||||
size_t *encoded_data_len,
|
||||
int *encoded_rate,
|
||||
unsigned int *flag)
|
||||
{
|
||||
short *dbuf;
|
||||
@@ -153,8 +159,10 @@ static switch_status switch_g711a_decode(switch_codec *codec,
|
||||
switch_codec *other_codec,
|
||||
void *encoded_data,
|
||||
size_t encoded_data_len,
|
||||
int encoded_rate,
|
||||
void *decoded_data,
|
||||
size_t *decoded_data_len,
|
||||
int *decoded_rate,
|
||||
unsigned int *flag)
|
||||
{
|
||||
short *dbuf;
|
||||
|
||||
@@ -298,6 +298,7 @@ static switch_status iax_set_codec(struct private_object *tech_pvt, struct iax_s
|
||||
int rate;
|
||||
ms = tech_pvt->write_codec.implementation->microseconds_per_frame / 1000;
|
||||
rate = tech_pvt->write_codec.implementation->samples_per_second;
|
||||
tech_pvt->read_frame.rate = rate;
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Codec %s/%d %d ms\n", dname, rate, ms);
|
||||
tech_pvt->read_frame.codec = &tech_pvt->read_codec;
|
||||
switch_core_session_set_read_codec(tech_pvt->session, &tech_pvt->read_codec);
|
||||
|
||||
@@ -103,7 +103,7 @@ void playback_function(switch_core_session *session, char *data)
|
||||
switch_channel_hangup(channel);
|
||||
return;
|
||||
}
|
||||
|
||||
write_frame.rate = fh.samplerate;
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "setup timer success %d bytes per %d ms!\n", len, interval);
|
||||
|
||||
/* start a thread to absorb incoming audio */
|
||||
@@ -136,7 +136,7 @@ void playback_function(switch_core_session *session, char *data)
|
||||
}
|
||||
|
||||
write_frame.datalen = ilen * 2;
|
||||
write_frame.samples = ilen;
|
||||
write_frame.samples = (int)ilen;
|
||||
#ifdef SWAP_LINEAR
|
||||
switch_swap_linear(write_frame.data, (int)write_frame.datalen / 2);
|
||||
#endif
|
||||
|
||||
@@ -75,6 +75,7 @@ static struct {
|
||||
int call_id;
|
||||
switch_hash *call_hash;
|
||||
switch_mutex_t *device_lock;
|
||||
int sample_rate;
|
||||
} globals;
|
||||
|
||||
struct private_object {
|
||||
@@ -559,6 +560,8 @@ static switch_status load_config(void)
|
||||
if (!strcasecmp(cfg.category, "settings")) {
|
||||
if (!strcmp(var, "debug")) {
|
||||
globals.debug = atoi(val);
|
||||
} else if (!strcmp(var, "sample_rate")) {
|
||||
globals.sample_rate = atoi(val);
|
||||
} else if (!strcmp(var, "dialplan")) {
|
||||
set_global_dialplan(val);
|
||||
} else if (!strcmp(var, "cid_name")) {
|
||||
@@ -585,6 +588,10 @@ static switch_status load_config(void)
|
||||
set_global_dialplan("default");
|
||||
}
|
||||
|
||||
if (!globals.sample_rate) {
|
||||
globals.sample_rate = 8000;
|
||||
}
|
||||
|
||||
switch_config_close_file(&cfg);
|
||||
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
@@ -692,7 +699,7 @@ error:
|
||||
|
||||
static switch_status engage_device(struct private_object *tech_pvt)
|
||||
{
|
||||
int sample_rate = 8000;
|
||||
int sample_rate = globals.sample_rate;
|
||||
int codec_ms = 20;
|
||||
|
||||
switch_channel *channel;
|
||||
@@ -723,7 +730,7 @@ static switch_status engage_device(struct private_object *tech_pvt)
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
tech_pvt->read_frame.rate = sample_rate;
|
||||
tech_pvt->read_frame.codec = &tech_pvt->read_codec;
|
||||
switch_core_session_set_read_codec(tech_pvt->session, &tech_pvt->read_codec);
|
||||
switch_core_session_set_write_codec(tech_pvt->session, &tech_pvt->write_codec);
|
||||
|
||||
@@ -42,21 +42,24 @@ static const char modname[] = "mod_rawaudio";
|
||||
#define MAX(a,b) ((a) > (b) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#define QUALITY 1
|
||||
|
||||
struct raw_resampler {
|
||||
void *resampler;
|
||||
int from;
|
||||
int to;
|
||||
double factor;
|
||||
float *buf;
|
||||
int buf_len;
|
||||
int buf_size;
|
||||
float *new_buf;
|
||||
int new_buf_len;
|
||||
int new_buf_size;
|
||||
};
|
||||
|
||||
struct raw_context {
|
||||
void *enc_resampler;
|
||||
int enc_from;
|
||||
int enc_to;
|
||||
double enc_factor;
|
||||
float *enc_buf;
|
||||
int enc_buf_len;
|
||||
int enc_buf_size;
|
||||
void *dec_resampler;
|
||||
int dec_from;
|
||||
int dec_to;
|
||||
double dec_factor;
|
||||
float *dec_buf;
|
||||
int dec_buf_len;
|
||||
int dec_buf_size;
|
||||
struct raw_resampler *enc;
|
||||
struct raw_resampler *dec;
|
||||
};
|
||||
|
||||
|
||||
@@ -67,13 +70,16 @@ static int resample(void *handle, double factor, float *src, int srclen, float *
|
||||
for(;;) {
|
||||
int srcBlock = MIN(srclen-srcpos, srclen);
|
||||
int lastFlag = (last && (srcBlock == srclen-srcpos));
|
||||
printf("resampling %d/%d (%d)\n", srcpos, srclen, MIN(dstlen-out, dstlen));
|
||||
o = resample_process(handle, factor, &src[srcpos], srcBlock, lastFlag, &srcused, &dst[out], dstlen-out);
|
||||
srcpos += srcused;
|
||||
if (o >= 0)
|
||||
//printf("resampling %d/%d (%d) %d %f\n", srcpos, srclen, MIN(dstlen-out, dstlen), srcused, factor);
|
||||
|
||||
srcpos += srcused;
|
||||
if (o >= 0) {
|
||||
out += o;
|
||||
if (o < 0 || (o == 0 && srcpos == srclen))
|
||||
}
|
||||
if (o < 0 || (o == 0 && srcpos == srclen)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -102,8 +108,10 @@ static switch_status switch_raw_encode(switch_codec *codec,
|
||||
switch_codec *other_codec,
|
||||
void *decoded_data,
|
||||
size_t decoded_data_len,
|
||||
int decoded_rate,
|
||||
void *encoded_data,
|
||||
size_t *encoded_data_len,
|
||||
int *encoded_rate,
|
||||
unsigned int *flag)
|
||||
{
|
||||
struct raw_context *context = codec->private;
|
||||
@@ -111,23 +119,49 @@ static switch_status switch_raw_encode(switch_codec *codec,
|
||||
/* NOOP indicates that the audio in is already the same as the audio out, so no conversion was necessary.
|
||||
TBD Support varying number of channels
|
||||
*/
|
||||
//printf("encode %d->%d (%d)\n", other_codec->implementation->samples_per_second, codec->implementation->samples_per_second, decoded_rate);
|
||||
|
||||
if (other_codec && codec->implementation->samples_per_second != other_codec->implementation->samples_per_second) {
|
||||
if (!context->enc_from) {
|
||||
printf("Activate Resample %d->%d\n", codec->implementation->samples_per_second, other_codec->implementation->samples_per_second);
|
||||
context->enc_from = codec->implementation->samples_per_second;
|
||||
context->enc_to = other_codec->implementation->samples_per_second;
|
||||
context->enc_factor = ((double) context->enc_from / (double)context->enc_to);
|
||||
context->enc_resampler = resample_open(1, context->enc_factor, context->enc_factor);
|
||||
context->enc_buf_size = codec->implementation->bytes_per_frame;
|
||||
context->enc_buf = (float *) switch_core_alloc(codec->memory_pool, context->enc_buf_size);
|
||||
if (other_codec &&
|
||||
codec->implementation->samples_per_second != other_codec->implementation->samples_per_second &&
|
||||
decoded_rate != other_codec->implementation->samples_per_second) {
|
||||
const short *ddp = decoded_data;
|
||||
short *edp = encoded_data;
|
||||
size_t ddplen = decoded_data_len / 2;
|
||||
|
||||
if (!context->enc) {
|
||||
|
||||
if (!(context->enc = switch_core_alloc(codec->memory_pool, sizeof(struct raw_resampler)))) {
|
||||
return SWITCH_STATUS_MEMERR;
|
||||
}
|
||||
|
||||
context->enc->from = codec->implementation->samples_per_second;
|
||||
context->enc->to = other_codec->implementation->samples_per_second;
|
||||
context->enc->factor = ((double)context->enc->from / (double)context->enc->to);
|
||||
|
||||
context->enc->resampler = resample_open(QUALITY, context->enc->factor, context->enc->factor);
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Encode Resample %d->%d %f\n", other_codec->implementation->samples_per_second, codec->implementation->samples_per_second, context->enc->factor);
|
||||
context->enc->buf_size = codec->implementation->bytes_per_frame * 10;
|
||||
context->enc->buf = (float *) switch_core_alloc(codec->memory_pool, context->enc->buf_size);
|
||||
context->enc->new_buf_size = codec->implementation->bytes_per_frame * 10;
|
||||
context->enc->new_buf = (float *) switch_core_alloc(codec->memory_pool, context->enc->new_buf_size);
|
||||
}
|
||||
|
||||
if (context->enc_from) {
|
||||
|
||||
if (context->enc) {
|
||||
context->enc->buf_len = switch_short_to_float(decoded_data, context->enc->buf, (int)ddplen);
|
||||
context->enc->new_buf_len = resample(context->enc->resampler,
|
||||
context->enc->factor,
|
||||
context->enc->buf,
|
||||
context->enc->buf_len,
|
||||
context->enc->new_buf,
|
||||
context->enc->new_buf_size,
|
||||
0);
|
||||
switch_float_to_short(context->enc->new_buf, edp, decoded_data_len * 2);
|
||||
*encoded_data_len = context->enc->new_buf_len * 2;
|
||||
*encoded_rate = context->enc->to;
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
return SWITCH_STATUS_GENERR;
|
||||
}
|
||||
|
||||
return SWITCH_STATUS_NOOP;
|
||||
@@ -137,13 +171,61 @@ static switch_status switch_raw_decode(switch_codec *codec,
|
||||
switch_codec *other_codec,
|
||||
void *encoded_data,
|
||||
size_t encoded_data_len,
|
||||
int encoded_rate,
|
||||
void *decoded_data,
|
||||
size_t *decoded_data_len,
|
||||
int *decoded_rate,
|
||||
unsigned int *flag)
|
||||
{
|
||||
struct raw_context *context = codec->private;
|
||||
|
||||
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", other_codec->implementation->samples_per_second, codec->implementation->samples_per_second, encoded_rate);
|
||||
|
||||
|
||||
if (other_codec &&
|
||||
codec->implementation->samples_per_second != other_codec->implementation->samples_per_second &&
|
||||
encoded_rate != other_codec->implementation->samples_per_second) {
|
||||
short *ddp = decoded_data;
|
||||
const short *edp = encoded_data;
|
||||
size_t edplen = encoded_data_len / 2;
|
||||
|
||||
if (!context->dec) {
|
||||
if (!(context->dec = switch_core_alloc(codec->memory_pool, sizeof(struct raw_resampler)))) {
|
||||
return SWITCH_STATUS_MEMERR;
|
||||
}
|
||||
|
||||
|
||||
context->dec->from = codec->implementation->samples_per_second;
|
||||
context->dec->to = other_codec->implementation->samples_per_second;
|
||||
context->dec->factor = ((double)context->dec->from / (double)context->dec->to);
|
||||
|
||||
context->dec->resampler = resample_open(QUALITY, context->dec->factor, context->dec->factor);
|
||||
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Decode Resample %d->%d %f\n", other_codec->implementation->samples_per_second, codec->implementation->samples_per_second, context->dec->factor);
|
||||
|
||||
context->dec->buf_size = codec->implementation->bytes_per_frame * 10;
|
||||
context->dec->buf = (float *) switch_core_alloc(codec->memory_pool, context->dec->buf_size);
|
||||
context->dec->new_buf_size = codec->implementation->bytes_per_frame * 10;
|
||||
context->dec->new_buf = (float *) switch_core_alloc(codec->memory_pool, context->dec->new_buf_size);
|
||||
}
|
||||
|
||||
if (context->dec) {
|
||||
context->dec->buf_len = switch_short_to_float(encoded_data, context->dec->buf, (int)edplen);
|
||||
context->dec->new_buf_len = resample(context->dec->resampler,
|
||||
context->dec->factor,
|
||||
context->dec->buf,
|
||||
context->dec->buf_len,
|
||||
context->dec->new_buf,
|
||||
context->dec->new_buf_size,
|
||||
0);
|
||||
switch_float_to_short(context->dec->new_buf, ddp, (int)edplen);
|
||||
*decoded_data_len = context->dec->new_buf_len * 2;
|
||||
*decoded_rate = context->dec->to;
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
return SWITCH_STATUS_GENERR;
|
||||
}
|
||||
|
||||
|
||||
return SWITCH_STATUS_NOOP;
|
||||
}
|
||||
@@ -151,6 +233,16 @@ static switch_status switch_raw_decode(switch_codec *codec,
|
||||
|
||||
static switch_status switch_raw_destroy(switch_codec *codec)
|
||||
{
|
||||
struct raw_context *context = codec->private;
|
||||
|
||||
if (context->enc && context->enc->resampler){
|
||||
resample_close(context->enc->resampler);
|
||||
}
|
||||
|
||||
if (context->dec && context->dec->resampler){
|
||||
resample_close(context->dec->resampler);
|
||||
}
|
||||
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -157,8 +157,10 @@ static switch_status switch_speex_encode(switch_codec *codec,
|
||||
switch_codec *other_codec,
|
||||
void *decoded_data,
|
||||
size_t decoded_data_len,
|
||||
int decoded_rate,
|
||||
void *encoded_data,
|
||||
size_t *encoded_data_len,
|
||||
int *encoded_rate,
|
||||
unsigned int *flag)
|
||||
{
|
||||
struct speex_context *context = codec->private;
|
||||
@@ -211,8 +213,10 @@ static switch_status switch_speex_decode(switch_codec *codec,
|
||||
switch_codec *other_codec,
|
||||
void *encoded_data,
|
||||
size_t encoded_data_len,
|
||||
int encoded_rate,
|
||||
void *decoded_data,
|
||||
size_t *decoded_data_len,
|
||||
int *decoded_rate,
|
||||
unsigned int *flag)
|
||||
{
|
||||
struct speex_context *context = codec->private;
|
||||
|
||||
@@ -198,6 +198,7 @@ static switch_status woomerachan_on_init(switch_core_session *session)
|
||||
{
|
||||
switch_channel *channel;
|
||||
struct private_object *tech_pvt = NULL;
|
||||
int rate = 8000;
|
||||
|
||||
tech_pvt = switch_core_session_get_private(session);
|
||||
assert(tech_pvt != NULL);
|
||||
@@ -207,18 +208,18 @@ 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, 1, 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", rate, 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, 1, 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", rate, 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;
|
||||
}
|
||||
|
||||
tech_pvt->frame.rate = rate;
|
||||
tech_pvt->frame.codec = &tech_pvt->read_codec;
|
||||
switch_core_session_set_read_codec(session, &tech_pvt->read_codec);
|
||||
switch_core_session_set_write_codec(session, &tech_pvt->write_codec);
|
||||
|
||||
Reference in new issue
Block a user