tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.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, 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  * @src: the #RTPSource
90  * @buffer: the RTCP buffer ready for synchronisation
91  * @user_data: user data specified when registering
92  *
93  * This callback will be called when @sess has an SR @buffer ready for doing
94  * synchronisation between streams.
95  *
96  * Returns: a #GstFlowReturn.
97  */
98 typedef GstFlowReturn (*RTPSessionSyncRTCP) (RTPSession *sess, RTPSource *src, GstBuffer *buffer, gpointer user_data);
99
100 /**
101  * RTPSessionClockRate:
102  * @sess: an #RTPSession
103  * @payload: the payload
104  * @user_data: user data specified when registering
105  *
106  * This callback will be called when @sess needs the clock-rate of @payload.
107  *
108  * Returns: the clock-rate of @pt.
109  */
110 typedef gint (*RTPSessionClockRate) (RTPSession *sess, guint8 payload, gpointer user_data);
111
112 /**
113  * RTPSessionReconsider:
114  * @sess: an #RTPSession
115  * @user_data: user data specified when registering
116  *
117  * This callback will be called when @sess needs to cancel the current timeout. 
118  * The currently running timeout should be canceled and a new reporting interval
119  * should be requested from @sess.
120  */
121 typedef void (*RTPSessionReconsider) (RTPSession *sess, gpointer user_data);
122
123 /**
124  * RTPSessionRequestKeyUnit:
125  * @sess: an #RTPSession
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,
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  * RTPSessionCallbacks:
149  * @RTPSessionProcessRTP: callback to process RTP packets
150  * @RTPSessionSendRTP: callback for sending RTP packets
151  * @RTPSessionSendRTCP: callback for sending RTCP packets
152  * @RTPSessionSyncRTCP: callback for handling SR packets
153  * @RTPSessionReconsider: callback for reconsidering the timeout
154  * @RTPSessionRequestKeyUnit: callback for requesting a new key unit
155  *
156  * These callbacks can be installed on the session manager to get notification
157  * when RTP and RTCP packets are ready for further processing. These callbacks
158  * are not implemented with signals for performance reasons.
159  */
160 typedef struct {
161   RTPSessionProcessRTP  process_rtp;
162   RTPSessionSendRTP     send_rtp;
163   RTPSessionSyncRTCP    sync_rtcp;
164   RTPSessionSendRTCP    send_rtcp;
165   RTPSessionClockRate   clock_rate;
166   RTPSessionReconsider  reconsider;
167   RTPSessionRequestKeyUnit request_key_unit;
168   RTPSessionRequestTime request_time;
169 } RTPSessionCallbacks;
170
171 /**
172  * RTPSession:
173  * @lock: lock to protect the session
174  * @source: the source of this session
175  * @ssrcs: Hashtable of sources indexed by SSRC
176  * @cnames: Hashtable of sources indexed by CNAME
177  * @num_sources: the number of sources
178  * @activecount: the number of active sources
179  * @callbacks: callbacks
180  * @user_data: user data passed in callbacks
181  * @stats: session statistics
182  *
183  * The RTP session manager object
184  */
185 struct _RTPSession {
186   GObject       object;
187
188   GMutex       *lock;
189
190   guint         header_len;
191   guint         mtu;
192
193   /* bandwidths */
194   gboolean     recalc_bandwidth;
195   guint        bandwidth;
196   gdouble      rtcp_bandwidth;
197   guint        rtcp_rr_bandwidth;
198   guint        rtcp_rs_bandwidth;
199
200   RTPSource    *source;
201
202   /* for sender/receiver counting */
203   guint32       key;
204   guint32       mask_idx;
205   guint32       mask;
206   GHashTable   *ssrcs[32];
207   GHashTable   *cnames;
208   guint         total_sources;
209
210   GstClockTime  next_rtcp_check_time;
211   GstClockTime  last_rtcp_send_time;
212   GstClockTime  start_time;
213   gboolean      first_rtcp;
214   gboolean      allow_early;
215
216   GstClockTime  next_early_rtcp_time;
217
218   gchar        *bye_reason;
219   gboolean      sent_bye;
220
221   RTPSessionCallbacks   callbacks;
222   gpointer              process_rtp_user_data;
223   gpointer              send_rtp_user_data;
224   gpointer              send_rtcp_user_data;
225   gpointer              sync_rtcp_user_data;
226   gpointer              clock_rate_user_data;
227   gpointer              reconsider_user_data;
228   gpointer              request_key_unit_user_data;
229   gpointer              request_time_user_data;
230
231   RTPSessionStats stats;
232
233   gboolean      change_ssrc;
234   gboolean      favor_new;
235   GstClockTime  rtcp_feedback_retention_window;
236   guint         rtcp_immediate_feedback_threshold;
237
238   GstClockTime last_keyframe_request;
239   gboolean     last_keyframe_all_headers;
240 };
241
242 /**
243  * RTPSessionClass:
244  * @on_new_ssrc: emited when a new source is found
245  * @on_bye_ssrc: emited when a source is gone
246  *
247  * The session class.
248  */
249 struct _RTPSessionClass {
250   GObjectClass   parent_class;
251
252   /* action signals */
253   RTPSource* (*get_source_by_ssrc) (RTPSession *sess, guint32 ssrc);
254
255   /* signals */
256   void (*on_new_ssrc)       (RTPSession *sess, RTPSource *source);
257   void (*on_ssrc_collision) (RTPSession *sess, RTPSource *source);
258   void (*on_ssrc_validated) (RTPSession *sess, RTPSource *source);
259   void (*on_ssrc_active)    (RTPSession *sess, RTPSource *source);
260   void (*on_ssrc_sdes)      (RTPSession *sess, RTPSource *source);
261   void (*on_bye_ssrc)       (RTPSession *sess, RTPSource *source);
262   void (*on_bye_timeout)    (RTPSession *sess, RTPSource *source);
263   void (*on_timeout)        (RTPSession *sess, RTPSource *source);
264   void (*on_sender_timeout) (RTPSession *sess, RTPSource *source);
265   gboolean (*on_sending_rtcp) (RTPSession *sess, GstBuffer *buffer,
266       gboolean early);
267   void (*on_feedback_rtcp)  (RTPSession *sess, guint type, guint fbtype,
268       guint sender_ssrc, guint media_ssrc, GstBuffer *fci);
269   void (*send_rtcp)         (RTPSession *sess, GstClockTimeDiff max_delay);
270 };
271
272 GType rtp_session_get_type (void);
273
274 /* create and configure */
275 RTPSession*     rtp_session_new           (void);
276 void            rtp_session_set_callbacks          (RTPSession *sess,
277                                                     RTPSessionCallbacks *callbacks,
278                                                     gpointer user_data);
279 void            rtp_session_set_process_rtp_callback   (RTPSession * sess,
280                                                     RTPSessionProcessRTP callback,
281                                                     gpointer user_data);
282 void            rtp_session_set_send_rtp_callback  (RTPSession * sess,
283                                                     RTPSessionSendRTP callback,
284                                                     gpointer user_data);
285 void            rtp_session_set_send_rtcp_callback   (RTPSession * sess,
286                                                     RTPSessionSendRTCP callback,
287                                                     gpointer user_data);
288 void            rtp_session_set_sync_rtcp_callback   (RTPSession * sess,
289                                                     RTPSessionSyncRTCP callback,
290                                                     gpointer user_data);
291 void            rtp_session_set_clock_rate_callback   (RTPSession * sess,
292                                                     RTPSessionClockRate callback,
293                                                     gpointer user_data);
294 void            rtp_session_set_reconsider_callback (RTPSession * sess,
295                                                     RTPSessionReconsider callback,
296                                                     gpointer user_data);
297 void            rtp_session_set_request_time_callback (RTPSession * sess,
298                                                     RTPSessionRequestTime callback,
299                                                     gpointer user_data);
300
301 void            rtp_session_set_bandwidth          (RTPSession *sess, gdouble bandwidth);
302 gdouble         rtp_session_get_bandwidth          (RTPSession *sess);
303 void            rtp_session_set_rtcp_fraction      (RTPSession *sess, gdouble fraction);
304 gdouble         rtp_session_get_rtcp_fraction      (RTPSession *sess);
305
306 gboolean        rtp_session_set_sdes_string        (RTPSession *sess, GstRTCPSDESType type,
307                                                     const gchar *cname);
308 gchar*          rtp_session_get_sdes_string        (RTPSession *sess, GstRTCPSDESType type);
309
310 GstStructure *  rtp_session_get_sdes_struct        (RTPSession *sess);
311 void            rtp_session_set_sdes_struct        (RTPSession *sess, const GstStructure *sdes);
312
313 /* handling sources */
314 RTPSource*      rtp_session_get_internal_source    (RTPSession *sess);
315
316 void            rtp_session_set_internal_ssrc      (RTPSession *sess, guint32 ssrc);
317 guint32         rtp_session_get_internal_ssrc      (RTPSession *sess);
318
319 gboolean        rtp_session_add_source             (RTPSession *sess, RTPSource *src);
320 guint           rtp_session_get_num_sources        (RTPSession *sess);
321 guint           rtp_session_get_num_active_sources (RTPSession *sess);
322 RTPSource*      rtp_session_get_source_by_ssrc     (RTPSession *sess, guint32 ssrc);
323 RTPSource*      rtp_session_get_source_by_cname    (RTPSession *sess, const gchar *cname);
324 RTPSource*      rtp_session_create_source          (RTPSession *sess);
325
326 /* processing packets from receivers */
327 GstFlowReturn   rtp_session_process_rtp            (RTPSession *sess, GstBuffer *buffer,
328                                                     GstClockTime current_time,
329                                                     GstClockTime running_time);
330 GstFlowReturn   rtp_session_process_rtcp           (RTPSession *sess, GstBuffer *buffer,
331                                                     GstClockTime current_time,
332                                                     guint64 ntpnstime);
333
334 /* processing packets for sending */
335 GstFlowReturn   rtp_session_send_rtp               (RTPSession *sess, gpointer data, gboolean is_list,
336                                                     GstClockTime current_time, GstClockTime running_time);
337
338 /* stopping the session */
339 GstFlowReturn   rtp_session_schedule_bye           (RTPSession *sess, const gchar *reason,
340                                                     GstClockTime current_time);
341
342 /* get interval for next RTCP interval */
343 GstClockTime    rtp_session_next_timeout           (RTPSession *sess, GstClockTime current_time);
344 GstFlowReturn   rtp_session_on_timeout             (RTPSession *sess, GstClockTime current_time,
345                                                     guint64 ntpnstime, GstClockTime running_time);
346
347 /* request the transmittion of an early RTCP packet */
348 void            rtp_session_request_early_rtcp     (RTPSession * sess, GstClockTime current_time,
349                                                     GstClockTimeDiff max_delay);
350
351 /* Notify session of a request for a new key unit */
352 gboolean        rtp_session_request_key_unit       (RTPSession * sess,
353                                                     guint32 ssrc,
354                                                     GstClockTime now,
355                                                     gboolean fir,
356                                                     gint count);
357
358 #endif /* __RTP_SESSION_H__ */