Skip to content

Commit

Permalink
webapp: auto detect channel
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Mar 19, 2021
1 parent a07273f commit 6a0df9e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/modules/slaudio/slaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ static int slaudio_init(void)
sess->ch = cnt * 2 + 1;
sess->track = cnt + 1;
sess->call = NULL;
sess->jb_max = 0;
sess->jitter.max = 0;
sess->run_src = false;
sess->run_play = false;

Expand All @@ -1541,7 +1541,7 @@ static int slaudio_init(void)
sess->local = false;
sess->stream = true;
sess->call = NULL;
sess->jb_max = 0;
sess->jitter.max = 0;
sess->ch = MAX_REMOTE_CHANNELS * 2 + 1;
list_append(&sessionl, &sess->le, sess);

Expand Down
74 changes: 58 additions & 16 deletions src/modules/webapp/jitter.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


#define STIME 192 /* 192 = 48 Samples * 2ch * 2 Bytes = 1ms */
#define STARTUP_COUNT 200

static int16_t *dummy[1920];

Expand Down Expand Up @@ -63,56 +64,97 @@ static void stereo_mono_ch_select(int16_t *buf, size_t sampc, enum sess_chmix ch
}


void webapp_jitter_reset(struct session *sess)
{
sess->jitter.startup = 0;
sess->jitter.max = 0;
sess->jitter.max_l = 0;
sess->jitter.max_r = 0;
sess->chmix = CH_STEREO;
}


void webapp_jitter(struct session *sess, int16_t *sampv,
auplay_write_h *wh, unsigned int sampc, void *arg)
{
int16_t max = 0;
struct aurx *rx = arg;
size_t bufsz = 0;
size_t frames = sampc / 2;

int16_t max_l = 0, max_r = 0, max = 0;
size_t bufsz = 0, pos = 0;

/* set threshold to 500ms */
int16_t silence_threshold = 48000/sampc;

bufsz = aubuf_cur_size(rx->aubuf);

if (bufsz <= 7680 && !sess->talk) { /* >=40ms (1920*2*2) */
if (bufsz <= 7680 &&
(!sess->jitter.talk ||
sess->jitter.startup < STARTUP_COUNT)) { /* >=40ms (1920*2*2) */
debug("webapp_jitter: increase latency %dms\n",
bufsz/STIME);
memset(sampv, 0, sampc * sizeof(int16_t));
return;
}

sess->bufsz = bufsz/STIME;
sess->jitter.bufsz = bufsz/STIME;

wh(sampv, sampc, arg);

if (sess->jitter.startup == STARTUP_COUNT) {
if (sess->jitter.max_l > 400 && sess->jitter.max_r < 400) {
sess->chmix = CH_MONO_L;
sess->changed = true;
}
if (sess->jitter.max_r > 400 && sess->jitter.max_l < 400) {
sess->chmix = CH_MONO_R;
sess->changed = true;
}
sess->jitter.startup = STARTUP_COUNT+1;
}

/* Mono chmix */
if (sess->chmix != CH_STEREO) {
stereo_mono_ch_select(sampv, sampc, sess->chmix);
}

/* Detect Talk spurt */
for (uint16_t pos = 0; pos < sampc; pos++)
/* Detect Talk spurt and channel usage */
for (uint16_t frame = 0; frame < frames; frame++)
{
if (sampv[pos] > max)
max = sampv[pos];
if (sampv[pos] > max_l)
max_l = sampv[pos];
if (sampv[pos+1] > max_r)
max_r = sampv[pos+1];
pos += 2;
}
sess->jb_max = (sess->jb_max + max) / 2;

if (sess->jb_max < 400) {
if (sess->silence_count < silence_threshold) {
sess->silence_count++;
if (max_l > max_r)
max = max_l;
else
max = max_r;

sess->jitter.max = (sess->jitter.max + max) / 2;
if (max_l > sess->jitter.max_l)
sess->jitter.max_l = max_l;
if (max_r > sess->jitter.max_r)
sess->jitter.max_r = max_r;
if (sess->jitter.startup <= STARTUP_COUNT)
sess->jitter.startup++;

if (sess->jitter.max < 400) {
if (sess->jitter.silence_count < silence_threshold) {
sess->jitter.silence_count++;
}
else {
sess->talk = false;
sess->jitter.talk = false;
}
}
else {
sess->talk = true;
sess->silence_count = 0;
sess->jitter.talk = true;
sess->jitter.silence_count = 0;
}

if (sess->talk)
if (sess->jitter.talk)
return;

bufsz = aubuf_cur_size(rx->aubuf);
Expand Down
12 changes: 10 additions & 2 deletions src/modules/webapp/sessions.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ int8_t webapp_call_update(struct call *call, char *state)

if (new && !sess->call) {
sess->call = call;
webapp_jitter_reset(sess);
}

if (sess->call != call)
Expand Down Expand Up @@ -258,11 +259,18 @@ static void jitter_buffer(void *arg)
if (sess->local || sess->stream)
continue;

re_snprintf(bufsz, sizeof(bufsz), "%d ", sess->bufsz);
re_snprintf(talk, sizeof(talk), "%d ", sess->talk);
re_snprintf(bufsz, sizeof(bufsz), "%d ", sess->jitter.bufsz);
re_snprintf(talk, sizeof(talk), "%d ", sess->jitter.talk);
strcat((char*)buffers, bufsz);
strcat((char*)talks, talk);

if (sess->changed) {
session_to_webapp_calls(sess);
sess->changed = false;
ws_send_json(WS_CALLS, webapp_calls);
}
}

n = strlen(buffers);
buffers[n-1] = '\0'; /* remove trailing space */

Expand Down
17 changes: 13 additions & 4 deletions src/modules/webapp/webapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ enum sess_chmix {
CH_MONO_R
};

struct sess_jitter {
bool talk;
int16_t bufsz;
int16_t max;
int16_t max_l;
int16_t max_r;
int16_t startup;
int16_t silence_count;
};

struct session {
struct le le;
struct ausrc_st *st_src;
Expand All @@ -63,12 +73,10 @@ struct session {
bool stream; /* only used for standalone */
bool local; /* only used for standalone */
int8_t track;
bool talk;
int16_t bufsz;
int16_t jb_max;
int16_t silence_count;
struct sess_jitter jitter;
enum sess_chmix chmix;
char *state;
bool changed;
#ifdef SLPLUGIN
bool primary;
bool run_auto_mix;
Expand Down Expand Up @@ -195,5 +203,6 @@ void slaudio_set_output(int value);
/*
* jitter.c
*/
void webapp_jitter_reset(struct session *sess);
void webapp_jitter(struct session *sess, int16_t *sampv,
auplay_write_h *wh, unsigned int sampc, void *arg);

0 comments on commit 6a0df9e

Please sign in to comment.