#include <string.h>
#include <stdlib.h>
#include <gst/rtp/gstrtpbuffer.h>
#include <gst/rtp/gstrtcpbuffer.h>
#include "rtpjitterbuffer.h"
GST_DEBUG_CATEGORY_STATIC (rtp_jitter_buffer_debug);
#define GST_CAT_DEFAULT rtp_jitter_buffer_debug
#define MAX_WINDOW RTP_JITTER_BUFFER_MAX_WINDOW
#define MAX_TIME (2 * GST_SECOND)
enum
{
LAST_SIGNAL
};
enum
{
PROP_0
};
static void rtp_jitter_buffer_finalize (GObject * object);
GType
rtp_jitter_buffer_mode_get_type (void)
{
static GType jitter_buffer_mode_type = 0;
static const GEnumValue jitter_buffer_modes[] = {
{RTP_JITTER_BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
{RTP_JITTER_BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
{RTP_JITTER_BUFFER_MODE_BUFFER, "Do low/high watermark buffering",
"buffer"},
{RTP_JITTER_BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks",
"synced"},
{0, NULL, NULL},
};
if (!jitter_buffer_mode_type) {
jitter_buffer_mode_type =
g_enum_register_static ("TsRTPJitterBufferMode", jitter_buffer_modes);
}
return jitter_buffer_mode_type;
}
G_DEFINE_TYPE (RTPJitterBuffer, rtp_jitter_buffer, G_TYPE_OBJECT);
static void
rtp_jitter_buffer_class_init (RTPJitterBufferClass * klass)
{
GObjectClass *gobject_class;
gobject_class = (GObjectClass *) klass;
gobject_class->finalize = rtp_jitter_buffer_finalize;
GST_DEBUG_CATEGORY_INIT (rtp_jitter_buffer_debug, "rtpjitterbuffer", 0,
"RTP Jitter Buffer");
}
static void
rtp_jitter_buffer_init (RTPJitterBuffer * jbuf)
{
g_mutex_init (&jbuf->clock_lock);
jbuf->packets = g_queue_new ();
jbuf->mode = RTP_JITTER_BUFFER_MODE_SLAVE;
rtp_jitter_buffer_reset_skew (jbuf);
}
static void
rtp_jitter_buffer_finalize (GObject * object)
{
RTPJitterBuffer *jbuf;
RTPJitterBufferItem *item;
jbuf = RTP_JITTER_BUFFER_CAST (object);
if (jbuf->media_clock_synced_id)
g_signal_handler_disconnect (jbuf->media_clock,
jbuf->media_clock_synced_id);
if (jbuf->media_clock) {
gst_clock_set_master (jbuf->media_clock, NULL);
gst_object_unref (jbuf->media_clock);
}
if (jbuf->pipeline_clock)
gst_object_unref (jbuf->pipeline_clock);
while ((item = (RTPJitterBufferItem *) g_queue_pop_head_link (jbuf->packets))) {
if (item->data)
gst_mini_object_unref (item->data);
g_slice_free (RTPJitterBufferItem, item);
}
g_queue_free (jbuf->packets);
g_mutex_clear (&jbuf->clock_lock);
G_OBJECT_CLASS (rtp_jitter_buffer_parent_class)->finalize (object);
}
RTPJitterBuffer *
rtp_jitter_buffer_new (void)
{
RTPJitterBuffer *jbuf;
jbuf = g_object_new (RTP_TYPE_JITTER_BUFFER, NULL);
return jbuf;
}
RTPJitterBufferMode
rtp_jitter_buffer_get_mode (RTPJitterBuffer * jbuf)
{
return jbuf->mode;
}
void
rtp_jitter_buffer_set_mode (RTPJitterBuffer * jbuf, RTPJitterBufferMode mode)
{
jbuf->mode = mode;
}
GstClockTime
rtp_jitter_buffer_get_delay (RTPJitterBuffer * jbuf)
{
g_print ("%p getting delay (%" G_GUINT64_FORMAT ")\n", jbuf, jbuf->delay);
return jbuf->delay;
}
void
rtp_jitter_buffer_set_delay (RTPJitterBuffer * jbuf, GstClockTime delay)
{
jbuf->delay = delay;
jbuf->low_level = (delay * 15) / 100;
jbuf->high_level = (delay * 90) / 100;
GST_DEBUG ("delay %" GST_TIME_FORMAT ", min %" GST_TIME_FORMAT ", max %"
GST_TIME_FORMAT, GST_TIME_ARGS (jbuf->delay),
GST_TIME_ARGS (jbuf->low_level), GST_TIME_ARGS (jbuf->high_level));
}
void
rtp_jitter_buffer_set_clock_rate (RTPJitterBuffer * jbuf, guint32 clock_rate)
{
if (jbuf->clock_rate != clock_rate) {
GST_DEBUG ("Clock rate changed from %" G_GUINT32_FORMAT " to %"
G_GUINT32_FORMAT, jbuf->clock_rate, clock_rate);
jbuf->clock_rate = clock_rate;
rtp_jitter_buffer_reset_skew (jbuf);
}
}
guint32
rtp_jitter_buffer_get_clock_rate (RTPJitterBuffer * jbuf)
{
return jbuf->clock_rate;
}
static void
media_clock_synced_cb (GstClock * clock G_GNUC_UNUSED,
gboolean synced G_GNUC_UNUSED, RTPJitterBuffer * jbuf)
{
GstClockTime internal, external;
g_mutex_lock (&jbuf->clock_lock);
if (jbuf->pipeline_clock) {
internal = gst_clock_get_internal_time (jbuf->media_clock);
external = gst_clock_get_time (jbuf->pipeline_clock);
gst_clock_set_calibration (jbuf->media_clock, internal, external, 1, 1);
}
g_mutex_unlock (&jbuf->clock_lock);
}
void
rtp_jitter_buffer_set_media_clock (RTPJitterBuffer * jbuf, GstClock * clock,
guint64 clock_offset)
{
g_mutex_lock (&jbuf->clock_lock);
if (jbuf->media_clock) {
if (jbuf->media_clock_synced_id)
g_signal_handler_disconnect (jbuf->media_clock,
jbuf->media_clock_synced_id);
jbuf->media_clock_synced_id = 0;
gst_object_unref (jbuf->media_clock);
}
jbuf->media_clock = clock;
jbuf->media_clock_offset = clock_offset;
if (jbuf->pipeline_clock && jbuf->media_clock &&
jbuf->pipeline_clock != jbuf->media_clock) {
jbuf->media_clock_synced_id =
g_signal_connect (jbuf->media_clock, "synced",
G_CALLBACK (media_clock_synced_cb), jbuf);
if (gst_clock_is_synced (jbuf->media_clock)) {
GstClockTime internal, external;
internal = gst_clock_get_internal_time (jbuf->media_clock);
external = gst_clock_get_time (jbuf->pipeline_clock);
gst_clock_set_calibration (jbuf->media_clock, internal, external, 1, 1);
}
gst_clock_set_master (jbuf->media_clock, jbuf->pipeline_clock);
}
g_mutex_unlock (&jbuf->clock_lock);
}
void
rtp_jitter_buffer_set_pipeline_clock (RTPJitterBuffer * jbuf, GstClock * clock)
{
g_mutex_lock (&jbuf->clock_lock);
if (jbuf->pipeline_clock)
gst_object_unref (jbuf->pipeline_clock);
jbuf->pipeline_clock = clock ? gst_object_ref (clock) : NULL;
if (jbuf->pipeline_clock && jbuf->media_clock &&
jbuf->pipeline_clock != jbuf->media_clock) {
if (gst_clock_is_synced (jbuf->media_clock)) {
GstClockTime internal, external;
internal = gst_clock_get_internal_time (jbuf->media_clock);
external = gst_clock_get_time (jbuf->pipeline_clock);
gst_clock_set_calibration (jbuf->media_clock, internal, external, 1, 1);
}
gst_clock_set_master (jbuf->media_clock, jbuf->pipeline_clock);
}
g_mutex_unlock (&jbuf->clock_lock);
}
gboolean
rtp_jitter_buffer_get_rfc7273_sync (RTPJitterBuffer * jbuf)
{
return jbuf->rfc7273_sync;
}
void
rtp_jitter_buffer_set_rfc7273_sync (RTPJitterBuffer * jbuf,
gboolean rfc7273_sync)
{
jbuf->rfc7273_sync = rfc7273_sync;
}
void
rtp_jitter_buffer_reset_skew (RTPJitterBuffer * jbuf)
{
jbuf->base_time = -1;
jbuf->base_rtptime = -1;
jbuf->base_extrtp = -1;
jbuf->media_clock_base_time = -1;
jbuf->ext_rtptime = -1;
jbuf->last_rtptime = -1;
jbuf->window_pos = 0;
jbuf->window_filling = TRUE;
jbuf->window_min = 0;
jbuf->skew = 0;
jbuf->prev_send_diff = -1;
jbuf->prev_out_time = -1;
jbuf->need_resync = TRUE;
GST_DEBUG ("reset skew correction");
}
void
rtp_jitter_buffer_disable_buffering (RTPJitterBuffer * jbuf, gboolean disabled)
{
jbuf->buffering_disabled = disabled;
}
static void
rtp_jitter_buffer_resync (RTPJitterBuffer * jbuf, GstClockTime time,
GstClockTime gstrtptime, guint64 ext_rtptime, gboolean reset_skew)
{
jbuf->base_time = time;
jbuf->media_clock_base_time = -1;
jbuf->base_rtptime = gstrtptime;
jbuf->base_extrtp = ext_rtptime;
jbuf->prev_out_time = -1;
jbuf->prev_send_diff = -1;
if (reset_skew) {
jbuf->window_filling = TRUE;
jbuf->window_pos = 0;
jbuf->window_min = 0;
jbuf->window_size = 0;
jbuf->skew = 0;
}
jbuf->need_resync = FALSE;
}
static guint64
get_buffer_level (RTPJitterBuffer * jbuf)
{
RTPJitterBufferItem *high_buf = NULL, *low_buf = NULL;
guint64 level;
high_buf = (RTPJitterBufferItem *) g_queue_peek_tail_link (jbuf->packets);
while (high_buf) {
if (high_buf->dts != GST_CLOCK_TIME_NONE
|| high_buf->pts != GST_CLOCK_TIME_NONE)
break;
high_buf = (RTPJitterBufferItem *) g_list_previous (high_buf);
}
low_buf = (RTPJitterBufferItem *) g_queue_peek_head_link (jbuf->packets);
while (low_buf) {
if (low_buf->dts != GST_CLOCK_TIME_NONE
|| low_buf->pts != GST_CLOCK_TIME_NONE)
break;
low_buf = (RTPJitterBufferItem *) g_list_next (low_buf);
}
if (!high_buf || !low_buf || high_buf == low_buf) {
level = 0;
} else {
guint64 high_ts, low_ts;
high_ts =
high_buf->dts != GST_CLOCK_TIME_NONE ? high_buf->dts : high_buf->pts;
low_ts = low_buf->dts != GST_CLOCK_TIME_NONE ? low_buf->dts : low_buf->pts;
if (high_ts > low_ts)
level = high_ts - low_ts;
else
level = 0;
GST_LOG_OBJECT (jbuf,
"low %" GST_TIME_FORMAT " high %" GST_TIME_FORMAT " level %"
G_GUINT64_FORMAT, GST_TIME_ARGS (low_ts), GST_TIME_ARGS (high_ts),
level);
}
return level;
}
static void
update_buffer_level (RTPJitterBuffer * jbuf, gint * percent)
{
gboolean post = FALSE;
guint64 level;
level = get_buffer_level (jbuf);
GST_DEBUG ("buffer level %" GST_TIME_FORMAT, GST_TIME_ARGS (level));
if (jbuf->buffering_disabled) {
GST_DEBUG ("buffering is disabled");
level = jbuf->high_level;
}
if (jbuf->buffering) {
post = TRUE;
if (level >= jbuf->high_level) {
GST_DEBUG ("buffering finished");
jbuf->buffering = FALSE;
}
} else {
if (level < jbuf->low_level) {
GST_DEBUG ("buffering started");
jbuf->buffering = TRUE;
post = TRUE;
}
}
if (post) {
gint perc;
if (jbuf->buffering && (jbuf->high_level != 0)) {
perc = (level * 100 / jbuf->high_level);
perc = MIN (perc, 100);
} else {
perc = 100;
}
if (percent)
*percent = perc;
GST_DEBUG ("buffering %d", perc);
}
}
static GstClockTime
calculate_skew (RTPJitterBuffer * jbuf, guint64 ext_rtptime,
GstClockTime gstrtptime, GstClockTime time, gint gap, gboolean is_rtx)
{
guint64 send_diff, recv_diff;
gint64 delta;
gint64 old;
guint pos, i;
GstClockTime out_time;
guint64 slope;
send_diff = gstrtptime - jbuf->base_rtptime;
if (time == GST_CLOCK_TIME_NONE || jbuf->base_time == GST_CLOCK_TIME_NONE
|| is_rtx)
goto no_skew;
recv_diff = time - jbuf->base_time;
delta = ((gint64) recv_diff) - ((gint64) send_diff);
if (recv_diff > 0)
slope = (send_diff * 8) / recv_diff;
else
slope = 8;
GST_DEBUG ("time %" GST_TIME_FORMAT ", base %" GST_TIME_FORMAT ", recv_diff %"
GST_TIME_FORMAT ", slope %" G_GUINT64_FORMAT, GST_TIME_ARGS (time),
GST_TIME_ARGS (jbuf->base_time), GST_TIME_ARGS (recv_diff), slope);
if (ABS (delta - jbuf->skew) > GST_SECOND) {
GST_WARNING ("delta - skew: %" GST_TIME_FORMAT " too big, reset skew",
GST_TIME_ARGS (ABS (delta - jbuf->skew)));
rtp_jitter_buffer_resync (jbuf, time, gstrtptime, ext_rtptime, TRUE);
send_diff = 0;
delta = 0;
gap = 0;
}
if (G_UNLIKELY (gap != 0))
goto no_skew;
pos = jbuf->window_pos;
if (G_UNLIKELY (jbuf->window_filling)) {
GST_DEBUG ("filling %d, delta %" G_GINT64_FORMAT, pos, delta);
jbuf->window[pos++] = delta;
if (G_UNLIKELY (pos == 1 || delta < jbuf->window_min))
jbuf->window_min = delta;
if (G_UNLIKELY (send_diff >= MAX_TIME || pos >= MAX_WINDOW)) {
jbuf->window_size = pos;
GST_DEBUG ("min %" G_GINT64_FORMAT, jbuf->window_min);
jbuf->skew = jbuf->window_min;
jbuf->window_filling = FALSE;
} else {
gint perc_time, perc_window, perc;
perc_time = send_diff * 100 / MAX_TIME;
perc_window = pos * 100 / MAX_WINDOW;
perc = MAX (perc_time, perc_window);
perc = perc * perc;
jbuf->skew =
(perc * jbuf->window_min + ((10000 - perc) * jbuf->skew)) / 10000;
jbuf->window_size = pos + 1;
}
} else {
old = jbuf->window[pos];
jbuf->window[pos++] = delta;
if (G_UNLIKELY (delta <= jbuf->window_min)) {
jbuf->window_min = delta;
} else if (G_UNLIKELY (old == jbuf->window_min)) {
gint64 min = G_MAXINT64;
for (i = 0; i < jbuf->window_size; i++) {
if (jbuf->window[i] == old) {
min = old;
break;
}
if (jbuf->window[i] < min)
min = jbuf->window[i];
}
jbuf->window_min = min;
}
jbuf->skew = (jbuf->window_min + (124 * jbuf->skew)) / 125;
GST_DEBUG ("delta %" G_GINT64_FORMAT ", new min: %" G_GINT64_FORMAT,
delta, jbuf->window_min);
}
if (G_UNLIKELY (pos >= jbuf->window_size))
pos = 0;
jbuf->window_pos = pos;
no_skew:
if (jbuf->base_time != GST_CLOCK_TIME_NONE) {
out_time = jbuf->base_time + send_diff;
if (jbuf->skew < 0 && out_time < (GstClockTime) - jbuf->skew) {
out_time = 0;
} else {
out_time += jbuf->skew;
}
} else
out_time = -1;
GST_DEBUG ("skew %" G_GINT64_FORMAT ", out %" GST_TIME_FORMAT,
jbuf->skew, GST_TIME_ARGS (out_time));
return out_time;
}
static void
queue_do_insert (RTPJitterBuffer * jbuf, GList * list, GList * item)
{
GQueue *queue = jbuf->packets;
if (G_LIKELY (list)) {
item->prev = list;
item->next = list->next;
list->next = item;
} else {
item->prev = NULL;
item->next = queue->head;
queue->head = item;
}
if (item->next)
item->next->prev = item;
else
queue->tail = item;
queue->length++;
}
GstClockTime
rtp_jitter_buffer_calculate_pts (RTPJitterBuffer * jbuf, GstClockTime dts,
gboolean estimated_dts, guint32 rtptime, GstClockTime base_time,
gint gap, gboolean is_rtx)
{
guint64 ext_rtptime;
GstClockTime gstrtptime, pts;
GstClock *media_clock, *pipeline_clock;
guint64 media_clock_offset;
gboolean rfc7273_mode;
if (GST_CLOCK_TIME_IS_VALID (dts) && !estimated_dts &&
jbuf->mode != RTP_JITTER_BUFFER_MODE_SLAVE &&
jbuf->base_time != GST_CLOCK_TIME_NONE
&& jbuf->last_rtptime != GST_CLOCK_TIME_NONE) {
GstClockTime ext_rtptime = jbuf->ext_rtptime;
ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
if (ext_rtptime > jbuf->last_rtptime + 3 * jbuf->clock_rate ||
ext_rtptime + 3 * jbuf->clock_rate < jbuf->last_rtptime) {
if (!is_rtx) {
GST_WARNING ("rtp delta too big, reset skew");
rtp_jitter_buffer_reset_skew (jbuf);
} else {
GST_WARNING ("rtp delta too big: ignore rtx packet");
media_clock = NULL;
pipeline_clock = NULL;
pts = GST_CLOCK_TIME_NONE;
goto done;
}
}
}
ext_rtptime = gst_rtp_buffer_ext_timestamp (&jbuf->ext_rtptime, rtptime);
if (jbuf->last_rtptime != GST_CLOCK_TIME_NONE
&& ext_rtptime == jbuf->last_rtptime) {
return jbuf->prev_out_time;
}
jbuf->last_rtptime = ext_rtptime;
g_mutex_lock (&jbuf->clock_lock);
media_clock = jbuf->media_clock ? gst_object_ref (jbuf->media_clock) : NULL;
pipeline_clock =
jbuf->pipeline_clock ? gst_object_ref (jbuf->pipeline_clock) : NULL;
media_clock_offset = jbuf->media_clock_offset;
g_mutex_unlock (&jbuf->clock_lock);
gstrtptime =
gst_util_uint64_scale_int (ext_rtptime, GST_SECOND, jbuf->clock_rate);
if (G_LIKELY (jbuf->base_rtptime != GST_CLOCK_TIME_NONE)) {
if (gstrtptime < jbuf->base_rtptime) {
if (!is_rtx) {
GST_WARNING ("backward timestamps at server, schedule resync");
jbuf->need_resync = TRUE;
} else {
GST_WARNING ("backward timestamps: ignore rtx packet");
pts = GST_CLOCK_TIME_NONE;
goto done;
}
}
}
switch (jbuf->mode) {
case RTP_JITTER_BUFFER_MODE_NONE:
case RTP_JITTER_BUFFER_MODE_BUFFER:
if (jbuf->base_time == GST_CLOCK_TIME_NONE)
dts = 0;
else
dts = -1;
break;
case RTP_JITTER_BUFFER_MODE_SYNCED:
if (jbuf->base_time != GST_CLOCK_TIME_NONE && !jbuf->need_resync)
dts = -1;
break;
case RTP_JITTER_BUFFER_MODE_SLAVE:
default:
break;
}
if (G_UNLIKELY (jbuf->need_resync && dts != GST_CLOCK_TIME_NONE)) {
if (is_rtx) {
GST_DEBUG ("not resyncing on rtx packet, discard");
pts = GST_CLOCK_TIME_NONE;
goto done;
}
GST_INFO ("resync to time %" GST_TIME_FORMAT ", rtptime %"
GST_TIME_FORMAT, GST_TIME_ARGS (dts), GST_TIME_ARGS (gstrtptime));
rtp_jitter_buffer_resync (jbuf, dts, gstrtptime, ext_rtptime, FALSE);
}
GST_DEBUG ("extrtp %" G_GUINT64_FORMAT ", gstrtp %" GST_TIME_FORMAT ", base %"
GST_TIME_FORMAT ", send_diff %" GST_TIME_FORMAT, ext_rtptime,
GST_TIME_ARGS (gstrtptime), GST_TIME_ARGS (jbuf->base_rtptime),
GST_TIME_ARGS (gstrtptime - jbuf->base_rtptime));
rfc7273_mode = media_clock && pipeline_clock
&& gst_clock_is_synced (media_clock);
if (rfc7273_mode && jbuf->mode == RTP_JITTER_BUFFER_MODE_SLAVE
&& (media_clock_offset == GST_CLOCK_TIME_NONE || !jbuf->rfc7273_sync)) {
GstClockTime internal, external;
GstClockTime rate_num, rate_denom;
GstClockTime nsrtptimediff, rtpntptime, rtpsystime;
gst_clock_get_calibration (media_clock, &internal, &external, &rate_num,
&rate_denom);
if (jbuf->media_clock_base_time == GST_CLOCK_TIME_NONE) {
if (jbuf->base_time != GST_CLOCK_TIME_NONE) {
jbuf->media_clock_base_time =
gst_clock_unadjust_with_calibration (media_clock,
jbuf->base_time + base_time, internal, external, rate_num,
rate_denom);
} else {
if (dts != GST_CLOCK_TIME_NONE)
jbuf->media_clock_base_time =
gst_clock_unadjust_with_calibration (media_clock, dts + base_time,
internal, external, rate_num, rate_denom);
else
jbuf->media_clock_base_time =
gst_clock_get_internal_time (media_clock);
jbuf->base_rtptime = gstrtptime;
}
}
if (gstrtptime > jbuf->base_rtptime)
nsrtptimediff = gstrtptime - jbuf->base_rtptime;
else
nsrtptimediff = 0;
rtpntptime = nsrtptimediff + jbuf->media_clock_base_time;
rtpsystime =
gst_clock_adjust_with_calibration (media_clock, rtpntptime, internal,
external, rate_num, rate_denom);
if (rtpsystime > base_time)
pts = rtpsystime - base_time;
else
pts = 0;
GST_DEBUG ("RFC7273 clock time %" GST_TIME_FORMAT ", out %" GST_TIME_FORMAT,
GST_TIME_ARGS (rtpsystime), GST_TIME_ARGS (pts));
} else if (rfc7273_mode && (jbuf->mode == RTP_JITTER_BUFFER_MODE_SLAVE
|| jbuf->mode == RTP_JITTER_BUFFER_MODE_SYNCED)
&& media_clock_offset != GST_CLOCK_TIME_NONE && jbuf->rfc7273_sync) {
GstClockTime ntptime, rtptime_tmp;
GstClockTime ntprtptime, rtpsystime;
GstClockTime internal, external;
GstClockTime rate_num, rate_denom;
dts = -1;
gst_clock_get_calibration (media_clock, &internal, &external, &rate_num,
&rate_denom);
ntptime = gst_clock_get_internal_time (media_clock);
ntprtptime = gst_util_uint64_scale (ntptime, jbuf->clock_rate, GST_SECOND);
ntprtptime += media_clock_offset;
ntprtptime &= 0xffffffff;
rtptime_tmp = rtptime;
if (ntprtptime > rtptime_tmp && ntprtptime - rtptime_tmp >= 0x80000000)
rtptime_tmp += G_GUINT64_CONSTANT (0x100000000);
else if (rtptime_tmp > ntprtptime && rtptime_tmp - ntprtptime >= 0x80000000)
ntprtptime += G_GUINT64_CONSTANT (0x100000000);
if (ntprtptime > rtptime_tmp)
ntptime -=
gst_util_uint64_scale (ntprtptime - rtptime_tmp, jbuf->clock_rate,
GST_SECOND);
else
ntptime +=
gst_util_uint64_scale (rtptime_tmp - ntprtptime, jbuf->clock_rate,
GST_SECOND);
rtpsystime =
gst_clock_adjust_with_calibration (media_clock, ntptime, internal,
external, rate_num, rate_denom);
if (rtpsystime > base_time)
pts = rtpsystime - base_time;
else
pts = 0;
GST_DEBUG ("RFC7273 clock time %" GST_TIME_FORMAT ", out %" GST_TIME_FORMAT,
GST_TIME_ARGS (rtpsystime), GST_TIME_ARGS (pts));
} else {
jbuf->media_clock_base_time = -1;
pts = calculate_skew (jbuf, ext_rtptime, gstrtptime, dts, gap, is_rtx);
}
if (G_LIKELY (pts != GST_CLOCK_TIME_NONE
&& jbuf->prev_out_time != GST_CLOCK_TIME_NONE
&& jbuf->prev_send_diff != -1)) {
if (G_UNLIKELY (
((gint64) (gstrtptime - jbuf->base_rtptime) > jbuf->prev_send_diff
&& pts < jbuf->prev_out_time) ||
((gint64) (gstrtptime - jbuf->base_rtptime) < jbuf->prev_send_diff
&& pts > jbuf->prev_out_time) ||
(gint64) (gstrtptime - jbuf->base_rtptime) == jbuf->prev_send_diff)) {
GST_DEBUG ("backwards timestamps, using previous time");
pts = jbuf->prev_out_time;
}
}
if (gap == 0 && dts != GST_CLOCK_TIME_NONE && pts + jbuf->delay < dts) {
GST_DEBUG ("out %" GST_TIME_FORMAT " + %" G_GUINT64_FORMAT " < time %"
GST_TIME_FORMAT ", reset jitterbuffer and discard", GST_TIME_ARGS (pts),
jbuf->delay, GST_TIME_ARGS (dts));
rtp_jitter_buffer_reset_skew (jbuf);
pts = GST_CLOCK_TIME_NONE;
goto done;
}
jbuf->prev_out_time = pts;
jbuf->prev_send_diff = gstrtptime - jbuf->base_rtptime;
done:
if (media_clock)
gst_object_unref (media_clock);
if (pipeline_clock)
gst_object_unref (pipeline_clock);
return pts;
}
gboolean
rtp_jitter_buffer_insert (RTPJitterBuffer * jbuf, RTPJitterBufferItem * item,
gboolean * head, gint * percent)
{
GList *list, *event = NULL;
guint16 seqnum;
g_return_val_if_fail (jbuf != NULL, FALSE);
g_return_val_if_fail (item != NULL, FALSE);
list = jbuf->packets->tail;
if (item->seqnum == G_MAXUINT)
goto append;
seqnum = item->seqnum;
for (; list; list = g_list_previous (list)) {
guint16 qseq;
gint gap;
RTPJitterBufferItem *qitem = (RTPJitterBufferItem *) list;
if (qitem->seqnum == G_MAXUINT) {
if (event == NULL)
event = list;
continue;
}
qseq = qitem->seqnum;
gap = gst_rtp_buffer_compare_seqnum (seqnum, qseq);
if (G_UNLIKELY (gap == 0))
goto duplicate;
if (G_LIKELY (gap < 0))
break;
event = NULL;
}
if (event)
list = event;
append:
queue_do_insert (jbuf, list, (GList *) item);
if (jbuf->mode == RTP_JITTER_BUFFER_MODE_BUFFER)
update_buffer_level (jbuf, percent);
else if (percent)
*percent = -1;
if (G_LIKELY (head))
*head = (list == NULL);
return TRUE;
duplicate:
{
GST_DEBUG ("duplicate packet %d found", (gint) seqnum);
if (G_LIKELY (head))
*head = FALSE;
return FALSE;
}
}
RTPJitterBufferItem *
rtp_jitter_buffer_pop (RTPJitterBuffer * jbuf, gint * percent)
{
GList *item = NULL;
GQueue *queue;
g_return_val_if_fail (jbuf != NULL, NULL);
queue = jbuf->packets;
item = queue->head;
if (item) {
queue->head = item->next;
if (queue->head)
queue->head->prev = NULL;
else
queue->tail = NULL;
queue->length--;
}
if (jbuf->mode == RTP_JITTER_BUFFER_MODE_BUFFER)
update_buffer_level (jbuf, percent);
else if (percent)
*percent = -1;
return (RTPJitterBufferItem *) item;
}
RTPJitterBufferItem *
rtp_jitter_buffer_peek (RTPJitterBuffer * jbuf)
{
g_return_val_if_fail (jbuf != NULL, NULL);
return (RTPJitterBufferItem *) jbuf->packets->head;
}
void
rtp_jitter_buffer_flush (RTPJitterBuffer * jbuf, GFunc free_func,
gpointer user_data)
{
GList *item;
g_return_if_fail (jbuf != NULL);
g_return_if_fail (free_func != NULL);
while ((item = g_queue_pop_head_link (jbuf->packets)))
free_func ((RTPJitterBufferItem *) item, user_data);
}
gboolean
rtp_jitter_buffer_is_buffering (RTPJitterBuffer * jbuf)
{
return jbuf->buffering && !jbuf->buffering_disabled;
}
void
rtp_jitter_buffer_set_buffering (RTPJitterBuffer * jbuf, gboolean buffering)
{
jbuf->buffering = buffering;
}
gint
rtp_jitter_buffer_get_percent (RTPJitterBuffer * jbuf)
{
gint percent;
guint64 level;
if (G_UNLIKELY (jbuf->high_level == 0))
return 100;
if (G_UNLIKELY (jbuf->buffering_disabled))
return 100;
level = get_buffer_level (jbuf);
percent = (level * 100 / jbuf->high_level);
percent = MIN (percent, 100);
return percent;
}
guint
rtp_jitter_buffer_num_packets (RTPJitterBuffer * jbuf)
{
g_return_val_if_fail (jbuf != NULL, 0);
return jbuf->packets->length;
}
guint32
rtp_jitter_buffer_get_ts_diff (RTPJitterBuffer * jbuf)
{
guint64 high_ts, low_ts;
RTPJitterBufferItem *high_buf, *low_buf;
guint32 result;
g_return_val_if_fail (jbuf != NULL, 0);
high_buf = (RTPJitterBufferItem *) g_queue_peek_tail_link (jbuf->packets);
low_buf = (RTPJitterBufferItem *) g_queue_peek_head_link (jbuf->packets);
if (!high_buf || !low_buf || high_buf == low_buf)
return 0;
high_ts = high_buf->rtptime;
low_ts = low_buf->rtptime;
if (high_ts >= low_ts) {
result = (guint32) (high_ts - low_ts);
} else {
result = (guint32) (high_ts + G_MAXUINT32 + 1 - low_ts);
}
return result;
}
static guint16
rtp_jitter_buffer_get_seqnum_diff (RTPJitterBuffer * jbuf)
{
guint32 high_seqnum, low_seqnum;
RTPJitterBufferItem *high_buf, *low_buf;
guint16 result;
g_return_val_if_fail (jbuf != NULL, 0);
high_buf = (RTPJitterBufferItem *) g_queue_peek_tail_link (jbuf->packets);
low_buf = (RTPJitterBufferItem *) g_queue_peek_head_link (jbuf->packets);
while (high_buf && high_buf->seqnum == G_MAXUINT)
high_buf = (RTPJitterBufferItem *) high_buf->prev;
while (low_buf && low_buf->seqnum == G_MAXUINT)
low_buf = (RTPJitterBufferItem *) low_buf->next;
if (!high_buf || !low_buf || high_buf == low_buf)
return 0;
high_seqnum = high_buf->seqnum;
low_seqnum = low_buf->seqnum;
if (high_seqnum >= low_seqnum) {
result = (guint32) (high_seqnum - low_seqnum);
} else {
result = (guint32) (high_seqnum + G_MAXUINT16 + 1 - low_seqnum);
}
return result;
}
void
rtp_jitter_buffer_get_sync (RTPJitterBuffer * jbuf, guint64 * rtptime,
guint64 * timestamp, guint32 * clock_rate, guint64 * last_rtptime)
{
if (rtptime)
*rtptime = jbuf->base_extrtp;
if (timestamp)
*timestamp = jbuf->base_time + jbuf->skew;
if (clock_rate)
*clock_rate = jbuf->clock_rate;
if (last_rtptime)
*last_rtptime = jbuf->last_rtptime;
}
gboolean
rtp_jitter_buffer_can_fast_start (RTPJitterBuffer * jbuf, gint num_packet)
{
gboolean ret = TRUE;
RTPJitterBufferItem *last_item = NULL, *item;
gint i;
if (rtp_jitter_buffer_num_packets (jbuf) < (guint) num_packet)
return FALSE;
item = rtp_jitter_buffer_peek (jbuf);
for (i = 0; i < num_packet; i++) {
if (G_LIKELY (last_item)) {
guint16 expected_seqnum = last_item->seqnum + 1;
if (expected_seqnum != item->seqnum) {
ret = FALSE;
break;
}
}
last_item = item;
item = (RTPJitterBufferItem *) last_item->next;
}
return ret;
}
gboolean
rtp_jitter_buffer_is_full (RTPJitterBuffer * jbuf)
{
return rtp_jitter_buffer_get_seqnum_diff (jbuf) >= 32765 &&
rtp_jitter_buffer_num_packets (jbuf) > 10000;
}
void
rtp_jitter_buffer_find_earliest (RTPJitterBuffer * jbuf, GstClockTime * pts,
guint * seqnum)
{
GList *tmp;
RTPJitterBufferItem *earliest = NULL;
*pts = GST_CLOCK_TIME_NONE;
*seqnum = 0;
for (tmp = jbuf->packets->head; tmp; tmp = tmp->next) {
RTPJitterBufferItem *item = (RTPJitterBufferItem *) tmp;
if (!earliest || item->pts <= earliest->pts)
earliest = item;
}
if (earliest) {
*pts = earliest->pts;
*seqnum = earliest->seqnum;
}
}