Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract named constants from magic numbers in toxav/audio.c. #698

Merged
merged 1 commit into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions toxav/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ ACSession *ac_new(Logger *log, ToxAV *av, uint32_t friend_number, toxav_audio_re
}

int status;
ac->decoder = opus_decoder_create(48000, 2, &status);
ac->decoder = opus_decoder_create(AUDIO_DECODER_START_SAMPLE_RATE, AUDIO_DECODER_START_CHANNEL_COUNT, &status);

if (status != OPUS_OK) {
LOGGER_ERROR(log, "Error while starting audio decoder: %s", opus_strerror(status));
goto BASE_CLEANUP;
}

if (!(ac->j_buf = jbuf_new(3))) {
if (!(ac->j_buf = jbuf_new(AUDIO_JITTERBUFFER_COUNT))) {
LOGGER_WARNING(log, "Jitter buffer creaton failed!");
opus_decoder_destroy(ac->decoder);
goto BASE_CLEANUP;
Expand All @@ -73,25 +73,25 @@ ACSession *ac_new(Logger *log, ToxAV *av, uint32_t friend_number, toxav_audio_re
ac->log = log;

/* Initialize encoders with default values */
ac->encoder = create_audio_encoder(log, 48000, 48000, 2);
ac->encoder = create_audio_encoder(log, AUDIO_START_BITRATE, AUDIO_START_SAMPLE_RATE, AUDIO_START_CHANNEL_COUNT);

if (ac->encoder == NULL) {
goto DECODER_CLEANUP;
}

ac->le_bit_rate = 48000;
ac->le_sample_rate = 48000;
ac->le_channel_count = 2;
ac->le_bit_rate = AUDIO_START_BITRATE;
ac->le_sample_rate = AUDIO_START_SAMPLE_RATE;
ac->le_channel_count = AUDIO_START_CHANNEL_COUNT;

ac->ld_channel_count = 2;
ac->ld_sample_rate = 48000;
ac->ld_channel_count = AUDIO_DECODER_START_CHANNEL_COUNT;
ac->ld_sample_rate = AUDIO_DECODER_START_SAMPLE_RATE;
ac->ldrts = 0; /* Make it possible to reconfigure straight away */

/* These need to be set in order to properly
* do error correction with opus */
ac->lp_frame_duration = 120;
ac->lp_sampling_rate = 48000;
ac->lp_channel_count = 1;
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;

ac->av = av;
ac->friend_number = friend_number;
Expand All @@ -108,6 +108,7 @@ ACSession *ac_new(Logger *log, ToxAV *av, uint32_t friend_number, toxav_audio_re
free(ac);
return NULL;
}

void ac_kill(ACSession *ac)
{
if (!ac) {
Expand All @@ -123,6 +124,7 @@ void ac_kill(ACSession *ac)
LOGGER_DEBUG(ac->log, "Terminated audio handler: %p", ac);
free(ac);
}

void ac_iterate(ACSession *ac)
{
if (!ac) {
Expand All @@ -132,7 +134,7 @@ void ac_iterate(ACSession *ac)
/* TODO(mannol): fix this and jitter buffering */

/* Enough space for the maximum frame size (120 ms 48 KHz stereo audio) */
int16_t tmp[5760 * 2];
int16_t temp_audio_buffer[AUDIO_MAX_BUFFER_SIZE_PCM16 * AUDIO_MAX_CHANNEL_COUNT];

struct RTPMessage *msg;
int rc = 0;
Expand All @@ -145,7 +147,7 @@ void ac_iterate(ACSession *ac)
if (rc == 2) {
LOGGER_DEBUG(ac->log, "OPUS correction");
int fs = (ac->lp_sampling_rate * ac->lp_frame_duration) / 1000;
rc = opus_decode(ac->decoder, NULL, 0, tmp, fs, 1);
rc = opus_decode(ac->decoder, NULL, 0, temp_audio_buffer, fs, 1);
} else {
/* Get values from packet and decode. */
/* NOTE: This didn't work very well */
Expand Down Expand Up @@ -178,7 +180,7 @@ void ac_iterate(ACSession *ac)
continue;
}

rc = opus_decode(ac->decoder, msg->data + 4, msg->len - 4, tmp, 5760, 0);
rc = opus_decode(ac->decoder, msg->data + 4, msg->len - 4, temp_audio_buffer, 5760, 0);
free(msg);
}

Expand All @@ -187,7 +189,7 @@ void ac_iterate(ACSession *ac)
} else if (ac->acb.first) {
ac->lp_frame_duration = (rc * 1000) / ac->lp_sampling_rate;

ac->acb.first(ac->av, ac->friend_number, tmp, rc, ac->lp_channel_count,
ac->acb.first(ac->av, ac->friend_number, temp_audio_buffer, rc, ac->lp_channel_count,
ac->lp_sampling_rate, ac->acb.second);
}

Expand All @@ -196,6 +198,7 @@ void ac_iterate(ACSession *ac)

pthread_mutex_unlock(ac->queue_mutex);
}

int ac_queue_message(void *acp, struct RTPMessage *msg)
{
if (!acp || !msg) {
Expand Down Expand Up @@ -228,6 +231,7 @@ int ac_queue_message(void *acp, struct RTPMessage *msg)

return 0;
}

int ac_reconfigure_encoder(ACSession *ac, int32_t bit_rate, int32_t sampling_rate, uint8_t channels)
{
if (!ac || !reconfigure_audio_encoder(ac->log, &ac->encoder, bit_rate,
Expand Down Expand Up @@ -378,15 +382,15 @@ OpusEncoder *create_audio_encoder(Logger *log, int32_t bit_rate, int32_t samplin
* NOTE This could also be adjusted on the fly, rather than hard-coded,
* with feedback from the receiving client.
*/
status = opus_encoder_ctl(rc, OPUS_SET_PACKET_LOSS_PERC(10));
status = opus_encoder_ctl(rc, OPUS_SET_PACKET_LOSS_PERC(AUDIO_OPUS_PACKET_LOSS_PERC));

if (status != OPUS_OK) {
LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status));
goto FAILURE;
}

/* Set algorithm to the highest complexity, maximizing compression */
status = opus_encoder_ctl(rc, OPUS_SET_COMPLEXITY(10));
status = opus_encoder_ctl(rc, OPUS_SET_COMPLEXITY(AUDIO_OPUS_COMPLEXITY));

if (status != OPUS_OK) {
LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status));
Expand All @@ -399,6 +403,7 @@ OpusEncoder *create_audio_encoder(Logger *log, int32_t bit_rate, int32_t samplin
opus_encoder_destroy(rc);
return NULL;
}

bool reconfigure_audio_encoder(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)
{
Expand Down Expand Up @@ -430,6 +435,7 @@ bool reconfigure_audio_encoder(Logger *log, OpusEncoder **e, int32_t new_br, int
LOGGER_DEBUG(log, "Reconfigured audio encoder br: %d sr: %d cc:%d", new_br, new_sr, new_ch);
return true;
}

bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels)
{
if (sampling_rate != ac->ld_sample_rate || channels != ac->ld_channel_count) {
Expand Down
20 changes: 20 additions & 0 deletions toxav/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@
#include <opus.h>
#include <pthread.h>

#define AUDIO_JITTERBUFFER_COUNT 3
#define AUDIO_MAX_SAMPLE_RATE 48000
#define AUDIO_MAX_CHANNEL_COUNT 2

#define AUDIO_START_SAMPLE_RATE 48000
#define AUDIO_START_BITRATE 48000
#define AUDIO_START_CHANNEL_COUNT 2
#define AUDIO_OPUS_PACKET_LOSS_PERC 10
#define AUDIO_OPUS_COMPLEXITY 10

#define AUDIO_DECODER_START_SAMPLE_RATE 48000
#define AUDIO_DECODER_START_CHANNEL_COUNT 1

#define AUDIO_MAX_FRAME_DURATION_MS 120

// ((sampling_rate_in_hz * frame_duration_in_ms) / 1000) * 2 // because PCM16 needs 2 bytes for 1 sample
// These are per frame and per channel.
#define AUDIO_MAX_BUFFER_SIZE_PCM16 ((AUDIO_MAX_SAMPLE_RATE * AUDIO_MAX_FRAME_DURATION_MS) / 1000)
#define AUDIO_MAX_BUFFER_SIZE_BYTES (AUDIO_MAX_BUFFER_SIZE_PCM16 * 2)

struct RTPMessage;

typedef struct ACSession_s {
Expand Down