diff --git a/configure.ac b/configure.ac index df917b42..b1fbbf23 100644 --- a/configure.ac +++ b/configure.ac @@ -604,6 +604,12 @@ AC_CHECK_LIB(crypto, EVP_PKEY_new_raw_private_key, [CRYPTOLIB="-lcrypto"], [AC_MSG_ERROR([libcrypto (OpenSSL 1.1.1+) not found])]) AC_SUBST(CRYPTOLIB) +dnl MCDE: libopus for dtchat's audio (48 kHz mono, 20 ms, 24 kbit/s). +OPUSLIB="" +AC_CHECK_LIB(opus, opus_encoder_create, + [OPUSLIB="-lopus"], [AC_MSG_ERROR([libopus not found; dtchat needs it for audio])]) +AC_SUBST(OPUSLIB) + dnl MCDE: libtls (libretls) for the dtchat IRC adapter. Without it the dnl adapter reports a clear error for tls:true accounts and plain dnl connections keep working. diff --git a/lib/libDtChat/Makefile.am b/lib/libDtChat/Makefile.am index 510665eb..b3fe2d7b 100644 --- a/lib/libDtChat/Makefile.am +++ b/lib/libDtChat/Makefile.am @@ -11,15 +11,16 @@ libDtChatIpc_la_LDFLAGS = -version-info 1:0:0 libDtChat_la_CPPFLAGS = -I$(top_srcdir)/include libDtChat_la_SOURCES = core.c loop.c store.c conv.c text.c adapter.c instance.c \ - irc.c identity.c peers.c local.c filexfer.c pair.c i18n.c dc_internal.h -libDtChat_la_LIBADD = libDtChatIpc.la $(CRYPTOLIB) $(LIBTLS) + irc.c identity.c peers.c local.c filexfer.c pair.c i18n.c media_audio.c \ + dc_internal.h +libDtChat_la_LIBADD = libDtChatIpc.la $(CRYPTOLIB) $(LIBTLS) $(OPUSLIB) libDtChat_la_LDFLAGS = -version-info 1:0:0 # Built by make mcde-host as well, so tools/mcde-tests.sh finds them in # build/mcde-host; make check still builds and runs them. noinst_PROGRAMS = test_json test_frame test_loop test_store test_conv test_text test_irc \ test_identity test_peers test_local test_filexfer test_pair test_fuzz \ - test_i18n test_adapter test_instance fake_adapter + test_i18n test_audio_opus test_adapter test_instance fake_adapter test_json_SOURCES = test_json.c test_json_CPPFLAGS = -I$(top_srcdir)/include test_json_LDADD = libDtChatIpc.la @@ -72,6 +73,9 @@ test_fuzz_LDADD = libDtChat.la libDtChatIpc.la test_i18n_SOURCES = test_i18n.c test_i18n_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) test_i18n_LDADD = libDtChat.la libDtChatIpc.la +test_audio_opus_SOURCES = test_audio_opus.c +test_audio_opus_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) +test_audio_opus_LDADD = libDtChat.la libDtChatIpc.la $(OPUSLIB) -lm TESTS = test_json test_frame test_loop test_store test_conv test_text test_irc \ test_identity test_peers test_local test_filexfer test_pair test_fuzz \ - test_i18n test_adapter test_instance + test_i18n test_audio_opus test_adapter test_instance diff --git a/lib/libDtChat/dc_internal.h b/lib/libDtChat/dc_internal.h index 943707a9..95782def 100644 --- a/lib/libDtChat/dc_internal.h +++ b/lib/libDtChat/dc_internal.h @@ -12,6 +12,7 @@ #include "DtChat/Ipc.h" #include +#include /* * The core's event loop: one poll(2) per run, monotonic one-shot timers and @@ -330,6 +331,27 @@ void dc_i18n_init(void); void dc_i18n_close(void); const char *dc_text(int id, const char *fallback); +/* + * Audio: Opus 48 kHz mono, 20 ms frames, 24 kbit/s, in-band FEC and PLC. + * dc_opus_enc_frame() encodes exactly DC_AUDIO_FRAME_SAMPLES samples; + * dc_opus_dec_frame() with LEN 0 conceals a lost frame. + */ +#define DC_AUDIO_RATE 48000 +#define DC_AUDIO_FRAME_MS 20 +#define DC_AUDIO_FRAME_SAMPLES (DC_AUDIO_RATE * DC_AUDIO_FRAME_MS / 1000) + +typedef struct DcOpusEnc DcOpusEnc; +typedef struct DcOpusDec DcOpusDec; + +DcOpusEnc *dc_opus_enc_new(int bitrate, char *err, size_t errlen); +int dc_opus_enc_frame(DcOpusEnc *enc, const int16_t *pcm, unsigned char *out, + size_t outlen); +void dc_opus_enc_free(DcOpusEnc *enc); +DcOpusDec *dc_opus_dec_new(char *err, size_t errlen); +int dc_opus_dec_frame(DcOpusDec *dec, const unsigned char *packet, size_t len, + int16_t *pcm); +void dc_opus_dec_free(DcOpusDec *dec); + /* * IRC lines (RFC 1459/2812 + IRCv3 tags). dc_irc_parse() copies the tags, * the prefix, the command, up to 15 parameters and the trailing text; diff --git a/lib/libDtChat/media_audio.c b/lib/libDtChat/media_audio.c new file mode 100644 index 00000000..f1b7d338 --- /dev/null +++ b/lib/libDtChat/media_audio.c @@ -0,0 +1,116 @@ +/* + * media_audio.c -- the Opus encoder and decoder (T6.2) + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + * + * 48 kHz mono, 20 ms frames (960 samples), 24 kbit/s by default, in-band FEC + * and packet-loss concealment. The codec's own delay (about 6.5 ms) is not + * compensated here; the jitter buffer and the call UI handle playout. + */ +#include "dc_internal.h" + +#include +#include + +#include + +struct DcOpusEnc { + OpusEncoder *enc; +}; + +struct DcOpusDec { + OpusDecoder *dec; +}; + +DcOpusEnc * +dc_opus_enc_new(int bitrate, char *err, size_t errlen) +{ + DcOpusEnc *enc; + int error = 0; + + enc = calloc(1, sizeof(*enc)); + if (enc == NULL) + return NULL; + enc->enc = opus_encoder_create(DC_AUDIO_RATE, 1, OPUS_APPLICATION_VOIP, + &error); + if (enc->enc == NULL || error != OPUS_OK) { + if (err != NULL && errlen > 0) + snprintf(err, errlen, "opus encoder: %s", + opus_strerror(error)); + dc_opus_enc_free(enc); + return NULL; + } + (void)opus_encoder_ctl(enc->enc, OPUS_SET_BITRATE(bitrate > 0 ? + bitrate : 24000)); + (void)opus_encoder_ctl(enc->enc, OPUS_SET_INBAND_FEC(1)); + (void)opus_encoder_ctl(enc->enc, OPUS_SET_PACKET_LOSS_PERC(10)); + (void)opus_encoder_ctl(enc->enc, OPUS_SET_COMPLEXITY(8)); + return enc; +} + +int +dc_opus_enc_frame(DcOpusEnc *enc, const int16_t *pcm, unsigned char *out, + size_t outlen) +{ + opus_int32 n; + + if (enc == NULL || pcm == NULL || out == NULL || + outlen > 0x7fffffff) + return -1; + n = opus_encode(enc->enc, pcm, DC_AUDIO_FRAME_SAMPLES, out, + (opus_int32)outlen); + return n < 0 ? -1 : (int)n; +} + +void +dc_opus_enc_free(DcOpusEnc *enc) +{ + if (enc == NULL) + return; + if (enc->enc != NULL) + opus_encoder_destroy(enc->enc); + free(enc); +} + +DcOpusDec * +dc_opus_dec_new(char *err, size_t errlen) +{ + DcOpusDec *dec; + int error = 0; + + dec = calloc(1, sizeof(*dec)); + if (dec == NULL) + return NULL; + dec->dec = opus_decoder_create(DC_AUDIO_RATE, 1, &error); + if (dec->dec == NULL || error != OPUS_OK) { + if (err != NULL && errlen > 0) + snprintf(err, errlen, "opus decoder: %s", + opus_strerror(error)); + dc_opus_dec_free(dec); + return NULL; + } + return dec; +} + +int +dc_opus_dec_frame(DcOpusDec *dec, const unsigned char *packet, size_t len, + int16_t *pcm) +{ + int n; + + if (dec == NULL || pcm == NULL) + return -1; + n = opus_decode(dec->dec, len > 0 ? packet : NULL, (opus_int32)len, + pcm, DC_AUDIO_FRAME_SAMPLES, 0); + return n < 0 ? -1 : n; +} + +void +dc_opus_dec_free(DcOpusDec *dec) +{ + if (dec == NULL) + return; + if (dec->dec != NULL) + opus_decoder_destroy(dec->dec); + free(dec); +} diff --git a/lib/libDtChat/test_audio_opus.c b/lib/libDtChat/test_audio_opus.c new file mode 100644 index 00000000..c0db782d --- /dev/null +++ b/lib/libDtChat/test_audio_opus.c @@ -0,0 +1,110 @@ +/* + * test_audio_opus.c -- tests for the Opus wrapper (T6.2) + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + */ +#include "dc_internal.h" + +#include +#include +#include +#include + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +static int fails; + +#define CHECK(cond) do {\ + if (!(cond)) {\ + fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__,\ + #cond);\ + fails++;\ + }\ +} while (0) + +#define FRAMES 100 /* two seconds */ + +static double +rms(const int16_t *pcm, size_t n) +{ + double sum = 0; + size_t i; + + for (i = 0; i < n; i++) + sum += (double)pcm[i] * (double)pcm[i]; + return n > 0 ? sqrt(sum / (double)n) : 0; +} + +int +main(void) +{ + DcOpusEnc *enc; + DcOpusDec *dec; + unsigned char packet[400]; + int16_t in[DC_AUDIO_FRAME_SAMPLES]; + int16_t out[DC_AUDIO_FRAME_SAMPLES]; + long long bytes = 0; + double decoded_rms; + double input_rms; + int frames_ok = 0; + int i, n; + + enc = dc_opus_enc_new(24000, NULL, 0); + dec = dc_opus_dec_new(NULL, 0); + CHECK(enc != NULL); + CHECK(dec != NULL); + if (enc == NULL || dec == NULL) + return 1; + + for (i = 0; i < FRAMES; i++) { + int s; + int16_t *dst = in; + + for (s = 0; s < DC_AUDIO_FRAME_SAMPLES; s++) { + double t = (double)(i * DC_AUDIO_FRAME_SAMPLES + s) / + (double)DC_AUDIO_RATE; + + in[s] = (int16_t)(sin(2.0 * M_PI * 440.0 * t) * + 9830.0); + } + n = dc_opus_enc_frame(enc, dst, packet, sizeof(packet)); + CHECK(n > 0); + if (n <= 0) + break; + bytes += n; + n = dc_opus_dec_frame(dec, packet, (size_t)n, out); + CHECK(n == DC_AUDIO_FRAME_SAMPLES); + if (n == DC_AUDIO_FRAME_SAMPLES && i > 5) { + decoded_rms = rms(out, DC_AUDIO_FRAME_SAMPLES); + input_rms = rms(dst, DC_AUDIO_FRAME_SAMPLES); + if (decoded_rms > input_rms * 0.5) + frames_ok++; + } + } + CHECK(frames_ok > FRAMES - 15); + /* 24 kbit/s for 2 s is 6000 bytes; allow the codec's overhead */ + CHECK(bytes > 3000 && bytes < 12000); + + /* packet loss concealment: after good frames, an empty packet */ + n = dc_opus_dec_frame(dec, NULL, 0, out); + CHECK(n == DC_AUDIO_FRAME_SAMPLES); + { + int non_zero = 0; + + for (i = 0; i < n; i++) + if (out[i] != 0) + non_zero++; + CHECK(non_zero > 0); + } + + dc_opus_enc_free(enc); + dc_opus_dec_free(dec); + if (fails != 0) { + fprintf(stderr, "%d test(s) failed\n", fails); + return 1; + } + printf("test_audio_opus: all ok\n"); + return 0; +}