jitterbuffer: cleanup timer handling

This commit is contained in:
Wim Taymans 2013-08-01 11:49:10 +02:00
parent 9d88ac9cbb
commit 7e43dba19b

View file

@ -1528,51 +1528,75 @@ apply_offset (GstRtpJitterBuffer * jitterbuffer, GstClockTime timestamp)
#define GST_FLOW_WAIT GST_FLOW_CUSTOM_SUCCESS #define GST_FLOW_WAIT GST_FLOW_CUSTOM_SUCCESS
static TimerData * static TimerData *
find_timer (GstRtpJitterBuffer * jitterbuffer, TimerType type, find_timer (GstRtpJitterBuffer * jitterbuffer, TimerType type, guint16 seqnum)
guint16 seqnum, gboolean * created)
{ {
GstRtpJitterBufferPrivate *priv = jitterbuffer->priv; GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
TimerData *timer; TimerData *timer = NULL;
gint i, len; gint i, len;
gboolean found = FALSE;
len = priv->timers->len; len = priv->timers->len;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
timer = &g_array_index (priv->timers, TimerData, i); TimerData *test = &g_array_index (priv->timers, TimerData, i);
if (timer->seqnum == seqnum && timer->type == type) { if (test->seqnum == seqnum && test->type == type) {
found = TRUE; timer = test;
break; break;
} }
} }
if (!found) { return timer;
/* not found, create */ }
g_array_set_size (priv->timers, len + 1);
timer = &g_array_index (priv->timers, TimerData, len); static TimerData *
timer->idx = len; add_timer (GstRtpJitterBuffer * jitterbuffer, TimerType type,
timer->type = type; guint16 seqnum, GstClockTime timeout)
timer->seqnum = seqnum; {
} GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
if (created) TimerData *timer;
*created = !found; gint len;
GST_DEBUG_OBJECT (jitterbuffer,
"add timer for seqnum %d to %" GST_TIME_FORMAT,
seqnum, GST_TIME_ARGS (timeout));
len = priv->timers->len;
g_array_set_size (priv->timers, len + 1);
timer = &g_array_index (priv->timers, TimerData, len);
timer->idx = len;
timer->type = type;
timer->seqnum = seqnum;
timer->timeout = timeout;
return timer; return timer;
} }
static GstFlowReturn static void
reschedule_timer (GstRtpJitterBuffer * jitterbuffer, TimerData * timer,
guint16 seqnum, GstClockTime timeout)
{
if (timer->seqnum == seqnum && timer->timeout == timeout)
return;
GST_DEBUG_OBJECT (jitterbuffer,
"replace timer for seqnum %d->%d to %" GST_TIME_FORMAT,
timer->seqnum, seqnum, GST_TIME_ARGS (timeout));
timer->timeout = timeout;
timer->seqnum = seqnum;
}
static TimerData *
set_timer (GstRtpJitterBuffer * jitterbuffer, TimerType type, set_timer (GstRtpJitterBuffer * jitterbuffer, TimerType type,
guint16 seqnum, GstClockTime timeout) guint16 seqnum, GstClockTime timeout)
{ {
TimerData *timer; TimerData *timer;
GST_DEBUG_OBJECT (jitterbuffer,
"set timer for seqnum %d to %" GST_TIME_FORMAT, seqnum,
GST_TIME_ARGS (timeout));
/* find the seqnum timer */ /* find the seqnum timer */
timer = find_timer (jitterbuffer, type, seqnum, NULL); timer = find_timer (jitterbuffer, type, seqnum);
timer->timeout = timeout; if (timer == NULL) {
timer = add_timer (jitterbuffer, type, seqnum, timeout);
return GST_FLOW_WAIT; } else {
reschedule_timer (jitterbuffer, timer, seqnum, timeout);
}
return timer;
} }
static void static void
@ -1823,7 +1847,8 @@ again:
/* we don't know what the next_seqnum should be, wait for the last /* we don't know what the next_seqnum should be, wait for the last
* possible moment to push this buffer, maybe we get an earlier seqnum * possible moment to push this buffer, maybe we get an earlier seqnum
* while we wait */ * while we wait */
result = set_timer (jitterbuffer, TIMER_TYPE_DEADLINE, seqnum, dts); set_timer (jitterbuffer, TIMER_TYPE_DEADLINE, seqnum, dts);
result = GST_FLOW_WAIT;
} else { } else {
/* else calculate GAP */ /* else calculate GAP */
gap = gst_rtp_buffer_compare_seqnum (next_seqnum, seqnum); gap = gst_rtp_buffer_compare_seqnum (next_seqnum, seqnum);
@ -1846,7 +1871,8 @@ again:
/* packet missing, estimate when we should ultimately push this packet */ /* packet missing, estimate when we should ultimately push this packet */
dts = estimate_dts (jitterbuffer, dts, gap); dts = estimate_dts (jitterbuffer, dts, gap);
/* and set a timer for it */ /* and set a timer for it */
result = set_timer (jitterbuffer, TIMER_TYPE_LOST, next_seqnum, dts); set_timer (jitterbuffer, TIMER_TYPE_LOST, next_seqnum, dts);
result = GST_FLOW_WAIT;
} }
} }
return result; return result;