rtpsession: relate received FIRs and PLIs to source
[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  * @user_data: user data specified when registering
75  *
76  * This callback will be called when @sess has @buffer ready for sending to
77  * all listening participants in this session.
78  *
79  * Returns: a #GstFlowReturn.
80  */
81 typedef GstFlowReturn (*RTPSessionSendRTCP) (RTPSession *sess, RTPSource *src, GstBuffer *buffer,
82     gpointer user_data);
83
84 /**
85  * RTPSessionSyncRTCP:
86  * @sess: an #RTPSession
87  * @buffer: the RTCP buffer ready for synchronisation
88  * @user_data: user data specified when registering
89  *
90  * This callback will be called when @sess has an SR @buffer ready for doing
91  * synchronisation between streams.
92  *
93  * Returns: a #GstFlowReturn.
94  */
95 typedef GstFlowReturn (*RTPSessionSyncRTCP) (RTPSession *sess, GstBuffer *buffer, gpointer user_data);
96
97 /**
98  * RTPSessionClockRate:
99  * @sess: an #RTPSession
100  * @payload: the payload
101  * @user_data: user data specified when registering
102  *
103  * This callback will be called when @sess needs the clock-rate of @payload.
104  *
105  * Returns: the clock-rate of @pt.
106  */
107 typedef gint (*RTPSessionClockRate) (RTPSession *sess, guint8 payload, gpointer user_data);
108
109 /**
110  * RTPSessionReconsider:
111  * @sess: an #RTPSession
112  * @user_data: user data specified when registering
113  *
114  * This callback will be called when @sess needs to cancel the current timeout.
115  * The currently running timeout should be canceled and a new reporting interval
116  * should be requested from @sess.
117  */
118 typedef void (*RTPSessionReconsider) (RTPSession *sess, gpointer user_data);
119
120 /**
121  * RTPSessionRequestKeyUnit:
122  * @sess: an #RTPSession
123  * @ssrc: SSRC of the source related to the key unit request
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, guint32 ssrc,
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  * @ssrc: SSRC of requested stream
151  * @user_data: user data specified when registering
152  *
153  * Notifies of NACKed frames.
154  */
155 typedef void (*RTPSessionNotifyNACK) (RTPSession *sess,
156     guint16 seqnum, guint16 blp, guint32 ssrc, gpointer user_data);
157
158 /**
159  * RTPSessionReconfigure:
160  * @sess: an #RTPSession
161  * @user_data: user data specified when registering
162  *
163  * This callback will be called when @sess wants to reconfigure the
164  * negotiated parameters.
165  */
166 typedef void (*RTPSessionReconfigure) (RTPSession *sess, gpointer user_data);
167
168 /**
169  * RTPSessionCallbacks:
170  * @RTPSessionProcessRTP: callback to process RTP packets
171  * @RTPSessionSendRTP: callback for sending RTP packets
172  * @RTPSessionSendRTCP: callback for sending RTCP packets
173  * @RTPSessionSyncRTCP: callback for handling SR packets
174  * @RTPSessionReconsider: callback for reconsidering the timeout
175  * @RTPSessionRequestKeyUnit: callback for requesting a new key unit
176  * @RTPSessionRequestTime: callback for requesting the current time
177  * @RTPSessionNotifyNACK: callback for notifying NACK
178  * @RTPSessionReconfigure: callback for requesting reconfiguration
179  *
180  * These callbacks can be installed on the session manager to get notification
181  * when RTP and RTCP packets are ready for further processing. These callbacks
182  * are not implemented with signals for performance reasons.
183  */
184 typedef struct {
185   RTPSessionProcessRTP  process_rtp;
186   RTPSessionSendRTP     send_rtp;
187   RTPSessionSyncRTCP    sync_rtcp;
188   RTPSessionSendRTCP    send_rtcp;
189   RTPSessionClockRate   clock_rate;
190   RTPSessionReconsider  reconsider;
191   RTPSessionRequestKeyUnit request_key_unit;
192   RTPSessionRequestTime request_time;
193   RTPSessionNotifyNACK  notify_nack;
194   RTPSessionReconfigure reconfigure;
195 } RTPSessionCallbacks;
196
197 /**
198  * RTPSession:
199  * @lock: lock to protect the session
200  * @source: the source of this session
201  * @ssrcs: Hashtable of sources indexed by SSRC
202  * @num_sources: the number of sources
203  * @activecount: the number of active sources
204  * @callbacks: callbacks
205  * @user_data: user data passed in callbacks
206  * @stats: session statistics
207  * @conflicting_addresses: GList of conflicting addresses
208  *
209  * The RTP session manager object
210  */
211 struct _RTPSession {
212   GObject       object;
213
214   GMutex        lock;
215
216   guint         header_len;
217   guint         mtu;
218
219   GstStructure *sdes;
220
221   guint         probation;
222   guint32       max_dropout_time;
223   guint32       max_misorder_time;
224
225   GstRTPProfile rtp_profile;
226
227   gboolean      reduced_size_rtcp;
228
229   /* bandwidths */
230   gboolean     recalc_bandwidth;
231   guint        bandwidth;
232   gdouble      rtcp_bandwidth;
233   guint        rtcp_rr_bandwidth;
234   guint        rtcp_rs_bandwidth;
235
236   guint32       suggested_ssrc;
237   gboolean      internal_ssrc_set;
238   gboolean      internal_ssrc_from_caps_or_property;
239
240   /* for sender/receiver counting */
241   guint32       key;
242   guint32       mask_idx;
243   guint32       mask;
244   GHashTable   *ssrcs[32];
245   guint         total_sources;
246
247   guint16       generation;
248   GstClockTime  next_rtcp_check_time; /* tn */
249   GstClockTime  last_rtcp_check_time; /* tp */
250   GstClockTime  last_rtcp_send_time;  /* t_rr_last */
251   GstClockTime  last_rtcp_interval;   /* T_rr */
252   GstClockTime  start_time;
253   gboolean      first_rtcp;
254   gboolean      allow_early;
255
256   GstClockTime  next_early_rtcp_time;
257
258   gboolean      scheduled_bye;
259
260   RTPSessionCallbacks   callbacks;
261   gpointer              process_rtp_user_data;
262   gpointer              send_rtp_user_data;
263   gpointer              send_rtcp_user_data;
264   gpointer              sync_rtcp_user_data;
265   gpointer              clock_rate_user_data;
266   gpointer              reconsider_user_data;
267   gpointer              request_key_unit_user_data;
268   gpointer              request_time_user_data;
269   gpointer              notify_nack_user_data;
270   gpointer              reconfigure_user_data;
271
272   RTPSessionStats stats;
273   RTPSessionStats bye_stats;
274
275   gboolean      favor_new;
276   GstClockTime  rtcp_feedback_retention_window;
277   guint         rtcp_immediate_feedback_threshold;
278
279   gboolean      is_doing_ptp;
280
281   GList         *conflicting_addresses;
282 };
283
284 /**
285  * RTPSessionClass:
286  * @on_new_ssrc: emited when a new source is found
287  * @on_bye_ssrc: emited when a source is gone
288  *
289  * The session class.
290  */
291 struct _RTPSessionClass {
292   GObjectClass   parent_class;
293
294   /* action signals */
295   RTPSource* (*get_source_by_ssrc) (RTPSession *sess, guint32 ssrc);
296
297   /* signals */
298   void (*on_new_ssrc)       (RTPSession *sess, RTPSource *source);
299   void (*on_ssrc_collision) (RTPSession *sess, RTPSource *source);
300   void (*on_ssrc_validated) (RTPSession *sess, RTPSource *source);
301   void (*on_ssrc_active)    (RTPSession *sess, RTPSource *source);
302   void (*on_ssrc_sdes)      (RTPSession *sess, RTPSource *source);
303   void (*on_bye_ssrc)       (RTPSession *sess, RTPSource *source);
304   void (*on_bye_timeout)    (RTPSession *sess, RTPSource *source);
305   void (*on_timeout)        (RTPSession *sess, RTPSource *source);
306   void (*on_sender_timeout) (RTPSession *sess, RTPSource *source);
307   gboolean (*on_sending_rtcp) (RTPSession *sess, GstBuffer *buffer,
308       gboolean early);
309   void (*on_app_rtcp)       (RTPSession *sess, guint subtype, guint ssrc,
310       const gchar *name, GstBuffer *data);
311   void (*on_feedback_rtcp)  (RTPSession *sess, guint type, guint fbtype,
312       guint sender_ssrc, guint media_ssrc, GstBuffer *fci);
313   gboolean (*send_rtcp)     (RTPSession *sess, GstClockTime max_delay);
314   void (*on_receiving_rtcp) (RTPSession *sess, GstBuffer *buffer);
315   void (*on_new_sender_ssrc)     (RTPSession *sess, RTPSource *source);
316   void (*on_sender_ssrc_active)  (RTPSession *sess, RTPSource *source);
317 };
318
319 GType rtp_session_get_type (void);
320
321 /* create and configure */
322 RTPSession*     rtp_session_new           (void);
323 void            rtp_session_set_callbacks          (RTPSession *sess,
324                                                     RTPSessionCallbacks *callbacks,
325                                                     gpointer user_data);
326 void            rtp_session_set_process_rtp_callback   (RTPSession * sess,
327                                                     RTPSessionProcessRTP callback,
328                                                     gpointer user_data);
329 void            rtp_session_set_send_rtp_callback  (RTPSession * sess,
330                                                     RTPSessionSendRTP callback,
331                                                     gpointer user_data);
332 void            rtp_session_set_send_rtcp_callback   (RTPSession * sess,
333                                                     RTPSessionSendRTCP callback,
334                                                     gpointer user_data);
335 void            rtp_session_set_sync_rtcp_callback   (RTPSession * sess,
336                                                     RTPSessionSyncRTCP callback,
337                                                     gpointer user_data);
338 void            rtp_session_set_clock_rate_callback   (RTPSession * sess,
339                                                     RTPSessionClockRate callback,
340                                                     gpointer user_data);
341 void            rtp_session_set_reconsider_callback (RTPSession * sess,
342                                                     RTPSessionReconsider callback,
343                                                     gpointer user_data);
344 void            rtp_session_set_request_time_callback (RTPSession * sess,
345                                                     RTPSessionRequestTime callback,
346                                                     gpointer user_data);
347
348 void            rtp_session_set_bandwidth          (RTPSession *sess, gdouble bandwidth);
349 gdouble         rtp_session_get_bandwidth          (RTPSession *sess);
350 void            rtp_session_set_rtcp_fraction      (RTPSession *sess, gdouble fraction);
351 gdouble         rtp_session_get_rtcp_fraction      (RTPSession *sess);
352
353 GstStructure *  rtp_session_get_sdes_struct        (RTPSession *sess);
354 void            rtp_session_set_sdes_struct        (RTPSession *sess, const GstStructure *sdes);
355
356 /* handling sources */
357 guint32         rtp_session_suggest_ssrc           (RTPSession *sess, gboolean *is_random);
358
359 gboolean        rtp_session_add_source             (RTPSession *sess, RTPSource *src);
360 guint           rtp_session_get_num_sources        (RTPSession *sess);
361 guint           rtp_session_get_num_active_sources (RTPSession *sess);
362 RTPSource*      rtp_session_get_source_by_ssrc     (RTPSession *sess, guint32 ssrc);
363 RTPSource*      rtp_session_create_source          (RTPSession *sess);
364
365 /* processing packets from receivers */
366 GstFlowReturn   rtp_session_process_rtp            (RTPSession *sess, GstBuffer *buffer,
367                                                     GstClockTime current_time,
368                                                     GstClockTime running_time,
369                                                     guint64 ntpnstime);
370 GstFlowReturn   rtp_session_process_rtcp           (RTPSession *sess, GstBuffer *buffer,
371                                                     GstClockTime current_time,
372                                                     guint64 ntpnstime);
373
374 /* processing packets for sending */
375 void            rtp_session_update_send_caps       (RTPSession *sess, GstCaps *caps);
376 GstFlowReturn   rtp_session_send_rtp               (RTPSession *sess, gpointer data, gboolean is_list,
377                                                     GstClockTime current_time, GstClockTime running_time);
378
379 /* scheduling bye */
380 void            rtp_session_mark_all_bye           (RTPSession *sess, const gchar *reason);
381 GstFlowReturn   rtp_session_schedule_bye           (RTPSession *sess, GstClockTime current_time);
382
383 /* get interval for next RTCP interval */
384 GstClockTime    rtp_session_next_timeout           (RTPSession *sess, GstClockTime current_time);
385 GstFlowReturn   rtp_session_on_timeout             (RTPSession *sess, GstClockTime current_time,
386                                                     guint64 ntpnstime, GstClockTime running_time);
387
388 /* request the transmittion of an early RTCP packet */
389 gboolean        rtp_session_request_early_rtcp     (RTPSession * sess, GstClockTime current_time,
390                                                     GstClockTime max_delay);
391
392 /* Notify session of a request for a new key unit */
393 gboolean        rtp_session_request_key_unit       (RTPSession * sess,
394                                                     guint32 ssrc,
395                                                     gboolean fir,
396                                                     gint count);
397 gboolean        rtp_session_request_nack           (RTPSession * sess,
398                                                     guint32 ssrc,
399                                                     guint16 seqnum,
400                                                     GstClockTime max_delay);
401
402
403 #endif /* __RTP_SESSION_H__ */