rtpsession: Always keep at least one NACK on early RTCP
[platform/upstream/gst-plugins-good.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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
21  * with newer GLib versions (>= 2.31.0) */
22 #define GLIB_DISABLE_DEPRECATION_WARNINGS
23
24 #include <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/rtp/gstrtcpbuffer.h>
28
29 #include <gst/glib-compat-private.h>
30
31 #include "rtpsession.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtp_session_debug);
34 #define GST_CAT_DEFAULT rtp_session_debug
35
36 /* signals and args */
37 enum
38 {
39   SIGNAL_GET_SOURCE_BY_SSRC,
40   SIGNAL_ON_NEW_SSRC,
41   SIGNAL_ON_SSRC_COLLISION,
42   SIGNAL_ON_SSRC_VALIDATED,
43   SIGNAL_ON_SSRC_ACTIVE,
44   SIGNAL_ON_SSRC_SDES,
45   SIGNAL_ON_BYE_SSRC,
46   SIGNAL_ON_BYE_TIMEOUT,
47   SIGNAL_ON_TIMEOUT,
48   SIGNAL_ON_SENDER_TIMEOUT,
49   SIGNAL_ON_SENDING_RTCP,
50   SIGNAL_ON_APP_RTCP,
51   SIGNAL_ON_FEEDBACK_RTCP,
52   SIGNAL_SEND_RTCP,
53   SIGNAL_SEND_RTCP_FULL,
54   SIGNAL_ON_RECEIVING_RTCP,
55   SIGNAL_ON_NEW_SENDER_SSRC,
56   SIGNAL_ON_SENDER_SSRC_ACTIVE,
57   SIGNAL_ON_SENDING_NACKS,
58   LAST_SIGNAL
59 };
60
61 #define DEFAULT_INTERNAL_SOURCE      NULL
62 #define DEFAULT_BANDWIDTH            0.0
63 #define DEFAULT_RTCP_FRACTION        RTP_STATS_RTCP_FRACTION
64 #define DEFAULT_RTCP_RR_BANDWIDTH    -1
65 #define DEFAULT_RTCP_RS_BANDWIDTH    -1
66 #define DEFAULT_RTCP_MTU             1400
67 #define DEFAULT_SDES                 NULL
68 #define DEFAULT_NUM_SOURCES          0
69 #define DEFAULT_NUM_ACTIVE_SOURCES   0
70 #define DEFAULT_SOURCES              NULL
71 #define DEFAULT_RTCP_MIN_INTERVAL    (RTP_STATS_MIN_INTERVAL * GST_SECOND)
72 #define DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW (2 * GST_SECOND)
73 #define DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD (3)
74 #define DEFAULT_PROBATION            RTP_DEFAULT_PROBATION
75 #define DEFAULT_MAX_DROPOUT_TIME     60000
76 #define DEFAULT_MAX_MISORDER_TIME    2000
77 #define DEFAULT_RTP_PROFILE          GST_RTP_PROFILE_AVP
78 #define DEFAULT_RTCP_REDUCED_SIZE    FALSE
79 #define DEFAULT_RTCP_DISABLE_SR_TIMESTAMP FALSE
80
81 enum
82 {
83   PROP_0,
84   PROP_INTERNAL_SSRC,
85   PROP_INTERNAL_SOURCE,
86   PROP_BANDWIDTH,
87   PROP_RTCP_FRACTION,
88   PROP_RTCP_RR_BANDWIDTH,
89   PROP_RTCP_RS_BANDWIDTH,
90   PROP_RTCP_MTU,
91   PROP_SDES,
92   PROP_NUM_SOURCES,
93   PROP_NUM_ACTIVE_SOURCES,
94   PROP_SOURCES,
95   PROP_FAVOR_NEW,
96   PROP_RTCP_MIN_INTERVAL,
97   PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
98   PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
99   PROP_PROBATION,
100   PROP_MAX_DROPOUT_TIME,
101   PROP_MAX_MISORDER_TIME,
102   PROP_STATS,
103   PROP_RTP_PROFILE,
104   PROP_RTCP_REDUCED_SIZE,
105   PROP_RTCP_DISABLE_SR_TIMESTAMP
106 };
107
108 /* update average packet size */
109 #define INIT_AVG(avg, val) \
110    (avg) = (val);
111 #define UPDATE_AVG(avg, val)            \
112   if ((avg) == 0)                       \
113    (avg) = (val);                       \
114   else                                  \
115    (avg) = ((val) + (15 * (avg))) >> 4;
116
117
118 /* GObject vmethods */
119 static void rtp_session_finalize (GObject * object);
120 static void rtp_session_set_property (GObject * object, guint prop_id,
121     const GValue * value, GParamSpec * pspec);
122 static void rtp_session_get_property (GObject * object, guint prop_id,
123     GValue * value, GParamSpec * pspec);
124
125 static gboolean rtp_session_send_rtcp (RTPSession * sess,
126     GstClockTime max_delay);
127 static gboolean rtp_session_send_rtcp_with_deadline (RTPSession * sess,
128     GstClockTime deadline);
129
130 static guint rtp_session_signals[LAST_SIGNAL] = { 0 };
131
132 G_DEFINE_TYPE (RTPSession, rtp_session, G_TYPE_OBJECT);
133
134 static guint32 rtp_session_create_new_ssrc (RTPSession * sess);
135 static RTPSource *obtain_source (RTPSession * sess, guint32 ssrc,
136     gboolean * created, RTPPacketInfo * pinfo, gboolean rtp);
137 static RTPSource *obtain_internal_source (RTPSession * sess,
138     guint32 ssrc, gboolean * created, GstClockTime current_time);
139 static GstFlowReturn rtp_session_schedule_bye_locked (RTPSession * sess,
140     GstClockTime current_time);
141 static GstClockTime calculate_rtcp_interval (RTPSession * sess,
142     gboolean deterministic, gboolean first);
143
144 static gboolean
145 accumulate_trues (GSignalInvocationHint * ihint, GValue * return_accu,
146     const GValue * handler_return, gpointer data)
147 {
148   if (g_value_get_boolean (handler_return))
149     g_value_set_boolean (return_accu, TRUE);
150
151   return TRUE;
152 }
153
154 static void
155 rtp_session_class_init (RTPSessionClass * klass)
156 {
157   GObjectClass *gobject_class;
158
159   gobject_class = (GObjectClass *) klass;
160
161   gobject_class->finalize = rtp_session_finalize;
162   gobject_class->set_property = rtp_session_set_property;
163   gobject_class->get_property = rtp_session_get_property;
164
165   /**
166    * RTPSession::get-source-by-ssrc:
167    * @session: the object which received the signal
168    * @ssrc: the SSRC of the RTPSource
169    *
170    * Request the #RTPSource object with SSRC @ssrc in @session.
171    */
172   rtp_session_signals[SIGNAL_GET_SOURCE_BY_SSRC] =
173       g_signal_new ("get-source-by-ssrc", G_TYPE_FROM_CLASS (klass),
174       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (RTPSessionClass,
175           get_source_by_ssrc), NULL, NULL, g_cclosure_marshal_generic,
176       RTP_TYPE_SOURCE, 1, G_TYPE_UINT);
177
178   /**
179    * RTPSession::on-new-ssrc:
180    * @session: the object which received the signal
181    * @src: the new RTPSource
182    *
183    * Notify of a new SSRC that entered @session.
184    */
185   rtp_session_signals[SIGNAL_ON_NEW_SSRC] =
186       g_signal_new ("on-new-ssrc", G_TYPE_FROM_CLASS (klass),
187       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_ssrc),
188       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
189       RTP_TYPE_SOURCE);
190   /**
191    * RTPSession::on-ssrc-collision:
192    * @session: the object which received the signal
193    * @src: the #RTPSource that caused a collision
194    *
195    * Notify when we have an SSRC collision
196    */
197   rtp_session_signals[SIGNAL_ON_SSRC_COLLISION] =
198       g_signal_new ("on-ssrc-collision", G_TYPE_FROM_CLASS (klass),
199       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_collision),
200       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
201       RTP_TYPE_SOURCE);
202   /**
203    * RTPSession::on-ssrc-validated:
204    * @session: the object which received the signal
205    * @src: the new validated RTPSource
206    *
207    * Notify of a new SSRC that became validated.
208    */
209   rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED] =
210       g_signal_new ("on-ssrc-validated", G_TYPE_FROM_CLASS (klass),
211       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_validated),
212       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
213       RTP_TYPE_SOURCE);
214   /**
215    * RTPSession::on-ssrc-active:
216    * @session: the object which received the signal
217    * @src: the active RTPSource
218    *
219    * Notify of a SSRC that is active, i.e., sending RTCP.
220    */
221   rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE] =
222       g_signal_new ("on-ssrc-active", G_TYPE_FROM_CLASS (klass),
223       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_active),
224       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
225       RTP_TYPE_SOURCE);
226   /**
227    * RTPSession::on-ssrc-sdes:
228    * @session: the object which received the signal
229    * @src: the RTPSource
230    *
231    * Notify that a new SDES was received for SSRC.
232    */
233   rtp_session_signals[SIGNAL_ON_SSRC_SDES] =
234       g_signal_new ("on-ssrc-sdes", G_TYPE_FROM_CLASS (klass),
235       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_ssrc_sdes),
236       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
237       RTP_TYPE_SOURCE);
238   /**
239    * RTPSession::on-bye-ssrc:
240    * @session: the object which received the signal
241    * @src: the RTPSource that went away
242    *
243    * Notify of an SSRC that became inactive because of a BYE packet.
244    */
245   rtp_session_signals[SIGNAL_ON_BYE_SSRC] =
246       g_signal_new ("on-bye-ssrc", G_TYPE_FROM_CLASS (klass),
247       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_ssrc),
248       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
249       RTP_TYPE_SOURCE);
250   /**
251    * RTPSession::on-bye-timeout:
252    * @session: the object which received the signal
253    * @src: the RTPSource that timed out
254    *
255    * Notify of an SSRC that has timed out because of BYE
256    */
257   rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT] =
258       g_signal_new ("on-bye-timeout", G_TYPE_FROM_CLASS (klass),
259       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_bye_timeout),
260       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
261       RTP_TYPE_SOURCE);
262   /**
263    * RTPSession::on-timeout:
264    * @session: the object which received the signal
265    * @src: the RTPSource that timed out
266    *
267    * Notify of an SSRC that has timed out
268    */
269   rtp_session_signals[SIGNAL_ON_TIMEOUT] =
270       g_signal_new ("on-timeout", G_TYPE_FROM_CLASS (klass),
271       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_timeout),
272       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
273       RTP_TYPE_SOURCE);
274   /**
275    * RTPSession::on-sender-timeout:
276    * @session: the object which received the signal
277    * @src: the RTPSource that timed out
278    *
279    * Notify of an SSRC that was a sender but timed out and became a receiver.
280    */
281   rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT] =
282       g_signal_new ("on-sender-timeout", G_TYPE_FROM_CLASS (klass),
283       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sender_timeout),
284       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
285       RTP_TYPE_SOURCE);
286
287   /**
288    * RTPSession::on-sending-rtcp
289    * @session: the object which received the signal
290    * @buffer: the #GstBuffer containing the RTCP packet about to be sent
291    * @early: %TRUE if the packet is early, %FALSE if it is regular
292    *
293    * This signal is emitted before sending an RTCP packet, it can be used
294    * to add extra RTCP Packets.
295    *
296    * Returns: %TRUE if the RTCP buffer should NOT be suppressed, %FALSE
297    * if suppressing it is acceptable
298    */
299   rtp_session_signals[SIGNAL_ON_SENDING_RTCP] =
300       g_signal_new ("on-sending-rtcp", G_TYPE_FROM_CLASS (klass),
301       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_rtcp),
302       accumulate_trues, NULL, g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2,
303       GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_BOOLEAN);
304
305   /**
306    * RTPSession::on-app-rtcp:
307    * @session: the object which received the signal
308    * @subtype: The subtype of the packet
309    * @ssrc: The SSRC/CSRC of the packet
310    * @name: The name of the packet
311    * @data: a #GstBuffer with the application-dependant data or %NULL if
312    * there was no data
313    *
314    * Notify that a RTCP APP packet has been received
315    */
316   rtp_session_signals[SIGNAL_ON_APP_RTCP] =
317       g_signal_new ("on-app-rtcp", G_TYPE_FROM_CLASS (klass),
318       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_app_rtcp),
319       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 4,
320       G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING, GST_TYPE_BUFFER);
321
322   /**
323    * RTPSession::on-feedback-rtcp:
324    * @session: the object which received the signal
325    * @type: Type of RTCP packet, will be %GST_RTCP_TYPE_RTPFB or
326    *  %GST_RTCP_TYPE_RTPFB
327    * @fbtype: The type of RTCP FB packet, probably part of #GstRTCPFBType
328    * @sender_ssrc: The SSRC of the sender
329    * @media_ssrc: The SSRC of the media this refers to
330    * @fci: a #GstBuffer with the FCI data from the FB packet or %NULL if
331    * there was no FCI
332    *
333    * Notify that a RTCP feedback packet has been received
334    */
335   rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP] =
336       g_signal_new ("on-feedback-rtcp", G_TYPE_FROM_CLASS (klass),
337       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_feedback_rtcp),
338       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 5, G_TYPE_UINT,
339       G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, GST_TYPE_BUFFER);
340
341   /**
342    * RTPSession::send-rtcp:
343    * @session: the object which received the signal
344    * @max_delay: The maximum delay after which the feedback will not be useful
345    *  anymore
346    *
347    * Requests that the #RTPSession initiate a new RTCP packet as soon as
348    * possible within the requested delay.
349    *
350    * This sets feedback to %TRUE if not already done before.
351    */
352   rtp_session_signals[SIGNAL_SEND_RTCP] =
353       g_signal_new ("send-rtcp", G_TYPE_FROM_CLASS (klass),
354       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
355       G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
356       g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_UINT64);
357
358   /**
359    * RTPSession::send-rtcp-full:
360    * @session: the object which received the signal
361    * @max_delay: The maximum delay after which the feedback will not be useful
362    *  anymore
363    *
364    * Requests that the #RTPSession initiate a new RTCP packet as soon as
365    * possible within the requested delay.
366    *
367    * This sets feedback to %TRUE if not already done before.
368    *
369    * Returns: TRUE if the new RTCP packet could be scheduled within the
370    * requested delay, FALSE otherwise.
371    *
372    * Since: 1.6
373    */
374   rtp_session_signals[SIGNAL_SEND_RTCP_FULL] =
375       g_signal_new ("send-rtcp-full", G_TYPE_FROM_CLASS (klass),
376       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
377       G_STRUCT_OFFSET (RTPSessionClass, send_rtcp), NULL, NULL,
378       g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 1, G_TYPE_UINT64);
379
380   /**
381    * RTPSession::on-receiving-rtcp
382    * @session: the object which received the signal
383    * @buffer: the #GstBuffer containing the RTCP packet that was received
384    *
385    * This signal is emitted when receiving an RTCP packet before it is handled
386    * by the session. It can be used to extract custom information from RTCP packets.
387    *
388    * Since: 1.6
389    */
390   rtp_session_signals[SIGNAL_ON_RECEIVING_RTCP] =
391       g_signal_new ("on-receiving-rtcp", G_TYPE_FROM_CLASS (klass),
392       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_receiving_rtcp),
393       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
394       GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
395
396   /**
397    * RTPSession::on-new-sender-ssrc:
398    * @session: the object which received the signal
399    * @src: the new sender RTPSource
400    *
401    * Notify of a new sender SSRC that entered @session.
402    *
403    * Since: 1.8
404    */
405   rtp_session_signals[SIGNAL_ON_NEW_SENDER_SSRC] =
406       g_signal_new ("on-new-sender-ssrc", G_TYPE_FROM_CLASS (klass),
407       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_new_sender_ssrc),
408       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
409       RTP_TYPE_SOURCE);
410
411   /**
412    * RTPSession::on-sender-ssrc-active:
413    * @session: the object which received the signal
414    * @src: the active sender RTPSource
415    *
416    * Notify of a sender SSRC that is active, i.e., sending RTCP.
417    *
418    * Since: 1.8
419    */
420   rtp_session_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE] =
421       g_signal_new ("on-sender-ssrc-active", G_TYPE_FROM_CLASS (klass),
422       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass,
423           on_sender_ssrc_active), NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
424       G_TYPE_NONE, 1, RTP_TYPE_SOURCE);
425
426   /**
427    * RTPSession::on-sending-nack
428    * @session: the object which received the signal
429    * @sender_ssrc: the sender ssrc
430    * @media_ssrc: the media ssrc
431    * @nacks: (element-type guint16): the list of seqnum to be nacked
432    * @buffer: the #GstBuffer containing the RTCP packet about to be sent
433    *
434    * This signal is emitted before NACK packets are added into the RTCP
435    * packet. This signal can be used to override the conversion of the NACK
436    * seqnum array into packets. This can be used if your protocol uses
437    * different type of NACK (e.g. based on RTCP APP).
438    *
439    * The handler should transform the seqnum from @nacks array into packets.
440    * @nacks seqnum must be consumed from the start. The remaining will be
441    * rescheduled for later base on bandwidth. Only one handler will be
442    * signalled.
443    *
444    * A handler may return 0 to signal that generic NACKs should be created
445    * for this set. This can be useful if the signal is used for other purpose
446    * or if the other type of NACK would use more space.
447    *
448    * Returns: the number of NACK seqnum that was consumed from @nacks.
449    *
450    * Since: 1.16
451    */
452   rtp_session_signals[SIGNAL_ON_SENDING_NACKS] =
453       g_signal_new ("on-sending-nacks", G_TYPE_FROM_CLASS (klass),
454       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (RTPSessionClass, on_sending_nacks),
455       g_signal_accumulator_first_wins, NULL, g_cclosure_marshal_generic,
456       G_TYPE_UINT, 4, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_ARRAY,
457       GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
458
459   g_object_class_install_property (gobject_class, PROP_INTERNAL_SSRC,
460       g_param_spec_uint ("internal-ssrc", "Internal SSRC",
461           "The internal SSRC used for the session (deprecated)",
462           0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
463
464   g_object_class_install_property (gobject_class, PROP_INTERNAL_SOURCE,
465       g_param_spec_object ("internal-source", "Internal Source",
466           "The internal source element of the session (deprecated)",
467           RTP_TYPE_SOURCE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
468
469   g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
470       g_param_spec_double ("bandwidth", "Bandwidth",
471           "The bandwidth of the session in bits per second (0 for auto-discover)",
472           0.0, G_MAXDOUBLE, DEFAULT_BANDWIDTH,
473           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
474
475   g_object_class_install_property (gobject_class, PROP_RTCP_FRACTION,
476       g_param_spec_double ("rtcp-fraction", "RTCP Fraction",
477           "The fraction of the bandwidth used for RTCP in bits per second (or as a real fraction of the RTP bandwidth if < 1)",
478           0.0, G_MAXDOUBLE, DEFAULT_RTCP_FRACTION,
479           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
480
481   g_object_class_install_property (gobject_class, PROP_RTCP_RR_BANDWIDTH,
482       g_param_spec_int ("rtcp-rr-bandwidth", "RTCP RR bandwidth",
483           "The RTCP bandwidth used for receivers in bits per second (-1 = default)",
484           -1, G_MAXINT, DEFAULT_RTCP_RR_BANDWIDTH,
485           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
486
487   g_object_class_install_property (gobject_class, PROP_RTCP_RS_BANDWIDTH,
488       g_param_spec_int ("rtcp-rs-bandwidth", "RTCP RS bandwidth",
489           "The RTCP bandwidth used for senders in bits per second (-1 = default)",
490           -1, G_MAXINT, DEFAULT_RTCP_RS_BANDWIDTH,
491           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
492
493   g_object_class_install_property (gobject_class, PROP_RTCP_MTU,
494       g_param_spec_uint ("rtcp-mtu", "RTCP MTU",
495           "The maximum size of the RTCP packets",
496           16, G_MAXINT16, DEFAULT_RTCP_MTU,
497           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
498
499   g_object_class_install_property (gobject_class, PROP_SDES,
500       g_param_spec_boxed ("sdes", "SDES",
501           "The SDES items of this session",
502           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
503
504   g_object_class_install_property (gobject_class, PROP_NUM_SOURCES,
505       g_param_spec_uint ("num-sources", "Num Sources",
506           "The number of sources in the session", 0, G_MAXUINT,
507           DEFAULT_NUM_SOURCES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
508
509   g_object_class_install_property (gobject_class, PROP_NUM_ACTIVE_SOURCES,
510       g_param_spec_uint ("num-active-sources", "Num Active Sources",
511           "The number of active sources in the session", 0, G_MAXUINT,
512           DEFAULT_NUM_ACTIVE_SOURCES,
513           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
514   /**
515    * RTPSource::sources
516    *
517    * Get a GValue Array of all sources in the session.
518    *
519    * <example>
520    * <title>Getting the #RTPSources of a session
521    * <programlisting>
522    * {
523    *   GValueArray *arr;
524    *   GValue *val;
525    *   guint i;
526    *
527    *   g_object_get (sess, "sources", &arr, NULL);
528    *
529    *   for (i = 0; i < arr->n_values; i++) {
530    *     RTPSource *source;
531    *
532    *     val = g_value_array_get_nth (arr, i);
533    *     source = g_value_get_object (val);
534    *   }
535    *   g_value_array_free (arr);
536    * }
537    * </programlisting>
538    * </example>
539    */
540   g_object_class_install_property (gobject_class, PROP_SOURCES,
541       g_param_spec_boxed ("sources", "Sources",
542           "An array of all known sources in the session",
543           G_TYPE_VALUE_ARRAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
544
545   g_object_class_install_property (gobject_class, PROP_FAVOR_NEW,
546       g_param_spec_boolean ("favor-new", "Favor new sources",
547           "Resolve SSRC conflict in favor of new sources", FALSE,
548           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
549
550   g_object_class_install_property (gobject_class, PROP_RTCP_MIN_INTERVAL,
551       g_param_spec_uint64 ("rtcp-min-interval", "Minimum RTCP interval",
552           "Minimum interval between Regular RTCP packet (in ns)",
553           0, G_MAXUINT64, DEFAULT_RTCP_MIN_INTERVAL,
554           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
555
556   g_object_class_install_property (gobject_class,
557       PROP_RTCP_FEEDBACK_RETENTION_WINDOW,
558       g_param_spec_uint64 ("rtcp-feedback-retention-window",
559           "RTCP Feedback retention window",
560           "Duration during which RTCP Feedback packets are retained (in ns)",
561           0, G_MAXUINT64, DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW,
562           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
563
564   g_object_class_install_property (gobject_class,
565       PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
566       g_param_spec_uint ("rtcp-immediate-feedback-threshold",
567           "RTCP Immediate Feedback threshold",
568           "The maximum number of members of a RTP session for which immediate"
569           " feedback is used (DEPRECATED: has no effect and is not needed)",
570           0, G_MAXUINT, DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD,
571           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
572
573   g_object_class_install_property (gobject_class, PROP_PROBATION,
574       g_param_spec_uint ("probation", "Number of probations",
575           "Consecutive packet sequence numbers to accept the source",
576           0, G_MAXUINT, DEFAULT_PROBATION,
577           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
578
579   g_object_class_install_property (gobject_class, PROP_MAX_DROPOUT_TIME,
580       g_param_spec_uint ("max-dropout-time", "Max dropout time",
581           "The maximum time (milliseconds) of missing packets tolerated.",
582           0, G_MAXUINT, DEFAULT_MAX_DROPOUT_TIME,
583           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
584
585   g_object_class_install_property (gobject_class, PROP_MAX_MISORDER_TIME,
586       g_param_spec_uint ("max-misorder-time", "Max misorder time",
587           "The maximum time (milliseconds) of misordered packets tolerated.",
588           0, G_MAXUINT, DEFAULT_MAX_MISORDER_TIME,
589           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
590
591   /**
592    * RTPSession::stats:
593    *
594    * Various session statistics. This property returns a GstStructure
595    * with name application/x-rtp-session-stats with the following fields:
596    *
597    *  "rtx-drop-count"  G_TYPE_UINT   The number of retransmission events
598    *      dropped (due to bandwidth constraints)
599    *  "sent-nack-count" G_TYPE_UINT   Number of NACKs sent
600    *  "recv-nack-count" G_TYPE_UINT   Number of NACKs received
601    *  "source-stats"    G_TYPE_BOXED  GValueArray of #RTPSource::stats for all
602    *      RTP sources (Since 1.8)
603    *
604    * Since: 1.4
605    */
606   g_object_class_install_property (gobject_class, PROP_STATS,
607       g_param_spec_boxed ("stats", "Statistics",
608           "Various statistics", GST_TYPE_STRUCTURE,
609           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
610
611   g_object_class_install_property (gobject_class, PROP_RTP_PROFILE,
612       g_param_spec_enum ("rtp-profile", "RTP Profile",
613           "RTP profile to use for this session", GST_TYPE_RTP_PROFILE,
614           DEFAULT_RTP_PROFILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
615
616   g_object_class_install_property (gobject_class, PROP_RTCP_REDUCED_SIZE,
617       g_param_spec_boolean ("rtcp-reduced-size", "RTCP Reduced Size",
618           "Use Reduced Size RTCP for feedback packets",
619           DEFAULT_RTCP_REDUCED_SIZE,
620           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
621
622   /**
623    * RTPSession::disable-sr-timestamp:
624    *
625    * Whether sender reports should be timestamped.
626    *
627    * Since: 1.16
628    */
629   g_object_class_install_property (gobject_class,
630       PROP_RTCP_DISABLE_SR_TIMESTAMP,
631       g_param_spec_boolean ("disable-sr-timestamp",
632           "Disable Sender Report Timestamp",
633           "Whether sender reports should be timestamped",
634           DEFAULT_RTCP_DISABLE_SR_TIMESTAMP,
635           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
636
637   klass->get_source_by_ssrc =
638       GST_DEBUG_FUNCPTR (rtp_session_get_source_by_ssrc);
639   klass->send_rtcp = GST_DEBUG_FUNCPTR (rtp_session_send_rtcp);
640
641   GST_DEBUG_CATEGORY_INIT (rtp_session_debug, "rtpsession", 0, "RTP Session");
642 }
643
644 static void
645 rtp_session_init (RTPSession * sess)
646 {
647   gint i;
648   gchar *str;
649
650   g_mutex_init (&sess->lock);
651   sess->key = g_random_int ();
652   sess->mask_idx = 0;
653   sess->mask = 0;
654
655   /* TODO: We currently only use the first hash table but this is the
656    * beginning of an implementation for RFC2762
657    for (i = 0; i < 32; i++) {
658    */
659   for (i = 0; i < 1; i++) {
660     sess->ssrcs[i] =
661         g_hash_table_new_full (NULL, NULL, NULL,
662         (GDestroyNotify) g_object_unref);
663   }
664
665   rtp_stats_init_defaults (&sess->stats);
666   INIT_AVG (sess->stats.avg_rtcp_packet_size, 100);
667   rtp_stats_set_min_interval (&sess->stats,
668       (gdouble) DEFAULT_RTCP_MIN_INTERVAL / GST_SECOND);
669
670   sess->recalc_bandwidth = TRUE;
671   sess->bandwidth = DEFAULT_BANDWIDTH;
672   sess->rtcp_bandwidth = DEFAULT_RTCP_FRACTION;
673   sess->rtcp_rr_bandwidth = DEFAULT_RTCP_RR_BANDWIDTH;
674   sess->rtcp_rs_bandwidth = DEFAULT_RTCP_RS_BANDWIDTH;
675
676   /* default UDP header length */
677   sess->header_len = 28;
678   sess->mtu = DEFAULT_RTCP_MTU;
679
680   sess->probation = DEFAULT_PROBATION;
681   sess->max_dropout_time = DEFAULT_MAX_DROPOUT_TIME;
682   sess->max_misorder_time = DEFAULT_MAX_MISORDER_TIME;
683
684   /* some default SDES entries */
685   sess->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
686
687   /* we do not want to leak details like the username or hostname here */
688   str = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
689   gst_structure_set (sess->sdes, "cname", G_TYPE_STRING, str, NULL);
690   g_free (str);
691
692 #if 0
693   /* we do not want to leak the user's real name here */
694   str = g_strdup_printf ("Anon%u", g_random_int ());
695   gst_structure_set (sdes, "name", G_TYPE_STRING, str, NULL);
696   g_free (str);
697 #endif
698
699   gst_structure_set (sess->sdes, "tool", G_TYPE_STRING, "GStreamer", NULL);
700
701   /* this is the SSRC we suggest */
702   sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
703   sess->internal_ssrc_set = FALSE;
704
705   sess->first_rtcp = TRUE;
706   sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
707   sess->last_rtcp_check_time = GST_CLOCK_TIME_NONE;
708   sess->last_rtcp_send_time = GST_CLOCK_TIME_NONE;
709   sess->last_rtcp_interval = GST_CLOCK_TIME_NONE;
710
711   sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
712   sess->rtcp_feedback_retention_window = DEFAULT_RTCP_FEEDBACK_RETENTION_WINDOW;
713   sess->rtcp_immediate_feedback_threshold =
714       DEFAULT_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD;
715   sess->rtp_profile = DEFAULT_RTP_PROFILE;
716   sess->reduced_size_rtcp = DEFAULT_RTCP_REDUCED_SIZE;
717   sess->timestamp_sender_reports = !DEFAULT_RTCP_DISABLE_SR_TIMESTAMP;
718
719   sess->is_doing_ptp = TRUE;
720 }
721
722 static void
723 rtp_session_finalize (GObject * object)
724 {
725   RTPSession *sess;
726   gint i;
727
728   sess = RTP_SESSION_CAST (object);
729
730   gst_structure_free (sess->sdes);
731
732   g_list_free_full (sess->conflicting_addresses,
733       (GDestroyNotify) rtp_conflicting_address_free);
734
735   /* TODO: Change this again when implementing RFC 2762
736    * for (i = 0; i < 32; i++)
737    */
738   for (i = 0; i < 1; i++)
739     g_hash_table_destroy (sess->ssrcs[i]);
740
741   g_mutex_clear (&sess->lock);
742
743   G_OBJECT_CLASS (rtp_session_parent_class)->finalize (object);
744 }
745
746 static void
747 copy_source (gpointer key, RTPSource * source, GValueArray * arr)
748 {
749   GValue value = { 0 };
750
751   g_value_init (&value, RTP_TYPE_SOURCE);
752   g_value_take_object (&value, source);
753   /* copies the value */
754   g_value_array_append (arr, &value);
755 }
756
757 static GValueArray *
758 rtp_session_create_sources (RTPSession * sess)
759 {
760   GValueArray *res;
761   guint size;
762
763   RTP_SESSION_LOCK (sess);
764   /* get number of elements in the table */
765   size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
766   /* create the result value array */
767   res = g_value_array_new (size);
768
769   /* and copy all values into the array */
770   g_hash_table_foreach (sess->ssrcs[sess->mask_idx], (GHFunc) copy_source, res);
771   RTP_SESSION_UNLOCK (sess);
772
773   return res;
774 }
775
776 static void
777 create_source_stats (gpointer key, RTPSource * source, GValueArray * arr)
778 {
779   GValue *value;
780   GstStructure *s;
781
782   g_object_get (source, "stats", &s, NULL);
783
784   g_value_array_append (arr, NULL);
785   value = g_value_array_get_nth (arr, arr->n_values - 1);
786   g_value_init (value, GST_TYPE_STRUCTURE);
787   g_value_take_boxed (value, s);
788 }
789
790 static GstStructure *
791 rtp_session_create_stats (RTPSession * sess)
792 {
793   GstStructure *s;
794   GValueArray *source_stats;
795   GValue source_stats_v = G_VALUE_INIT;
796   guint size;
797
798   RTP_SESSION_LOCK (sess);
799   s = gst_structure_new ("application/x-rtp-session-stats",
800       "rtx-drop-count", G_TYPE_UINT, sess->stats.nacks_dropped,
801       "sent-nack-count", G_TYPE_UINT, sess->stats.nacks_sent,
802       "recv-nack-count", G_TYPE_UINT, sess->stats.nacks_received, NULL);
803
804   size = g_hash_table_size (sess->ssrcs[sess->mask_idx]);
805   source_stats = g_value_array_new (size);
806   g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
807       (GHFunc) create_source_stats, source_stats);
808   RTP_SESSION_UNLOCK (sess);
809
810   g_value_init (&source_stats_v, G_TYPE_VALUE_ARRAY);
811   g_value_take_boxed (&source_stats_v, source_stats);
812   gst_structure_take_value (s, "source-stats", &source_stats_v);
813
814   return s;
815 }
816
817 static void
818 rtp_session_set_property (GObject * object, guint prop_id,
819     const GValue * value, GParamSpec * pspec)
820 {
821   RTPSession *sess;
822
823   sess = RTP_SESSION (object);
824
825   switch (prop_id) {
826     case PROP_INTERNAL_SSRC:
827       RTP_SESSION_LOCK (sess);
828       sess->suggested_ssrc = g_value_get_uint (value);
829       sess->internal_ssrc_set = TRUE;
830       sess->internal_ssrc_from_caps_or_property = TRUE;
831       RTP_SESSION_UNLOCK (sess);
832       if (sess->callbacks.reconfigure)
833         sess->callbacks.reconfigure (sess, sess->reconfigure_user_data);
834       break;
835     case PROP_BANDWIDTH:
836       RTP_SESSION_LOCK (sess);
837       sess->bandwidth = g_value_get_double (value);
838       sess->recalc_bandwidth = TRUE;
839       RTP_SESSION_UNLOCK (sess);
840       break;
841     case PROP_RTCP_FRACTION:
842       RTP_SESSION_LOCK (sess);
843       sess->rtcp_bandwidth = g_value_get_double (value);
844       sess->recalc_bandwidth = TRUE;
845       RTP_SESSION_UNLOCK (sess);
846       break;
847     case PROP_RTCP_RR_BANDWIDTH:
848       RTP_SESSION_LOCK (sess);
849       sess->rtcp_rr_bandwidth = g_value_get_int (value);
850       sess->recalc_bandwidth = TRUE;
851       RTP_SESSION_UNLOCK (sess);
852       break;
853     case PROP_RTCP_RS_BANDWIDTH:
854       RTP_SESSION_LOCK (sess);
855       sess->rtcp_rs_bandwidth = g_value_get_int (value);
856       sess->recalc_bandwidth = TRUE;
857       RTP_SESSION_UNLOCK (sess);
858       break;
859     case PROP_RTCP_MTU:
860       sess->mtu = g_value_get_uint (value);
861       break;
862     case PROP_SDES:
863       rtp_session_set_sdes_struct (sess, g_value_get_boxed (value));
864       break;
865     case PROP_FAVOR_NEW:
866       sess->favor_new = g_value_get_boolean (value);
867       break;
868     case PROP_RTCP_MIN_INTERVAL:
869       rtp_stats_set_min_interval (&sess->stats,
870           (gdouble) g_value_get_uint64 (value) / GST_SECOND);
871       /* trigger reconsideration */
872       RTP_SESSION_LOCK (sess);
873       sess->next_rtcp_check_time = 0;
874       RTP_SESSION_UNLOCK (sess);
875       if (sess->callbacks.reconsider)
876         sess->callbacks.reconsider (sess, sess->reconsider_user_data);
877       break;
878     case PROP_RTCP_FEEDBACK_RETENTION_WINDOW:
879       sess->rtcp_feedback_retention_window = g_value_get_uint64 (value);
880       break;
881     case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
882       sess->rtcp_immediate_feedback_threshold = g_value_get_uint (value);
883       break;
884     case PROP_PROBATION:
885       sess->probation = g_value_get_uint (value);
886       break;
887     case PROP_MAX_DROPOUT_TIME:
888       sess->max_dropout_time = g_value_get_uint (value);
889       break;
890     case PROP_MAX_MISORDER_TIME:
891       sess->max_misorder_time = g_value_get_uint (value);
892       break;
893     case PROP_RTP_PROFILE:
894       sess->rtp_profile = g_value_get_enum (value);
895       /* trigger reconsideration */
896       RTP_SESSION_LOCK (sess);
897       sess->next_rtcp_check_time = 0;
898       RTP_SESSION_UNLOCK (sess);
899       if (sess->callbacks.reconsider)
900         sess->callbacks.reconsider (sess, sess->reconsider_user_data);
901       break;
902     case PROP_RTCP_REDUCED_SIZE:
903       sess->reduced_size_rtcp = g_value_get_boolean (value);
904       break;
905     case PROP_RTCP_DISABLE_SR_TIMESTAMP:
906       sess->timestamp_sender_reports = !g_value_get_boolean (value);
907       break;
908     default:
909       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
910       break;
911   }
912 }
913
914 static void
915 rtp_session_get_property (GObject * object, guint prop_id,
916     GValue * value, GParamSpec * pspec)
917 {
918   RTPSession *sess;
919
920   sess = RTP_SESSION (object);
921
922   switch (prop_id) {
923     case PROP_INTERNAL_SSRC:
924       g_value_set_uint (value, rtp_session_suggest_ssrc (sess, NULL));
925       break;
926     case PROP_INTERNAL_SOURCE:
927       /* FIXME, return a random source */
928       g_value_set_object (value, NULL);
929       break;
930     case PROP_BANDWIDTH:
931       g_value_set_double (value, sess->bandwidth);
932       break;
933     case PROP_RTCP_FRACTION:
934       g_value_set_double (value, sess->rtcp_bandwidth);
935       break;
936     case PROP_RTCP_RR_BANDWIDTH:
937       g_value_set_int (value, sess->rtcp_rr_bandwidth);
938       break;
939     case PROP_RTCP_RS_BANDWIDTH:
940       g_value_set_int (value, sess->rtcp_rs_bandwidth);
941       break;
942     case PROP_RTCP_MTU:
943       g_value_set_uint (value, sess->mtu);
944       break;
945     case PROP_SDES:
946       g_value_take_boxed (value, rtp_session_get_sdes_struct (sess));
947       break;
948     case PROP_NUM_SOURCES:
949       g_value_set_uint (value, rtp_session_get_num_sources (sess));
950       break;
951     case PROP_NUM_ACTIVE_SOURCES:
952       g_value_set_uint (value, rtp_session_get_num_active_sources (sess));
953       break;
954     case PROP_SOURCES:
955       g_value_take_boxed (value, rtp_session_create_sources (sess));
956       break;
957     case PROP_FAVOR_NEW:
958       g_value_set_boolean (value, sess->favor_new);
959       break;
960     case PROP_RTCP_MIN_INTERVAL:
961       g_value_set_uint64 (value, sess->stats.min_interval * GST_SECOND);
962       break;
963     case PROP_RTCP_FEEDBACK_RETENTION_WINDOW:
964       g_value_set_uint64 (value, sess->rtcp_feedback_retention_window);
965       break;
966     case PROP_RTCP_IMMEDIATE_FEEDBACK_THRESHOLD:
967       g_value_set_uint (value, sess->rtcp_immediate_feedback_threshold);
968       break;
969     case PROP_PROBATION:
970       g_value_set_uint (value, sess->probation);
971       break;
972     case PROP_MAX_DROPOUT_TIME:
973       g_value_set_uint (value, sess->max_dropout_time);
974       break;
975     case PROP_MAX_MISORDER_TIME:
976       g_value_set_uint (value, sess->max_misorder_time);
977       break;
978     case PROP_STATS:
979       g_value_take_boxed (value, rtp_session_create_stats (sess));
980       break;
981     case PROP_RTP_PROFILE:
982       g_value_set_enum (value, sess->rtp_profile);
983       break;
984     case PROP_RTCP_REDUCED_SIZE:
985       g_value_set_boolean (value, sess->reduced_size_rtcp);
986       break;
987     case PROP_RTCP_DISABLE_SR_TIMESTAMP:
988       g_value_set_boolean (value, !sess->timestamp_sender_reports);
989       break;
990     default:
991       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
992       break;
993   }
994 }
995
996 static void
997 on_new_ssrc (RTPSession * sess, RTPSource * source)
998 {
999   g_object_ref (source);
1000   RTP_SESSION_UNLOCK (sess);
1001   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SSRC], 0, source);
1002   RTP_SESSION_LOCK (sess);
1003   g_object_unref (source);
1004 }
1005
1006 static void
1007 on_ssrc_collision (RTPSession * sess, RTPSource * source)
1008 {
1009   g_object_ref (source);
1010   RTP_SESSION_UNLOCK (sess);
1011   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_COLLISION], 0,
1012       source);
1013   RTP_SESSION_LOCK (sess);
1014   g_object_unref (source);
1015 }
1016
1017 static void
1018 on_ssrc_validated (RTPSession * sess, RTPSource * source)
1019 {
1020   g_object_ref (source);
1021   RTP_SESSION_UNLOCK (sess);
1022   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_VALIDATED], 0,
1023       source);
1024   RTP_SESSION_LOCK (sess);
1025   g_object_unref (source);
1026 }
1027
1028 static void
1029 on_ssrc_active (RTPSession * sess, RTPSource * source)
1030 {
1031   g_object_ref (source);
1032   RTP_SESSION_UNLOCK (sess);
1033   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_ACTIVE], 0, source);
1034   RTP_SESSION_LOCK (sess);
1035   g_object_unref (source);
1036 }
1037
1038 static void
1039 on_ssrc_sdes (RTPSession * sess, RTPSource * source)
1040 {
1041   g_object_ref (source);
1042   GST_DEBUG ("SDES changed for SSRC %08x", source->ssrc);
1043   RTP_SESSION_UNLOCK (sess);
1044   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SSRC_SDES], 0, source);
1045   RTP_SESSION_LOCK (sess);
1046   g_object_unref (source);
1047 }
1048
1049 static void
1050 on_bye_ssrc (RTPSession * sess, RTPSource * source)
1051 {
1052   g_object_ref (source);
1053   RTP_SESSION_UNLOCK (sess);
1054   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_SSRC], 0, source);
1055   RTP_SESSION_LOCK (sess);
1056   g_object_unref (source);
1057 }
1058
1059 static void
1060 on_bye_timeout (RTPSession * sess, RTPSource * source)
1061 {
1062   g_object_ref (source);
1063   RTP_SESSION_UNLOCK (sess);
1064   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_BYE_TIMEOUT], 0, source);
1065   RTP_SESSION_LOCK (sess);
1066   g_object_unref (source);
1067 }
1068
1069 static void
1070 on_timeout (RTPSession * sess, RTPSource * source)
1071 {
1072   g_object_ref (source);
1073   RTP_SESSION_UNLOCK (sess);
1074   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_TIMEOUT], 0, source);
1075   RTP_SESSION_LOCK (sess);
1076   g_object_unref (source);
1077 }
1078
1079 static void
1080 on_sender_timeout (RTPSession * sess, RTPSource * source)
1081 {
1082   g_object_ref (source);
1083   RTP_SESSION_UNLOCK (sess);
1084   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_TIMEOUT], 0,
1085       source);
1086   RTP_SESSION_LOCK (sess);
1087   g_object_unref (source);
1088 }
1089
1090 static void
1091 on_new_sender_ssrc (RTPSession * sess, RTPSource * source)
1092 {
1093   g_object_ref (source);
1094   RTP_SESSION_UNLOCK (sess);
1095   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_NEW_SENDER_SSRC], 0,
1096       source);
1097   RTP_SESSION_LOCK (sess);
1098   g_object_unref (source);
1099 }
1100
1101 static void
1102 on_sender_ssrc_active (RTPSession * sess, RTPSource * source)
1103 {
1104   g_object_ref (source);
1105   RTP_SESSION_UNLOCK (sess);
1106   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDER_SSRC_ACTIVE], 0,
1107       source);
1108   RTP_SESSION_LOCK (sess);
1109   g_object_unref (source);
1110 }
1111
1112 /**
1113  * rtp_session_new:
1114  *
1115  * Create a new session object.
1116  *
1117  * Returns: a new #RTPSession. g_object_unref() after usage.
1118  */
1119 RTPSession *
1120 rtp_session_new (void)
1121 {
1122   RTPSession *sess;
1123
1124   sess = g_object_new (RTP_TYPE_SESSION, NULL);
1125
1126   return sess;
1127 }
1128
1129 /**
1130  * rtp_session_reset:
1131  * @sess: an #RTPSession
1132  *
1133  * Reset the sources of @sess.
1134  */
1135 void
1136 rtp_session_reset (RTPSession * sess)
1137 {
1138   g_return_if_fail (RTP_IS_SESSION (sess));
1139
1140   /* remove all sources */
1141   g_hash_table_remove_all (sess->ssrcs[sess->mask_idx]);
1142   sess->total_sources = 0;
1143   sess->stats.sender_sources = 0;
1144   sess->stats.internal_sender_sources = 0;
1145   sess->stats.internal_sources = 0;
1146   sess->stats.active_sources = 0;
1147
1148   sess->generation = 0;
1149   sess->first_rtcp = TRUE;
1150   sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
1151   sess->last_rtcp_check_time = GST_CLOCK_TIME_NONE;
1152   sess->last_rtcp_send_time = GST_CLOCK_TIME_NONE;
1153   sess->last_rtcp_interval = GST_CLOCK_TIME_NONE;
1154   sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
1155   sess->scheduled_bye = FALSE;
1156
1157   /* reset session stats */
1158   sess->stats.bye_members = 0;
1159   sess->stats.nacks_dropped = 0;
1160   sess->stats.nacks_sent = 0;
1161   sess->stats.nacks_received = 0;
1162
1163   sess->is_doing_ptp = TRUE;
1164
1165   g_list_free_full (sess->conflicting_addresses,
1166       (GDestroyNotify) rtp_conflicting_address_free);
1167   sess->conflicting_addresses = NULL;
1168 }
1169
1170 /**
1171  * rtp_session_set_callbacks:
1172  * @sess: an #RTPSession
1173  * @callbacks: callbacks to configure
1174  * @user_data: user data passed in the callbacks
1175  *
1176  * Configure a set of callbacks to be notified of actions.
1177  */
1178 void
1179 rtp_session_set_callbacks (RTPSession * sess, RTPSessionCallbacks * callbacks,
1180     gpointer user_data)
1181 {
1182   g_return_if_fail (RTP_IS_SESSION (sess));
1183
1184   if (callbacks->process_rtp) {
1185     sess->callbacks.process_rtp = callbacks->process_rtp;
1186     sess->process_rtp_user_data = user_data;
1187   }
1188   if (callbacks->send_rtp) {
1189     sess->callbacks.send_rtp = callbacks->send_rtp;
1190     sess->send_rtp_user_data = user_data;
1191   }
1192   if (callbacks->send_rtcp) {
1193     sess->callbacks.send_rtcp = callbacks->send_rtcp;
1194     sess->send_rtcp_user_data = user_data;
1195   }
1196   if (callbacks->sync_rtcp) {
1197     sess->callbacks.sync_rtcp = callbacks->sync_rtcp;
1198     sess->sync_rtcp_user_data = user_data;
1199   }
1200   if (callbacks->clock_rate) {
1201     sess->callbacks.clock_rate = callbacks->clock_rate;
1202     sess->clock_rate_user_data = user_data;
1203   }
1204   if (callbacks->reconsider) {
1205     sess->callbacks.reconsider = callbacks->reconsider;
1206     sess->reconsider_user_data = user_data;
1207   }
1208   if (callbacks->request_key_unit) {
1209     sess->callbacks.request_key_unit = callbacks->request_key_unit;
1210     sess->request_key_unit_user_data = user_data;
1211   }
1212   if (callbacks->request_time) {
1213     sess->callbacks.request_time = callbacks->request_time;
1214     sess->request_time_user_data = user_data;
1215   }
1216   if (callbacks->notify_nack) {
1217     sess->callbacks.notify_nack = callbacks->notify_nack;
1218     sess->notify_nack_user_data = user_data;
1219   }
1220   if (callbacks->reconfigure) {
1221     sess->callbacks.reconfigure = callbacks->reconfigure;
1222     sess->reconfigure_user_data = user_data;
1223   }
1224   if (callbacks->notify_early_rtcp) {
1225     sess->callbacks.notify_early_rtcp = callbacks->notify_early_rtcp;
1226     sess->notify_early_rtcp_user_data = user_data;
1227   }
1228 }
1229
1230 /**
1231  * rtp_session_set_process_rtp_callback:
1232  * @sess: an #RTPSession
1233  * @callback: callback to set
1234  * @user_data: user data passed in the callback
1235  *
1236  * Configure only the process_rtp callback to be notified of the process_rtp action.
1237  */
1238 void
1239 rtp_session_set_process_rtp_callback (RTPSession * sess,
1240     RTPSessionProcessRTP callback, gpointer user_data)
1241 {
1242   g_return_if_fail (RTP_IS_SESSION (sess));
1243
1244   sess->callbacks.process_rtp = callback;
1245   sess->process_rtp_user_data = user_data;
1246 }
1247
1248 /**
1249  * rtp_session_set_send_rtp_callback:
1250  * @sess: an #RTPSession
1251  * @callback: callback to set
1252  * @user_data: user data passed in the callback
1253  *
1254  * Configure only the send_rtp callback to be notified of the send_rtp action.
1255  */
1256 void
1257 rtp_session_set_send_rtp_callback (RTPSession * sess,
1258     RTPSessionSendRTP callback, gpointer user_data)
1259 {
1260   g_return_if_fail (RTP_IS_SESSION (sess));
1261
1262   sess->callbacks.send_rtp = callback;
1263   sess->send_rtp_user_data = user_data;
1264 }
1265
1266 /**
1267  * rtp_session_set_send_rtcp_callback:
1268  * @sess: an #RTPSession
1269  * @callback: callback to set
1270  * @user_data: user data passed in the callback
1271  *
1272  * Configure only the send_rtcp callback to be notified of the send_rtcp action.
1273  */
1274 void
1275 rtp_session_set_send_rtcp_callback (RTPSession * sess,
1276     RTPSessionSendRTCP callback, gpointer user_data)
1277 {
1278   g_return_if_fail (RTP_IS_SESSION (sess));
1279
1280   sess->callbacks.send_rtcp = callback;
1281   sess->send_rtcp_user_data = user_data;
1282 }
1283
1284 /**
1285  * rtp_session_set_sync_rtcp_callback:
1286  * @sess: an #RTPSession
1287  * @callback: callback to set
1288  * @user_data: user data passed in the callback
1289  *
1290  * Configure only the sync_rtcp callback to be notified of the sync_rtcp action.
1291  */
1292 void
1293 rtp_session_set_sync_rtcp_callback (RTPSession * sess,
1294     RTPSessionSyncRTCP callback, gpointer user_data)
1295 {
1296   g_return_if_fail (RTP_IS_SESSION (sess));
1297
1298   sess->callbacks.sync_rtcp = callback;
1299   sess->sync_rtcp_user_data = user_data;
1300 }
1301
1302 /**
1303  * rtp_session_set_clock_rate_callback:
1304  * @sess: an #RTPSession
1305  * @callback: callback to set
1306  * @user_data: user data passed in the callback
1307  *
1308  * Configure only the clock_rate callback to be notified of the clock_rate action.
1309  */
1310 void
1311 rtp_session_set_clock_rate_callback (RTPSession * sess,
1312     RTPSessionClockRate callback, gpointer user_data)
1313 {
1314   g_return_if_fail (RTP_IS_SESSION (sess));
1315
1316   sess->callbacks.clock_rate = callback;
1317   sess->clock_rate_user_data = user_data;
1318 }
1319
1320 /**
1321  * rtp_session_set_reconsider_callback:
1322  * @sess: an #RTPSession
1323  * @callback: callback to set
1324  * @user_data: user data passed in the callback
1325  *
1326  * Configure only the reconsider callback to be notified of the reconsider action.
1327  */
1328 void
1329 rtp_session_set_reconsider_callback (RTPSession * sess,
1330     RTPSessionReconsider callback, gpointer user_data)
1331 {
1332   g_return_if_fail (RTP_IS_SESSION (sess));
1333
1334   sess->callbacks.reconsider = callback;
1335   sess->reconsider_user_data = user_data;
1336 }
1337
1338 /**
1339  * rtp_session_set_request_time_callback:
1340  * @sess: an #RTPSession
1341  * @callback: callback to set
1342  * @user_data: user data passed in the callback
1343  *
1344  * Configure only the request_time callback
1345  */
1346 void
1347 rtp_session_set_request_time_callback (RTPSession * sess,
1348     RTPSessionRequestTime callback, gpointer user_data)
1349 {
1350   g_return_if_fail (RTP_IS_SESSION (sess));
1351
1352   sess->callbacks.request_time = callback;
1353   sess->request_time_user_data = user_data;
1354 }
1355
1356 /**
1357  * rtp_session_set_bandwidth:
1358  * @sess: an #RTPSession
1359  * @bandwidth: the bandwidth allocated
1360  *
1361  * Set the session bandwidth in bits per second.
1362  */
1363 void
1364 rtp_session_set_bandwidth (RTPSession * sess, gdouble bandwidth)
1365 {
1366   g_return_if_fail (RTP_IS_SESSION (sess));
1367
1368   RTP_SESSION_LOCK (sess);
1369   sess->stats.bandwidth = bandwidth;
1370   RTP_SESSION_UNLOCK (sess);
1371 }
1372
1373 /**
1374  * rtp_session_get_bandwidth:
1375  * @sess: an #RTPSession
1376  *
1377  * Get the session bandwidth.
1378  *
1379  * Returns: the session bandwidth.
1380  */
1381 gdouble
1382 rtp_session_get_bandwidth (RTPSession * sess)
1383 {
1384   gdouble result;
1385
1386   g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1387
1388   RTP_SESSION_LOCK (sess);
1389   result = sess->stats.bandwidth;
1390   RTP_SESSION_UNLOCK (sess);
1391
1392   return result;
1393 }
1394
1395 /**
1396  * rtp_session_set_rtcp_fraction:
1397  * @sess: an #RTPSession
1398  * @bandwidth: the RTCP bandwidth
1399  *
1400  * Set the bandwidth in bits per second that should be used for RTCP
1401  * messages.
1402  */
1403 void
1404 rtp_session_set_rtcp_fraction (RTPSession * sess, gdouble bandwidth)
1405 {
1406   g_return_if_fail (RTP_IS_SESSION (sess));
1407
1408   RTP_SESSION_LOCK (sess);
1409   sess->stats.rtcp_bandwidth = bandwidth;
1410   RTP_SESSION_UNLOCK (sess);
1411 }
1412
1413 /**
1414  * rtp_session_get_rtcp_fraction:
1415  * @sess: an #RTPSession
1416  *
1417  * Get the session bandwidth used for RTCP.
1418  *
1419  * Returns: The bandwidth used for RTCP messages.
1420  */
1421 gdouble
1422 rtp_session_get_rtcp_fraction (RTPSession * sess)
1423 {
1424   gdouble result;
1425
1426   g_return_val_if_fail (RTP_IS_SESSION (sess), 0.0);
1427
1428   RTP_SESSION_LOCK (sess);
1429   result = sess->stats.rtcp_bandwidth;
1430   RTP_SESSION_UNLOCK (sess);
1431
1432   return result;
1433 }
1434
1435 /**
1436  * rtp_session_get_sdes_struct:
1437  * @sess: an #RTSPSession
1438  *
1439  * Get the SDES data as a #GstStructure
1440  *
1441  * Returns: a GstStructure with SDES items for @sess. This function returns a
1442  * copy of the SDES structure, use gst_structure_free() after usage.
1443  */
1444 GstStructure *
1445 rtp_session_get_sdes_struct (RTPSession * sess)
1446 {
1447   GstStructure *result = NULL;
1448
1449   g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
1450
1451   RTP_SESSION_LOCK (sess);
1452   if (sess->sdes)
1453     result = gst_structure_copy (sess->sdes);
1454   RTP_SESSION_UNLOCK (sess);
1455
1456   return result;
1457 }
1458
1459 static void
1460 source_set_sdes (const gchar * key, RTPSource * source, GstStructure * sdes)
1461 {
1462   rtp_source_set_sdes_struct (source, gst_structure_copy (sdes));
1463 }
1464
1465 /**
1466  * rtp_session_set_sdes_struct:
1467  * @sess: an #RTSPSession
1468  * @sdes: a #GstStructure
1469  *
1470  * Set the SDES data as a #GstStructure. This function makes a copy of @sdes.
1471  */
1472 void
1473 rtp_session_set_sdes_struct (RTPSession * sess, const GstStructure * sdes)
1474 {
1475   g_return_if_fail (sdes);
1476   g_return_if_fail (RTP_IS_SESSION (sess));
1477
1478   RTP_SESSION_LOCK (sess);
1479   if (sess->sdes)
1480     gst_structure_free (sess->sdes);
1481   sess->sdes = gst_structure_copy (sdes);
1482
1483   g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1484       (GHFunc) source_set_sdes, sess->sdes);
1485   RTP_SESSION_UNLOCK (sess);
1486 }
1487
1488 static GstFlowReturn
1489 source_push_rtp (RTPSource * source, gpointer data, RTPSession * session)
1490 {
1491   GstFlowReturn result = GST_FLOW_OK;
1492
1493   if (source->internal) {
1494     GST_LOG ("source %08x pushed sender RTP packet", source->ssrc);
1495
1496     RTP_SESSION_UNLOCK (session);
1497
1498     if (session->callbacks.send_rtp)
1499       result =
1500           session->callbacks.send_rtp (session, source, data,
1501           session->send_rtp_user_data);
1502     else {
1503       gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
1504     }
1505   } else {
1506     GST_LOG ("source %08x pushed receiver RTP packet", source->ssrc);
1507     RTP_SESSION_UNLOCK (session);
1508
1509     if (session->callbacks.process_rtp)
1510       result =
1511           session->callbacks.process_rtp (session, source,
1512           GST_BUFFER_CAST (data), session->process_rtp_user_data);
1513     else
1514       gst_buffer_unref (GST_BUFFER_CAST (data));
1515   }
1516   RTP_SESSION_LOCK (session);
1517
1518   return result;
1519 }
1520
1521 static gint
1522 source_clock_rate (RTPSource * source, guint8 pt, RTPSession * session)
1523 {
1524   gint result;
1525
1526   RTP_SESSION_UNLOCK (session);
1527
1528   if (session->callbacks.clock_rate)
1529     result =
1530         session->callbacks.clock_rate (session, pt,
1531         session->clock_rate_user_data);
1532   else
1533     result = -1;
1534
1535   RTP_SESSION_LOCK (session);
1536
1537   GST_DEBUG ("got clock-rate %d for pt %d", result, pt);
1538
1539   return result;
1540 }
1541
1542 static RTPSourceCallbacks callbacks = {
1543   (RTPSourcePushRTP) source_push_rtp,
1544   (RTPSourceClockRate) source_clock_rate,
1545 };
1546
1547
1548 /**
1549  * rtp_session_find_conflicting_address:
1550  * @session: The session the packet came in
1551  * @address: address to check for
1552  * @time: The time when the packet that is possibly in conflict arrived
1553  *
1554  * Checks if an address which has a conflict is already known. If it is
1555  * a known conflict, remember the time
1556  *
1557  * Returns: TRUE if it was a known conflict, FALSE otherwise
1558  */
1559 static gboolean
1560 rtp_session_find_conflicting_address (RTPSession * session,
1561     GSocketAddress * address, GstClockTime time)
1562 {
1563   return find_conflicting_address (session->conflicting_addresses, address,
1564       time);
1565 }
1566
1567 /**
1568  * rtp_session_add_conflicting_address:
1569  * @session: The session the packet came in
1570  * @address: address to remember
1571  * @time: The time when the packet that is in conflict arrived
1572  *
1573  * Adds a new conflict address
1574  */
1575 static void
1576 rtp_session_add_conflicting_address (RTPSession * sess,
1577     GSocketAddress * address, GstClockTime time)
1578 {
1579   sess->conflicting_addresses =
1580       add_conflicting_address (sess->conflicting_addresses, address, time);
1581 }
1582
1583
1584 static gboolean
1585 check_collision (RTPSession * sess, RTPSource * source,
1586     RTPPacketInfo * pinfo, gboolean rtp)
1587 {
1588   guint32 ssrc;
1589
1590   /* If we have no pinfo address, we can't do collision checking */
1591   if (!pinfo->address)
1592     return FALSE;
1593
1594   ssrc = rtp_source_get_ssrc (source);
1595
1596   if (!source->internal) {
1597     GSocketAddress *from;
1598
1599     /* This is not our local source, but lets check if two remote
1600      * source collide */
1601     if (rtp) {
1602       from = source->rtp_from;
1603     } else {
1604       from = source->rtcp_from;
1605     }
1606
1607     if (from) {
1608       if (__g_socket_address_equal (from, pinfo->address)) {
1609         /* Address is the same */
1610         return FALSE;
1611       } else {
1612         GST_LOG ("we have a third-party collision or loop ssrc:%x", ssrc);
1613         if (sess->favor_new) {
1614           if (rtp_source_find_conflicting_address (source,
1615                   pinfo->address, pinfo->current_time)) {
1616             gchar *buf1;
1617
1618             buf1 = __g_socket_address_to_string (pinfo->address);
1619             GST_LOG ("Known conflict on %x for %s, dropping packet", ssrc,
1620                 buf1);
1621             g_free (buf1);
1622
1623             return TRUE;
1624           } else {
1625             gchar *buf1, *buf2;
1626
1627             /* Current address is not a known conflict, lets assume this is
1628              * a new source. Save old address in possible conflict list
1629              */
1630             rtp_source_add_conflicting_address (source, from,
1631                 pinfo->current_time);
1632
1633             buf1 = __g_socket_address_to_string (from);
1634             buf2 = __g_socket_address_to_string (pinfo->address);
1635
1636             GST_DEBUG ("New conflict for ssrc %x, replacing %s with %s,"
1637                 " saving old as known conflict", ssrc, buf1, buf2);
1638
1639             if (rtp)
1640               rtp_source_set_rtp_from (source, pinfo->address);
1641             else
1642               rtp_source_set_rtcp_from (source, pinfo->address);
1643
1644             g_free (buf1);
1645             g_free (buf2);
1646
1647             return FALSE;
1648           }
1649         } else {
1650           /* Don't need to save old addresses, we ignore new sources */
1651           return TRUE;
1652         }
1653       }
1654     } else {
1655       /* We don't already have a from address for RTP, just set it */
1656       if (rtp)
1657         rtp_source_set_rtp_from (source, pinfo->address);
1658       else
1659         rtp_source_set_rtcp_from (source, pinfo->address);
1660       return FALSE;
1661     }
1662
1663     /* FIXME: Log 3rd party collision somehow
1664      * Maybe should be done in upper layer, only the SDES can tell us
1665      * if its a collision or a loop
1666      */
1667   } else {
1668     /* This is sending with our ssrc, is it an address we already know */
1669     if (rtp_session_find_conflicting_address (sess, pinfo->address,
1670             pinfo->current_time)) {
1671       /* Its a known conflict, its probably a loop, not a collision
1672        * lets just drop the incoming packet
1673        */
1674       GST_DEBUG ("Our packets are being looped back to us, dropping");
1675     } else {
1676       /* Its a new collision, lets change our SSRC */
1677       rtp_session_add_conflicting_address (sess, pinfo->address,
1678           pinfo->current_time);
1679
1680       GST_DEBUG ("Collision for SSRC %x", ssrc);
1681       /* mark the source BYE */
1682       rtp_source_mark_bye (source, "SSRC Collision");
1683       /* if we were suggesting this SSRC, change to something else */
1684       if (sess->suggested_ssrc == ssrc) {
1685         sess->suggested_ssrc = rtp_session_create_new_ssrc (sess);
1686         sess->internal_ssrc_set = TRUE;
1687       }
1688
1689       on_ssrc_collision (sess, source);
1690
1691       rtp_session_schedule_bye_locked (sess, pinfo->current_time);
1692     }
1693   }
1694
1695   return TRUE;
1696 }
1697
1698 typedef struct
1699 {
1700   gboolean is_doing_ptp;
1701   GSocketAddress *new_addr;
1702 } CompareAddrData;
1703
1704 /* check if the two given ip addr are the same (do not care about the port) */
1705 static gboolean
1706 ip_addr_equal (GSocketAddress * a, GSocketAddress * b)
1707 {
1708   return
1709       g_inet_address_equal (g_inet_socket_address_get_address
1710       (G_INET_SOCKET_ADDRESS (a)),
1711       g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (b)));
1712 }
1713
1714 static void
1715 compare_rtp_source_addr (const gchar * key, RTPSource * source,
1716     CompareAddrData * data)
1717 {
1718   /* only compare ip addr of remote sources which are also not closing */
1719   if (!source->internal && !source->closing && source->rtp_from) {
1720     /* look for the first rtp source */
1721     if (!data->new_addr)
1722       data->new_addr = source->rtp_from;
1723     /* compare current ip addr with the first one */
1724     else
1725       data->is_doing_ptp &= ip_addr_equal (data->new_addr, source->rtp_from);
1726   }
1727 }
1728
1729 static void
1730 compare_rtcp_source_addr (const gchar * key, RTPSource * source,
1731     CompareAddrData * data)
1732 {
1733   /* only compare ip addr of remote sources which are also not closing */
1734   if (!source->internal && !source->closing && source->rtcp_from) {
1735     /* look for the first rtcp source */
1736     if (!data->new_addr)
1737       data->new_addr = source->rtcp_from;
1738     else
1739       /* compare current ip addr with the first one */
1740       data->is_doing_ptp &= ip_addr_equal (data->new_addr, source->rtcp_from);
1741   }
1742 }
1743
1744 /* loop over our non-internal source to know if the session
1745  * is doing point-to-point */
1746 static void
1747 session_update_ptp (RTPSession * sess)
1748 {
1749   /* to know if the session is doing point to point, the ip addr
1750    * of each non-internal (=remotes) source have to be compared
1751    * to each other.
1752    */
1753   gboolean is_doing_rtp_ptp;
1754   gboolean is_doing_rtcp_ptp;
1755   CompareAddrData data;
1756
1757   /* compare the first remote source's ip addr that receive rtp packets
1758    * with other remote rtp source.
1759    * it's enough because the session just needs to know if they are all
1760    * equals or not
1761    */
1762   data.is_doing_ptp = TRUE;
1763   data.new_addr = NULL;
1764   g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1765       (GHFunc) compare_rtp_source_addr, (gpointer) & data);
1766   is_doing_rtp_ptp = data.is_doing_ptp;
1767
1768   /* same but about rtcp */
1769   data.is_doing_ptp = TRUE;
1770   data.new_addr = NULL;
1771   g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
1772       (GHFunc) compare_rtcp_source_addr, (gpointer) & data);
1773   is_doing_rtcp_ptp = data.is_doing_ptp;
1774
1775   /* the session is doing point-to-point if all rtp remote have the same
1776    * ip addr and if all rtcp remote sources have the same ip addr */
1777   sess->is_doing_ptp = is_doing_rtp_ptp && is_doing_rtcp_ptp;
1778
1779   GST_DEBUG ("doing point-to-point: %d", sess->is_doing_ptp);
1780 }
1781
1782 static void
1783 add_source (RTPSession * sess, RTPSource * src)
1784 {
1785   g_hash_table_insert (sess->ssrcs[sess->mask_idx],
1786       GINT_TO_POINTER (src->ssrc), src);
1787   /* report the new source ASAP */
1788   src->generation = sess->generation;
1789   /* we have one more source now */
1790   sess->total_sources++;
1791   if (RTP_SOURCE_IS_ACTIVE (src))
1792     sess->stats.active_sources++;
1793   if (src->internal) {
1794     sess->stats.internal_sources++;
1795     if (!sess->internal_ssrc_from_caps_or_property
1796         && sess->suggested_ssrc != src->ssrc) {
1797       sess->suggested_ssrc = src->ssrc;
1798       sess->internal_ssrc_set = TRUE;
1799     }
1800   }
1801
1802   /* update point-to-point status */
1803   if (!src->internal)
1804     session_update_ptp (sess);
1805 }
1806
1807 static RTPSource *
1808 find_source (RTPSession * sess, guint32 ssrc)
1809 {
1810   return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
1811       GINT_TO_POINTER (ssrc));
1812 }
1813
1814 /* must be called with the session lock, the returned source needs to be
1815  * unreffed after usage. */
1816 static RTPSource *
1817 obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1818     RTPPacketInfo * pinfo, gboolean rtp)
1819 {
1820   RTPSource *source;
1821
1822   source = find_source (sess, ssrc);
1823   if (source == NULL) {
1824     /* make new Source in probation and insert */
1825     source = rtp_source_new (ssrc);
1826
1827     GST_DEBUG ("creating new source %08x %p", ssrc, source);
1828
1829     /* for RTP packets we need to set the source in probation. Receiving RTCP
1830      * packets of an SSRC, on the other hand, is a strong indication that we
1831      * are dealing with a valid source. */
1832     g_object_set (source, "probation", rtp ? sess->probation : 0,
1833         "max-dropout-time", sess->max_dropout_time, "max-misorder-time",
1834         sess->max_misorder_time, NULL);
1835
1836     /* store from address, if any */
1837     if (pinfo->address) {
1838       if (rtp)
1839         rtp_source_set_rtp_from (source, pinfo->address);
1840       else
1841         rtp_source_set_rtcp_from (source, pinfo->address);
1842     }
1843
1844     /* configure a callback on the source */
1845     rtp_source_set_callbacks (source, &callbacks, sess);
1846
1847     add_source (sess, source);
1848     *created = TRUE;
1849   } else {
1850     *created = FALSE;
1851     /* check for collision, this updates the address when not previously set */
1852     if (check_collision (sess, source, pinfo, rtp)) {
1853       return NULL;
1854     }
1855     /* Receiving RTCP packets of an SSRC is a strong indication that we
1856      * are dealing with a valid source. */
1857     if (!rtp)
1858       g_object_set (source, "probation", 0, NULL);
1859   }
1860   /* update last activity */
1861   source->last_activity = pinfo->current_time;
1862   if (rtp)
1863     source->last_rtp_activity = pinfo->current_time;
1864   g_object_ref (source);
1865
1866   return source;
1867 }
1868
1869 /* must be called with the session lock, the returned source needs to be
1870  * unreffed after usage. */
1871 static RTPSource *
1872 obtain_internal_source (RTPSession * sess, guint32 ssrc, gboolean * created,
1873     GstClockTime current_time)
1874 {
1875   RTPSource *source;
1876
1877   source = find_source (sess, ssrc);
1878   if (source == NULL) {
1879     /* make new internal Source and insert */
1880     source = rtp_source_new (ssrc);
1881
1882     GST_DEBUG ("creating new internal source %08x %p", ssrc, source);
1883
1884     source->validated = TRUE;
1885     source->internal = TRUE;
1886     source->probation = FALSE;
1887     rtp_source_set_sdes_struct (source, gst_structure_copy (sess->sdes));
1888     rtp_source_set_callbacks (source, &callbacks, sess);
1889
1890     add_source (sess, source);
1891     *created = TRUE;
1892   } else {
1893     *created = FALSE;
1894   }
1895   /* update last activity */
1896   if (current_time != GST_CLOCK_TIME_NONE) {
1897     source->last_activity = current_time;
1898     source->last_rtp_activity = current_time;
1899   }
1900   g_object_ref (source);
1901
1902   return source;
1903 }
1904
1905 /**
1906  * rtp_session_suggest_ssrc:
1907  * @sess: a #RTPSession
1908  * @is_random: if the suggested ssrc is random
1909  *
1910  * Suggest an unused SSRC in @sess.
1911  *
1912  * Returns: a free unused SSRC
1913  */
1914 guint32
1915 rtp_session_suggest_ssrc (RTPSession * sess, gboolean * is_random)
1916 {
1917   guint32 result;
1918
1919   g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1920
1921   RTP_SESSION_LOCK (sess);
1922   result = sess->suggested_ssrc;
1923   if (is_random)
1924     *is_random = !sess->internal_ssrc_set;
1925   RTP_SESSION_UNLOCK (sess);
1926
1927   return result;
1928 }
1929
1930 /**
1931  * rtp_session_add_source:
1932  * @sess: a #RTPSession
1933  * @src: #RTPSource to add
1934  *
1935  * Add @src to @session.
1936  *
1937  * Returns: %TRUE on success, %FALSE if a source with the same SSRC already
1938  * existed in the session.
1939  */
1940 gboolean
1941 rtp_session_add_source (RTPSession * sess, RTPSource * src)
1942 {
1943   gboolean result = FALSE;
1944   RTPSource *find;
1945
1946   g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1947   g_return_val_if_fail (src != NULL, FALSE);
1948
1949   RTP_SESSION_LOCK (sess);
1950   find = find_source (sess, src->ssrc);
1951   if (find == NULL) {
1952     add_source (sess, src);
1953     result = TRUE;
1954   }
1955   RTP_SESSION_UNLOCK (sess);
1956
1957   return result;
1958 }
1959
1960 /**
1961  * rtp_session_get_num_sources:
1962  * @sess: an #RTPSession
1963  *
1964  * Get the number of sources in @sess.
1965  *
1966  * Returns: The number of sources in @sess.
1967  */
1968 guint
1969 rtp_session_get_num_sources (RTPSession * sess)
1970 {
1971   guint result;
1972
1973   g_return_val_if_fail (RTP_IS_SESSION (sess), FALSE);
1974
1975   RTP_SESSION_LOCK (sess);
1976   result = sess->total_sources;
1977   RTP_SESSION_UNLOCK (sess);
1978
1979   return result;
1980 }
1981
1982 /**
1983  * rtp_session_get_num_active_sources:
1984  * @sess: an #RTPSession
1985  *
1986  * Get the number of active sources in @sess. A source is considered active when
1987  * it has been validated and has not yet received a BYE RTCP message.
1988  *
1989  * Returns: The number of active sources in @sess.
1990  */
1991 guint
1992 rtp_session_get_num_active_sources (RTPSession * sess)
1993 {
1994   guint result;
1995
1996   g_return_val_if_fail (RTP_IS_SESSION (sess), 0);
1997
1998   RTP_SESSION_LOCK (sess);
1999   result = sess->stats.active_sources;
2000   RTP_SESSION_UNLOCK (sess);
2001
2002   return result;
2003 }
2004
2005 /**
2006  * rtp_session_get_source_by_ssrc:
2007  * @sess: an #RTPSession
2008  * @ssrc: an SSRC
2009  *
2010  * Find the source with @ssrc in @sess.
2011  *
2012  * Returns: a #RTPSource with SSRC @ssrc or NULL if the source was not found.
2013  * g_object_unref() after usage.
2014  */
2015 RTPSource *
2016 rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
2017 {
2018   RTPSource *result;
2019
2020   g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
2021
2022   RTP_SESSION_LOCK (sess);
2023   result = find_source (sess, ssrc);
2024   if (result != NULL)
2025     g_object_ref (result);
2026   RTP_SESSION_UNLOCK (sess);
2027
2028   return result;
2029 }
2030
2031 /* should be called with the SESSION lock */
2032 static guint32
2033 rtp_session_create_new_ssrc (RTPSession * sess)
2034 {
2035   guint32 ssrc;
2036
2037   while (TRUE) {
2038     ssrc = g_random_int ();
2039
2040     /* see if it exists in the session, we're done if it doesn't */
2041     if (find_source (sess, ssrc) == NULL)
2042       break;
2043   }
2044   return ssrc;
2045 }
2046
2047 static gboolean
2048 update_packet (GstBuffer ** buffer, guint idx, RTPPacketInfo * pinfo)
2049 {
2050   GstNetAddressMeta *meta;
2051
2052   /* get packet size including header overhead */
2053   pinfo->bytes += gst_buffer_get_size (*buffer) + pinfo->header_len;
2054   pinfo->packets++;
2055
2056   if (pinfo->rtp) {
2057     GstRTPBuffer rtp = { NULL };
2058
2059     if (!gst_rtp_buffer_map (*buffer, GST_MAP_READ, &rtp))
2060       goto invalid_packet;
2061
2062     pinfo->payload_len += gst_rtp_buffer_get_payload_len (&rtp);
2063     if (idx == 0) {
2064       gint i;
2065
2066       /* only keep info for first buffer */
2067       pinfo->ssrc = gst_rtp_buffer_get_ssrc (&rtp);
2068       pinfo->seqnum = gst_rtp_buffer_get_seq (&rtp);
2069       pinfo->pt = gst_rtp_buffer_get_payload_type (&rtp);
2070       pinfo->rtptime = gst_rtp_buffer_get_timestamp (&rtp);
2071       /* copy available csrc */
2072       pinfo->csrc_count = gst_rtp_buffer_get_csrc_count (&rtp);
2073       for (i = 0; i < pinfo->csrc_count; i++)
2074         pinfo->csrcs[i] = gst_rtp_buffer_get_csrc (&rtp, i);
2075     }
2076     gst_rtp_buffer_unmap (&rtp);
2077   }
2078
2079   if (idx == 0) {
2080     /* for netbuffer we can store the IP address to check for collisions */
2081     meta = gst_buffer_get_net_address_meta (*buffer);
2082     if (pinfo->address)
2083       g_object_unref (pinfo->address);
2084     if (meta) {
2085       pinfo->address = G_SOCKET_ADDRESS (g_object_ref (meta->addr));
2086     } else {
2087       pinfo->address = NULL;
2088     }
2089   }
2090   return TRUE;
2091
2092   /* ERRORS */
2093 invalid_packet:
2094   {
2095     GST_DEBUG ("invalid RTP packet received");
2096     return FALSE;
2097   }
2098 }
2099
2100 /* update the RTPPacketInfo structure with the current time and other bits
2101  * about the current buffer we are handling.
2102  * This function is typically called when a validated packet is received.
2103  * This function should be called with the RTP_SESSION_LOCK
2104  */
2105 static gboolean
2106 update_packet_info (RTPSession * sess, RTPPacketInfo * pinfo,
2107     gboolean send, gboolean rtp, gboolean is_list, gpointer data,
2108     GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
2109 {
2110   gboolean res;
2111
2112   pinfo->send = send;
2113   pinfo->rtp = rtp;
2114   pinfo->is_list = is_list;
2115   pinfo->data = data;
2116   pinfo->current_time = current_time;
2117   pinfo->running_time = running_time;
2118   pinfo->ntpnstime = ntpnstime;
2119   pinfo->header_len = sess->header_len;
2120   pinfo->bytes = 0;
2121   pinfo->payload_len = 0;
2122   pinfo->packets = 0;
2123
2124   if (is_list) {
2125     GstBufferList *list = GST_BUFFER_LIST_CAST (data);
2126     res =
2127         gst_buffer_list_foreach (list, (GstBufferListFunc) update_packet,
2128         pinfo);
2129   } else {
2130     GstBuffer *buffer = GST_BUFFER_CAST (data);
2131     res = update_packet (&buffer, 0, pinfo);
2132   }
2133   return res;
2134 }
2135
2136 static void
2137 clean_packet_info (RTPPacketInfo * pinfo)
2138 {
2139   if (pinfo->address)
2140     g_object_unref (pinfo->address);
2141   if (pinfo->data) {
2142     gst_mini_object_unref (pinfo->data);
2143     pinfo->data = NULL;
2144   }
2145 }
2146
2147 static gboolean
2148 source_update_active (RTPSession * sess, RTPSource * source,
2149     gboolean prevactive)
2150 {
2151   gboolean active = RTP_SOURCE_IS_ACTIVE (source);
2152   guint32 ssrc = source->ssrc;
2153
2154   if (prevactive == active)
2155     return FALSE;
2156
2157   if (active) {
2158     sess->stats.active_sources++;
2159     GST_DEBUG ("source: %08x became active, %d active sources", ssrc,
2160         sess->stats.active_sources);
2161   } else {
2162     sess->stats.active_sources--;
2163     GST_DEBUG ("source: %08x became inactive, %d active sources", ssrc,
2164         sess->stats.active_sources);
2165   }
2166   return TRUE;
2167 }
2168
2169 static gboolean
2170 source_update_sender (RTPSession * sess, RTPSource * source,
2171     gboolean prevsender)
2172 {
2173   gboolean sender = RTP_SOURCE_IS_SENDER (source);
2174   guint32 ssrc = source->ssrc;
2175
2176   if (prevsender == sender)
2177     return FALSE;
2178
2179   if (sender) {
2180     sess->stats.sender_sources++;
2181     if (source->internal)
2182       sess->stats.internal_sender_sources++;
2183     GST_DEBUG ("source: %08x became sender, %d sender sources", ssrc,
2184         sess->stats.sender_sources);
2185   } else {
2186     sess->stats.sender_sources--;
2187     if (source->internal)
2188       sess->stats.internal_sender_sources--;
2189     GST_DEBUG ("source: %08x became non sender, %d sender sources", ssrc,
2190         sess->stats.sender_sources);
2191   }
2192   return TRUE;
2193 }
2194
2195 /**
2196  * rtp_session_process_rtp:
2197  * @sess: and #RTPSession
2198  * @buffer: an RTP buffer
2199  * @current_time: the current system time
2200  * @running_time: the running_time of @buffer
2201  *
2202  * Process an RTP buffer in the session manager. This function takes ownership
2203  * of @buffer.
2204  *
2205  * Returns: a #GstFlowReturn.
2206  */
2207 GstFlowReturn
2208 rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
2209     GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
2210 {
2211   GstFlowReturn result;
2212   guint32 ssrc;
2213   RTPSource *source;
2214   gboolean created;
2215   gboolean prevsender, prevactive;
2216   RTPPacketInfo pinfo = { 0, };
2217   guint64 oldrate;
2218
2219   g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2220   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2221
2222   RTP_SESSION_LOCK (sess);
2223
2224   /* update pinfo stats */
2225   if (!update_packet_info (sess, &pinfo, FALSE, TRUE, FALSE, buffer,
2226           current_time, running_time, ntpnstime)) {
2227     GST_DEBUG ("invalid RTP packet received");
2228     RTP_SESSION_UNLOCK (sess);
2229     return rtp_session_process_rtcp (sess, buffer, current_time, running_time,
2230         ntpnstime);
2231   }
2232
2233   ssrc = pinfo.ssrc;
2234
2235   source = obtain_source (sess, ssrc, &created, &pinfo, TRUE);
2236   if (!source)
2237     goto collision;
2238
2239   prevsender = RTP_SOURCE_IS_SENDER (source);
2240   prevactive = RTP_SOURCE_IS_ACTIVE (source);
2241   oldrate = source->bitrate;
2242
2243   if (created)
2244     on_new_ssrc (sess, source);
2245
2246   /* let source process the packet */
2247   result = rtp_source_process_rtp (source, &pinfo);
2248
2249   /* source became active */
2250   if (source_update_active (sess, source, prevactive))
2251     on_ssrc_validated (sess, source);
2252
2253   source_update_sender (sess, source, prevsender);
2254
2255   if (oldrate != source->bitrate)
2256     sess->recalc_bandwidth = TRUE;
2257
2258
2259   if (source->validated) {
2260     gboolean created;
2261     gint i;
2262
2263     /* for validated sources, we add the CSRCs as well */
2264     for (i = 0; i < pinfo.csrc_count; i++) {
2265       guint32 csrc;
2266       RTPSource *csrc_src;
2267
2268       csrc = pinfo.csrcs[i];
2269
2270       /* get source */
2271       csrc_src = obtain_source (sess, csrc, &created, &pinfo, TRUE);
2272       if (!csrc_src)
2273         continue;
2274
2275       if (created) {
2276         GST_DEBUG ("created new CSRC: %08x", csrc);
2277         rtp_source_set_as_csrc (csrc_src);
2278         source_update_active (sess, csrc_src, FALSE);
2279         on_new_ssrc (sess, csrc_src);
2280       }
2281       g_object_unref (csrc_src);
2282     }
2283   }
2284   g_object_unref (source);
2285
2286   RTP_SESSION_UNLOCK (sess);
2287
2288   clean_packet_info (&pinfo);
2289
2290   return result;
2291
2292   /* ERRORS */
2293 collision:
2294   {
2295     RTP_SESSION_UNLOCK (sess);
2296     clean_packet_info (&pinfo);
2297     GST_DEBUG ("ignoring packet because its collisioning");
2298     return GST_FLOW_OK;
2299   }
2300 }
2301
2302 static void
2303 rtp_session_process_rb (RTPSession * sess, RTPSource * source,
2304     GstRTCPPacket * packet, RTPPacketInfo * pinfo)
2305 {
2306   guint count, i;
2307
2308   count = gst_rtcp_packet_get_rb_count (packet);
2309   for (i = 0; i < count; i++) {
2310     guint32 ssrc, exthighestseq, jitter, lsr, dlsr;
2311     guint8 fractionlost;
2312     gint32 packetslost;
2313     RTPSource *src;
2314
2315     gst_rtcp_packet_get_rb (packet, i, &ssrc, &fractionlost,
2316         &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
2317
2318     GST_DEBUG ("RB %d: SSRC %08x, jitter %" G_GUINT32_FORMAT, i, ssrc, jitter);
2319
2320     /* find our own source */
2321     src = find_source (sess, ssrc);
2322     if (src == NULL)
2323       continue;
2324
2325     if (src->internal && RTP_SOURCE_IS_ACTIVE (src)) {
2326       /* only deal with report blocks for our session, we update the stats of
2327        * the sender of the RTCP message. We could also compare our stats against
2328        * the other sender to see if we are better or worse. */
2329       /* FIXME, need to keep track who the RB block is from */
2330       rtp_source_process_rb (source, pinfo->ntpnstime, fractionlost,
2331           packetslost, exthighestseq, jitter, lsr, dlsr);
2332     }
2333   }
2334   on_ssrc_active (sess, source);
2335 }
2336
2337 /* A Sender report contains statistics about how the sender is doing. This
2338  * includes timing informataion such as the relation between RTP and NTP
2339  * timestamps and the number of packets/bytes it sent to us.
2340  *
2341  * In this report is also included a set of report blocks related to how this
2342  * sender is receiving data (in case we (or somebody else) is also sending stuff
2343  * to it). This info includes the packet loss, jitter and seqnum. It also
2344  * contains information to calculate the round trip time (LSR/DLSR).
2345  */
2346 static void
2347 rtp_session_process_sr (RTPSession * sess, GstRTCPPacket * packet,
2348     RTPPacketInfo * pinfo, gboolean * do_sync)
2349 {
2350   guint32 senderssrc, rtptime, packet_count, octet_count;
2351   guint64 ntptime;
2352   RTPSource *source;
2353   gboolean created, prevsender;
2354
2355   gst_rtcp_packet_sr_get_sender_info (packet, &senderssrc, &ntptime, &rtptime,
2356       &packet_count, &octet_count);
2357
2358   GST_DEBUG ("got SR packet: SSRC %08x, time %" GST_TIME_FORMAT,
2359       senderssrc, GST_TIME_ARGS (pinfo->current_time));
2360
2361   source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
2362   if (!source)
2363     return;
2364
2365   /* skip non-bye packets for sources that are marked BYE */
2366   if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2367     goto out;
2368
2369   /* don't try to do lip-sync for sources that sent a BYE */
2370   if (RTP_SOURCE_IS_MARKED_BYE (source))
2371     *do_sync = FALSE;
2372   else
2373     *do_sync = TRUE;
2374
2375   prevsender = RTP_SOURCE_IS_SENDER (source);
2376
2377   /* first update the source */
2378   rtp_source_process_sr (source, pinfo->current_time, ntptime, rtptime,
2379       packet_count, octet_count);
2380
2381   source_update_sender (sess, source, prevsender);
2382
2383   if (created)
2384     on_new_ssrc (sess, source);
2385
2386   rtp_session_process_rb (sess, source, packet, pinfo);
2387
2388 out:
2389   g_object_unref (source);
2390 }
2391
2392 /* A receiver report contains statistics about how a receiver is doing. It
2393  * includes stuff like packet loss, jitter and the seqnum it received last. It
2394  * also contains info to calculate the round trip time.
2395  *
2396  * We are only interested in how the sender of this report is doing wrt to us.
2397  */
2398 static void
2399 rtp_session_process_rr (RTPSession * sess, GstRTCPPacket * packet,
2400     RTPPacketInfo * pinfo)
2401 {
2402   guint32 senderssrc;
2403   RTPSource *source;
2404   gboolean created;
2405
2406   senderssrc = gst_rtcp_packet_rr_get_ssrc (packet);
2407
2408   GST_DEBUG ("got RR packet: SSRC %08x", senderssrc);
2409
2410   source = obtain_source (sess, senderssrc, &created, pinfo, FALSE);
2411   if (!source)
2412     return;
2413
2414   /* skip non-bye packets for sources that are marked BYE */
2415   if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2416     goto out;
2417
2418   if (created)
2419     on_new_ssrc (sess, source);
2420
2421   rtp_session_process_rb (sess, source, packet, pinfo);
2422
2423 out:
2424   g_object_unref (source);
2425 }
2426
2427 /* Get SDES items and store them in the SSRC */
2428 static void
2429 rtp_session_process_sdes (RTPSession * sess, GstRTCPPacket * packet,
2430     RTPPacketInfo * pinfo)
2431 {
2432   guint items, i, j;
2433   gboolean more_items, more_entries;
2434
2435   items = gst_rtcp_packet_sdes_get_item_count (packet);
2436   GST_DEBUG ("got SDES packet with %d items", items);
2437
2438   more_items = gst_rtcp_packet_sdes_first_item (packet);
2439   i = 0;
2440   while (more_items) {
2441     guint32 ssrc;
2442     gboolean changed, created, prevactive;
2443     RTPSource *source;
2444     GstStructure *sdes;
2445
2446     ssrc = gst_rtcp_packet_sdes_get_ssrc (packet);
2447
2448     GST_DEBUG ("item %d, SSRC %08x", i, ssrc);
2449
2450     changed = FALSE;
2451
2452     /* find src, no probation when dealing with RTCP */
2453     source = obtain_source (sess, ssrc, &created, pinfo, FALSE);
2454     if (!source)
2455       return;
2456
2457     /* skip non-bye packets for sources that are marked BYE */
2458     if (sess->scheduled_bye && RTP_SOURCE_IS_MARKED_BYE (source))
2459       goto next;
2460
2461     sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
2462
2463     more_entries = gst_rtcp_packet_sdes_first_entry (packet);
2464     j = 0;
2465     while (more_entries) {
2466       GstRTCPSDESType type;
2467       guint8 len;
2468       guint8 *data;
2469       gchar *name;
2470       gchar *value;
2471
2472       gst_rtcp_packet_sdes_get_entry (packet, &type, &len, &data);
2473
2474       GST_DEBUG ("entry %d, type %d, len %d, data %.*s", j, type, len, len,
2475           data);
2476
2477       if (type == GST_RTCP_SDES_PRIV) {
2478         name = g_strndup ((const gchar *) &data[1], data[0]);
2479         len -= data[0] + 1;
2480         data += data[0] + 1;
2481       } else {
2482         name = g_strdup (gst_rtcp_sdes_type_to_name (type));
2483       }
2484
2485       value = g_strndup ((const gchar *) data, len);
2486
2487       if (g_utf8_validate (value, -1, NULL)) {
2488         gst_structure_set (sdes, name, G_TYPE_STRING, value, NULL);
2489       } else {
2490         GST_WARNING ("ignore SDES field %s with non-utf8 data %s", name, value);
2491       }
2492
2493       g_free (name);
2494       g_free (value);
2495
2496       more_entries = gst_rtcp_packet_sdes_next_entry (packet);
2497       j++;
2498     }
2499
2500     /* takes ownership of sdes */
2501     changed = rtp_source_set_sdes_struct (source, sdes);
2502
2503     prevactive = RTP_SOURCE_IS_ACTIVE (source);
2504     source->validated = TRUE;
2505
2506     if (created)
2507       on_new_ssrc (sess, source);
2508
2509     /* source became active */
2510     if (source_update_active (sess, source, prevactive))
2511       on_ssrc_validated (sess, source);
2512
2513     if (changed)
2514       on_ssrc_sdes (sess, source);
2515
2516   next:
2517     g_object_unref (source);
2518
2519     more_items = gst_rtcp_packet_sdes_next_item (packet);
2520     i++;
2521   }
2522 }
2523
2524 /* BYE is sent when a client leaves the session
2525  */
2526 static void
2527 rtp_session_process_bye (RTPSession * sess, GstRTCPPacket * packet,
2528     RTPPacketInfo * pinfo)
2529 {
2530   guint count, i;
2531   gchar *reason;
2532   gboolean reconsider = FALSE;
2533
2534   reason = gst_rtcp_packet_bye_get_reason (packet);
2535   GST_DEBUG ("got BYE packet (reason: %s)", GST_STR_NULL (reason));
2536
2537   count = gst_rtcp_packet_bye_get_ssrc_count (packet);
2538   for (i = 0; i < count; i++) {
2539     guint32 ssrc;
2540     RTPSource *source;
2541     gboolean prevactive, prevsender;
2542     guint pmembers, members;
2543
2544     ssrc = gst_rtcp_packet_bye_get_nth_ssrc (packet, i);
2545     GST_DEBUG ("SSRC: %08x", ssrc);
2546
2547     /* find src and mark bye, no probation when dealing with RTCP */
2548     source = find_source (sess, ssrc);
2549     if (!source || source->internal) {
2550       GST_DEBUG ("Ignoring suspicious BYE packet (reason: %s)",
2551           !source ? "can't find source" : "has internal source SSRC");
2552       break;
2553     }
2554
2555     /* store time for when we need to time out this source */
2556     source->bye_time = pinfo->current_time;
2557
2558     prevactive = RTP_SOURCE_IS_ACTIVE (source);
2559     prevsender = RTP_SOURCE_IS_SENDER (source);
2560
2561     /* mark the source BYE */
2562     rtp_source_mark_bye (source, reason);
2563
2564     pmembers = sess->stats.active_sources;
2565
2566     source_update_active (sess, source, prevactive);
2567     source_update_sender (sess, source, prevsender);
2568
2569     members = sess->stats.active_sources;
2570
2571     if (!sess->scheduled_bye && members < pmembers) {
2572       /* some members went away since the previous timeout estimate.
2573        * Perform reverse reconsideration but only when we are not scheduling a
2574        * BYE ourselves. */
2575       if (sess->next_rtcp_check_time != GST_CLOCK_TIME_NONE &&
2576           pinfo->current_time < sess->next_rtcp_check_time) {
2577         GstClockTime time_remaining;
2578
2579         /* Scale our next RTCP check time according to the change of numbers
2580          * of members. But only if a) this is the first RTCP, or b) this is not
2581          * a feedback session, or c) this is a feedback session but we schedule
2582          * for every RTCP interval (aka no t-rr-interval set).
2583          *
2584          * FIXME: a) and b) are not great as we will possibly go below Tmin
2585          * for non-feedback profiles and in case of a) below
2586          * Tmin/t-rr-interval in any case.
2587          */
2588         if (sess->last_rtcp_send_time == GST_CLOCK_TIME_NONE ||
2589             !(sess->rtp_profile == GST_RTP_PROFILE_AVPF
2590                 || sess->rtp_profile == GST_RTP_PROFILE_SAVPF) ||
2591             sess->next_rtcp_check_time - sess->last_rtcp_send_time ==
2592             sess->last_rtcp_interval) {
2593           time_remaining = sess->next_rtcp_check_time - pinfo->current_time;
2594           sess->next_rtcp_check_time =
2595               gst_util_uint64_scale (time_remaining, members, pmembers);
2596           sess->next_rtcp_check_time += pinfo->current_time;
2597         }
2598         sess->last_rtcp_interval =
2599             gst_util_uint64_scale (sess->last_rtcp_interval, members, pmembers);
2600
2601         GST_DEBUG ("reverse reconsideration %" GST_TIME_FORMAT,
2602             GST_TIME_ARGS (sess->next_rtcp_check_time));
2603
2604         /* mark pending reconsider. We only want to signal the reconsideration
2605          * once after we handled all the source in the bye packet */
2606         reconsider = TRUE;
2607       }
2608     }
2609
2610     on_bye_ssrc (sess, source);
2611   }
2612   if (reconsider) {
2613     RTP_SESSION_UNLOCK (sess);
2614     /* notify app of reconsideration */
2615     if (sess->callbacks.reconsider)
2616       sess->callbacks.reconsider (sess, sess->reconsider_user_data);
2617     RTP_SESSION_LOCK (sess);
2618   }
2619
2620   g_free (reason);
2621 }
2622
2623 static void
2624 rtp_session_process_app (RTPSession * sess, GstRTCPPacket * packet,
2625     RTPPacketInfo * pinfo)
2626 {
2627   GST_DEBUG ("received APP");
2628
2629   if (g_signal_has_handler_pending (sess,
2630           rtp_session_signals[SIGNAL_ON_APP_RTCP], 0, TRUE)) {
2631     GstBuffer *data_buffer = NULL;
2632     guint16 data_length;
2633     gchar name[5];
2634
2635     data_length = gst_rtcp_packet_app_get_data_length (packet) * 4;
2636     if (data_length > 0) {
2637       guint8 *data = gst_rtcp_packet_app_get_data (packet);
2638       data_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2639           GST_BUFFER_COPY_MEMORY, data - packet->rtcp->map.data, data_length);
2640       GST_BUFFER_PTS (data_buffer) = pinfo->running_time;
2641     }
2642
2643     memcpy (name, gst_rtcp_packet_app_get_name (packet), 4);
2644     name[4] = '\0';
2645
2646     RTP_SESSION_UNLOCK (sess);
2647     g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_APP_RTCP], 0,
2648         gst_rtcp_packet_app_get_subtype (packet),
2649         gst_rtcp_packet_app_get_ssrc (packet), name, data_buffer);
2650     RTP_SESSION_LOCK (sess);
2651
2652     if (data_buffer)
2653       gst_buffer_unref (data_buffer);
2654   }
2655 }
2656
2657 static gboolean
2658 rtp_session_request_local_key_unit (RTPSession * sess, RTPSource * src,
2659     guint32 media_ssrc, gboolean fir, GstClockTime current_time)
2660 {
2661   guint32 round_trip = 0;
2662
2663   rtp_source_get_last_rb (src, NULL, NULL, NULL, NULL, NULL, NULL, &round_trip);
2664
2665   if (src->last_keyframe_request != GST_CLOCK_TIME_NONE && round_trip) {
2666     GstClockTime round_trip_in_ns = gst_util_uint64_scale (round_trip,
2667         GST_SECOND, 65536);
2668
2669     /* Sanity check to avoid always ignoring PLI/FIR if we receive RTCP
2670      * packets with erroneous values resulting in crazy high RTT. */
2671     if (round_trip_in_ns > 5 * GST_SECOND)
2672       round_trip_in_ns = GST_SECOND / 2;
2673
2674     if (current_time - src->last_keyframe_request < 2 * round_trip_in_ns) {
2675       GST_DEBUG ("Ignoring %s request from %X because one was send without one "
2676           "RTT (%" GST_TIME_FORMAT " < %" GST_TIME_FORMAT ")",
2677           fir ? "FIR" : "PLI", rtp_source_get_ssrc (src),
2678           GST_TIME_ARGS (current_time - src->last_keyframe_request),
2679           GST_TIME_ARGS (round_trip_in_ns));
2680       return FALSE;
2681     }
2682   }
2683
2684   src->last_keyframe_request = current_time;
2685
2686   GST_LOG ("received %s request from %X about %X %p(%p)", fir ? "FIR" : "PLI",
2687       rtp_source_get_ssrc (src), media_ssrc, sess->callbacks.process_rtp,
2688       sess->callbacks.request_key_unit);
2689
2690   RTP_SESSION_UNLOCK (sess);
2691   sess->callbacks.request_key_unit (sess, media_ssrc, fir,
2692       sess->request_key_unit_user_data);
2693   RTP_SESSION_LOCK (sess);
2694
2695   return TRUE;
2696 }
2697
2698 static void
2699 rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
2700     guint32 media_ssrc, GstClockTime current_time)
2701 {
2702   RTPSource *src;
2703
2704   if (!sess->callbacks.request_key_unit)
2705     return;
2706
2707   src = find_source (sess, sender_ssrc);
2708   if (src == NULL) {
2709     /* try to find a src with media_ssrc instead */
2710     src = find_source (sess, media_ssrc);
2711     if (src == NULL)
2712       return;
2713   }
2714
2715   rtp_session_request_local_key_unit (sess, src, media_ssrc, FALSE,
2716       current_time);
2717 }
2718
2719 static void
2720 rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
2721     guint32 media_ssrc, guint8 * fci_data, guint fci_length,
2722     GstClockTime current_time)
2723 {
2724   RTPSource *src;
2725   guint32 ssrc;
2726   guint position = 0;
2727   gboolean our_request = FALSE;
2728
2729   if (!sess->callbacks.request_key_unit)
2730     return;
2731
2732   if (fci_length < 8)
2733     return;
2734
2735   src = find_source (sess, sender_ssrc);
2736
2737   /* Hack because Google fails to set the sender_ssrc correctly */
2738   if (!src && sender_ssrc == 1) {
2739     GHashTableIter iter;
2740
2741     /* we can't find the source if there are multiple */
2742     if (sess->stats.sender_sources > sess->stats.internal_sender_sources + 1)
2743       return;
2744
2745     g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
2746     while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
2747       if (!src->internal && rtp_source_is_sender (src))
2748         break;
2749       src = NULL;
2750     }
2751   }
2752   if (!src)
2753     return;
2754
2755   for (position = 0; position < fci_length; position += 8) {
2756     guint8 *data = fci_data + position;
2757     RTPSource *own;
2758
2759     ssrc = GST_READ_UINT32_BE (data);
2760
2761     own = find_source (sess, ssrc);
2762     if (own == NULL)
2763       continue;
2764
2765     if (own->internal) {
2766       our_request = TRUE;
2767       break;
2768     }
2769   }
2770   if (!our_request)
2771     return;
2772
2773   rtp_session_request_local_key_unit (sess, src, media_ssrc, TRUE,
2774       current_time);
2775 }
2776
2777 static void
2778 rtp_session_process_nack (RTPSession * sess, guint32 sender_ssrc,
2779     guint32 media_ssrc, guint8 * fci_data, guint fci_length,
2780     GstClockTime current_time)
2781 {
2782   sess->stats.nacks_received++;
2783
2784   if (!sess->callbacks.notify_nack)
2785     return;
2786
2787   while (fci_length > 0) {
2788     guint16 seqnum, blp;
2789
2790     seqnum = GST_READ_UINT16_BE (fci_data);
2791     blp = GST_READ_UINT16_BE (fci_data + 2);
2792
2793     GST_DEBUG ("NACK #%u, blp %04x, SSRC 0x%08x", seqnum, blp, media_ssrc);
2794
2795     RTP_SESSION_UNLOCK (sess);
2796     sess->callbacks.notify_nack (sess, seqnum, blp, media_ssrc,
2797         sess->notify_nack_user_data);
2798     RTP_SESSION_LOCK (sess);
2799
2800     fci_data += 4;
2801     fci_length -= 4;
2802   }
2803 }
2804
2805 static void
2806 rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
2807     RTPPacketInfo * pinfo, GstClockTime current_time)
2808 {
2809   GstRTCPType type;
2810   GstRTCPFBType fbtype;
2811   guint32 sender_ssrc, media_ssrc;
2812   guint8 *fci_data;
2813   guint fci_length;
2814   RTPSource *src;
2815
2816   /* The feedback packet must include both sender SSRC and media SSRC */
2817   if (packet->length < 2)
2818     return;
2819
2820   type = gst_rtcp_packet_get_type (packet);
2821   fbtype = gst_rtcp_packet_fb_get_type (packet);
2822   sender_ssrc = gst_rtcp_packet_fb_get_sender_ssrc (packet);
2823   media_ssrc = gst_rtcp_packet_fb_get_media_ssrc (packet);
2824
2825   src = find_source (sess, media_ssrc);
2826
2827   /* skip non-bye packets for sources that are marked BYE */
2828   if (sess->scheduled_bye && src && RTP_SOURCE_IS_MARKED_BYE (src))
2829     return;
2830
2831   if (src)
2832     g_object_ref (src);
2833
2834   fci_data = gst_rtcp_packet_fb_get_fci (packet);
2835   fci_length = gst_rtcp_packet_fb_get_fci_length (packet) * sizeof (guint32);
2836
2837   GST_DEBUG ("received feedback %d:%d from %08X about %08X with FCI of "
2838       "length %d", type, fbtype, sender_ssrc, media_ssrc, fci_length);
2839
2840   if (g_signal_has_handler_pending (sess,
2841           rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0, TRUE)) {
2842     GstBuffer *fci_buffer = NULL;
2843
2844     if (fci_length > 0) {
2845       fci_buffer = gst_buffer_copy_region (packet->rtcp->buffer,
2846           GST_BUFFER_COPY_MEMORY, fci_data - packet->rtcp->map.data,
2847           fci_length);
2848       GST_BUFFER_PTS (fci_buffer) = pinfo->running_time;
2849     }
2850
2851     RTP_SESSION_UNLOCK (sess);
2852     g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_FEEDBACK_RTCP], 0,
2853         type, fbtype, sender_ssrc, media_ssrc, fci_buffer);
2854     RTP_SESSION_LOCK (sess);
2855
2856     if (fci_buffer)
2857       gst_buffer_unref (fci_buffer);
2858   }
2859
2860   if (src && sess->rtcp_feedback_retention_window != GST_CLOCK_TIME_NONE) {
2861     rtp_source_retain_rtcp_packet (src, packet, pinfo->running_time);
2862   }
2863
2864   if ((src && src->internal) ||
2865       /* PSFB FIR puts the media ssrc inside the FCI */
2866       (type == GST_RTCP_TYPE_PSFB && fbtype == GST_RTCP_PSFB_TYPE_FIR)) {
2867     switch (type) {
2868       case GST_RTCP_TYPE_PSFB:
2869         switch (fbtype) {
2870           case GST_RTCP_PSFB_TYPE_PLI:
2871             if (src)
2872               src->stats.recv_pli_count++;
2873             rtp_session_process_pli (sess, sender_ssrc, media_ssrc,
2874                 current_time);
2875             break;
2876           case GST_RTCP_PSFB_TYPE_FIR:
2877             if (src)
2878               src->stats.recv_fir_count++;
2879             rtp_session_process_fir (sess, sender_ssrc, media_ssrc, fci_data,
2880                 fci_length, current_time);
2881             break;
2882           default:
2883             break;
2884         }
2885         break;
2886       case GST_RTCP_TYPE_RTPFB:
2887         switch (fbtype) {
2888           case GST_RTCP_RTPFB_TYPE_NACK:
2889             if (src)
2890               src->stats.recv_nack_count++;
2891             rtp_session_process_nack (sess, sender_ssrc, media_ssrc,
2892                 fci_data, fci_length, current_time);
2893             break;
2894           default:
2895             break;
2896         }
2897       default:
2898         break;
2899     }
2900   }
2901
2902   if (src)
2903     g_object_unref (src);
2904 }
2905
2906 /**
2907  * rtp_session_process_rtcp:
2908  * @sess: and #RTPSession
2909  * @buffer: an RTCP buffer
2910  * @current_time: the current system time
2911  * @ntpnstime: the current NTP time in nanoseconds
2912  *
2913  * Process an RTCP buffer in the session manager. This function takes ownership
2914  * of @buffer.
2915  *
2916  * Returns: a #GstFlowReturn.
2917  */
2918 GstFlowReturn
2919 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
2920     GstClockTime current_time, GstClockTime running_time, guint64 ntpnstime)
2921 {
2922   GstRTCPPacket packet;
2923   gboolean more, is_bye = FALSE, do_sync = FALSE;
2924   RTPPacketInfo pinfo = { 0, };
2925   GstFlowReturn result = GST_FLOW_OK;
2926   GstRTCPBuffer rtcp = { NULL, };
2927
2928   g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
2929   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2930
2931   if (!gst_rtcp_buffer_validate_reduced (buffer))
2932     goto invalid_packet;
2933
2934   GST_DEBUG ("received RTCP packet");
2935
2936   g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_RECEIVING_RTCP], 0,
2937       buffer);
2938
2939   RTP_SESSION_LOCK (sess);
2940   /* update pinfo stats */
2941   update_packet_info (sess, &pinfo, FALSE, FALSE, FALSE, buffer, current_time,
2942       running_time, ntpnstime);
2943
2944   /* start processing the compound packet */
2945   gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2946   more = gst_rtcp_buffer_get_first_packet (&rtcp, &packet);
2947   while (more) {
2948     GstRTCPType type;
2949
2950     type = gst_rtcp_packet_get_type (&packet);
2951
2952     switch (type) {
2953       case GST_RTCP_TYPE_SR:
2954         rtp_session_process_sr (sess, &packet, &pinfo, &do_sync);
2955         break;
2956       case GST_RTCP_TYPE_RR:
2957         rtp_session_process_rr (sess, &packet, &pinfo);
2958         break;
2959       case GST_RTCP_TYPE_SDES:
2960         rtp_session_process_sdes (sess, &packet, &pinfo);
2961         break;
2962       case GST_RTCP_TYPE_BYE:
2963         is_bye = TRUE;
2964         /* don't try to attempt lip-sync anymore for streams with a BYE */
2965         do_sync = FALSE;
2966         rtp_session_process_bye (sess, &packet, &pinfo);
2967         break;
2968       case GST_RTCP_TYPE_APP:
2969         rtp_session_process_app (sess, &packet, &pinfo);
2970         break;
2971       case GST_RTCP_TYPE_RTPFB:
2972       case GST_RTCP_TYPE_PSFB:
2973         rtp_session_process_feedback (sess, &packet, &pinfo, current_time);
2974         break;
2975       case GST_RTCP_TYPE_XR:
2976         /* FIXME: This block is added to downgrade warning level.
2977          * Once the parser is implemented, it should be replaced with
2978          * a proper process function. */
2979         GST_DEBUG ("got RTCP XR packet, but ignored");
2980         break;
2981       default:
2982         GST_WARNING ("got unknown RTCP packet type: %d", type);
2983         break;
2984     }
2985     more = gst_rtcp_packet_move_to_next (&packet);
2986   }
2987
2988   gst_rtcp_buffer_unmap (&rtcp);
2989
2990   /* if we are scheduling a BYE, we only want to count bye packets, else we
2991    * count everything */
2992   if (sess->scheduled_bye && is_bye) {
2993     sess->bye_stats.bye_members++;
2994     UPDATE_AVG (sess->bye_stats.avg_rtcp_packet_size, pinfo.bytes);
2995   }
2996
2997   /* keep track of average packet size */
2998   UPDATE_AVG (sess->stats.avg_rtcp_packet_size, pinfo.bytes);
2999
3000   GST_DEBUG ("%p, received RTCP packet, avg size %u, %u", &sess->stats,
3001       sess->stats.avg_rtcp_packet_size, pinfo.bytes);
3002   RTP_SESSION_UNLOCK (sess);
3003
3004   pinfo.data = NULL;
3005   clean_packet_info (&pinfo);
3006
3007   /* notify caller of sr packets in the callback */
3008   if (do_sync && sess->callbacks.sync_rtcp) {
3009     result = sess->callbacks.sync_rtcp (sess, buffer,
3010         sess->sync_rtcp_user_data);
3011   } else
3012     gst_buffer_unref (buffer);
3013
3014   return result;
3015
3016   /* ERRORS */
3017 invalid_packet:
3018   {
3019     GST_DEBUG ("invalid RTCP packet received");
3020     gst_buffer_unref (buffer);
3021     return GST_FLOW_OK;
3022   }
3023 }
3024
3025 /**
3026  * rtp_session_update_send_caps:
3027  * @sess: an #RTPSession
3028  * @caps: a #GstCaps
3029  *
3030  * Update the caps of the sender in the rtp session.
3031  */
3032 void
3033 rtp_session_update_send_caps (RTPSession * sess, GstCaps * caps)
3034 {
3035   GstStructure *s;
3036   guint ssrc;
3037
3038   g_return_if_fail (RTP_IS_SESSION (sess));
3039   g_return_if_fail (GST_IS_CAPS (caps));
3040
3041   GST_LOG ("received caps %" GST_PTR_FORMAT, caps);
3042
3043   s = gst_caps_get_structure (caps, 0);
3044
3045   if (gst_structure_get_uint (s, "ssrc", &ssrc)) {
3046     RTPSource *source;
3047     gboolean created;
3048
3049     RTP_SESSION_LOCK (sess);
3050     source = obtain_internal_source (sess, ssrc, &created, GST_CLOCK_TIME_NONE);
3051     sess->suggested_ssrc = ssrc;
3052     sess->internal_ssrc_set = TRUE;
3053     sess->internal_ssrc_from_caps_or_property = TRUE;
3054     if (source) {
3055       rtp_source_update_caps (source, caps);
3056
3057       if (created)
3058         on_new_sender_ssrc (sess, source);
3059
3060       g_object_unref (source);
3061     }
3062
3063     if (gst_structure_get_uint (s, "rtx-ssrc", &ssrc)) {
3064       source =
3065           obtain_internal_source (sess, ssrc, &created, GST_CLOCK_TIME_NONE);
3066       if (source) {
3067         rtp_source_update_caps (source, caps);
3068
3069         if (created)
3070           on_new_sender_ssrc (sess, source);
3071
3072         g_object_unref (source);
3073       }
3074     }
3075     RTP_SESSION_UNLOCK (sess);
3076   } else {
3077     sess->internal_ssrc_from_caps_or_property = FALSE;
3078   }
3079 }
3080
3081 /**
3082  * rtp_session_send_rtp:
3083  * @sess: an #RTPSession
3084  * @data: pointer to either an RTP buffer or a list of RTP buffers
3085  * @is_list: TRUE when @data is a buffer list
3086  * @current_time: the current system time
3087  * @running_time: the running time of @data
3088  *
3089  * Send the RTP data (a buffer or buffer list) in the session manager. This
3090  * function takes ownership of @data.
3091  *
3092  * Returns: a #GstFlowReturn.
3093  */
3094 GstFlowReturn
3095 rtp_session_send_rtp (RTPSession * sess, gpointer data, gboolean is_list,
3096     GstClockTime current_time, GstClockTime running_time)
3097 {
3098   GstFlowReturn result;
3099   RTPSource *source;
3100   gboolean prevsender;
3101   guint64 oldrate;
3102   RTPPacketInfo pinfo = { 0, };
3103   gboolean created;
3104
3105   g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3106   g_return_val_if_fail (is_list || GST_IS_BUFFER (data), GST_FLOW_ERROR);
3107
3108   GST_LOG ("received RTP %s for sending", is_list ? "list" : "packet");
3109
3110   RTP_SESSION_LOCK (sess);
3111   if (!update_packet_info (sess, &pinfo, TRUE, TRUE, is_list, data,
3112           current_time, running_time, -1))
3113     goto invalid_packet;
3114
3115   source = obtain_internal_source (sess, pinfo.ssrc, &created, current_time);
3116   if (created)
3117     on_new_sender_ssrc (sess, source);
3118
3119   if (!source->internal)
3120     /* FIXME: Send GstRTPCollision upstream  */
3121     goto collision;
3122
3123   prevsender = RTP_SOURCE_IS_SENDER (source);
3124   oldrate = source->bitrate;
3125
3126   /* we use our own source to send */
3127   result = rtp_source_send_rtp (source, &pinfo);
3128
3129   source_update_sender (sess, source, prevsender);
3130
3131   if (oldrate != source->bitrate)
3132     sess->recalc_bandwidth = TRUE;
3133   RTP_SESSION_UNLOCK (sess);
3134
3135   g_object_unref (source);
3136   clean_packet_info (&pinfo);
3137
3138   return result;
3139
3140 invalid_packet:
3141   {
3142     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3143     RTP_SESSION_UNLOCK (sess);
3144     GST_DEBUG ("invalid RTP packet received");
3145     return GST_FLOW_OK;
3146   }
3147 collision:
3148   {
3149     g_object_unref (source);
3150     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3151     RTP_SESSION_UNLOCK (sess);
3152     GST_WARNING ("non-internal source with same ssrc %08x, drop packet",
3153         pinfo.ssrc);
3154     return GST_FLOW_OK;
3155   }
3156 }
3157
3158 static void
3159 add_bitrates (gpointer key, RTPSource * source, gdouble * bandwidth)
3160 {
3161   *bandwidth += source->bitrate;
3162 }
3163
3164 /* must be called with session lock */
3165 static GstClockTime
3166 calculate_rtcp_interval (RTPSession * sess, gboolean deterministic,
3167     gboolean first)
3168 {
3169   GstClockTime result;
3170   RTPSessionStats *stats;
3171
3172   /* recalculate bandwidth when it changed */
3173   if (sess->recalc_bandwidth) {
3174     gdouble bandwidth;
3175
3176     if (sess->bandwidth > 0)
3177       bandwidth = sess->bandwidth;
3178     else {
3179       /* If it is <= 0, then try to estimate the actual bandwidth */
3180       bandwidth = 0;
3181
3182       g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3183           (GHFunc) add_bitrates, &bandwidth);
3184     }
3185     if (bandwidth < RTP_STATS_BANDWIDTH)
3186       bandwidth = RTP_STATS_BANDWIDTH;
3187
3188     rtp_stats_set_bandwidths (&sess->stats, bandwidth,
3189         sess->rtcp_bandwidth, sess->rtcp_rs_bandwidth, sess->rtcp_rr_bandwidth);
3190
3191     sess->recalc_bandwidth = FALSE;
3192   }
3193
3194   if (sess->scheduled_bye) {
3195     stats = &sess->bye_stats;
3196     result = rtp_stats_calculate_bye_interval (stats);
3197   } else {
3198     session_update_ptp (sess);
3199
3200     stats = &sess->stats;
3201     result = rtp_stats_calculate_rtcp_interval (stats,
3202         stats->internal_sender_sources > 0, sess->rtp_profile,
3203         sess->is_doing_ptp, first);
3204   }
3205
3206   GST_DEBUG ("next deterministic interval: %" GST_TIME_FORMAT ", first %d",
3207       GST_TIME_ARGS (result), first);
3208
3209   if (!deterministic && result != GST_CLOCK_TIME_NONE)
3210     result = rtp_stats_add_rtcp_jitter (stats, result);
3211
3212   GST_DEBUG ("next interval: %" GST_TIME_FORMAT, GST_TIME_ARGS (result));
3213
3214   return result;
3215 }
3216
3217 static void
3218 source_mark_bye (const gchar * key, RTPSource * source, const gchar * reason)
3219 {
3220   if (source->internal)
3221     rtp_source_mark_bye (source, reason);
3222 }
3223
3224 /**
3225  * rtp_session_mark_all_bye:
3226  * @sess: an #RTPSession
3227  * @reason: a reason
3228  *
3229  * Mark all internal sources of the session as BYE with @reason.
3230  */
3231 void
3232 rtp_session_mark_all_bye (RTPSession * sess, const gchar * reason)
3233 {
3234   g_return_if_fail (RTP_IS_SESSION (sess));
3235
3236   RTP_SESSION_LOCK (sess);
3237   g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3238       (GHFunc) source_mark_bye, (gpointer) reason);
3239   RTP_SESSION_UNLOCK (sess);
3240 }
3241
3242 /* Stop the current @sess and schedule a BYE message for the other members.
3243  * One must have the session lock to call this function
3244  */
3245 static GstFlowReturn
3246 rtp_session_schedule_bye_locked (RTPSession * sess, GstClockTime current_time)
3247 {
3248   GstFlowReturn result = GST_FLOW_OK;
3249   GstClockTime interval;
3250
3251   /* nothing to do it we already scheduled bye */
3252   if (sess->scheduled_bye)
3253     goto done;
3254
3255   /* we schedule BYE now */
3256   sess->scheduled_bye = TRUE;
3257   /* at least one member wants to send a BYE */
3258   memcpy (&sess->bye_stats, &sess->stats, sizeof (RTPSessionStats));
3259   INIT_AVG (sess->bye_stats.avg_rtcp_packet_size, 100);
3260   sess->bye_stats.bye_members = 1;
3261   sess->first_rtcp = TRUE;
3262
3263   /* reschedule transmission */
3264   sess->last_rtcp_send_time = current_time;
3265   sess->last_rtcp_check_time = current_time;
3266   interval = calculate_rtcp_interval (sess, FALSE, TRUE);
3267
3268   if (interval != GST_CLOCK_TIME_NONE)
3269     sess->next_rtcp_check_time = current_time + interval;
3270   else
3271     sess->next_rtcp_check_time = GST_CLOCK_TIME_NONE;
3272   sess->last_rtcp_interval = interval;
3273
3274   GST_DEBUG ("Schedule BYE for %" GST_TIME_FORMAT ", %" GST_TIME_FORMAT,
3275       GST_TIME_ARGS (interval), GST_TIME_ARGS (sess->next_rtcp_check_time));
3276
3277   RTP_SESSION_UNLOCK (sess);
3278   /* notify app of reconsideration */
3279   if (sess->callbacks.reconsider)
3280     sess->callbacks.reconsider (sess, sess->reconsider_user_data);
3281   RTP_SESSION_LOCK (sess);
3282 done:
3283
3284   return result;
3285 }
3286
3287 /**
3288  * rtp_session_schedule_bye:
3289  * @sess: an #RTPSession
3290  * @current_time: the current system time
3291  *
3292  * Schedule a BYE message for all sources marked as BYE in @sess.
3293  *
3294  * Returns: a #GstFlowReturn.
3295  */
3296 GstFlowReturn
3297 rtp_session_schedule_bye (RTPSession * sess, GstClockTime current_time)
3298 {
3299   GstFlowReturn result;
3300
3301   g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
3302
3303   RTP_SESSION_LOCK (sess);
3304   result = rtp_session_schedule_bye_locked (sess, current_time);
3305   RTP_SESSION_UNLOCK (sess);
3306
3307   return result;
3308 }
3309
3310 /**
3311  * rtp_session_next_timeout:
3312  * @sess: an #RTPSession
3313  * @current_time: the current system time
3314  *
3315  * Get the next time we should perform session maintenance tasks.
3316  *
3317  * Returns: a time when rtp_session_on_timeout() should be called with the
3318  * current system time.
3319  */
3320 GstClockTime
3321 rtp_session_next_timeout (RTPSession * sess, GstClockTime current_time)
3322 {
3323   GstClockTime result, interval = 0;
3324
3325   g_return_val_if_fail (RTP_IS_SESSION (sess), GST_CLOCK_TIME_NONE);
3326
3327   RTP_SESSION_LOCK (sess);
3328
3329   if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
3330     GST_DEBUG ("have early rtcp time");
3331     result = sess->next_early_rtcp_time;
3332     goto early_exit;
3333   }
3334
3335   result = sess->next_rtcp_check_time;
3336
3337   GST_DEBUG ("current time: %" GST_TIME_FORMAT
3338       ", next time: %" GST_TIME_FORMAT,
3339       GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
3340
3341   if (result == GST_CLOCK_TIME_NONE || result < current_time) {
3342     GST_DEBUG ("take current time as base");
3343     /* our previous check time expired, start counting from the current time
3344      * again. */
3345     result = current_time;
3346   }
3347
3348   if (sess->scheduled_bye) {
3349     if (sess->bye_stats.active_sources >= 50) {
3350       GST_DEBUG ("reconsider BYE, more than 50 sources");
3351       /* reconsider BYE if members >= 50 */
3352       interval = calculate_rtcp_interval (sess, FALSE, TRUE);
3353       sess->last_rtcp_interval = interval;
3354     }
3355   } else {
3356     if (sess->first_rtcp) {
3357       GST_DEBUG ("first RTCP packet");
3358       /* we are called for the first time */
3359       interval = calculate_rtcp_interval (sess, FALSE, TRUE);
3360       sess->last_rtcp_interval = interval;
3361     } else if (sess->next_rtcp_check_time < current_time) {
3362       GST_DEBUG ("old check time expired, getting new timeout");
3363       /* get a new timeout when we need to */
3364       interval = calculate_rtcp_interval (sess, FALSE, FALSE);
3365       sess->last_rtcp_interval = interval;
3366
3367       if ((sess->rtp_profile == GST_RTP_PROFILE_AVPF
3368               || sess->rtp_profile == GST_RTP_PROFILE_SAVPF)
3369           && interval != GST_CLOCK_TIME_NONE) {
3370         /* Apply the rules from RFC 4585 section 3.5.3 */
3371         if (sess->stats.min_interval != 0) {
3372           GstClockTime T_rr_current_interval = g_random_double_range (0.5,
3373               1.5) * sess->stats.min_interval * GST_SECOND;
3374
3375           if (T_rr_current_interval > interval) {
3376             GST_DEBUG ("Adjusting interval for t-rr-interval: %" GST_TIME_FORMAT
3377                 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (T_rr_current_interval),
3378                 GST_TIME_ARGS (interval));
3379             interval = T_rr_current_interval;
3380           }
3381         }
3382       }
3383     }
3384   }
3385
3386   if (interval != GST_CLOCK_TIME_NONE)
3387     result += interval;
3388   else
3389     result = GST_CLOCK_TIME_NONE;
3390
3391   sess->next_rtcp_check_time = result;
3392
3393 early_exit:
3394
3395   GST_DEBUG ("current time: %" GST_TIME_FORMAT
3396       ", next time: %" GST_TIME_FORMAT,
3397       GST_TIME_ARGS (current_time), GST_TIME_ARGS (result));
3398   RTP_SESSION_UNLOCK (sess);
3399
3400   return result;
3401 }
3402
3403 typedef struct
3404 {
3405   RTPSource *source;
3406   gboolean is_bye;
3407   GstBuffer *buffer;
3408 } ReportOutput;
3409
3410 typedef struct
3411 {
3412   GstRTCPBuffer rtcpbuf;
3413   RTPSession *sess;
3414   RTPSource *source;
3415   guint num_to_report;
3416   gboolean have_fir;
3417   gboolean have_pli;
3418   gboolean have_nack;
3419   GstBuffer *rtcp;
3420   GstClockTime current_time;
3421   guint64 ntpnstime;
3422   GstClockTime running_time;
3423   GstClockTime interval;
3424   GstRTCPPacket packet;
3425   gboolean has_sdes;
3426   gboolean is_early;
3427   gboolean may_suppress;
3428   GQueue output;
3429   guint nacked_seqnums;
3430 } ReportData;
3431
3432 static void
3433 session_start_rtcp (RTPSession * sess, ReportData * data)
3434 {
3435   GstRTCPPacket *packet = &data->packet;
3436   RTPSource *own = data->source;
3437   GstRTCPBuffer *rtcp = &data->rtcpbuf;
3438
3439   data->rtcp = gst_rtcp_buffer_new (sess->mtu);
3440   data->has_sdes = FALSE;
3441
3442   gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
3443
3444   if (data->is_early && sess->reduced_size_rtcp)
3445     return;
3446
3447   if (RTP_SOURCE_IS_SENDER (own)) {
3448     guint64 ntptime;
3449     guint32 rtptime;
3450     guint32 packet_count, octet_count;
3451
3452     /* we are a sender, create SR */
3453     GST_DEBUG ("create SR for SSRC %08x", own->ssrc);
3454     gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SR, packet);
3455
3456     /* get latest stats */
3457     rtp_source_get_new_sr (own, data->ntpnstime, data->running_time,
3458         &ntptime, &rtptime, &packet_count, &octet_count);
3459     /* store stats */
3460     rtp_source_process_sr (own, data->current_time, ntptime, rtptime,
3461         packet_count, octet_count);
3462
3463     /* fill in sender report info */
3464     gst_rtcp_packet_sr_set_sender_info (packet, own->ssrc,
3465         sess->timestamp_sender_reports ? ntptime : 0,
3466         sess->timestamp_sender_reports ? rtptime : 0,
3467         packet_count, octet_count);
3468   } else {
3469     /* we are only receiver, create RR */
3470     GST_DEBUG ("create RR for SSRC %08x", own->ssrc);
3471     gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RR, packet);
3472     gst_rtcp_packet_rr_set_ssrc (packet, own->ssrc);
3473   }
3474 }
3475
3476 /* construct a Sender or Receiver Report */
3477 static void
3478 session_report_blocks (const gchar * key, RTPSource * source, ReportData * data)
3479 {
3480   RTPSession *sess = data->sess;
3481   GstRTCPPacket *packet = &data->packet;
3482   guint8 fractionlost;
3483   gint32 packetslost;
3484   guint32 exthighestseq, jitter;
3485   guint32 lsr, dlsr;
3486
3487   /* don't report for sources in future generations */
3488   if (((gint16) (source->generation - sess->generation)) > 0) {
3489     GST_DEBUG ("source %08x generation %u > %u", source->ssrc,
3490         source->generation, sess->generation);
3491     return;
3492   }
3493
3494   if (g_hash_table_contains (source->reported_in_sr_of,
3495           GUINT_TO_POINTER (data->source->ssrc))) {
3496     GST_DEBUG ("source %08x already reported in this generation", source->ssrc);
3497     return;
3498   }
3499
3500   if (gst_rtcp_packet_get_rb_count (packet) == GST_RTCP_MAX_RB_COUNT) {
3501     GST_DEBUG ("max RB count reached");
3502     return;
3503   }
3504
3505   /* only report about remote sources */
3506   if (source->internal)
3507     goto reported;
3508
3509   if (!RTP_SOURCE_IS_SENDER (source)) {
3510     GST_DEBUG ("source %08x not sender", source->ssrc);
3511     goto reported;
3512   }
3513
3514   if (source->disable_rtcp) {
3515     GST_DEBUG ("source %08x has RTCP disabled", source->ssrc);
3516     goto reported;
3517   }
3518
3519   GST_DEBUG ("create RB for SSRC %08x", source->ssrc);
3520
3521   /* get new stats */
3522   rtp_source_get_new_rb (source, data->current_time, &fractionlost,
3523       &packetslost, &exthighestseq, &jitter, &lsr, &dlsr);
3524
3525   /* store last generated RR packet */
3526   source->last_rr.is_valid = TRUE;
3527   source->last_rr.fractionlost = fractionlost;
3528   source->last_rr.packetslost = packetslost;
3529   source->last_rr.exthighestseq = exthighestseq;
3530   source->last_rr.jitter = jitter;
3531   source->last_rr.lsr = lsr;
3532   source->last_rr.dlsr = dlsr;
3533
3534   /* packet is not yet filled, add report block for this source. */
3535   gst_rtcp_packet_add_rb (packet, source->ssrc, fractionlost, packetslost,
3536       exthighestseq, jitter, lsr, dlsr);
3537
3538 reported:
3539   g_hash_table_add (source->reported_in_sr_of,
3540       GUINT_TO_POINTER (data->source->ssrc));
3541 }
3542
3543 /* construct FIR */
3544 static void
3545 session_add_fir (const gchar * key, RTPSource * source, ReportData * data)
3546 {
3547   GstRTCPPacket *packet = &data->packet;
3548   guint16 len;
3549   guint8 *fci_data;
3550
3551   if (!source->send_fir)
3552     return;
3553
3554   len = gst_rtcp_packet_fb_get_fci_length (packet);
3555   if (!gst_rtcp_packet_fb_set_fci_length (packet, len + 2))
3556     /* exit because the packet is full, will put next request in a
3557      * further packet */
3558     return;
3559
3560   fci_data = gst_rtcp_packet_fb_get_fci (packet) + (len * 4);
3561
3562   GST_WRITE_UINT32_BE (fci_data, source->ssrc);
3563   fci_data += 4;
3564   fci_data[0] = source->current_send_fir_seqnum;
3565   fci_data[1] = fci_data[2] = fci_data[3] = 0;
3566
3567   source->send_fir = FALSE;
3568   source->stats.sent_fir_count++;
3569 }
3570
3571 static void
3572 session_fir (RTPSession * sess, ReportData * data)
3573 {
3574   GstRTCPBuffer *rtcp = &data->rtcpbuf;
3575   GstRTCPPacket *packet = &data->packet;
3576
3577   if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
3578     return;
3579
3580   gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_FIR);
3581   gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3582   gst_rtcp_packet_fb_set_media_ssrc (packet, 0);
3583
3584   g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
3585       (GHFunc) session_add_fir, data);
3586
3587   if (gst_rtcp_packet_fb_get_fci_length (packet) == 0)
3588     gst_rtcp_packet_remove (packet);
3589   else
3590     data->may_suppress = FALSE;
3591 }
3592
3593 static gboolean
3594 has_pli_compare_func (gconstpointer a, gconstpointer ignored)
3595 {
3596   GstRTCPPacket packet;
3597   GstRTCPBuffer rtcp = { NULL, };
3598   gboolean ret = FALSE;
3599
3600   gst_rtcp_buffer_map ((GstBuffer *) a, GST_MAP_READ, &rtcp);
3601
3602   if (gst_rtcp_buffer_get_first_packet (&rtcp, &packet)) {
3603     if (gst_rtcp_packet_get_type (&packet) == GST_RTCP_TYPE_PSFB &&
3604         gst_rtcp_packet_fb_get_type (&packet) == GST_RTCP_PSFB_TYPE_PLI)
3605       ret = TRUE;
3606   }
3607
3608   gst_rtcp_buffer_unmap (&rtcp);
3609
3610   return ret;
3611 }
3612
3613 /* construct PLI */
3614 static void
3615 session_pli (const gchar * key, RTPSource * source, ReportData * data)
3616 {
3617   GstRTCPBuffer *rtcp = &data->rtcpbuf;
3618   GstRTCPPacket *packet = &data->packet;
3619
3620   if (!source->send_pli)
3621     return;
3622
3623   if (rtp_source_has_retained (source, has_pli_compare_func, NULL))
3624     return;
3625
3626   if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_PSFB, packet))
3627     /* exit because the packet is full, will put next request in a
3628      * further packet */
3629     return;
3630
3631   gst_rtcp_packet_fb_set_type (packet, GST_RTCP_PSFB_TYPE_PLI);
3632   gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3633   gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3634
3635   source->send_pli = FALSE;
3636   data->may_suppress = FALSE;
3637
3638   source->stats.sent_pli_count++;
3639 }
3640
3641 /* construct NACK */
3642 static void
3643 session_nack (const gchar * key, RTPSource * source, ReportData * data)
3644 {
3645   RTPSession *sess = data->sess;
3646   GstRTCPBuffer *rtcp = &data->rtcpbuf;
3647   GstRTCPPacket *packet = &data->packet;
3648   guint16 *nacks;
3649   GstClockTime *nack_deadlines;
3650   guint n_nacks, i = 0;
3651   guint nacked_seqnums = 0;
3652   guint16 n_fb_nacks = 0;
3653   guint8 *fci_data;
3654
3655   if (!source->send_nack)
3656     return;
3657
3658   nacks = rtp_source_get_nacks (source, &n_nacks);
3659   nack_deadlines = rtp_source_get_nack_deadlines (source, NULL);
3660   GST_DEBUG ("%u NACKs current time %" GST_TIME_FORMAT, n_nacks,
3661       GST_TIME_ARGS (data->current_time));
3662
3663   /* cleanup expired nacks */
3664   for (i = 0; i < n_nacks; i++) {
3665     GST_DEBUG ("#%u deadline %" GST_TIME_FORMAT, nacks[i],
3666         GST_TIME_ARGS (nack_deadlines[i]));
3667     if (nack_deadlines[i] >= data->current_time)
3668       break;
3669   }
3670
3671   if (data->is_early) {
3672     /* don't remove them all if this is an early RTCP packet. It may happen
3673      * that the NACKs are late due to high RTT, not sending NACKs at all would
3674      * keep the RTX RTT stats high and maintain a dropping state. */
3675     i = MIN (n_nacks - 1, i);
3676   }
3677
3678   if (i) {
3679     GST_WARNING ("Removing %u expired NACKS", i);
3680     rtp_source_clear_nacks (source, i);
3681     n_nacks -= i;
3682     if (n_nacks == 0)
3683       return;
3684   }
3685
3686   /* allow overriding NACK to packet conversion */
3687   if (g_signal_has_handler_pending (sess,
3688           rtp_session_signals[SIGNAL_ON_SENDING_NACKS], 0, TRUE)) {
3689     /* this is needed as it will actually resize the buffer */
3690     gst_rtcp_buffer_unmap (rtcp);
3691
3692     g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_NACKS], 0,
3693         data->source->ssrc, source->ssrc, source->nacks, data->rtcp,
3694         &nacked_seqnums);
3695
3696     /* and now remap for the remaining work */
3697     gst_rtcp_buffer_map (data->rtcp, GST_MAP_READWRITE, rtcp);
3698
3699     if (nacked_seqnums > 0)
3700       goto done;
3701   }
3702
3703   if (!gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_RTPFB, packet))
3704     /* exit because the packet is full, will put next request in a
3705      * further packet */
3706     return;
3707
3708   gst_rtcp_packet_fb_set_type (packet, GST_RTCP_RTPFB_TYPE_NACK);
3709   gst_rtcp_packet_fb_set_sender_ssrc (packet, data->source->ssrc);
3710   gst_rtcp_packet_fb_set_media_ssrc (packet, source->ssrc);
3711
3712   if (!gst_rtcp_packet_fb_set_fci_length (packet, 1)) {
3713     gst_rtcp_packet_remove (packet);
3714     GST_WARNING ("no nacks fit in the packet");
3715     return;
3716   }
3717
3718   fci_data = gst_rtcp_packet_fb_get_fci (packet);
3719   for (i = 0; i < n_nacks; i = nacked_seqnums) {
3720     guint16 seqnum = nacks[i];
3721     guint16 blp = 0;
3722     guint j;
3723
3724     if (!gst_rtcp_packet_fb_set_fci_length (packet, n_fb_nacks + 1))
3725       break;
3726
3727     n_fb_nacks++;
3728     nacked_seqnums++;
3729
3730     for (j = i + 1; j < n_nacks; j++) {
3731       gint diff;
3732
3733       diff = gst_rtp_buffer_compare_seqnum (seqnum, nacks[j]);
3734       GST_TRACE ("[%u][%u] %u %u diff %i", i, j, seqnum, nacks[j], diff);
3735       if (diff > 16)
3736         break;
3737
3738       blp |= 1 << (diff - 1);
3739       nacked_seqnums++;
3740     }
3741
3742     GST_WRITE_UINT32_BE (fci_data, seqnum << 16 | blp);
3743     fci_data += 4;
3744   }
3745
3746   GST_DEBUG ("Sent %u seqnums into %u FB NACKs", nacked_seqnums, n_fb_nacks);
3747   source->stats.sent_nack_count += n_fb_nacks;
3748
3749 done:
3750   data->nacked_seqnums += nacked_seqnums;
3751   rtp_source_clear_nacks (source, nacked_seqnums);
3752   data->may_suppress = FALSE;
3753 }
3754
3755 /* perform cleanup of sources that timed out */
3756 static void
3757 session_cleanup (const gchar * key, RTPSource * source, ReportData * data)
3758 {
3759   gboolean remove = FALSE;
3760   gboolean byetimeout = FALSE;
3761   gboolean sendertimeout = FALSE;
3762   gboolean is_sender, is_active;
3763   RTPSession *sess = data->sess;
3764   GstClockTime interval, binterval;
3765   GstClockTime btime;
3766
3767   GST_DEBUG ("look at %08x, generation %u", source->ssrc, source->generation);
3768
3769   /* check for outdated collisions */
3770   if (source->internal) {
3771     GST_DEBUG ("Timing out collisions for %x", source->ssrc);
3772     rtp_source_timeout (source, data->current_time, data->running_time,
3773         sess->rtcp_feedback_retention_window);
3774   }
3775
3776   /* nothing else to do when without RTCP */
3777   if (data->interval == GST_CLOCK_TIME_NONE)
3778     return;
3779
3780   is_sender = RTP_SOURCE_IS_SENDER (source);
3781   is_active = RTP_SOURCE_IS_ACTIVE (source);
3782
3783   /* our own rtcp interval may have been forced low by secondary configuration,
3784    * while sender side may still operate with higher interval,
3785    * so do not just take our interval to decide on timing out sender,
3786    * but take (if data->interval <= 5 * GST_SECOND):
3787    *   interval = CLAMP (sender_interval, data->interval, 5 * GST_SECOND)
3788    * where sender_interval is difference between last 2 received RTCP reports
3789    */
3790   if (data->interval >= 5 * GST_SECOND || source->internal) {
3791     binterval = data->interval;
3792   } else {
3793     GST_LOG ("prev_rtcp %" GST_TIME_FORMAT ", last_rtcp %" GST_TIME_FORMAT,
3794         GST_TIME_ARGS (source->stats.prev_rtcptime),
3795         GST_TIME_ARGS (source->stats.last_rtcptime));
3796     /* if not received enough yet, fallback to larger default */
3797     if (source->stats.last_rtcptime > source->stats.prev_rtcptime)
3798       binterval = source->stats.last_rtcptime - source->stats.prev_rtcptime;
3799     else
3800       binterval = 5 * GST_SECOND;
3801     binterval = CLAMP (binterval, data->interval, 5 * GST_SECOND);
3802   }
3803   GST_LOG ("timeout base interval %" GST_TIME_FORMAT,
3804       GST_TIME_ARGS (binterval));
3805
3806   if (!source->internal && source->marked_bye) {
3807     /* if we received a BYE from the source, remove the source after some
3808      * time. */
3809     if (data->current_time > source->bye_time &&
3810         data->current_time - source->bye_time > sess->stats.bye_timeout) {
3811       GST_DEBUG ("removing BYE source %08x", source->ssrc);
3812       remove = TRUE;
3813       byetimeout = TRUE;
3814     }
3815   }
3816
3817   if (source->internal && source->sent_bye) {
3818     GST_DEBUG ("removing internal source that has sent BYE %08x", source->ssrc);
3819     remove = TRUE;
3820   }
3821
3822   /* sources that were inactive for more than 5 times the deterministic reporting
3823    * interval get timed out. the min timeout is 5 seconds. */
3824   /* mind old time that might pre-date last time going to PLAYING */
3825   btime = MAX (source->last_activity, sess->start_time);
3826   if (data->current_time > btime) {
3827     interval = MAX (binterval * 5, 5 * GST_SECOND);
3828     if (data->current_time - btime > interval) {
3829       GST_DEBUG ("removing timeout source %08x, last %" GST_TIME_FORMAT,
3830           source->ssrc, GST_TIME_ARGS (btime));
3831       if (source->internal) {
3832         /* this is an internal source that is not using our suggested ssrc.
3833          * since there must be another source using this ssrc, we can remove
3834          * this one instead of making it a receiver forever */
3835         if (source->ssrc != sess->suggested_ssrc) {
3836           rtp_source_mark_bye (source, "timed out");
3837           /* do not schedule bye here, since we are inside the RTCP timeout
3838            * processing and scheduling bye will interfere with SR/RR sending */
3839         }
3840       } else {
3841         remove = TRUE;
3842       }
3843     }
3844   }
3845
3846   /* senders that did not send for a long time become a receiver, this also
3847    * holds for our own sources. */
3848   if (is_sender) {
3849     /* mind old time that might pre-date last time going to PLAYING */
3850     btime = MAX (source->last_rtp_activity, sess->start_time);
3851     if (data->current_time > btime) {
3852       interval = MAX (binterval * 2, 5 * GST_SECOND);
3853       if (data->current_time - btime > interval) {
3854         GST_DEBUG ("sender source %08x timed out and became receiver, last %"
3855             GST_TIME_FORMAT, source->ssrc, GST_TIME_ARGS (btime));
3856         sendertimeout = TRUE;
3857       }
3858     }
3859   }
3860
3861   if (remove) {
3862     sess->total_sources--;
3863     if (is_sender) {
3864       sess->stats.sender_sources--;
3865       if (source->internal)
3866         sess->stats.internal_sender_sources--;
3867     }
3868     if (is_active)
3869       sess->stats.active_sources--;
3870
3871     if (source->internal)
3872       sess->stats.internal_sources--;
3873
3874     if (byetimeout)
3875       on_bye_timeout (sess, source);
3876     else
3877       on_timeout (sess, source);
3878   } else {
3879     if (sendertimeout) {
3880       source->is_sender = FALSE;
3881       sess->stats.sender_sources--;
3882       if (source->internal)
3883         sess->stats.internal_sender_sources--;
3884
3885       on_sender_timeout (sess, source);
3886     }
3887     /* count how many source to report in this generation */
3888     if (((gint16) (source->generation - sess->generation)) <= 0)
3889       data->num_to_report++;
3890   }
3891   source->closing = remove;
3892 }
3893
3894 static void
3895 session_sdes (RTPSession * sess, ReportData * data)
3896 {
3897   GstRTCPPacket *packet = &data->packet;
3898   const GstStructure *sdes;
3899   gint i, n_fields;
3900   GstRTCPBuffer *rtcp = &data->rtcpbuf;
3901
3902   /* add SDES packet */
3903   gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_SDES, packet);
3904
3905   gst_rtcp_packet_sdes_add_item (packet, data->source->ssrc);
3906
3907   sdes = rtp_source_get_sdes_struct (data->source);
3908
3909   /* add all fields in the structure, the order is not important. */
3910   n_fields = gst_structure_n_fields (sdes);
3911   for (i = 0; i < n_fields; ++i) {
3912     const gchar *field;
3913     const gchar *value;
3914     GstRTCPSDESType type;
3915
3916     field = gst_structure_nth_field_name (sdes, i);
3917     if (field == NULL)
3918       continue;
3919     value = gst_structure_get_string (sdes, field);
3920     if (value == NULL)
3921       continue;
3922     type = gst_rtcp_sdes_name_to_type (field);
3923
3924     /* Early packets are minimal and only include the CNAME */
3925     if (data->is_early && type != GST_RTCP_SDES_CNAME)
3926       continue;
3927
3928     if (type > GST_RTCP_SDES_END && type < GST_RTCP_SDES_PRIV) {
3929       gst_rtcp_packet_sdes_add_entry (packet, type, strlen (value),
3930           (const guint8 *) value);
3931     } else if (type == GST_RTCP_SDES_PRIV) {
3932       gsize prefix_len;
3933       gsize value_len;
3934       gsize data_len;
3935       guint8 data[256];
3936
3937       /* don't accept entries that are too big */
3938       prefix_len = strlen (field);
3939       if (prefix_len > 255)
3940         continue;
3941       value_len = strlen (value);
3942       if (value_len > 255)
3943         continue;
3944       data_len = 1 + prefix_len + value_len;
3945       if (data_len > 255)
3946         continue;
3947
3948       data[0] = prefix_len;
3949       memcpy (&data[1], field, prefix_len);
3950       memcpy (&data[1 + prefix_len], value, value_len);
3951
3952       gst_rtcp_packet_sdes_add_entry (packet, type, data_len, data);
3953     }
3954   }
3955
3956   data->has_sdes = TRUE;
3957 }
3958
3959 /* schedule a BYE packet */
3960 static void
3961 make_source_bye (RTPSession * sess, RTPSource * source, ReportData * data)
3962 {
3963   GstRTCPPacket *packet = &data->packet;
3964   GstRTCPBuffer *rtcp = &data->rtcpbuf;
3965
3966   /* add SDES */
3967   session_sdes (sess, data);
3968   /* add a BYE packet */
3969   gst_rtcp_buffer_add_packet (rtcp, GST_RTCP_TYPE_BYE, packet);
3970   gst_rtcp_packet_bye_add_ssrc (packet, source->ssrc);
3971   if (source->bye_reason)
3972     gst_rtcp_packet_bye_set_reason (packet, source->bye_reason);
3973
3974   /* we have a BYE packet now */
3975   source->sent_bye = TRUE;
3976 }
3977
3978 static gboolean
3979 is_rtcp_time (RTPSession * sess, GstClockTime current_time, ReportData * data)
3980 {
3981   GstClockTime new_send_time;
3982   GstClockTime interval;
3983   RTPSessionStats *stats;
3984
3985   if (sess->scheduled_bye)
3986     stats = &sess->bye_stats;
3987   else
3988     stats = &sess->stats;
3989
3990   if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time))
3991     data->is_early = TRUE;
3992   else
3993     data->is_early = FALSE;
3994
3995   if (data->is_early && sess->next_early_rtcp_time <= current_time) {
3996     GST_DEBUG ("early feedback %" GST_TIME_FORMAT " <= now %"
3997         GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_early_rtcp_time),
3998         GST_TIME_ARGS (current_time));
3999   } else if (sess->next_rtcp_check_time == GST_CLOCK_TIME_NONE ||
4000       sess->next_rtcp_check_time > current_time) {
4001     GST_DEBUG ("no check time yet, next %" GST_TIME_FORMAT " > now %"
4002         GST_TIME_FORMAT, GST_TIME_ARGS (sess->next_rtcp_check_time),
4003         GST_TIME_ARGS (current_time));
4004     return FALSE;
4005   }
4006
4007   /* take interval and add jitter */
4008   interval = data->interval;
4009   if (interval != GST_CLOCK_TIME_NONE)
4010     interval = rtp_stats_add_rtcp_jitter (stats, interval);
4011
4012   if (sess->last_rtcp_check_time != GST_CLOCK_TIME_NONE) {
4013     /* perform forward reconsideration */
4014     if (interval != GST_CLOCK_TIME_NONE) {
4015       GstClockTime elapsed;
4016
4017       /* get elapsed time since we last reported */
4018       elapsed = current_time - sess->last_rtcp_check_time;
4019
4020       GST_DEBUG ("forward reconsideration %" GST_TIME_FORMAT ", elapsed %"
4021           GST_TIME_FORMAT, GST_TIME_ARGS (interval), GST_TIME_ARGS (elapsed));
4022       new_send_time = interval + sess->last_rtcp_check_time;
4023     } else {
4024       new_send_time = sess->last_rtcp_check_time;
4025     }
4026   } else {
4027     /* If this is the first RTCP packet, we can reconsider anything based
4028      * on the last RTCP send time because there was none.
4029      */
4030     g_warn_if_fail (!data->is_early);
4031     data->is_early = FALSE;
4032     new_send_time = current_time;
4033   }
4034
4035   if (!data->is_early) {
4036     /* check if reconsideration */
4037     if (new_send_time == GST_CLOCK_TIME_NONE || current_time < new_send_time) {
4038       GST_DEBUG ("reconsider RTCP for %" GST_TIME_FORMAT,
4039           GST_TIME_ARGS (new_send_time));
4040       /* store new check time */
4041       sess->next_rtcp_check_time = new_send_time;
4042       sess->last_rtcp_interval = interval;
4043       return FALSE;
4044     }
4045
4046     sess->last_rtcp_interval = interval;
4047     if ((sess->rtp_profile == GST_RTP_PROFILE_AVPF
4048             || sess->rtp_profile == GST_RTP_PROFILE_SAVPF)
4049         && interval != GST_CLOCK_TIME_NONE) {
4050       /* Apply the rules from RFC 4585 section 3.5.3 */
4051       if (stats->min_interval != 0 && !sess->first_rtcp) {
4052         GstClockTime T_rr_current_interval =
4053             g_random_double_range (0.5, 1.5) * stats->min_interval * GST_SECOND;
4054
4055         if (T_rr_current_interval > interval) {
4056           GST_DEBUG ("Adjusting interval for t-rr-interval: %" GST_TIME_FORMAT
4057               " > %" GST_TIME_FORMAT, GST_TIME_ARGS (T_rr_current_interval),
4058               GST_TIME_ARGS (interval));
4059           interval = T_rr_current_interval;
4060         }
4061       }
4062     }
4063     sess->next_rtcp_check_time = current_time + interval;
4064   }
4065
4066
4067   GST_DEBUG ("can send RTCP now, next %" GST_TIME_FORMAT,
4068       GST_TIME_ARGS (sess->next_rtcp_check_time));
4069
4070   return TRUE;
4071 }
4072
4073 static void
4074 clone_ssrcs_hashtable (gchar * key, RTPSource * source, GHashTable * hash_table)
4075 {
4076   g_hash_table_insert (hash_table, key, g_object_ref (source));
4077 }
4078
4079 static gboolean
4080 remove_closing_sources (const gchar * key, RTPSource * source,
4081     ReportData * data)
4082 {
4083   if (source->closing)
4084     return TRUE;
4085
4086   if (source->send_fir)
4087     data->have_fir = TRUE;
4088   if (source->send_pli)
4089     data->have_pli = TRUE;
4090   if (source->send_nack)
4091     data->have_nack = TRUE;
4092
4093   return FALSE;
4094 }
4095
4096 static void
4097 generate_rtcp (const gchar * key, RTPSource * source, ReportData * data)
4098 {
4099   RTPSession *sess = data->sess;
4100   gboolean is_bye = FALSE;
4101   ReportOutput *output;
4102
4103   /* only generate RTCP for active internal sources */
4104   if (!source->internal || source->sent_bye)
4105     return;
4106
4107   /* ignore other sources when we do the timeout after a scheduled BYE */
4108   if (sess->scheduled_bye && !source->marked_bye)
4109     return;
4110
4111   /* skip if RTCP is disabled */
4112   if (source->disable_rtcp) {
4113     GST_DEBUG ("source %08x has RTCP disabled", source->ssrc);
4114     return;
4115   }
4116
4117   data->source = source;
4118
4119   /* open packet */
4120   session_start_rtcp (sess, data);
4121
4122   if (source->marked_bye) {
4123     /* send BYE */
4124     make_source_bye (sess, source, data);
4125     is_bye = TRUE;
4126   } else if (!data->is_early) {
4127     /* loop over all known sources and add report blocks. If we are early, we
4128      * just make a minimal RTCP packet and skip this step */
4129     g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4130         (GHFunc) session_report_blocks, data);
4131   }
4132   if (!data->has_sdes && (!data->is_early || !sess->reduced_size_rtcp))
4133     session_sdes (sess, data);
4134
4135   if (data->have_fir)
4136     session_fir (sess, data);
4137
4138   if (data->have_pli)
4139     g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4140         (GHFunc) session_pli, data);
4141
4142   if (data->have_nack)
4143     g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4144         (GHFunc) session_nack, data);
4145
4146   gst_rtcp_buffer_unmap (&data->rtcpbuf);
4147
4148   output = g_slice_new (ReportOutput);
4149   output->source = g_object_ref (source);
4150   output->is_bye = is_bye;
4151   output->buffer = data->rtcp;
4152   /* queue the RTCP packet to push later */
4153   g_queue_push_tail (&data->output, output);
4154 }
4155
4156 static void
4157 update_generation (const gchar * key, RTPSource * source, ReportData * data)
4158 {
4159   RTPSession *sess = data->sess;
4160
4161   if (g_hash_table_size (source->reported_in_sr_of) >=
4162       sess->stats.internal_sources) {
4163     /* source is reported, move to next generation */
4164     source->generation = sess->generation + 1;
4165     g_hash_table_remove_all (source->reported_in_sr_of);
4166
4167     GST_LOG ("reported source %x, new generation: %d", source->ssrc,
4168         source->generation);
4169
4170     /* if we reported all sources in this generation, move to next */
4171     if (--data->num_to_report == 0) {
4172       sess->generation++;
4173       GST_DEBUG ("all reported, generation now %u", sess->generation);
4174     }
4175   }
4176 }
4177
4178 static void
4179 schedule_remaining_nacks (const gchar * key, RTPSource * source,
4180     ReportData * data)
4181 {
4182   RTPSession *sess = data->sess;
4183   GstClockTime *nack_deadlines;
4184   GstClockTime deadline;
4185   guint n_nacks;
4186
4187   if (!source->send_nack)
4188     return;
4189
4190   /* the scheduling is entirely based on available bandwidth, just take the
4191    * biggest seqnum, which will have the largest deadline to request early
4192    * RTCP. */
4193   nack_deadlines = rtp_source_get_nack_deadlines (source, &n_nacks);
4194   deadline = nack_deadlines[n_nacks - 1];
4195   RTP_SESSION_UNLOCK (sess);
4196   rtp_session_send_rtcp_with_deadline (sess, deadline);
4197   RTP_SESSION_LOCK (sess);
4198 }
4199
4200 static gboolean
4201 rtp_session_are_all_sources_bye (RTPSession * sess)
4202 {
4203   GHashTableIter iter;
4204   RTPSource *src;
4205
4206   RTP_SESSION_LOCK (sess);
4207   g_hash_table_iter_init (&iter, sess->ssrcs[sess->mask_idx]);
4208   while (g_hash_table_iter_next (&iter, NULL, (gpointer *) & src)) {
4209     if (src->internal && !src->sent_bye) {
4210       RTP_SESSION_UNLOCK (sess);
4211       return FALSE;
4212     }
4213   }
4214   RTP_SESSION_UNLOCK (sess);
4215
4216   return TRUE;
4217 }
4218
4219 /**
4220  * rtp_session_on_timeout:
4221  * @sess: an #RTPSession
4222  * @current_time: the current system time
4223  * @ntpnstime: the current NTP time in nanoseconds
4224  * @running_time: the current running_time of the pipeline
4225  *
4226  * Perform maintenance actions after the timeout obtained with
4227  * rtp_session_next_timeout() expired.
4228  *
4229  * This function will perform timeouts of receivers and senders, send a BYE
4230  * packet or generate RTCP packets with current session stats.
4231  *
4232  * This function can call the #RTPSessionSendRTCP callback, possibly multiple
4233  * times, for each packet that should be processed.
4234  *
4235  * Returns: a #GstFlowReturn.
4236  */
4237 GstFlowReturn
4238 rtp_session_on_timeout (RTPSession * sess, GstClockTime current_time,
4239     guint64 ntpnstime, GstClockTime running_time)
4240 {
4241   GstFlowReturn result = GST_FLOW_OK;
4242   ReportData data = { GST_RTCP_BUFFER_INIT };
4243   GHashTable *table_copy;
4244   ReportOutput *output;
4245   gboolean all_empty = FALSE;
4246
4247   g_return_val_if_fail (RTP_IS_SESSION (sess), GST_FLOW_ERROR);
4248
4249   GST_DEBUG ("reporting at %" GST_TIME_FORMAT ", NTP time %" GST_TIME_FORMAT
4250       ", running-time %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
4251       GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (running_time));
4252
4253   data.sess = sess;
4254   data.current_time = current_time;
4255   data.ntpnstime = ntpnstime;
4256   data.running_time = running_time;
4257   data.num_to_report = 0;
4258   data.may_suppress = FALSE;
4259   data.nacked_seqnums = 0;
4260   g_queue_init (&data.output);
4261
4262   RTP_SESSION_LOCK (sess);
4263   /* get a new interval, we need this for various cleanups etc */
4264   data.interval = calculate_rtcp_interval (sess, TRUE, sess->first_rtcp);
4265
4266   GST_DEBUG ("interval %" GST_TIME_FORMAT, GST_TIME_ARGS (data.interval));
4267
4268   /* we need an internal source now */
4269   if (sess->stats.internal_sources == 0) {
4270     RTPSource *source;
4271     gboolean created;
4272
4273     source = obtain_internal_source (sess, sess->suggested_ssrc, &created,
4274         current_time);
4275     sess->internal_ssrc_set = TRUE;
4276
4277     if (created)
4278       on_new_sender_ssrc (sess, source);
4279
4280     g_object_unref (source);
4281   }
4282
4283   sess->conflicting_addresses =
4284       timeout_conflicting_addresses (sess->conflicting_addresses, current_time);
4285
4286   /* Make a local copy of the hashtable. We need to do this because the
4287    * cleanup stage below releases the session lock. */
4288   table_copy = g_hash_table_new_full (NULL, NULL, NULL,
4289       (GDestroyNotify) g_object_unref);
4290   g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4291       (GHFunc) clone_ssrcs_hashtable, table_copy);
4292
4293   /* Clean up the session, mark the source for removing, this might release the
4294    * session lock. */
4295   g_hash_table_foreach (table_copy, (GHFunc) session_cleanup, &data);
4296   g_hash_table_destroy (table_copy);
4297
4298   /* Now remove the marked sources */
4299   g_hash_table_foreach_remove (sess->ssrcs[sess->mask_idx],
4300       (GHRFunc) remove_closing_sources, &data);
4301
4302   /* update point-to-point status */
4303   session_update_ptp (sess);
4304
4305   /* see if we need to generate SR or RR packets */
4306   if (!is_rtcp_time (sess, current_time, &data))
4307     goto done;
4308
4309   /* check if all the buffers are empty afer generation */
4310   all_empty = TRUE;
4311
4312   GST_DEBUG
4313       ("doing RTCP generation %u for %u sources, early %d, may suppress %d",
4314       sess->generation, data.num_to_report, data.is_early, data.may_suppress);
4315
4316   /* generate RTCP for all internal sources */
4317   g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4318       (GHFunc) generate_rtcp, &data);
4319
4320   /* update the generation for all the sources that have been reported */
4321   g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4322       (GHFunc) update_generation, &data);
4323
4324   /* we keep track of the last report time in order to timeout inactive
4325    * receivers or senders */
4326   if (!data.is_early) {
4327     GST_DEBUG ("Time since last regular RTCP: %" GST_TIME_FORMAT " - %"
4328         GST_TIME_FORMAT " = %" GST_TIME_FORMAT,
4329         GST_TIME_ARGS (data.current_time),
4330         GST_TIME_ARGS (sess->last_rtcp_send_time),
4331         GST_TIME_ARGS (data.current_time - sess->last_rtcp_send_time));
4332     sess->last_rtcp_send_time = data.current_time;
4333   }
4334
4335   GST_DEBUG ("Time since last RTCP: %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT
4336       " = %" GST_TIME_FORMAT, GST_TIME_ARGS (data.current_time),
4337       GST_TIME_ARGS (sess->last_rtcp_check_time),
4338       GST_TIME_ARGS (data.current_time - sess->last_rtcp_check_time));
4339   sess->last_rtcp_check_time = data.current_time;
4340   sess->first_rtcp = FALSE;
4341   sess->next_early_rtcp_time = GST_CLOCK_TIME_NONE;
4342   sess->scheduled_bye = FALSE;
4343
4344 done:
4345   RTP_SESSION_UNLOCK (sess);
4346
4347   /* notify about updated statistics */
4348   g_object_notify (G_OBJECT (sess), "stats");
4349
4350   /* push out the RTCP packets */
4351   while ((output = g_queue_pop_head (&data.output))) {
4352     gboolean do_not_suppress, empty_buffer;
4353     GstBuffer *buffer = output->buffer;
4354     RTPSource *source = output->source;
4355
4356     /* Give the user a change to add its own packet */
4357     g_signal_emit (sess, rtp_session_signals[SIGNAL_ON_SENDING_RTCP], 0,
4358         buffer, data.is_early, &do_not_suppress);
4359
4360     empty_buffer = gst_buffer_get_size (buffer) == 0;
4361
4362     if (!empty_buffer)
4363       all_empty = FALSE;
4364
4365     if (sess->callbacks.send_rtcp &&
4366         !empty_buffer && (do_not_suppress || !data.may_suppress)) {
4367       guint packet_size;
4368
4369       packet_size = gst_buffer_get_size (buffer) + sess->header_len;
4370
4371       UPDATE_AVG (sess->stats.avg_rtcp_packet_size, packet_size);
4372       GST_DEBUG ("%p, sending RTCP packet, avg size %u, %u", &sess->stats,
4373           sess->stats.avg_rtcp_packet_size, packet_size);
4374       result =
4375           sess->callbacks.send_rtcp (sess, source, buffer,
4376           rtp_session_are_all_sources_bye (sess), sess->send_rtcp_user_data);
4377
4378       RTP_SESSION_LOCK (sess);
4379       sess->stats.nacks_sent += data.nacked_seqnums;
4380       on_sender_ssrc_active (sess, source);
4381       RTP_SESSION_UNLOCK (sess);
4382     } else {
4383       GST_DEBUG ("freeing packet callback: %p"
4384           " empty_buffer: %d, "
4385           " do_not_suppress: %d may_suppress: %d", sess->callbacks.send_rtcp,
4386           empty_buffer, do_not_suppress, data.may_suppress);
4387       if (!empty_buffer) {
4388         RTP_SESSION_LOCK (sess);
4389         sess->stats.nacks_dropped += data.nacked_seqnums;
4390         RTP_SESSION_UNLOCK (sess);
4391       }
4392       gst_buffer_unref (buffer);
4393     }
4394     g_object_unref (source);
4395     g_slice_free (ReportOutput, output);
4396   }
4397
4398   if (all_empty)
4399     GST_ERROR ("generated empty RTCP messages for all the sources");
4400
4401   /* schedule remaining nacks */
4402   RTP_SESSION_LOCK (sess);
4403   g_hash_table_foreach (sess->ssrcs[sess->mask_idx],
4404       (GHFunc) schedule_remaining_nacks, &data);
4405   RTP_SESSION_UNLOCK (sess);
4406
4407   return result;
4408 }
4409
4410 /**
4411  * rtp_session_request_early_rtcp:
4412  * @sess: an #RTPSession
4413  * @current_time: the current system time
4414  * @max_delay: maximum delay
4415  *
4416  * Request transmission of early RTCP
4417  *
4418  * Returns: %TRUE if the related RTCP can be scheduled.
4419  */
4420 gboolean
4421 rtp_session_request_early_rtcp (RTPSession * sess, GstClockTime current_time,
4422     GstClockTime max_delay)
4423 {
4424   GstClockTime T_dither_max, T_rr, offset = 0;
4425   gboolean ret;
4426   gboolean allow_early;
4427
4428   /* Implements the algorithm described in RFC 4585 section 3.5.2 */
4429
4430   RTP_SESSION_LOCK (sess);
4431
4432   /* We assume a feedback profile if something is requesting RTCP
4433    * to be sent */
4434   sess->rtp_profile = GST_RTP_PROFILE_AVPF;
4435
4436   /* Check if already requested */
4437   /*  RFC 4585 section 3.5.2 step 2 */
4438   if (GST_CLOCK_TIME_IS_VALID (sess->next_early_rtcp_time)) {
4439     GST_LOG_OBJECT (sess, "already have next early rtcp time");
4440     ret = (current_time + max_delay > sess->next_early_rtcp_time);
4441     goto end;
4442   }
4443
4444   if (!GST_CLOCK_TIME_IS_VALID (sess->next_rtcp_check_time)) {
4445     GST_LOG_OBJECT (sess, "no next RTCP check time");
4446     ret = FALSE;
4447     goto end;
4448   }
4449
4450   /* RFC 4585 section 3.5.3 step 1
4451    * If no regular RTCP packet has been sent before, then a regular
4452    * RTCP packet has to be scheduled first and FB messages might be
4453    * included there
4454    */
4455   if (!GST_CLOCK_TIME_IS_VALID (sess->last_rtcp_send_time)) {
4456     GST_LOG_OBJECT (sess, "no RTCP sent yet");
4457
4458     if (current_time + max_delay > sess->next_rtcp_check_time) {
4459       GST_LOG_OBJECT (sess,
4460           "next scheduled time is soon %" GST_TIME_FORMAT " + %" GST_TIME_FORMAT
4461           " > %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
4462           GST_TIME_ARGS (max_delay),
4463           GST_TIME_ARGS (sess->next_rtcp_check_time));
4464       ret = TRUE;
4465     } else {
4466       GST_LOG_OBJECT (sess,
4467           "can't allow early feedback, next scheduled time is too late %"
4468           GST_TIME_FORMAT " + %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT,
4469           GST_TIME_ARGS (current_time), GST_TIME_ARGS (max_delay),
4470           GST_TIME_ARGS (sess->next_rtcp_check_time));
4471       ret = FALSE;
4472     }
4473     goto end;
4474   }
4475
4476   T_rr = sess->last_rtcp_interval;
4477
4478   /*  RFC 4585 section 3.5.2 step 2b */
4479   /* If the total sources is <=2, then there is only us and one peer */
4480   /* When there is one auxiliary stream the session can still do point
4481    * to point.
4482    */
4483   if (sess->is_doing_ptp) {
4484     T_dither_max = 0;
4485   } else {
4486     /* Divide by 2 because l = 0.5 */
4487     T_dither_max = T_rr;
4488     T_dither_max /= 2;
4489   }
4490
4491   /*  RFC 4585 section 3.5.2 step 3 */
4492   if (current_time + T_dither_max > sess->next_rtcp_check_time) {
4493     GST_LOG_OBJECT (sess,
4494         "don't send because of dither, next scheduled time is too soon %"
4495         GST_TIME_FORMAT " + %" GST_TIME_FORMAT " > %" GST_TIME_FORMAT,
4496         GST_TIME_ARGS (current_time), GST_TIME_ARGS (T_dither_max),
4497         GST_TIME_ARGS (sess->next_rtcp_check_time));
4498     ret = T_dither_max <= max_delay;
4499     goto end;
4500   }
4501
4502   /*  RFC 4585 section 3.5.2 step 4a and
4503    *  RFC 4585 section 3.5.2 step 6 */
4504   allow_early = FALSE;
4505   if (sess->last_rtcp_check_time == sess->last_rtcp_send_time) {
4506     /* Last time we sent a full RTCP packet, we can now immediately
4507      * send an early one as allow_early was reset to TRUE */
4508     allow_early = TRUE;
4509   } else if (sess->last_rtcp_check_time + T_rr <= current_time + max_delay) {
4510     /* Last packet we sent was an early RTCP packet and more than
4511      * T_rr has passed since then, meaning we would have suppressed
4512      * a regular RTCP packet already and reset allow_early to TRUE */
4513     allow_early = TRUE;
4514
4515     /* We have to offset a bit as T_rr has not passed yet, but will before
4516      * max_delay */
4517     if (sess->last_rtcp_check_time + T_rr > current_time)
4518       offset = (sess->last_rtcp_check_time + T_rr) - current_time;
4519   } else {
4520     GST_DEBUG_OBJECT (sess,
4521         "can't allow early RTCP yet: last regular %" GST_TIME_FORMAT ", %"
4522         GST_TIME_FORMAT " + %" GST_TIME_FORMAT " > %" GST_TIME_FORMAT " + %"
4523         GST_TIME_FORMAT, GST_TIME_ARGS (sess->last_rtcp_send_time),
4524         GST_TIME_ARGS (sess->last_rtcp_check_time), GST_TIME_ARGS (T_rr),
4525         GST_TIME_ARGS (current_time), GST_TIME_ARGS (max_delay));
4526   }
4527
4528   if (!allow_early) {
4529     /* Ignore the request a scheduled packet will be in time anyway */
4530     if (current_time + max_delay > sess->next_rtcp_check_time) {
4531       GST_LOG_OBJECT (sess,
4532           "next scheduled time is soon %" GST_TIME_FORMAT " + %" GST_TIME_FORMAT
4533           " > %" GST_TIME_FORMAT, GST_TIME_ARGS (current_time),
4534           GST_TIME_ARGS (max_delay),
4535           GST_TIME_ARGS (sess->next_rtcp_check_time));
4536       ret = TRUE;
4537     } else {
4538       GST_LOG_OBJECT (sess,
4539           "can't allow early feedback and next scheduled time is too late %"
4540           GST_TIME_FORMAT " + %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT,
4541           GST_TIME_ARGS (current_time), GST_TIME_ARGS (max_delay),
4542           GST_TIME_ARGS (sess->next_rtcp_check_time));
4543       ret = FALSE;
4544     }
4545     goto end;
4546   }
4547
4548   /*  RFC 4585 section 3.5.2 step 4b */
4549   if (T_dither_max) {
4550     /* Schedule an early transmission later */
4551     sess->next_early_rtcp_time = g_random_double () * T_dither_max +
4552         current_time + offset;
4553   } else {
4554     /* If no dithering, schedule it for NOW */
4555     sess->next_early_rtcp_time = current_time + offset;
4556   }
4557
4558   GST_LOG_OBJECT (sess, "next early RTCP time %" GST_TIME_FORMAT
4559       ", next regular RTCP time %" GST_TIME_FORMAT,
4560       GST_TIME_ARGS (sess->next_early_rtcp_time),
4561       GST_TIME_ARGS (sess->next_rtcp_check_time));
4562   RTP_SESSION_UNLOCK (sess);
4563
4564   /* notify app of need to send packet early
4565    * and therefore of timeout change */
4566   if (sess->callbacks.reconsider)
4567     sess->callbacks.reconsider (sess, sess->reconsider_user_data);
4568
4569   return TRUE;
4570
4571 end:
4572
4573   RTP_SESSION_UNLOCK (sess);
4574
4575   return ret;
4576 }
4577
4578 static gboolean
4579 rtp_session_send_rtcp_internal (RTPSession * sess, GstClockTime now,
4580     GstClockTime max_delay)
4581 {
4582   /* notify the application that we intend to send early RTCP */
4583   if (sess->callbacks.notify_early_rtcp)
4584     sess->callbacks.notify_early_rtcp (sess, sess->notify_early_rtcp_user_data);
4585
4586   return rtp_session_request_early_rtcp (sess, now, max_delay);
4587 }
4588
4589 static gboolean
4590 rtp_session_send_rtcp_with_deadline (RTPSession * sess, GstClockTime deadline)
4591 {
4592   GstClockTime now, max_delay;
4593
4594   if (!sess->callbacks.send_rtcp)
4595     return FALSE;
4596
4597   now = sess->callbacks.request_time (sess, sess->request_time_user_data);
4598
4599   if (deadline < now)
4600     return FALSE;
4601
4602   max_delay = deadline - now;
4603
4604   return rtp_session_send_rtcp_internal (sess, now, max_delay);
4605 }
4606
4607 static gboolean
4608 rtp_session_send_rtcp (RTPSession * sess, GstClockTime max_delay)
4609 {
4610   GstClockTime now;
4611
4612   if (!sess->callbacks.send_rtcp)
4613     return FALSE;
4614
4615   now = sess->callbacks.request_time (sess, sess->request_time_user_data);
4616
4617   return rtp_session_send_rtcp_internal (sess, now, max_delay);
4618 }
4619
4620 gboolean
4621 rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc,
4622     gboolean fir, gint count)
4623 {
4624   RTPSource *src;
4625
4626   RTP_SESSION_LOCK (sess);
4627   src = find_source (sess, ssrc);
4628   if (src == NULL)
4629     goto no_source;
4630
4631   if (fir) {
4632     src->send_pli = FALSE;
4633     src->send_fir = TRUE;
4634
4635     if (count == -1 || count != src->last_fir_count)
4636       src->current_send_fir_seqnum++;
4637     src->last_fir_count = count;
4638   } else if (!src->send_fir) {
4639     src->send_pli = TRUE;
4640   }
4641   RTP_SESSION_UNLOCK (sess);
4642
4643   if (!rtp_session_send_rtcp (sess, 5 * GST_SECOND)) {
4644     GST_DEBUG ("FIR/PLI not sent early, sending with next regular RTCP");
4645   }
4646
4647   return TRUE;
4648
4649   /* ERRORS */
4650 no_source:
4651   {
4652     RTP_SESSION_UNLOCK (sess);
4653     return FALSE;
4654   }
4655 }
4656
4657 /**
4658  * rtp_session_request_nack:
4659  * @sess: a #RTPSession
4660  * @ssrc: the SSRC
4661  * @seqnum: the missing seqnum
4662  * @max_delay: max delay to request NACK
4663  *
4664  * Request scheduling of a NACK feedback packet for @seqnum in @ssrc.
4665  *
4666  * Returns: %TRUE if the NACK feedback could be scheduled
4667  */
4668 gboolean
4669 rtp_session_request_nack (RTPSession * sess, guint32 ssrc, guint16 seqnum,
4670     GstClockTime max_delay)
4671 {
4672   RTPSource *source;
4673   GstClockTime now;
4674
4675   if (!sess->callbacks.send_rtcp)
4676     return FALSE;
4677
4678   now = sess->callbacks.request_time (sess, sess->request_time_user_data);
4679
4680   RTP_SESSION_LOCK (sess);
4681   source = find_source (sess, ssrc);
4682   if (source == NULL)
4683     goto no_source;
4684
4685   GST_DEBUG ("request NACK for SSRC %08x, #%u, deadline %" GST_TIME_FORMAT,
4686       ssrc, seqnum, GST_TIME_ARGS (now + max_delay));
4687   rtp_source_register_nack (source, seqnum, now + max_delay);
4688   RTP_SESSION_UNLOCK (sess);
4689
4690   if (!rtp_session_send_rtcp_internal (sess, now, max_delay)) {
4691     GST_DEBUG ("NACK not sent early, sending with next regular RTCP");
4692   }
4693
4694   return TRUE;
4695
4696   /* ERRORS */
4697 no_source:
4698   {
4699     RTP_SESSION_UNLOCK (sess);
4700     return FALSE;
4701   }
4702 }