rtpsession: Handle RTCP-SR-REQ (RFC6051) RTCP feedback message
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / 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 #include "rtptwcc.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  * @data: the RTP buffer or buffer list ready for sending
61  * @user_data: user data specified when registering
62  *
63  * This callback will be called when @sess has @data (a buffer or buffer list)
64  * ready for sending to all listening participants in this session.
65  *
66  * Returns: a #GstFlowReturn.
67  */
68 typedef GstFlowReturn (*RTPSessionSendRTP) (RTPSession *sess, RTPSource *src, gpointer data, gpointer user_data);
69
70 /**
71  * RTPSessionSendRTCP:
72  * @sess: an #RTPSession
73  * @src: the #RTPSource
74  * @buffer: the RTCP buffer ready for sending
75  * @eos: if an EOS event should be pushed
76  * @user_data: user data specified when registering
77  *
78  * This callback will be called when @sess has @buffer ready for sending to
79  * all listening participants in this session.
80  *
81  * Returns: a #GstFlowReturn.
82  */
83 typedef GstFlowReturn (*RTPSessionSendRTCP) (RTPSession *sess, RTPSource *src, GstBuffer *buffer,
84     gboolean eos, gpointer user_data);
85
86 /**
87  * RTPSessionSyncRTCP:
88  * @sess: an #RTPSession
89  * @buffer: the RTCP buffer ready for synchronisation
90  * @user_data: user data specified when registering
91  *
92  * This callback will be called when @sess has an SR @buffer ready for doing
93  * synchronisation between streams.
94  *
95  * Returns: a #GstFlowReturn.
96  */
97 typedef GstFlowReturn (*RTPSessionSyncRTCP) (RTPSession *sess, GstBuffer *buffer, gpointer user_data);
98
99 /**
100  * RTPSessionClockRate:
101  * @sess: an #RTPSession
102  * @payload: the payload
103  * @user_data: user data specified when registering
104  *
105  * This callback will be called when @sess needs the clock-rate of @payload.
106  *
107  * Returns: the clock-rate of @pt.
108  */
109 typedef gint (*RTPSessionClockRate) (RTPSession *sess, guint8 payload, gpointer user_data);
110
111 /**
112  * RTPSessionReconsider:
113  * @sess: an #RTPSession
114  * @user_data: user data specified when registering
115  *
116  * This callback will be called when @sess needs to cancel the current timeout.
117  * The currently running timeout should be canceled and a new reporting interval
118  * should be requested from @sess.
119  */
120 typedef void (*RTPSessionReconsider) (RTPSession *sess, gpointer user_data);
121
122 /**
123  * RTPSessionRequestKeyUnit:
124  * @sess: an #RTPSession
125  * @ssrc: SSRC of the source related to the key unit request
126  * @all_headers: %TRUE if "all-headers" property should be set on the key unit
127  *  request
128  * @user_data: user data specified when registering
129  *
130  * Asks the encoder to produce a key unit as soon as possibly within the
131  * bandwidth constraints
132  */
133 typedef void (*RTPSessionRequestKeyUnit) (RTPSession *sess, guint32 ssrc,
134     gboolean all_headers, gpointer user_data);
135
136 /**
137  * RTPSessionRequestTime:
138  * @sess: an #RTPSession
139  * @user_data: user data specified when registering
140  *
141  * This callback will be called when @sess needs the current time. The time
142  * should be returned as a #GstClockTime
143  */
144 typedef GstClockTime (*RTPSessionRequestTime) (RTPSession *sess,
145     gpointer user_data);
146
147 /**
148  * RTPSessionNotifyNACK:
149  * @sess: an #RTPSession
150  * @seqnum: the missing seqnum
151  * @blp: other missing seqnums
152  * @ssrc: SSRC of requested stream
153  * @user_data: user data specified when registering
154  *
155  * Notifies of NACKed frames.
156  */
157 typedef void (*RTPSessionNotifyNACK) (RTPSession *sess,
158     guint16 seqnum, guint16 blp, guint32 ssrc, gpointer user_data);
159
160 /**
161  * RTPSessionNotifyTWCC:
162  * @user_data: user data specified when registering
163  *
164  * Notifies of Transport-wide congestion control packets and stats.
165  */
166 typedef void (*RTPSessionNotifyTWCC) (RTPSession *sess,
167     GstStructure * twcc_packets, GstStructure * twcc_stats, gpointer user_data);
168
169 /**
170  * RTPSessionReconfigure:
171  * @sess: an #RTPSession
172  * @user_data: user data specified when registering
173  *
174  * This callback will be called when @sess wants to reconfigure the
175  * negotiated parameters.
176  */
177 typedef void (*RTPSessionReconfigure) (RTPSession *sess, gpointer user_data);
178
179 /**
180  * RTPSessionNotifyEarlyRTCP:
181  * @sess: an #RTPSession
182  * @user_data: user data specified when registering
183  *
184  * Notifies of early RTCP being requested
185  */
186 typedef void (*RTPSessionNotifyEarlyRTCP) (RTPSession *sess,
187     gpointer user_data);
188
189 /**
190  * RTPSessionCallbacks:
191  * @RTPSessionProcessRTP: callback to process RTP packets
192  * @RTPSessionSendRTP: callback for sending RTP packets
193  * @RTPSessionSendRTCP: callback for sending RTCP packets
194  * @RTPSessionSyncRTCP: callback for handling SR packets
195  * @RTPSessionReconsider: callback for reconsidering the timeout
196  * @RTPSessionRequestKeyUnit: callback for requesting a new key unit
197  * @RTPSessionRequestTime: callback for requesting the current time
198  * @RTPSessionNotifyNACK: callback for notifying NACK
199  * @RTPSessionNotifyTWCC: callback for notifying TWCC
200  * @RTPSessionReconfigure: callback for requesting reconfiguration
201  * @RTPSessionNotifyEarlyRTCP: callback for notifying early RTCP
202  *
203  * These callbacks can be installed on the session manager to get notification
204  * when RTP and RTCP packets are ready for further processing. These callbacks
205  * are not implemented with signals for performance reasons.
206  */
207 typedef struct {
208   RTPSessionProcessRTP  process_rtp;
209   RTPSessionSendRTP     send_rtp;
210   RTPSessionSyncRTCP    sync_rtcp;
211   RTPSessionSendRTCP    send_rtcp;
212   RTPSessionClockRate   clock_rate;
213   RTPSessionReconsider  reconsider;
214   RTPSessionRequestKeyUnit request_key_unit;
215   RTPSessionRequestTime request_time;
216   RTPSessionNotifyNACK  notify_nack;
217   RTPSessionNotifyTWCC  notify_twcc;
218   RTPSessionReconfigure reconfigure;
219   RTPSessionNotifyEarlyRTCP notify_early_rtcp;
220 } RTPSessionCallbacks;
221
222 /**
223  * RTPSession:
224  * @lock: lock to protect the session
225  * @source: the source of this session
226  * @ssrcs: Hashtable of sources indexed by SSRC
227  * @num_sources: the number of sources
228  * @activecount: the number of active sources
229  * @callbacks: callbacks
230  * @user_data: user data passed in callbacks
231  * @stats: session statistics
232  * @conflicting_addresses: GList of conflicting addresses
233  *
234  * The RTP session manager object
235  */
236 struct _RTPSession {
237   GObject       object;
238
239   GMutex        lock;
240
241   guint         header_len;
242   guint         mtu;
243
244   GstStructure *sdes;
245
246   guint         probation;
247   guint32       max_dropout_time;
248   guint32       max_misorder_time;
249
250   GstRTPProfile rtp_profile;
251
252   gboolean      reduced_size_rtcp;
253
254   /* bandwidths */
255   gboolean     recalc_bandwidth;
256   guint        bandwidth;
257   gdouble      rtcp_bandwidth;
258   guint        rtcp_rr_bandwidth;
259   guint        rtcp_rs_bandwidth;
260
261   guint32       suggested_ssrc;
262   gboolean      internal_ssrc_set;
263   gboolean      internal_ssrc_from_caps_or_property;
264
265   /* for sender/receiver counting */
266   guint32       key;
267   guint32       mask_idx;
268   guint32       mask;
269   GHashTable   *ssrcs[32];
270   guint         total_sources;
271
272   guint16       generation;
273   GstClockTime  next_rtcp_check_time; /* tn */
274   GstClockTime  last_rtcp_check_time; /* tp */
275   GstClockTime  last_rtcp_send_time;  /* t_rr_last */
276   GstClockTime  last_rtcp_interval;   /* T_rr */
277   GstClockTime  start_time;
278   gboolean      first_rtcp;
279   gboolean      allow_early;
280
281   GstClockTime  next_early_rtcp_time;
282
283   gboolean      sr_req_pending;
284   gboolean      scheduled_bye;
285
286   RTPSessionCallbacks   callbacks;
287   gpointer              process_rtp_user_data;
288   gpointer              send_rtp_user_data;
289   gpointer              send_rtcp_user_data;
290   gpointer              sync_rtcp_user_data;
291   gpointer              clock_rate_user_data;
292   gpointer              reconsider_user_data;
293   gpointer              request_key_unit_user_data;
294   gpointer              request_time_user_data;
295   gpointer              notify_nack_user_data;
296   gpointer              notify_twcc_user_data;
297   gpointer              reconfigure_user_data;
298   gpointer              notify_early_rtcp_user_data;
299
300   RTPSessionStats stats;
301   RTPSessionStats bye_stats;
302
303   gboolean      favor_new;
304   GstClockTime  rtcp_feedback_retention_window;
305   guint         rtcp_immediate_feedback_threshold;
306
307   gboolean      is_doing_ptp;
308
309   GList         *conflicting_addresses;
310
311   gboolean timestamp_sender_reports;
312
313   /* RFC6051 64-bit NTP header extension */
314   guint8 send_ntp64_ext_id;
315
316   /* Transport-wide cc-extension */
317   RTPTWCCManager *twcc;
318   RTPTWCCStats *twcc_stats;
319 };
320
321 /**
322  * RTPSessionClass:
323  * @on_new_ssrc: emitted when a new source is found
324  * @on_bye_ssrc: emitted when a source is gone
325  *
326  * The session class.
327  */
328 struct _RTPSessionClass {
329   GObjectClass   parent_class;
330
331   /* action signals */
332   RTPSource* (*get_source_by_ssrc) (RTPSession *sess, guint32 ssrc);
333
334   /* signals */
335   void (*on_new_ssrc)       (RTPSession *sess, RTPSource *source);
336   void (*on_ssrc_collision) (RTPSession *sess, RTPSource *source);
337   void (*on_ssrc_validated) (RTPSession *sess, RTPSource *source);
338   void (*on_ssrc_active)    (RTPSession *sess, RTPSource *source);
339   void (*on_ssrc_sdes)      (RTPSession *sess, RTPSource *source);
340   void (*on_bye_ssrc)       (RTPSession *sess, RTPSource *source);
341   void (*on_bye_timeout)    (RTPSession *sess, RTPSource *source);
342   void (*on_timeout)        (RTPSession *sess, RTPSource *source);
343   void (*on_sender_timeout) (RTPSession *sess, RTPSource *source);
344   gboolean (*on_sending_rtcp) (RTPSession *sess, GstBuffer *buffer,
345       gboolean early);
346   void (*on_app_rtcp)       (RTPSession *sess, guint subtype, guint ssrc,
347       const gchar *name, GstBuffer *data);
348   void (*on_feedback_rtcp)  (RTPSession *sess, guint type, guint fbtype,
349       guint sender_ssrc, guint media_ssrc, GstBuffer *fci);
350   gboolean (*send_rtcp)     (RTPSession *sess, GstClockTime max_delay);
351   void (*on_receiving_rtcp) (RTPSession *sess, GstBuffer *buffer);
352   void (*on_new_sender_ssrc)     (RTPSession *sess, RTPSource *source);
353   void (*on_sender_ssrc_active)  (RTPSession *sess, RTPSource *source);
354   guint (*on_sending_nacks) (RTPSession *sess, guint sender_ssrc,
355       guint media_ssrc, GArray *nacks, GstBuffer *buffer);
356 };
357
358 GType rtp_session_get_type (void);
359
360 /* create and configure */
361 RTPSession*     rtp_session_new           (void);
362 void            rtp_session_reset                  (RTPSession *sess);
363 void            rtp_session_set_callbacks          (RTPSession *sess,
364                                                     RTPSessionCallbacks *callbacks,
365                                                     gpointer user_data);
366 void            rtp_session_set_process_rtp_callback   (RTPSession * sess,
367                                                     RTPSessionProcessRTP callback,
368                                                     gpointer user_data);
369 void            rtp_session_set_send_rtp_callback  (RTPSession * sess,
370                                                     RTPSessionSendRTP callback,
371                                                     gpointer user_data);
372 void            rtp_session_set_send_rtcp_callback   (RTPSession * sess,
373                                                     RTPSessionSendRTCP callback,
374                                                     gpointer user_data);
375 void            rtp_session_set_sync_rtcp_callback   (RTPSession * sess,
376                                                     RTPSessionSyncRTCP callback,
377                                                     gpointer user_data);
378 void            rtp_session_set_clock_rate_callback   (RTPSession * sess,
379                                                     RTPSessionClockRate callback,
380                                                     gpointer user_data);
381 void            rtp_session_set_reconsider_callback (RTPSession * sess,
382                                                     RTPSessionReconsider callback,
383                                                     gpointer user_data);
384 void            rtp_session_set_request_time_callback (RTPSession * sess,
385                                                     RTPSessionRequestTime callback,
386                                                     gpointer user_data);
387
388 void            rtp_session_set_bandwidth          (RTPSession *sess, gdouble bandwidth);
389 gdouble         rtp_session_get_bandwidth          (RTPSession *sess);
390 void            rtp_session_set_rtcp_fraction      (RTPSession *sess, gdouble fraction);
391 gdouble         rtp_session_get_rtcp_fraction      (RTPSession *sess);
392
393 GstStructure *  rtp_session_get_sdes_struct        (RTPSession *sess);
394 void            rtp_session_set_sdes_struct        (RTPSession *sess, const GstStructure *sdes);
395
396 /* handling sources */
397 guint32         rtp_session_suggest_ssrc           (RTPSession *sess, gboolean *is_random);
398
399 gboolean        rtp_session_add_source             (RTPSession *sess, RTPSource *src);
400 guint           rtp_session_get_num_sources        (RTPSession *sess);
401 guint           rtp_session_get_num_active_sources (RTPSession *sess);
402 RTPSource*      rtp_session_get_source_by_ssrc     (RTPSession *sess, guint32 ssrc);
403
404 /* processing packets from receivers */
405 GstFlowReturn   rtp_session_process_rtp            (RTPSession *sess, GstBuffer *buffer,
406                                                     GstClockTime current_time,
407                                                     GstClockTime running_time,
408                                                     guint64 ntpnstime);
409 GstFlowReturn   rtp_session_process_rtcp           (RTPSession *sess, GstBuffer *buffer,
410                                                     GstClockTime current_time,
411                                                     GstClockTime running_time,
412                                                     guint64 ntpnstime);
413
414 /* processing packets for sending */
415 void            rtp_session_update_send_caps       (RTPSession *sess, GstCaps *caps);
416 GstFlowReturn   rtp_session_send_rtp               (RTPSession *sess, gpointer data, gboolean is_list,
417                                                     GstClockTime current_time, GstClockTime running_time,
418                                                     guint64 ntpnstime);
419
420 /* scheduling bye */
421 void            rtp_session_mark_all_bye           (RTPSession *sess, const gchar *reason);
422 GstFlowReturn   rtp_session_schedule_bye           (RTPSession *sess, GstClockTime current_time);
423
424 /* get interval for next RTCP interval */
425 GstClockTime    rtp_session_next_timeout           (RTPSession *sess, GstClockTime current_time);
426 GstFlowReturn   rtp_session_on_timeout             (RTPSession *sess, GstClockTime current_time,
427                                                     guint64 ntpnstime, GstClockTime running_time);
428
429 /* request the transmission of an early RTCP packet */
430 gboolean        rtp_session_request_early_rtcp     (RTPSession * sess, GstClockTime current_time,
431                                                     GstClockTime max_delay);
432
433 /* Notify session of a request for a new key unit */
434 gboolean        rtp_session_request_key_unit       (RTPSession * sess,
435                                                     guint32 ssrc,
436                                                     gboolean fir,
437                                                     gint count);
438 gboolean        rtp_session_request_nack           (RTPSession * sess,
439                                                     guint32 ssrc,
440                                                     guint16 seqnum,
441                                                     GstClockTime max_delay);
442
443 void            rtp_session_update_recv_caps_structure (RTPSession * sess, const GstStructure * s);
444
445
446 #endif /* __RTP_SESSION_H__ */