jitterbuffer: rework resync handling
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / rtpjitterbuffer.h
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifndef __RTP_JITTER_BUFFER_H__
21 #define __RTP_JITTER_BUFFER_H__
22
23 #include <gst/gst.h>
24 #include <gst/rtp/gstrtcpbuffer.h>
25
26 typedef struct _RTPJitterBuffer RTPJitterBuffer;
27 typedef struct _RTPJitterBufferClass RTPJitterBufferClass;
28 typedef struct _RTPJitterBufferItem RTPJitterBufferItem;
29
30 #define RTP_TYPE_JITTER_BUFFER             (rtp_jitter_buffer_get_type())
31 #define RTP_JITTER_BUFFER(src)             (G_TYPE_CHECK_INSTANCE_CAST((src),RTP_TYPE_JITTER_BUFFER,RTPJitterBuffer))
32 #define RTP_JITTER_BUFFER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),RTP_TYPE_JITTER_BUFFER,RTPJitterBufferClass))
33 #define RTP_IS_JITTER_BUFFER(src)          (G_TYPE_CHECK_INSTANCE_TYPE((src),RTP_TYPE_JITTER_BUFFER))
34 #define RTP_IS_JITTER_BUFFER_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),RTP_TYPE_JITTER_BUFFER))
35 #define RTP_JITTER_BUFFER_CAST(src)        ((RTPJitterBuffer *)(src))
36
37 /**
38  * RTPJitterBufferMode:
39  *
40  * RTP_JITTER_BUFFER_MODE_NONE: don't do any skew correction, outgoing
41  *    timestamps are calculated directly from the RTP timestamps. This mode is
42  *    good for recording but not for real-time applications.
43  * RTP_JITTER_BUFFER_MODE_SLAVE: calculate the skew between sender and receiver
44  *    and produce smoothed adjusted outgoing timestamps. This mode is good for
45  *    low latency communications.
46  * RTP_JITTER_BUFFER_MODE_BUFFER: buffer packets between low/high watermarks.
47  *    This mode is good for streaming communication.
48  * RTP_JITTER_BUFFER_MODE_SYNCED: sender and receiver clocks are synchronized,
49  *    like #RTP_JITTER_BUFFER_MODE_SLAVE but skew is assumed to be 0. Good for
50  *    low latency communication when sender and receiver clocks are
51  *    synchronized and there is thus no clock skew.
52  * RTP_JITTER_BUFFER_MODE_LAST: last buffer mode.
53  *
54  * The different buffer modes for a jitterbuffer.
55  */
56 typedef enum {
57   RTP_JITTER_BUFFER_MODE_NONE    = 0,
58   RTP_JITTER_BUFFER_MODE_SLAVE   = 1,
59   RTP_JITTER_BUFFER_MODE_BUFFER  = 2,
60   /* FIXME 3 is missing because it was used for 'auto' in jitterbuffer */
61   RTP_JITTER_BUFFER_MODE_SYNCED  = 4,
62   RTP_JITTER_BUFFER_MODE_LAST
63 } RTPJitterBufferMode;
64
65 #define RTP_TYPE_JITTER_BUFFER_MODE (rtp_jitter_buffer_mode_get_type())
66 GType rtp_jitter_buffer_mode_get_type (void);
67
68 #define RTP_JITTER_BUFFER_MAX_WINDOW 512
69 /**
70  * RTPJitterBuffer:
71  *
72  * A JitterBuffer in the #RTPSession
73  */
74 struct _RTPJitterBuffer {
75   GObject        object;
76
77   GQueue        *packets;
78
79   RTPJitterBufferMode mode;
80
81   GstClockTime   delay;
82
83   /* for buffering */
84   gboolean          buffering;
85   guint64           low_level;
86   guint64           high_level;
87
88   /* for calculating skew */
89   gboolean       need_resync;
90   GstClockTime   base_time;
91   GstClockTime   base_rtptime;
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   gboolean       window_filling;
101   gint64         window_min;
102   gint64         skew;
103   gint64         prev_send_diff;
104   gboolean       buffering_disabled;
105 };
106
107 struct _RTPJitterBufferClass {
108   GObjectClass   parent_class;
109 };
110
111 /**
112  * RTPJitterBufferItem:
113  * @data: the data of the item
114  * @next: pointer to next item
115  * @prev: pointer to previous item
116  * @type: the type of @data, used freely by caller
117  * @dts: input DTS
118  * @pts: output PTS
119  * @seqnum: seqnum, the seqnum is used to insert the item in the
120  *   right position in the jitterbuffer and detect duplicates. Use -1 to
121  *   append.
122  * @count: amount of seqnum in this item
123  * @rtptime: rtp timestamp
124  *
125  * An object containing an RTP packet or event.
126  */
127 struct _RTPJitterBufferItem {
128   gpointer data;
129   GList *next;
130   GList *prev;
131   guint type;
132   GstClockTime dts;
133   GstClockTime pts;
134   guint seqnum;
135   guint count;
136   guint rtptime;
137 };
138
139 GType rtp_jitter_buffer_get_type (void);
140
141 /* managing lifetime */
142 RTPJitterBuffer*      rtp_jitter_buffer_new              (void);
143
144 RTPJitterBufferMode   rtp_jitter_buffer_get_mode         (RTPJitterBuffer *jbuf);
145 void                  rtp_jitter_buffer_set_mode         (RTPJitterBuffer *jbuf, RTPJitterBufferMode mode);
146
147 GstClockTime          rtp_jitter_buffer_get_delay        (RTPJitterBuffer *jbuf);
148 void                  rtp_jitter_buffer_set_delay        (RTPJitterBuffer *jbuf, GstClockTime delay);
149
150 void                  rtp_jitter_buffer_set_clock_rate   (RTPJitterBuffer *jbuf, guint32 clock_rate);
151 guint32               rtp_jitter_buffer_get_clock_rate   (RTPJitterBuffer *jbuf);
152
153 void                  rtp_jitter_buffer_reset_skew       (RTPJitterBuffer *jbuf);
154
155 gboolean              rtp_jitter_buffer_insert           (RTPJitterBuffer *jbuf,
156                                                           RTPJitterBufferItem *item,
157                                                           gboolean *head, gint *percent);
158
159 void                  rtp_jitter_buffer_disable_buffering (RTPJitterBuffer *jbuf, gboolean disabled);
160
161 RTPJitterBufferItem * rtp_jitter_buffer_peek             (RTPJitterBuffer *jbuf);
162 RTPJitterBufferItem * rtp_jitter_buffer_pop              (RTPJitterBuffer *jbuf, gint *percent);
163
164 void                  rtp_jitter_buffer_flush            (RTPJitterBuffer *jbuf,
165                                                           GFunc free_func, gpointer user_data);
166
167 gboolean              rtp_jitter_buffer_is_buffering     (RTPJitterBuffer * jbuf);
168 void                  rtp_jitter_buffer_set_buffering    (RTPJitterBuffer * jbuf, gboolean buffering);
169 gint                  rtp_jitter_buffer_get_percent      (RTPJitterBuffer * jbuf);
170
171 guint                 rtp_jitter_buffer_num_packets      (RTPJitterBuffer *jbuf);
172 guint32               rtp_jitter_buffer_get_ts_diff      (RTPJitterBuffer *jbuf);
173
174 void                  rtp_jitter_buffer_get_sync         (RTPJitterBuffer *jbuf, guint64 *rtptime,
175                                                           guint64 *timestamp, guint32 *clock_rate,
176                                                           guint64 *last_rtptime);
177
178 #endif /* __RTP_JITTER_BUFFER_H__ */