3ca6c161e209197825a44b034f40706dc96ae0f2
[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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifndef __RTP_SESSION_H__
21 #define __RTP_SESSION_H__
22
23 #include <gst/gst.h>
24
25 #include "rtpsource.h"
26
27 typedef struct _RTPSession RTPSession;
28 typedef struct _RTPSessionClass RTPSessionClass;
29
30 #define RTP_TYPE_SESSION             (rtp_session_get_type())
31 #define RTP_SESSION(sess)            (G_TYPE_CHECK_INSTANCE_CAST((sess),RTP_TYPE_SESSION,RTPSession))
32 #define RTP_SESSION_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),RTP_TYPE_SESSION,RTPSessionClass))
33 #define RTP_IS_SESSION(sess)         (G_TYPE_CHECK_INSTANCE_TYPE((sess),RTP_TYPE_SESSION))
34 #define RTP_IS_SESSION_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),RTP_TYPE_SESSION))
35 #define RTP_SESSION_CAST(sess)       ((RTPSession *)(sess))
36
37 #define RTP_SESSION_LOCK(sess)     (g_mutex_lock (&(sess)->lock))
38 #define RTP_SESSION_UNLOCK(sess)   (g_mutex_unlock (&(sess)->lock))
39
40 /**
41  * RTPSessionProcessRTP:
42  * @sess: an #RTPSession
43  * @src: the #RTPSource
44  * @buffer: the RTP buffer ready for processing
45  * @user_data: user data specified when registering
46  *
47  * This callback will be called when @sess has @buffer ready for further
48  * processing. Processing the buffer typically includes decoding and displaying
49  * the buffer.
50  *
51  * Returns: a #GstFlowReturn.
52  */
53 typedef GstFlowReturn (*RTPSessionProcessRTP) (RTPSession *sess, RTPSource *src, GstBuffer *buffer, gpointer user_data);
54
55 /**
56  * RTPSessionSendRTP:
57  * @sess: an #RTPSession
58  * @src: the #RTPSource
59  * @buffer: the RTP buffer ready for sending
60  * @user_data: user data specified when registering
61  *
62  * This callback will be called when @sess has @buffer ready for sending to
63  * all listening participants in this session.
64  *
65  * Returns: a #GstFlowReturn.
66  */
67 typedef GstFlowReturn (*RTPSessionSendRTP) (RTPSession *sess, RTPSource *src, gpointer data, gpointer user_data);
68
69 /**
70  * RTPSessionSendRTCP:
71  * @sess: an #RTPSession
72  * @src: the #RTPSource
73  * @buffer: the RTCP buffer ready for sending
74  * @eos: if an EOS event should be pushed
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,
83     gboolean eos, gpointer user_data);
84
85 /**
86  * RTPSessionSyncRTCP:
87  * @sess: an #RTPSession
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, 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  * RTPSessionRequestKeyUnit:
123  * @sess: an #RTPSession
124  * @all_headers: %TRUE if "all-headers" property should be set on the key unit
125  *  request
126  * @user_data: user data specified when registering
127  *
128  * Asks the encoder to produce a key unit as soon as possibly within the
129  * bandwidth constraints
130  */
131 typedef void (*RTPSessionRequestKeyUnit) (RTPSession *sess,
132     gboolean all_headers, gpointer user_data);
133
134 /**
135  * RTPSessionRequestTime:
136  * @sess: an #RTPSession
137  * @user_data: user data specified when registering
138  *
139  * This callback will be called when @sess needs the current time. The time
140  * should be returned as a #GstClockTime
141  */
142 typedef GstClockTime (*RTPSessionRequestTime) (RTPSession *sess,
143     gpointer user_data);
144
145 /**
146  * RTPSessionNotifyNACK:
147  * @sess: an #RTPSession
148  * @seqnum: the missing seqnum
149  * @blp: other missing seqnums
150  * @user_data: user data specified when registering
151  *
152  * Notifies of NACKed frames.
153  */
154 typedef void (*RTPSessionNotifyNACK) (RTPSession *sess,
155     guint16 seqnum, guint16 blp, gpointer user_data);
156
157 /**
158  * RTPSessionCallbacks:
159  * @RTPSessionProcessRTP: callback to process RTP packets
160  * @RTPSessionSendRTP: callback for sending RTP packets
161  * @RTPSessionSendRTCP: callback for sending RTCP packets
162  * @RTPSessionSyncRTCP: callback for handling SR packets
163  * @RTPSessionReconsider: callback for reconsidering the timeout
164  * @RTPSessionRequestKeyUnit: callback for requesting a new key unit
165  * @RTPSessionRequestTime: callback for requesting the current time
166  * @RTPSessionNotifyNACK: callback for notifying NACK
167  *
168  * These callbacks can be installed on the session manager to get notification
169  * when RTP and RTCP packets are ready for further processing. These callbacks
170  * are not implemented with signals for performance reasons.
171  */
172 typedef struct {
173   RTPSessionProcessRTP  process_rtp;
174   RTPSessionSendRTP     send_rtp;
175   RTPSessionSyncRTCP    sync_rtcp;
176   RTPSessionSendRTCP    send_rtcp;
177   RTPSessionClockRate   clock_rate;
178   RTPSessionReconsider  reconsider;
179   RTPSessionRequestKeyUnit request_key_unit;
180   RTPSessionRequestTime request_time;
181   RTPSessionNotifyNACK  notify_nack;
182 } RTPSessionCallbacks;
183
184 /**
185  * RTPSession:
186  * @lock: lock to protect the session
187  * @source: the source of this session
188  * @ssrcs: Hashtable of sources indexed by SSRC
189  * @num_sources: the number of sources
190  * @activecount: the number of active sources
191  * @callbacks: callbacks
192  * @user_data: user data passed in callbacks
193  * @stats: session statistics
194  *
195  * The RTP session manager object
196  */
197 struct _RTPSession {
198   GObject       object;
199
200   GMutex        lock;
201
202   guint         header_len;
203   guint         mtu;
204
205   GstStructure *sdes;
206
207   guint         probation;
208
209   /* bandwidths */
210   gboolean     recalc_bandwidth;
211   guint        bandwidth;
212   gdouble      rtcp_bandwidth;
213   guint        rtcp_rr_bandwidth;
214   guint        rtcp_rs_bandwidth;
215
216   guint32       suggested_ssrc;
217
218   /* for sender/receiver counting */
219   guint32       key;
220   guint32       mask_idx;
221   guint32       mask;
222   GHashTable   *ssrcs[32];
223   guint         total_sources;
224
225   guint16       generation;
226   GstClockTime  next_rtcp_check_time;
227   GstClockTime  last_rtcp_send_time;
228   GstClockTime  start_time;
229   gboolean      first_rtcp;
230   gboolean      allow_early;
231
232   GstClockTime  next_early_rtcp_time;
233
234   gboolean      scheduled_bye;
235
236   RTPSessionCallbacks   callbacks;
237   gpointer              process_rtp_user_data;
238   gpointer              send_rtp_user_data;
239   gpointer              send_rtcp_user_data;
240   gpointer              sync_rtcp_user_data;
241   gpointer              clock_rate_user_data;
242   gpointer              reconsider_user_data;
243   gpointer              request_key_unit_user_data;
244   gpointer              request_time_user_data;
245   gpointer              notify_nack_user_data;
246
247   RTPSessionStats stats;
248
249   gboolean      favor_new;
250   GstClockTime  rtcp_feedback_retention_window;
251   guint         rtcp_immediate_feedback_threshold;
252
253   GstClockTime last_keyframe_request;
254   gboolean     last_keyframe_all_headers;
255 };
256
257 /**
258  * RTPSessionClass:
259  * @on_new_ssrc: emited when a new source is found
260  * @on_bye_ssrc: emited when a source is gone
261  *
262  * The session class.
263  */
264 struct _RTPSessionClass {
265   GObjectClass   parent_class;
266
267   /* action signals */
268   RTPSource* (*get_source_by_ssrc) (RTPSession *sess, guint32 ssrc);
269
270   /* signals */
271   void (*on_new_ssrc)       (RTPSession *sess, RTPSource *source);
272   void (*on_ssrc_collision) (RTPSession *sess, RTPSource *source);
273   void (*on_ssrc_validated) (RTPSession *sess, RTPSource *source);
274   void (*on_ssrc_active)    (RTPSession *sess, RTPSource *source);
275   void (*on_ssrc_sdes)      (RTPSession *sess, RTPSource *source);
276   void (*on_bye_ssrc)       (RTPSession *sess, RTPSource *source);
277   void (*on_bye_timeout)    (RTPSession *sess, RTPSource *source);
278   void (*on_timeout)        (RTPSession *sess, RTPSource *source);
279   void (*on_sender_timeout) (RTPSession *sess, RTPSource *source);
280   gboolean (*on_sending_rtcp) (RTPSession *sess, GstBuffer *buffer,
281       gboolean early);
282   void (*on_feedback_rtcp)  (RTPSession *sess, guint type, guint fbtype,
283       guint sender_ssrc, guint media_ssrc, GstBuffer *fci);
284   void (*send_rtcp)         (RTPSession *sess, GstClockTime max_delay);
285 };
286
287 GType rtp_session_get_type (void);
288
289 /* create and configure */
290 RTPSession*     rtp_session_new           (void);
291 void            rtp_session_set_callbacks          (RTPSession *sess,
292                                                     RTPSessionCallbacks *callbacks,
293                                                     gpointer user_data);
294 void            rtp_session_set_process_rtp_callback   (RTPSession * sess,
295                                                     RTPSessionProcessRTP callback,
296                                                     gpointer user_data);
297 void            rtp_session_set_send_rtp_callback  (RTPSession * sess,
298                                                     RTPSessionSendRTP callback,
299                                                     gpointer user_data);
300 void            rtp_session_set_send_rtcp_callback   (RTPSession * sess,
301                                                     RTPSessionSendRTCP callback,
302                                                     gpointer user_data);
303 void            rtp_session_set_sync_rtcp_callback   (RTPSession * sess,
304                                                     RTPSessionSyncRTCP callback,
305                                                     gpointer user_data);
306 void            rtp_session_set_clock_rate_callback   (RTPSession * sess,
307                                                     RTPSessionClockRate callback,
308                                                     gpointer user_data);
309 void            rtp_session_set_reconsider_callback (RTPSession * sess,
310                                                     RTPSessionReconsider callback,
311                                                     gpointer user_data);
312 void            rtp_session_set_request_time_callback (RTPSession * sess,
313                                                     RTPSessionRequestTime callback,
314                                                     gpointer user_data);
315
316 void            rtp_session_set_bandwidth          (RTPSession *sess, gdouble bandwidth);
317 gdouble         rtp_session_get_bandwidth          (RTPSession *sess);
318 void            rtp_session_set_rtcp_fraction      (RTPSession *sess, gdouble fraction);
319 gdouble         rtp_session_get_rtcp_fraction      (RTPSession *sess);
320
321 GstStructure *  rtp_session_get_sdes_struct        (RTPSession *sess);
322 void            rtp_session_set_sdes_struct        (RTPSession *sess, const GstStructure *sdes);
323
324 /* handling sources */
325 guint32         rtp_session_suggest_ssrc           (RTPSession *sess);
326
327 gboolean        rtp_session_add_source             (RTPSession *sess, RTPSource *src);
328 guint           rtp_session_get_num_sources        (RTPSession *sess);
329 guint           rtp_session_get_num_active_sources (RTPSession *sess);
330 RTPSource*      rtp_session_get_source_by_ssrc     (RTPSession *sess, guint32 ssrc);
331 RTPSource*      rtp_session_create_source          (RTPSession *sess);
332
333 /* processing packets from receivers */
334 GstFlowReturn   rtp_session_process_rtp            (RTPSession *sess, GstBuffer *buffer,
335                                                     GstClockTime current_time,
336                                                     GstClockTime running_time,
337                                                     guint64 ntpnstime);
338 GstFlowReturn   rtp_session_process_rtcp           (RTPSession *sess, GstBuffer *buffer,
339                                                     GstClockTime current_time,
340                                                     guint64 ntpnstime);
341
342 /* processing packets for sending */
343 void            rtp_session_update_send_caps       (RTPSession *sess, GstCaps *caps);
344 GstFlowReturn   rtp_session_send_rtp               (RTPSession *sess, gpointer data, gboolean is_list,
345                                                     GstClockTime current_time, GstClockTime running_time);
346
347 /* scheduling bye */
348 void            rtp_session_mark_all_bye           (RTPSession *sess, const gchar *reason);
349 GstFlowReturn   rtp_session_schedule_bye           (RTPSession *sess, GstClockTime current_time);
350
351 /* get interval for next RTCP interval */
352 GstClockTime    rtp_session_next_timeout           (RTPSession *sess, GstClockTime current_time);
353 GstFlowReturn   rtp_session_on_timeout             (RTPSession *sess, GstClockTime current_time,
354                                                     guint64 ntpnstime, GstClockTime running_time);
355
356 /* request the transmittion of an early RTCP packet */
357 void            rtp_session_request_early_rtcp     (RTPSession * sess, GstClockTime current_time,
358                                                     GstClockTime max_delay);
359
360 /* Notify session of a request for a new key unit */
361 gboolean        rtp_session_request_key_unit       (RTPSession * sess,
362                                                     guint32 ssrc,
363                                                     gboolean fir,
364                                                     gint count);
365 gboolean        rtp_session_request_nack           (RTPSession * sess,
366                                                     guint32 ssrc,
367                                                     guint16 seqnum,
368                                                     GstClockTime max_delay);
369
370
371 #endif /* __RTP_SESSION_H__ */