gst/rtpmanager/gstrtpsession.c: Use method to get the internal SSRC.
[platform/upstream/gstreamer.git] / gst / rtpmanager / rtpsession.c
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 #include <string.h>
21
22 #include <gst/rtp/gstrtpbuffer.h>
23 #include <gst/rtp/gstrtcpbuffer.h>
24 #include <gst/netbuffer/gstnetbuffer.h>
25
26 #include "gstrtpbin-marshal.h"
27 #include "rtpsession.h"
28
29 GST_DEBUG_CATEGORY_STATIC (rtp_session_debug);
30 #define GST_CAT_DEFAULT rtp_session_debug
31
32 /* signals and args */
33 enum
34 {
35   SIGNAL_GET_SOURCE_BY_SSRC,
36   SIGNAL_ON_NEW_SSRC,
37   SIGNAL_ON_SSRC_COLLISION,
38   SIGNAL_ON_SSRC_VALIDATED,
39   SIGNAL_ON_SSRC_ACTIVE,
40   SIGNAL_ON_SSRC_SDES,
41   SIGNAL_ON_BYE_SSRC,
42   SIGNAL_ON_BYE_TIMEOUT,
43   SIGNAL_ON_TIMEOUT,
44   SIGNAL_ON_SENDER_TIMEOUT,
45   LAST_SIGNAL
46 };
47
48 #define DEFAULT_INTERNAL_SOURCE      NULL
49 #define DEFAULT_BANDWIDTH            RTP_STATS_BANDWIDTH
50 #define DEFAULT_RTCP_FRACTION        RTP_STATS_RTCP_BANDWIDTH
51 #define DEFAULT_RTCP_MTU             1400
52 #define DEFAULT_SDES_CNAME           NULL
53 #define DEFAULT_SDES_NAME            NULL
54 #define DEFAULT_SDES_EMAIL           NULL
55 #define DEFAULT_SDES_PHONE           NULL
56 #define DEFAULT_SDES_LOCATION        NULL
57 #define DEFAULT_SDES_TOOL            NULL
58 #define DEFAULT_SDES_NOTE            NULL
59 #define DEFAULT_NUM_SOURCES          0
60 #define DEFAULT_NUM_ACTIVE_SOURCES   0
61 #define DEFAULT_SOURCES              NULL
62
63 enum
64 {
65   PROP_0,
66   PROP_INTERNAL_SSRC,
67   PROP_INTERNAL_SOURCE,
68   PROP_BANDWIDTH,
69   PROP_RTCP_FRACTION,
70   PROP_RTCP_MTU,
71   PROP_SDES_CNAME,
72   PROP_SDES_NAME,
73   PROP_SDES_EMAIL,
74   PROP_SDES_PHONE,
75   PROP_SDES_LOCATION,
76   PROP_SDES_TOOL,
77   PROP_SDES_NOTE,
78   PROP_NUM_SOURCES,
79   PROP_NUM_ACTIVE_SOURCES,
80   PROP_SOURCES,
81   PROP_LAST
82 };
83
84 /* update average packet size, we keep this scaled by 16 to keep enough
85  * precision. */
86 #define UPDATE_AVG(avg, val)            \
87   if ((avg) == 0)                       \
88    (avg) = (val) << 4;                  \
89   else                                  \
90    (avg) = ((val) + (15 * (avg))) >> 4;
91
92 /* The number RTCP intervals after which to timeout entries in the
93  * collision table
94  */
95 #define RTCP_INTERVAL_COLLISION_TIMEOUT 10
96
97 /* GObject vmethods */
98 static void rtp_session_finalize (GObject * object);
99 static void rtp_session_set_property (GObject * object, guint prop_id,
100     const GValue * value, GParamSpec * pspec);
101 static void rtp_session_get_property (GObject * object, guint prop_id,
102     GValue * value, GParamSpec * pspec);
103
104 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
105
106 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
107
108 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
109     gboolean * created, RTPArrivalStats * arrival, gboolean rtp);
110 static GstFlowReturn rtp_session_send_bye_locked (RTPSession * sess,
111     const gchar * reason, GstClockTime current_time);
112 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
113     gboolean deterministic, gboolean first);
114
115 static void
116 rtp_session_class_init (RTPSessionClass * klass)
117 {
118   GObjectClass *gobject_class;
119
120   gobject_class = (GObjectClass *) klass;
121
122   gobject_class->finalize = rtp_session_finalize;
123   gobject_class->set_property = rtp_session_set_property;
124   gobject_class->get_property = rtp_session_get_property;
125
126   /**
127    * RTPSession::get-source-by-ssrc:
128    * @session: the object which received the signal
129    * @ssrc: the SSRC of the RTPSource
130    *
131    * Request the #RTPSource object with SSRC @ssrc in @session.
132    */
133   rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
134       g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
135       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
136           get_source_by_ssrc), NULL, NULL, gst_rtp_bin_marshal_OBJECT__UINT,
137       RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
138
139   /**
140    * RTPSession::on-new-ssrc:
141    * @session: the object which received the signal
142    * @src: the new RTPSource
143    *
144    * Notify of a new SSRC that entered @session.
145    */
146   rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
147       g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
148       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
149       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
150       RTP_TYPE_SOURCE);
151   /**
152    * RTPSession::on-ssrc-collision:
153    * @session: the object which received the signal
154    * @src: the #RTPSource that caused a collision
155    *
156    * Notify when we have an SSRC collision
157    */
158   rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
159       g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
160       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
161       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
162       RTP_TYPE_SOURCE);
163   /**
164    * RTPSession::on-ssrc-validated:
165    * @session: the object which received the signal
166    * @src: the new validated RTPSource
167    *
168    * Notify of a new SSRC that became validated.
169    */
170   rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
171       g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
172       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
173       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
174       RTP_TYPE_SOURCE);
175   /**
176    * RTPSession::on-ssrc-active:
177    * @session: the object which received the signal
178    * @src: the active RTPSource
179    *
180    * Notify of a SSRC that is active, i.e., sending RTCP.
181    */
182   rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
183       g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
184       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
185       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
186       RTP_TYPE_SOURCE);
187   /**
188    * RTPSession::on-ssrc-sdes:
189    * @session: the object which received the signal
190    * @src: the RTPSource
191    *
192    * Notify that a new SDES was received for SSRC.
193    */
194   rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
195       g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
196       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
197       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
198       RTP_TYPE_SOURCE);
199   /**
200    * RTPSession::on-bye-ssrc:
201    * @session: the object which received the signal
202    * @src: the RTPSource that went away
203    *
204    * Notify of an SSRC that became inactive because of a BYE packet.
205    */
206   rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
207       g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
208       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
209       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
210       RTP_TYPE_SOURCE);
211   /**
212    * RTPSession::on-bye-timeout:
213    * @session: the object which received the signal
214    * @src: the RTPSource that timed out
215    *
216    * Notify of an SSRC that has timed out because of BYE
217    */
218   rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
219       g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
220       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
221       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
222       RTP_TYPE_SOURCE);
223   /**
224    * RTPSession::on-timeout:
225    * @session: the object which received the signal
226    * @src: the RTPSource that timed out
227    *
228    * Notify of an SSRC that has timed out
229    */
230   rtp_session_signals[SIGNAL_ON_TIMEOUT] =
231       g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
232       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
233       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
234       RTP_TYPE_SOURCE);
235   /**
236    * RTPSession::on-sender-timeout:
237    * @session: the object which received the signal
238    * @src: the RTPSource that timed out
239    *
240    * Notify of an SSRC that was a sender but timed out and became a receiver.
241    */
242   rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
243       g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
244       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
245       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
246       RTP_TYPE_SOURCE);
247
248   g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
249       g_param_spec_uint ("internal-ssrc", "Internal SSRC",
250           "The internal SSRC used for the session",
251           0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252
253   g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
254       g_param_spec_object ("internal-source", "Internal Source",
255           "The internal source element of the session",
256           RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
257
258   g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
259       g_param_spec_double ("bandwidth", "Bandwidth",
260           "The bandwidth of the session",
261           0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
262           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263
264   g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
265       g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
266           "The fraction of the bandwidth used for RTCP",
267           0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
268           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
269
270   g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
271       g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
272           "The maximum size of the RTCP packets",
273           16, G_MAXINT16, DEFAULT_RTCP_MTU,
274           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275
276   g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
277       g_param_spec_string ("sdes-cname", "SDES CNAME",
278           "The CNAME to put in SDES messages of this session",
279           DEFAULT_SDES_CNAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
280
281   g_object_class_install_property (gobject_class, PROP_SDES_NAME,
282       g_param_spec_string ("sdes-name", "SDES NAME",
283           "The NAME to put in SDES messages of this session",
284           DEFAULT_SDES_NAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
285
286   g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
287       g_param_spec_string ("sdes-email", "SDES EMAIL",
288           "The EMAIL to put in SDES messages of this session",
289           DEFAULT_SDES_EMAIL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
290
291   g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
292       g_param_spec_string ("sdes-phone", "SDES PHONE",
293           "The PHONE to put in SDES messages of this session",
294           DEFAULT_SDES_PHONE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295
296   g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
297       g_param_spec_string ("sdes-location", "SDES LOCATION",
298           "The LOCATION to put in SDES messages of this session",
299           DEFAULT_SDES_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
300
301   g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
302       g_param_spec_string ("sdes-tool", "SDES TOOL",
303           "The TOOL to put in SDES messages of this session",
304           DEFAULT_SDES_TOOL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
305
306   g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
307       g_param_spec_string ("sdes-note", "SDES NOTE",
308           "The NOTE to put in SDES messages of this session",
309           DEFAULT_SDES_NOTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
310
311   g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
312       g_param_spec_uint ("num-sources", "Num Sources",
313           "The number of sources in the session", 0, G_MAXUINT,
314           DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
315
316   g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
317       g_param_spec_uint ("num-active-sources", "Num Active Sources",
318           "The number of active sources in the session", 0, G_MAXUINT,
319           DEFAULT_NUM_ACTIVE_SOURCES,
320           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
321   /**
322    * RTPSource::sources
323    *
324    * Get a GValue Array of all sources in the session.
325    *
326    * <example>
327    * <title>Getting the #RTPSources of a session
328    * <programlisting>
329    * {
330    *   GValueArray *arr;
331    *   GValue *val;
332    *   guint i;
333    *
334    *   g_object_get (sess, "sources", &arr, NULL);
335    *
336    *   for (i = 0; i < arr->n_values; i++) {
337    *     RTPSource *source;
338    *
339    *     val = g_value_array_get_nth (arr, i);
340    *     source = g_value_get_object (val);
341    *   }
342    *   g_value_array_free (arr);
343    * }
344    * </programlisting>
345    * </example>
346    */
347   g_object_class_install_property (gobject_class, PROP_SOURCES,
348       g_param_spec_boxed ("sources", "Sources",
349           "An array of all known sources in the session",
350           G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
351
352   klass->get_source_by_ssrc =
353       GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
354
355   GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
356 }
357
358 static void
359 rtp_session_init (RTPSession * sess)
360 {
361   gint i;
362   gchar *str;
363
364   sess->lock = g_mutex_new ();
365   sess->key = g_random_int ();
366   sess->mask_idx = 0;
367   sess->mask = 0;
368
369   for (i = 0; i < 32; i++) {
370     sess->ssrcs[i] =
371         g_hash_table_new_full (NULL, NULL, NULL,
372         (GDestroyNotify) g_object_unref);
373   }
374   sess->cnames = g_hash_table_new_full (NULL, NULL, g_free, NULL);
375
376   rtp_stats_init_defaults (&sess->stats);
377
378   /* create an active SSRC for this session manager */
379   sess->source = rtp_session_create_source (sess);
380   sess->source->validated = TRUE;
381   sess->source->internal = TRUE;
382   sess->stats.active_sources++;
383
384   /* default UDP header length */
385   sess->header_len = 28;
386   sess->mtu = DEFAULT_RTCP_MTU;
387
388   /* some default SDES entries */
389   str = g_strdup_printf ("%s@%s", g_get_user_name (), g_get_host_name ());
390   rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_CNAME, str);
391   g_free (str);
392
393   rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_NAME,
394       g_get_real_name ());
395   rtp_source_set_sdes_string (sess->source, GST_RTCP_SDES_TOOL, "GStreamer");
396
397   sess->first_rtcp = TRUE;
398
399   GST_DEBUG ("%p: session using SSRC: %08x", sess, sess->source->ssrc);
400 }
401
402 static void
403 rtp_session_finalize (GObject * object)
404 {
405   RTPSession *sess;
406   gint i;
407
408   sess = RTP_SESSION_CAST (object);
409
410   g_mutex_free (sess->lock);
411   for (i = 0; i < 32; i++)
412     g_hash_table_destroy (sess->ssrcs[i]);
413
414   g_free (sess->bye_reason);
415
416   g_hash_table_destroy (sess->cnames);
417   g_object_unref (sess->source);
418
419   G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
420 }
421
422 static void
423 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
424 {
425   GValue value = { 0 };
426
427   g_value_init (&value, RTP_TYPE_SOURCE);
428   g_value_take_object (&value, source);
429   g_value_array_append (arr, &value);
430 }
431
432 static GValueArray *
433 rtp_session_create_sources (RTPSession * sess)
434 {
435   GValueArray *res;
436   guint size;
437
438   RTP_SESSION_LOCK (sess);
439   /* get number of elements in the table */
440   size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
441   /* create the result value array */
442   res = g_value_array_new (size);
443
444   /* and copy all values into the array */
445   g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
446   RTP_SESSION_UNLOCK (sess);
447
448   return res;
449 }
450
451 static void
452 rtp_session_set_property (GObject * object, guint prop_id,
453     const GValue * value, GParamSpec * pspec)
454 {
455   RTPSession *sess;
456
457   sess = RTP_SESSION (object);
458
459   switch (prop_id) {
460     case PROP_INTERNAL_SSRC:
461       rtp_session_set_internal_ssrc (sess, g_value_get_uint (value));
462       break;
463     case PROP_BANDWIDTH:
464       rtp_session_set_bandwidth (sess, g_value_get_double (value));
465       break;
466     case PROP_RTCP_FRACTION:
467       rtp_session_set_rtcp_fraction (sess, g_value_get_double (value));
468       break;
469     case PROP_RTCP_MTU:
470       sess->mtu = g_value_get_uint (value);
471       break;
472     case PROP_SDES_CNAME:
473       rtp_session_set_sdes_string (sess, GST_RTCP_SDES_CNAME,
474           g_value_get_string (value));
475       break;
476     case PROP_SDES_NAME:
477       rtp_session_set_sdes_string (sess, GST_RTCP_SDES_NAME,
478           g_value_get_string (value));
479       break;
480     case PROP_SDES_EMAIL:
481       rtp_session_set_sdes_string (sess, GST_RTCP_SDES_EMAIL,
482           g_value_get_string (value));
483       break;
484     case PROP_SDES_PHONE:
485       rtp_session_set_sdes_string (sess, GST_RTCP_SDES_PHONE,
486           g_value_get_string (value));
487       break;
488     case PROP_SDES_LOCATION:
489       rtp_session_set_sdes_string (sess, GST_RTCP_SDES_LOC,
490           g_value_get_string (value));
491       break;
492     case PROP_SDES_TOOL:
493       rtp_session_set_sdes_string (sess, GST_RTCP_SDES_TOOL,
494           g_value_get_string (value));
495       break;
496     case PROP_SDES_NOTE:
497       rtp_session_set_sdes_string (sess, GST_RTCP_SDES_NOTE,
498           g_value_get_string (value));
499       break;
500     default:
501       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
502       break;
503   }
504 }
505
506 static void
507 rtp_session_get_property (GObject * object, guint prop_id,
508     GValue * value, GParamSpec * pspec)
509 {
510   RTPSession *sess;
511
512   sess = RTP_SESSION (object);
513
514   switch (prop_id) {
515     case PROP_INTERNAL_SSRC:
516       g_value_set_uint (value, rtp_session_get_internal_ssrc (sess));
517       break;
518     case PROP_INTERNAL_SOURCE:
519       g_value_take_object (value, rtp_session_get_internal_source (sess));
520       break;
521     case PROP_BANDWIDTH:
522       g_value_set_double (value, rtp_session_get_bandwidth (sess));
523       break;
524     case PROP_RTCP_FRACTION:
525       g_value_set_double (value, rtp_session_get_rtcp_fraction (sess));
526       break;
527     case PROP_RTCP_MTU:
528       g_value_set_uint (value, sess->mtu);
529       break;
530     case PROP_SDES_CNAME:
531       g_value_take_string (value, rtp_session_get_sdes_string (sess,
532               GST_RTCP_SDES_CNAME));
533       break;
534     case PROP_SDES_NAME:
535       g_value_take_string (value, rtp_session_get_sdes_string (sess,
536               GST_RTCP_SDES_NAME));
537       break;
538     case PROP_SDES_EMAIL:
539       g_value_take_string (value, rtp_session_get_sdes_string (sess,
540               GST_RTCP_SDES_EMAIL));
541       break;
542     case PROP_SDES_PHONE:
543       g_value_take_string (value, rtp_session_get_sdes_string (sess,
544               GST_RTCP_SDES_PHONE));
545       break;
546     case PROP_SDES_LOCATION:
547       g_value_take_string (value, rtp_session_get_sdes_string (sess,
548               GST_RTCP_SDES_LOC));
549       break;
550     case PROP_SDES_TOOL:
551       g_value_take_string (value, rtp_session_get_sdes_string (sess,
552               GST_RTCP_SDES_TOOL));
553       break;
554     case PROP_SDES_NOTE:
555       g_value_take_string (value, rtp_session_get_sdes_string (sess,
556               GST_RTCP_SDES_NOTE));
557       break;
558     case PROP_NUM_SOURCES:
559       g_value_set_uint (value, rtp_session_get_num_sources (sess));
560       break;
561     case PROP_NUM_ACTIVE_SOURCES:
562       g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
563       break;
564     case PROP_SOURCES:
565       g_value_take_boxed (value, rtp_session_create_sources (sess));
566       break;
567     default:
568       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
569       break;
570   }
571 }
572
573 static void
574 on_new_ssrc (RTPSession * sess, RTPSource * source)
575 {
576   g_object_ref (source);
577   RTP_SESSION_UNLOCK (sess);
578   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
579   RTP_SESSION_LOCK (sess);
580   g_object_unref (source);
581 }
582
583 static void
584 on_ssrc_collision (RTPSession * sess, RTPSource * source)
585 {
586   g_object_ref (source);
587   RTP_SESSION_UNLOCK (sess);
588   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
589       source);
590   RTP_SESSION_LOCK (sess);
591   g_object_unref (source);
592 }
593
594 static void
595 on_ssrc_validated (RTPSession * sess, RTPSource * source)
596 {
597   g_object_ref (source);
598   RTP_SESSION_UNLOCK (sess);
599   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
600       source);
601   RTP_SESSION_LOCK (sess);
602   g_object_unref (source);
603 }
604
605 static void
606 on_ssrc_active (RTPSession * sess, RTPSource * source)
607 {
608   g_object_ref (source);
609   RTP_SESSION_UNLOCK (sess);
610   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
611   RTP_SESSION_LOCK (sess);
612   g_object_unref (source);
613 }
614
615 static void
616 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
617 {
618   g_object_ref (source);
619   GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
620   RTP_SESSION_UNLOCK (sess);
621   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
622   RTP_SESSION_LOCK (sess);
623   g_object_unref (source);
624 }
625
626 static void
627 on_bye_ssrc (RTPSession * sess, RTPSource * source)
628 {
629   g_object_ref (source);
630   RTP_SESSION_UNLOCK (sess);
631   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
632   RTP_SESSION_LOCK (sess);
633   g_object_unref (source);
634 }
635
636 static void
637 on_bye_timeout (RTPSession * sess, RTPSource * source)
638 {
639   g_object_ref (source);
640   RTP_SESSION_UNLOCK (sess);
641   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
642   RTP_SESSION_LOCK (sess);
643   g_object_unref (source);
644 }
645
646 static void
647 on_timeout (RTPSession * sess, RTPSource * source)
648 {
649   g_object_ref (source);
650   RTP_SESSION_UNLOCK (sess);
651   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
652   RTP_SESSION_LOCK (sess);
653   g_object_unref (source);
654 }
655
656 static void
657 on_sender_timeout (RTPSession * sess, RTPSource * source)
658 {
659   g_object_ref (source);
660   RTP_SESSION_UNLOCK (sess);
661   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
662       source);
663   RTP_SESSION_LOCK (sess);
664   g_object_unref (source);
665 }
666
667 /**
668  * rtp_session_new:
669  *
670  * Create a new session object.
671  *
672  * Returns: a new #RTPSession. g_object_unref() after usage.
673  */
674 RTPSession *
675 rtp_session_new (void)
676 {
677   RTPSession *sess;
678
679   sess = g_object_new (RTP_TYPE_SESSION, NULL);
680
681   return sess;
682 }
683
684 /**
685  * rtp_session_set_callbacks:
686  * @sess: an #RTPSession
687  * @callbacks: callbacks to configure
688  * @user_data: user data passed in the callbacks
689  *
690  * Configure a set of callbacks to be notified of actions.
691  */
692 void
693 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
694     gpointer user_data)
695 {
696   g_return_if_fail (RTP_IS_SESSION (sess));
697
698   if (callbacks->process_rtp) {
699     sess->callbacks.process_rtp = callbacks->process_rtp;
700     sess->process_rtp_user_data = user_data;
701   }
702   if (callbacks->send_rtp) {
703     sess->callbacks.send_rtp = callbacks->send_rtp;
704     sess->send_rtp_user_data = user_data;
705   }
706   if (callbacks->send_rtcp) {
707     sess->callbacks.send_rtcp = callbacks->send_rtcp;
708     sess->send_rtcp_user_data = user_data;
709   }
710   if (callbacks->sync_rtcp) {
711     sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
712     sess->sync_rtcp_user_data = user_data;
713   }
714   if (callbacks->clock_rate) {
715     sess->callbacks.clock_rate = callbacks->clock_rate;
716     sess->clock_rate_user_data = user_data;
717   }
718   if (callbacks->reconsider) {
719     sess->callbacks.reconsider = callbacks->reconsider;
720     sess->reconsider_user_data = user_data;
721   }
722 }
723
724 /**
725  * rtp_session_set_process_rtp_callback:
726  * @sess: an #RTPSession
727  * @callback: callback to set
728  * @user_data: user data passed in the callback
729  *
730  * Configure only the process_rtp callback to be notified of the process_rtp action.
731  */
732 void
733 rtp_session_set_process_rtp_callback (RTPSession * sess,
734     RTPSessionProcessRTP callback, gpointer user_data)
735 {
736   g_return_if_fail (RTP_IS_SESSION (sess));
737
738   sess->callbacks.process_rtp = callback;
739   sess->process_rtp_user_data = user_data;
740 }
741
742 /**
743  * rtp_session_set_send_rtp_callback:
744  * @sess: an #RTPSession
745  * @callback: callback to set
746  * @user_data: user data passed in the callback
747  *
748  * Configure only the send_rtp callback to be notified of the send_rtp action.
749  */
750 void
751 rtp_session_set_send_rtp_callback (RTPSession * sess,
752     RTPSessionSendRTP callback, gpointer user_data)
753 {
754   g_return_if_fail (RTP_IS_SESSION (sess));
755
756   sess->callbacks.send_rtp = callback;
757   sess->send_rtp_user_data = user_data;
758 }
759
760 /**
761  * rtp_session_set_send_rtcp_callback:
762  * @sess: an #RTPSession
763  * @callback: callback to set
764  * @user_data: user data passed in the callback
765  *
766  * Configure only the send_rtcp callback to be notified of the send_rtcp action.
767  */
768 void
769 rtp_session_set_send_rtcp_callback (RTPSession * sess,
770     RTPSessionSendRTCP callback, gpointer user_data)
771 {
772   g_return_if_fail (RTP_IS_SESSION (sess));
773
774   sess->callbacks.send_rtcp = callback;
775   sess->send_rtcp_user_data = user_data;
776 }
777
778 /**
779  * rtp_session_set_sync_rtcp_callback:
780  * @sess: an #RTPSession
781  * @callback: callback to set
782  * @user_data: user data passed in the callback
783  *
784  * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
785  */
786 void
787 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
788     RTPSessionSyncRTCP callback, gpointer user_data)
789 {
790   g_return_if_fail (RTP_IS_SESSION (sess));
791
792   sess->callbacks.sync_rtcp = callback;
793   sess->sync_rtcp_user_data = user_data;
794 }
795
796 /**
797  * rtp_session_set_clock_rate_callback:
798  * @sess: an #RTPSession
799  * @callback: callback to set
800  * @user_data: user data passed in the callback
801  *
802  * Configure only the clock_rate callback to be notified of the clock_rate action.
803  */
804 void
805 rtp_session_set_clock_rate_callback (RTPSession * sess,
806     RTPSessionClockRate callback, gpointer user_data)
807 {
808   g_return_if_fail (RTP_IS_SESSION (sess));
809
810   sess->callbacks.clock_rate = callback;
811   sess->clock_rate_user_data = user_data;
812 }
813
814 /**
815  * rtp_session_set_reconsider_callback:
816  * @sess: an #RTPSession
817  * @callback: callback to set
818  * @user_data: user data passed in the callback
819  *
820  * Configure only the reconsider callback to be notified of the reconsider action.
821  */
822 void
823 rtp_session_set_reconsider_callback (RTPSession * sess,
824     RTPSessionReconsider callback, gpointer user_data)
825 {
826   g_return_if_fail (RTP_IS_SESSION (sess));
827
828   sess->callbacks.reconsider = callback;
829   sess->reconsider_user_data = user_data;
830 }
831
832 /**
833  * rtp_session_set_bandwidth:
834  * @sess: an #RTPSession
835  * @bandwidth: the bandwidth allocated
836  *
837  * Set the session bandwidth in bytes per second.
838  */
839 void
840 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
841 {
842   g_return_if_fail (RTP_IS_SESSION (sess));
843
844   RTP_SESSION_LOCK (sess);
845   sess->stats.bandwidth = bandwidth;
846   RTP_SESSION_UNLOCK (sess);
847 }
848
849 /**
850  * rtp_session_get_bandwidth:
851  * @sess: an #RTPSession
852  *
853  * Get the session bandwidth.
854  *
855  * Returns: the session bandwidth.
856  */
857 gdouble
858 rtp_session_get_bandwidth (RTPSession * sess)
859 {
860   gdouble result;
861
862   g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
863
864   RTP_SESSION_LOCK (sess);
865   result = sess->stats.bandwidth;
866   RTP_SESSION_UNLOCK (sess);
867
868   return result;
869 }
870
871 /**
872  * rtp_session_set_rtcp_fraction:
873  * @sess: an #RTPSession
874  * @bandwidth: the RTCP bandwidth
875  *
876  * Set the bandwidth that should be used for RTCP
877  * messages.
878  */
879 void
880 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
881 {
882   g_return_if_fail (RTP_IS_SESSION (sess));
883
884   RTP_SESSION_LOCK (sess);
885   sess->stats.rtcp_bandwidth = bandwidth;
886   RTP_SESSION_UNLOCK (sess);
887 }
888
889 /**
890  * rtp_session_get_rtcp_fraction:
891  * @sess: an #RTPSession
892  *
893  * Get the session bandwidth used for RTCP.
894  *
895  * Returns: The bandwidth used for RTCP messages.
896  */
897 gdouble
898 rtp_session_get_rtcp_fraction (RTPSession * sess)
899 {
900   gdouble result;
901
902   g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
903
904   RTP_SESSION_LOCK (sess);
905   result = sess->stats.rtcp_bandwidth;
906   RTP_SESSION_UNLOCK (sess);
907
908   return result;
909 }
910
911 /**
912  * rtp_session_set_sdes_string:
913  * @sess: an #RTPSession
914  * @type: the type of the SDES item
915  * @item: a null-terminated string to set. 
916  *
917  * Store an SDES item of @type in @sess. 
918  *
919  * Returns: %FALSE if the data was unchanged @type is invalid.
920  */
921 gboolean
922 rtp_session_set_sdes_string (RTPSession * sess, GstRTCPSDESType type,
923     const gchar * item)
924 {
925   gboolean result;
926
927   g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
928
929   RTP_SESSION_LOCK (sess);
930   result = rtp_source_set_sdes_string (sess->source, type, item);
931   RTP_SESSION_UNLOCK (sess);
932
933   return result;
934 }
935
936 /**
937  * rtp_session_get_sdes_string:
938  * @sess: an #RTPSession
939  * @type: the type of the SDES item
940  *
941  * Get the SDES item of @type from @sess. 
942  *
943  * Returns: a null-terminated copy of the SDES item or NULL when @type was not
944  * valid. g_free() after usage.
945  */
946 gchar *
947 rtp_session_get_sdes_string (RTPSession * sess, GstRTCPSDESType type)
948 {
949   gchar *result;
950
951   g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
952
953   RTP_SESSION_LOCK (sess);
954   result = rtp_source_get_sdes_string (sess->source, type);
955   RTP_SESSION_UNLOCK (sess);
956
957   return result;
958 }
959
960 static GstFlowReturn
961 source_push_rtp (RTPSource * source, GstBuffer * buffer, RTPSession * session)
962 {
963   GstFlowReturn result = GST_FLOW_OK;
964
965   if (source == session->source) {
966     GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
967
968     RTP_SESSION_UNLOCK (session);
969
970     if (session->callbacks.send_rtp)
971       result =
972           session->callbacks.send_rtp (session, source, buffer,
973           session->send_rtp_user_data);
974     else
975       gst_buffer_unref (buffer);
976
977   } else {
978     GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
979     RTP_SESSION_UNLOCK (session);
980
981     if (session->callbacks.process_rtp)
982       result =
983           session->callbacks.process_rtp (session, source, buffer,
984           session->process_rtp_user_data);
985     else
986       gst_buffer_unref (buffer);
987   }
988   RTP_SESSION_LOCK (session);
989
990   return result;
991 }
992
993 static gint
994 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
995 {
996   gint result;
997
998   RTP_SESSION_UNLOCK (session);
999
1000   if (session->callbacks.clock_rate)
1001     result =
1002         session->callbacks.clock_rate (session, pt,
1003         session->clock_rate_user_data);
1004   else
1005     result = -1;
1006
1007   RTP_SESSION_LOCK (session);
1008
1009   GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1010
1011   return result;
1012 }
1013
1014 static RTPSourceCallbacks callbacks = {
1015   (RTPSourcePushRTP) source_push_rtp,
1016   (RTPSourceClockRate) source_clock_rate,
1017 };
1018
1019 /**
1020  * find_add_conflicting_addresses:
1021  * @sess: The session to check in
1022  * @arrival: The arrival stats for the buffer
1023  *
1024  * Checks if an address which has a conflict is already known,
1025  *  otherwise remembers it to prevent loops.
1026  *
1027  * Returns: TRUE if it was a known conflict, FALSE otherwise
1028  */
1029
1030 static gboolean
1031 find_add_conflicting_addresses (RTPSession * sess, RTPArrivalStats * arrival)
1032 {
1033   GList *item;
1034   RTPConflictingAddress *new_conflict;
1035
1036   for (item = g_list_first (sess->conflicting_addresses);
1037       item; item = g_list_next (item)) {
1038     RTPConflictingAddress *known_conflict = item->data;
1039
1040     if (gst_netaddress_equal (&arrival->address, &known_conflict->address)) {
1041       known_conflict->time = arrival->time;
1042       return TRUE;
1043     }
1044   }
1045
1046   new_conflict = g_new0 (RTPConflictingAddress, 1);
1047
1048   memcpy (&new_conflict->address, &arrival->address, sizeof (GstNetAddress));
1049   new_conflict->time = arrival->time;
1050
1051   sess->conflicting_addresses = g_list_prepend (sess->conflicting_addresses,
1052       new_conflict);
1053
1054   return FALSE;
1055 }
1056
1057 static gboolean
1058 check_collision (RTPSession * sess, RTPSource * source,
1059     RTPArrivalStats * arrival, gboolean rtp)
1060 {
1061   /* If we have no arrival address, we can't do collision checking */
1062   if (!arrival->have_address)
1063     return FALSE;
1064
1065   if (sess->source != source) {
1066     /* This is not our local source, but lets check if two remote
1067      * source collide
1068      */
1069     if (rtp) {
1070       if (source->have_rtp_from) {
1071         if (gst_netaddress_equal (&source->rtp_from, &arrival->address))
1072           /* Address is the same */
1073           return FALSE;
1074       } else {
1075         /* We don't already have a from address for RTP, just set it */
1076         rtp_source_set_rtp_from (source, &arrival->address);
1077         return FALSE;
1078       }
1079     } else {
1080       if (source->have_rtcp_from) {
1081         if (gst_netaddress_equal (&source->rtcp_from, &arrival->address))
1082           /* Address is the same */
1083           return FALSE;
1084       } else {
1085         /* We don't already have a from address for RTCP, just set it */
1086         rtp_source_set_rtcp_from (source, &arrival->address);
1087         return FALSE;
1088       }
1089     }
1090     /* We received RTP or RTCP from this source before but the network address
1091      * changed. In this case, we have third-party collision or loop */
1092     GST_DEBUG ("we have a third-party collision or loop");
1093
1094     /* FIXME: Log 3rd party collision somehow
1095      * Maybe should be done in upper layer, only the SDES can tell us
1096      * if its a collision or a loop
1097      */
1098   } else {
1099     /* This is sending with our ssrc, is it an address we already know */
1100
1101     if (find_add_conflicting_addresses (sess, arrival)) {
1102       /* Its a known conflict, its probably a loop, not a collision
1103        * lets just drop the incoming packet
1104        */
1105       GST_DEBUG ("Our packets are being looped back to us, dropping");
1106     } else {
1107       /* Its a new collision, lets change our SSRC */
1108
1109       GST_DEBUG ("Collision for SSRC %x", rtp_source_get_ssrc (source));
1110       on_ssrc_collision (sess, source);
1111
1112       rtp_session_send_bye_locked (sess, "SSRC Collision", arrival->time);
1113
1114       sess->change_ssrc = TRUE;
1115     }
1116   }
1117
1118   return TRUE;
1119 }
1120
1121
1122 /* must be called with the session lock, the returned source needs to be
1123  * unreffed after usage. */
1124 static RTPSource *
1125 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1126     RTPArrivalStats * arrival, gboolean rtp)
1127 {
1128   RTPSource *source;
1129
1130   source =
1131       g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1132   if (source == NULL) {
1133     /* make new Source in probation and insert */
1134     source = rtp_source_new (ssrc);
1135
1136     /* for RTP packets we need to set the source in probation. Receiving RTCP
1137      * packets of an SSRC, on the other hand, is a strong indication that we
1138      * are dealing with a valid source. */
1139     if (rtp)
1140       source->probation = RTP_DEFAULT_PROBATION;
1141     else
1142       source->probation = 0;
1143
1144     /* store from address, if any */
1145     if (arrival->have_address) {
1146       if (rtp)
1147         rtp_source_set_rtp_from (source, &arrival->address);
1148       else
1149         rtp_source_set_rtcp_from (source, &arrival->address);
1150     }
1151
1152     /* configure a callback on the source */
1153     rtp_source_set_callbacks (source, &callbacks, sess);
1154
1155     g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1156         source);
1157
1158     /* we have one more source now */
1159     sess->total_sources++;
1160     *created = TRUE;
1161   } else {
1162     *created = FALSE;
1163     /* check for collision, this updates the address when not previously set */
1164     if (check_collision (sess, source, arrival, rtp)) {
1165       return NULL;
1166     }
1167   }
1168   /* update last activity */
1169   source->last_activity = arrival->time;
1170   if (rtp)
1171     source->last_rtp_activity = arrival->time;
1172   g_object_ref (source);
1173
1174   return source;
1175 }
1176
1177 /**
1178  * rtp_session_get_internal_source:
1179  * @sess: a #RTPSession
1180  *
1181  * Get the internal #RTPSource of @sess.
1182  *
1183  * Returns: The internal #RTPSource. g_object_unref() after usage.
1184  */
1185 RTPSource *
1186 rtp_session_get_internal_source (RTPSession * sess)
1187 {
1188   RTPSource *result;
1189
1190   g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1191
1192   result = g_object_ref (sess->source);
1193
1194   return result;
1195 }
1196
1197 /**
1198  * rtp_session_set_internal_ssrc:
1199  * @sess: a #RTPSession
1200  * @ssrc: an SSRC
1201  *
1202  * Set the SSRC of @sess to @ssrc.
1203  */
1204 void
1205 rtp_session_set_internal_ssrc (RTPSession * sess, guint32 ssrc)
1206 {
1207   RTP_SESSION_LOCK (sess);
1208   if (ssrc != sess->source->ssrc) {
1209     g_hash_table_steal (sess->ssrcs[sess->mask_idx],
1210         GINT_TO_POINTER (sess->source->ssrc));
1211
1212     sess->source->ssrc = ssrc;
1213     rtp_source_reset (sess->source);
1214
1215     g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1216         GINT_TO_POINTER (sess->source->ssrc), sess->source);
1217   }
1218   RTP_SESSION_UNLOCK (sess);
1219 }
1220
1221 /**
1222  * rtp_session_get_internal_ssrc:
1223  * @sess: a #RTPSession
1224  *
1225  * Get the internal SSRC of @sess.
1226  *
1227  * Returns: The SSRC of the session. 
1228  */
1229 guint32
1230 rtp_session_get_internal_ssrc (RTPSession * sess)
1231 {
1232   guint32 ssrc;
1233
1234   RTP_SESSION_LOCK (sess);
1235   ssrc = sess->source->ssrc;
1236   RTP_SESSION_UNLOCK (sess);
1237
1238   return ssrc;
1239 }
1240
1241 /**
1242  * rtp_session_add_source:
1243  * @sess: a #RTPSession
1244  * @src: #RTPSource to add
1245  *
1246  * Add @src to @session.
1247  *
1248  * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1249  * existed in the session.
1250  */
1251 gboolean
1252 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1253 {
1254   gboolean result = FALSE;
1255   RTPSource *find;
1256
1257   g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1258   g_return_val_if_fail (src != NULL, FALSE);
1259
1260   RTP_SESSION_LOCK (sess);
1261   find =
1262       g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1263       GINT_TO_POINTER (src->ssrc));
1264   if (find == NULL) {
1265     g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1266         GINT_TO_POINTER (src->ssrc), src);
1267     /* we have one more source now */
1268     sess->total_sources++;
1269     result = TRUE;
1270   }
1271   RTP_SESSION_UNLOCK (sess);
1272
1273   return result;
1274 }
1275
1276 /**
1277  * rtp_session_get_num_sources:
1278  * @sess: an #RTPSession
1279  *
1280  * Get the number of sources in @sess.
1281  *
1282  * Returns: The number of sources in @sess.
1283  */
1284 guint
1285 rtp_session_get_num_sources (RTPSession * sess)
1286 {
1287   guint result;
1288
1289   g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1290
1291   RTP_SESSION_LOCK (sess);
1292   result = sess->total_sources;
1293   RTP_SESSION_UNLOCK (sess);
1294
1295   return result;
1296 }
1297
1298 /**
1299  * rtp_session_get_num_active_sources:
1300  * @sess: an #RTPSession
1301  *
1302  * Get the number of active sources in @sess. A source is considered active when
1303  * it has been validated and has not yet received a BYE RTCP message.
1304  *
1305  * Returns: The number of active sources in @sess.
1306  */
1307 guint
1308 rtp_session_get_num_active_sources (RTPSession * sess)
1309 {
1310   guint result;
1311
1312   g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1313
1314   RTP_SESSION_LOCK (sess);
1315   result = sess->stats.active_sources;
1316   RTP_SESSION_UNLOCK (sess);
1317
1318   return result;
1319 }
1320
1321 /**
1322  * rtp_session_get_source_by_ssrc:
1323  * @sess: an #RTPSession
1324  * @ssrc: an SSRC
1325  *
1326  * Find the source with @ssrc in @sess.
1327  *
1328  * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
1329  * g_object_unref() after usage.
1330  */
1331 RTPSource *
1332 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
1333 {
1334   RTPSource *result;
1335
1336   g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1337
1338   RTP_SESSION_LOCK (sess);
1339   result =
1340       g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
1341   if (result)
1342     g_object_ref (result);
1343   RTP_SESSION_UNLOCK (sess);
1344
1345   return result;
1346 }
1347
1348 /**
1349  * rtp_session_get_source_by_cname:
1350  * @sess: a #RTPSession
1351  * @cname: an CNAME
1352  *
1353  * Find the source with @cname in @sess.
1354  *
1355  * Returns: a #RTPSource with CNAME @cname or NULL if the source was not found.
1356  * g_object_unref() after usage.
1357  */
1358 RTPSource *
1359 rtp_session_get_source_by_cname (RTPSession * sess, const gchar * cname)
1360 {
1361   RTPSource *result;
1362
1363   g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1364   g_return_val_if_fail (cname != NULL, NULL);
1365
1366   RTP_SESSION_LOCK (sess);
1367   result = g_hash_table_lookup (sess->cnames, cname);
1368   if (result)
1369     g_object_ref (result);
1370   RTP_SESSION_UNLOCK (sess);
1371
1372   return result;
1373 }
1374
1375 static guint32
1376 rtp_session_create_new_ssrc (RTPSession * sess)
1377 {
1378   guint32 ssrc;
1379
1380   while (TRUE) {
1381     ssrc = g_random_int ();
1382
1383     /* see if it exists in the session, we're done if it doesn't */
1384     if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1385             GINT_TO_POINTER (ssrc)) == NULL)
1386       break;
1387   }
1388
1389   return ssrc;
1390 }
1391
1392
1393 /**
1394  * rtp_session_create_source:
1395  * @sess: an #RTPSession
1396  *
1397  * Create an #RTPSource for use in @sess. This function will create a source
1398  * with an ssrc that is currently not used by any participants in the session.
1399  *
1400  * Returns: an #RTPSource.
1401  */
1402 RTPSource *
1403 rtp_session_create_source (RTPSession * sess)
1404 {
1405   guint32 ssrc;
1406   RTPSource *source;
1407
1408   RTP_SESSION_LOCK (sess);
1409   ssrc = rtp_session_create_new_ssrc (sess);
1410   source = rtp_source_new (ssrc);
1411   rtp_source_set_callbacks (source, &callbacks, sess);
1412   /* we need an additional ref for the source in the hashtable */
1413   g_object_ref (source);
1414   g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc),
1415       source);
1416   /* we have one more source now */
1417   sess->total_sources++;
1418   RTP_SESSION_UNLOCK (sess);
1419
1420   return source;
1421 }
1422
1423 /* update the RTPArrivalStats structure with the current time and other bits
1424  * about the current buffer we are handling.
1425  * This function is typically called when a validated packet is received.
1426  * This function should be called with the SESSION_LOCK
1427  */
1428 static void
1429 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
1430     gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
1431     GstClockTime running_time, guint64 ntpnstime)
1432 {
1433   /* get time of arrival */
1434   arrival->time = current_time;
1435   arrival->running_time = running_time;
1436   arrival->ntpnstime = ntpnstime;
1437
1438   /* get packet size including header overhead */
1439   arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
1440
1441   if (rtp) {
1442     arrival->payload_len = gst_rtp_buffer_get_payload_len (buffer);
1443   } else {
1444     arrival->payload_len = 0;
1445   }
1446
1447   /* for netbuffer we can store the IP address to check for collisions */
1448   arrival->have_address = GST_IS_NETBUFFER (buffer);
1449   if (arrival->have_address) {
1450     GstNetBuffer *netbuf = (GstNetBuffer *) buffer;
1451
1452     memcpy (&arrival->address, &netbuf->from, sizeof (GstNetAddress));
1453   }
1454 }
1455
1456 /**
1457  * rtp_session_process_rtp:
1458  * @sess: and #RTPSession
1459  * @buffer: an RTP buffer
1460  * @current_time: the current system time
1461  * @ntpnstime: the NTP arrival time in nanoseconds
1462  *
1463  * Process an RTP buffer in the session manager. This function takes ownership
1464  * of @buffer.
1465  *
1466  * Returns: a #GstFlowReturn.
1467  */
1468 GstFlowReturn
1469 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
1470     GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
1471 {
1472   GstFlowReturn result;
1473   guint32 ssrc;
1474   RTPSource *source;
1475   gboolean created;
1476   gboolean prevsender, prevactive;
1477   RTPArrivalStats arrival;
1478
1479   g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1480   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1481
1482   if (!gst_rtp_buffer_validate (buffer))
1483     goto invalid_packet;
1484
1485   RTP_SESSION_LOCK (sess);
1486   /* update arrival stats */
1487   update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
1488       running_time, ntpnstime);
1489
1490   /* ignore more RTP packets when we left the session */
1491   if (sess->source->received_bye)
1492     goto ignore;
1493
1494   /* get SSRC and look up in session database */
1495   ssrc = gst_rtp_buffer_get_ssrc (buffer);
1496   source = obtain_source (sess, ssrc, &created, &arrival, TRUE);
1497   if (!source)
1498     goto collision;
1499
1500   prevsender = RTP_SOURCE_IS_SENDER (source);
1501   prevactive = RTP_SOURCE_IS_ACTIVE (source);
1502
1503   /* we need to ref so that we can process the CSRCs later */
1504   gst_buffer_ref (buffer);
1505
1506   /* let source process the packet */
1507   result = rtp_source_process_rtp (source, buffer, &arrival);
1508
1509   /* source became active */
1510   if (prevactive != RTP_SOURCE_IS_ACTIVE (source)) {
1511     sess->stats.active_sources++;
1512     GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
1513         sess->stats.active_sources);
1514     on_ssrc_validated (sess, source);
1515   }
1516   if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1517     sess->stats.sender_sources++;
1518     GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
1519         sess->stats.sender_sources);
1520   }
1521
1522   if (created)
1523     on_new_ssrc (sess, source);
1524
1525   if (source->validated) {
1526     guint8 i, count;
1527     gboolean created;
1528
1529     /* for validated sources, we add the CSRCs as well */
1530     count = gst_rtp_buffer_get_csrc_count (buffer);
1531
1532     for (i = 0; i < count; i++) {
1533       guint32 csrc;
1534       RTPSource *csrc_src;
1535
1536       csrc = gst_rtp_buffer_get_csrc (buffer, i);
1537
1538       /* get source */
1539       csrc_src = obtain_source (sess, csrc, &created, &arrival, TRUE);
1540       if (!csrc_src)
1541         continue;
1542
1543       if (created) {
1544         GST_DEBUG ("created new CSRC: %08x", csrc);
1545         rtp_source_set_as_csrc (csrc_src);
1546         if (RTP_SOURCE_IS_ACTIVE (csrc_src))
1547           sess->stats.active_sources++;
1548         on_new_ssrc (sess, csrc_src);
1549       }
1550       g_object_unref (csrc_src);
1551     }
1552   }
1553   g_object_unref (source);
1554   gst_buffer_unref (buffer);
1555
1556   RTP_SESSION_UNLOCK (sess);
1557
1558   return result;
1559
1560   /* ERRORS */
1561 invalid_packet:
1562   {
1563     gst_buffer_unref (buffer);
1564     GST_DEBUG ("invalid RTP packet received");
1565     return GST_FLOW_OK;
1566   }
1567 ignore:
1568   {
1569     gst_buffer_unref (buffer);
1570     RTP_SESSION_UNLOCK (sess);
1571     GST_DEBUG ("ignoring RTP packet because we are leaving");
1572     return GST_FLOW_OK;
1573   }
1574 collision:
1575   {
1576     gst_buffer_unref (buffer);
1577     RTP_SESSION_UNLOCK (sess);
1578     GST_DEBUG ("ignoring packet because its collisioning");
1579     return GST_FLOW_OK;
1580   }
1581 }
1582
1583 static void
1584 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
1585     GstRTCPPacket * packet, RTPArrivalStats * arrival)
1586 {
1587   guint count, i;
1588
1589   count = gst_rtcp_packet_get_rb_count (packet);
1590   for (i = 0; i < count; i++) {
1591     guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
1592     guint8 fractionlost;
1593     gint32 packetslost;
1594
1595     gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
1596         &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
1597
1598     GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
1599
1600     if (ssrc == sess->source->ssrc) {
1601       /* only deal with report blocks for our session, we update the stats of
1602        * the sender of the RTCP message. We could also compare our stats against
1603        * the other sender to see if we are better or worse. */
1604       rtp_source_process_rb (source, arrival->time, fractionlost, packetslost,
1605           exthighestseq, jitter, lsr, dlsr);
1606
1607       on_ssrc_active (sess, source);
1608     }
1609   }
1610 }
1611
1612 /* A Sender report contains statistics about how the sender is doing. This
1613  * includes timing informataion such as the relation between RTP and NTP
1614  * timestamps and the number of packets/bytes it sent to us.
1615  *
1616  * In this report is also included a set of report blocks related to how this
1617  * sender is receiving data (in case we (or somebody else) is also sending stuff
1618  * to it). This info includes the packet loss, jitter and seqnum. It also
1619  * contains information to calculate the round trip time (LSR/DLSR).
1620  */
1621 static void
1622 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
1623     RTPArrivalStats * arrival)
1624 {
1625   guint32 senderssrc, rtptime, packet_count, octet_count;
1626   guint64 ntptime;
1627   RTPSource *source;
1628   gboolean created, prevsender;
1629
1630   gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
1631       &packet_count, &octet_count);
1632
1633   GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
1634       senderssrc, GST_TIME_ARGS (arrival->time));
1635
1636   source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1637   if (!source)
1638     return;
1639
1640   prevsender = RTP_SOURCE_IS_SENDER (source);
1641
1642   /* first update the source */
1643   rtp_source_process_sr (source, arrival->time, ntptime, rtptime, packet_count,
1644       octet_count);
1645
1646   if (prevsender != RTP_SOURCE_IS_SENDER (source)) {
1647     sess->stats.sender_sources++;
1648     GST_DEBUG ("source: %08x became sender, %d sender sources", senderssrc,
1649         sess->stats.sender_sources);
1650   }
1651
1652   if (created)
1653     on_new_ssrc (sess, source);
1654
1655   rtp_session_process_rb (sess, source, packet, arrival);
1656   g_object_unref (source);
1657 }
1658
1659 /* A receiver report contains statistics about how a receiver is doing. It
1660  * includes stuff like packet loss, jitter and the seqnum it received last. It
1661  * also contains info to calculate the round trip time.
1662  *
1663  * We are only interested in how the sender of this report is doing wrt to us.
1664  */
1665 static void
1666 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
1667     RTPArrivalStats * arrival)
1668 {
1669   guint32 senderssrc;
1670   RTPSource *source;
1671   gboolean created;
1672
1673   senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
1674
1675   GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
1676
1677   source = obtain_source (sess, senderssrc, &created, arrival, FALSE);
1678   if (!source)
1679     return;
1680
1681   if (created)
1682     on_new_ssrc (sess, source);
1683
1684   rtp_session_process_rb (sess, source, packet, arrival);
1685   g_object_unref (source);
1686 }
1687
1688 /* Get SDES items and store them in the SSRC */
1689 static void
1690 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
1691     RTPArrivalStats * arrival)
1692 {
1693   guint items, i, j;
1694   gboolean more_items, more_entries;
1695
1696   items = gst_rtcp_packet_sdes_get_item_count (packet);
1697   GST_DEBUG ("got SDES packet with %d items", items);
1698
1699   more_items = gst_rtcp_packet_sdes_first_item (packet);
1700   i = 0;
1701   while (more_items) {
1702     guint32 ssrc;
1703     gboolean changed, created;
1704     RTPSource *source;
1705
1706     ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
1707
1708     GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
1709
1710     changed = FALSE;
1711
1712     /* find src, no probation when dealing with RTCP */
1713     source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1714     if (!source)
1715       return;
1716
1717     more_entries = gst_rtcp_packet_sdes_first_entry (packet);
1718     j = 0;
1719     while (more_entries) {
1720       GstRTCPSDESType type;
1721       guint8 len;
1722       guint8 *data;
1723
1724       gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
1725
1726       GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
1727           data);
1728
1729       changed |= rtp_source_set_sdes (source, type, data, len);
1730
1731       more_entries = gst_rtcp_packet_sdes_next_entry (packet);
1732       j++;
1733     }
1734
1735     source->validated = TRUE;
1736
1737     if (created)
1738       on_new_ssrc (sess, source);
1739     if (changed)
1740       on_ssrc_sdes (sess, source);
1741
1742     g_object_unref (source);
1743
1744     more_items = gst_rtcp_packet_sdes_next_item (packet);
1745     i++;
1746   }
1747 }
1748
1749 /* BYE is sent when a client leaves the session
1750  */
1751 static void
1752 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
1753     RTPArrivalStats * arrival)
1754 {
1755   guint count, i;
1756   gchar *reason;
1757
1758   reason = gst_rtcp_packet_bye_get_reason (packet);
1759   GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
1760
1761   count = gst_rtcp_packet_bye_get_ssrc_count (packet);
1762   for (i = 0; i < count; i++) {
1763     guint32 ssrc;
1764     RTPSource *source;
1765     gboolean created, prevactive, prevsender;
1766     guint pmembers, members;
1767
1768     ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
1769     GST_DEBUG ("SSRC: %08x", ssrc);
1770
1771     /* find src and mark bye, no probation when dealing with RTCP */
1772     source = obtain_source (sess, ssrc, &created, arrival, FALSE);
1773     if (!source)
1774       return;
1775
1776     /* store time for when we need to time out this source */
1777     source->bye_time = arrival->time;
1778
1779     prevactive = RTP_SOURCE_IS_ACTIVE (source);
1780     prevsender = RTP_SOURCE_IS_SENDER (source);
1781
1782     /* let the source handle the rest */
1783     rtp_source_process_bye (source, reason);
1784
1785     pmembers = sess->stats.active_sources;
1786
1787     if (prevactive && !RTP_SOURCE_IS_ACTIVE (source)) {
1788       sess->stats.active_sources--;
1789       GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
1790           sess->stats.active_sources);
1791     }
1792     if (prevsender && !RTP_SOURCE_IS_SENDER (source)) {
1793       sess->stats.sender_sources--;
1794       GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
1795           sess->stats.sender_sources);
1796     }
1797     members = sess->stats.active_sources;
1798
1799     if (!sess->source->received_bye && members < pmembers) {
1800       /* some members went away since the previous timeout estimate.
1801        * Perform reverse reconsideration but only when we are not scheduling a
1802        * BYE ourselves. */
1803       if (arrival->time < sess->next_rtcp_check_time) {
1804         GstClockTime time_remaining;
1805
1806         time_remaining = sess->next_rtcp_check_time - arrival->time;
1807         sess->next_rtcp_check_time =
1808             gst_util_uint64_scale (time_remaining, members, pmembers);
1809
1810         GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
1811             GST_TIME_ARGS (sess->next_rtcp_check_time));
1812
1813         sess->next_rtcp_check_time += arrival->time;
1814
1815         RTP_SESSION_UNLOCK (sess);
1816         /* notify app of reconsideration */
1817         if (sess->callbacks.reconsider)
1818           sess->callbacks.reconsider (sess, sess->reconsider_user_data);
1819         RTP_SESSION_LOCK (sess);
1820       }
1821     }
1822
1823     if (created)
1824       on_new_ssrc (sess, source);
1825
1826     on_bye_ssrc (sess, source);
1827
1828     g_object_unref (source);
1829   }
1830   g_free (reason);
1831 }
1832
1833 static void
1834 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
1835     RTPArrivalStats * arrival)
1836 {
1837   GST_DEBUG ("received APP");
1838 }
1839
1840 /**
1841  * rtp_session_process_rtcp:
1842  * @sess: and #RTPSession
1843  * @buffer: an RTCP buffer
1844  * @current_time: the current system time
1845  *
1846  * Process an RTCP buffer in the session manager. This function takes ownership
1847  * of @buffer.
1848  *
1849  * Returns: a #GstFlowReturn.
1850  */
1851 GstFlowReturn
1852 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
1853     GstClockTime current_time)
1854 {
1855   GstRTCPPacket packet;
1856   gboolean more, is_bye = FALSE, is_sr = FALSE;
1857   RTPArrivalStats arrival;
1858   GstFlowReturn result = GST_FLOW_OK;
1859
1860   g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1861   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1862
1863   if (!gst_rtcp_buffer_validate (buffer))
1864     goto invalid_packet;
1865
1866   GST_DEBUG ("received RTCP packet");
1867
1868   RTP_SESSION_LOCK (sess);
1869   /* update arrival stats */
1870   update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1, -1);
1871
1872   if (sess->sent_bye)
1873     goto ignore;
1874
1875   /* make writable, we might want to change the buffer */
1876   buffer = gst_buffer_make_metadata_writable (buffer);
1877
1878   /* start processing the compound packet */
1879   more = gst_rtcp_buffer_get_first_packet (buffer, &packet);
1880   while (more) {
1881     GstRTCPType type;
1882
1883     type = gst_rtcp_packet_get_type (&packet);
1884
1885     /* when we are leaving the session, we should ignore all non-BYE messages */
1886     if (sess->source->received_bye && type != GST_RTCP_TYPE_BYE) {
1887       GST_DEBUG ("ignoring non-BYE RTCP packet because we are leaving");
1888       goto next;
1889     }
1890
1891     switch (type) {
1892       case GST_RTCP_TYPE_SR:
1893         rtp_session_process_sr (sess, &packet, &arrival);
1894         is_sr = TRUE;
1895         break;
1896       case GST_RTCP_TYPE_RR:
1897         rtp_session_process_rr (sess, &packet, &arrival);
1898         break;
1899       case GST_RTCP_TYPE_SDES:
1900         rtp_session_process_sdes (sess, &packet, &arrival);
1901         break;
1902       case GST_RTCP_TYPE_BYE:
1903         is_bye = TRUE;
1904         rtp_session_process_bye (sess, &packet, &arrival);
1905         break;
1906       case GST_RTCP_TYPE_APP:
1907         rtp_session_process_app (sess, &packet, &arrival);
1908         break;
1909       default:
1910         GST_WARNING ("got unknown RTCP packet");
1911         break;
1912     }
1913   next:
1914     more = gst_rtcp_packet_move_to_next (&packet);
1915   }
1916
1917   /* if we are scheduling a BYE, we only want to count bye packets, else we
1918    * count everything */
1919   if (sess->source->received_bye) {
1920     if (is_bye) {
1921       sess->stats.bye_members++;
1922       UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1923     }
1924   } else {
1925     /* keep track of average packet size */
1926     UPDATE_AVG (sess->stats.avg_rtcp_packet_size, arrival.bytes);
1927   }
1928   RTP_SESSION_UNLOCK (sess);
1929
1930   /* notify caller of sr packets in the callback */
1931   if (is_sr && sess->callbacks.sync_rtcp)
1932     result = sess->callbacks.sync_rtcp (sess, sess->source, buffer,
1933         sess->sync_rtcp_user_data);
1934   else
1935     gst_buffer_unref (buffer);
1936
1937   return result;
1938
1939   /* ERRORS */
1940 invalid_packet:
1941   {
1942     GST_DEBUG ("invalid RTCP packet received");
1943     gst_buffer_unref (buffer);
1944     return GST_FLOW_OK;
1945   }
1946 ignore:
1947   {
1948     gst_buffer_unref (buffer);
1949     RTP_SESSION_UNLOCK (sess);
1950     GST_DEBUG ("ignoring RTP packet because we left");
1951     return GST_FLOW_OK;
1952   }
1953 }
1954
1955 /**
1956  * rtp_session_send_rtp:
1957  * @sess: an #RTPSession
1958  * @buffer: an RTP buffer
1959  * @current_time: the current system time
1960  * @ntpnstime: the NTP time in nanoseconds of when this buffer was captured.
1961  * This is the buffer timestamp converted to NTP time.
1962  *
1963  * Send the RTP buffer in the session manager. This function takes ownership of
1964  * @buffer.
1965  *
1966  * Returns: a #GstFlowReturn.
1967  */
1968 GstFlowReturn
1969 rtp_session_send_rtp (RTPSession * sess, GstBuffer * buffer,
1970     GstClockTime current_time, guint64 ntpnstime)
1971 {
1972   GstFlowReturn result;
1973   RTPSource *source;
1974   gboolean prevsender;
1975
1976   g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
1977   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1978
1979   if (!gst_rtp_buffer_validate (buffer))
1980     goto invalid_packet;
1981
1982   GST_LOG ("received RTP packet for sending");
1983
1984   RTP_SESSION_LOCK (sess);
1985   source = sess->source;
1986
1987   /* update last activity */
1988   source->last_rtp_activity = current_time;
1989
1990   prevsender = RTP_SOURCE_IS_SENDER (source);
1991
1992   /* we use our own source to send */
1993   result = rtp_source_send_rtp (source, buffer, ntpnstime);
1994
1995   if (RTP_SOURCE_IS_SENDER (source) && !prevsender)
1996     sess->stats.sender_sources++;
1997   RTP_SESSION_UNLOCK (sess);
1998
1999   return result;
2000
2001   /* ERRORS */
2002 invalid_packet:
2003   {
2004     gst_buffer_unref (buffer);
2005     GST_DEBUG ("invalid RTP packet received");
2006     return GST_FLOW_OK;
2007   }
2008 }
2009
2010 static GstClockTime
2011 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
2012     gboolean first)
2013 {
2014   GstClockTime result;
2015
2016   if (sess->source->received_bye) {
2017     result = rtp_stats_calculate_bye_interval (&sess->stats);
2018   } else {
2019     result = rtp_stats_calculate_rtcp_interval (&sess->stats,
2020         RTP_SOURCE_IS_SENDER (sess->source), first);
2021   }
2022
2023   GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
2024       GST_TIME_ARGS (result), first);
2025
2026   if (!deterministic)
2027     result = rtp_stats_add_rtcp_jitter (&sess->stats, result);
2028
2029   GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2030
2031   return result;
2032 }
2033
2034 /**
2035  * rtp_session_send_bye_locked:
2036  * @sess: an #RTPSession
2037  * @reason: a reason or NULL
2038  *
2039  * Stop the current @sess and schedule a BYE message for the other members.
2040  *
2041  * One must have the session lock to call this function
2042  *
2043  * Returns: a #GstFlowReturn.
2044  */
2045 static GstFlowReturn
2046 rtp_session_send_bye_locked (RTPSession * sess, const gchar * reason,
2047     GstClockTime current_time)
2048 {
2049   GstFlowReturn result = GST_FLOW_OK;
2050   RTPSource *source;
2051   GstClockTime interval;
2052
2053   g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2054
2055   source = sess->source;
2056
2057   /* ignore more BYEs */
2058   if (source->received_bye)
2059     goto done;
2060
2061   /* we have BYE now */
2062   source->received_bye = TRUE;
2063   /* at least one member wants to send a BYE */
2064   g_free (sess->bye_reason);
2065   sess->bye_reason = g_strdup (reason);
2066   sess->stats.avg_rtcp_packet_size = 100;
2067   sess->stats.bye_members = 1;
2068   sess->first_rtcp = TRUE;
2069   sess->sent_bye = FALSE;
2070
2071   /* reschedule transmission */
2072   sess->last_rtcp_send_time = current_time;
2073   interval = calculate_rtcp_interval (sess, FALSE, TRUE);
2074   sess->next_rtcp_check_time = current_time + interval;
2075
2076   GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
2077       GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
2078
2079   RTP_SESSION_UNLOCK (sess);
2080   /* notify app of reconsideration */
2081   if (sess->callbacks.reconsider)
2082     sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2083   RTP_SESSION_LOCK (sess);
2084 done:
2085
2086   return result;
2087 }
2088
2089 /**
2090  * rtp_session_send_bye:
2091  * @sess: an #RTPSession
2092  * @reason: a reason or NULL
2093  * @current_time: the current system time
2094  *
2095  * Stop the current @sess and schedule a BYE message for the other members.
2096  *
2097  * One must have the session lock to call this function
2098  *
2099  * Returns: a #GstFlowReturn.
2100  */
2101 GstFlowReturn
2102 rtp_session_send_bye (RTPSession * sess, const gchar * reason,
2103     GstClockTime current_time)
2104 {
2105   GstFlowReturn result = GST_FLOW_OK;
2106
2107   g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2108
2109   RTP_SESSION_LOCK (sess);
2110   result = rtp_session_send_bye_locked (sess, reason, current_time);
2111   RTP_SESSION_UNLOCK (sess);
2112
2113   return result;
2114 }
2115
2116 /**
2117  * rtp_session_next_timeout:
2118  * @sess: an #RTPSession
2119  * @current_time: the current system time
2120  *
2121  * Get the next time we should perform session maintenance tasks.
2122  *
2123  * Returns: a time when rtp_session_on_timeout() should be called with the
2124  * current system time.
2125  */
2126 GstClockTime
2127 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
2128 {
2129   GstClockTime result;
2130
2131   g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2132
2133   RTP_SESSION_LOCK (sess);
2134
2135   result = sess->next_rtcp_check_time;
2136
2137   GST_DEBUG ("current time: %" GST_TIME_FORMAT ", next :%" GST_TIME_FORMAT,
2138       GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
2139
2140   if (result < current_time) {
2141     GST_DEBUG ("take current time as base");
2142     /* our previous check time expired, start counting from the current time
2143      * again. */
2144     result = current_time;
2145   }
2146
2147   if (sess->source->received_bye) {
2148     if (sess->sent_bye) {
2149       GST_DEBUG ("we sent BYE already");
2150       result = GST_CLOCK_TIME_NONE;
2151     } else if (sess->stats.active_sources >= 50) {
2152       GST_DEBUG ("reconsider BYE, more than 50 sources");
2153       /* reconsider BYE if members >= 50 */
2154       result += calculate_rtcp_interval (sess, FALSE, TRUE);
2155     }
2156   } else {
2157     if (sess->first_rtcp) {
2158       GST_DEBUG ("first RTCP packet");
2159       /* we are called for the first time */
2160       result += calculate_rtcp_interval (sess, FALSE, TRUE);
2161     } else if (sess->next_rtcp_check_time < current_time) {
2162       GST_DEBUG ("old check time expired, getting new timeout");
2163       /* get a new timeout when we need to */
2164       result += calculate_rtcp_interval (sess, FALSE, FALSE);
2165     }
2166   }
2167   sess->next_rtcp_check_time = result;
2168
2169   GST_DEBUG ("next timeout: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
2170   RTP_SESSION_UNLOCK (sess);
2171
2172   return result;
2173 }
2174
2175 typedef struct
2176 {
2177   RTPSession *sess;
2178   GstBuffer *rtcp;
2179   GstClockTime current_time;
2180   guint64 ntpnstime;
2181   GstClockTime interval;
2182   GstRTCPPacket packet;
2183   gboolean is_bye;
2184   gboolean has_sdes;
2185 } ReportData;
2186
2187 static void
2188 session_start_rtcp (RTPSession * sess, ReportData * data)
2189 {
2190   GstRTCPPacket *packet = &data->packet;
2191   RTPSource *own = sess->source;
2192
2193   data->rtcp = gst_rtcp_buffer_new (sess->mtu);
2194
2195   if (RTP_SOURCE_IS_SENDER (own)) {
2196     guint64 ntptime;
2197     guint32 rtptime;
2198     guint32 packet_count, octet_count;
2199
2200     /* we are a sender, create SR */
2201     GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
2202     gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SR, packet);
2203
2204     /* get latest stats */
2205     rtp_source_get_new_sr (own, data->ntpnstime, &ntptime, &rtptime,
2206         &packet_count, &octet_count);
2207     /* store stats */
2208     rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
2209         packet_count, octet_count);
2210
2211     /* fill in sender report info */
2212     gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
2213         ntptime, rtptime, packet_count, octet_count);
2214   } else {
2215     /* we are only receiver, create RR */
2216     GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
2217     gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_RR, packet);
2218     gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
2219   }
2220 }
2221
2222 /* construct a Sender or Receiver Report */
2223 static void
2224 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
2225 {
2226   RTPSession *sess = data->sess;
2227   GstRTCPPacket *packet = &data->packet;
2228
2229   /* create a new buffer if needed */
2230   if (data->rtcp == NULL) {
2231     session_start_rtcp (sess, data);
2232   }
2233   if (gst_rtcp_packet_get_rb_count (packet) < GST_RTCP_MAX_RB_COUNT) {
2234     /* only report about other sender sources */
2235     if (source != sess->source && RTP_SOURCE_IS_SENDER (source)) {
2236       guint8 fractionlost;
2237       gint32 packetslost;
2238       guint32 exthighestseq, jitter;
2239       guint32 lsr, dlsr;
2240
2241       /* get new stats */
2242       rtp_source_get_new_rb (source, data->current_time, &fractionlost,
2243           &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2244
2245       /* packet is not yet filled, add report block for this source. */
2246       gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
2247           exthighestseq, jitter, lsr, dlsr);
2248     }
2249   }
2250 }
2251
2252 /* perform cleanup of sources that timed out */
2253 static gboolean
2254 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
2255 {
2256   gboolean remove = FALSE;
2257   gboolean byetimeout = FALSE;
2258   gboolean sendertimeout = FALSE;
2259   gboolean is_sender, is_active;
2260   RTPSession *sess = data->sess;
2261   GstClockTime interval;
2262
2263   is_sender = RTP_SOURCE_IS_SENDER (source);
2264   is_active = RTP_SOURCE_IS_ACTIVE (source);
2265
2266   /* check for our own source, we don't want to delete our own source. */
2267   if (!(source == sess->source)) {
2268     if (source->received_bye) {
2269       /* if we received a BYE from the source, remove the source after some
2270        * time. */
2271       if (data->current_time > source->bye_time &&
2272           data->current_time - source->bye_time > sess->stats.bye_timeout) {
2273         GST_DEBUG ("removing BYE source %08x", source->ssrc);
2274         remove = TRUE;
2275         byetimeout = TRUE;
2276       }
2277     }
2278     /* sources that were inactive for more than 5 times the deterministic reporting
2279      * interval get timed out. the min timeout is 5 seconds. */
2280     if (data->current_time > source->last_activity) {
2281       interval = MAX (data->interval * 5, 5 * GST_SECOND);
2282       if (data->current_time - source->last_activity > interval) {
2283         GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
2284             source->ssrc, GST_TIME_ARGS (source->last_activity));
2285         remove = TRUE;
2286       }
2287     }
2288   }
2289
2290   /* senders that did not send for a long time become a receiver, this also
2291    * holds for our own source. */
2292   if (is_sender) {
2293     if (data->current_time > source->last_rtp_activity) {
2294       interval = MAX (data->interval * 2, 5 * GST_SECOND);
2295       if (data->current_time - source->last_rtp_activity > interval) {
2296         GST_DEBUG ("sender source %08x timed out and became receiver, last %"
2297             GST_TIME_FORMAT, source->ssrc,
2298             GST_TIME_ARGS (source->last_rtp_activity));
2299         source->is_sender = FALSE;
2300         sess->stats.sender_sources--;
2301         sendertimeout = TRUE;
2302       }
2303     }
2304   }
2305
2306   if (remove) {
2307     sess->total_sources--;
2308     if (is_sender)
2309       sess->stats.sender_sources--;
2310     if (is_active)
2311       sess->stats.active_sources--;
2312
2313     if (byetimeout)
2314       on_bye_timeout (sess, source);
2315     else
2316       on_timeout (sess, source);
2317   } else {
2318     if (sendertimeout)
2319       on_sender_timeout (sess, source);
2320   }
2321   return remove;
2322 }
2323
2324 static void
2325 session_sdes (RTPSession * sess, ReportData * data)
2326 {
2327   GstRTCPPacket *packet = &data->packet;
2328   guint8 *sdes_data;
2329   guint sdes_len;
2330
2331   /* add SDES packet */
2332   gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_SDES, packet);
2333
2334   gst_rtcp_packet_sdes_add_item (packet, sess->source->ssrc);
2335
2336   rtp_source_get_sdes (sess->source, GST_RTCP_SDES_CNAME, &sdes_data,
2337       &sdes_len);
2338   gst_rtcp_packet_sdes_add_entry (packet, GST_RTCP_SDES_CNAME, sdes_len,
2339       sdes_data);
2340
2341   /* other SDES items must only be added at regular intervals and only when the
2342    * user requests to since it might be a privacy problem */
2343 #if 0
2344   gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_NAME,
2345       strlen (sess->name), (guint8 *) sess->name);
2346   gst_rtcp_packet_sdes_add_entry (&packet, GST_RTCP_SDES_TOOL,
2347       strlen (sess->tool), (guint8 *) sess->tool);
2348 #endif
2349
2350   data->has_sdes = TRUE;
2351 }
2352
2353 /* schedule a BYE packet */
2354 static void
2355 session_bye (RTPSession * sess, ReportData * data)
2356 {
2357   GstRTCPPacket *packet = &data->packet;
2358
2359   /* open packet */
2360   session_start_rtcp (sess, data);
2361
2362   /* add SDES */
2363   session_sdes (sess, data);
2364
2365   /* add a BYE packet */
2366   gst_rtcp_buffer_add_packet (data->rtcp, GST_RTCP_TYPE_BYE, packet);
2367   gst_rtcp_packet_bye_add_ssrc (packet, sess->source->ssrc);
2368   if (sess->bye_reason)
2369     gst_rtcp_packet_bye_set_reason (packet, sess->bye_reason);
2370
2371   /* we have a BYE packet now */
2372   data->is_bye = TRUE;
2373 }
2374
2375 static gboolean
2376 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
2377 {
2378   GstClockTime new_send_time, elapsed;
2379   gboolean result;
2380
2381   /* no need to check yet */
2382   if (sess->next_rtcp_check_time > current_time) {
2383     GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
2384         GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
2385         GST_TIME_ARGS (current_time));
2386     return FALSE;
2387   }
2388
2389   /* get elapsed time since we last reported */
2390   elapsed = current_time - sess->last_rtcp_send_time;
2391
2392   /* perform forward reconsideration */
2393   new_send_time = rtp_stats_add_rtcp_jitter (&sess->stats, data->interval);
2394
2395   GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
2396       GST_TIME_FORMAT, GST_TIME_ARGS (new_send_time), GST_TIME_ARGS (elapsed));
2397
2398   new_send_time += sess->last_rtcp_send_time;
2399
2400   /* check if reconsideration */
2401   if (current_time < new_send_time) {
2402     GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
2403         GST_TIME_ARGS (new_send_time));
2404     result = FALSE;
2405     /* store new check time */
2406     sess->next_rtcp_check_time = new_send_time;
2407   } else {
2408     result = TRUE;
2409     new_send_time = calculate_rtcp_interval (sess, FALSE, FALSE);
2410
2411     GST_DEBUG ("can send RTCP now, next interval %" GST_TIME_FORMAT,
2412         GST_TIME_ARGS (new_send_time));
2413     sess->next_rtcp_check_time = current_time + new_send_time;
2414   }
2415   return result;
2416 }
2417
2418 /**
2419  * rtp_session_on_timeout:
2420  * @sess: an #RTPSession
2421  * @current_time: the current system time
2422  * @ntpnstime: the current NTP time in nanoseconds
2423  *
2424  * Perform maintenance actions after the timeout obtained with
2425  * rtp_session_next_timeout() expired.
2426  *
2427  * This function will perform timeouts of receivers and senders, send a BYE
2428  * packet or generate RTCP packets with current session stats.
2429  *
2430  * This function can call the #RTPSessionSendRTCP callback, possibly multiple
2431  * times, for each packet that should be processed.
2432  *
2433  * Returns: a #GstFlowReturn.
2434  */
2435 GstFlowReturn
2436 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
2437     guint64 ntpnstime)
2438 {
2439   GstFlowReturn result = GST_FLOW_OK;
2440   GList *item;
2441   ReportData data;
2442   RTPSource *own;
2443
2444   g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2445
2446   GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT,
2447       GST_TIME_ARGS (current_time), GST_TIME_ARGS (ntpnstime));
2448
2449   data.sess = sess;
2450   data.rtcp = NULL;
2451   data.current_time = current_time;
2452   data.ntpnstime = ntpnstime;
2453   data.is_bye = FALSE;
2454   data.has_sdes = FALSE;
2455
2456   own = sess->source;
2457
2458   RTP_SESSION_LOCK (sess);
2459   /* get a new interval, we need this for various cleanups etc */
2460   data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
2461
2462   /* first perform cleanups */
2463   g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
2464       (GHRFunc) session_cleanup, &data);
2465
2466   /* see if we need to generate SR or RR packets */
2467   if (is_rtcp_time (sess, current_time, &data)) {
2468     if (own->received_bye) {
2469       /* generate BYE instead */
2470       GST_DEBUG ("generating BYE message");
2471       session_bye (sess, &data);
2472       sess->sent_bye = TRUE;
2473     } else {
2474       /* loop over all known sources and do something */
2475       g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
2476           (GHFunc) session_report_blocks, &data);
2477     }
2478   }
2479
2480   if (data.rtcp) {
2481     guint size;
2482
2483     /* we keep track of the last report time in order to timeout inactive
2484      * receivers or senders */
2485     sess->last_rtcp_send_time = data.current_time;
2486     sess->first_rtcp = FALSE;
2487
2488     /* add SDES for this source when not already added */
2489     if (!data.has_sdes)
2490       session_sdes (sess, &data);
2491
2492     /* update average RTCP size before sending */
2493     size = GST_BUFFER_SIZE (data.rtcp) + sess->header_len;
2494     UPDATE_AVG (sess->stats.avg_rtcp_packet_size, size);
2495   }
2496
2497   /* check for outdated collisions */
2498   GST_DEBUG ("checking collision list");
2499   item = g_list_first (sess->conflicting_addresses);
2500   while (item) {
2501     RTPConflictingAddress *known_conflict = item->data;
2502     GList *next_item = g_list_next (item);
2503
2504     if (known_conflict->time < current_time - (data.interval *
2505             RTCP_INTERVAL_COLLISION_TIMEOUT)) {
2506       sess->conflicting_addresses =
2507           g_list_delete_link (sess->conflicting_addresses, item);
2508       GST_DEBUG ("collision %p timed out", known_conflict);
2509       g_free (known_conflict);
2510     }
2511     item = next_item;
2512   }
2513
2514   if (sess->change_ssrc) {
2515     GST_DEBUG ("need to change our SSRC (%08x)", own->ssrc);
2516     g_hash_table_steal (sess->ssrcs[sess->mask_idx],
2517         GINT_TO_POINTER (own->ssrc));
2518
2519     own->ssrc = rtp_session_create_new_ssrc (sess);
2520     rtp_source_reset (own);
2521
2522     g_hash_table_insert (sess->ssrcs[sess->mask_idx],
2523         GINT_TO_POINTER (own->ssrc), own);
2524
2525     g_free (sess->bye_reason);
2526     sess->bye_reason = NULL;
2527     sess->sent_bye = FALSE;
2528     sess->change_ssrc = FALSE;
2529     GST_DEBUG ("changed our SSRC to %08x", own->ssrc);
2530   }
2531   RTP_SESSION_UNLOCK (sess);
2532
2533   /* push out the RTCP packet */
2534   if (data.rtcp) {
2535     /* close the RTCP packet */
2536     gst_rtcp_buffer_end (data.rtcp);
2537
2538     GST_DEBUG ("sending packet");
2539     if (sess->callbacks.send_rtcp)
2540       result = sess->callbacks.send_rtcp (sess, own, data.rtcp,
2541           sess->sent_bye, sess->send_rtcp_user_data);
2542     else {
2543       GST_DEBUG ("freeing packet");
2544       gst_buffer_unref (data.rtcp);
2545     }
2546   }
2547
2548   return result;
2549 }