rtpsession.c 122 KB
Newer Older
1
/* GStreamer
2
 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3
4
5
6
7
8
9
10
11
12
13
14
15
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
16
17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
18
19
 */

20
21
22
23
/* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
 * with newer GLib versions (>= 2.31.0) */
#define GLIB_DISABLE_DEPRECATION_WARNINGS

24
25
26
27
28
#include <string.h>

#include <gst/rtp/gstrtpbuffer.h>
#include <gst/rtp/gstrtcpbuffer.h>

29
30
#include <gst/glib-compat-private.h>

31
32
33
34
35
36
37
38
#include "rtpsession.h"

GST_DEBUG_CATEGORY_STATIC (rtp_session_debug);
#define GST_CAT_DEFAULT rtp_session_debug

/* signals and args */
enum
{
39
  SIGNAL_GET_SOURCE_BY_SSRC,
40
41
42
  SIGNAL_ON_NEW_SSRC,
  SIGNAL_ON_SSRC_COLLISION,
  SIGNAL_ON_SSRC_VALIDATED,
43
  SIGNAL_ON_SSRC_ACTIVE,
Wim Taymans's avatar
Wim Taymans committed
44
  SIGNAL_ON_SSRC_SDES,
45
  SIGNAL_ON_BYE_SSRC,
46
47
  SIGNAL_ON_BYE_TIMEOUT,
  SIGNAL_ON_TIMEOUT,
48
  SIGNAL_ON_SENDER_TIMEOUT,
49
  SIGNAL_ON_SENDING_RTCP,
50
  SIGNAL_ON_FEEDBACK_RTCP,
51
  SIGNAL_SEND_RTCP,
52
  SIGNAL_SEND_RTCP_FULL,
53
  SIGNAL_ON_RECEIVING_RTCP,
54
55
56
  LAST_SIGNAL
};

Wim Taymans's avatar
Wim Taymans committed
57
#define DEFAULT_INTERNAL_SOURCE      NULL
58
59
#define DEFAULT_BANDWIDTH            0.0
#define DEFAULT_RTCP_FRACTION        RTP_STATS_RTCP_FRACTION
60
61
#define DEFAULT_RTCP_RR_BANDWIDTH    -1
#define DEFAULT_RTCP_RS_BANDWIDTH    -1
62
#define DEFAULT_RTCP_MTU             1400
63
#define DEFAULT_SDES                 NULL
Wim Taymans's avatar
Wim Taymans committed
64
65
#define DEFAULT_NUM_SOURCES          0
#define DEFAULT_NUM_ACTIVE_SOURCES   0
66
#define DEFAULT_SOURCES              NULL
67
#define DEFAULT_RTCP_MIN_INTERVAL    (RTP_STATS_MIN_INTERVAL * GST_SECOND)
68
#define DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW (2 * GST_SECOND)
69
#define DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD (3)
70
#define DEFAULT_PROBATION            RTP_DEFAULT_PROBATION
71
#define DEFAULT_RTP_PROFILE          GST_RTP_PROFILE_AVP
72
73
74

enum
{
Wim Taymans's avatar
Wim Taymans committed
75
  PROP_0,
76
  PROP_INTERNAL_SSRC,
Wim Taymans's avatar
Wim Taymans committed
77
78
79
  PROP_INTERNAL_SOURCE,
  PROP_BANDWIDTH,
  PROP_RTCP_FRACTION,
80
81
  PROP_RTCP_RR_BANDWIDTH,
  PROP_RTCP_RS_BANDWIDTH,
82
  PROP_RTCP_MTU,
83
  PROP_SDES,
Wim Taymans's avatar
Wim Taymans committed
84
85
  PROP_NUM_SOURCES,
  PROP_NUM_ACTIVE_SOURCES,
86
  PROP_SOURCES,
87
  PROP_FAVOR_NEW,
88
  PROP_RTCP_MIN_INTERVAL,
89
  PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
90
  PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
91
  PROP_PROBATION,
92
93
  PROP_STATS,
  PROP_RTP_PROFILE
94
95
};

96
97
98
/* update average packet size */
#define INIT_AVG(avg, val) \
   (avg) = (val);
Wim Taymans's avatar
Wim Taymans committed
99
100
#define UPDATE_AVG(avg, val)            \
  if ((avg) == 0)                       \
101
   (avg) = (val);                       \
Wim Taymans's avatar
Wim Taymans committed
102
  else                                  \
103
104
   (avg) = ((val) + (15 * (avg))) >> 4;

105

106
107
108
109
110
111
112
/* GObject vmethods */
static void rtp_session_finalize (GObject * object);
static void rtp_session_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void rtp_session_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

Sebastian Dröge's avatar
Sebastian Dröge committed
113
114
static gboolean rtp_session_send_rtcp (RTPSession * sess,
    GstClockTime max_delay);
115

116
117
118
119
static guint rtp_session_signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);

120
static guint32 rtp_session_create_new_ssrc (RTPSession * sess);
121
static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
122
    gboolean * created, RTPPacketInfo * pinfo, gboolean rtp);
123
static RTPSource *obtain_internal_source (RTPSession * sess,
124
    guint32 ssrc, gboolean * created, GstClockTime current_time);
125
static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
126
    GstClockTime current_time);
127
128
static GstClockTime calculate_rtcp_interval (RTPSession * sess,
    gboolean deterministic, gboolean first);
129

130
131
132
133
134
135
136
137
138
139
static gboolean
accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
    const GValue * handler_return, gpointer data)
{
  if (g_value_get_boolean (handler_return))
    g_value_set_boolean (return_accu, TRUE);

  return TRUE;
}

140
141
142
143
144
145
146
147
148
149
150
static void
rtp_session_class_init (RTPSessionClass * klass)
{
  GObjectClass *gobject_class;

  gobject_class = (GObjectClass *) klass;

  gobject_class->finalize = rtp_session_finalize;
  gobject_class->set_property = rtp_session_set_property;
  gobject_class->get_property = rtp_session_get_property;

151
152
153
154
155
156
157
158
159
160
  /**
   * RTPSession::get-source-by-ssrc:
   * @session: the object which received the signal
   * @ssrc: the SSRC of the RTPSource
   *
   * Request the #RTPSource object with SSRC @ssrc in @session.
   */
  rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
      g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
161
          get_source_by_ssrc), NULL, NULL, g_cclosure_marshal_generic,
162
163
      RTP_TYPE_SOURCE, 1, G_TYPE_UINT);

164
165
166
167
168
169
170
171
172
173
174
  /**
   * RTPSession::on-new-ssrc:
   * @session: the object which received the signal
   * @src: the new RTPSource
   *
   * Notify of a new SSRC that entered @session.
   */
  rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
      g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
      NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
175
      RTP_TYPE_SOURCE);
176
  /**
177
   * RTPSession::on-ssrc-collision:
178
179
180
181
182
183
184
185
186
   * @session: the object which received the signal
   * @src: the #RTPSource that caused a collision
   *
   * Notify when we have an SSRC collision
   */
  rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
      g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
      NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
187
      RTP_TYPE_SOURCE);
188
  /**
189
   * RTPSession::on-ssrc-validated:
190
191
192
193
194
195
196
197
198
   * @session: the object which received the signal
   * @src: the new validated RTPSource
   *
   * Notify of a new SSRC that became validated.
   */
  rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
      g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
      NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
199
      RTP_TYPE_SOURCE);
200
  /**
Wim Taymans's avatar
Wim Taymans committed
201
   * RTPSession::on-ssrc-active:
202
203
204
205
206
207
208
209
210
   * @session: the object which received the signal
   * @src: the active RTPSource
   *
   * Notify of a SSRC that is active, i.e., sending RTCP.
   */
  rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
      g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
      NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
211
      RTP_TYPE_SOURCE);
Wim Taymans's avatar
Wim Taymans committed
212
213
214
215
216
217
218
219
220
221
222
  /**
   * RTPSession::on-ssrc-sdes:
   * @session: the object which received the signal
   * @src: the RTPSource
   *
   * Notify that a new SDES was received for SSRC.
   */
  rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
      g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
      NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
223
      RTP_TYPE_SOURCE);
224
225
226
227
228
229
230
231
232
233
234
  /**
   * RTPSession::on-bye-ssrc:
   * @session: the object which received the signal
   * @src: the RTPSource that went away
   *
   * Notify of an SSRC that became inactive because of a BYE packet.
   */
  rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
      g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
      NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
235
      RTP_TYPE_SOURCE);
236
237
238
239
240
241
242
243
244
245
246
  /**
   * RTPSession::on-bye-timeout:
   * @session: the object which received the signal
   * @src: the RTPSource that timed out
   *
   * Notify of an SSRC that has timed out because of BYE
   */
  rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
      g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
      NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
247
      RTP_TYPE_SOURCE);
248
249
250
251
252
253
254
255
256
257
258
  /**
   * RTPSession::on-timeout:
   * @session: the object which received the signal
   * @src: the RTPSource that timed out
   *
   * Notify of an SSRC that has timed out
   */
  rtp_session_signals[SIGNAL_ON_TIMEOUT] =
      g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
      NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
259
      RTP_TYPE_SOURCE);
260
261
262
263
264
265
266
267
268
269
270
271
  /**
   * RTPSession::on-sender-timeout:
   * @session: the object which received the signal
   * @src: the RTPSource that timed out
   *
   * Notify of an SSRC that was a sender but timed out and became a receiver.
   */
  rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
      g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
      NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
      RTP_TYPE_SOURCE);
272

273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  /**
   * RTPSession::on-sending-rtcp
   * @session: the object which received the signal
   * @buffer: the #GstBuffer containing the RTCP packet about to be sent
   * @early: %TRUE if the packet is early, %FALSE if it is regular
   *
   * This signal is emitted before sending an RTCP packet, it can be used
   * to add extra RTCP Packets.
   *
   * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
   * if suppressing it is acceptable
   */
  rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
      g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
288
289
      accumulate_trues, NULL, g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2,
      GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_BOOLEAN);
290

291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  /**
   * RTPSession::on-feedback-rtcp:
   * @session: the object which received the signal
   * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
   *  %GST_RTCP_TYPE_RTPFB
   * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
   * @sender_ssrc: The SSRC of the sender
   * @media_ssrc: The SSRC of the media this refers to
   * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
   * there was no FCI
   *
   * Notify that a RTCP feedback packet has been received
   */
  rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
      g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
307
308
      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 5, G_TYPE_UINT,
      G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, GST_TYPE_BUFFER);
309

310
311
312
313
314
315
316
317
  /**
   * RTPSession::send-rtcp:
   * @session: the object which received the signal
   * @max_delay: The maximum delay after which the feedback will not be useful
   *  anymore
   *
   * Requests that the #RTPSession initiate a new RTCP packet as soon as
   * possible within the requested delay.
318
319
   *
   * This sets feedback to %TRUE if not already done before.
320
321
322
323
324
   */
  rtp_session_signals[SIGNAL_SEND_RTCP] =
      g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
      G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
325
      g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_UINT64);
326

327
328
329
330
331
332
333
334
335
  /**
   * RTPSession::send-rtcp-full:
   * @session: the object which received the signal
   * @max_delay: The maximum delay after which the feedback will not be useful
   *  anymore
   *
   * Requests that the #RTPSession initiate a new RTCP packet as soon as
   * possible within the requested delay.
   *
336
337
   * This sets feedback to %TRUE if not already done before.
   *
338
339
340
341
342
343
344
345
346
347
348
   * Returns: TRUE if the new RTCP packet could be scheduled within the
   * requested delay, FALSE otherwise.
   *
   * Since: 1.6
   */
  rtp_session_signals[SIGNAL_SEND_RTCP_FULL] =
      g_signal_new ("send-rtcp-full", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
      G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
      g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 1, G_TYPE_UINT64);

349
350
351
352
353
354
355
356
357
358
359
  /**
   * RTPSession::on-receiving-rtcp
   * @session: the object which received the signal
   * @buffer: the #GstBuffer containing the RTCP packet that was received
   *
   * This signal is emitted when receiving an RTCP packet before it is handled
   * by the session. It can be used to extract custom information from RTCP packets.
   *
   * Since: 1.6
   */
  rtp_session_signals[SIGNAL_ON_RECEIVING_RTCP] =
Sebastian Dröge's avatar
Sebastian Dröge committed
360
      g_signal_new ("on-receiving-rtcp", G_TYPE_FROM_CLASS (klass),
361
362
363
364
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_receiving_rtcp),
      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
      GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);

365
366
  g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
      g_param_spec_uint ("internal-ssrc", "Internal SSRC",
367
          "The internal SSRC used for the session (deprecated)",
368
369
          0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

Wim Taymans's avatar
Wim Taymans committed
370
371
  g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
      g_param_spec_object ("internal-source", "Internal Source",
372
          "The internal source element of the session (deprecated)",
373
          RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
374
375
376

  g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
      g_param_spec_double ("bandwidth", "Bandwidth",
377
          "The bandwidth of the session (0 for auto-discover)",
378
379
          0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
380
381
382

  g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
      g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
383
          "The fraction of the bandwidth used for RTCP (or as a real fraction of the RTP bandwidth if < 1)",
384
385
          0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
386

387
388
389
  g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
      g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
          "The RTCP bandwidth used for receivers in bytes per second (-1 = default)",
390
391
          -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392
393
394
395

  g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
      g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
          "The RTCP bandwidth used for senders in bytes per second (-1 = default)",
396
397
          -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398

399
400
401
402
403
404
  g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
      g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
          "The maximum size of the RTCP packets",
          16, G_MAXINT16, DEFAULT_RTCP_MTU,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

405
406
407
408
  g_object_class_install_property (gobject_class, PROP_SDES,
      g_param_spec_boxed ("sdes", "SDES",
          "The SDES items of this session",
          GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
409
410
411
412

  g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
      g_param_spec_uint ("num-sources", "Num Sources",
          "The number of sources in the session", 0, G_MAXUINT,
413
          DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
414
415
416
417

  g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
      g_param_spec_uint ("num-active-sources", "Num Active Sources",
          "The number of active sources in the session", 0, G_MAXUINT,
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
          DEFAULT_NUM_ACTIVE_SOURCES,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
  /**
   * RTPSource::sources
   *
   * Get a GValue Array of all sources in the session.
   *
   * <example>
   * <title>Getting the #RTPSources of a session
   * <programlisting>
   * {
   *   GValueArray *arr;
   *   GValue *val;
   *   guint i;
   *
   *   g_object_get (sess, "sources", &arr, NULL);
   *
   *   for (i = 0; i < arr->n_values; i++) {
   *     RTPSource *source;
   *
   *     val = g_value_array_get_nth (arr, i);
   *     source = g_value_get_object (val);
   *   }
   *   g_value_array_free (arr);
   * }
   * </programlisting>
   * </example>
   */
  g_object_class_install_property (gobject_class, PROP_SOURCES,
      g_param_spec_boxed ("sources", "Sources",
          "An array of all known sources in the session",
          G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
450

451
452
453
454
455
  g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
      g_param_spec_boolean ("favor-new", "Favor new sources",
          "Resolve SSRC conflict in favor of new sources", FALSE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

456
457
458
459
460
  g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
      g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
          "Minimum interval between Regular RTCP packet (in ns)",
          0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461

462
463
464
465
466
467
468
469
  g_object_class_install_property (gobject_class,
      PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
      g_param_spec_uint64 ("rtcp-feedback-retention-window",
          "RTCP Feedback retention window",
          "Duration during which RTCP Feedback packets are retained (in ns)",
          0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

470
471
472
473
474
  g_object_class_install_property (gobject_class,
      PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
      g_param_spec_uint ("rtcp-immediate-feedback-threshold",
          "RTCP Immediate Feedback threshold",
          "The maximum number of members of a RTP session for which immediate"
475
          " feedback is used (DEPRECATED: has no effect and is not needed)",
476
          0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
477
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
478

479
480
481
482
483
484
  g_object_class_install_property (gobject_class, PROP_PROBATION,
      g_param_spec_uint ("probation", "Number of probations",
          "Consecutive packet sequence numbers to accept the source",
          0, G_MAXUINT, DEFAULT_PROBATION,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

485
486
487
488
489
490
491
492
493
494
495
  /**
   * RTPSession::stats:
   *
   * Various session statistics. This property returns a GstStructure
   * with name application/x-rtp-session-stats with the following fields:
   *
   *  "rtx-drop-count"  G_TYPE_UINT   The number of retransmission events
   *      dropped (due to bandwidth constraints)
   *  "sent-nack-count" G_TYPE_UINT   Number of NACKs sent
   *  "recv-nack-count" G_TYPE_UINT   Number of NACKs received
   *
496
   * Since: 1.4
497
498
499
500
501
502
   */
  g_object_class_install_property (gobject_class, PROP_STATS,
      g_param_spec_boxed ("stats", "Statistics",
          "Various statistics", GST_TYPE_STRUCTURE,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

503
504
505
506
507
  g_object_class_install_property (gobject_class, PROP_RTP_PROFILE,
      g_param_spec_enum ("rtp-profile", "RTP Profile",
          "RTP profile to use for this session", GST_TYPE_RTP_PROFILE,
          DEFAULT_RTP_PROFILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

508
509
  klass->get_source_by_ssrc =
      GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
510
  klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
511

512
513
514
515
516
517
  GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
}

static void
rtp_session_init (RTPSession * sess)
{
518
  gint i;
Wim Taymans's avatar
Wim Taymans committed
519
  gchar *str;
520

Wim Taymans's avatar
Wim Taymans committed
521
  g_mutex_init (&sess->lock);
522
  sess->key = g_random_int ();
523
524
  sess->mask_idx = 0;
  sess->mask = 0;
525

526
527
528
529
530
  /* TODO: We currently only use the first hash table but this is the
   * beginning of an implementation for RFC2762
   for (i = 0; i < 32; i++) {
   */
  for (i = 0; i < 1; i++) {
531
532
533
534
    sess->ssrcs[i] =
        g_hash_table_new_full (NULL, NULL, NULL,
        (GDestroyNotify) g_object_unref);
  }
535
536

  rtp_stats_init_defaults (&sess->stats);
537
538
539
  INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
  rtp_stats_set_min_interval (&sess->stats,
      (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
540

541
542
543
544
545
546
  sess->recalc_bandwidth = TRUE;
  sess->bandwidth = DEFAULT_BANDWIDTH;
  sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
  sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
  sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;

547
548
  /* default UDP header length */
  sess->header_len = 28;
549
  sess->mtu = DEFAULT_RTCP_MTU;
550

551
552
  sess->probation = DEFAULT_PROBATION;

553
  /* some default SDES entries */
554
  sess->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
555
556

  /* we do not want to leak details like the username or hostname here */
557
  str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
558
  gst_structure_set (sess->sdes, "cname", G_TYPE_STRING, str, NULL);
Wim Taymans's avatar
Wim Taymans committed
559
560
  g_free (str);

561
562
563
#if 0
  /* we do not want to leak the user's real name here */
  str = g_strdup_printf ("Anon%u", g_random_int ());
564
  gst_structure_set (sdes, "name", G_TYPE_STRING, str, NULL);
565
566
567
  g_free (str);
#endif

568
  gst_structure_set (sess->sdes, "tool", G_TYPE_STRING, "GStreamer", NULL);
569

570
571
  /* this is the SSRC we suggest */
  sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
572
  sess->internal_ssrc_set = FALSE;
573

574
  sess->first_rtcp = TRUE;
575
  sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
576
  sess->last_rtcp_check_time = GST_CLOCK_TIME_NONE;
577
  sess->last_rtcp_send_time = GST_CLOCK_TIME_NONE;
578
  sess->last_rtcp_interval = GST_CLOCK_TIME_NONE;
579
580

  sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
581
  sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
582
583
  sess->rtcp_immediate_feedback_threshold =
      DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
584
  sess->rtp_profile = DEFAULT_RTP_PROFILE;
585

586
  sess->last_keyframe_request = GST_CLOCK_TIME_NONE;
587
588

  sess->is_doing_ptp = TRUE;
589
590
591
592
593
594
}

static void
rtp_session_finalize (GObject * object)
{
  RTPSession *sess;
595
  gint i;
596
597
598

  sess = RTP_SESSION_CAST (object);

599
  gst_structure_free (sess->sdes);
600

601
602
603
  g_list_free_full (sess->conflicting_addresses,
      (GDestroyNotify) rtp_conflicting_address_free);

604
605
606
607
  /* TODO: Change this again when implementing RFC 2762
   * for (i = 0; i < 32; i++)
   */
  for (i = 0; i < 1; i++)
608
    g_hash_table_destroy (sess->ssrcs[i]);
609

610
  g_mutex_clear (&sess->lock);
611
612
613
614

  G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
}

615
616
617
618
619
620
621
static void
copy_source (gpointer key, RTPSource * source, GValueArray * arr)
{
  GValue value = { 0 };

  g_value_init (&value, RTP_TYPE_SOURCE);
  g_value_take_object (&value, source);
Wim Taymans's avatar
Wim Taymans committed
622
  /* copies the value */
623
624
625
626
627
628
629
630
631
632
633
  g_value_array_append (arr, &value);
}

static GValueArray *
rtp_session_create_sources (RTPSession * sess)
{
  GValueArray *res;
  guint size;

  RTP_SESSION_LOCK (sess);
  /* get number of elements in the table */
634
  size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
635
636
637
638
  /* create the result value array */
  res = g_value_array_new (size);

  /* and copy all values into the array */
639
  g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
640
641
642
643
644
  RTP_SESSION_UNLOCK (sess);

  return res;
}

645
646
647
648
649
650
651
652
653
654
655
656
657
static GstStructure *
rtp_session_create_stats (RTPSession * sess)
{
  GstStructure *s;

  s = gst_structure_new ("application/x-rtp-session-stats",
      "rtx-drop-count", G_TYPE_UINT, sess->stats.nacks_dropped,
      "sent-nack-count", G_TYPE_UINT, sess->stats.nacks_sent,
      "recv-nack-count", G_TYPE_UINT, sess->stats.nacks_received, NULL);

  return s;
}

658
659
660
661
662
663
664
665
666
static void
rtp_session_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  RTPSession *sess;

  sess = RTP_SESSION (object);

  switch (prop_id) {
667
    case PROP_INTERNAL_SSRC:
668
669
      RTP_SESSION_LOCK (sess);
      sess->suggested_ssrc = g_value_get_uint (value);
670
      sess->internal_ssrc_set = TRUE;
671
      RTP_SESSION_UNLOCK (sess);
672
673
      if (sess->callbacks.reconfigure)
        sess->callbacks.reconfigure (sess, sess->reconfigure_user_data);
674
      break;
Wim Taymans's avatar
Wim Taymans committed
675
    case PROP_BANDWIDTH:
676
      RTP_SESSION_LOCK (sess);
677
678
      sess->bandwidth = g_value_get_double (value);
      sess->recalc_bandwidth = TRUE;
679
      RTP_SESSION_UNLOCK (sess);
Wim Taymans's avatar
Wim Taymans committed
680
681
      break;
    case PROP_RTCP_FRACTION:
682
      RTP_SESSION_LOCK (sess);
683
684
      sess->rtcp_bandwidth = g_value_get_double (value);
      sess->recalc_bandwidth = TRUE;
685
      RTP_SESSION_UNLOCK (sess);
686
687
      break;
    case PROP_RTCP_RR_BANDWIDTH:
688
      RTP_SESSION_LOCK (sess);
689
690
      sess->rtcp_rr_bandwidth = g_value_get_int (value);
      sess->recalc_bandwidth = TRUE;
691
      RTP_SESSION_UNLOCK (sess);
692
693
      break;
    case PROP_RTCP_RS_BANDWIDTH:
694
      RTP_SESSION_LOCK (sess);
695
696
      sess->rtcp_rs_bandwidth = g_value_get_int (value);
      sess->recalc_bandwidth = TRUE;
697
      RTP_SESSION_UNLOCK (sess);
Wim Taymans's avatar
Wim Taymans committed
698
      break;
699
700
701
    case PROP_RTCP_MTU:
      sess->mtu = g_value_get_uint (value);
      break;
702
703
    case PROP_SDES:
      rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
Wim Taymans's avatar
Wim Taymans committed
704
      break;
705
706
707
    case PROP_FAVOR_NEW:
      sess->favor_new = g_value_get_boolean (value);
      break;
708
709
710
    case PROP_RTCP_MIN_INTERVAL:
      rtp_stats_set_min_interval (&sess->stats,
          (gdouble) g_value_get_uint64 (value) / GST_SECOND);
711
712
713
714
715
716
      /* trigger reconsideration */
      RTP_SESSION_LOCK (sess);
      sess->next_rtcp_check_time = 0;
      RTP_SESSION_UNLOCK (sess);
      if (sess->callbacks.reconsider)
        sess->callbacks.reconsider (sess, sess->reconsider_user_data);
717
      break;
718
719
720
    case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
      sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
      break;
721
722
723
    case PROP_PROBATION:
      sess->probation = g_value_get_uint (value);
      break;
724
725
726
727
728
729
730
731
732
    case PROP_RTP_PROFILE:
      sess->rtp_profile = g_value_get_enum (value);
      /* trigger reconsideration */
      RTP_SESSION_LOCK (sess);
      sess->next_rtcp_check_time = 0;
      RTP_SESSION_UNLOCK (sess);
      if (sess->callbacks.reconsider)
        sess->callbacks.reconsider (sess, sess->reconsider_user_data);
      break;
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
rtp_session_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  RTPSession *sess;

  sess = RTP_SESSION (object);

  switch (prop_id) {
748
    case PROP_INTERNAL_SSRC:
749
      g_value_set_uint (value, rtp_session_suggest_ssrc (sess, NULL));
750
      break;
Wim Taymans's avatar
Wim Taymans committed
751
    case PROP_INTERNAL_SOURCE:
752
753
      /* FIXME, return a random source */
      g_value_set_object (value, NULL);
Wim Taymans's avatar
Wim Taymans committed
754
755
      break;
    case PROP_BANDWIDTH:
756
      g_value_set_double (value, sess->bandwidth);
Wim Taymans's avatar
Wim Taymans committed
757
758
      break;
    case PROP_RTCP_FRACTION:
759
760
761
762
763
764
765
      g_value_set_double (value, sess->rtcp_bandwidth);
      break;
    case PROP_RTCP_RR_BANDWIDTH:
      g_value_set_int (value, sess->rtcp_rr_bandwidth);
      break;
    case PROP_RTCP_RS_BANDWIDTH:
      g_value_set_int (value, sess->rtcp_rs_bandwidth);
Wim Taymans's avatar
Wim Taymans committed
766
      break;
767
768
769
    case PROP_RTCP_MTU:
      g_value_set_uint (value, sess->mtu);
      break;
770
771
    case PROP_SDES:
      g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
Wim Taymans's avatar
Wim Taymans committed
772
773
774
775
776
777
778
      break;
    case PROP_NUM_SOURCES:
      g_value_set_uint (value, rtp_session_get_num_sources (sess));
      break;
    case PROP_NUM_ACTIVE_SOURCES:
      g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
      break;
779
780
781
    case PROP_SOURCES:
      g_value_take_boxed (value, rtp_session_create_sources (sess));
      break;
782
783
784
    case PROP_FAVOR_NEW:
      g_value_set_boolean (value, sess->favor_new);
      break;
785
786
787
    case PROP_RTCP_MIN_INTERVAL:
      g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
      break;
788
789
790
    case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
      g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
      break;
791
792
793
    case PROP_PROBATION:
      g_value_set_uint (value, sess->probation);
      break;
794
795
796
    case PROP_STATS:
      g_value_take_boxed (value, rtp_session_create_stats (sess));
      break;
797
798
799
    case PROP_RTP_PROFILE:
      g_value_set_enum (value, sess->rtp_profile);
      break;
800
801
802
803
804
805
806
807
808
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
on_new_ssrc (RTPSession * sess, RTPSource * source)
{
809
  g_object_ref (source);
810
  RTP_SESSION_UNLOCK (sess);
811
  g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
812
  RTP_SESSION_LOCK (sess);
813
  g_object_unref (source);
814
815
816
817
818
}

static void
on_ssrc_collision (RTPSession * sess, RTPSource * source)
{
819
  g_object_ref (source);
820
  RTP_SESSION_UNLOCK (sess);
821
822
  g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
      source);
823
  RTP_SESSION_LOCK (sess);
824
  g_object_unref (source);
825
826
827
828
829
}

static void
on_ssrc_validated (RTPSession * sess, RTPSource * source)
{
830
  g_object_ref (source);
831
  RTP_SESSION_UNLOCK (sess);
832
833
  g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
      source);
834
  RTP_SESSION_LOCK (sess);
835
  g_object_unref (source);
836
837
}

838
839
840
static void
on_ssrc_active (RTPSession * sess, RTPSource * source)
{
841
  g_object_ref (source);
842
843
844
  RTP_SESSION_UNLOCK (sess);
  g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
  RTP_SESSION_LOCK (sess);
845
  g_object_unref (source);
846
847
}

Wim Taymans's avatar
Wim Taymans committed
848
849
850
static void
on_ssrc_sdes (RTPSession * sess, RTPSource * source)
{
851
  g_object_ref (source);
852
  GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
Wim Taymans's avatar
Wim Taymans committed
853
854
855
  RTP_SESSION_UNLOCK (sess);
  g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
  RTP_SESSION_LOCK (sess);
856
  g_object_unref (source);
Wim Taymans's avatar
Wim Taymans committed
857
858
}

859
860
861
static void
on_bye_ssrc (RTPSession * sess, RTPSource * source)
{
862
  g_object_ref (source);
863
  RTP_SESSION_UNLOCK (sess);
864
  g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
865
  RTP_SESSION_LOCK (sess);
866
  g_object_unref (source);
867
868
}

869
870
871
static void
on_bye_timeout (RTPSession * sess, RTPSource * source)
{
872
  g_object_ref (source);
873
  RTP_SESSION_UNLOCK (sess);
874
  g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
875
  RTP_SESSION_LOCK (sess);
876
  g_object_unref (source);
877
878
879
880
881
}

static void
on_timeout (RTPSession * sess, RTPSource * source)
{
882
  g_object_ref (source);
883
  RTP_SESSION_UNLOCK (sess);
884
  g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
885
  RTP_SESSION_LOCK (sess);
886
  g_object_unref (source);
887
888
}

889
890
891
static void
on_sender_timeout (RTPSession * sess, RTPSource * source)
{
892
  g_object_ref (source);
893
894
895
896
  RTP_SESSION_UNLOCK (sess);
  g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
      source);
  RTP_SESSION_LOCK (sess);
897
  g_object_unref (source);
898
899
}

900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/**
 * rtp_session_new:
 *
 * Create a new session object.
 *
 * Returns: a new #RTPSession. g_object_unref() after usage.
 */
RTPSession *
rtp_session_new (void)
{
  RTPSession *sess;

  sess = g_object_new (RTP_TYPE_SESSION, NULL);

  return sess;
}

/**
 * rtp_session_set_callbacks:
 * @sess: an #RTPSession
 * @callbacks: callbacks to configure
 * @user_data: user data passed in the callbacks
 *
 * Configure a set of callbacks to be notified of actions.
 */
void
rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
    gpointer user_data)
{
  g_return_if_fail (RTP_IS_SESSION (sess));

931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
  if (callbacks->process_rtp) {
    sess->callbacks.process_rtp = callbacks->process_rtp;
    sess->process_rtp_user_data = user_data;
  }
  if (callbacks->send_rtp) {
    sess->callbacks.send_rtp = callbacks->send_rtp;
    sess->send_rtp_user_data = user_data;
  }
  if (callbacks->send_rtcp) {
    sess->callbacks.send_rtcp = callbacks->send_rtcp;
    sess->send_rtcp_user_data = user_data;
  }
  if (callbacks->sync_rtcp) {
    sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
    sess->sync_rtcp_user_data = user_data;
  }
  if (callbacks->clock_rate) {
    sess->callbacks.clock_rate = callbacks->clock_rate;
    sess->clock_rate_user_data = user_data;
  }
  if (callbacks->reconsider) {
    sess->callbacks.reconsider = callbacks->reconsider;
    sess->reconsider_user_data = user_data;
  }
955
956
957
958
  if (callbacks->request_key_unit) {
    sess->callbacks.request_key_unit = callbacks->request_key_unit;
    sess->request_key_unit_user_data = user_data;
  }
959
960
961
962
  if (callbacks->request_time) {
    sess->callbacks.request_time = callbacks->request_time;
    sess->request_time_user_data = user_data;
  }
963
964
965
966
  if (callbacks->notify_nack) {
    sess->callbacks.notify_nack = callbacks->notify_nack;
    sess->notify_nack_user_data = user_data;
  }
967
968
969
970
  if (callbacks->reconfigure) {
    sess->callbacks.reconfigure = callbacks->reconfigure;
    sess->reconfigure_user_data = user_data;
  }
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
}

/**
 * rtp_session_set_process_rtp_callback:
 * @sess: an #RTPSession
 * @callback: callback to set
 * @user_data: user data passed in the callback
 *
 * Configure only the process_rtp callback to be notified of the process_rtp action.
 */
void
rtp_session_set_process_rtp_callback (RTPSession * sess,
    RTPSessionProcessRTP callback, gpointer user_data)
{
  g_return_if_fail (RTP_IS_SESSION (sess));

  sess->callbacks.process_rtp = callback;
  sess->process_rtp_user_data = user_data;
}

/**
 * rtp_session_set_send_rtp_callback:
 * @sess: an #RTPSession
 * @callback: callback to set
 * @user_data: user data passed in the callback
 *
 * Configure only the send_rtp callback to be notified of the send_rtp action.
 */
void
rtp_session_set_send_rtp_callback (RTPSession * sess,
    RTPSessionSendRTP callback, gpointer user_data)
{
  g_return_if_fail (RTP_IS_SESSION (sess));

  sess->callbacks.send_rtp = callback;
  sess->send_rtp_user_data = user_data;
}

/**
 * rtp_session_set_send_rtcp_callback:
 * @sess: an #RTPSession
 * @callback: callback to set
 * @user_data: user data passed in the callback
 *
 * Configure only the send_rtcp callback to be notified of the send_rtcp action.
 */
void
rtp_session_set_send_rtcp_callback (RTPSession * sess,
    RTPSessionSendRTCP callback, gpointer user_data)
{
  g_return_if_fail (RTP_IS_SESSION (sess));

  sess->callbacks.send_rtcp = callback;
  sess->send_rtcp_user_data = user_data;
}

/**
 * rtp_session_set_sync_rtcp_callback:
 * @sess: an #RTPSession
 * @callback: callback to set
 * @user_data: user data passed in the callback
 *
 * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
 */
void
rtp_session_set_sync_rtcp_callback (RTPSession * sess,
    RTPSessionSyncRTCP callback, gpointer user_data)
{
  g_return_if_fail (RTP_IS_SESSION (sess));

  sess->callbacks.sync_rtcp = callback;
  sess->sync_rtcp_user_data = user_data;
}

/**
 * rtp_session_set_clock_rate_callback:
 * @sess: an #RTPSession
 * @callback: callback to set
 * @user_data: user data passed in the callback
 *
 * Configure only the clock_rate callback to be notified of the clock_rate action.
 */
void
rtp_session_set_clock_rate_callback (RTPSession * sess,
    RTPSessionClockRate callback, gpointer user_data)
{
  g_return_if_fail (RTP_IS_SESSION (sess));

  sess->callbacks.clock_rate = callback;
  sess->clock_rate_user_data = user_data;
}

/**
 * rtp_session_set_reconsider_callback:
 * @sess: an #RTPSession
 * @callback: callback to set
 * @user_data: user data passed in the callback
 *
 * Configure only the reconsider callback to be notified of the reconsider action.
 */
void
rtp_session_set_reconsider_callback (RTPSession * sess,
    RTPSessionReconsider callback, gpointer user_data)
{
  g_return_if_fail (RTP_IS_SESSION (sess));

  sess->callbacks.reconsider = callback;
  sess->reconsider_user_data = user_data;
1079
1080
}

1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
/**
 * rtp_session_set_request_time_callback:
 * @sess: an #RTPSession
 * @callback: callback to set
 * @user_data: user data passed in the callback
 *
 * Configure only the request_time callback
 */
void
rtp_session_set_request_time_callback (RTPSession * sess,
    RTPSessionRequestTime callback, gpointer user_data)
{
  g_return_if_fail (RTP_IS_SESSION (sess));

  sess->callbacks.request_time = callback;
  sess->request_time_user_data = user_data;
}

1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
/**
 * rtp_session_set_bandwidth:
 * @sess: an #RTPSession
 * @bandwidth: the bandwidth allocated
 *
 * Set the session bandwidth in bytes per second.
 */
void
rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
{
  g_return_if_fail (RTP_IS_SESSION (sess));

Wim Taymans's avatar
Wim Taymans committed
1111
  RTP_SESSION_LOCK (sess);
1112
  sess->stats.bandwidth = bandwidth;
Wim Taymans's avatar
Wim Taymans committed
1113
  RTP_SESSION_UNLOCK (sess);
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
}

/**
 * rtp_session_get_bandwidth:
 * @sess: an #RTPSession
 *
 * Get the session bandwidth.
 *
 * Returns: the session bandwidth.
 */
gdouble
rtp_session_get_bandwidth (RTPSession * sess)
{
Wim Taymans's avatar
Wim Taymans committed
1127
1128
  gdouble result;

1129
1130
  g_return_val_if_fail (RTP_IS_SESSION (sess), 0);

Wim Taymans's avatar
Wim Taymans committed
1131
1132
1133
1134
1135
  RTP_SESSION_LOCK (sess);
  result = sess->stats.bandwidth;
  RTP_SESSION_UNLOCK (sess);

  return result;
1136
1137
1138
}

/**
Wim Taymans's avatar
Wim Taymans committed
1139
 * rtp_session_set_rtcp_fraction:
1140
1141
1142
 * @sess: an #RTPSession
 * @bandwidth: the RTCP bandwidth
 *
1143
 * Set the bandwidth in bytes per second that should be used for RTCP
1144
 * messages.
1145
1146
 */
void
Wim Taymans's avatar
Wim Taymans committed
1147
rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1148
1149
1150
{
  g_return_if_fail (RTP_IS_SESSION (sess));

Wim Taymans's avatar
Wim Taymans committed
1151
  RTP_SESSION_LOCK (sess);
1152
  sess->stats.rtcp_bandwidth = bandwidth;
Wim Taymans's avatar
Wim Taymans committed
1153
  RTP_SESSION_UNLOCK (sess);
1154
1155
1156
}

/**
Wim Taymans's avatar
Wim Taymans committed
1157
 * rtp_session_get_rtcp_fraction:
1158
1159
1160
1161
1162
1163
1164
 * @sess: an #RTPSession
 *
 * Get the session bandwidth used for RTCP.
 *
 * Returns: The bandwidth used for RTCP messages.
 */
gdouble
Wim Taymans's avatar
Wim Taymans committed
1165
rtp_session_get_rtcp_fraction (RTPSession * sess)
1166
{
Wim Taymans's avatar
Wim Taymans committed
1167
  gdouble result;
1168

Wim Taymans's avatar
Wim Taymans committed
1169
  g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1170

Wim Taymans's avatar
Wim Taymans committed
1171
1172
1173
  RTP_SESSION_LOCK (sess);
  result = sess->stats.rtcp_bandwidth;
  RTP_SESSION_UNLOCK (sess);
1174

Wim Taymans's avatar
Wim Taymans committed
1175
  return result;
1176
1177
}

1178
1179
1180
1181
1182
1183
/**
 * rtp_session_get_sdes_struct:
 * @sess: an #RTSPSession
 *
 * Get the SDES data as a #GstStructure
 *
1184
1185
 * Returns: a GstStructure with SDES items for @sess. This function returns a
 * copy of the SDES structure, use gst_structure_free() after usage.
1186
1187
1188
1189
 */
GstStructure *
rtp_session_get_sdes_struct (RTPSession * sess)
{
1190
  GstStructure *result = NULL;
1191
1192
1193
1194

  g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);

  RTP_SESSION_LOCK (sess);
1195
1196
  if (sess->sdes)
    result = gst_structure_copy (sess->sdes);
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
  RTP_SESSION_UNLOCK (sess);

  return result;
}

/**
 * rtp_session_set_sdes_struct:
 * @sess: an #RTSPSession
 * @sdes: a #GstStructure
 *
1207
 * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1208
1209
1210
1211
 */
void
rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
{
1212
  g_return_if_fail (sdes);
1213
1214
1215
  g_return_if_fail (RTP_IS_SESSION (sess));

  RTP_SESSION_LOCK (sess);
1216
1217
1218
  if (sess->sdes)
    gst_structure_free (sess->sdes);
  sess->sdes = gst_structure_copy (sdes);
1219
1220
1221
  RTP_SESSION_UNLOCK (sess);
}

1222
static GstFlowReturn
1223
source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1224
1225
1226
{
  GstFlowReturn result = GST_FLOW_OK;

1227
  if (source->internal) {
1228
    GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1229

1230
    RTP_SESSION_UNLOCK (session);
1231

1232
1233
    if (session->callbacks.send_rtp)
      result =
1234
          session->callbacks.send_rtp (session, source, data,
1235
          session->send_rtp_user_data);
1236
1237
1238
    else {
      gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
    }
1239
  } else {
1240
    GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1241
1242
    RTP_SESSION_UNLOCK (session);

1243
1244
    if (session->callbacks.process_rtp)
      result =