From b839fce160d88ed93846af45fe34f655fe80c682 Mon Sep 17 00:00:00 2001 From: "Alfred E. Heggestad" Date: Sun, 1 May 2016 21:01:27 +0200 Subject: aucodec: split srate into srate and crate (Clock Rate) --- include/baresip.h | 3 ++- modules/amr/amr.c | 4 ++-- modules/bv32/bv32.c | 2 +- modules/codec2/codec2.c | 1 + modules/g711/g711.c | 4 ++-- modules/g722/g722.c | 2 +- modules/g7221/g7221.c | 1 + modules/g726/g726.c | 8 ++++---- modules/gsm/gsm.c | 2 +- modules/ilbc/ilbc.c | 2 +- modules/isac/isac.c | 4 ++-- modules/l16/l16.c | 16 ++++++++-------- modules/mpa/mpa.c | 3 ++- modules/opus/opus.c | 1 + modules/silk/silk.c | 2 +- modules/speex/speex.c | 12 ++++++------ src/audio.c | 31 +++++++++++++++---------------- test/call.c | 1 + 18 files changed, 52 insertions(+), 47 deletions(-) diff --git a/include/baresip.h b/include/baresip.h index b05469d..cb99946 100644 --- a/include/baresip.h +++ b/include/baresip.h @@ -737,7 +737,8 @@ struct aucodec { struct le le; const char *pt; const char *name; - uint32_t srate; + uint32_t srate; /* Audio samplerate */ + uint32_t crate; /* RTP Clock rate */ uint8_t ch; const char *fmtp; auenc_update_h *encupdh; diff --git a/modules/amr/amr.c b/modules/amr/amr.c index 74e353a..0596812 100644 --- a/modules/amr/amr.c +++ b/modules/amr/amr.c @@ -292,7 +292,7 @@ static int decode_nb(struct audec_state *st, int16_t *sampv, #ifdef AMR_WB static struct aucodec amr_wb = { - LE_INIT, NULL, "AMR-WB", 16000, 1, NULL, + LE_INIT, NULL, "AMR-WB", 16000, 16000, 1, NULL, encode_update, encode_wb, decode_update, decode_wb, NULL, amr_fmtp_enc, amr_fmtp_cmp @@ -300,7 +300,7 @@ static struct aucodec amr_wb = { #endif #ifdef AMR_NB static struct aucodec amr_nb = { - LE_INIT, NULL, "AMR", 8000, 1, NULL, + LE_INIT, NULL, "AMR", 8000, 8000, 1, NULL, encode_update, encode_nb, decode_update, decode_nb, NULL, amr_fmtp_enc, amr_fmtp_cmp diff --git a/modules/bv32/bv32.c b/modules/bv32/bv32.c index c19a8bc..f604885 100644 --- a/modules/bv32/bv32.c +++ b/modules/bv32/bv32.c @@ -151,7 +151,7 @@ static int plc(struct audec_state *st, int16_t *sampv, size_t *sampc) static struct aucodec bv32 = { - LE_INIT, 0, "BV32", 16000, 1, NULL, + LE_INIT, 0, "BV32", 16000, 16000, 1, NULL, encode_update, encode, decode_update, decode, plc, NULL, NULL diff --git a/modules/codec2/codec2.c b/modules/codec2/codec2.c index 422adc9..67fd7e6 100644 --- a/modules/codec2/codec2.c +++ b/modules/codec2/codec2.c @@ -165,6 +165,7 @@ static struct aucodec codec2 = { NULL, "CODEC2", 8000, + 8000, 1, NULL, encode_update, diff --git a/modules/g711/g711.c b/modules/g711/g711.c index acee027..0353fe0 100644 --- a/modules/g711/g711.c +++ b/modules/g711/g711.c @@ -97,12 +97,12 @@ static int pcma_decode(struct audec_state *ads, int16_t *sampv, static struct aucodec pcmu = { - LE_INIT, "0", "PCMU", 8000, 1, NULL, + LE_INIT, "0", "PCMU", 8000, 8000, 1, NULL, NULL, pcmu_encode, NULL, pcmu_decode, NULL, NULL, NULL }; static struct aucodec pcma = { - LE_INIT, "8", "PCMA", 8000, 1, NULL, + LE_INIT, "8", "PCMA", 8000, 8000, 1, NULL, NULL, pcma_encode, NULL, pcma_decode, NULL, NULL, NULL }; diff --git a/modules/g722/g722.c b/modules/g722/g722.c index bc0fda0..2b5a7a4 100644 --- a/modules/g722/g722.c +++ b/modules/g722/g722.c @@ -162,7 +162,7 @@ static int decode(struct audec_state *st, int16_t *sampv, size_t *sampc, static struct aucodec g722 = { - LE_INIT, "9", "G722", 8000, 1, NULL, + LE_INIT, "9", "G722", 16000, 8000, 1, NULL, encode_update, encode, decode_update, decode, NULL, NULL, NULL diff --git a/modules/g7221/g7221.c b/modules/g7221/g7221.c index 7211c15..7651e92 100644 --- a/modules/g7221/g7221.c +++ b/modules/g7221/g7221.c @@ -13,6 +13,7 @@ static struct g7221_aucodec g7221 = { .ac = { .name = "G7221", .srate = 16000, + .crate = 16000, .ch = 1, .encupdh = g7221_encode_update, .ench = g7221_encode, diff --git a/modules/g726/g726.c b/modules/g726/g726.c index 3553f47..a23a985 100644 --- a/modules/g726/g726.c +++ b/modules/g726/g726.c @@ -149,28 +149,28 @@ static int decode(struct audec_state *st, int16_t *sampv, static struct g726_aucodec g726[4] = { { { - LE_INIT, 0, "G726-40", 8000, 1, NULL, + LE_INIT, 0, "G726-40", 8000, 8000, 1, NULL, encode_update, encode, decode_update, decode, 0, 0, 0 }, 40000 }, { { - LE_INIT, 0, "G726-32", 8000, 1, NULL, + LE_INIT, 0, "G726-32", 8000, 8000, 1, NULL, encode_update, encode, decode_update, decode, 0, 0, 0 }, 32000 }, { { - LE_INIT, 0, "G726-24", 8000, 1, NULL, + LE_INIT, 0, "G726-24", 8000, 8000, 1, NULL, encode_update, encode, decode_update, decode, 0, 0, 0 }, 24000 }, { { - LE_INIT, 0, "G726-16", 8000, 1, NULL, + LE_INIT, 0, "G726-16", 8000, 8000, 1, NULL, encode_update, encode, decode_update, decode, 0, 0, 0 }, 16000 diff --git a/modules/gsm/gsm.c b/modules/gsm/gsm.c index 86203b9..2fc006e 100644 --- a/modules/gsm/gsm.c +++ b/modules/gsm/gsm.c @@ -149,7 +149,7 @@ static int decode(struct audec_state *st, int16_t *sampv, size_t *sampc, static struct aucodec ac_gsm = { - LE_INIT, "3", "GSM", 8000, 1, NULL, + LE_INIT, "3", "GSM", 8000, 8000, 1, NULL, encode_update, encode, decode_update, decode, NULL, NULL, NULL }; diff --git a/modules/ilbc/ilbc.c b/modules/ilbc/ilbc.c index 229dc28..7c2ceed 100644 --- a/modules/ilbc/ilbc.c +++ b/modules/ilbc/ilbc.c @@ -327,7 +327,7 @@ static int pkloss(struct audec_state *st, int16_t *sampv, size_t *sampc) static struct aucodec ilbc = { - LE_INIT, 0, "iLBC", 8000, 1, ilbc_fmtp, + LE_INIT, 0, "iLBC", 8000, 8000, 1, ilbc_fmtp, encode_update, encode, decode_update, decode, pkloss, 0, 0 }; diff --git a/modules/isac/isac.c b/modules/isac/isac.c index 648031a..9e98c35 100644 --- a/modules/isac/isac.c +++ b/modules/isac/isac.c @@ -186,11 +186,11 @@ static int plc(struct audec_state *st, int16_t *sampv, size_t *sampc) static struct aucodec isacv[] = { { - LE_INIT, 0, "isac", 32000, 1, NULL, + LE_INIT, 0, "isac", 32000, 32000, 1, NULL, encode_update, encode, decode_update, decode, plc, NULL, NULL }, { - LE_INIT, 0, "isac", 16000, 1, NULL, + LE_INIT, 0, "isac", 16000, 16000, 1, NULL, encode_update, encode, decode_update, decode, plc, NULL, NULL } }; diff --git a/modules/l16/l16.c b/modules/l16/l16.c index a8dc97f..2f81caa 100644 --- a/modules/l16/l16.c +++ b/modules/l16/l16.c @@ -62,14 +62,14 @@ static int decode(struct audec_state *st, int16_t *sampv, size_t *sampc, /* See RFC 3551 */ static struct aucodec l16v[NR_CODECS] = { - {LE_INIT, "10", "L16", 44100, 2, 0, 0, encode, 0, decode, 0, 0, 0}, - {LE_INIT, 0, "L16", 32000, 2, 0, 0, encode, 0, decode, 0, 0, 0}, - {LE_INIT, 0, "L16", 16000, 2, 0, 0, encode, 0, decode, 0, 0, 0}, - {LE_INIT, 0, "L16", 8000, 2, 0, 0, encode, 0, decode, 0, 0, 0}, - {LE_INIT, "11", "L16", 44100, 1, 0, 0, encode, 0, decode, 0, 0, 0}, - {LE_INIT, 0, "L16", 32000, 1, 0, 0, encode, 0, decode, 0, 0, 0}, - {LE_INIT, 0, "L16", 16000, 1, 0, 0, encode, 0, decode, 0, 0, 0}, - {LE_INIT, 0, "L16", 8000, 1, 0, 0, encode, 0, decode, 0, 0, 0}, +{LE_INIT, "10", "L16", 44100, 44100, 2, 0, 0, encode, 0, decode, 0, 0, 0}, +{LE_INIT, 0, "L16", 32000, 32000, 2, 0, 0, encode, 0, decode, 0, 0, 0}, +{LE_INIT, 0, "L16", 16000, 16000, 2, 0, 0, encode, 0, decode, 0, 0, 0}, +{LE_INIT, 0, "L16", 8000, 8000, 2, 0, 0, encode, 0, decode, 0, 0, 0}, +{LE_INIT, "11", "L16", 44100, 44100, 1, 0, 0, encode, 0, decode, 0, 0, 0}, +{LE_INIT, 0, "L16", 32000, 32000, 1, 0, 0, encode, 0, decode, 0, 0, 0}, +{LE_INIT, 0, "L16", 16000, 16000, 1, 0, 0, encode, 0, decode, 0, 0, 0}, +{LE_INIT, 0, "L16", 8000, 8000, 1, 0, 0, encode, 0, decode, 0, 0, 0}, }; diff --git a/modules/mpa/mpa.c b/modules/mpa/mpa.c index 88ebfbe..6c2081b 100644 --- a/modules/mpa/mpa.c +++ b/modules/mpa/mpa.c @@ -82,7 +82,8 @@ static struct aucodec mpa = { .pt = NULL, /* for the time being, to cope with AVT AC1 interop problems, we do not use "14" here */ .name = "MPA", - .srate = 90000, + .srate = 48000, + .crate = 90000, .ch = 1, .fmtp = "", .encupdh = mpa_encode_update, diff --git a/modules/opus/opus.c b/modules/opus/opus.c index ec483f6..bf92604 100644 --- a/modules/opus/opus.c +++ b/modules/opus/opus.c @@ -29,6 +29,7 @@ static struct aucodec opus = { .name = "opus", .srate = 48000, + .crate = 48000, .ch = 2, .fmtp = "stereo=1;sprop-stereo=1", .encupdh = opus_encode_update, diff --git a/modules/silk/silk.c b/modules/silk/silk.c index 8232188..73432d2 100644 --- a/modules/silk/silk.c +++ b/modules/silk/silk.c @@ -226,7 +226,7 @@ static int plc(struct audec_state *st, int16_t *sampv, size_t *sampc) static struct aucodec silk[] = { { - LE_INIT, 0, "SILK", 24000, 1, NULL, + LE_INIT, 0, "SILK", 24000, 24000, 1, NULL, encode_update, encode, decode_update, decode, plc, 0, 0 }, diff --git a/modules/speex/speex.c b/modules/speex/speex.c index 0780e8c..13c6e79 100644 --- a/modules/speex/speex.c +++ b/modules/speex/speex.c @@ -462,19 +462,19 @@ static void config_parse(struct conf *conf) static struct aucodec speexv[] = { /* Stereo Speex */ - {LE_INIT, 0, "speex", 32000, 2, speex_fmtp_wb, + {LE_INIT, 0, "speex", 32000, 32000, 2, speex_fmtp_wb, encode_update, encode, decode_update, decode, pkloss, 0, 0}, - {LE_INIT, 0, "speex", 16000, 2, speex_fmtp_wb, + {LE_INIT, 0, "speex", 16000, 16000, 2, speex_fmtp_wb, encode_update, encode, decode_update, decode, pkloss, 0, 0}, - {LE_INIT, 0, "speex", 8000, 2, speex_fmtp_nb, + {LE_INIT, 0, "speex", 8000, 8000, 2, speex_fmtp_nb, encode_update, encode, decode_update, decode, pkloss, 0, 0}, /* Standard Speex */ - {LE_INIT, 0, "speex", 32000, 1, speex_fmtp_wb, + {LE_INIT, 0, "speex", 32000, 32000, 1, speex_fmtp_wb, encode_update, encode, decode_update, decode, pkloss, 0, 0}, - {LE_INIT, 0, "speex", 16000, 1, speex_fmtp_wb, + {LE_INIT, 0, "speex", 16000, 16000, 1, speex_fmtp_wb, encode_update, encode, decode_update, decode, pkloss, 0, 0}, - {LE_INIT, 0, "speex", 8000, 1, speex_fmtp_nb, + {LE_INIT, 0, "speex", 8000, 8000, 1, speex_fmtp_nb, encode_update, encode, decode_update, decode, pkloss, 0, 0}, }; diff --git a/src/audio.c b/src/audio.c index d862e89..6c72c3e 100644 --- a/src/audio.c +++ b/src/audio.c @@ -91,8 +91,6 @@ struct autx { uint32_t ts_tel; /**< Timestamp for Telephony Events */ size_t psize; /**< Packet size for sending */ bool marker; /**< Marker bit for outgoing RTP */ - bool is_g722; /**< Set if encoder is G.722 codec */ - bool is_mpa; /**< Set if encoder is MPA codec */ bool muted; /**< Audio source is muted */ int cur_key; /**< Currently transmitted event */ @@ -248,17 +246,12 @@ static inline uint32_t get_srate(const struct aucodec *ac) if (!ac) return 0; - if (!str_casecmp(ac->name, "G722")) - return 16000; - else if (!str_casecmp(ac->name, "MPA")) - return 48000; - else - return ac->srate; + return ac->srate; } /** - * Get the DSP samplerate for an audio-codec (exception for MPA) + * Get the DSP channels for an audio-codec (exception for MPA) */ static inline uint32_t get_ch(const struct aucodec *ac) { @@ -303,7 +296,12 @@ static int add_audio_codec(struct audio *a, struct sdp_media *m, return 0; } - return sdp_format_add(NULL, m, false, ac->pt, ac->name, ac->srate, + if (ac->crate < 8000) { + warning("audio: illegal clock rate %u\n", ac->crate); + return EINVAL; + } + + return sdp_format_add(NULL, m, false, ac->pt, ac->name, ac->crate, ac->ch, ac->fmtp_ench, ac->fmtp_cmph, ac, false, "%s", ac->fmtp); } @@ -323,6 +321,7 @@ static void encode_rtp_send(struct audio *a, struct autx *tx, int16_t *sampv, size_t sampc) { size_t frame_size; /* number of samples per channel */ + size_t sampc_rtp; size_t len; int err; @@ -349,11 +348,13 @@ static void encode_rtp_send(struct audio *a, struct autx *tx, goto out; } + /* Convert from audio samplerate to RTP clockrate */ + sampc_rtp = sampc * tx->ac->crate / tx->ac->srate; + /* The RTP clock rate used for generating the RTP timestamp is * independent of the number of channels and the encoding */ - frame_size = (tx->is_g722 ? sampc/2 : - tx->is_mpa ? sampc*90/48 : sampc) / get_ch(tx->ac); + frame_size = sampc_rtp / get_ch(tx->ac); tx->ts += (uint32_t)frame_size; @@ -1195,8 +1196,6 @@ int audio_encoder_set(struct audio *a, const struct aucodec *ac, tx->ausrc = mem_deref(tx->ausrc); } - tx->is_g722 = (0 == str_casecmp(ac->name, "G722")); - tx->is_mpa = (0 == str_casecmp(ac->name, "MPA")); tx->enc = mem_deref(tx->enc); tx->ac = ac; } @@ -1213,7 +1212,7 @@ int audio_encoder_set(struct audio *a, const struct aucodec *ac, } } - stream_set_srate(a->strm, get_srate(ac), get_srate(ac)); + stream_set_srate(a->strm, ac->crate, ac->crate); stream_update_encoder(a->strm, pt_tx); if (!tx->ausrc) { @@ -1256,7 +1255,7 @@ int audio_decoder_set(struct audio *a, const struct aucodec *ac, } } - stream_set_srate(a->strm, get_srate(ac), get_srate(ac)); + stream_set_srate(a->strm, ac->crate, ac->crate); if (reset) { diff --git a/test/call.c b/test/call.c index 7bf3e26..0c168ad 100644 --- a/test/call.c +++ b/test/call.c @@ -88,6 +88,7 @@ static struct aucodec dummy_pcma = { .pt = "8", .name = "PCMA", .srate = 8000, + .crate = 8000, .ch = 1, }; -- cgit v1.2.3