Audio rework, new jitter buffer

Jitter buffer is now based on packets, not on samples. The frames are
dejittered in received form. After reading from jitter buffer, they are
decoded in correct order. If a frame is missing, it is concealed by
repeating audio.
This commit is contained in:
Andreas Eversberg 2024-03-09 19:37:33 +01:00
parent 14b2df8907
commit 863ba053ef
6 changed files with 707 additions and 462 deletions

View File

@ -78,7 +78,7 @@ AC_CHECK_LIB([m], [main], [], [echo "Failed to find lib!" ; exit -1])
AC_CHECK_LIB([pthread], [main], [], [echo "Failed to find lib!" ; exit -1])
PKG_CHECK_MODULES(LIBOSMOCORE, libosmocore >= 1.9.0)
PKG_CHECK_MODULES(LIBOSMOCC, libosmocc >= 1.0.0)
PKG_CHECK_MODULES(LIBOSMOCC, libosmocc >= 2.0.0)
AC_ARG_WITH([gsm], [AS_HELP_STRING([--with-gsm], [compile with GSM codec support @<:@default=check@:>@]) ], [], [with_gsm="check"])
AS_IF([test "x$with_gsm" != xno], [AC_CHECK_LIB([gsm], [main], [with_gsm="yes"], [with_gsm="no"])])

View File

@ -21,12 +21,13 @@
*
* Storing:
*
* Each saved frame is sorted into the list of packages by their sequence
* number.
* Each saved frame is sorted into the list of packages by their timestamp.
*
* The first packet will be stored with a delay of minimum jitter window size.
* The first packet will be stored with a timestamp offset of minimum jitter
* window size or half of the target size, depending on the adaptive jitter
* buffer flag.
*
* Packets with the same sequence are dropped.
* Packets with the same timestamp are dropped.
*
* Early packts that exceed maximum jitter window size cause jitter
* window to shift into the future.
@ -35,37 +36,36 @@
* delay). Minimum jitter window size is added also, to prevent subsequent
* packets from beeing late too.
*
* If no sequence is provided (autosequence), the sequence number is generated
* by a counter. Also the timestamp is generated by counting the length of each
* frame.
* If adaptive jitter buffer is used, a delay that exceed the target size
* is reduced to the target size.
*
* If ssrc changes, the buffer is reset.
* If ssrc changes, the buffer is reset, but not locked again.
*
*
* Playout:
* Loading:
*
* The caller of the playout function can request any length of samples from
* the packet list. The packt's time stamp and the jitter window time stamp
* indicate what portion of a packet is already provided to the caller.
* Complete packet, sent to the caller, are removed.
* jitter_offset() will return the number of samples between the jitter buffer's head and the first packet afterwards. Packets that already passed the jitter buffer's head are ignored. If no frame is ahead the jitter buffer's head, a negative value is returned.
*
* Missing packets are interpolated by repeating last 20ms of audio (optional)
* or by inserting zeroes (sample size > 1 byte) or by inserting 0xff (sample
* size = 1). In case of repeating audio, the number of turns are limited until
* buffer is reset to silence, if no frames are received for a certain time.
* jitter_load() will remove and return the frame at the jitter buffer's head. Packet that already passed the jitter buffer's head are deleted. If no frame matches the jitter buffer's head, NULL is returned.
*
* Optionally the constant delay will be measured continuously and lowered if
* greater than minimum window size. (adaptive jitter buffer size)
* jitter_advance() will advance the jitter buffer's head by the given number of samples.
*
* Note that the delay is measured with time stamp of frame, no matter what
* the length is. Length is an extra delay, but not considered here.
* jitter_load_samples() will read decoded samples from jitter buffer's frames.
* This means that that the decoder of each frame must generate samples of equal type and size.
* If there is a gap between jitter buffer's head and the next frame, the samples are taken from the last frame.
* The conceal function is called in this case, to extrapolate the missing samples.
* If no conceal function is given, the last frame is repeated.
* If there is no gap between jitter buffer's head and the next frame, the frame is decoded and the samples are taken from that frame.
* After that the jitter buffer's head is advanced by the number of samples read.
*
* *TBD*
*
*
* Unlocking:
*
* If the buffer is created or reset, the buffer is locked, so no packets are
* stored. When the playout routine is called, the buffer is unlocked. This
* prevents from filling the buffer before playout is performed, which would
* stored. When the loading routine is called, the buffer is unlocked. This
* prevents from filling the buffer before loading is performed, which would
* cause high delay.
*
*/
@ -82,33 +82,19 @@
#define INITIAL_DELAY_INTERVAL 0.5
#define REPEAT_DELAY_INTERVAL 3.0
#define EXTRA_BUFFER 0.020 // 20 ms
#define EXTRA_TIMEOUT 0.500 // maximum time to repeat extrapolation buffer
/* uncomment to enable heavy debugging */
//#define HEAVY_DEBUG
//#define VISUAL_DEBUG
static int unnamed_count = 1;
/* create jitter buffer */
int jitter_create(jitter_t *jb, const char *name, double samplerate, int sample_size, double target_window_duration, double max_window_duration, uint32_t window_flags)
int jitter_create(jitter_t *jb, const char *name, double samplerate, double target_window_duration, double max_window_duration, uint32_t window_flags)
{
int rc = 0;
memset(jb, 0, sizeof(*jb));
jb->sample_duration = 1.0 / samplerate;
jb->sample_size = sample_size;
jb->target_window_size = (int)(samplerate * target_window_duration);
jb->max_window_size = (int)(samplerate * max_window_duration);
jb->window_flags = window_flags;
jb->extra_size = (int)(EXTRA_BUFFER * samplerate);
jb->extra_samples = calloc(sample_size, jb->extra_size);
if (!jb->extra_samples) {
LOGP(DJITTER, LOGL_ERROR, "No memory for frame.\n");
rc = -ENOMEM;
goto error;
}
jb->extra_timeout_max = (int)ceil(EXTRA_TIMEOUT / EXTRA_BUFFER);
memset(jb, 0, sizeof(*jb));
/* optionally give a string to be show with the debug */
if (name && *name)
@ -116,36 +102,37 @@ int jitter_create(jitter_t *jb, const char *name, double samplerate, int sample_
else
snprintf(jb->name, sizeof(jb->name) - 1, "(unnamed %d) ", unnamed_count++);
jb->sample_duration = 1.0 / samplerate;
jb->samples_20ms = samplerate / 50;
jb->target_window_size = (int)ceil(target_window_duration / jb->sample_duration);
jb->max_window_size = (int)ceil(max_window_duration / jb->sample_duration);
jb->window_flags = window_flags;
jitter_reset(jb);
LOGP(DJITTER, LOGL_INFO, "%sCreated jitter buffer. (samplerate=%.0f, target_window=%.0fms, max_window=%.0fms, flag:latency=%s flag:repeat=%s)\n", jb->name, samplerate, target_window_duration * 1000.0, max_window_duration * 1000.0, (window_flags & JITTER_FLAG_LATENCY) ? "true" : "false", (window_flags & JITTER_FLAG_REPEAT) ? "true" : "false");
LOGP(DJITTER, LOGL_INFO, "%s Created jitter buffer. (samperate=%.0f, target_window=%.0fms, max_window=%.0fms, flag:latency=%s flag:repeat=%s)\n",
jb->name,
samplerate,
(double)jb->target_window_size * jb->sample_duration * 1000.0,
(double)jb->max_window_size * jb->sample_duration * 1000.0,
(window_flags & JITTER_FLAG_LATENCY) ? "true" : "false",
(window_flags & JITTER_FLAG_REPEAT) ? "true" : "false");
error:
if (rc)
jitter_destroy(jb);
return rc;
}
static void clear_extra_buffer(jitter_t *jb)
{
if (jb->sample_size == 1)
memset(jb->extra_samples, 0xff, jb->sample_size * jb->extra_size);
else
memset(jb->extra_samples, 0, jb->sample_size * jb->extra_size);
}
/* reset jitter buffer */
void jitter_reset(jitter_t *jb)
{
jitter_frame_t *jf, *temp;
LOGP(DJITTER, LOGL_INFO, "%sReset jitter buffer.\n", jb->name);
LOGP(DJITTER, LOGL_INFO, "%s Reset jitter buffer.\n", jb->name);
/* jitter buffer locked */
jb->unlocked = 0;
jb->unlocked = false;
/* window becomes invalid */
jb->window_valid = 0;
jb->window_valid = false;
/* remove all pending frames */
jf = jb->frame_list;
@ -156,269 +143,410 @@ void jitter_reset(jitter_t *jb)
}
jb->frame_list = NULL;
/* clear extrapolation buffer */
if (jb->extra_samples)
clear_extra_buffer(jb);
jb->extra_index = 0;
jb->extra_timeout_count = jb->extra_timeout_max; /* no data in buffer yet, so we set timeout condition */
/* delay measurement and reduction */
jb->delay_counter = 0.0;
jb->delay_interval = INITIAL_DELAY_INTERVAL;
jb->min_delay_value = -1;
/* remove current sample buffer */
free(jb->spl_buf);
jb->spl_buf = NULL;
jb->spl_valid = false;
}
void jitter_destroy(jitter_t *jb)
{
jitter_reset(jb);
LOGP(DJITTER, LOGL_INFO, "%sDestroying jitter buffer.\n", jb->name);
if (jb->extra_samples) {
free(jb->extra_samples);
jb->extra_samples = NULL;
}
LOGP(DJITTER, LOGL_INFO, "%s Destroying jitter buffer.\n", jb->name);
}
/* store audio in jitterbuffer
*
* stop if buffer is completely filled
*/
void jitter_save(jitter_t *jb, void *samples, int length, int has_sequence, uint16_t sequence, uint32_t timestamp, uint32_t ssrc)
jitter_frame_t *jitter_frame_alloc(void (*decoder)(uint8_t *src_data, int src_len, uint8_t **dst_data, int *dst_len, void *priv), void *decoder_priv, uint8_t *data, int size, uint8_t marker, uint16_t sequence, uint32_t timestamp, uint32_t ssrc)
{
jitter_frame_t *jf, **jfp;
int16_t offset_sequence;
jitter_frame_t *jf;
jf = malloc(sizeof(*jf) + size);
if (!jf) {
LOGP(DJITTER, LOGL_ERROR, "No memory for frame.\n");
return NULL;
}
memset(jf, 0, sizeof(*jf)); // note: clear header only
jf->decoder = decoder;
jf->decoder_priv = decoder_priv;
memcpy(jf->data, data, size);
jf->size = size;
jf->marker = marker;
jf->sequence = sequence;
jf->timestamp = timestamp;
jf->ssrc = ssrc;
return jf;
}
void jitter_frame_free(jitter_frame_t *jf)
{
free(jf);
}
void jitter_frame_get(jitter_frame_t *jf, void (**decoder)(uint8_t *src_data, int src_len, uint8_t **dst_data, int *dst_len, void *priv), void **decoder_priv, uint8_t **data, int *size, uint8_t *marker, uint16_t *sequence, uint32_t *timestamp, uint32_t *ssrc)
{
if (decoder)
*decoder = jf->decoder;
if (decoder_priv)
*decoder_priv = jf->decoder_priv;
if (data)
*data = jf->data;
if (size)
*size = jf->size;
if (marker)
*marker = jf->marker;
if (sequence)
*sequence = jf->sequence;
if (timestamp)
*timestamp = jf->timestamp;
if (ssrc)
*ssrc = jf->ssrc;
}
/* Store frame in jitterbuffer
*
* Use sequence number to order frames.
* Use timestamp to handle delay.
*/
void jitter_save(jitter_t *jb, jitter_frame_t *jf)
{
jitter_frame_t **jfp;
int32_t offset_timestamp;
/* ignore frames until the buffer is unlocked by jitter_load() */
if (!jb->unlocked)
if (!jb->unlocked) {
jitter_frame_free(jf);
return;
/* omit frames with no data */
if (length < 1)
return;
/* generate sequence and timestamp automatically, if enabled */
if (!has_sequence) {
#ifdef DEBUG_JITTER
LOGP(DJITTER, LOGL_DEBUG, "%sSave frame of %d samples (no seqence).\n", jb->name, length);
#endif
sequence = jb->next_sequence;
jb->next_sequence++;
timestamp = jb->next_timestamp;
jb->next_timestamp += length;
ssrc = jb->window_ssrc;
} else {
#ifdef HEAVY_DEBUG
LOGP(DJITTER, LOGL_DEBUG, "%sSave frame of %d samples (seqence=%u timestamp=%u ssrc=0x%02x).\n", jb->name, length, sequence, timestamp, ssrc);
#endif
jb->next_sequence = sequence + 1;
jb->next_timestamp = timestamp + length;
}
/* first packet (with this ssrc) sets window size to target_window_size */
if (!jb->window_valid || jb->window_ssrc != ssrc) {
if (!jb->window_valid || jb->window_ssrc != jf->ssrc) {
if (!jb->window_valid)
LOGP(DJITTER, LOGL_DEBUG, "%s Initial frame after init or reset.\n", jb->name);
else
LOGP(DJITTER, LOGL_DEBUG, "%s SSRC changed.\n", jb->name);
// NOTE: Reset must be called before finding the frame location below, because there will be no frame in list anymore!
jitter_reset(jb);
jb->unlocked = 1;
jb->unlocked = true;
/* when using dynamic jitter buffer, we use half of the target delay */
if ((jb->window_flags & JITTER_FLAG_LATENCY)) {
jb->window_timestamp = timestamp - (uint32_t)jb->target_window_size / 2;
jb->window_timestamp = jf->timestamp - (uint32_t)jb->target_window_size / 2;
} else {
jb->window_timestamp = timestamp - (uint32_t)jb->target_window_size;
jb->window_timestamp = jf->timestamp - (uint32_t)jb->target_window_size;
}
jb->window_valid = 1;
jb->window_ssrc = ssrc;
jb->window_valid = true;
jb->window_ssrc = jf->ssrc;
jb->min_delay = -1;
jb->delay_counter = 0.0;
jb->delay_interval = INITIAL_DELAY_INTERVAL;
}
/* reduce delay */
if (jb->delay_counter >= jb->delay_interval) {
if (jb->min_delay >= 0)
LOGP(DJITTER, LOGL_DEBUG, "%s Statistics: target_window_delay=%.0fms max_window_delay=%.0fms current min_delay=%.0fms\n",
jb->name,
(double)jb->target_window_size * jb->sample_duration * 1000.0,
(double)jb->max_window_size * jb->sample_duration * 1000.0,
(double)jb->min_delay * jb->sample_duration * 1000.0);
/* delay reduction, if minimum delay is greater than target jitter window size */
if ((jb->window_flags & JITTER_FLAG_LATENCY) && jb->min_delay > jb->target_window_size) {
LOGP(DJITTER, LOGL_DEBUG, "%s Reducing current minimum delay of %.0fms, because maximum delay is greater than target window size of %.0fms.\n",
jb->name,
(double)jb->min_delay * jb->sample_duration * 1000.0,
(double)jb->target_window_size * jb->sample_duration * 1000.0);
/* only reduce delay to half of the target window size */
jb->window_timestamp += jb->min_delay - jb->target_window_size / 2;
}
jb->delay_counter -= jb->delay_interval;
jb->delay_interval = REPEAT_DELAY_INTERVAL;
jb->min_delay = -1;
}
/* find location where to put frame into the list, depending on sequence number */
jfp = &jb->frame_list;
while(*jfp) {
offset_sequence = (int16_t)(sequence - (*jfp)->sequence);
offset_timestamp = (int16_t)(jf->timestamp - (*jfp)->timestamp);
/* found double entry */
if (offset_sequence == 0) {
LOGP(DJITTER, LOGL_DEBUG, "%s Dropping double packet (sequence = %d)\n", jb->name, sequence);
if (offset_timestamp == 0) {
LOGP(DJITTER, LOGL_DEBUG, "%s Dropping double packet (timestamp = %u)\n", jb->name, jf->timestamp);
jitter_frame_free(jf);
return;
}
/* offset is negative, so we found the position to insert frame */
if (offset_sequence < 0)
if (offset_timestamp < 0)
break;
jfp = &((*jfp)->next);
}
offset_timestamp = timestamp - jb->window_timestamp;
offset_timestamp = jf->timestamp - jb->window_timestamp;
#ifdef HEAVY_DEBUG
LOGP(DJITTER, LOGL_DEBUG, "%sFrame has offset of %.0fms in jitter buffer.\n", jb->name, (double)offset_timestamp * jb->sample_duration * 1000.0);
LOGP(DJITTER, LOGL_DEBUG, "%s Frame has offset of %.0fms in jitter buffer.\n", jb->name, (double)offset_timestamp * jb->sample_duration * 1000.0);
#endif
/* measure delay */
if (jb->min_delay_value < 0 || offset_timestamp < jb->min_delay_value)
jb->min_delay_value = offset_timestamp;
if (jb->min_delay < 0 || offset_timestamp < jb->min_delay)
jb->min_delay = offset_timestamp;
/* if frame is too early (delay ceases), shift window to the future */
if (offset_timestamp > jb->max_window_size) {
if ((jb->window_flags & JITTER_FLAG_LATENCY)) {
LOGP(DJITTER, LOGL_DEBUG, "%s Frame too early: Shift jitter buffer to the future, to make the frame fit to the end. (offset_timestamp(%d) > max_window_size(%d))\n", jb->name, offset_timestamp, jb->max_window_size);
LOGP(DJITTER, LOGL_DEBUG, "%s Frame too early: Shift jitter buffer to the future, to make the frame fit to the end. (offset_sequence(%d) > max_window_size(%d))\n", jb->name, offset_timestamp, jb->max_window_size);
/* shift window so it fits to the end of window */
jb->window_timestamp = timestamp - jb->max_window_size;
jb->window_timestamp = jf->timestamp - jb->max_window_size;
jb->min_delay = -1;
jb->delay_counter = 0.0;
jb->delay_interval = REPEAT_DELAY_INTERVAL;
} else {
LOGP(DJITTER, LOGL_DEBUG, "%s Frame too early: Shift jitter buffer to the future, to make the frame fit to the target delay. (offset_timestamp(%d) > max_window_size(%d))\n", jb->name, offset_timestamp, jb->max_window_size);
LOGP(DJITTER, LOGL_DEBUG, "%s Frame too early: Shift jitter buffer to the future, to make the frame fit to the target delay. (offset_sequence(%d) > max_window_size(%d))\n", jb->name, offset_timestamp, jb->max_window_size);
/* shift window so frame fits to the start of window + target delay */
jb->window_timestamp = timestamp - (uint32_t)(jb->target_window_size);
jb->window_timestamp = jf->timestamp - jb->target_window_size;
jb->min_delay = -1;
jb->delay_counter = 0.0;
jb->delay_interval = REPEAT_DELAY_INTERVAL;
}
}
/* is frame is too late, shift window to the past. */
if (offset_timestamp < 0) {
if ((jb->window_flags & JITTER_FLAG_LATENCY)) {
LOGP(DJITTER, LOGL_DEBUG, "%s Frame too late: Shift jitter buffer to the past, and add target window size. (offset_timestamp(%d) < 0)\n", jb->name, offset_timestamp);
LOGP(DJITTER, LOGL_DEBUG, "%s Frame too late: Shift jitter buffer to the past, and add target window size. (offset_sequence(%d) < 0)\n", jb->name, offset_timestamp);
/* shift window so frame fits to the start of window + half of target delay */
jb->window_timestamp = timestamp - (uint32_t)(jb->target_window_size) / 2;
jb->window_timestamp = jf->timestamp - jb->target_window_size / 2;
jb->min_delay = -1;
jb->delay_counter = 0.0;
jb->delay_interval = REPEAT_DELAY_INTERVAL;
} else {
LOGP(DJITTER, LOGL_DEBUG, "%s Frame too late: Shift jitter buffer to the past, and add half target window size. (offset_timestamp(%d) < 0)\n", jb->name, offset_timestamp);
LOGP(DJITTER, LOGL_DEBUG, "%s Frame too late: Shift jitter buffer to the past, and add half target window size. (offset_sequence(%d) < 0)\n", jb->name, offset_timestamp);
/* shift window so frame fits to the start of window + target delay */
jb->window_timestamp = timestamp - (uint32_t)(jb->target_window_size);
jb->window_timestamp = jf->timestamp - jb->target_window_size;
jb->min_delay = -1;
jb->delay_counter = 0.0;
jb->delay_interval = REPEAT_DELAY_INTERVAL;
}
}
/* insert or append frame */
#ifdef HEAVY_DEBUG
LOGP(DJITTER, LOGL_DEBUG, "%s Store frame\n", jb->name);
#include <time.h>
static struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
LOGP(DJITTER, LOGL_DEBUG, "%s Store frame. %ld.%04ld\n", jb->name, tv.tv_sec, tv.tv_nsec / 1000000);
#endif
jf = malloc(sizeof(*jf) + length * jb->sample_size);
if (!jf) {
LOGP(DJITTER, LOGL_ERROR, "No memory for frame.\n");
return;
}
memset(jf, 0, sizeof(*jf)); // note: clear header only
jf->sequence = sequence;
jf->timestamp = timestamp;
memcpy(jf->samples, samples, length * jb->sample_size);
jf->length = length;
jf->next = *jfp;
*jfp = jf;
}
/* get audio from jitterbuffer
*/
void jitter_load(jitter_t *jb, void *samples, int length)
/* get offset to next chunk, return -1, if there is no */
int32_t jitter_offset(jitter_t *jb)
{
jitter_frame_t *jf;
int32_t count, count2, index;
int16_t offset_timestamp = 0;
/* now unlock jitter buffer */
jb->unlocked = true;
/* get timestamp of chunk that is not in the past */
for (jf = jb->frame_list; jf; jf = jf->next) {
offset_timestamp = jf->timestamp - jb->window_timestamp;
if (offset_timestamp >= 0)
break;
}
return (jf) ? offset_timestamp : -1;
}
/* get next data chunk from jitterbuffer */
jitter_frame_t *jitter_load(jitter_t *jb)
{
jitter_frame_t *jf;
int32_t offset_timestamp;
#ifdef HEAVY_DEBUG
LOGP(DJITTER, LOGL_DEBUG, "%sLoad chunk of %d samples.\n", jb->name, length);
static struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
LOGP(DJITTER, LOGL_DEBUG, "%s Load frame. %ld.%04ld\n", jb->name, tv.tv_sec, tv.tv_nsec / 1000000);
#endif
/* now unlock jitter buffer */
jb->unlocked = 1;
jb->unlocked = true;
/* reduce delay */
jb->delay_counter += jb->sample_duration * (double)length;
if (jb->delay_counter >= jb->delay_interval) {
if (jb->min_delay_value >= 0)
LOGP(DJITTER, LOGL_DEBUG, "%s Statistics: target_window_delay=%.0fms max_window_delay=%.0fms current min_delay=%.0fms\n", jb->name, (double)jb->target_window_size * jb->sample_duration * 1000.0, (double)jb->max_window_size * jb->sample_duration * 1000.0, (double)jb->min_delay_value * jb->sample_duration * 1000.0);
/* delay reduction, if maximum delay is greater than target jitter window size */
if ((jb->window_flags & JITTER_FLAG_LATENCY) && jb->min_delay_value > jb->target_window_size) {
LOGP(DJITTER, LOGL_DEBUG, "%s Reducing current minimum delay of %.0fms, because maximum delay is greater than target window size of %.0fms.\n", jb->name, (double)jb->min_delay_value * jb->sample_duration * 1000.0, (double)jb->target_window_size * jb->sample_duration * 1000.0);
/* only reduce delay to half of the target window size */
jb->window_timestamp += jb->min_delay_value - jb->target_window_size / 2;
}
jb->delay_counter -= jb->delay_interval;
jb->delay_interval = REPEAT_DELAY_INTERVAL;
jb->min_delay_value = -1;
/* get current chunk, free all chunks that are in the past */
while ((jf = jb->frame_list)) {
offset_timestamp = jf->timestamp - jb->window_timestamp;
if (offset_timestamp >= 0)
break;
/* detach and free */
jb->frame_list = jf->next;
jitter_frame_free(jf);
}
/* process all frames until output buffer is loaded */
while (length) {
/* always get frame with the lowest sequence number (1st frame) */
jf = jb->frame_list;
/* next frame in the future */
if (jf && jf->timestamp != jb->window_timestamp)
return NULL;
if (jf) {
count = jf->timestamp - jb->window_timestamp;
if (count > length)
count = length;
} else
count = length;
/* if there is no frame or we have not reached frame's time stamp, extrapolate */
if (count > 0) {
#ifdef HEAVY_DEBUG
if (jf)
LOGP(DJITTER, LOGL_DEBUG, "%s There is a frame ahead in buffer after %d samples. Interpolating gap.\n", jb->name, jf->timestamp - jb->window_timestamp);
else
LOGP(DJITTER, LOGL_DEBUG, "%s There is no frame ahead in buffer. Interpolating gap.\n", jb->name);
#endif
/* extrapolate by playing the extrapolation buffer */
while (count) {
count2 = count;
if (count2 > jb->extra_size - jb->extra_index)
count2 = jb->extra_size - jb->extra_index;
memcpy(samples, (uint8_t *)jb->extra_samples + jb->extra_index * jb->sample_size, count2 * jb->sample_size);
jb->extra_index += count2;
if (jb->extra_index == jb->extra_size) {
jb->extra_index = 0;
if ((jb->window_flags & JITTER_FLAG_REPEAT) && jb->extra_timeout_count < jb->extra_timeout_max) {
jb->extra_timeout_count++;
if (jb->extra_timeout_count == jb->extra_timeout_max) {
#ifdef HEAVY_DEBUG
LOGP(DJITTER, LOGL_DEBUG, "%s Repeated jitter buffer enough, clearing to silence.\n", jb->name);
#endif
clear_extra_buffer(jb);
}
}
}
samples = (uint8_t *)samples + count2 * jb->sample_size;
length -= count2;
jb->window_timestamp += count2;
count -= count2;
}
if (length == 0)
return;
}
/* copy samples from frame (what is not in the past) */
index = jb->window_timestamp - jf->timestamp;
while (index < jf->length) {
/* use the lowest value of 'playout length' or 'remaining packet length' */
count = length;
if (jf->length - index < count)
count = jf->length - index;
/* if extrapolation is to be written, limit count to what we can store into buffer */
if ((jb->window_flags & JITTER_FLAG_REPEAT) && jb->extra_size - jb->extra_index < count)
count = jb->extra_size - jb->extra_index;
/* copy samples from packet to play out, increment sample pointer and decrement length */
#ifdef HEAVY_DEBUG
LOGP(DJITTER, LOGL_DEBUG, "%s Copy data (offset=%u count=%u) from frame (sequence=%u timestamp=%u length=%u).\n", jb->name, index, count, jf->sequence, jf->timestamp, jf->length);
#endif
memcpy(samples, (uint8_t *)jf->samples + index * jb->sample_size, count * jb->sample_size);
samples = (uint8_t *)samples + count * jb->sample_size;
length -= count;
/* copy frame data to extrapolation buffer also, increment index */
if ((jb->window_flags & JITTER_FLAG_REPEAT)) {
memcpy((uint8_t *)jb->extra_samples + jb->extra_index * jb->sample_size, (uint8_t *)jf->samples + index * jb->sample_size, count * jb->sample_size);
jb->extra_index += count;
if (jb->extra_index == jb->extra_size)
jb->extra_index = 0;
jb->extra_timeout_count = 0; /* now we have new data, we reset timeout condition */
}
/* increment time stamp */
jb->window_timestamp += count;
index += count;
/* if there was enough to play out, we are done */
if (length == 0)
return;
}
/* free frame, because all samples are now in the past */
/* detach, and return */
if (jf)
jb->frame_list = jf->next;
free(jf);
return jf;
}
/* now go for next loop, in case there is still date to play out */
/* advance time stamp of jitter buffer */
void jitter_advance(jitter_t *jb, uint32_t offset)
{
if (!jb->window_valid)
return;
jb->window_timestamp += offset;
/* increment timer to check delay */
jb->delay_counter += jb->sample_duration * (double)offset;
}
/* load samples from jitter buffer
* store in spl_buf until all copied
* conceal, if frame is missing
* ceate silence, if no spl_buf exists in the first place */
void jitter_load_samples(jitter_t *jb, uint8_t *spl, int len, size_t sample_size, void (*conceal)(uint8_t *spl, int len, void *priv), void *conceal_priv)
{
jitter_frame_t *jf;
int32_t offset;
void (*decoder)(uint8_t *src_data, int src_len, uint8_t **dst_data, int *dst_len, void *priv);
void *decoder_priv;
uint8_t *payload;
int payload_len;
int tocopy;
#ifdef VISUAL_DEBUG
int32_t offset_timestamp;
char debug[jb->max_window_size + 32];
int last = 0;
memset(debug, ' ', sizeof(debug));
for (jf = jb->frame_list; jf; jf = jf->next) {
offset_timestamp = jf->timestamp - jb->window_timestamp;
if (offset_timestamp < 0)
continue;
offset_timestamp = (int)((double)offset_timestamp * jb->sample_duration * 1000.0);
debug[offset_timestamp] = '0' + jf->sequence % 10;
last = offset_timestamp + 1;
}
debug[last] = '\0';
LOGP(DJITTER, LOGL_DEBUG, "%s:%s\n", jb->name, debug);
#endif
next_chunk:
/* nothing more to return */
if (!len)
return;
copy_chunk:
/* consume from buffer, if valid */
if (jb->spl_buf && jb->spl_valid) {
tocopy = jb->spl_len - jb->spl_pos;
if (tocopy > len)
tocopy = len;
#ifdef HEAVY_DEBUG
LOGP(DJITTER, LOGL_DEBUG, "%s loading %d samples: from valid sample buffer.\n", jb->name, tocopy);
#endif
/* advance jitter buffer */
jitter_advance(jb, tocopy);
memcpy(spl, jb->spl_buf + jb->spl_pos * sample_size, tocopy * sample_size);
spl += tocopy * sample_size;
len -= tocopy;
jb->spl_pos += tocopy;
if (jb->spl_pos == jb->spl_len) {
jb->spl_pos = 0;
jb->spl_valid = false;
}
goto next_chunk;
}
/* get offset to next frame in jitter buffer */
offset = jitter_offset(jb);
/* jitter buffer is empty, so we must conceal all samples we have */
if (offset < 0)
offset = len;
/* if we have an offset, we need to conceal the samples */
if (offset > 0) {
/* only process as much samples as need */
if (offset > len)
offset = len;
#ifdef HEAVY_DEBUG
LOGP(DJITTER, LOGL_DEBUG, "%s concealing %d samples: from invalid sample buffer.\n", jb->name, offset);
#endif
/* advance jitter buffer */
jitter_advance(jb, offset);
/* if there is no buffer, allocate 20ms, filled with 0 */
if (!jb->spl_buf) {
jb->spl_len = jb->samples_20ms;
jb->spl_buf = calloc(jb->spl_len, sample_size);
}
/* do until all samples are processed */
while (offset) {
tocopy = jb->spl_len - jb->spl_pos;
if (tocopy > offset)
tocopy = offset;
if (conceal)
conceal(jb->spl_buf + jb->spl_pos * sample_size, tocopy, conceal_priv);
memcpy(spl, jb->spl_buf + jb->spl_pos * sample_size, tocopy * sample_size);
spl += tocopy * sample_size;
len -= tocopy;
jb->spl_pos += tocopy;
if (jb->spl_pos == jb->spl_len)
jb->spl_pos = 0;
offset -= tocopy;
}
goto next_chunk;
}
/* load from jitter buffer (it should work, because offset equals 0 */
jf = jitter_load(jb);
if (!jf) {
LOGP(DJITTER, LOGL_ERROR, "%s Failed to get frame from jitter buffer, please fix!\n", jb->name);
jitter_reset(jb);
return;
}
#ifdef HEAVY_DEBUG
LOGP(DJITTER, LOGL_DEBUG, "%s loading new frame to sample buffer.\n", jb->name);
#endif
/* get data from frame */
jitter_frame_get(jf, &decoder, &decoder_priv, &payload, &payload_len, NULL, NULL, NULL, NULL);
/* free previous buffer */
free(jb->spl_buf);
jb->spl_buf = NULL;
jb->spl_pos = 0;
/* decode */
if (decoder) {
decoder(payload, payload_len, &jb->spl_buf, &jb->spl_len, decoder_priv);
if (!jb->spl_buf) {
jitter_frame_free(jf);
return;
}
} else {
/* no decoder, so just copy as it is */
jb->spl_buf = malloc(payload_len);
if (!jb->spl_buf) {
jitter_frame_free(jf);
return;
}
memcpy(jb->spl_buf, payload, payload_len);
jb->spl_len = payload_len;
}
jb->spl_len /= sample_size;
jb->spl_valid = true;
/* free jiter frame */
jitter_frame_free(jf);
goto copy_chunk;
}
void jitter_conceal_s16(uint8_t *_spl, int len, void __attribute__((unused)) *priv)
{
int16_t *spl = (int16_t *)_spl;
while (len) {
*spl++ /= 1.5;
len--;
}
}

View File

@ -4,57 +4,64 @@
#define JITTER_FLAG_REPEAT (1 << 1) // repeat audio to extrapolate gaps
/* window settings for low latency audio and extrapolation of gaps */
#define JITTER_AUDIO 0.050, 1.000, JITTER_FLAG_LATENCY | JITTER_FLAG_REPEAT
#define JITTER_AUDIO 0.060, 1.000, JITTER_FLAG_LATENCY | JITTER_FLAG_REPEAT
/* window settings for analog data (fax/modem) or digial data (HDLC) */
#define JITTER_DATA 0.100, 0.200, JITTER_FLAG_NONE
typedef struct jitter_frame {
struct jitter_frame *next;
void (*decoder)(uint8_t *src_data, int src_len, uint8_t **dst_data, int *dst_len, void *priv);
void *decoder_priv;
uint8_t marker;
uint16_t sequence;
uint32_t timestamp;
int length;
uint8_t samples[0];
uint32_t ssrc;
int size;
uint8_t data[0];
} jitter_frame_t;
typedef struct jitter {
char name[64];
/* sample properties */
int sample_size;
double sample_duration;
/* automatic sequence generation */
uint16_t next_sequence;
uint32_t next_timestamp;
/* frame properties */
double sample_duration; /* duration of a frame (ms) */
int samples_20ms; /* samples to compensate a gap of unknown size */
/* window properties */
int unlocked;
uint32_t window_flags;
int target_window_size;
int max_window_size;
int window_valid;
uint32_t window_ssrc;
uint32_t window_timestamp;
bool unlocked; /* jitter buffer will be locked until some reads from it */
uint32_t window_flags; /* flags to alter behaviour of jitter buffer */
int target_window_size; /* target size of window (frames) */
int max_window_size; /* maximum size of window (frames) */
bool window_valid; /* set, if first frame has been received */
uint32_t window_ssrc; /* current sync source of window */
uint32_t window_timestamp; /* lowest timestamp number in window */
/* reduction of delay */
double delay_interval;
double delay_counter;
int32_t min_delay_value;
/* extrapolation */
int extra_size;
int extra_index;
void *extra_samples;
int extra_timeout_max;
int extra_timeout_count;
double delay_interval; /* interval for delay measurement (seconds) */
double delay_counter; /* current counter to count interval (seconds) */
int min_delay; /* minimum delay measured during interval (frames) */
/* list of frames */
jitter_frame_t *frame_list;
/* sample buffer (optional) */
uint8_t *spl_buf; /* current samples buffer */
int spl_pos; /* position of in buffer */
int spl_len; /* total buffer size */
bool spl_valid; /* if buffer has valid frame (not repeated) */
} jitter_t;
int jitter_create(jitter_t *jb, const char *name, double samplerate, int sample_size, double target_window_duration, double max_window_duration, uint32_t window_flags);
int jitter_create(jitter_t *jb, const char *name, double samplerate, double target_window_duration, double max_window_duration, uint32_t window_flags);
void jitter_reset(jitter_t *jb);
void jitter_destroy(jitter_t *jb);
void jitter_save(jitter_t *jb, void *samples, int length, int has_sequence, uint16_t sequence, uint32_t timestamp, uint32_t ssrc);
void jitter_load(jitter_t *jb, void *samples, int length);
jitter_frame_t *jitter_frame_alloc(void (*decoder)(uint8_t *src_data, int src_len, uint8_t **dst_data, int *dst_len, void *priv), void *decoder_priv, uint8_t *data, int size, uint8_t marker, uint16_t sequence, uint32_t timestamp, uint32_t ssrc);
void jitter_frame_free(jitter_frame_t *jf);
void jitter_frame_get(jitter_frame_t *jf, void (**decoder)(uint8_t *src_data, int src_len, uint8_t **dst_data, int *dst_len, void *priv), void **decoder_priv, uint8_t **data, int *size, uint8_t *marker, uint16_t *sequence, uint32_t *timestamp, uint32_t *ssrc);
void jitter_save(jitter_t *jb, jitter_frame_t *jf);
int32_t jitter_offset(jitter_t *jb);
jitter_frame_t *jitter_load(jitter_t *jb);
void jitter_advance(jitter_t *jb, uint32_t offset);
void jitter_load_samples(jitter_t *jb, uint8_t *spl, int len, size_t sample_size, void (*conceal)(uint8_t *spl, int len, void *priv), void *conceal_priv);
void jitter_conceal_s16(uint8_t *_spl, int len, void __attribute__((unused)) *priv);

View File

@ -23,72 +23,89 @@
* This diagrams shows the audio processing. The function for each processing
* segment is given by the names ending with "()".
*
* ORIGINATOR
*
* receive_originator()
* | /|\
* | |
* \|/ |
* +-------+ +-------+
* |int to | |samples|
* |samples| |to int |
* +-------+ +-------+
* | /|\
* +------+ | |
* | |/ | |
* | DTMF |---| |
* | |\ | |
* +------+ | |
* \|/ |
* +-------+ +-------+
* | TX- | | RX- |
* | GAIN | | GAIN |
* +-------+ +-------+
* | /|\
* | |
* | |
* +------+ | | +------+
* | TX- |/ | | \| RX- |
* | |---| |---| |
* |JITTER|\ | | /|JITTER|
* +------+ | | +------+
* | |
* +-------+ |
* clock->|jitter | |
* |buffer | |
* +-------+ |
* | |
* | |
* \|/ send_originator()
*-----------------------------------
* send_terminator() /|\
* | | +------+
* | |\ | WAVE |
* | | \_| | call_clock()
* | | | PLAY |
* | | +------+
* | |
* | +-------+
* | |jitter |
* | |buffer |<-clock
* | +-------+
* | /|\
* | | +------+
* | | \| |
* | |---| DTMF |
* | | /| |
* | | +------+
* \|/ |
* +-------+ +-------+
* |samples| |int to |
* |to int | |samples|
* +-------+ +-------+
* | /|\
* | |
* \|/ |
* receive_terminator()
* receive_originator() receive_terminator()
* | |
* | |
* \|/ \|/
* +-------+ +-------+
* |jitter | |jitter |
* |save | |save |
* +-------+ +-------+
*
* TERMINATOR
*
* clock()
* |
* |
* ORIG \|/ TERM
* +---------+----------+
* | |
* \|/ \|/
* +-------+ +-------+
* |jitter | |jitter |
* |load | |load |
* +-------+ +-------+
* | |
* \|/ \|/
* +-------+ +-------+
* |decode | |decode |
* | | | |
* +-------+ +-------+
* | |
* \|/ \|/
* +-------+ +-------+
* |int to | |int to |
* |samples| |samples|
* +-------+ +-------+
* | |
* +------+ | | +------+
* | |/ | | \| |
* | DTMF |---| |---| DTMF |
* | |\ | | /| |
* +------+ | | +------+
* | |
* +------+ | | +------+
* | WAVE | | | | WAVE |
* | |_ | | _| |
* | PLAY | \ | | / | PLAY |
* +------+ \| |/ +------+
* | |
* | +------+ |
* |\ | WAVE | /|
* | \____| |____/ |
* | |RECORD| |
* | +------+ |
* | |
* send_terminator() send_originator()
* | |
* \|/ \|/
* +-------+ +-------+
* | TX- | | RX- |
* |COMPRES| |COMPRES|
* +-------+ +-------+
* | |
* \|/ \|/
* +-------+ +-------+
* | TX- | | RX- |
* | GAIN | | GAIN |
* +-------+ +-------+
* | |
* \|/ \|/
* +-------+ +-------+
* |samples| |samples|
* |to int | |to int |
* +-------+ +-------+
* | |
* \|/ \|/
* +-------+ +-------+
* |encode | |encode |
* | | | |
* +-------+ +-------+
* | |
* | |
* \|/ \|/
*
* RTP RTP
*
* The clock triggers read from jitter buffer and replaces it with wave,
* if playing. Also it record what is sent to originator and terminator,
@ -126,53 +143,69 @@ static void gain_samples(sample_t *samples, int length, double gain)
*samples++ *= level;
}
static void send_terminator(call_relation_t *relation, sample_t *samples, int len, uint8_t marker)
{
int16_t spl[len];
/* convert samples to int16 */
samples_to_int16_speech(spl, samples, len);
/* encode and send via RTP */
osmo_cc_rtp_send(relation->codec, (uint8_t *)spl, len * sizeof(*spl), marker, 1, len, relation);
}
void receive_originator(struct osmo_cc_session_codec *codec, uint8_t marker, uint16_t sequence, uint32_t timestamp, uint32_t ssrc, uint8_t *data, int len)
void receive_originator(struct osmo_cc_session_codec *codec, uint8_t marker, uint16_t sequence, uint32_t timestamp, uint32_t ssrc, uint8_t *payload, int payload_len)
{
call_relation_t *relation = codec->media->session->priv;
call_t *call = relation->call;
len = len / 2;
sample_t samples[len];
jitter_frame_t *jf;
/* telephone-events */
if (codec->decoder == decode_te) {
struct telephone_event *te = (struct telephone_event *)data;
rx_telephone_event(relation, marker, te);
uint8_t *data;
int len;
struct telephone_event *te;
codec->decoder(payload, payload_len, &data, &len, relation);
te = (struct telephone_event *)data;
rx_telephone_event(relation, marker, te, 0);
free(data);
return;
}
/* convert int16 to samples */
int16_to_samples_speech(samples, (int16_t *)data, len);
/* dtmf decoding */
if (relation->dtmf_dec_enable)
dtmf_decode(&relation->dtmf_dec, samples, len);
/* compress */
if (call->tx_compress)
sendevolumenregler(&call->tx_compressor, samples, len);
/* adjust gain */
if (call->tx_gain)
gain_samples(samples, len, call->tx_gain);
/* store to originator jitter buffer */
jitter_save(&call->orig_dejitter, samples, len, 1, sequence, timestamp, ssrc);
jf = jitter_frame_alloc(codec->decoder, relation, payload, payload_len, marker, sequence, timestamp, ssrc);
if (!jf)
return;
jitter_save(&call->orig_dejitter, jf);
}
static void send_originator(call_relation_t *relation, sample_t *samples, int len, uint8_t marker)
void receive_terminator(struct osmo_cc_session_codec *codec, uint8_t marker, uint16_t sequence, uint32_t timestamp, uint32_t ssrc, uint8_t *payload, int payload_len)
{
call_relation_t *relation = codec->media->session->priv;
call_t *call = relation->call;
jitter_frame_t *jf;
/* ignore data from forking call */
if (call->forking)
return;
/* telephone-events */
if (codec->decoder == decode_te) {
uint8_t *data;
int len;
struct telephone_event *te;
codec->decoder(payload, payload_len, &data, &len, relation);
te = (struct telephone_event *)data;
rx_telephone_event(relation, marker, te, 1);
free(data);
return;
}
/* store to terminator jitter buffer */
jf = jitter_frame_alloc(codec->decoder, relation, payload, payload_len, marker, sequence, timestamp, ssrc);
if (!jf)
return;
jitter_save(&call->term_dejitter, jf);
}
static void send_originator(call_relation_t *relation, sample_t *samples, int len)
{
int16_t spl[len];
call_t *call = relation->call;
uint8_t *payload;
int payload_len;
if (!relation->codec)
return;
/* compress */
if (call->rx_compress)
@ -185,38 +218,44 @@ static void send_originator(call_relation_t *relation, sample_t *samples, int le
/* convert samples to int16 */
samples_to_int16_speech(spl, samples, len);
osmo_cc_rtp_send(relation->codec, (uint8_t *)spl, len * sizeof(*spl), marker, 1, len, relation);
/* encode and send via RTP */
relation->codec->encoder((uint8_t *)spl, len * 2, &payload, &payload_len, relation);
osmo_cc_rtp_send(relation->codec, payload, payload_len, 0, 1, len);
free(payload);
}
void receive_terminator(struct osmo_cc_session_codec *codec, uint8_t __attribute__((unused)) marker, uint16_t sequence, uint32_t timestamp, uint32_t ssrc, uint8_t *data, int len)
static void send_terminator(call_relation_t *relation, sample_t *samples, int len)
{
call_relation_t *relation = codec->media->session->priv;
int16_t spl[len];
call_t *call = relation->call;
len = len / 2;
sample_t samples[len];
uint8_t *payload;
int payload_len;
/* ignore data from forking call */
if (call->forking)
if (!relation->codec)
return;
if (codec->decoder == decode_te) {
LOGP(DROUTER, LOGL_NOTICE, "Ignoring received telephony-events from terminator.\n");
return;
}
/* compress */
if (call->tx_compress)
sendevolumenregler(&call->tx_compressor, samples, len);
/* convert int16 to samples */
int16_to_samples_speech(samples, (int16_t *)data, len);
/* adjust gain */
if (call->tx_gain)
gain_samples(samples, len, call->tx_gain);
/* dtmf decoding */
if (relation->dtmf_dec_enable)
dtmf_decode(&relation->dtmf_dec, samples, len);
/* convert samples to int16 */
samples_to_int16_speech(spl, samples, len);
/* store to terminator jitter buffer */
jitter_save(&call->term_dejitter, samples, len, 1, sequence, timestamp, ssrc);
/* encode and send via RTP */
relation->codec->encoder((uint8_t *)spl, len * 2, &payload, &payload_len, relation);
osmo_cc_rtp_send(relation->codec, payload, payload_len, 0, 1, len);
free(payload);
}
void tx_telephone_event(call_relation_t *relation, uint8_t marker, struct telephone_event *te)
{
uint8_t *payload;
int payload_len;
/* only if codec was negotiated */
if (!relation->telephone_event)
return;
@ -226,75 +265,113 @@ void tx_telephone_event(call_relation_t *relation, uint8_t marker, struct teleph
return;
/* encode and send via RTP */
osmo_cc_rtp_send_ts(relation->telephone_event, (uint8_t *)te, sizeof(*te), marker, relation->codec->media->tx_sequence, relation->codec->media->tx_timestamp, relation);
relation->telephone_event->encoder((uint8_t *)te, sizeof(*te), &payload, &payload_len, relation);
osmo_cc_rtp_send_ts(relation->telephone_event, payload, payload_len, marker, relation->codec->media->tx_sequence, relation->codec->media->tx_timestamp);
}
int _play_wave(wave_play_t *play, sample_t *buffer, int len, int play_loop, const char *play_filename, double play_deviation)
{
sample_t wbuffer[len], wbuffer2[len], *waves[2];
int got = 0;
int finished = 0;
int rc;
int i;
read_again:
waves[0] = wbuffer + got;
waves[1] = wbuffer2 + got;
rc = wave_read(play, waves, len - got);
got += rc;
/* we have a short read (hit the end) or nothing to play left (hit the end without short read) */
if (!play->left) {
wave_destroy_playback(play);
if (play_loop) {
int samplerate = 0, channels = 0;
int rc;
rc = wave_create_playback(play, play_filename, &samplerate, &channels, play_deviation);
if (rc >= 0)
goto read_again;
} else {
/* notify routing about finished playback */
finished = 1;
}
}
/* in case wie do not get all samples filled, append silence */
while (got < len) {
wbuffer[got++] = 0.0;
wbuffer2[got++] = 0.0;
}
/* convert stereo to mono */
if (play->channels == 2) {
for (i = 0; i < len; i++)
buffer[i] = (wbuffer[i] + wbuffer2[i]) / 2.0;
} else {
for (i = 0; i < len; i++)
buffer[i] = wbuffer[i];
}
return finished;
}
void call_clock(int len)
{
call_t *call;
call_t *call, *call_next;
call_relation_t *relation;
sample_t sbuffer[len], sbuffer2[len], *samples[2];
sample_t wbuffer[len], wbuffer2[len], *waves[2];
int i;
int16_t spl[len];
sample_t orig_samples[len], term_samples[len], *samples[2] = { orig_samples, term_samples };
int rc;
for (call = call_list; call; call = call->next) {
for (call = call_list; call; call = call_next) {
call_next = call->next;
relation = call->relation_list;
/* do we have no RTP proxy ? */
if (!relation->cc_session || !relation->codec)
continue;
/* load from both jitter buffers */
samples[0] = sbuffer;
samples[1] = sbuffer2;
jitter_load(&call->orig_dejitter, samples[0], len);
if (!call->forking && relation->next)
jitter_load(&call->term_dejitter, samples[1], len);
else
memset(samples[1], 0, len * sizeof(sample_t));
/* process originating audio */
jitter_load_samples(&call->orig_dejitter, (uint8_t *)spl, len, sizeof(*spl), jitter_conceal_s16, NULL);
/* convert to samples */
int16_to_samples_speech(orig_samples, spl, len);
/* dtmf decoding */
if (relation->dtmf_dec_enable)
dtmf_decode(&relation->dtmf_dec, orig_samples, len);
if (!call->forking && relation->next) {
/* process terminating audio */
jitter_load_samples(&call->term_dejitter, (uint8_t *)spl, len, sizeof(*spl), jitter_conceal_s16, NULL);
/* convert to samples */
int16_to_samples_speech(term_samples, spl, len);
/* dtmf decoding */
if (relation->next->dtmf_dec_enable)
dtmf_decode(&relation->next->dtmf_dec, term_samples, len);
} else
memset(term_samples, 0, len * sizeof(sample_t));
/* play (overload data from jitter buffer) */
if (call->play.fp) {
int got = 0;
read_again:
waves[0] = wbuffer + got;
waves[1] = wbuffer2 + got;
rc = wave_read(&call->play, waves, len - got);
got += rc;
/* we have a short read (hit the end) or nothing to play left (hit the end without short read) */
if (!call->play.left) {
wave_destroy_playback(&call->play);
if (call->play_loop) {
int samplerate = 0, channels = 0;
int rc;
rc = wave_create_playback(&call->play, call->play_filename, &samplerate, &channels, call->play_deviation);
if (rc >= 0)
goto read_again;
} else {
/* notify routing about finished playback */
if (call->routing.routing)
routing_send(&call->routing, "wave-finished");
}
}
/* in case wie do not get all samples filled, append silence */
while (got < len) {
wbuffer[got++] = 0.0;
wbuffer2[got++] = 0.0;
}
/* convert stereo to mono */
if (call->play.channels == 2) {
for (i = 0; i < len; i++)
sbuffer2[i] = wbuffer[i] + wbuffer2[i];
} else {
for (i = 0; i < len; i++)
sbuffer2[i] = wbuffer[i];
}
if (call->orig_play.fp) {
rc = _play_wave(&call->orig_play, term_samples, len, call->orig_play_loop, call->orig_play_filename, call->orig_play_deviation);
if (call->routing.routing && rc)
routing_send(&call->routing, "wave-finished");
}
if (call->term_play.fp) {
rc = _play_wave(&call->term_play, orig_samples, len, call->term_play_loop, call->term_play_filename, call->term_play_deviation);
if (call->routing.routing && rc)
routing_send(&call->routing, "called-wave-finished");
}
/* record */
if (call->rec.fp)
wave_write(&call->rec, samples, len);
/* forward audio (no marker set) */
send_originator(relation, sbuffer2, len, 0);
/* forward audio */
send_originator(relation, term_samples, len);
if (!call->forking && relation->next)
send_terminator(relation->next, sbuffer, len, 0);
send_terminator(relation->next, orig_samples, len);
}
}

View File

@ -198,6 +198,7 @@ static call_t *call_create(void)
{
call_t *call, **call_p;
static int call_num = 0;
char jitter_name[64];
int rc;
call = calloc(1, sizeof(*call));
@ -210,10 +211,12 @@ static call_t *call_create(void)
call->routing.call = call;
/* allocate jitter buffer */
rc = jitter_create(&call->orig_dejitter, "tx", 8000, sizeof(sample_t), JITTER_AUDIO);
sprintf(jitter_name, "call #%d orig->term", call->num);
rc = jitter_create(&call->orig_dejitter, jitter_name, 8000.0, JITTER_AUDIO);
if (rc < 0)
abort();
rc = jitter_create(&call->term_dejitter, "tx", 8000, sizeof(sample_t), JITTER_AUDIO);
sprintf(jitter_name, "call #%d term->orig", call->num);
rc = jitter_create(&call->term_dejitter, jitter_name, 8000.0, JITTER_AUDIO);
if (rc < 0)
abort();
@ -237,7 +240,8 @@ static void call_destroy(call_t *call)
new_state(call, CALL_STATE_IDLE);
/* playback and record */
wave_destroy_playback(&call->play);
wave_destroy_playback(&call->orig_play);
wave_destroy_playback(&call->term_play);
wave_destroy_record(&call->rec);
/* remove setup message */
@ -442,7 +446,7 @@ static void orig_setup(osmo_cc_endpoint_t *cc_ep, uint32_t callref, osmo_cc_msg_
/* creating call instance, transparent until setup with hdlc */
call = call_create();
if (!call) {
LOGP(DROUTER, LOGL_ERROR, "Cannot create calll instance.\n");
LOGP(DROUTER, LOGL_ERROR, "Cannot create call instance.\n");
abort();
}
relation = relation_create(call);
@ -1412,11 +1416,17 @@ struct param_def param_play[] = {
static void routing_play(call_t *call, int argc, char *argv[], struct command_def *command_def)
{
wave_play_t *play;
int samplerate = 8000, channels = 0;
double deviation;
int rc;
wave_destroy_playback(&call->play);
if (!strncmp(command_def->name, "called-", 7))
play = &call->term_play;
else
play = &call->orig_play;
wave_destroy_playback(play);
if (!call->rtp_proxy) {
LOGP(DROUTER, LOGL_ERROR, "RTP-Proxy must be enabled to play a file!.\n");
@ -1434,27 +1444,40 @@ static void routing_play(call_t *call, int argc, char *argv[], struct command_de
param.volume = "1.0";
deviation = 1.0 / SPEECH_LEVEL * atof(param.volume);
rc = wave_create_playback(&call->play, param.filename, &samplerate, &channels, deviation);
rc = wave_create_playback(play, param.filename, &samplerate, &channels, deviation);
if (rc < 0)
return;
strncpy(call->play_filename, param.filename, sizeof(call->play_filename) - 1);
call->play_deviation = deviation;
if (!strncmp(command_def->name, "called-", 7)) {
strncpy(call->term_play_filename, param.filename, sizeof(call->term_play_filename) - 1);
call->term_play_deviation = deviation;
if (param.loop)
call->term_play_loop = 1;
} else {
strncpy(call->orig_play_filename, param.filename, sizeof(call->orig_play_filename) - 1);
call->orig_play_deviation = deviation;
if (param.loop)
call->orig_play_loop = 1;
}
if (channels != 1 && channels != 2) {
wave_destroy_playback(&call->play);
wave_destroy_playback(play);
LOGP(DROUTER, LOGL_ERROR, "'play' command reqires a wave file that has 1 or 2 channels only.\n");
return;
}
if (param.loop)
call->play_loop = 1;
}
/* routing orders us stop playing a wave file */
static void routing_play_stop(call_t *call, int __attribute__((unused)) argc, char __attribute__((unused)) *argv[], struct command_def __attribute__((unused)) *command_def)
{
wave_destroy_playback(&call->play);
wave_play_t *play;
if (!strncmp(command_def->name, "called-", 7))
play = &call->term_play;
else
play = &call->orig_play;
wave_destroy_playback(play);
}
/* routing orders us to record a wave file */
@ -2090,7 +2113,7 @@ static void routing_dtmf(call_t *call, int __attribute__((unused)) argc, char __
{
call_relation_t *relation = call->relation_list;
if (!strcmp(command_def->name, "called-dtmf")) {
if (!strncmp(command_def->name, "called-", 7)) {
relation = relation->next;
if (!relation || relation->next) {
@ -2112,7 +2135,7 @@ static void routing_dtmf_stop(call_t *call, int __attribute__((unused)) argc, ch
{
call_relation_t *relation = call->relation_list;
if (!strcmp(command_def->name, "called-dtmf-stop")) {
if (!strncmp(command_def->name, "called-", 7)) {
relation = relation->next;
if (!relation || relation->next)
@ -2136,7 +2159,7 @@ static void routing_te(call_t *call, int __attribute__((unused)) argc, char __at
uint8_t marker;
uint8_t event;
if (!strcmp(command_def->name, "called-telephone-event")) {
if (!strncmp(command_def->name, "called-", 7)) {
relation = relation->next;
if (!relation || relation->next) {
@ -2238,8 +2261,10 @@ static void routing_error(call_t *call, int __attribute__((unused)) argc, char _
struct command_def command_def[] = {
{ "rtp-proxy", routing_rtp_proxy, "Turn on RTP proxy, so that audio processing is possible.", param_rtp_proxy },
{ "play", routing_play, "Play given audio file.", param_play },
{ "play-stop", routing_play_stop, "Stop playing audio file.", NULL },
{ "play", routing_play, "Play given audio file at callee.", param_play },
{ "play-stop", routing_play_stop, "Stop playing audio file at callee.", NULL },
{ "called-play", routing_play, "Play given audio file.", param_play },
{ "called-play-stop", routing_play_stop, "Stop playing audio file.", NULL },
{ "record", routing_record, "Record to given audio file.", param_record },
{ "record-stop", routing_record_stop, "Stop recording audio file.", NULL },
{ "tx-gain", routing_tx_gain, "Set gain of audio sent to originating interface.", param_gain },
@ -2404,9 +2429,13 @@ void routing_close(routing_t *routing)
call_destroy(call);
}
void rx_telephone_event(call_relation_t *relation, uint8_t marker, struct telephone_event *te)
void rx_telephone_event(call_relation_t *relation, uint8_t marker, struct telephone_event *te, int called)
{
char digit_string[7] = "dtmf x";
char digit_string[32];
if (called)
sprintf(digit_string, "called-dtmf x");
else
sprintf(digit_string, "dtmf x");
if (te->event < 16) {
if (marker && te->volume <= 36) {
@ -2415,11 +2444,11 @@ void rx_telephone_event(call_relation_t *relation, uint8_t marker, struct teleph
relation->te_started = 1;
} else
LOGP(DROUTER, LOGL_INFO, "Received start and end of Telephone-Event '%d' duration=%d ms (marker set)\n", te->event, te->duration / 8);
digit_string[5] = "0123456789*#ABCD"[te->event];
digit_string[strlen(digit_string) - 1] = "0123456789*#ABCD"[te->event];
} else if (!relation->te_started && !te->e && te->volume <= 36) {
LOGP(DROUTER, LOGL_INFO, "Received start of Telephone-Event '%d' duration=%d ms (marker not set, this is wrong!)\n", te->event, te->duration / 8);
relation->te_started = 1;
digit_string[5] = "0123456789*#ABCD"[te->event];
digit_string[strlen(digit_string) - 1] = "0123456789*#ABCD"[te->event];
} else if (relation->te_started && !te->e && te->volume <= 36) {
LOGP(DROUTER, LOGL_DEBUG, "Received subsequent Telephone-Event '%d' duration=%d ms\n", te->event, te->duration / 8);
} else if (relation->te_started && te->e) {
@ -2434,7 +2463,7 @@ void rx_telephone_event(call_relation_t *relation, uint8_t marker, struct teleph
if (!relation->call->routing.routing)
return;
if (digit_string[5] != 'x')
if (digit_string[strlen(digit_string) - 1] != 'x')
routing_send(&relation->call->routing, digit_string);
}

View File

@ -90,10 +90,14 @@ typedef struct call {
struct timeval metering_unit_period; /* period of one metering unit */
/* wave playback/record */
wave_play_t play; /* play a wave file */
int play_loop; /* set to play loop */
char play_filename[256];/* stored for reopen on loop */
double play_deviation; /* stored for reopen on loop */
wave_play_t orig_play; /* play a wave file */
int orig_play_loop; /* set to play loop */
char orig_play_filename[256];/* stored for reopen on loop */
double orig_play_deviation; /* stored for reopen on loop */
wave_play_t term_play; /* play a wave file */
int term_play_loop; /* set to play loop */
char term_play_filename[256];/* stored for reopen on loop */
double term_play_deviation; /* stored for reopen on loop */
wave_rec_t rec; /* record a wave file */
} call_t;
@ -110,7 +114,7 @@ int call_init(osmo_cc_endpoint_t *cc_ep1, osmo_cc_endpoint_t *cc_ep2, const char
void call_exit(void);
int call_handle(void);
void cc_message(osmo_cc_endpoint_t *ep, uint32_t callref, osmo_cc_msg_t *msg);
void rx_telephone_event(call_relation_t *relation, uint8_t marker, struct telephone_event *te);
void rx_telephone_event(call_relation_t *relation, uint8_t marker, struct telephone_event *te, int called);
void routing_help(void);