2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
20 #ifndef __RTP_SESSION_H__
21 #define __RTP_SESSION_H__
25 #include "rtpsource.h"
28 typedef struct _RTPSession RTPSession;
29 typedef struct _RTPSessionClass RTPSessionClass;
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))
38 #define RTP_SESSION_LOCK(sess) (g_mutex_lock (&(sess)->lock))
39 #define RTP_SESSION_UNLOCK(sess) (g_mutex_unlock (&(sess)->lock))
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
48 * This callback will be called when @sess has @buffer ready for further
49 * processing. Processing the buffer typically includes decoding and displaying
52 * Returns: a #GstFlowReturn.
54 typedef GstFlowReturn (*RTPSessionProcessRTP) (RTPSession *sess, RTPSource *src, GstBuffer *buffer, gpointer user_data);
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
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.
66 * Returns: a #GstFlowReturn.
68 typedef GstFlowReturn (*RTPSessionSendRTP) (RTPSession *sess, RTPSource *src, gpointer data, gpointer user_data);
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
78 * This callback will be called when @sess has @buffer ready for sending to
79 * all listening participants in this session.
81 * Returns: a #GstFlowReturn.
83 typedef GstFlowReturn (*RTPSessionSendRTCP) (RTPSession *sess, RTPSource *src, GstBuffer *buffer,
84 gboolean eos, gpointer user_data);
88 * @sess: an #RTPSession
89 * @buffer: the RTCP buffer ready for synchronisation
90 * @user_data: user data specified when registering
92 * This callback will be called when @sess has an SR @buffer ready for doing
93 * synchronisation between streams.
95 * Returns: a #GstFlowReturn.
97 typedef GstFlowReturn (*RTPSessionSyncRTCP) (RTPSession *sess, GstBuffer *buffer, gpointer user_data);
100 * RTPSessionClockRate:
101 * @sess: an #RTPSession
102 * @payload: the payload
103 * @user_data: user data specified when registering
105 * This callback will be called when @sess needs the clock-rate of @payload.
107 * Returns: the clock-rate of @pt.
109 typedef gint (*RTPSessionClockRate) (RTPSession *sess, guint8 payload, gpointer user_data);
112 * RTPSessionReconsider:
113 * @sess: an #RTPSession
114 * @user_data: user data specified when registering
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.
120 typedef void (*RTPSessionReconsider) (RTPSession *sess, gpointer user_data);
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
128 * @user_data: user data specified when registering
130 * Asks the encoder to produce a key unit as soon as possibly within the
131 * bandwidth constraints
133 typedef void (*RTPSessionRequestKeyUnit) (RTPSession *sess, guint32 ssrc,
134 gboolean all_headers, gpointer user_data);
137 * RTPSessionRequestTime:
138 * @sess: an #RTPSession
139 * @user_data: user data specified when registering
141 * This callback will be called when @sess needs the current time. The time
142 * should be returned as a #GstClockTime
144 typedef GstClockTime (*RTPSessionRequestTime) (RTPSession *sess,
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
155 * Notifies of NACKed frames.
157 typedef void (*RTPSessionNotifyNACK) (RTPSession *sess,
158 guint16 seqnum, guint16 blp, guint32 ssrc, gpointer user_data);
161 * RTPSessionNotifyTWCC:
162 * @user_data: user data specified when registering
164 * Notifies of Transport-wide congestion control packets and stats.
166 typedef void (*RTPSessionNotifyTWCC) (RTPSession *sess,
167 GstStructure * twcc_packets, GstStructure * twcc_stats, gpointer user_data);
170 * RTPSessionReconfigure:
171 * @sess: an #RTPSession
172 * @user_data: user data specified when registering
174 * This callback will be called when @sess wants to reconfigure the
175 * negotiated parameters.
177 typedef void (*RTPSessionReconfigure) (RTPSession *sess, gpointer user_data);
180 * RTPSessionNotifyEarlyRTCP:
181 * @sess: an #RTPSession
182 * @user_data: user data specified when registering
184 * Notifies of early RTCP being requested
186 typedef void (*RTPSessionNotifyEarlyRTCP) (RTPSession *sess,
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
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.
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;
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
234 * The RTP session manager object
247 guint32 max_dropout_time;
248 guint32 max_misorder_time;
250 GstRTPProfile rtp_profile;
252 gboolean reduced_size_rtcp;
255 gboolean recalc_bandwidth;
257 gdouble rtcp_bandwidth;
258 guint rtcp_rr_bandwidth;
259 guint rtcp_rs_bandwidth;
261 guint32 suggested_ssrc;
262 gboolean internal_ssrc_set;
263 gboolean internal_ssrc_from_caps_or_property;
265 /* for sender/receiver counting */
269 GHashTable *ssrcs[32];
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;
279 gboolean allow_early;
281 GstClockTime next_early_rtcp_time;
283 gboolean sr_req_pending;
284 gboolean scheduled_bye;
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;
300 RTPSessionStats stats;
301 RTPSessionStats bye_stats;
304 GstClockTime rtcp_feedback_retention_window;
305 guint rtcp_immediate_feedback_threshold;
307 gboolean is_doing_ptp;
309 GList *conflicting_addresses;
311 gboolean timestamp_sender_reports;
313 /* RFC6051 64-bit NTP header extension */
314 guint8 send_ntp64_ext_id;
316 /* Transport-wide cc-extension */
317 RTPTWCCManager *twcc;
318 RTPTWCCStats *twcc_stats;
323 * @on_new_ssrc: emitted when a new source is found
324 * @on_bye_ssrc: emitted when a source is gone
328 struct _RTPSessionClass {
329 GObjectClass parent_class;
332 RTPSource* (*get_source_by_ssrc) (RTPSession *sess, guint32 ssrc);
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,
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);
358 GType rtp_session_get_type (void);
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,
366 void rtp_session_set_process_rtp_callback (RTPSession * sess,
367 RTPSessionProcessRTP callback,
369 void rtp_session_set_send_rtp_callback (RTPSession * sess,
370 RTPSessionSendRTP callback,
372 void rtp_session_set_send_rtcp_callback (RTPSession * sess,
373 RTPSessionSendRTCP callback,
375 void rtp_session_set_sync_rtcp_callback (RTPSession * sess,
376 RTPSessionSyncRTCP callback,
378 void rtp_session_set_clock_rate_callback (RTPSession * sess,
379 RTPSessionClockRate callback,
381 void rtp_session_set_reconsider_callback (RTPSession * sess,
382 RTPSessionReconsider callback,
384 void rtp_session_set_request_time_callback (RTPSession * sess,
385 RTPSessionRequestTime callback,
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);
393 GstStructure * rtp_session_get_sdes_struct (RTPSession *sess);
394 void rtp_session_set_sdes_struct (RTPSession *sess, const GstStructure *sdes);
396 /* handling sources */
397 guint32 rtp_session_suggest_ssrc (RTPSession *sess, gboolean *is_random);
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);
404 /* processing packets from receivers */
405 GstFlowReturn rtp_session_process_rtp (RTPSession *sess, GstBuffer *buffer,
406 GstClockTime current_time,
407 GstClockTime running_time,
409 GstFlowReturn rtp_session_process_rtcp (RTPSession *sess, GstBuffer *buffer,
410 GstClockTime current_time,
411 GstClockTime running_time,
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,
421 void rtp_session_mark_all_bye (RTPSession *sess, const gchar *reason);
422 GstFlowReturn rtp_session_schedule_bye (RTPSession *sess, GstClockTime current_time);
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);
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);
433 /* Notify session of a request for a new key unit */
434 gboolean rtp_session_request_key_unit (RTPSession * sess,
438 gboolean rtp_session_request_nack (RTPSession * sess,
441 GstClockTime max_delay);
443 void rtp_session_update_recv_caps_structure (RTPSession * sess, const GstStructure * s);
446 #endif /* __RTP_SESSION_H__ */