gst/rtpmanager/gstrtpbin.c: Reset rtp timestamp interpollation when we detect a gap...
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / rtpsession.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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifndef __RTP_SESSION_H__
21 #define __RTP_SESSION_H__
22
23 #include <gst/gst.h>
24 #include <gst/netbuffer/gstnetbuffer.h>
25
26 #include "rtpsource.h"
27
28 typedef struct _RTPSession RTPSession;
29 typedef struct _RTPSessionClass RTPSessionClass;
30
31 #define RTP_TYPE_SESSION             (rtp_session_get_type())
32 #define RTP_SESSION(sess)            (G_TYPE_CHECK_INSTANCE_CAST((sess),RTP_TYPE_SESSION,RTPSession))
33 #define RTP_SESSION_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),RTP_TYPE_SESSION,RTPSessionClass))
34 #define RTP_IS_SESSION(sess)         (G_TYPE_CHECK_INSTANCE_TYPE((sess),RTP_TYPE_SESSION))
35 #define RTP_IS_SESSION_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),RTP_TYPE_SESSION))
36 #define RTP_SESSION_CAST(sess)       ((RTPSession *)(sess))
37
38 #define RTP_SESSION_LOCK(sess)     (g_mutex_lock ((sess)->lock))
39 #define RTP_SESSION_UNLOCK(sess)   (g_mutex_unlock ((sess)->lock))
40
41 /**
42  * RTPSessionProcessRTP:
43  * @sess: an #RTPSession
44  * @src: the #RTPSource
45  * @buffer: the RTP buffer ready for processing
46  * @user_data: user data specified when registering
47  *
48  * This callback will be called when @sess has @buffer ready for further
49  * processing. Processing the buffer typically includes decoding and displaying
50  * the buffer.
51  *
52  * Returns: a #GstFlowReturn.
53  */
54 typedef GstFlowReturn (*RTPSessionProcessRTP) (RTPSession *sess, RTPSource *src, GstBuffer *buffer, gpointer user_data);
55
56 /**
57  * RTPSessionSendRTP:
58  * @sess: an #RTPSession
59  * @src: the #RTPSource
60  * @buffer: the RTP buffer ready for sending
61  * @user_data: user data specified when registering
62  *
63  * This callback will be called when @sess has @buffer ready for sending to
64  * all listening participants in this session.
65  *
66  * Returns: a #GstFlowReturn.
67  */
68 typedef GstFlowReturn (*RTPSessionSendRTP) (RTPSession *sess, RTPSource *src, GstBuffer *buffer, gpointer user_data);
69
70 /**
71  * RTPSessionSendRTCP:
72  * @sess: an #RTPSession
73  * @src: the #RTPSource
74  * @buffer: the RTCP buffer ready for sending
75  * @user_data: user data specified when registering
76  *
77  * This callback will be called when @sess has @buffer ready for sending to
78  * all listening participants in this session.
79  *
80  * Returns: a #GstFlowReturn.
81  */
82 typedef GstFlowReturn (*RTPSessionSendRTCP) (RTPSession *sess, RTPSource *src, GstBuffer *buffer, gpointer user_data);
83
84 /**
85  * RTPSessionSyncRTCP:
86  * @sess: an #RTPSession
87  * @src: the #RTPSource
88  * @buffer: the RTCP buffer ready for synchronisation
89  * @user_data: user data specified when registering
90  *
91  * This callback will be called when @sess has an SR @buffer ready for doing
92  * synchronisation between streams.
93  *
94  * Returns: a #GstFlowReturn.
95  */
96 typedef GstFlowReturn (*RTPSessionSyncRTCP) (RTPSession *sess, RTPSource *src, GstBuffer *buffer, gpointer user_data);
97
98 /**
99  * RTPSessionClockRate:
100  * @sess: an #RTPSession
101  * @payload: the payload
102  * @user_data: user data specified when registering
103  *
104  * This callback will be called when @sess needs the clock-rate of @payload.
105  *
106  * Returns: the clock-rate of @pt.
107  */
108 typedef gint (*RTPSessionClockRate) (RTPSession *sess, guint8 payload, gpointer user_data);
109
110 /**
111  * RTPSessionReconsider:
112  * @sess: an #RTPSession
113  * @user_data: user data specified when registering
114  *
115  * This callback will be called when @sess needs to cancel the current timeout. 
116  * The currently running timeout should be canceled and a new reporting interval
117  * should be requested from @sess.
118  */
119 typedef void (*RTPSessionReconsider) (RTPSession *sess, gpointer user_data);
120
121 /**
122  * RTPSessionCallbacks:
123  * @RTPSessionProcessRTP: callback to process RTP packets
124  * @RTPSessionSendRTP: callback for sending RTP packets
125  * @RTPSessionSendRTCP: callback for sending RTCP packets
126  * @RTPSessionSyncRTCP: callback for handling SR packets
127  * @RTPSessionReconsider: callback for reconsidering the timeout
128  *
129  * These callbacks can be installed on the session manager to get notification
130  * when RTP and RTCP packets are ready for further processing. These callbacks
131  * are not implemented with signals for performance reasons.
132  */
133 typedef struct {
134   RTPSessionProcessRTP  process_rtp;
135   RTPSessionSendRTP     send_rtp;
136   RTPSessionSyncRTCP    sync_rtcp;
137   RTPSessionSendRTCP    send_rtcp;
138   RTPSessionClockRate   clock_rate;
139   RTPSessionReconsider  reconsider;
140 } RTPSessionCallbacks;
141
142 /**
143  * RTPConflictingAddress:
144  * @address: #GstNetAddress which conflicted
145  * @last_conflict_time: time when the last conflict was seen
146  *
147  * This structure is used to account for addresses that have conflicted to find
148  * loops.
149  */
150
151 typedef struct {
152   GstNetAddress address;
153   GstClockTime time;
154 } RTPConflictingAddress;
155
156 /**
157  * RTPSession:
158  * @lock: lock to protect the session
159  * @source: the source of this session
160  * @ssrcs: Hashtable of sources indexed by SSRC
161  * @cnames: Hashtable of sources indexed by CNAME
162  * @num_sources: the number of sources
163  * @activecount: the number of active sources
164  * @callbacks: callbacks
165  * @user_data: user data passed in callbacks
166  * @stats: session statistics
167  * @conflicting_addresses: GList of conflicting addresses
168  *
169  * The RTP session manager object
170  */
171 struct _RTPSession {
172   GObject       object;
173
174   GMutex       *lock;
175
176   guint         header_len;
177   guint         mtu;
178
179   RTPSource    *source;
180
181   /* for sender/receiver counting */
182   guint32       key;
183   guint32       mask_idx;
184   guint32       mask;
185   GHashTable   *ssrcs[32];
186   GHashTable   *cnames;
187   guint         total_sources;
188
189   GstClockTime  next_rtcp_check_time;
190   GstClockTime  last_rtcp_send_time;
191   gboolean      first_rtcp;
192
193   gchar        *bye_reason;
194   gboolean      sent_bye;
195
196   RTPSessionCallbacks   callbacks;
197   gpointer              process_rtp_user_data;
198   gpointer              send_rtp_user_data;
199   gpointer              send_rtcp_user_data;
200   gpointer              sync_rtcp_user_data;
201   gpointer              clock_rate_user_data;
202   gpointer              reconsider_user_data;
203
204   RTPSessionStats stats;
205
206   GList         *conflicting_addresses;
207   gboolean      change_ssrc;
208 };
209
210 /**
211  * RTPSessionClass:
212  * @on_new_ssrc: emited when a new source is found
213  * @on_bye_ssrc: emited when a source is gone
214  *
215  * The session class.
216  */
217 struct _RTPSessionClass {
218   GObjectClass   parent_class;
219
220   /* signals */
221   void (*on_new_ssrc)       (RTPSession *sess, RTPSource *source);
222   void (*on_ssrc_collision) (RTPSession *sess, RTPSource *source);
223   void (*on_ssrc_validated) (RTPSession *sess, RTPSource *source);
224   void (*on_ssrc_active)    (RTPSession *sess, RTPSource *source);
225   void (*on_ssrc_sdes)      (RTPSession *sess, RTPSource *source);
226   void (*on_bye_ssrc)       (RTPSession *sess, RTPSource *source);
227   void (*on_bye_timeout)    (RTPSession *sess, RTPSource *source);
228   void (*on_timeout)        (RTPSession *sess, RTPSource *source);
229 };
230
231 GType rtp_session_get_type (void);
232
233 /* create and configure */
234 RTPSession*     rtp_session_new           (void);
235 void            rtp_session_set_callbacks          (RTPSession *sess,
236                                                     RTPSessionCallbacks *callbacks,
237                                                     gpointer user_data);
238 void            rtp_session_set_process_rtp_callback   (RTPSession * sess,
239                                                     RTPSessionProcessRTP callback,
240                                                     gpointer user_data);
241 void            rtp_session_set_send_rtp_callback  (RTPSession * sess,
242                                                     RTPSessionSendRTP callback,
243                                                     gpointer user_data);
244 void            rtp_session_set_send_rtcp_callback   (RTPSession * sess,
245                                                     RTPSessionSendRTCP callback,
246                                                     gpointer user_data);
247 void            rtp_session_set_sync_rtcp_callback   (RTPSession * sess,
248                                                     RTPSessionSyncRTCP callback,
249                                                     gpointer user_data);
250 void            rtp_session_set_clock_rate_callback   (RTPSession * sess,
251                                                     RTPSessionClockRate callback,
252                                                     gpointer user_data);
253 void            rtp_session_set_reconsider_callback (RTPSession * sess,
254                                                     RTPSessionReconsider callback,
255                                                     gpointer user_data);
256 void            rtp_session_set_bandwidth          (RTPSession *sess, gdouble bandwidth);
257 gdouble         rtp_session_get_bandwidth          (RTPSession *sess);
258 void            rtp_session_set_rtcp_fraction      (RTPSession *sess, gdouble fraction);
259 gdouble         rtp_session_get_rtcp_fraction      (RTPSession *sess);
260
261 gboolean        rtp_session_set_sdes_string        (RTPSession *sess, GstRTCPSDESType type,
262                                                     const gchar *cname);
263 gchar*          rtp_session_get_sdes_string        (RTPSession *sess, GstRTCPSDESType type);
264
265 /* handling sources */
266 RTPSource*      rtp_session_get_internal_source    (RTPSession *sess);
267
268 void            rtp_session_set_internal_ssrc      (RTPSession *sess, guint32 ssrc);
269 guint32         rtp_session_get_internal_ssrc      (RTPSession *sess);
270
271 gboolean        rtp_session_add_source             (RTPSession *sess, RTPSource *src);
272 guint           rtp_session_get_num_sources        (RTPSession *sess);
273 guint           rtp_session_get_num_active_sources (RTPSession *sess);
274 RTPSource*      rtp_session_get_source_by_ssrc     (RTPSession *sess, guint32 ssrc);
275 RTPSource*      rtp_session_get_source_by_cname    (RTPSession *sess, const gchar *cname);
276 RTPSource*      rtp_session_create_source          (RTPSession *sess);
277
278 /* processing packets from receivers */
279 GstFlowReturn   rtp_session_process_rtp            (RTPSession *sess, GstBuffer *buffer, GstClockTime current_time, guint64 ntpnstime);
280 GstFlowReturn   rtp_session_process_rtcp           (RTPSession *sess, GstBuffer *buffer, GstClockTime current_time);
281
282 /* processing packets for sending */
283 GstFlowReturn   rtp_session_send_rtp               (RTPSession *sess, GstBuffer *buffer, GstClockTime current_time, guint64 ntpnstime);
284
285 /* stopping the session */
286 GstFlowReturn   rtp_session_send_bye               (RTPSession *sess, const gchar *reason, GstClockTime current_time);
287
288 /* get interval for next RTCP interval */
289 GstClockTime    rtp_session_next_timeout           (RTPSession *sess, GstClockTime current_time);
290 GstFlowReturn   rtp_session_on_timeout             (RTPSession *sess, GstClockTime current_time, guint64 ntpnstime);
291
292 #endif /* __RTP_SESSION_H__ */