mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-19 19:52:48 +00:00
clean up formatting (bug #3934)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@5363 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
141
jitterbuf.c
141
jitterbuf.c
@@ -70,7 +70,8 @@ jitterbuf * jb_new()
|
||||
|
||||
|
||||
jb = malloc(sizeof(jitterbuf));
|
||||
if(!jb) return NULL;
|
||||
if (!jb)
|
||||
return NULL;
|
||||
|
||||
jb_reset(jb);
|
||||
|
||||
@@ -85,7 +86,7 @@ void jb_destroy(jitterbuf *jb)
|
||||
|
||||
/* free all the frames on the "free list" */
|
||||
frame = jb->free;
|
||||
while(frame != NULL) {
|
||||
while (frame != NULL) {
|
||||
jb_frame *next = frame->next;
|
||||
free(frame);
|
||||
frame = next;
|
||||
@@ -112,7 +113,8 @@ static void history_put(jitterbuf *jb, long ts, long now)
|
||||
long kicked;
|
||||
|
||||
/* don't add special/negative times to history */
|
||||
if(ts <= 0) return;
|
||||
if (ts <= 0)
|
||||
return;
|
||||
|
||||
kicked = jb->history[jb->hist_ptr & JB_HISTORY_SZ];
|
||||
|
||||
@@ -123,27 +125,27 @@ static void history_put(jitterbuf *jb, long ts, long now)
|
||||
* that got kicked out of the history is also not involved
|
||||
* We do a number of comparisons, but it's probably still worthwhile, because it will usually
|
||||
* succeed, and should be a lot faster than going through all 500 packets in history */
|
||||
if(!jb->hist_maxbuf_valid)
|
||||
if (!jb->hist_maxbuf_valid)
|
||||
return;
|
||||
|
||||
/* don't do this until we've filled history
|
||||
* (reduces some edge cases below) */
|
||||
if(jb->hist_ptr < JB_HISTORY_SZ)
|
||||
if (jb->hist_ptr < JB_HISTORY_SZ)
|
||||
goto invalidate;
|
||||
|
||||
/* if the new delay would go into min */
|
||||
if(delay < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])
|
||||
if (delay < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])
|
||||
goto invalidate;
|
||||
|
||||
/* or max.. */
|
||||
if(delay > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])
|
||||
if (delay > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])
|
||||
goto invalidate;
|
||||
|
||||
/* or the kicked delay would be in min */
|
||||
if(kicked <= jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])
|
||||
if (kicked <= jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1])
|
||||
goto invalidate;
|
||||
|
||||
if(kicked >= jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])
|
||||
if (kicked >= jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1])
|
||||
goto invalidate;
|
||||
|
||||
/* if we got here, we don't need to invalidate, 'cause this delay didn't
|
||||
@@ -161,11 +163,12 @@ static void history_calc_maxbuf(jitterbuf *jb)
|
||||
{
|
||||
int i,j;
|
||||
|
||||
if(jb->hist_ptr == 0) return;
|
||||
if (jb->hist_ptr == 0)
|
||||
return;
|
||||
|
||||
|
||||
/* initialize maxbuf/minbuf to the latest value */
|
||||
for(i=0;i<JB_HISTORY_MAXBUF_SZ;i++) {
|
||||
for (i=0;i<JB_HISTORY_MAXBUF_SZ;i++) {
|
||||
/*
|
||||
* jb->hist_maxbuf[i] = jb->history[(jb->hist_ptr-1) % JB_HISTORY_SZ];
|
||||
* jb->hist_minbuf[i] = jb->history[(jb->hist_ptr-1) % JB_HISTORY_SZ];
|
||||
@@ -180,16 +183,16 @@ static void history_calc_maxbuf(jitterbuf *jb)
|
||||
/* start at the beginning, or JB_HISTORY_SZ frames ago */
|
||||
i = (jb->hist_ptr > JB_HISTORY_SZ) ? (jb->hist_ptr - JB_HISTORY_SZ) : 0;
|
||||
|
||||
for(;i<jb->hist_ptr;i++) {
|
||||
for (;i<jb->hist_ptr;i++) {
|
||||
long toins = jb->history[i % JB_HISTORY_SZ];
|
||||
|
||||
/* if the maxbuf should get this */
|
||||
if(toins > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1]) {
|
||||
if (toins > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1]) {
|
||||
|
||||
/* insertion-sort it into the maxbuf */
|
||||
for(j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
|
||||
for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
|
||||
/* found where it fits */
|
||||
if(toins > jb->hist_maxbuf[j]) {
|
||||
if (toins > jb->hist_maxbuf[j]) {
|
||||
/* move over */
|
||||
memmove(jb->hist_maxbuf+j+1,jb->hist_maxbuf+j, (JB_HISTORY_MAXBUF_SZ-(j+1)) * sizeof(long));
|
||||
/* insert */
|
||||
@@ -201,12 +204,12 @@ static void history_calc_maxbuf(jitterbuf *jb)
|
||||
}
|
||||
|
||||
/* if the minbuf should get this */
|
||||
if(toins < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1]) {
|
||||
if (toins < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1]) {
|
||||
|
||||
/* insertion-sort it into the maxbuf */
|
||||
for(j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
|
||||
for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) {
|
||||
/* found where it fits */
|
||||
if(toins < jb->hist_minbuf[j]) {
|
||||
if (toins < jb->hist_minbuf[j]) {
|
||||
/* move over */
|
||||
memmove(jb->hist_minbuf+j+1,jb->hist_minbuf+j, (JB_HISTORY_MAXBUF_SZ-(j+1)) * sizeof(long));
|
||||
/* insert */
|
||||
@@ -217,14 +220,14 @@ static void history_calc_maxbuf(jitterbuf *jb)
|
||||
}
|
||||
}
|
||||
|
||||
if(0) {
|
||||
if (0) {
|
||||
int k;
|
||||
fprintf(stderr, "toins = %ld\n", toins);
|
||||
fprintf(stderr, "maxbuf =");
|
||||
for(k=0;k<JB_HISTORY_MAXBUF_SZ;k++)
|
||||
for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++)
|
||||
fprintf(stderr, "%ld ", jb->hist_maxbuf[k]);
|
||||
fprintf(stderr, "\nminbuf =");
|
||||
for(k=0;k<JB_HISTORY_MAXBUF_SZ;k++)
|
||||
for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++)
|
||||
fprintf(stderr, "%ld ", jb->hist_minbuf[k]);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
@@ -239,7 +242,7 @@ static void history_get(jitterbuf *jb)
|
||||
int index;
|
||||
int count;
|
||||
|
||||
if(!jb->hist_maxbuf_valid)
|
||||
if (!jb->hist_maxbuf_valid)
|
||||
history_calc_maxbuf(jb);
|
||||
|
||||
/* count is how many items in history we're examining */
|
||||
@@ -249,10 +252,11 @@ static void history_get(jitterbuf *jb)
|
||||
index = count * JB_HISTORY_DROPPCT / 100;
|
||||
|
||||
/* sanity checks for index */
|
||||
if(index > (JB_HISTORY_MAXBUF_SZ - 1)) index = JB_HISTORY_MAXBUF_SZ - 1;
|
||||
if (index > (JB_HISTORY_MAXBUF_SZ - 1))
|
||||
index = JB_HISTORY_MAXBUF_SZ - 1;
|
||||
|
||||
|
||||
if(index < 0) {
|
||||
if (index < 0) {
|
||||
jb->info.min = 0;
|
||||
jb->info.jitter = 0;
|
||||
return;
|
||||
@@ -280,13 +284,13 @@ static void queue_put(jitterbuf *jb, void *data, int type, long ms, long ts)
|
||||
jb_frame *p;
|
||||
|
||||
frame = jb->free;
|
||||
if(frame) {
|
||||
if (frame) {
|
||||
jb->free = frame->next;
|
||||
} else {
|
||||
frame = malloc(sizeof(jb_frame));
|
||||
}
|
||||
|
||||
if(!frame) {
|
||||
if (!frame) {
|
||||
jb_err("cannot allocate frame\n");
|
||||
return;
|
||||
}
|
||||
@@ -303,12 +307,10 @@ static void queue_put(jitterbuf *jb, void *data, int type, long ms, long ts)
|
||||
* jb->frames->prev points to the highest ts
|
||||
*/
|
||||
|
||||
if(!jb->frames) { /* queue is empty */
|
||||
if (!jb->frames) { /* queue is empty */
|
||||
jb->frames = frame;
|
||||
frame->next = frame;
|
||||
frame->prev = frame;
|
||||
} else if(ts < jb->frames->ts) {
|
||||
frame->next = jb->frames;
|
||||
frame->prev = jb->frames->prev;
|
||||
|
||||
frame->next->prev = frame;
|
||||
@@ -319,9 +321,9 @@ static void queue_put(jitterbuf *jb, void *data, int type, long ms, long ts)
|
||||
p = jb->frames;
|
||||
|
||||
/* frame is out of order */
|
||||
if(ts < p->prev->ts) jb->info.frames_ooo++;
|
||||
if (ts < p->prev->ts) jb->info.frames_ooo++;
|
||||
|
||||
while(ts < p->prev->ts && p->prev != jb->frames)
|
||||
while (ts < p->prev->ts && p->prev != jb->frames)
|
||||
p = p->prev;
|
||||
|
||||
frame->next = p;
|
||||
@@ -334,14 +336,18 @@ static void queue_put(jitterbuf *jb, void *data, int type, long ms, long ts)
|
||||
|
||||
static long queue_next(jitterbuf *jb)
|
||||
{
|
||||
if(jb->frames) return jb->frames->ts;
|
||||
else return -1;
|
||||
if (jb->frames)
|
||||
return jb->frames->ts;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
static long queue_last(jitterbuf *jb)
|
||||
{
|
||||
if(jb->frames) return jb->frames->prev->ts;
|
||||
else return -1;
|
||||
if (jb->frames)
|
||||
return jb->frames->prev->ts;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
static jb_frame *_queue_get(jitterbuf *jb, long ts, int all)
|
||||
@@ -349,17 +355,17 @@ static jb_frame *_queue_get(jitterbuf *jb, long ts, int all)
|
||||
jb_frame *frame;
|
||||
frame = jb->frames;
|
||||
|
||||
if(!frame)
|
||||
if (!frame)
|
||||
return NULL;
|
||||
|
||||
/*jb_warn("queue_get: ASK %ld FIRST %ld\n", ts, frame->ts); */
|
||||
|
||||
if(all || ts > frame->ts) {
|
||||
if (all || ts > frame->ts) {
|
||||
/* remove this frame */
|
||||
frame->prev->next = frame->next;
|
||||
frame->next->prev = frame->prev;
|
||||
|
||||
if(frame->next == frame)
|
||||
if (frame->next == frame)
|
||||
jb->frames = NULL;
|
||||
else
|
||||
jb->frames = frame->next;
|
||||
@@ -392,15 +398,16 @@ static jb_frame *queue_getall(jitterbuf *jb)
|
||||
/* some diagnostics */
|
||||
static void jb_dbginfo(jitterbuf *jb)
|
||||
{
|
||||
if(dbgf == NULL) return;
|
||||
if (dbgf == NULL)
|
||||
return;
|
||||
|
||||
jb_dbg("\njb info: fin=%ld fout=%ld flate=%ld flost=%ld fdrop=%ld fcur=%ld\n",
|
||||
jb->info.frames_in, jb->info.frames_out, jb->info.frames_late, jb->info.frames_lost, jb->info.frames_dropped, jb->info.frames_cur);
|
||||
|
||||
jb_dbg(" jitter=%ld current=%ld target=%ld min=%ld sil=%d len=%d len/fcur=%ld\n",
|
||||
jb_dbg("jitter=%ld current=%ld target=%ld min=%ld sil=%d len=%d len/fcur=%ld\n",
|
||||
jb->info.jitter, jb->info.current, jb->info.target, jb->info.min, jb->info.silence, jb->info.current - jb->info.min,
|
||||
jb->info.frames_cur ? (jb->info.current - jb->info.min)/jb->info.frames_cur : -8);
|
||||
if(jb->info.frames_in > 0)
|
||||
if (jb->info.frames_in > 0)
|
||||
jb_dbg("jb info: Loss PCT = %ld%%, Late PCT = %ld%%\n",
|
||||
jb->info.frames_lost * 100/(jb->info.frames_in + jb->info.frames_lost),
|
||||
jb->info.frames_late * 100/jb->info.frames_in);
|
||||
@@ -418,12 +425,12 @@ static void jb_chkqueue(jitterbuf *jb)
|
||||
int i=0;
|
||||
jb_frame *p = jb->frames;
|
||||
|
||||
if(!p) {
|
||||
if (!p) {
|
||||
return;
|
||||
}
|
||||
|
||||
do {
|
||||
if(p->next == NULL) {
|
||||
if (p->next == NULL) {
|
||||
jb_err("Queue is BROKEN at item [%d]", i);
|
||||
}
|
||||
i++;
|
||||
@@ -438,7 +445,7 @@ static void jb_dbgqueue(jitterbuf *jb)
|
||||
|
||||
jb_dbg("queue: ");
|
||||
|
||||
if(!p) {
|
||||
if (!p) {
|
||||
jb_dbg("EMPTY\n");
|
||||
return;
|
||||
}
|
||||
@@ -458,7 +465,7 @@ int jb_put(jitterbuf *jb, void *data, int type, long ms, long ts, long now)
|
||||
|
||||
jb->info.frames_in++;
|
||||
|
||||
if(type == JB_TYPE_VOICE) {
|
||||
if (type == JB_TYPE_VOICE) {
|
||||
/* presently, I'm only adding VOICE frames to history and drift calculations; mostly because with the
|
||||
* IAX integrations, I'm sending retransmitted control frames with their awkward timestamps through */
|
||||
history_put(jb,ts,now);
|
||||
@@ -475,7 +482,7 @@ static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now)
|
||||
jb_frame *frame;
|
||||
long diff;
|
||||
|
||||
/*if((now - jb_next(jb)) > 2 * jb->info.last_voice_ms) jb_warn("SCHED: %ld", (now - jb_next(jb))); */
|
||||
/*if ((now - jb_next(jb)) > 2 * jb->info.last_voice_ms) jb_warn("SCHED: %ld", (now - jb_next(jb))); */
|
||||
/* get jitter info */
|
||||
history_get(jb);
|
||||
|
||||
@@ -484,7 +491,7 @@ static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now)
|
||||
jb->info.target = jb->info.jitter + jb->info.min + 2 * jb->info.last_voice_ms;
|
||||
|
||||
/* if a hard clamp was requested, use it */
|
||||
if((jb->info.max_jitterbuf) && ((jb->info.target - jb->info.min) > jb->info.max_jitterbuf)) {
|
||||
if ((jb->info.max_jitterbuf) && ((jb->info.target - jb->info.min) > jb->info.max_jitterbuf)) {
|
||||
jb_dbg("clamping target from %d to %d\n", (jb->info.target - jb->info.min), jb->info.max_jitterbuf);
|
||||
jb->info.target = jb->info.min + jb->info.max_jitterbuf;
|
||||
}
|
||||
@@ -498,13 +505,14 @@ static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now)
|
||||
jb->info.last_voice_ts += jb->info.last_voice_ms;
|
||||
|
||||
/* let's work on non-silent case first */
|
||||
if(!jb->info.silence) {
|
||||
if (!jb->info.silence) {
|
||||
/* we want to grow */
|
||||
if( (diff > 0) &&
|
||||
if ((diff > 0) &&
|
||||
/* we haven't grown in 2 frames' length */
|
||||
(((jb->info.last_adjustment + 2 * jb->info.last_voice_ms ) < now) ||
|
||||
/* we need to grow more than the "length" we have left */
|
||||
(diff > queue_last(jb) - queue_next(jb)) ) ) {
|
||||
|
||||
jb->info.current += jb->info.last_voice_ms;
|
||||
jb->info.last_adjustment = now;
|
||||
jb_dbg("G");
|
||||
@@ -514,11 +522,11 @@ static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now)
|
||||
frame = queue_get(jb, jb->info.last_voice_ts - jb->info.current);
|
||||
|
||||
/* not a voice frame; just return it. */
|
||||
if(frame && frame->type != JB_TYPE_VOICE) {
|
||||
if (frame && frame->type != JB_TYPE_VOICE) {
|
||||
/* rewind last_voice_ts, since this isn't voice */
|
||||
jb->info.last_voice_ts -= jb->info.last_voice_ms;
|
||||
|
||||
if(frame->type == JB_TYPE_SILENCE)
|
||||
if (frame->type == JB_TYPE_SILENCE)
|
||||
jb->info.silence = 1;
|
||||
|
||||
*frameout = *frame;
|
||||
@@ -529,7 +537,7 @@ static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now)
|
||||
|
||||
|
||||
/* voice frame is late */
|
||||
if(frame && frame->ts + jb->info.current < jb->info.last_voice_ts - jb->info.last_voice_ms ) {
|
||||
if (frame && frame->ts + jb->info.current < jb->info.last_voice_ts - jb->info.last_voice_ms ) {
|
||||
*frameout = *frame;
|
||||
/* rewind last_voice, since we're just dumping */
|
||||
jb->info.last_voice_ts -= jb->info.last_voice_ms;
|
||||
@@ -544,12 +552,12 @@ static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now)
|
||||
}
|
||||
|
||||
/* keep track of frame sizes, to allow for variable sized-frames */
|
||||
if(frame && frame->ms > 0) {
|
||||
if (frame && frame->ms > 0) {
|
||||
jb->info.last_voice_ms = frame->ms;
|
||||
}
|
||||
|
||||
/* we want to shrink; shrink at 1 frame / 500ms */
|
||||
if(diff < -2 * jb->info.last_voice_ms &&
|
||||
if (diff < -2 * jb->info.last_voice_ms &&
|
||||
((!frame && jb->info.last_adjustment + 80 < now) ||
|
||||
(jb->info.last_adjustment + 500 < now))) {
|
||||
|
||||
@@ -558,7 +566,7 @@ static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now)
|
||||
jb->info.current -= jb->info.last_voice_ms;
|
||||
jb->info.last_adjustment = now;
|
||||
|
||||
if(frame) {
|
||||
if (frame) {
|
||||
*frameout = *frame;
|
||||
jb->info.frames_out++;
|
||||
decrement_losspct(jb);
|
||||
@@ -573,7 +581,7 @@ static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now)
|
||||
}
|
||||
|
||||
/* lost frame */
|
||||
if(!frame) {
|
||||
if (!frame) {
|
||||
/* this is a bit of a hack for now, but if we're close to
|
||||
* target, and we find a missing frame, it makes sense to
|
||||
* grow, because the frame might just be a bit late;
|
||||
@@ -589,7 +597,7 @@ static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now)
|
||||
* But, this still seemed like a good idea, except that it ended up making a single actual
|
||||
* lost frame get interpolated two or more times, when there was "room" to grow, so it might
|
||||
* be a bit of a bad idea overall */
|
||||
/*if(diff > -1 * jb->info.last_voice_ms) {
|
||||
/*if (diff > -1 * jb->info.last_voice_ms) {
|
||||
jb->info.current += jb->info.last_voice_ms;
|
||||
jb->info.last_adjustment = now;
|
||||
jb_warn("g");
|
||||
@@ -616,10 +624,10 @@ static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now)
|
||||
/* jb->info.silence = 0; */
|
||||
|
||||
frame = queue_get(jb, now - jb->info.current);
|
||||
if(!frame) {
|
||||
if (!frame) {
|
||||
return JB_NOFRAME;
|
||||
}
|
||||
if(frame && frame->type == JB_TYPE_VOICE) {
|
||||
if (frame && frame->type == JB_TYPE_VOICE) {
|
||||
/* try setting current to target right away here */
|
||||
jb->info.current = jb->info.target;
|
||||
jb->info.silence = 0;
|
||||
@@ -637,13 +645,14 @@ static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now)
|
||||
|
||||
long jb_next(jitterbuf *jb)
|
||||
{
|
||||
if(jb->info.silence) {
|
||||
if (jb->info.silence) {
|
||||
long next = queue_next(jb);
|
||||
if(next > 0) {
|
||||
if (next > 0) {
|
||||
history_get(jb);
|
||||
return next + jb->info.target;
|
||||
}
|
||||
else return JB_LONGMAX;
|
||||
else
|
||||
return JB_LONGMAX;
|
||||
} else {
|
||||
return jb->info.last_voice_ts + jb->info.last_voice_ms;
|
||||
}
|
||||
@@ -656,10 +665,12 @@ int jb_get(jitterbuf *jb, jb_frame *frameout, long now)
|
||||
static int lastts=0;
|
||||
int thists = ((ret == JB_OK) || (ret == JB_DROP)) ? frameout->ts : 0;
|
||||
jb_warn("jb_get(%x,%x,%ld) = %d (%d)\n", jb, frameout, now, ret, thists);
|
||||
if(thists && thists < lastts) jb_warn("XXXX timestamp roll-back!!!\n");
|
||||
if (thists && thists < lastts) jb_warn("XXXX timestamp roll-back!!!\n");
|
||||
lastts = thists;
|
||||
#endif
|
||||
if(ret == JB_INTERP) frameout->ms = jb->info.last_voice_ms;
|
||||
if(ret == JB_INTERP)
|
||||
frameout->ms = jb->info.last_voice_ms;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -668,7 +679,7 @@ int jb_getall(jitterbuf *jb, jb_frame *frameout)
|
||||
jb_frame *frame;
|
||||
frame = queue_getall(jb);
|
||||
|
||||
if(!frame) {
|
||||
if (!frame) {
|
||||
return JB_NOFRAME;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user