rtpjitterbuffer.h 7.75 KB
Newer Older
1
/* GStreamer
2
 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
16 17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
18 19 20 21 22 23 24 25 26 27
 */

#ifndef __RTP_JITTER_BUFFER_H__
#define __RTP_JITTER_BUFFER_H__

#include <gst/gst.h>
#include <gst/rtp/gstrtcpbuffer.h>

typedef struct _RTPJitterBuffer RTPJitterBuffer;
typedef struct _RTPJitterBufferClass RTPJitterBufferClass;
28
typedef struct _RTPJitterBufferItem RTPJitterBufferItem;
29 30 31 32 33 34 35 36

#define RTP_TYPE_JITTER_BUFFER             (rtp_jitter_buffer_get_type())
#define RTP_JITTER_BUFFER(src)             (G_TYPE_CHECK_INSTANCE_CAST((src),RTP_TYPE_JITTER_BUFFER,RTPJitterBuffer))
#define RTP_JITTER_BUFFER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),RTP_TYPE_JITTER_BUFFER,RTPJitterBufferClass))
#define RTP_IS_JITTER_BUFFER(src)          (G_TYPE_CHECK_INSTANCE_TYPE((src),RTP_TYPE_JITTER_BUFFER))
#define RTP_IS_JITTER_BUFFER_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),RTP_TYPE_JITTER_BUFFER))
#define RTP_JITTER_BUFFER_CAST(src)        ((RTPJitterBuffer *)(src))

37 38
/**
 * RTPJitterBufferMode:
39
 * @RTP_JITTER_BUFFER_MODE_NONE: don't do any skew correction, outgoing
40 41
 *    timestamps are calculated directly from the RTP timestamps. This mode is
 *    good for recording but not for real-time applications.
42
 * @RTP_JITTER_BUFFER_MODE_SLAVE: calculate the skew between sender and receiver
43 44
 *    and produce smoothed adjusted outgoing timestamps. This mode is good for
 *    low latency communications.
45
 * @RTP_JITTER_BUFFER_MODE_BUFFER: buffer packets between low/high watermarks.
46
 *    This mode is good for streaming communication.
47
 * @RTP_JITTER_BUFFER_MODE_SYNCED: sender and receiver clocks are synchronized,
48 49 50
 *    like #RTP_JITTER_BUFFER_MODE_SLAVE but skew is assumed to be 0. Good for
 *    low latency communication when sender and receiver clocks are
 *    synchronized and there is thus no clock skew.
51
 * @RTP_JITTER_BUFFER_MODE_LAST: last buffer mode.
52 53 54 55 56 57 58
 *
 * The different buffer modes for a jitterbuffer.
 */
typedef enum {
  RTP_JITTER_BUFFER_MODE_NONE    = 0,
  RTP_JITTER_BUFFER_MODE_SLAVE   = 1,
  RTP_JITTER_BUFFER_MODE_BUFFER  = 2,
Wim Taymans's avatar
Wim Taymans committed
59 60
  /* FIXME 3 is missing because it was used for 'auto' in jitterbuffer */
  RTP_JITTER_BUFFER_MODE_SYNCED  = 4,
61 62 63
  RTP_JITTER_BUFFER_MODE_LAST
} RTPJitterBufferMode;

64 65 66
#define RTP_TYPE_JITTER_BUFFER_MODE (rtp_jitter_buffer_mode_get_type())
GType rtp_jitter_buffer_mode_get_type (void);

67
#define RTP_JITTER_BUFFER_MAX_WINDOW 512
68 69 70 71 72 73
/**
 * RTPJitterBuffer:
 *
 * A JitterBuffer in the #RTPSession
 */
struct _RTPJitterBuffer {
74 75 76
  GObject        object;

  GQueue        *packets;
77

78 79
  RTPJitterBufferMode mode;

80 81 82 83 84 85
  GstClockTime   delay;

  /* for buffering */
  gboolean          buffering;
  guint64           low_level;
  guint64           high_level;
86

87
  /* for calculating skew */
88
  gboolean       need_resync;
89 90
  GstClockTime   base_time;
  GstClockTime   base_rtptime;
91
  GstClockTime   media_clock_base_time;
92
  guint32        clock_rate;
93
  GstClockTime   base_extrtp;
94
  GstClockTime   prev_out_time;
95
  guint64        ext_rtptime;
96
  guint64        last_rtptime;
97
  gint64         window[RTP_JITTER_BUFFER_MAX_WINDOW];
98
  guint          window_pos;
99
  guint          window_size;
100 101 102
  gboolean       window_filling;
  gint64         window_min;
  gint64         skew;
103
  gint64         prev_send_diff;
104
  gboolean       buffering_disabled;
105 106 107 108 109 110 111 112

  GMutex         clock_lock;
  GstClock      *pipeline_clock;
  GstClock      *media_clock;
  gulong         media_clock_synced_id;
  guint64        media_clock_offset;

  gboolean       rfc7273_sync;
113 114 115 116 117 118
};

struct _RTPJitterBufferClass {
  GObjectClass   parent_class;
};

119 120 121 122 123
/**
 * RTPJitterBufferItem:
 * @data: the data of the item
 * @next: pointer to next item
 * @prev: pointer to previous item
124
 * @type: the type of @data, used freely by caller
125 126
 * @dts: input DTS
 * @pts: output PTS
127 128 129
 * @seqnum: seqnum, the seqnum is used to insert the item in the
 *   right position in the jitterbuffer and detect duplicates. Use -1 to
 *   append.
130
 * @count: amount of seqnum in this item
131 132 133 134 135 136 137 138
 * @rtptime: rtp timestamp
 *
 * An object containing an RTP packet or event.
 */
struct _RTPJitterBufferItem {
  gpointer data;
  GList *next;
  GList *prev;
139
  guint type;
140 141 142
  GstClockTime dts;
  GstClockTime pts;
  guint seqnum;
143
  guint count;
144 145 146
  guint rtptime;
};

147 148 149
GType rtp_jitter_buffer_get_type (void);

/* managing lifetime */
150 151
RTPJitterBuffer*      rtp_jitter_buffer_new              (void);

152 153 154
RTPJitterBufferMode   rtp_jitter_buffer_get_mode         (RTPJitterBuffer *jbuf);
void                  rtp_jitter_buffer_set_mode         (RTPJitterBuffer *jbuf, RTPJitterBufferMode mode);

155 156 157
GstClockTime          rtp_jitter_buffer_get_delay        (RTPJitterBuffer *jbuf);
void                  rtp_jitter_buffer_set_delay        (RTPJitterBuffer *jbuf, GstClockTime delay);

158 159 160
void                  rtp_jitter_buffer_set_clock_rate   (RTPJitterBuffer *jbuf, guint32 clock_rate);
guint32               rtp_jitter_buffer_get_clock_rate   (RTPJitterBuffer *jbuf);

161 162 163 164 165 166
void                  rtp_jitter_buffer_set_media_clock  (RTPJitterBuffer *jbuf, GstClock * clock, guint64 clock_offset);
void                  rtp_jitter_buffer_set_pipeline_clock (RTPJitterBuffer *jbuf, GstClock * clock);

gboolean              rtp_jitter_buffer_get_rfc7273_sync (RTPJitterBuffer *jbuf);
void                  rtp_jitter_buffer_set_rfc7273_sync (RTPJitterBuffer *jbuf, gboolean rfc7273_sync);

167 168
void                  rtp_jitter_buffer_reset_skew       (RTPJitterBuffer *jbuf);

169 170
gboolean              rtp_jitter_buffer_insert           (RTPJitterBuffer *jbuf,
                                                          RTPJitterBufferItem *item,
171
                                                          gboolean *head, gint *percent);
172 173 174

void                  rtp_jitter_buffer_disable_buffering (RTPJitterBuffer *jbuf, gboolean disabled);

175 176
RTPJitterBufferItem * rtp_jitter_buffer_peek             (RTPJitterBuffer *jbuf);
RTPJitterBufferItem * rtp_jitter_buffer_pop              (RTPJitterBuffer *jbuf, gint *percent);
177

Wim Taymans's avatar
Wim Taymans committed
178 179
void                  rtp_jitter_buffer_flush            (RTPJitterBuffer *jbuf,
                                                          GFunc free_func, gpointer user_data);
180

181
gboolean              rtp_jitter_buffer_is_buffering     (RTPJitterBuffer * jbuf);
Wim Taymans's avatar
Wim Taymans committed
182 183
void                  rtp_jitter_buffer_set_buffering    (RTPJitterBuffer * jbuf, gboolean buffering);
gint                  rtp_jitter_buffer_get_percent      (RTPJitterBuffer * jbuf);
184

185 186
guint                 rtp_jitter_buffer_num_packets      (RTPJitterBuffer *jbuf);
guint32               rtp_jitter_buffer_get_ts_diff      (RTPJitterBuffer *jbuf);
187

188
void                  rtp_jitter_buffer_get_sync         (RTPJitterBuffer *jbuf, guint64 *rtptime,
189
                                                          guint64 *timestamp, guint32 *clock_rate,
190
                                                          guint64 *last_rtptime);
191

192
GstClockTime          rtp_jitter_buffer_calculate_pts    (RTPJitterBuffer * jbuf, GstClockTime dts, gboolean estimated_dts,
193 194
                                                          guint32 rtptime, GstClockTime base_time);

195 196
gboolean              rtp_jitter_buffer_can_fast_start   (RTPJitterBuffer * jbuf, gint num_packet);

197
#endif /* __RTP_JITTER_BUFFER_H__ */