jitterbuffer: do skew estimation only for new timestamps
[platform/upstream/gstreamer.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
29 #define RTP_TYPE_JITTER_BUFFER             (rtp_jitter_buffer_get_type())
30 #define RTP_JITTER_BUFFER(src)             (G_TYPE_CHECK_INSTANCE_CAST((src),RTP_TYPE_JITTER_BUFFER,RTPJitterBuffer))
31 #define RTP_JITTER_BUFFER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),RTP_TYPE_JITTER_BUFFER,RTPJitterBufferClass))
32 #define RTP_IS_JITTER_BUFFER(src)          (G_TYPE_CHECK_INSTANCE_TYPE((src),RTP_TYPE_JITTER_BUFFER))
33 #define RTP_IS_JITTER_BUFFER_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),RTP_TYPE_JITTER_BUFFER))
34 #define RTP_JITTER_BUFFER_CAST(src)        ((RTPJitterBuffer *)(src))
35
36 /**
37  * RTPJitterBufferMode:
38  *
39  * RTP_JITTER_BUFFER_MODE_NONE: don't do any skew correction, outgoing
40  *    timestamps are calculated directly from the RTP timestamps. This mode is
41  *    good for recording but not for real-time applications.
42  * RTP_JITTER_BUFFER_MODE_SLAVE: calculate the skew between sender and receiver
43  *    and produce smoothed adjusted outgoing timestamps. This mode is good for
44  *    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_LAST: last buffer mode.
48  *
49  * The different buffer modes for a jitterbuffer.
50  */
51 typedef enum {
52   RTP_JITTER_BUFFER_MODE_NONE    = 0,
53   RTP_JITTER_BUFFER_MODE_SLAVE   = 1,
54   RTP_JITTER_BUFFER_MODE_BUFFER  = 2,
55   RTP_JITTER_BUFFER_MODE_LAST
56 } RTPJitterBufferMode;
57
58 #define RTP_TYPE_JITTER_BUFFER_MODE (rtp_jitter_buffer_mode_get_type())
59 GType rtp_jitter_buffer_mode_get_type (void);
60
61 #define RTP_JITTER_BUFFER_MAX_WINDOW 512
62 /**
63  * RTPJitterBuffer:
64  *
65  * A JitterBuffer in the #RTPSession
66  */
67 struct _RTPJitterBuffer {
68   GObject        object;
69
70   GQueue        *packets;
71
72   RTPJitterBufferMode mode;
73
74   GstClockTime   delay;
75
76   /* for buffering */
77   gboolean          buffering;
78   guint64           low_level;
79   guint64           high_level;
80
81   /* for calculating skew */
82   GstClockTime   base_time;
83   GstClockTime   base_rtptime;
84   guint32        clock_rate;
85   GstClockTime   base_extrtp;
86   GstClockTime   prev_out_time;
87   guint64        ext_rtptime;
88   guint64        last_rtptime;
89   gint64         window[RTP_JITTER_BUFFER_MAX_WINDOW];
90   guint          window_pos;
91   guint          window_size;
92   gboolean       window_filling;
93   gint64         window_min;
94   gint64         skew;
95   gint64         prev_send_diff;
96 };
97
98 struct _RTPJitterBufferClass {
99   GObjectClass   parent_class;
100 };
101
102 GType rtp_jitter_buffer_get_type (void);
103
104 /* managing lifetime */
105 RTPJitterBuffer*      rtp_jitter_buffer_new              (void);
106
107 RTPJitterBufferMode   rtp_jitter_buffer_get_mode         (RTPJitterBuffer *jbuf);
108 void                  rtp_jitter_buffer_set_mode         (RTPJitterBuffer *jbuf, RTPJitterBufferMode mode);
109
110 GstClockTime          rtp_jitter_buffer_get_delay        (RTPJitterBuffer *jbuf);
111 void                  rtp_jitter_buffer_set_delay        (RTPJitterBuffer *jbuf, GstClockTime delay);
112
113 void                  rtp_jitter_buffer_reset_skew       (RTPJitterBuffer *jbuf);
114
115 gboolean              rtp_jitter_buffer_insert           (RTPJitterBuffer *jbuf, GstBuffer *buf,
116                                                           GstClockTime time,
117                                                           guint32 clock_rate,
118                                                           gboolean *tail, gint *percent);
119 GstBuffer *           rtp_jitter_buffer_peek             (RTPJitterBuffer *jbuf);
120 GstBuffer *           rtp_jitter_buffer_pop              (RTPJitterBuffer *jbuf, gint *percent);
121
122 void                  rtp_jitter_buffer_flush            (RTPJitterBuffer *jbuf);
123
124 gboolean              rtp_jitter_buffer_is_buffering     (RTPJitterBuffer * jbuf);
125 void                  rtp_jitter_buffer_set_buffering    (RTPJitterBuffer * jbuf, gboolean buffering);
126 gint                  rtp_jitter_buffer_get_percent      (RTPJitterBuffer * jbuf);
127
128 guint                 rtp_jitter_buffer_num_packets      (RTPJitterBuffer *jbuf);
129 guint32               rtp_jitter_buffer_get_ts_diff      (RTPJitterBuffer *jbuf);
130
131 void                  rtp_jitter_buffer_get_sync         (RTPJitterBuffer *jbuf, guint64 *rtptime,
132                                                           guint64 *timestamp, guint32 *clock_rate,
133                                                           guint64 *last_rtptime);
134
135 #endif /* __RTP_JITTER_BUFFER_H__ */