2020-03-12 09:10:33 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-3.0-or-later
|
2018-08-26 07:36:42 +08:00
|
|
|
* Copyright © 2016-2018 The TokTok team.
|
2017-01-14 23:46:31 +08:00
|
|
|
* Copyright © 2013-2015 Tox project.
|
2015-04-21 08:31:12 +08:00
|
|
|
*/
|
2015-10-11 05:54:23 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
#include "audio.h"
|
2016-09-11 22:47:51 +08:00
|
|
|
|
2018-02-27 10:07:59 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
#include "rtp.h"
|
|
|
|
|
|
|
|
#include "../toxcore/logger.h"
|
2018-07-08 16:43:42 +08:00
|
|
|
#include "../toxcore/mono_time.h"
|
2015-04-21 08:31:12 +08:00
|
|
|
|
|
|
|
static struct JitterBuffer *jbuf_new(uint32_t capacity);
|
|
|
|
static void jbuf_clear(struct JitterBuffer *q);
|
|
|
|
static void jbuf_free(struct JitterBuffer *q);
|
2018-06-24 01:16:28 +08:00
|
|
|
static int jbuf_write(const Logger *log, struct JitterBuffer *q, struct RTPMessage *m);
|
2015-10-11 05:54:23 +08:00
|
|
|
static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success);
|
2018-06-24 01:16:28 +08:00
|
|
|
static OpusEncoder *create_audio_encoder(const Logger *log, int32_t bit_rate, int32_t sampling_rate,
|
|
|
|
int32_t channel_count);
|
|
|
|
static bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, int32_t new_br, int32_t new_sr,
|
|
|
|
uint8_t new_ch, int32_t *old_br, int32_t *old_sr, int32_t *old_ch);
|
|
|
|
static bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels);
|
2015-04-21 08:31:12 +08:00
|
|
|
|
|
|
|
|
2015-04-29 07:01:25 +08:00
|
|
|
|
2018-08-27 04:34:26 +08:00
|
|
|
ACSession *ac_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number,
|
2018-08-18 01:22:18 +08:00
|
|
|
toxav_audio_receive_frame_cb *cb, void *cb_data)
|
2015-04-21 08:31:12 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
ACSession *ac = (ACSession *)calloc(sizeof(ACSession), 1);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
if (!ac) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(log, "Allocation failed! Application might misbehave!");
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
if (create_recursive_mutex(ac->queue_mutex) != 0) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(log, "Failed to create recursive mutex!");
|
2015-04-21 08:31:12 +08:00
|
|
|
free(ac);
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
int status;
|
2018-01-20 02:43:28 +08:00
|
|
|
ac->decoder = opus_decoder_create(AUDIO_DECODER_START_SAMPLE_RATE, AUDIO_DECODER_START_CHANNEL_COUNT, &status);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
if (status != OPUS_OK) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(log, "Error while starting audio decoder: %s", opus_strerror(status));
|
2015-04-21 08:31:12 +08:00
|
|
|
goto BASE_CLEANUP;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-07-08 22:19:07 +08:00
|
|
|
ac->j_buf = jbuf_new(AUDIO_JITTERBUFFER_COUNT);
|
|
|
|
|
|
|
|
if (ac->j_buf == nullptr) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(log, "Jitter buffer creaton failed!");
|
2015-04-21 08:31:12 +08:00
|
|
|
opus_decoder_destroy(ac->decoder);
|
|
|
|
goto BASE_CLEANUP;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-18 01:22:18 +08:00
|
|
|
ac->mono_time = mono_time;
|
2016-08-19 20:07:45 +08:00
|
|
|
ac->log = log;
|
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
/* Initialize encoders with default values */
|
2018-01-20 02:43:28 +08:00
|
|
|
ac->encoder = create_audio_encoder(log, AUDIO_START_BITRATE, AUDIO_START_SAMPLE_RATE, AUDIO_START_CHANNEL_COUNT);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (ac->encoder == nullptr) {
|
2015-04-21 08:31:12 +08:00
|
|
|
goto DECODER_CLEANUP;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2018-01-20 02:43:28 +08:00
|
|
|
ac->le_bit_rate = AUDIO_START_BITRATE;
|
|
|
|
ac->le_sample_rate = AUDIO_START_SAMPLE_RATE;
|
|
|
|
ac->le_channel_count = AUDIO_START_CHANNEL_COUNT;
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2018-01-20 02:43:28 +08:00
|
|
|
ac->ld_channel_count = AUDIO_DECODER_START_CHANNEL_COUNT;
|
|
|
|
ac->ld_sample_rate = AUDIO_DECODER_START_SAMPLE_RATE;
|
2015-10-11 05:54:23 +08:00
|
|
|
ac->ldrts = 0; /* Make it possible to reconfigure straight away */
|
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
/* These need to be set in order to properly
|
|
|
|
* do error correction with opus */
|
2018-01-20 02:43:28 +08:00
|
|
|
ac->lp_frame_duration = AUDIO_MAX_FRAME_DURATION_MS;
|
|
|
|
ac->lp_sampling_rate = AUDIO_DECODER_START_SAMPLE_RATE;
|
|
|
|
ac->lp_channel_count = AUDIO_DECODER_START_CHANNEL_COUNT;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
ac->av = av;
|
2015-04-29 07:01:25 +08:00
|
|
|
ac->friend_number = friend_number;
|
2018-08-12 18:55:20 +08:00
|
|
|
ac->acb = cb;
|
|
|
|
ac->acb_user_data = cb_data;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
return ac;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
DECODER_CLEANUP:
|
|
|
|
opus_decoder_destroy(ac->decoder);
|
2016-09-18 08:31:55 +08:00
|
|
|
jbuf_free((struct JitterBuffer *)ac->j_buf);
|
2015-04-21 08:31:12 +08:00
|
|
|
BASE_CLEANUP:
|
|
|
|
pthread_mutex_destroy(ac->queue_mutex);
|
|
|
|
free(ac);
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2018-01-20 02:43:28 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
void ac_kill(ACSession *ac)
|
2015-04-21 08:31:12 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!ac) {
|
2015-04-21 08:31:12 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
opus_encoder_destroy(ac->encoder);
|
|
|
|
opus_decoder_destroy(ac->decoder);
|
2016-09-18 08:31:55 +08:00
|
|
|
jbuf_free((struct JitterBuffer *)ac->j_buf);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
pthread_mutex_destroy(ac->queue_mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-02-23 10:22:38 +08:00
|
|
|
LOGGER_DEBUG(ac->log, "Terminated audio handler: %p", (void *)ac);
|
2015-04-21 08:31:12 +08:00
|
|
|
free(ac);
|
|
|
|
}
|
2018-01-20 02:43:28 +08:00
|
|
|
|
2016-09-20 04:49:04 +08:00
|
|
|
void ac_iterate(ACSession *ac)
|
2015-04-21 08:31:12 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!ac) {
|
2015-04-21 08:31:12 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-12 23:48:35 +08:00
|
|
|
/* TODO(mannol): fix this and jitter buffering */
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
/* Enough space for the maximum frame size (120 ms 48 KHz stereo audio) */
|
2018-01-20 02:43:28 +08:00
|
|
|
int16_t temp_audio_buffer[AUDIO_MAX_BUFFER_SIZE_PCM16 * AUDIO_MAX_CHANNEL_COUNT];
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
pthread_mutex_lock(ac->queue_mutex);
|
2018-07-08 22:19:07 +08:00
|
|
|
struct JitterBuffer *const j_buf = (struct JitterBuffer *)ac->j_buf;
|
|
|
|
|
|
|
|
int rc = 0;
|
|
|
|
struct RTPMessage *msg = jbuf_read(j_buf, &rc);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-07-08 22:19:07 +08:00
|
|
|
for (; msg != nullptr || rc == 2; msg = jbuf_read(j_buf, &rc)) {
|
2015-04-21 08:31:12 +08:00
|
|
|
pthread_mutex_unlock(ac->queue_mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
if (rc == 2) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_DEBUG(ac->log, "OPUS correction");
|
2015-10-11 05:54:23 +08:00
|
|
|
int fs = (ac->lp_sampling_rate * ac->lp_frame_duration) / 1000;
|
2018-01-29 05:30:39 +08:00
|
|
|
rc = opus_decode(ac->decoder, nullptr, 0, temp_audio_buffer, fs, 1);
|
2015-04-21 08:31:12 +08:00
|
|
|
} else {
|
|
|
|
/* Get values from packet and decode. */
|
2016-08-19 20:07:45 +08:00
|
|
|
/* NOTE: This didn't work very well */
|
|
|
|
#if 0
|
2015-04-21 08:31:12 +08:00
|
|
|
rc = convert_bw_to_sampling_rate(opus_packet_get_bandwidth(msg->data));
|
2016-08-19 20:07:45 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
if (rc != -1) {
|
|
|
|
cs->last_packet_sampling_rate = rc;
|
|
|
|
} else {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(ac->log, "Failed to load packet values!");
|
2015-04-26 06:31:03 +08:00
|
|
|
rtp_free_msg(msg);
|
2015-04-21 08:31:12 +08:00
|
|
|
continue;
|
2016-08-19 20:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
/* Pick up sampling rate from packet */
|
2015-10-11 05:54:23 +08:00
|
|
|
memcpy(&ac->lp_sampling_rate, msg->data, 4);
|
2017-01-07 16:28:53 +08:00
|
|
|
ac->lp_sampling_rate = net_ntohl(ac->lp_sampling_rate);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
ac->lp_channel_count = opus_packet_get_nb_channels(msg->data + 4);
|
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
/** NOTE: even though OPUS supports decoding mono frames with stereo decoder and vice versa,
|
|
|
|
* it didn't work quite well.
|
|
|
|
*/
|
2015-10-11 05:54:23 +08:00
|
|
|
if (!reconfigure_audio_decoder(ac, ac->lp_sampling_rate, ac->lp_channel_count)) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(ac->log, "Failed to reconfigure decoder!");
|
2015-10-11 05:54:23 +08:00
|
|
|
free(msg);
|
2015-04-21 08:31:12 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-20 04:55:27 +08:00
|
|
|
/*
|
|
|
|
* frame_size = opus_decode(dec, packet, len, decoded, max_size, 0);
|
|
|
|
* where
|
|
|
|
* packet is the byte array containing the compressed data
|
|
|
|
* len is the exact number of bytes contained in the packet
|
|
|
|
* decoded is the decoded audio data in opus_int16 (or float for opus_decode_float())
|
|
|
|
* max_size is the max duration of the frame in samples (per channel) that can fit
|
|
|
|
* into the decoded_frame array
|
|
|
|
*/
|
2018-01-20 02:43:28 +08:00
|
|
|
rc = opus_decode(ac->decoder, msg->data + 4, msg->len - 4, temp_audio_buffer, 5760, 0);
|
2015-10-11 05:54:23 +08:00
|
|
|
free(msg);
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
if (rc < 0) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(ac->log, "Decoding error: %s", opus_strerror(rc));
|
2018-08-12 18:55:20 +08:00
|
|
|
} else if (ac->acb) {
|
2015-10-11 05:54:23 +08:00
|
|
|
ac->lp_frame_duration = (rc * 1000) / ac->lp_sampling_rate;
|
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
ac->acb(ac->av, ac->friend_number, temp_audio_buffer, rc, ac->lp_channel_count,
|
|
|
|
ac->lp_sampling_rate, ac->acb_user_data);
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
pthread_mutex_unlock(ac->queue_mutex);
|
|
|
|
}
|
2018-01-20 02:43:28 +08:00
|
|
|
|
2018-08-27 04:34:26 +08:00
|
|
|
int ac_queue_message(Mono_Time *mono_time, void *acp, struct RTPMessage *msg)
|
2015-04-22 08:09:37 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!acp || !msg) {
|
2015-04-22 08:09:37 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
ACSession *ac = (ACSession *)acp;
|
2016-08-19 20:07:45 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
if ((msg->header.pt & 0x7f) == (RTP_TYPE_AUDIO + 2) % 128) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(ac->log, "Got dummy!");
|
2015-10-11 05:54:23 +08:00
|
|
|
free(msg);
|
2015-04-22 08:09:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
if ((msg->header.pt & 0x7f) != RTP_TYPE_AUDIO % 128) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(ac->log, "Invalid payload type!");
|
2015-10-11 05:54:23 +08:00
|
|
|
free(msg);
|
2015-04-22 08:09:37 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(ac->queue_mutex);
|
2016-09-18 08:31:55 +08:00
|
|
|
int rc = jbuf_write(ac->log, (struct JitterBuffer *)ac->j_buf, msg);
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(ac->queue_mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
if (rc == -1) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(ac->log, "Could not queue the message!");
|
2015-10-11 05:54:23 +08:00
|
|
|
free(msg);
|
2015-04-22 08:09:37 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-01-20 02:43:28 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
int ac_reconfigure_encoder(ACSession *ac, int32_t bit_rate, int32_t sampling_rate, uint8_t channels)
|
2015-04-21 08:31:12 +08:00
|
|
|
{
|
2016-08-19 20:07:45 +08:00
|
|
|
if (!ac || !reconfigure_audio_encoder(ac->log, &ac->encoder, bit_rate,
|
2015-10-11 05:54:23 +08:00
|
|
|
sampling_rate, channels,
|
2015-11-04 02:42:05 +08:00
|
|
|
&ac->le_bit_rate,
|
2015-10-11 05:54:23 +08:00
|
|
|
&ac->le_sample_rate,
|
2016-09-01 02:12:19 +08:00
|
|
|
&ac->le_channel_count)) {
|
2015-04-26 06:31:03 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct JitterBuffer {
|
2015-10-11 05:54:23 +08:00
|
|
|
struct RTPMessage **queue;
|
|
|
|
uint32_t size;
|
|
|
|
uint32_t capacity;
|
|
|
|
uint16_t bottom;
|
|
|
|
uint16_t top;
|
2015-04-21 08:31:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct JitterBuffer *jbuf_new(uint32_t capacity)
|
|
|
|
{
|
|
|
|
unsigned int size = 1;
|
|
|
|
|
|
|
|
while (size <= (capacity * 4)) {
|
|
|
|
size *= 2;
|
|
|
|
}
|
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
struct JitterBuffer *q = (struct JitterBuffer *)calloc(sizeof(struct JitterBuffer), 1);
|
2015-04-21 08:31:12 +08:00
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
if (!q) {
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-04-21 08:31:12 +08:00
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
q->queue = (struct RTPMessage **)calloc(sizeof(struct RTPMessage *), size);
|
|
|
|
|
|
|
|
if (!q->queue) {
|
2015-04-21 08:31:12 +08:00
|
|
|
free(q);
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
q->size = size;
|
|
|
|
q->capacity = capacity;
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
static void jbuf_clear(struct JitterBuffer *q)
|
|
|
|
{
|
|
|
|
for (; q->bottom != q->top; ++q->bottom) {
|
|
|
|
if (q->queue[q->bottom % q->size]) {
|
2015-10-11 05:54:23 +08:00
|
|
|
free(q->queue[q->bottom % q->size]);
|
2018-01-29 05:30:39 +08:00
|
|
|
q->queue[q->bottom % q->size] = nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void jbuf_free(struct JitterBuffer *q)
|
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!q) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-21 08:31:12 +08:00
|
|
|
|
|
|
|
jbuf_clear(q);
|
|
|
|
free(q->queue);
|
|
|
|
free(q);
|
|
|
|
}
|
2018-06-24 01:16:28 +08:00
|
|
|
static int jbuf_write(const Logger *log, struct JitterBuffer *q, struct RTPMessage *m)
|
2015-04-21 08:31:12 +08:00
|
|
|
{
|
2015-10-11 05:54:23 +08:00
|
|
|
uint16_t sequnum = m->header.sequnum;
|
2015-04-21 08:31:12 +08:00
|
|
|
|
|
|
|
unsigned int num = sequnum % q->size;
|
|
|
|
|
|
|
|
if ((uint32_t)(sequnum - q->bottom) > q->size) {
|
2018-02-23 10:22:38 +08:00
|
|
|
LOGGER_DEBUG(log, "Clearing filled jitter buffer: %p", (void *)q);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
jbuf_clear(q);
|
|
|
|
q->bottom = sequnum - q->capacity;
|
|
|
|
q->queue[num] = m;
|
|
|
|
q->top = sequnum + 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (q->queue[num]) {
|
2015-04-21 08:31:12 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-04-21 08:31:12 +08:00
|
|
|
|
|
|
|
q->queue[num] = m;
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if ((sequnum - q->bottom) >= (q->top - q->bottom)) {
|
2015-04-21 08:31:12 +08:00
|
|
|
q->top = sequnum + 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-04-21 08:31:12 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success)
|
2015-04-21 08:31:12 +08:00
|
|
|
{
|
|
|
|
if (q->top == q->bottom) {
|
|
|
|
*success = 0;
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int num = q->bottom % q->size;
|
|
|
|
|
|
|
|
if (q->queue[num]) {
|
2015-10-11 05:54:23 +08:00
|
|
|
struct RTPMessage *ret = q->queue[num];
|
2018-01-29 05:30:39 +08:00
|
|
|
q->queue[num] = nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
++q->bottom;
|
|
|
|
*success = 1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((uint32_t)(q->top - q->bottom) > q->capacity) {
|
|
|
|
++q->bottom;
|
|
|
|
*success = 2;
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*success = 0;
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2018-06-24 01:16:28 +08:00
|
|
|
OpusEncoder *create_audio_encoder(const Logger *log, int32_t bit_rate, int32_t sampling_rate, int32_t channel_count)
|
2015-04-21 08:31:12 +08:00
|
|
|
{
|
|
|
|
int status = OPUS_OK;
|
2018-01-20 04:55:27 +08:00
|
|
|
/*
|
|
|
|
* OPUS_APPLICATION_VOIP Process signal for improved speech intelligibility
|
|
|
|
* OPUS_APPLICATION_AUDIO Favor faithfulness to the original input
|
|
|
|
* OPUS_APPLICATION_RESTRICTED_LOWDELAY Configure the minimum possible coding delay
|
|
|
|
*/
|
2015-10-11 05:54:23 +08:00
|
|
|
OpusEncoder *rc = opus_encoder_create(sampling_rate, channel_count, OPUS_APPLICATION_VOIP, &status);
|
|
|
|
|
|
|
|
if (status != OPUS_OK) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(log, "Error while starting audio encoder: %s", opus_strerror(status));
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-20 04:55:27 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Rates from 500 to 512000 bits per second are meaningful as well as the special
|
|
|
|
* values OPUS_BITRATE_AUTO and OPUS_BITRATE_MAX. The value OPUS_BITRATE_MAX can
|
|
|
|
* be used to cause the codec to use as much rate as it can, which is useful for
|
|
|
|
* controlling the rate by adjusting the output buffer size.
|
|
|
|
*
|
|
|
|
* Parameters:
|
2020-03-14 10:22:39 +08:00
|
|
|
* `[in]` `x` `opus_int32`: bitrate in bits per second.
|
2018-01-20 04:55:27 +08:00
|
|
|
*/
|
2015-04-29 07:01:25 +08:00
|
|
|
status = opus_encoder_ctl(rc, OPUS_SET_BITRATE(bit_rate));
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
if (status != OPUS_OK) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status));
|
2015-04-21 08:31:12 +08:00
|
|
|
goto FAILURE;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-20 04:55:27 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Configures the encoder's use of inband forward error correction.
|
|
|
|
* Note:
|
|
|
|
* This is only applicable to the LPC layer
|
|
|
|
* Parameters:
|
2020-03-14 10:22:39 +08:00
|
|
|
* `[in]` `x` `int`: FEC flag, 0 (disabled) is default
|
2018-01-20 04:55:27 +08:00
|
|
|
*/
|
2015-08-09 00:01:36 +08:00
|
|
|
/* Enable in-band forward error correction in codec */
|
|
|
|
status = opus_encoder_ctl(rc, OPUS_SET_INBAND_FEC(1));
|
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
if (status != OPUS_OK) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status));
|
2015-08-09 00:01:36 +08:00
|
|
|
goto FAILURE;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-20 04:55:27 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Configures the encoder's expected packet loss percentage.
|
|
|
|
* Higher values with trigger progressively more loss resistant behavior in
|
|
|
|
* the encoder at the expense of quality at a given bitrate in the lossless case,
|
|
|
|
* but greater quality under loss.
|
|
|
|
* Parameters:
|
2020-03-14 10:22:39 +08:00
|
|
|
* `[in]` `x` `int`: Loss percentage in the range 0-100, inclusive.
|
2018-01-20 04:55:27 +08:00
|
|
|
*/
|
2015-08-09 00:01:36 +08:00
|
|
|
/* Make codec resistant to up to 10% packet loss
|
|
|
|
* NOTE This could also be adjusted on the fly, rather than hard-coded,
|
|
|
|
* with feedback from the receiving client.
|
|
|
|
*/
|
2018-01-20 02:43:28 +08:00
|
|
|
status = opus_encoder_ctl(rc, OPUS_SET_PACKET_LOSS_PERC(AUDIO_OPUS_PACKET_LOSS_PERC));
|
2015-08-09 00:01:36 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
if (status != OPUS_OK) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status));
|
2015-08-09 00:01:36 +08:00
|
|
|
goto FAILURE;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-20 04:55:27 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Configures the encoder's computational complexity.
|
|
|
|
*
|
|
|
|
* The supported range is 0-10 inclusive with 10 representing the highest complexity.
|
|
|
|
* The default value is 10.
|
|
|
|
*
|
|
|
|
* Parameters:
|
2020-03-14 10:22:39 +08:00
|
|
|
* `[in]` `x` `int`: 0-10, inclusive
|
2018-01-20 04:55:27 +08:00
|
|
|
*/
|
2015-08-09 00:01:36 +08:00
|
|
|
/* Set algorithm to the highest complexity, maximizing compression */
|
2018-01-20 02:43:28 +08:00
|
|
|
status = opus_encoder_ctl(rc, OPUS_SET_COMPLEXITY(AUDIO_OPUS_COMPLEXITY));
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
if (status != OPUS_OK) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status));
|
2015-04-21 08:31:12 +08:00
|
|
|
goto FAILURE;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
return rc;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
FAILURE:
|
|
|
|
opus_encoder_destroy(rc);
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2018-01-20 02:43:28 +08:00
|
|
|
|
2018-06-24 01:16:28 +08:00
|
|
|
bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, int32_t new_br, int32_t new_sr, uint8_t new_ch,
|
2015-10-11 05:54:23 +08:00
|
|
|
int32_t *old_br, int32_t *old_sr, int32_t *old_ch)
|
2015-04-26 06:31:03 +08:00
|
|
|
{
|
|
|
|
/* Values are checked in toxav.c */
|
|
|
|
if (*old_sr != new_sr || *old_ch != new_ch) {
|
2016-08-19 20:07:45 +08:00
|
|
|
OpusEncoder *new_encoder = create_audio_encoder(log, new_br, new_sr, new_ch);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (new_encoder == nullptr) {
|
2015-04-26 06:31:03 +08:00
|
|
|
return false;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-26 06:31:03 +08:00
|
|
|
opus_encoder_destroy(*e);
|
|
|
|
*e = new_encoder;
|
2016-09-01 02:12:19 +08:00
|
|
|
} else if (*old_br == new_br) {
|
2015-04-26 06:31:03 +08:00
|
|
|
return true; /* Nothing changed */
|
2016-09-01 03:40:20 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 03:40:20 +08:00
|
|
|
int status = opus_encoder_ctl(*e, OPUS_SET_BITRATE(new_br));
|
|
|
|
|
|
|
|
if (status != OPUS_OK) {
|
|
|
|
LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status));
|
|
|
|
return false;
|
2015-04-26 06:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*old_br = new_br;
|
|
|
|
*old_sr = new_sr;
|
|
|
|
*old_ch = new_ch;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_DEBUG(log, "Reconfigured audio encoder br: %d sr: %d cc:%d", new_br, new_sr, new_ch);
|
2015-04-26 06:31:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-01-20 02:43:28 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels)
|
2015-04-21 08:31:12 +08:00
|
|
|
{
|
2015-10-11 05:54:23 +08:00
|
|
|
if (sampling_rate != ac->ld_sample_rate || channels != ac->ld_channel_count) {
|
2018-08-18 01:22:18 +08:00
|
|
|
if (current_time_monotonic(ac->mono_time) - ac->ldrts < 500) {
|
2015-04-21 08:31:12 +08:00
|
|
|
return false;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
int status;
|
2015-10-11 05:54:23 +08:00
|
|
|
OpusDecoder *new_dec = opus_decoder_create(sampling_rate, channels, &status);
|
|
|
|
|
|
|
|
if (status != OPUS_OK) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(ac->log, "Error while starting audio decoder(%d %d): %s", sampling_rate, channels, opus_strerror(status));
|
2015-04-21 08:31:12 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
ac->ld_sample_rate = sampling_rate;
|
|
|
|
ac->ld_channel_count = channels;
|
2018-08-18 01:22:18 +08:00
|
|
|
ac->ldrts = current_time_monotonic(ac->mono_time);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
opus_decoder_destroy(ac->decoder);
|
|
|
|
ac->decoder = new_dec;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_DEBUG(ac->log, "Reconfigured audio decoder sr: %d cc: %d", sampling_rate, channels);
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
return true;
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|