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 scheduled_bye; 284 285 RTPSessionCallbacks callbacks; 286 gpointer process_rtp_user_data; 287 gpointer send_rtp_user_data; 288 gpointer send_rtcp_user_data; 289 gpointer sync_rtcp_user_data; 290 gpointer clock_rate_user_data; 291 gpointer reconsider_user_data; 292 gpointer request_key_unit_user_data; 293 gpointer request_time_user_data; 294 gpointer notify_nack_user_data; 295 gpointer notify_twcc_user_data; 296 gpointer reconfigure_user_data; 297 gpointer notify_early_rtcp_user_data; 298 299 RTPSessionStats stats; 300 RTPSessionStats bye_stats; 301 302 gboolean favor_new; 303 GstClockTime rtcp_feedback_retention_window; 304 guint rtcp_immediate_feedback_threshold; 305 306 gboolean is_doing_ptp; 307 308 GList *conflicting_addresses; 309 310 gboolean timestamp_sender_reports; 311 312 /* Transport-wide cc-extension */ 313 RTPTWCCManager *twcc; 314 RTPTWCCStats *twcc_stats; 315 guint8 twcc_recv_ext_id; 316 guint8 twcc_send_ext_id; 317 }; 318 319 /** 320 * RTPSessionClass: 321 * @on_new_ssrc: emitted when a new source is found 322 * @on_bye_ssrc: emitted when a source is gone 323 * 324 * The session class. 325 */ 326 struct _RTPSessionClass { 327 GObjectClass parent_class; 328 329 /* action signals */ 330 RTPSource* (*get_source_by_ssrc) (RTPSession *sess, guint32 ssrc); 331 332 /* signals */ 333 void (*on_new_ssrc) (RTPSession *sess, RTPSource *source); 334 void (*on_ssrc_collision) (RTPSession *sess, RTPSource *source); 335 void (*on_ssrc_validated) (RTPSession *sess, RTPSource *source); 336 void (*on_ssrc_active) (RTPSession *sess, RTPSource *source); 337 void (*on_ssrc_sdes) (RTPSession *sess, RTPSource *source); 338 void (*on_bye_ssrc) (RTPSession *sess, RTPSource *source); 339 void (*on_bye_timeout) (RTPSession *sess, RTPSource *source); 340 void (*on_timeout) (RTPSession *sess, RTPSource *source); 341 void (*on_sender_timeout) (RTPSession *sess, RTPSource *source); 342 gboolean (*on_sending_rtcp) (RTPSession *sess, GstBuffer *buffer, 343 gboolean early); 344 void (*on_app_rtcp) (RTPSession *sess, guint subtype, guint ssrc, 345 const gchar *name, GstBuffer *data); 346 void (*on_feedback_rtcp) (RTPSession *sess, guint type, guint fbtype, 347 guint sender_ssrc, guint media_ssrc, GstBuffer *fci); 348 gboolean (*send_rtcp) (RTPSession *sess, GstClockTime max_delay); 349 void (*on_receiving_rtcp) (RTPSession *sess, GstBuffer *buffer); 350 void (*on_new_sender_ssrc) (RTPSession *sess, RTPSource *source); 351 void (*on_sender_ssrc_active) (RTPSession *sess, RTPSource *source); 352 guint (*on_sending_nacks) (RTPSession *sess, guint sender_ssrc, 353 guint media_ssrc, GArray *nacks, GstBuffer *buffer); 354 }; 355 356 GType rtp_session_get_type (void); 357 358 /* create and configure */ 359 RTPSession* rtp_session_new (void); 360 void rtp_session_reset (RTPSession *sess); 361 void rtp_session_set_callbacks (RTPSession *sess, 362 RTPSessionCallbacks *callbacks, 363 gpointer user_data); 364 void rtp_session_set_process_rtp_callback (RTPSession * sess, 365 RTPSessionProcessRTP callback, 366 gpointer user_data); 367 void rtp_session_set_send_rtp_callback (RTPSession * sess, 368 RTPSessionSendRTP callback, 369 gpointer user_data); 370 void rtp_session_set_send_rtcp_callback (RTPSession * sess, 371 RTPSessionSendRTCP callback, 372 gpointer user_data); 373 void rtp_session_set_sync_rtcp_callback (RTPSession * sess, 374 RTPSessionSyncRTCP callback, 375 gpointer user_data); 376 void rtp_session_set_clock_rate_callback (RTPSession * sess, 377 RTPSessionClockRate callback, 378 gpointer user_data); 379 void rtp_session_set_reconsider_callback (RTPSession * sess, 380 RTPSessionReconsider callback, 381 gpointer user_data); 382 void rtp_session_set_request_time_callback (RTPSession * sess, 383 RTPSessionRequestTime callback, 384 gpointer user_data); 385 386 void rtp_session_set_bandwidth (RTPSession *sess, gdouble bandwidth); 387 gdouble rtp_session_get_bandwidth (RTPSession *sess); 388 void rtp_session_set_rtcp_fraction (RTPSession *sess, gdouble fraction); 389 gdouble rtp_session_get_rtcp_fraction (RTPSession *sess); 390 391 GstStructure * rtp_session_get_sdes_struct (RTPSession *sess); 392 void rtp_session_set_sdes_struct (RTPSession *sess, const GstStructure *sdes); 393 394 /* handling sources */ 395 guint32 rtp_session_suggest_ssrc (RTPSession *sess, gboolean *is_random); 396 397 gboolean rtp_session_add_source (RTPSession *sess, RTPSource *src); 398 guint rtp_session_get_num_sources (RTPSession *sess); 399 guint rtp_session_get_num_active_sources (RTPSession *sess); 400 RTPSource* rtp_session_get_source_by_ssrc (RTPSession *sess, guint32 ssrc); 401 402 /* processing packets from receivers */ 403 GstFlowReturn rtp_session_process_rtp (RTPSession *sess, GstBuffer *buffer, 404 GstClockTime current_time, 405 GstClockTime running_time, 406 guint64 ntpnstime); 407 GstFlowReturn rtp_session_process_rtcp (RTPSession *sess, GstBuffer *buffer, 408 GstClockTime current_time, 409 GstClockTime running_time, 410 guint64 ntpnstime); 411 412 /* processing packets for sending */ 413 void rtp_session_update_send_caps (RTPSession *sess, GstCaps *caps); 414 GstFlowReturn rtp_session_send_rtp (RTPSession *sess, gpointer data, gboolean is_list, 415 GstClockTime current_time, GstClockTime running_time); 416 417 /* scheduling bye */ 418 void rtp_session_mark_all_bye (RTPSession *sess, const gchar *reason); 419 GstFlowReturn rtp_session_schedule_bye (RTPSession *sess, GstClockTime current_time); 420 421 /* get interval for next RTCP interval */ 422 GstClockTime rtp_session_next_timeout (RTPSession *sess, GstClockTime current_time); 423 GstFlowReturn rtp_session_on_timeout (RTPSession *sess, GstClockTime current_time, 424 guint64 ntpnstime, GstClockTime running_time); 425 426 /* request the transmission of an early RTCP packet */ 427 gboolean rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time, 428 GstClockTime max_delay); 429 430 /* Notify session of a request for a new key unit */ 431 gboolean rtp_session_request_key_unit (RTPSession * sess, 432 guint32 ssrc, 433 gboolean fir, 434 gint count); 435 gboolean rtp_session_request_nack (RTPSession * sess, 436 guint32 ssrc, 437 guint16 seqnum, 438 GstClockTime max_delay); 439 440 void rtp_session_update_recv_caps_structure (RTPSession * sess, const GstStructure * s); 441 442 443 #endif /* __RTP_SESSION_H__ */ 444