diff --git a/Dockerfile b/Dockerfile index fc9fee560..d19e63947 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,6 +16,7 @@ RUN apt-get update \ libavcodec-extra \ libavfilter-dev \ libbcg729-dev \ + libcodec2-dev \ libcurl4-openssl-dev \ libevent-dev \ libglib2.0-dev \ diff --git a/daemon/Makefile b/daemon/Makefile index 5b532bc7a..42d8c91e1 100644 --- a/daemon/Makefile +++ b/daemon/Makefile @@ -75,6 +75,9 @@ ifeq ($(with_transcoding),yes) CFLAGS += $(CFLAGS_BCG729) LDLIBS += $(LDLIBS_BCG729) +CFLAGS += $(CFLAGS_CODEC2) +LDLIBS += $(LDLIBS_CODEC2) + CFLAGS += $(CFLAGS_CODEC_CHAIN) LDLIBS += $(LDLIBS_CODEC_CHAIN) endif diff --git a/debian/control b/debian/control index f7f7644f7..0d6615cf7 100644 --- a/debian/control +++ b/debian/control @@ -17,6 +17,7 @@ Build-Depends: libbcg729-dev , libbellesip-dev , libbencode-perl, + libcodec2-dev , libcrypt-openssl-rsa-perl, libcrypt-rijndael-perl, libcurl4-openssl-dev | libcurl4-gnutls-dev, diff --git a/docs/compiling_and_installing.md b/docs/compiling_and_installing.md index a2bce5c75..dec90a503 100644 --- a/docs/compiling_and_installing.md +++ b/docs/compiling_and_installing.md @@ -82,6 +82,7 @@ test suite. - *libiptc* library for iptables management (optional) - *ffmpeg* codec libraries for transcoding (optional) such as *libavcodec*, *libavfilter*, *libswresample* - *bcg729* for full G.729 transcoding support (optional) + - *libcodec2* for Codec 2 transcoding support (optional) - *libmosquitto* - *libwebsockets* - *libopus* diff --git a/docs/transcoding.md b/docs/transcoding.md index c8bad1205..f79212c3f 100644 --- a/docs/transcoding.md +++ b/docs/transcoding.md @@ -31,6 +31,7 @@ The following codecs are supported by *rtpengine*: * Opus * AMR (narrowband and wideband) * EVS (if supplied -- see below) +* Codec 2 (if built with *libcodec2* -- see below) Codec support is dependent on support provided by the `ffmpeg` codec libraries, which may vary from version to version. Use the `--codecs` command line option to have *rtpengine* print a list of codecs @@ -72,6 +73,28 @@ Set the environment variable `export DEB_BUILD_PROFILES="pkg.ngcp-rtpengine.nobcg729"` (or use the `-P` flag to the *dpkg* tools) and then build the *rtpengine* packages. +## Codec 2 support + +Transcoding to and from [Codec 2](https://www.rowetel.com/?page_id=452) is available through the +*libcodec2* library. The build system uses the library if *pkg-config* finds it, or if its headers +are installed in `/usr/include/codec2`. Without it, *rtpengine* still recognises Codec 2 in SDP and +passes it through, but can't transcode it. In a Debian build environment, the build dependency on +*libcodec2-dev* can be dropped with the build profile `pkg.ngcp-rtpengine.nocodec2`. + +There is no standard RTP payload format for Codec 2. *Rtpengine* follows the convention used by +*Asterisk*: the encoding name is `codec2` with a clock rate of 8000 Hz (mono only), and each RTP +packet carries one or more complete Codec 2 frames back to back, each padded to whole bytes. + +The Codec 2 mode is selected through the format parameter `mode=`, with one of the values `3200`, +`2400`, `1600`, `1400`, `1300`, `1200` or `700C`. Without it, mode 2400 is used, which is also +the only mode *Asterisk* supports. Both sides must use the same mode for a Codec 2 stream to be +passed through without transcoding. Modes 1600 and below use 40 ms frames, so their packetisation +time is always a multiple of 40 ms. Other modes (such as 450) can be passed through, but not +transcoded. + +When requesting Codec 2 through the `transcode` option, the mode can be given as the bit rate, for +example `codec2/8000/1/3200`, or as a format parameter, for example `codec2/8000/1///mode=1300`. + ## DTMF transcoding *Rtpengine* supports transcoding between RFC 2833/4733 DTMF event packets (`telephone-event` payloads) diff --git a/lib/codec2.c b/lib/codec2.c new file mode 100644 index 000000000..5279185ca --- /dev/null +++ b/lib/codec2.c @@ -0,0 +1,405 @@ +#include "codecmod.h" +#include "loglib.h" + +// Codec 2 (https://www.rowetel.com/?page_id=452) through libcodec2. +// +// There is no RFC payload format for Codec 2. This follows what Asterisk does: encoding +// name "codec2" with an 8 kHz clock, and the payload of each RTP packet is one or more +// complete Codec 2 frames back to back, each padded to whole bytes as produced by +// libcodec2. The bit rate (Codec 2 "mode") is selected through the optional format +// parameter "mode=" (3200, 2400, 1600, 1400, 1300, 1200 or 700C). Without it, mode 2400 +// is assumed, which is the only mode Asterisk supports. + +#define CODEC2_DEFAULT_MODE 2400 + + +// modes are identified by their bit rate (700 for 700C); returns -1 if there is no such mode +static int codec2_mode_from_int(int br) { + switch (br) { + case 3200: + case 2400: + case 1600: + case 1400: + case 1300: + case 1200: + case 700: + return br; + } + return -1; +} + +// returns the mode for a "mode=" value, or -1 if unknown +static int codec2_mode_from_str(const str *s) { + if (!str_cmp(s, "700C") || !str_cmp(s, "700c")) + return 700; + return codec2_mode_from_int(str_to_i(s, -1)); +} + +static const char *codec2_mode_name(int mode) { + switch (mode) { + case 3200: + return "3200"; + case 2400: + return "2400"; + case 1600: + return "1600"; + case 1400: + return "1400"; + case 1300: + return "1300"; + case 1200: + return "1200"; + case 700: + return "700C"; + } + return NULL; +} + +static const char *codec2_mode_fmtp(int mode) { + switch (mode) { + case 3200: + return "mode=3200"; + case 2400: + return "mode=2400"; + case 1600: + return "mode=1600"; + case 1400: + return "mode=1400"; + case 1300: + return "mode=1300"; + case 1200: + return "mode=1200"; + case 700: + return "mode=700C"; + } + return NULL; +} + +static void codec2_parse_format_cb(str *key, str *token, void *data) { + union codec_format_options *opts = data; + + if (str_cmp(key, "mode")) + return; + + int mode = codec2_mode_from_str(token); + if (mode < 0) { + ilog(LOG_WARN | LOG_FLAG_LIMIT, "Unsupported Codec 2 mode '" STR_FORMAT "'", + STR_FMT(token)); + opts->codec2.mode = -1; + return; + } + opts->codec2.mode = mode; +} + +static bool codec2_format_parse(struct rtp_codec_format *f, const str *fmtp) { + codeclib_key_value_parse(fmtp, codec2_parse_format_cb, &f->parsed); + return true; +} + +// absent "mode=" means the default mode +static int codec2_effective_mode(const struct rtp_payload_type *p) { + if (!p->format.fmtp_parsed) + return CODEC2_DEFAULT_MODE; + return p->format.parsed.codec2.mode ?: CODEC2_DEFAULT_MODE; +} + +static int codec2_format_cmp(const struct rtp_payload_type *A, const struct rtp_payload_type *B) { + // different modes are different bit streams + int a = codec2_effective_mode(A); + int b = codec2_effective_mode(B); + if (a < 0 || b < 0) { + // a mode we don't know (e.g. 450): can still be passed through if both + // sides use the very same format parameters + if (a != b || str_cmp_str(&A->format_parameters, &B->format_parameters)) + return -1; + return 0; + } + if (a != b) + return -1; + return 0; +} + +static GString *codec2_format_print(const struct rtp_payload_type *p) { + if (!p->format.fmtp_parsed) + return NULL; + if (p->format.parsed.codec2.mode < 0) + return NULL; // unknown mode: pass the original fmtp through + + GString *s = g_string_new(""); + const char *fmtp = codec2_mode_fmtp(p->format.parsed.codec2.mode); + if (fmtp) + g_string_append(s, fmtp); + + return s; +} + + +static void codec2_init(struct rtp_payload_type *pt) { + if (pt->clock_rate != 8000) { + ilog(LOG_WARN, "Codec 2 is only supported with a clock rate of 8000 Hz"); + pt->clock_rate = 8000; + } + + // a bit rate given in the codec string (e.g. "codec2/8000/1/3200") selects the mode, + // so that it's also signalled in the SDP + if (pt->format.fmtp_parsed && !pt->format.parsed.codec2.mode && pt->bitrate + && pt->bitrate != CODEC2_DEFAULT_MODE) + { + int mode = codec2_mode_from_int(pt->bitrate); + if (mode > 0) { + pt->format.parsed.codec2.mode = mode; + // also keep the plain fmtp string in line. A static string is fine here + // because the caller copies it right after this + if (!pt->format_parameters.len) + pt->format_parameters = STR(codec2_mode_fmtp(mode)); + } + else + ilog(LOG_WARN, "Codec 2 has no mode for a bit rate of %i bit/s", pt->bitrate); + } + + int mode = codec2_effective_mode(pt); + if (mode > 0) + pt->bitrate = mode; + + // modes 1600 and below use 40 ms frames + if (mode > 0 && mode <= 1600 && pt->ptime > 0 && pt->ptime % 40) { + int np = (pt->ptime + 39) / 40 * 40; + ilog(LOG_DEBUG, "Codec 2 mode %s uses 40 ms frames, using ptime %i ms instead of %i ms", + codec2_mode_name(mode), np, pt->ptime); + pt->ptime = np; + } +} + + +#ifdef HAVE_CODEC2 + +#include "fix_frame_channel_layout.compat" + + +static int codec2_lib_mode(int mode) { + switch (mode) { + case 3200: + return CODEC2_MODE_3200; + case 2400: + return CODEC2_MODE_2400; + case 1600: + return CODEC2_MODE_1600; + case 1400: + return CODEC2_MODE_1400; + case 1300: + return CODEC2_MODE_1300; + case 1200: + return CODEC2_MODE_1200; + case 700: + return CODEC2_MODE_700C; + } + return -1; +} + +// the mode comes from fmtp only, so that both sides agree on it +static int codec2_select_mode(const union codec_format_options *fmtp) { + if (fmtp && fmtp->codec2.mode) + return fmtp->codec2.mode; + return CODEC2_DEFAULT_MODE; +} + + +static void codec2_def_init(struct codec_def_s *def) { + struct CODEC2 *c2 = codec2_create(CODEC2_MODE_2400); + if (!c2) + return; + codec2_destroy(c2); + def->support_encoding = 1; + def->support_decoding = 1; +} + + +static const char *codec2_decoder_init(decoder_t *dec, const str *extra_opts) { + if (dec->in_format.clockrate != 8000) + return "Codec 2 requires a clock rate of 8000 Hz"; + if (dec->in_format.channels != 1) + return "Codec 2 supports mono only"; + + int mode = codec2_select_mode(&dec->format_options); + int lib_mode = codec2_lib_mode(mode); + if (lib_mode < 0) + return "unsupported Codec 2 mode"; + + dec->codec2 = codec2_create(lib_mode); + if (!dec->codec2) + return "failed to create Codec 2 decoder"; + + ilog(LOG_DEBUG, "Created Codec 2 decoder in mode %s (%i samples and %i bytes per frame)", + codec2_mode_name(mode), codec2_samples_per_frame(dec->codec2), + codec2_bytes_per_frame(dec->codec2)); + + return NULL; +} + +static int codec2_decoder_input(decoder_t *dec, const str *data, frame_q *out, bool mark) { + struct CODEC2 *c2 = dec->codec2; + const int bytes_per_frame = codec2_bytes_per_frame(c2); + const int samples_per_frame = codec2_samples_per_frame(c2); + + size_t num_frames = data->len / bytes_per_frame; + if (num_frames == 0 || data->len % bytes_per_frame) { + ilog(LOG_WARN | LOG_FLAG_LIMIT, "Codec 2 payload of %zu bytes is not a multiple of the " + "frame size (%i bytes)", data->len, bytes_per_frame); + if (num_frames == 0) + return -1; + } + + AVFrame *frame = av_frame_alloc(); + frame->nb_samples = num_frames * samples_per_frame; + frame->format = AV_SAMPLE_FMT_S16; + frame->sample_rate = dec->in_format.clockrate; + DEF_CH_LAYOUT(&frame->CH_LAYOUT, 1); + frame->pts = dec->pts; + if (av_frame_get_buffer(frame, 0) < 0) + abort(); + + const unsigned char *in = (const unsigned char *) data->s; + short *pcm = (short *) frame->extended_data[0]; + for (size_t i = 0; i < num_frames; i++) { + codec2_decode(c2, pcm, in); + in += bytes_per_frame; + pcm += samples_per_frame; + } + + t_queue_push_tail(out, frame); + return 0; +} + +static void codec2_decoder_close(decoder_t *dec) { + if (dec->codec2) + codec2_destroy(dec->codec2); + dec->codec2 = NULL; +} + + +static const char *codec2_encoder_init(encoder_t *enc, const str *extra_opts) { + if (enc->requested_format.channels != 1) + ilog(LOG_WARN, "Codec 2 supports mono only, ignoring %i requested channels", + enc->requested_format.channels); + + int mode = codec2_select_mode(&enc->format_options); + int lib_mode = codec2_lib_mode(mode); + if (lib_mode < 0) + return "unsupported Codec 2 mode"; + + enc->codec2 = codec2_create(lib_mode); + if (!enc->codec2) + return "failed to create Codec 2 encoder"; + + if (enc->requested_format.clockrate != 8000) + ilog(LOG_WARN, "Codec 2 requires a clock rate of 8000 Hz, ignoring requested %i Hz", + enc->requested_format.clockrate); + + enc->actual_format = enc->requested_format; + enc->actual_format.format = AV_SAMPLE_FMT_S16; + enc->actual_format.channels = 1; + enc->actual_format.clockrate = 8000; + enc->actual_format.time_base = 8000; + + // put as many whole frames into each packet as fit the ptime, but at least one + // (the 1600 and lower modes use 40 ms frames) + int samples_per_frame = codec2_samples_per_frame(enc->codec2); + int frame_ms = samples_per_frame * 1000 / enc->actual_format.clockrate; + int frames = enc->ptime / frame_ms; + if (frames < 1) + frames = 1; + if (frames * frame_ms != enc->ptime) + ilog(LOG_INFO, "Codec 2 mode %s uses %i ms frames, sending %i ms per packet instead of " + "ptime %i ms", codec2_mode_name(mode), frame_ms, frames * frame_ms, enc->ptime); + + enc->samples_per_frame = frames * samples_per_frame; + enc->samples_per_packet = enc->samples_per_frame; + enc->bitrate = mode; + + ilog(LOG_DEBUG, "Created Codec 2 encoder in mode %s (%i frames of %i bytes per packet)", + codec2_mode_name(mode), frames, codec2_bytes_per_frame(enc->codec2)); + + return NULL; +} + +static int codec2_encoder_input(encoder_t *enc, AVFrame **frame) { + if (!*frame) + return 0; + + struct CODEC2 *c2 = enc->codec2; + const int samples_per_frame = codec2_samples_per_frame(c2); + const int bytes_per_frame = codec2_bytes_per_frame(c2); + + if ((*frame)->nb_samples != enc->samples_per_frame) { + ilog(LOG_ERR | LOG_FLAG_LIMIT, "Codec 2: input %i samples instead of %i", + (*frame)->nb_samples, enc->samples_per_frame); + return -1; + } + + int num_frames = (*frame)->nb_samples / samples_per_frame; + av_new_packet(enc->avpkt, num_frames * bytes_per_frame); + + short *pcm = (short *) (*frame)->extended_data[0]; + unsigned char *out = enc->avpkt->data; + for (int i = 0; i < num_frames; i++) { + codec2_encode(c2, out, pcm); + pcm += samples_per_frame; + out += bytes_per_frame; + } + + enc->avpkt->pts = (*frame)->pts; + enc->avpkt->duration = (*frame)->nb_samples; + + return 0; +} + +static void codec2_encoder_close(encoder_t *enc) { + if (enc->codec2) + codec2_destroy(enc->codec2); + enc->codec2 = NULL; +} + + +static const codec_type_t codec_type_codec2 = { + .def_init = codec2_def_init, + .decoder_init = codec2_decoder_init, + .decoder_input = codec2_decoder_input, + .decoder_close = codec2_decoder_close, + .encoder_init = codec2_encoder_init, + .encoder_input = codec2_encoder_input, + .encoder_close = codec2_encoder_close, +}; + +#endif + + +static const codec_def_t codec2 = { + .rtpname = "codec2", + .avcodec_id = -1, + .default_clockrate = 8000, + .default_channels = 1, + .default_bitrate = CODEC2_DEFAULT_MODE, + .default_ptime = 20, + .minimum_ptime = 20, + .init = codec2_init, + .format_parse = codec2_format_parse, + .format_cmp = codec2_format_cmp, + .format_print = codec2_format_print, + .packetizer = &packetizer_passthrough, + .bits_per_sample = 1, // at most 3200 bit/s at 8000 samples/s + .media_type = MT_AUDIO, +#ifdef HAVE_CODEC2 + .codec_type = &codec_type_codec2, +#endif + .dtx_methods = { + [DTX_SILENCE] = &dtx_method_silence, + [DTX_CN] = &dtx_method_cn, + }, + .fixed_sizes = 1, +}; + + +__attribute__((constructor)) +static void init(void) { + codeclib_register_codec(&codec2); +} diff --git a/lib/codeclib.h b/lib/codeclib.h index b00f956f2..9f83b7af5 100644 --- a/lib/codeclib.h +++ b/lib/codeclib.h @@ -64,6 +64,9 @@ typedef GString *format_print_f(const struct rtp_payload_type *); #include #include #endif +#ifdef HAVE_CODEC2 +#include +#endif #include #define AMR_FT_TYPES 14 @@ -348,6 +351,9 @@ struct decoder_s { } dtmf; void *evs; OpusDecoder *opus; +#ifdef HAVE_CODEC2 + struct CODEC2 *codec2; +#endif }; unsigned long rtp_ts; @@ -404,6 +410,9 @@ struct encoder_s { int64_t cmr_in_ts; } evs; OpusEncoder *opus; +#ifdef HAVE_CODEC2 + struct CODEC2 *codec2; +#endif }; AVPacket *avpkt; AVAudioFifo *fifo; diff --git a/lib/common.Makefile b/lib/common.Makefile index c744b0b2a..693fa26d8 100644 --- a/lib/common.Makefile +++ b/lib/common.Makefile @@ -41,7 +41,7 @@ $(OBJS): Makefile ../include/* ../lib/*.h ../kernel-module/*.h -M "date:$(BUILD_DATE)" \ -o "$@" -resample.c codeclib.c avc.c mix.c packet.c g729.c opus.c evs.c pseudo.c: ../lib/fix_frame_channel_layout.compat +resample.c codeclib.c avc.c mix.c packet.c g729.c opus.c evs.c pseudo.c codec2.c: ../lib/fix_frame_channel_layout.compat ifeq ($(with_transcoding),yes) ../daemon/codec.c codec.c: ../lib/dtmf_rx_fillin.compat diff --git a/lib/deps.Makefile b/lib/deps.Makefile index 08f4213be..4a071da73 100644 --- a/lib/deps.Makefile +++ b/lib/deps.Makefile @@ -2,11 +2,15 @@ $(top_srcdir)/config.mk: $(top_srcdir)/utils/gen-common-flags \ $(top_srcdir)/utils/gen-bcg729-flags \ + $(top_srcdir)/utils/gen-codec2-flags \ $(top_srcdir)/utils/gen-codec-chain-flags $(top_srcdir)/utils/gen-common-flags >$@.new ifeq (,$(filter pkg.ngcp-rtpengine.nobcg729 pkg.rtpengine.nobcg729,$(DEB_BUILD_PROFILES))) $(top_srcdir)/utils/gen-bcg729-flags >>$@.new endif +ifeq (,$(filter pkg.ngcp-rtpengine.nocodec2 pkg.rtpengine.nocodec2,$(DEB_BUILD_PROFILES))) + $(top_srcdir)/utils/gen-codec2-flags >>$@.new +endif ifneq (,$(filter pkg.ngcp-rtpengine.codec-chain pkg.rtpengine.codec-chain,$(DEB_BUILD_PROFILES))) $(top_srcdir)/utils/gen-codec-chain-flags >>$@.new endif diff --git a/lib/flags.Makefile b/lib/flags.Makefile index 2f26d1260..aa5309b69 100644 --- a/lib/flags.Makefile +++ b/lib/flags.Makefile @@ -19,6 +19,7 @@ CFLAGS += -DWITH_TRANSCODING CODEC_SRCS += g711.c g723.c g722.c qcelp.c g729.c speex.c gsm.c ilbc.strhash.c opus.strhash.c CODEC_SRCS += evs.strhash.c vorbis.c ac3.c atrac.c evrc.c amr.strhash.c pseudo.c CODEC_SRCS += g726.c l16.c u8.c mp3.c +CODEC_SRCS += codec2.c CODEC_SRCS += avc.c CODEC_SRCS += vp8.c CODEC_SRCS += vp9.c diff --git a/lib/rtplib.h b/lib/rtplib.h index fb18da290..4f436c02f 100644 --- a/lib/rtplib.h +++ b/lib/rtplib.h @@ -94,6 +94,10 @@ union codec_format_options { int level_asymmetry_allowed:1; } h264; + + struct { + int mode; // bit rate of the "mode=" parameter (700 for 700C), 0 = not given + } codec2; }; struct rtp_codec_format { diff --git a/perf-tester/Makefile b/perf-tester/Makefile index 17cfc6102..9051d768c 100644 --- a/perf-tester/Makefile +++ b/perf-tester/Makefile @@ -43,6 +43,9 @@ LDLIBS += $(LDLIBS_OPENSSL) CFLAGS += $(CFLAGS_BCG729) LDLIBS += $(LDLIBS_BCG729) +CFLAGS += $(CFLAGS_CODEC2) +LDLIBS += $(LDLIBS_CODEC2) + CFLAGS += $(CFLAGS_CODEC_CHAIN) LDLIBS += $(LDLIBS_CODEC_CHAIN) diff --git a/recording-daemon/Makefile b/recording-daemon/Makefile index 6a2265e8b..1a1c6a23f 100644 --- a/recording-daemon/Makefile +++ b/recording-daemon/Makefile @@ -40,6 +40,9 @@ LDLIBS += $(LDLIBS_LIBJWT) CFLAGS += $(CFLAGS_BCG729) LDLIBS += $(LDLIBS_BCG729) +CFLAGS += $(CFLAGS_CODEC2) +LDLIBS += $(LDLIBS_CODEC2) + SRCS := epoll.c garbage.c inotify.c main.c metafile.c stream.c recaux.c packet.c \ decoder.c output.c mix.c db.c log.c forward.c tag.c custom_poller.c notify.c tls_send.c s3.c \ gcs.c diff --git a/t/Makefile b/t/Makefile index caa855cf9..4187a6c09 100644 --- a/t/Makefile +++ b/t/Makefile @@ -31,6 +31,7 @@ CFLAGS += $(CFLAGS_LIBSWSCALE) CFLAGS += $(CFLAGS_LIBAVFILTER) CFLAGS += $(CFLAGS_SPANDSP) CFLAGS += $(CFLAGS_OPUS) +CFLAGS += $(CFLAGS_CODEC2) CFLAGS += $(CFLAGS_LIBZ) CFLAGS += $(CFLAGS_LIBWEBSOCKETS) CFLAGS += $(CFLAGS_LIBEVENT) @@ -67,6 +68,7 @@ LDLIBS += $(LDLIBS_LIBEVENT) LDLIBS += $(LDLIBS_LIBHIREDIS) LDLIBS += $(LDLIBS_MYSQL) LDLIBS += $(LDLIBS_BCG729) +LDLIBS += $(LDLIBS_CODEC2) endif CFLAGS += $(CFLAGS_CODEC_CHAIN) @@ -545,6 +547,7 @@ test-transcode: test-transcode.o \ ../lib/bencode.o \ ../lib/bufferpool.o \ ../lib/cchain.o \ + ../lib/codec2.o \ ../lib/codeclib.o \ ../lib/dtmflib.o \ ../lib/g711.o \ diff --git a/t/test-transcode.c b/t/test-transcode.c index c28a36742..e9d28ef1f 100644 --- a/t/test-transcode.c +++ b/t/test-transcode.c @@ -212,7 +212,7 @@ static void __expect(const char *file, int line, rtp_pt_q *dumper, const char *c g_string_free(s, TRUE); } -#ifdef WITH_AMR_TESTS +#if defined(WITH_AMR_TESTS) || defined(HAVE_CODEC2) #define check_encoder(side, otherside, in_pt, out_pt, out_bitrate) \ __check_encoder(__FILE__, __LINE__, media_ ## side, media_ ## otherside, in_pt, out_pt, out_bitrate) @@ -432,6 +432,14 @@ static void dtmf(const char *s) { #define PCMU_silence "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" #define G722_payload "\x23\x84\x20\x84\x20\x84\x04\x84\x04\x04\x84\x04\x84\x04\x84\x05\x85\x46\x87\x48\xc8\x48\x88\x48\xc8\x49\x8a\x4b\xcc\x4c\x8c\x4c\xcc\x4c\x8c\x4d\xce\x50\xcf\x51\x90\x50\xcf\x12\xd1\x52\xd2\x54\x91\x52\xd2\x54\x92\x54\xd3\x56\x93\xd6\x94\xd4\x93\xd7\xd5\x55\x94\x55\xd5\x55\xd4\x56\xd5\x17\xd7\x5a\x95\xd7\x97\xd9\xd4\x16\x58\x57\x98\xd5\xd7\x5b\x96\xda\xd6\x1b\x57\x5a\xd6\x1a\x57\x5b\x98\xd6\xd8\x56\x98\xd7\xd9\x5a\x95\xdb\xd6\x1c\x52\x5e\xd7\x5c\x93\xdf\x99\xd5\xd7\x5f\xd9\x14\x56\x7f\x92\xda\xd9\x5c\x92\xdd\xd7\x5d\x92\xff\xd6\x5a\x96\xdc\xd5\x18\x56\x7e\xd2\x5e\x96\xde\x94\xd8\xd8\x58\xd3\x79\x93\xfb\x90\xdc\xd6\x5b\xdd\x58\x96\xff" #define AMR_WB_payload "\xf0\x1c\xf3\x06\x08\x10\x77\x32\x23\x20\xd3\x50\x62\x12\xc7\x7c\xe2\xea\x84\x0e\x6e\xf4\x4d\xe4\x7f\xc9\x4c\xcc\x58\x5d\xed\xcc\x5d\x7c\x6c\x14\x7d\xc0" // octet aligned +#ifdef HAVE_CODEC2 +// Codec 2 frames: only the sizes are checked, as the encoded bits depend on the libcodec2 version +#define CODEC2_3200_payload "\x8c\x25\x13\x6a\x9d\x4e\x22\x07" // 64 bits per 20 ms +#define CODEC2_2400_payload "\x8c\x25\x13\x6a\x9d\x4e" // 48 bits per 20 ms +#define CODEC2_1600_payload "\x8c\x25\x13\x6a\x9d\x4e\x22\x07" // 64 bits per 40 ms +#define CODEC2_1300_payload "\x8c\x25\x13\x6a\x9d\x4e\x20" // 52 bits per 40 ms +#define CODEC2_450_payload "\x8c\x25\x13" // 18 bits per 40 ms +#endif #define AMR_WB_payload_noe "\xf1\xfc\xc1\x82\x04\x1d\xcc\x88\xc8\x34\xd4\x18\x84\xb1\xdf\x38\xba\xa1\x03\x9b\xbd\x13\x79\x1f\xf2\x53\x33\x16\x17\x7b\x73\x17\x5f\x1b\x05\x1f\x70" // bandwidth efficient int main(void) { @@ -766,6 +774,100 @@ int main(void) { } #endif +#ifdef HAVE_CODEC2 + { + str codec_name = STR_CONST("codec2"); + codec_def_t *def = codec_find(&codec_name, MT_AUDIO); + assert(def); + // built with libcodec2, so both directions must be supported + assert(def->support_encoding && def->support_decoding); + + // forward Codec 2, default mode 2400: one 6-byte frame per 20 ms + start(); + sdp_pt(0, PCMU, 8000); + transcode(codec2); + offer(); + expect(A, "0/PCMU/8000"); + expect(B, "0/PCMU/8000 96/codec2/8000"); + sdp_pt(96, codec2, 8000); + answer(); + expect(A, "0/PCMU/8000"); + expect(B, "96/codec2/8000"); + packet_seq_nf(A, 0, PCMU_payload, 0, 0, 96, CODEC2_2400_payload); + packet_seq_nf(A, 0, PCMU_payload, 160, 1, 96, CODEC2_2400_payload); + packet_seq_nf(B, 96, CODEC2_2400_payload, 0, 0, 0, PCMU_payload); + packet_seq_nf(B, 96, CODEC2_2400_payload, 160, 1, 0, PCMU_payload); + check_encoder(A, B, 0, 96, 2400); + end(); + + // reverse Codec 2, mode 3200: one 8-byte frame per 20 ms + start(); + sdp_pt_fmt(96, codec2, 8000, "mode=3200"); + transcode(PCMU); + offer(); + expect(A, "96/codec2/8000/mode=3200"); + expect(B, "96/codec2/8000/mode=3200 0/PCMU/8000"); + sdp_pt(0, PCMU, 8000); + answer(); + expect(A, "96/codec2/8000/mode=3200"); + expect(B, "0/PCMU/8000"); + packet_seq_nf(B, 0, PCMU_payload, 0, 0, 96, CODEC2_3200_payload); + packet_seq_nf(B, 0, PCMU_payload, 160, 1, 96, CODEC2_3200_payload); + packet_seq_nf(A, 96, CODEC2_3200_payload, 0, 0, 0, PCMU_payload); + packet_seq_nf(A, 96, CODEC2_3200_payload, 160, 1, 0, PCMU_payload); + check_encoder(B, A, 0, 96, 3200); + end(); + + // forward Codec 2, mode 1300 selected through the bit rate: 40 ms frames, + // so two 20 ms PCMU packets make one 7-byte Codec 2 packet + start(); + sdp_pt(0, PCMU, 8000); + transcode_s("codec2/8000/1/1300"); + offer(); + expect(A, "0/PCMU/8000"); + expect(B, "0/PCMU/8000 96/codec2/8000/mode=1300"); + sdp_pt_fmt(96, codec2, 8000, "mode=1300"); + answer(); + expect(A, "0/PCMU/8000"); + expect(B, "96/codec2/8000/mode=1300"); + packet_seq(A, 0, PCMU_payload, 0, 0, -1, ""); // nothing: half a frame + packet_seq_nf(A, 0, PCMU_payload, 160, 1, 96, CODEC2_1300_payload); + packet_seq(A, 0, PCMU_payload, 320, 2, -1, ""); + packet_seq_nf(A, 0, PCMU_payload, 480, 3, 96, CODEC2_1300_payload); + check_encoder(A, B, 0, 96, 1300); + end(); + + // same mode on both sides: plain passthrough + start(); + sdp_pt_fmt(96, codec2, 8000, "mode=1600"); + transcode(PCMU); + offer(); + expect(A, "96/codec2/8000/mode=1600"); + expect(B, "96/codec2/8000/mode=1600 0/PCMU/8000"); + sdp_pt_fmt(96, codec2, 8000, "mode=1600"); + answer(); + expect(A, "96/codec2/8000/mode=1600"); + expect(B, "96/codec2/8000/mode=1600"); + packet(A, 96, CODEC2_1600_payload, 96, CODEC2_1600_payload); + packet(B, 96, CODEC2_1600_payload, 96, CODEC2_1600_payload); + end(); + + // a mode we can't transcode is still passed through unchanged + start(); + sdp_pt_fmt(96, codec2, 8000, "mode=450"); + offer(); + expect(A, "96/codec2/8000/mode=450"); + expect(B, "96/codec2/8000/mode=450"); + sdp_pt_fmt(96, codec2, 8000, "mode=450"); + answer(); + expect(A, "96/codec2/8000/mode=450"); + expect(B, "96/codec2/8000/mode=450"); + packet(A, 96, CODEC2_450_payload, 96, CODEC2_450_payload); + packet(B, 96, CODEC2_450_payload, 96, CODEC2_450_payload); + end(); + } +#endif + // G.722 <> PCMA start(); sdp_pt(8, PCMA, 8000); diff --git a/utils/gen-codec2-flags b/utils/gen-codec2-flags new file mode 100755 index 000000000..7f1ecb7fb --- /dev/null +++ b/utils/gen-codec2-flags @@ -0,0 +1,13 @@ +#!/bin/bash + +# Codec 2 support is optional: only enabled if libcodec2 is found + +if pkg-config --exists codec2; then + echo "CFLAGS_CODEC2 := -DHAVE_CODEC2" + echo "CFLAGS_CODEC2 += $(pkg-config --cflags codec2)" + echo "LDLIBS_CODEC2 := $(pkg-config --libs codec2)" +elif [ -e /usr/include/codec2/codec2.h ]; then + echo "CFLAGS_CODEC2 := -DHAVE_CODEC2" + echo "CFLAGS_CODEC2 += -I/usr/include/codec2" + echo "LDLIBS_CODEC2 := -lcodec2" +fi