rtpbasepayload: add auto-header-extension property
[platform/upstream/gstreamer.git] / gst-libs / gst / rtp / gstrtpbasepayload.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim@fluendo.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
13  */
14
15 /**
16  * SECTION:gstrtpbasepayload
17  * @title: GstRTPBasePayload
18  * @short_description: Base class for RTP payloader
19  *
20  * Provides a base class for RTP payloaders
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28
29 #include <gst/rtp/gstrtpbuffer.h>
30
31 #include "gstrtpbasepayload.h"
32 #include "gstrtpmeta.h"
33 #include "gstrtphdrext.h"
34
35 GST_DEBUG_CATEGORY_STATIC (rtpbasepayload_debug);
36 #define GST_CAT_DEFAULT (rtpbasepayload_debug)
37
38 static gboolean enable_experimental_twcc = FALSE;
39
40 struct _GstRTPBasePayloadPrivate
41 {
42   gboolean ts_offset_random;
43   gboolean seqnum_offset_random;
44   gboolean ssrc_random;
45   guint16 next_seqnum;
46   gboolean perfect_rtptime;
47   gint notified_first_timestamp;
48
49   gboolean pt_set;
50
51   gboolean source_info;
52   GstBuffer *input_meta_buffer;
53
54   guint64 base_offset;
55   gint64 base_rtime;
56   guint64 base_rtime_hz;
57   guint64 running_time;
58   gboolean scale_rtptime;
59   gboolean auto_hdr_ext;
60
61   gint64 prop_max_ptime;
62   gint64 caps_max_ptime;
63
64   gboolean onvif_no_rate_control;
65
66   gboolean negotiated;
67
68   gboolean delay_segment;
69   GstEvent *pending_segment;
70
71   GstCaps *subclass_srccaps;
72   GstCaps *sinkcaps;
73
74   /* array of GstRTPHeaderExtension's * */
75   GPtrArray *header_exts;
76 };
77
78 /* RTPBasePayload signals and args */
79 enum
80 {
81   SIGNAL_0,
82   SIGNAL_REQUEST_EXTENSION,
83   SIGNAL_ADD_EXTENSION,
84   SIGNAL_CLEAR_EXTENSIONS,
85   LAST_SIGNAL
86 };
87
88 static guint gst_rtp_base_payload_signals[LAST_SIGNAL] = { 0 };
89
90 /* FIXME 0.11, a better default is the Ethernet MTU of
91  * 1500 - sizeof(headers) as pointed out by marcelm in IRC:
92  * So an Ethernet MTU of 1500, minus 60 for the max IP, minus 8 for UDP, gives
93  * 1432 bytes or so.  And that should be adjusted downward further for other
94  * encapsulations like PPPoE, so 1400 at most.
95  */
96 #define DEFAULT_MTU                     1400
97 #define DEFAULT_PT                      96
98 #define DEFAULT_SSRC                    -1
99 #define DEFAULT_TIMESTAMP_OFFSET        -1
100 #define DEFAULT_SEQNUM_OFFSET           -1
101 #define DEFAULT_MAX_PTIME               -1
102 #define DEFAULT_MIN_PTIME               0
103 #define DEFAULT_PERFECT_RTPTIME         TRUE
104 #define DEFAULT_PTIME_MULTIPLE          0
105 #define DEFAULT_RUNNING_TIME            GST_CLOCK_TIME_NONE
106 #define DEFAULT_SOURCE_INFO             FALSE
107 #define DEFAULT_ONVIF_NO_RATE_CONTROL   FALSE
108 #define DEFAULT_SCALE_RTPTIME           TRUE
109 #define DEFAULT_AUTO_HEADER_EXTENSION   TRUE
110
111 #define RTP_HEADER_EXT_ONE_BYTE_MAX_SIZE 16
112 #define RTP_HEADER_EXT_TWO_BYTE_MAX_SIZE 256
113 #define RTP_HEADER_EXT_ONE_BYTE_MAX_ID 14
114 #define RTP_HEADER_EXT_TWO_BYTE_MAX_ID 255
115
116 enum
117 {
118   PROP_0,
119   PROP_MTU,
120   PROP_PT,
121   PROP_SSRC,
122   PROP_TIMESTAMP_OFFSET,
123   PROP_SEQNUM_OFFSET,
124   PROP_MAX_PTIME,
125   PROP_MIN_PTIME,
126   PROP_TIMESTAMP,
127   PROP_SEQNUM,
128   PROP_PERFECT_RTPTIME,
129   PROP_PTIME_MULTIPLE,
130   PROP_STATS,
131   PROP_SOURCE_INFO,
132   PROP_ONVIF_NO_RATE_CONTROL,
133   PROP_SCALE_RTPTIME,
134   PROP_AUTO_HEADER_EXTENSION,
135   PROP_LAST
136 };
137
138 static void gst_rtp_base_payload_class_init (GstRTPBasePayloadClass * klass);
139 static void gst_rtp_base_payload_init (GstRTPBasePayload * rtpbasepayload,
140     gpointer g_class);
141 static void gst_rtp_base_payload_finalize (GObject * object);
142
143 static GstCaps *gst_rtp_base_payload_getcaps_default (GstRTPBasePayload *
144     rtpbasepayload, GstPad * pad, GstCaps * filter);
145
146 static gboolean gst_rtp_base_payload_sink_event_default (GstRTPBasePayload *
147     rtpbasepayload, GstEvent * event);
148 static gboolean gst_rtp_base_payload_sink_event (GstPad * pad,
149     GstObject * parent, GstEvent * event);
150 static gboolean gst_rtp_base_payload_src_event_default (GstRTPBasePayload *
151     rtpbasepayload, GstEvent * event);
152 static gboolean gst_rtp_base_payload_src_event (GstPad * pad,
153     GstObject * parent, GstEvent * event);
154 static gboolean gst_rtp_base_payload_query_default (GstRTPBasePayload *
155     rtpbasepayload, GstPad * pad, GstQuery * query);
156 static gboolean gst_rtp_base_payload_query (GstPad * pad, GstObject * parent,
157     GstQuery * query);
158 static GstFlowReturn gst_rtp_base_payload_chain (GstPad * pad,
159     GstObject * parent, GstBuffer * buffer);
160
161 static void gst_rtp_base_payload_set_property (GObject * object, guint prop_id,
162     const GValue * value, GParamSpec * pspec);
163 static void gst_rtp_base_payload_get_property (GObject * object, guint prop_id,
164     GValue * value, GParamSpec * pspec);
165
166 static GstStateChangeReturn gst_rtp_base_payload_change_state (GstElement *
167     element, GstStateChange transition);
168
169 static gboolean gst_rtp_base_payload_negotiate (GstRTPBasePayload * payload);
170
171 static void gst_rtp_base_payload_add_extension (GstRTPBasePayload * payload,
172     GstRTPHeaderExtension * ext);
173 static void gst_rtp_base_payload_clear_extensions (GstRTPBasePayload * payload);
174
175 static GstElementClass *parent_class = NULL;
176 static gint private_offset = 0;
177
178 GType
179 gst_rtp_base_payload_get_type (void)
180 {
181   static GType rtpbasepayload_type = 0;
182
183   if (g_once_init_enter ((gsize *) & rtpbasepayload_type)) {
184     static const GTypeInfo rtpbasepayload_info = {
185       sizeof (GstRTPBasePayloadClass),
186       NULL,
187       NULL,
188       (GClassInitFunc) gst_rtp_base_payload_class_init,
189       NULL,
190       NULL,
191       sizeof (GstRTPBasePayload),
192       0,
193       (GInstanceInitFunc) gst_rtp_base_payload_init,
194     };
195     GType _type;
196
197     _type = g_type_register_static (GST_TYPE_ELEMENT, "GstRTPBasePayload",
198         &rtpbasepayload_info, G_TYPE_FLAG_ABSTRACT);
199
200     private_offset =
201         g_type_add_instance_private (_type, sizeof (GstRTPBasePayloadPrivate));
202
203     g_once_init_leave ((gsize *) & rtpbasepayload_type, _type);
204   }
205   return rtpbasepayload_type;
206 }
207
208 static inline GstRTPBasePayloadPrivate *
209 gst_rtp_base_payload_get_instance_private (GstRTPBasePayload * self)
210 {
211   return (G_STRUCT_MEMBER_P (self, private_offset));
212 }
213
214 static GstRTPHeaderExtension *
215 gst_rtp_base_payload_request_extension_default (GstRTPBasePayload * payload,
216     guint ext_id, const gchar * uri)
217 {
218   GstRTPHeaderExtension *ext = NULL;
219
220   if (!payload->priv->auto_hdr_ext)
221     return NULL;
222
223   ext = gst_rtp_header_extension_create_from_uri (uri);
224   if (ext) {
225     GST_DEBUG_OBJECT (payload,
226         "Automatically enabled extension %s for uri \'%s\'",
227         GST_ELEMENT_NAME (ext), uri);
228
229     gst_rtp_header_extension_set_id (ext, ext_id);
230   } else {
231     GST_DEBUG_OBJECT (payload,
232         "Didn't find any extension implementing uri \'%s\'", uri);
233   }
234
235   return ext;
236 }
237
238 static gboolean
239 extension_accumulator (GSignalInvocationHint * ihint,
240     GValue * return_accu, const GValue * handler_return, gpointer data)
241 {
242   gpointer ext;
243
244   /* Call default handler if user callback didn't create the extension */
245   ext = g_value_get_object (handler_return);
246   if (!ext)
247     return TRUE;
248
249   g_value_set_object (return_accu, ext);
250   return FALSE;
251 }
252
253 static void
254 gst_rtp_base_payload_class_init (GstRTPBasePayloadClass * klass)
255 {
256   GObjectClass *gobject_class;
257   GstElementClass *gstelement_class;
258
259   gobject_class = (GObjectClass *) klass;
260   gstelement_class = (GstElementClass *) klass;
261
262   if (g_getenv ("GST_RTP_ENABLE_EXPERIMENTAL_TWCC_PROPERTY"))
263     enable_experimental_twcc = TRUE;
264
265   if (private_offset != 0)
266     g_type_class_adjust_private_offset (klass, &private_offset);
267
268   parent_class = g_type_class_peek_parent (klass);
269
270   gobject_class->finalize = gst_rtp_base_payload_finalize;
271
272   gobject_class->set_property = gst_rtp_base_payload_set_property;
273   gobject_class->get_property = gst_rtp_base_payload_get_property;
274
275   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MTU,
276       g_param_spec_uint ("mtu", "MTU",
277           "Maximum size of one packet",
278           28, G_MAXUINT, DEFAULT_MTU,
279           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
280   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PT,
281       g_param_spec_uint ("pt", "payload type",
282           "The payload type of the packets", 0, 0x7f, DEFAULT_PT,
283           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
285       g_param_spec_uint ("ssrc", "SSRC",
286           "The SSRC of the packets (default == random)", 0, G_MAXUINT32,
287           DEFAULT_SSRC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
288   g_object_class_install_property (G_OBJECT_CLASS (klass),
289       PROP_TIMESTAMP_OFFSET, g_param_spec_uint ("timestamp-offset",
290           "Timestamp Offset",
291           "Offset to add to all outgoing timestamps (default = random)", 0,
292           G_MAXUINT32, DEFAULT_TIMESTAMP_OFFSET,
293           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
294   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
295       g_param_spec_int ("seqnum-offset", "Sequence number Offset",
296           "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXUINT16,
297           DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_PTIME,
299       g_param_spec_int64 ("max-ptime", "Max packet time",
300           "Maximum duration of the packet data in ns (-1 = unlimited up to MTU)",
301           -1, G_MAXINT64, DEFAULT_MAX_PTIME,
302           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
303   /**
304    * GstRTPBasePayload:min-ptime:
305    *
306    * Minimum duration of the packet data in ns (can't go above MTU)
307    **/
308   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MIN_PTIME,
309       g_param_spec_int64 ("min-ptime", "Min packet time",
310           "Minimum duration of the packet data in ns (can't go above MTU)",
311           0, G_MAXINT64, DEFAULT_MIN_PTIME,
312           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
313
314   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMESTAMP,
315       g_param_spec_uint ("timestamp", "Timestamp",
316           "The RTP timestamp of the last processed packet",
317           0, G_MAXUINT32, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
318   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
319       g_param_spec_uint ("seqnum", "Sequence number",
320           "The RTP sequence number of the last processed packet",
321           0, G_MAXUINT16, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
322
323   /**
324    * GstRTPBasePayload:perfect-rtptime:
325    *
326    * Try to use the offset fields to generate perfect RTP timestamps. When this
327    * option is disabled, RTP timestamps are generated from GST_BUFFER_PTS of
328    * each payloaded buffer. The PTSes of buffers may not necessarily increment
329    * with the amount of data in each input buffer, consider e.g. the case where
330    * the buffer arrives from a network which means that the PTS is unrelated to
331    * the amount of data. Because the RTP timestamps are generated from
332    * GST_BUFFER_PTS this can result in RTP timestamps that also don't increment
333    * with the amount of data in the payloaded packet. To circumvent this it is
334    * possible to set the perfect rtptime option enabled. When this option is
335    * enabled the payloader will increment the RTP timestamps based on
336    * GST_BUFFER_OFFSET which relates to the amount of data in each packet
337    * rather than the GST_BUFFER_PTS of each buffer and therefore the RTP
338    * timestamps will more closely correlate with the amount of data in each
339    * buffer. Currently GstRTPBasePayload is limited to handling perfect RTP
340    * timestamps for audio streams.
341    */
342   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PERFECT_RTPTIME,
343       g_param_spec_boolean ("perfect-rtptime", "Perfect RTP Time",
344           "Generate perfect RTP timestamps when possible",
345           DEFAULT_PERFECT_RTPTIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346   /**
347    * GstRTPBasePayload:ptime-multiple:
348    *
349    * Force buffers to be multiples of this duration in ns (0 disables)
350    **/
351   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PTIME_MULTIPLE,
352       g_param_spec_int64 ("ptime-multiple", "Packet time multiple",
353           "Force buffers to be multiples of this duration in ns (0 disables)",
354           0, G_MAXINT64, DEFAULT_PTIME_MULTIPLE,
355           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
356
357   /**
358    * GstRTPBasePayload:stats:
359    *
360    * Various payloader statistics retrieved atomically (and are therefore
361    * synchroized with each other), these can be used e.g. to generate an
362    * RTP-Info header. This property return a GstStructure named
363    * application/x-rtp-payload-stats containing the following fields relating to
364    * the last processed buffer and current state of the stream being payloaded:
365    *
366    *   * `clock-rate` :#G_TYPE_UINT, clock-rate of the stream
367    *   * `running-time` :#G_TYPE_UINT64, running time
368    *   * `seqnum` :#G_TYPE_UINT, sequence number, same as #GstRTPBasePayload:seqnum
369    *   * `timestamp` :#G_TYPE_UINT, RTP timestamp, same as #GstRTPBasePayload:timestamp
370    *   * `ssrc` :#G_TYPE_UINT, The SSRC in use
371    *   * `pt` :#G_TYPE_UINT, The Payload type in use, same as #GstRTPBasePayload:pt
372    *   * `seqnum-offset` :#G_TYPE_UINT, The current offset added to the seqnum
373    *   * `timestamp-offset` :#G_TYPE_UINT, The current offset added to the timestamp
374    **/
375   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_STATS,
376       g_param_spec_boxed ("stats", "Statistics", "Various statistics",
377           GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
378
379   /**
380    * GstRTPBasePayload:source-info:
381    *
382    * Enable writing the CSRC field in allocated RTP header based on RTP source
383    * information found in the input buffer's #GstRTPSourceMeta.
384    *
385    * Since: 1.16
386    **/
387   g_object_class_install_property (gobject_class, PROP_SOURCE_INFO,
388       g_param_spec_boolean ("source-info", "RTP source information",
389           "Write CSRC based on buffer meta RTP source information",
390           DEFAULT_SOURCE_INFO, G_PARAM_READWRITE));
391
392   /**
393    * GstRTPBasePayload:onvif-no-rate-control:
394    *
395    * Make the payloader timestamp packets according to the Rate-Control=no
396    * behaviour specified in the ONVIF replay spec.
397    *
398    * Since: 1.16
399    */
400   g_object_class_install_property (G_OBJECT_CLASS (klass),
401       PROP_ONVIF_NO_RATE_CONTROL, g_param_spec_boolean ("onvif-no-rate-control",
402           "ONVIF no rate control",
403           "Enable ONVIF Rate-Control=no timestamping mode",
404           DEFAULT_ONVIF_NO_RATE_CONTROL,
405           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406
407   /**
408    * GstRTPBasePayload:scale-rtptime:
409    *
410    * Make the RTP packets' timestamps be scaled with the segment's rate
411    * (corresponding to RTSP speed parameter). Disabling this property means
412    * the timestamps will not be affected by the set delivery speed (RTSP speed).
413    * 
414    * Example: A server wants to allow streaming a recorded video in double 
415    * speed but still have the timestamps correspond to the position in the 
416    * video. This is achieved by the client setting RTSP Speed to 2 while the 
417    * server has this property disabled.
418    * 
419    * Since: 1.18
420    */
421   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SCALE_RTPTIME,
422       g_param_spec_boolean ("scale-rtptime", "Scale RTP time",
423           "Whether the RTP timestamp should be scaled with the rate (speed)",
424           DEFAULT_SCALE_RTPTIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
425
426   /**
427    * GstRTPBasePayload:auto-header-extension:
428    *
429    * If enabled, the payloader will automatically try to enable all the
430    * RTP header extensions provided in the src caps, saving the application
431    * the need to handle these extensions manually using the
432    * GstRTPBasePayload::request-extension: signal.
433    *
434    * Since: 1.20
435    */
436   g_object_class_install_property (G_OBJECT_CLASS (klass),
437       PROP_AUTO_HEADER_EXTENSION, g_param_spec_boolean ("auto-header-extension",
438           "Automatic RTP header extension",
439           "Whether RTP header extensions should be automatically enabled, if an implementation is available",
440           DEFAULT_AUTO_HEADER_EXTENSION,
441           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
442
443   /**
444    * GstRTPBasePayload::add-extension:
445    * @object: the #GstRTPBasePayload
446    * @ext: (transfer full): the #GstRTPHeaderExtension
447    *
448    * Add @ext as an extension for writing part of an RTP header extension onto
449    * outgoing RTP packets.
450    *
451    * Since: 1.20
452    */
453   gst_rtp_base_payload_signals[SIGNAL_ADD_EXTENSION] =
454       g_signal_new_class_handler ("add-extension", G_TYPE_FROM_CLASS (klass),
455       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
456       G_CALLBACK (gst_rtp_base_payload_add_extension), NULL, NULL, NULL,
457       G_TYPE_NONE, 1, GST_TYPE_RTP_HEADER_EXTENSION);
458
459   /**
460    * GstRTPBasePayload::request-extension:
461    * @object: the #GstRTPBasePayload
462    * @ext_id: the extension id being requested
463    * @ext_uri: the extension URI being requested
464    *
465    * The returned @ext must be configured with the correct @ext_id and with the
466    * necessary attributes as required by the extension implementation.
467    *
468    * Returns: (transfer full): the #GstRTPHeaderExtension for @ext_id, or %NULL
469    *
470    * Since: 1.20
471    */
472   gst_rtp_base_payload_signals[SIGNAL_REQUEST_EXTENSION] =
473       g_signal_new_class_handler ("request-extension",
474       G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
475       G_CALLBACK (gst_rtp_base_payload_request_extension_default),
476       extension_accumulator, NULL, NULL,
477       GST_TYPE_RTP_HEADER_EXTENSION, 2, G_TYPE_UINT, G_TYPE_STRING);
478
479   /**
480    * GstRTPBasePayload::clear-extensions:
481    * @object: the #GstRTPBasePayload
482    *
483    * Clear all RTP header extensions used by this payloader.
484    *
485    * Since: 1.20
486    */
487   gst_rtp_base_payload_signals[SIGNAL_CLEAR_EXTENSIONS] =
488       g_signal_new_class_handler ("clear-extensions", G_TYPE_FROM_CLASS (klass),
489       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
490       G_CALLBACK (gst_rtp_base_payload_clear_extensions), NULL, NULL, NULL,
491       G_TYPE_NONE, 0);
492
493   gstelement_class->change_state = gst_rtp_base_payload_change_state;
494
495   klass->get_caps = gst_rtp_base_payload_getcaps_default;
496   klass->sink_event = gst_rtp_base_payload_sink_event_default;
497   klass->src_event = gst_rtp_base_payload_src_event_default;
498   klass->query = gst_rtp_base_payload_query_default;
499
500   GST_DEBUG_CATEGORY_INIT (rtpbasepayload_debug, "rtpbasepayload", 0,
501       "Base class for RTP Payloaders");
502 }
503
504 static void
505 gst_rtp_base_payload_init (GstRTPBasePayload * rtpbasepayload, gpointer g_class)
506 {
507   GstPadTemplate *templ;
508   GstRTPBasePayloadPrivate *priv;
509
510   rtpbasepayload->priv = priv =
511       gst_rtp_base_payload_get_instance_private (rtpbasepayload);
512
513   templ =
514       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
515   g_return_if_fail (templ != NULL);
516
517   rtpbasepayload->srcpad = gst_pad_new_from_template (templ, "src");
518   gst_pad_set_event_function (rtpbasepayload->srcpad,
519       gst_rtp_base_payload_src_event);
520   gst_element_add_pad (GST_ELEMENT (rtpbasepayload), rtpbasepayload->srcpad);
521
522   templ =
523       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
524   g_return_if_fail (templ != NULL);
525
526   rtpbasepayload->sinkpad = gst_pad_new_from_template (templ, "sink");
527   gst_pad_set_chain_function (rtpbasepayload->sinkpad,
528       gst_rtp_base_payload_chain);
529   gst_pad_set_event_function (rtpbasepayload->sinkpad,
530       gst_rtp_base_payload_sink_event);
531   gst_pad_set_query_function (rtpbasepayload->sinkpad,
532       gst_rtp_base_payload_query);
533   gst_element_add_pad (GST_ELEMENT (rtpbasepayload), rtpbasepayload->sinkpad);
534
535   rtpbasepayload->mtu = DEFAULT_MTU;
536   rtpbasepayload->pt = DEFAULT_PT;
537   rtpbasepayload->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
538   rtpbasepayload->ssrc = DEFAULT_SSRC;
539   rtpbasepayload->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
540   priv->running_time = DEFAULT_RUNNING_TIME;
541   priv->seqnum_offset_random = (rtpbasepayload->seqnum_offset == -1);
542   priv->ts_offset_random = (rtpbasepayload->ts_offset == -1);
543   priv->ssrc_random = (rtpbasepayload->ssrc == -1);
544   priv->pt_set = FALSE;
545   priv->source_info = DEFAULT_SOURCE_INFO;
546
547   rtpbasepayload->max_ptime = DEFAULT_MAX_PTIME;
548   rtpbasepayload->min_ptime = DEFAULT_MIN_PTIME;
549   rtpbasepayload->priv->perfect_rtptime = DEFAULT_PERFECT_RTPTIME;
550   rtpbasepayload->ptime_multiple = DEFAULT_PTIME_MULTIPLE;
551   rtpbasepayload->priv->base_offset = GST_BUFFER_OFFSET_NONE;
552   rtpbasepayload->priv->base_rtime_hz = GST_BUFFER_OFFSET_NONE;
553   rtpbasepayload->priv->onvif_no_rate_control = DEFAULT_ONVIF_NO_RATE_CONTROL;
554   rtpbasepayload->priv->scale_rtptime = DEFAULT_SCALE_RTPTIME;
555   rtpbasepayload->priv->auto_hdr_ext = DEFAULT_AUTO_HEADER_EXTENSION;
556
557   rtpbasepayload->media = NULL;
558   rtpbasepayload->encoding_name = NULL;
559
560   rtpbasepayload->clock_rate = 0;
561
562   rtpbasepayload->priv->caps_max_ptime = DEFAULT_MAX_PTIME;
563   rtpbasepayload->priv->prop_max_ptime = DEFAULT_MAX_PTIME;
564   rtpbasepayload->priv->header_exts =
565       g_ptr_array_new_with_free_func ((GDestroyNotify) gst_object_unref);
566 }
567
568 static void
569 gst_rtp_base_payload_finalize (GObject * object)
570 {
571   GstRTPBasePayload *rtpbasepayload;
572
573   rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
574
575   g_free (rtpbasepayload->media);
576   rtpbasepayload->media = NULL;
577   g_free (rtpbasepayload->encoding_name);
578   rtpbasepayload->encoding_name = NULL;
579
580   gst_caps_replace (&rtpbasepayload->priv->subclass_srccaps, NULL);
581   gst_caps_replace (&rtpbasepayload->priv->sinkcaps, NULL);
582
583   g_ptr_array_unref (rtpbasepayload->priv->header_exts);
584   rtpbasepayload->priv->header_exts = NULL;
585
586   G_OBJECT_CLASS (parent_class)->finalize (object);
587 }
588
589 static GstCaps *
590 gst_rtp_base_payload_getcaps_default (GstRTPBasePayload * rtpbasepayload,
591     GstPad * pad, GstCaps * filter)
592 {
593   GstCaps *caps;
594
595   caps = GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
596   GST_DEBUG_OBJECT (pad,
597       "using pad template %p with caps %p %" GST_PTR_FORMAT,
598       GST_PAD_PAD_TEMPLATE (pad), caps, caps);
599
600   if (filter)
601     caps = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
602   else
603     caps = gst_caps_ref (caps);
604
605   return caps;
606 }
607
608 static gboolean
609 gst_rtp_base_payload_sink_event_default (GstRTPBasePayload * rtpbasepayload,
610     GstEvent * event)
611 {
612   GstObject *parent = GST_OBJECT_CAST (rtpbasepayload);
613   gboolean res = FALSE;
614
615   switch (GST_EVENT_TYPE (event)) {
616     case GST_EVENT_FLUSH_START:
617       res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
618       break;
619     case GST_EVENT_FLUSH_STOP:
620       res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
621       gst_segment_init (&rtpbasepayload->segment, GST_FORMAT_UNDEFINED);
622       gst_event_replace (&rtpbasepayload->priv->pending_segment, NULL);
623       break;
624     case GST_EVENT_CAPS:
625     {
626       GstRTPBasePayloadClass *rtpbasepayload_class;
627       GstCaps *caps;
628
629       gst_event_parse_caps (event, &caps);
630       GST_DEBUG_OBJECT (rtpbasepayload, "setting caps %" GST_PTR_FORMAT, caps);
631
632       gst_caps_replace (&rtpbasepayload->priv->sinkcaps, caps);
633
634       rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
635       if (rtpbasepayload_class->set_caps)
636         res = rtpbasepayload_class->set_caps (rtpbasepayload, caps);
637       else
638         res = gst_rtp_base_payload_negotiate (rtpbasepayload);
639
640       rtpbasepayload->priv->negotiated = res;
641
642       gst_event_unref (event);
643       break;
644     }
645     case GST_EVENT_SEGMENT:
646     {
647       GstSegment *segment;
648
649       segment = &rtpbasepayload->segment;
650       gst_event_copy_segment (event, segment);
651
652       rtpbasepayload->priv->base_offset = GST_BUFFER_OFFSET_NONE;
653
654       GST_DEBUG_OBJECT (rtpbasepayload,
655           "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
656       if (rtpbasepayload->priv->delay_segment) {
657         gst_event_replace (&rtpbasepayload->priv->pending_segment, event);
658         gst_event_unref (event);
659         res = TRUE;
660       } else {
661         res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
662       }
663       break;
664     }
665     case GST_EVENT_GAP:
666     {
667       if (G_UNLIKELY (rtpbasepayload->priv->pending_segment)) {
668         gst_pad_push_event (rtpbasepayload->srcpad,
669             rtpbasepayload->priv->pending_segment);
670         rtpbasepayload->priv->pending_segment = FALSE;
671         rtpbasepayload->priv->delay_segment = FALSE;
672       }
673       res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
674       break;
675     }
676     default:
677       res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
678       break;
679   }
680   return res;
681 }
682
683 static gboolean
684 gst_rtp_base_payload_sink_event (GstPad * pad, GstObject * parent,
685     GstEvent * event)
686 {
687   GstRTPBasePayload *rtpbasepayload;
688   GstRTPBasePayloadClass *rtpbasepayload_class;
689   gboolean res = FALSE;
690
691   rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
692   rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
693
694   if (rtpbasepayload_class->sink_event)
695     res = rtpbasepayload_class->sink_event (rtpbasepayload, event);
696   else
697     gst_event_unref (event);
698
699   return res;
700 }
701
702 static gboolean
703 gst_rtp_base_payload_src_event_default (GstRTPBasePayload * rtpbasepayload,
704     GstEvent * event)
705 {
706   GstObject *parent = GST_OBJECT_CAST (rtpbasepayload);
707   gboolean res = TRUE, forward = TRUE;
708
709   switch (GST_EVENT_TYPE (event)) {
710     case GST_EVENT_CUSTOM_UPSTREAM:
711     {
712       const GstStructure *s = gst_event_get_structure (event);
713
714       if (gst_structure_has_name (s, "GstRTPCollision")) {
715         guint ssrc = 0;
716
717         if (!gst_structure_get_uint (s, "ssrc", &ssrc))
718           ssrc = -1;
719
720         GST_DEBUG_OBJECT (rtpbasepayload, "collided ssrc: %" G_GUINT32_FORMAT,
721             ssrc);
722
723         /* choose another ssrc for our stream */
724         if (ssrc == rtpbasepayload->current_ssrc) {
725           GstCaps *caps;
726           guint suggested_ssrc = 0;
727
728           if (gst_structure_get_uint (s, "suggested-ssrc", &suggested_ssrc))
729             rtpbasepayload->current_ssrc = suggested_ssrc;
730
731           while (ssrc == rtpbasepayload->current_ssrc)
732             rtpbasepayload->current_ssrc = g_random_int ();
733
734           caps = gst_pad_get_current_caps (rtpbasepayload->srcpad);
735           if (caps) {
736             caps = gst_caps_make_writable (caps);
737             gst_caps_set_simple (caps,
738                 "ssrc", G_TYPE_UINT, rtpbasepayload->current_ssrc, NULL);
739             res = gst_pad_set_caps (rtpbasepayload->srcpad, caps);
740             gst_caps_unref (caps);
741           }
742
743           /* the event was for us */
744           forward = FALSE;
745         }
746       }
747       break;
748     }
749     default:
750       break;
751   }
752
753   if (forward)
754     res = gst_pad_event_default (rtpbasepayload->srcpad, parent, event);
755   else
756     gst_event_unref (event);
757
758   return res;
759 }
760
761 static gboolean
762 gst_rtp_base_payload_src_event (GstPad * pad, GstObject * parent,
763     GstEvent * event)
764 {
765   GstRTPBasePayload *rtpbasepayload;
766   GstRTPBasePayloadClass *rtpbasepayload_class;
767   gboolean res = FALSE;
768
769   rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
770   rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
771
772   if (rtpbasepayload_class->src_event)
773     res = rtpbasepayload_class->src_event (rtpbasepayload, event);
774   else
775     gst_event_unref (event);
776
777   return res;
778 }
779
780
781 static gboolean
782 gst_rtp_base_payload_query_default (GstRTPBasePayload * rtpbasepayload,
783     GstPad * pad, GstQuery * query)
784 {
785   gboolean res = FALSE;
786
787   switch (GST_QUERY_TYPE (query)) {
788     case GST_QUERY_CAPS:
789     {
790       GstRTPBasePayloadClass *rtpbasepayload_class;
791       GstCaps *filter, *caps;
792
793       gst_query_parse_caps (query, &filter);
794       GST_DEBUG_OBJECT (rtpbasepayload, "getting caps with filter %"
795           GST_PTR_FORMAT, filter);
796
797       rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
798       if (rtpbasepayload_class->get_caps) {
799         caps = rtpbasepayload_class->get_caps (rtpbasepayload, pad, filter);
800         gst_query_set_caps_result (query, caps);
801         gst_caps_unref (caps);
802         res = TRUE;
803       }
804       break;
805     }
806     default:
807       res =
808           gst_pad_query_default (pad, GST_OBJECT_CAST (rtpbasepayload), query);
809       break;
810   }
811   return res;
812 }
813
814 static gboolean
815 gst_rtp_base_payload_query (GstPad * pad, GstObject * parent, GstQuery * query)
816 {
817   GstRTPBasePayload *rtpbasepayload;
818   GstRTPBasePayloadClass *rtpbasepayload_class;
819   gboolean res = FALSE;
820
821   rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
822   rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
823
824   if (rtpbasepayload_class->query)
825     res = rtpbasepayload_class->query (rtpbasepayload, pad, query);
826
827   return res;
828 }
829
830 static GstFlowReturn
831 gst_rtp_base_payload_chain (GstPad * pad, GstObject * parent,
832     GstBuffer * buffer)
833 {
834   GstRTPBasePayload *rtpbasepayload;
835   GstRTPBasePayloadClass *rtpbasepayload_class;
836   GstFlowReturn ret;
837
838   rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
839   rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
840
841   if (!rtpbasepayload_class->handle_buffer)
842     goto no_function;
843
844   if (!rtpbasepayload->priv->negotiated)
845     goto not_negotiated;
846
847   if (rtpbasepayload->priv->source_info
848       || rtpbasepayload->priv->header_exts->len > 0) {
849     /* Save a copy of meta (instead of taking an extra reference before
850      * handle_buffer) to make the meta available when allocating a output
851      * buffer. */
852     rtpbasepayload->priv->input_meta_buffer = gst_buffer_new ();
853     gst_buffer_copy_into (rtpbasepayload->priv->input_meta_buffer, buffer,
854         GST_BUFFER_COPY_META, 0, -1);
855   }
856
857   if (gst_pad_check_reconfigure (GST_RTP_BASE_PAYLOAD_SRCPAD (rtpbasepayload))) {
858     if (!gst_rtp_base_payload_negotiate (rtpbasepayload)) {
859       gst_pad_mark_reconfigure (GST_RTP_BASE_PAYLOAD_SRCPAD (rtpbasepayload));
860       if (GST_PAD_IS_FLUSHING (GST_RTP_BASE_PAYLOAD_SRCPAD (rtpbasepayload))) {
861         goto flushing;
862       } else {
863         goto negotiate_failed;
864       }
865     }
866   }
867
868   ret = rtpbasepayload_class->handle_buffer (rtpbasepayload, buffer);
869
870   gst_buffer_replace (&rtpbasepayload->priv->input_meta_buffer, NULL);
871
872   return ret;
873
874   /* ERRORS */
875 no_function:
876   {
877     GST_ELEMENT_ERROR (rtpbasepayload, STREAM, NOT_IMPLEMENTED, (NULL),
878         ("subclass did not implement handle_buffer function"));
879     gst_buffer_unref (buffer);
880     return GST_FLOW_ERROR;
881   }
882 not_negotiated:
883   {
884     GST_ELEMENT_ERROR (rtpbasepayload, CORE, NEGOTIATION, (NULL),
885         ("No input format was negotiated, i.e. no caps event was received. "
886             "Perhaps you need a parser or typefind element before the payloader"));
887     gst_buffer_unref (buffer);
888     return GST_FLOW_NOT_NEGOTIATED;
889   }
890 negotiate_failed:
891   {
892     GST_DEBUG_OBJECT (rtpbasepayload, "Not negotiated");
893     gst_buffer_unref (buffer);
894     return GST_FLOW_NOT_NEGOTIATED;
895   }
896 flushing:
897   {
898     GST_DEBUG_OBJECT (rtpbasepayload, "we are flushing");
899     gst_buffer_unref (buffer);
900     return GST_FLOW_FLUSHING;
901   }
902 }
903
904 /**
905  * gst_rtp_base_payload_set_options:
906  * @payload: a #GstRTPBasePayload
907  * @media: the media type (typically "audio" or "video")
908  * @dynamic: if the payload type is dynamic
909  * @encoding_name: the encoding name
910  * @clock_rate: the clock rate of the media
911  *
912  * Set the rtp options of the payloader. These options will be set in the caps
913  * of the payloader. Subclasses must call this method before calling
914  * gst_rtp_base_payload_push() or gst_rtp_base_payload_set_outcaps().
915  */
916 void
917 gst_rtp_base_payload_set_options (GstRTPBasePayload * payload,
918     const gchar * media, gboolean dynamic, const gchar * encoding_name,
919     guint32 clock_rate)
920 {
921   g_return_if_fail (payload != NULL);
922   g_return_if_fail (clock_rate != 0);
923
924   g_free (payload->media);
925   payload->media = g_strdup (media);
926   payload->dynamic = dynamic;
927   g_free (payload->encoding_name);
928   payload->encoding_name = g_strdup (encoding_name);
929   payload->clock_rate = clock_rate;
930 }
931
932 static gboolean
933 copy_fixed (GQuark field_id, const GValue * value, GstStructure * dest)
934 {
935   if (gst_value_is_fixed (value)) {
936     gst_structure_id_set_value (dest, field_id, value);
937   }
938   return TRUE;
939 }
940
941 static void
942 update_max_ptime (GstRTPBasePayload * rtpbasepayload)
943 {
944   if (rtpbasepayload->priv->caps_max_ptime != -1 &&
945       rtpbasepayload->priv->prop_max_ptime != -1)
946     rtpbasepayload->max_ptime = MIN (rtpbasepayload->priv->caps_max_ptime,
947         rtpbasepayload->priv->prop_max_ptime);
948   else if (rtpbasepayload->priv->caps_max_ptime != -1)
949     rtpbasepayload->max_ptime = rtpbasepayload->priv->caps_max_ptime;
950   else if (rtpbasepayload->priv->prop_max_ptime != -1)
951     rtpbasepayload->max_ptime = rtpbasepayload->priv->prop_max_ptime;
952   else
953     rtpbasepayload->max_ptime = DEFAULT_MAX_PTIME;
954 }
955
956 static gboolean
957 _set_caps (GQuark field_id, const GValue * value, GstCaps * caps)
958 {
959   gst_caps_set_value (caps, g_quark_to_string (field_id), value);
960
961   return TRUE;
962 }
963
964 /**
965  * gst_rtp_base_payload_set_outcaps_structure:
966  * @payload: a #GstRTPBasePayload
967  * @s: (nullable): a #GstStructure with the caps fields
968  *
969  * Configure the output caps with the optional fields.
970  *
971  * Returns: %TRUE if the caps could be set.
972  *
973  * Since: 1.20
974  */
975 gboolean
976 gst_rtp_base_payload_set_outcaps_structure (GstRTPBasePayload * payload,
977     GstStructure * s)
978 {
979   GstCaps *srccaps;
980
981   /* fill in the defaults, their properties cannot be negotiated. */
982   srccaps = gst_caps_new_simple ("application/x-rtp",
983       "media", G_TYPE_STRING, payload->media,
984       "clock-rate", G_TYPE_INT, payload->clock_rate,
985       "encoding-name", G_TYPE_STRING, payload->encoding_name, NULL);
986
987   GST_DEBUG_OBJECT (payload, "defaults: %" GST_PTR_FORMAT, srccaps);
988
989   if (s && gst_structure_n_fields (s) > 0) {
990     gst_structure_foreach (s, (GstStructureForeachFunc) _set_caps, srccaps);
991
992     GST_DEBUG_OBJECT (payload, "custom added: %" GST_PTR_FORMAT, srccaps);
993   }
994
995   gst_caps_replace (&payload->priv->subclass_srccaps, srccaps);
996   gst_caps_unref (srccaps);
997
998   return gst_rtp_base_payload_negotiate (payload);
999 }
1000
1001 /**
1002  * gst_rtp_base_payload_set_outcaps:
1003  * @payload: a #GstRTPBasePayload
1004  * @fieldname: the first field name or %NULL
1005  * @...: field values
1006  *
1007  * Configure the output caps with the optional parameters.
1008  *
1009  * Variable arguments should be in the form field name, field type
1010  * (as a GType), value(s).  The last variable argument should be NULL.
1011  *
1012  * Returns: %TRUE if the caps could be set.
1013  */
1014 gboolean
1015 gst_rtp_base_payload_set_outcaps (GstRTPBasePayload * payload,
1016     const gchar * fieldname, ...)
1017 {
1018   gboolean result;
1019   GstStructure *s = NULL;
1020
1021   if (fieldname) {
1022     va_list varargs;
1023
1024     s = gst_structure_new_empty ("unused");
1025
1026     /* override with custom properties */
1027     va_start (varargs, fieldname);
1028     gst_structure_set_valist (s, fieldname, varargs);
1029     va_end (varargs);
1030   }
1031
1032   result = gst_rtp_base_payload_set_outcaps_structure (payload, s);
1033
1034   gst_clear_structure (&s);
1035
1036   return result;
1037 }
1038
1039 static void
1040 add_and_ref_item (GstRTPHeaderExtension * ext, GPtrArray * ret)
1041 {
1042   g_ptr_array_add (ret, gst_object_ref (ext));
1043 }
1044
1045 static void
1046 remove_item_from (GstRTPHeaderExtension * ext, GPtrArray * ret)
1047 {
1048   g_ptr_array_remove_fast (ret, ext);
1049 }
1050
1051 static void
1052 add_item_to (GstRTPHeaderExtension * ext, GPtrArray * ret)
1053 {
1054   g_ptr_array_add (ret, ext);
1055 }
1056
1057 static void
1058 add_header_ext_to_caps (GstRTPHeaderExtension * ext, GstCaps * caps)
1059 {
1060   if (!gst_rtp_header_extension_set_caps_from_attributes (ext, caps)) {
1061     GST_WARNING ("Failed to set caps from rtp header extension");
1062   }
1063 }
1064
1065 static gboolean
1066 gst_rtp_base_payload_negotiate (GstRTPBasePayload * payload)
1067 {
1068   GstCaps *templ, *peercaps, *srccaps;
1069   GstStructure *s, *d;
1070   gboolean res = TRUE;
1071
1072   payload->priv->caps_max_ptime = DEFAULT_MAX_PTIME;
1073   payload->ptime = 0;
1074
1075   gst_pad_check_reconfigure (payload->srcpad);
1076
1077   templ = gst_pad_get_pad_template_caps (payload->srcpad);
1078
1079   if (payload->priv->subclass_srccaps) {
1080     GstCaps *tmp = gst_caps_intersect (payload->priv->subclass_srccaps,
1081         templ);
1082     gst_caps_unref (templ);
1083     templ = tmp;
1084   }
1085
1086   peercaps = gst_pad_peer_query_caps (payload->srcpad, templ);
1087
1088   if (peercaps == NULL) {
1089     /* no peer caps, just add the other properties */
1090
1091     srccaps = gst_caps_copy (templ);
1092     gst_caps_set_simple (srccaps,
1093         "payload", G_TYPE_INT, GST_RTP_BASE_PAYLOAD_PT (payload),
1094         "ssrc", G_TYPE_UINT, payload->current_ssrc,
1095         "timestamp-offset", G_TYPE_UINT, payload->ts_base,
1096         "seqnum-offset", G_TYPE_UINT, payload->seqnum_base, NULL);
1097
1098     GST_DEBUG_OBJECT (payload, "no peer caps: %" GST_PTR_FORMAT, srccaps);
1099   } else {
1100     GstCaps *temp;
1101     const GValue *value;
1102     gboolean have_pt = FALSE;
1103     gboolean have_ts_offset = FALSE;
1104     gboolean have_seqnum_offset = FALSE;
1105     guint max_ptime, ptime;
1106
1107     /* peer provides caps we can use to fixate. They are already intersected
1108      * with our srccaps, just make them writable */
1109     temp = gst_caps_make_writable (peercaps);
1110     peercaps = NULL;
1111
1112     if (gst_caps_is_empty (temp)) {
1113       gst_caps_unref (temp);
1114       gst_caps_unref (templ);
1115       res = FALSE;
1116       goto out;
1117     }
1118
1119     /* We prefer the pt, timestamp-offset, seqnum-offset from the
1120      * property (if set), or any previously configured value over what
1121      * downstream prefers. Only if downstream can't accept that, or the
1122      * properties were not set, we fall back to choosing downstream's
1123      * preferred value
1124      *
1125      * For ssrc we prefer any value downstream suggests, otherwise
1126      * the property value or as a last resort a random value.
1127      * This difference for ssrc is implemented for retaining backwards
1128      * compatibility with changing rtpsession's internal-ssrc property.
1129      *
1130      * FIXME 2.0: All these properties should go away and be negotiated
1131      * via caps only!
1132      */
1133
1134     /* try to use the previously set pt, or the one from the property */
1135     if (payload->priv->pt_set || gst_pad_has_current_caps (payload->srcpad)) {
1136       GstCaps *probe_caps = gst_caps_copy (templ);
1137       GstCaps *intersection;
1138
1139       gst_caps_set_simple (probe_caps, "payload", G_TYPE_INT,
1140           GST_RTP_BASE_PAYLOAD_PT (payload), NULL);
1141       intersection = gst_caps_intersect (probe_caps, temp);
1142
1143       if (!gst_caps_is_empty (intersection)) {
1144         GST_LOG_OBJECT (payload, "Using selected pt %d",
1145             GST_RTP_BASE_PAYLOAD_PT (payload));
1146         have_pt = TRUE;
1147         gst_caps_unref (temp);
1148         temp = intersection;
1149       } else {
1150         GST_WARNING_OBJECT (payload, "Can't use selected pt %d",
1151             GST_RTP_BASE_PAYLOAD_PT (payload));
1152         gst_caps_unref (intersection);
1153       }
1154       gst_caps_unref (probe_caps);
1155     }
1156
1157     /* If we got no pt above, select one now */
1158     if (!have_pt) {
1159       gint pt;
1160
1161       /* get first structure */
1162       s = gst_caps_get_structure (temp, 0);
1163
1164       if (gst_structure_get_int (s, "payload", &pt)) {
1165         /* use peer pt */
1166         GST_RTP_BASE_PAYLOAD_PT (payload) = pt;
1167         GST_LOG_OBJECT (payload, "using peer pt %d", pt);
1168       } else {
1169         if (gst_structure_has_field (s, "payload")) {
1170           /* can only fixate if there is a field */
1171           gst_structure_fixate_field_nearest_int (s, "payload",
1172               GST_RTP_BASE_PAYLOAD_PT (payload));
1173           gst_structure_get_int (s, "payload", &pt);
1174           GST_RTP_BASE_PAYLOAD_PT (payload) = pt;
1175           GST_LOG_OBJECT (payload, "using peer pt %d", pt);
1176         } else {
1177           /* no pt field, use the internal pt */
1178           pt = GST_RTP_BASE_PAYLOAD_PT (payload);
1179           gst_structure_set (s, "payload", G_TYPE_INT, pt, NULL);
1180           GST_LOG_OBJECT (payload, "using internal pt %d", pt);
1181         }
1182       }
1183       s = NULL;
1184     }
1185
1186     /* If we got no ssrc above, select one now */
1187     /* get first structure */
1188     s = gst_caps_get_structure (temp, 0);
1189
1190     if (gst_structure_has_field_typed (s, "ssrc", G_TYPE_UINT)) {
1191       value = gst_structure_get_value (s, "ssrc");
1192       payload->current_ssrc = g_value_get_uint (value);
1193       GST_LOG_OBJECT (payload, "using peer ssrc %08x", payload->current_ssrc);
1194     } else {
1195       /* FIXME, fixate_nearest_uint would be even better but we
1196        * don't support uint ranges so how likely is it that anybody
1197        * uses a list of possible ssrcs */
1198       gst_structure_set (s, "ssrc", G_TYPE_UINT, payload->current_ssrc, NULL);
1199       GST_LOG_OBJECT (payload, "using internal ssrc %08x",
1200           payload->current_ssrc);
1201     }
1202     s = NULL;
1203
1204     /* try to select the previously used timestamp-offset, or the one from the property */
1205     if (!payload->priv->ts_offset_random
1206         || gst_pad_has_current_caps (payload->srcpad)) {
1207       GstCaps *probe_caps = gst_caps_copy (templ);
1208       GstCaps *intersection;
1209
1210       gst_caps_set_simple (probe_caps, "timestamp-offset", G_TYPE_UINT,
1211           payload->ts_base, NULL);
1212       intersection = gst_caps_intersect (probe_caps, temp);
1213
1214       if (!gst_caps_is_empty (intersection)) {
1215         GST_LOG_OBJECT (payload, "Using selected timestamp-offset %u",
1216             payload->ts_base);
1217         gst_caps_unref (temp);
1218         temp = intersection;
1219         have_ts_offset = TRUE;
1220       } else {
1221         GST_WARNING_OBJECT (payload, "Can't use selected timestamp-offset %u",
1222             payload->ts_base);
1223         gst_caps_unref (intersection);
1224       }
1225       gst_caps_unref (probe_caps);
1226     }
1227
1228     /* If we got no timestamp-offset above, select one now */
1229     if (!have_ts_offset) {
1230       /* get first structure */
1231       s = gst_caps_get_structure (temp, 0);
1232
1233       if (gst_structure_has_field_typed (s, "timestamp-offset", G_TYPE_UINT)) {
1234         value = gst_structure_get_value (s, "timestamp-offset");
1235         payload->ts_base = g_value_get_uint (value);
1236         GST_LOG_OBJECT (payload, "using peer timestamp-offset %u",
1237             payload->ts_base);
1238       } else {
1239         /* FIXME, fixate_nearest_uint would be even better but we
1240          * don't support uint ranges so how likely is it that anybody
1241          * uses a list of possible timestamp-offsets */
1242         gst_structure_set (s, "timestamp-offset", G_TYPE_UINT, payload->ts_base,
1243             NULL);
1244         GST_LOG_OBJECT (payload, "using internal timestamp-offset %u",
1245             payload->ts_base);
1246       }
1247       s = NULL;
1248     }
1249
1250     /* try to select the previously used seqnum-offset, or the one from the property */
1251     if (!payload->priv->seqnum_offset_random
1252         || gst_pad_has_current_caps (payload->srcpad)) {
1253       GstCaps *probe_caps = gst_caps_copy (templ);
1254       GstCaps *intersection;
1255
1256       gst_caps_set_simple (probe_caps, "seqnum-offset", G_TYPE_UINT,
1257           payload->seqnum_base, NULL);
1258       intersection = gst_caps_intersect (probe_caps, temp);
1259
1260       if (!gst_caps_is_empty (intersection)) {
1261         GST_LOG_OBJECT (payload, "Using selected seqnum-offset %u",
1262             payload->seqnum_base);
1263         gst_caps_unref (temp);
1264         temp = intersection;
1265         have_seqnum_offset = TRUE;
1266       } else {
1267         GST_WARNING_OBJECT (payload, "Can't use selected seqnum-offset %u",
1268             payload->seqnum_base);
1269         gst_caps_unref (intersection);
1270       }
1271       gst_caps_unref (probe_caps);
1272     }
1273
1274     /* If we got no seqnum-offset above, select one now */
1275     if (!have_seqnum_offset) {
1276       /* get first structure */
1277       s = gst_caps_get_structure (temp, 0);
1278
1279       if (gst_structure_has_field_typed (s, "seqnum-offset", G_TYPE_UINT)) {
1280         value = gst_structure_get_value (s, "seqnum-offset");
1281         payload->seqnum_base = g_value_get_uint (value);
1282         GST_LOG_OBJECT (payload, "using peer seqnum-offset %u",
1283             payload->seqnum_base);
1284         payload->priv->next_seqnum = payload->seqnum_base;
1285         payload->seqnum = payload->seqnum_base;
1286         payload->priv->seqnum_offset_random = FALSE;
1287       } else {
1288         /* FIXME, fixate_nearest_uint would be even better but we
1289          * don't support uint ranges so how likely is it that anybody
1290          * uses a list of possible seqnum-offsets */
1291         gst_structure_set (s, "seqnum-offset", G_TYPE_UINT,
1292             payload->seqnum_base, NULL);
1293         GST_LOG_OBJECT (payload, "using internal seqnum-offset %u",
1294             payload->seqnum_base);
1295       }
1296
1297       s = NULL;
1298     }
1299
1300     /* now fixate, start by taking the first caps */
1301     temp = gst_caps_truncate (temp);
1302
1303     /* get first structure */
1304     s = gst_caps_get_structure (temp, 0);
1305
1306     if (gst_structure_get_uint (s, "maxptime", &max_ptime))
1307       payload->priv->caps_max_ptime = max_ptime * GST_MSECOND;
1308
1309     if (gst_structure_get_uint (s, "ptime", &ptime))
1310       payload->ptime = ptime * GST_MSECOND;
1311
1312     /* make the target caps by copying over all the fixed fields, removing the
1313      * unfixed fields. */
1314     srccaps = gst_caps_new_empty_simple (gst_structure_get_name (s));
1315     d = gst_caps_get_structure (srccaps, 0);
1316
1317     gst_structure_foreach (s, (GstStructureForeachFunc) copy_fixed, d);
1318
1319     gst_caps_unref (temp);
1320
1321     GST_DEBUG_OBJECT (payload, "with peer caps: %" GST_PTR_FORMAT, srccaps);
1322   }
1323
1324   if (payload->priv->sinkcaps != NULL) {
1325     s = gst_caps_get_structure (payload->priv->sinkcaps, 0);
1326     if (g_str_has_prefix (gst_structure_get_name (s), "video")) {
1327       gboolean has_framerate;
1328       gint num, denom;
1329
1330       GST_DEBUG_OBJECT (payload, "video caps: %" GST_PTR_FORMAT,
1331           payload->priv->sinkcaps);
1332
1333       has_framerate = gst_structure_get_fraction (s, "framerate", &num, &denom);
1334       if (has_framerate && num == 0 && denom == 1) {
1335         has_framerate =
1336             gst_structure_get_fraction (s, "max-framerate", &num, &denom);
1337       }
1338
1339       if (has_framerate) {
1340         gchar str[G_ASCII_DTOSTR_BUF_SIZE];
1341         gdouble framerate;
1342
1343         gst_util_fraction_to_double (num, denom, &framerate);
1344         g_ascii_dtostr (str, G_ASCII_DTOSTR_BUF_SIZE, framerate);
1345         d = gst_caps_get_structure (srccaps, 0);
1346         gst_structure_set (d, "a-framerate", G_TYPE_STRING, str, NULL);
1347       }
1348
1349       GST_DEBUG_OBJECT (payload, "with video caps: %" GST_PTR_FORMAT, srccaps);
1350     }
1351   }
1352
1353   update_max_ptime (payload);
1354
1355   {
1356     /* try to find header extension implementations for the list in the
1357      * caps */
1358     GstStructure *s = gst_caps_get_structure (srccaps, 0);
1359     guint i, j, n_fields = gst_structure_n_fields (s);
1360     GPtrArray *header_exts = g_ptr_array_new_with_free_func (gst_object_unref);
1361     GPtrArray *to_add = g_ptr_array_new ();
1362     GPtrArray *to_remove = g_ptr_array_new ();
1363
1364     GST_OBJECT_LOCK (payload);
1365     g_ptr_array_foreach (payload->priv->header_exts,
1366         (GFunc) add_and_ref_item, header_exts);
1367     GST_OBJECT_UNLOCK (payload);
1368
1369     for (i = 0; i < n_fields; i++) {
1370       const gchar *field_name = gst_structure_nth_field_name (s, i);
1371       if (g_str_has_prefix (field_name, "extmap-")) {
1372         const GValue *val;
1373         const gchar *uri = NULL;
1374         gchar *nptr;
1375         guint64 ext_id;
1376         GstRTPHeaderExtension *ext = NULL;
1377
1378         errno = 0;
1379         ext_id = g_ascii_strtoull (&field_name[strlen ("extmap-")], &nptr, 10);
1380         if (errno != 0 || (ext_id == 0 && field_name == nptr)) {
1381           GST_WARNING_OBJECT (payload, "could not parse id from %s",
1382               field_name);
1383           res = FALSE;
1384           goto ext_out;
1385         }
1386
1387         val = gst_structure_get_value (s, field_name);
1388         if (G_VALUE_HOLDS_STRING (val)) {
1389           uri = g_value_get_string (val);
1390         } else if (GST_VALUE_HOLDS_ARRAY (val)) {
1391           /* the uri is the second value in the array */
1392           const GValue *str = gst_value_array_get_value (val, 1);
1393           if (G_VALUE_HOLDS_STRING (str)) {
1394             uri = g_value_get_string (str);
1395           }
1396         }
1397
1398         if (!uri) {
1399           GST_WARNING_OBJECT (payload, "could not get extmap uri for "
1400               "field %s", field_name);
1401           res = FALSE;
1402           goto ext_out;
1403         }
1404
1405         /* try to find if this extension mapping already exists */
1406         for (j = 0; j < header_exts->len; j++) {
1407           ext = g_ptr_array_index (header_exts, j);
1408           if (gst_rtp_header_extension_get_id (ext) == ext_id) {
1409             if (g_strcmp0 (uri, gst_rtp_header_extension_get_uri (ext)) == 0) {
1410               /* still matching, we're good, set attributes from caps in case
1411                * the caps have been updated */
1412               if (!gst_rtp_header_extension_set_attributes_from_caps (ext,
1413                       srccaps)) {
1414                 GST_WARNING_OBJECT (payload,
1415                     "Failed to configure rtp header " "extension %"
1416                     GST_PTR_FORMAT " attributes from caps %" GST_PTR_FORMAT,
1417                     ext, srccaps);
1418                 res = FALSE;
1419                 goto ext_out;
1420               }
1421               break;
1422             } else {
1423               GST_DEBUG_OBJECT (payload, "extension id %" G_GUINT64_FORMAT
1424                   "was replaced with a different extension uri "
1425                   "original:\'%s' vs \'%s\'", ext_id,
1426                   gst_rtp_header_extension_get_uri (ext), uri);
1427               g_ptr_array_add (to_remove, ext);
1428               ext = NULL;
1429               break;
1430             }
1431           } else {
1432             ext = NULL;
1433           }
1434         }
1435
1436         /* if no extension, attempt to request one */
1437         if (!ext) {
1438           GST_DEBUG_OBJECT (payload, "requesting extension for id %"
1439               G_GUINT64_FORMAT " and uri %s", ext_id, uri);
1440           g_signal_emit (payload,
1441               gst_rtp_base_payload_signals[SIGNAL_REQUEST_EXTENSION], 0,
1442               ext_id, uri, &ext);
1443           GST_DEBUG_OBJECT (payload, "request returned extension %p \'%s\' "
1444               "for id %" G_GUINT64_FORMAT " and uri %s", ext,
1445               ext ? GST_OBJECT_NAME (ext) : "", ext_id, uri);
1446
1447           /* We require caller to set the appropriate extension if it's required */
1448           if (ext && gst_rtp_header_extension_get_id (ext) != ext_id) {
1449             g_warning ("\'request-extension\' signal provided an rtp header "
1450                 "extension for uri \'%s\' that does not match the requested "
1451                 "extension id %" G_GUINT64_FORMAT, uri, ext_id);
1452             gst_clear_object (&ext);
1453           }
1454
1455           if (ext && !gst_rtp_header_extension_set_attributes_from_caps (ext,
1456                   srccaps)) {
1457             GST_WARNING_OBJECT (payload,
1458                 "Failed to configure rtp header " "extension %"
1459                 GST_PTR_FORMAT " attributes from caps %" GST_PTR_FORMAT,
1460                 ext, srccaps);
1461             res = FALSE;
1462             g_clear_object (&ext);
1463             goto ext_out;
1464           }
1465
1466           if (ext) {
1467             g_ptr_array_add (to_add, ext);
1468           }
1469         }
1470       }
1471     }
1472
1473     GST_OBJECT_LOCK (payload);
1474     g_ptr_array_foreach (to_remove, (GFunc) remove_item_from,
1475         payload->priv->header_exts);
1476     g_ptr_array_foreach (to_add, (GFunc) add_item_to,
1477         payload->priv->header_exts);
1478     /* add extension information to srccaps */
1479     g_ptr_array_foreach (payload->priv->header_exts,
1480         (GFunc) add_header_ext_to_caps, srccaps);
1481     GST_OBJECT_UNLOCK (payload);
1482
1483   ext_out:
1484     g_ptr_array_unref (to_add);
1485     g_ptr_array_unref (to_remove);
1486     g_ptr_array_unref (header_exts);
1487   }
1488
1489   GST_DEBUG_OBJECT (payload, "configuring caps %" GST_PTR_FORMAT, srccaps);
1490
1491   if (res)
1492     res = gst_pad_set_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload), srccaps);
1493   gst_caps_unref (srccaps);
1494   gst_caps_unref (templ);
1495
1496 out:
1497
1498   if (!res)
1499     gst_pad_mark_reconfigure (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
1500
1501   return res;
1502 }
1503
1504 /**
1505  * gst_rtp_base_payload_is_filled:
1506  * @payload: a #GstRTPBasePayload
1507  * @size: the size of the packet
1508  * @duration: the duration of the packet
1509  *
1510  * Check if the packet with @size and @duration would exceed the configured
1511  * maximum size.
1512  *
1513  * Returns: %TRUE if the packet of @size and @duration would exceed the
1514  * configured MTU or max_ptime.
1515  */
1516 gboolean
1517 gst_rtp_base_payload_is_filled (GstRTPBasePayload * payload,
1518     guint size, GstClockTime duration)
1519 {
1520   if (size > payload->mtu)
1521     return TRUE;
1522
1523   if (payload->max_ptime != -1 && duration >= payload->max_ptime)
1524     return TRUE;
1525
1526   return FALSE;
1527 }
1528
1529 typedef struct
1530 {
1531   GstRTPBasePayload *payload;
1532   guint32 ssrc;
1533   guint16 seqnum;
1534   guint8 pt;
1535   GstClockTime dts;
1536   GstClockTime pts;
1537   guint64 offset;
1538   guint32 rtptime;
1539 } HeaderData;
1540
1541 static gboolean
1542 find_timestamp (GstBuffer ** buffer, guint idx, gpointer user_data)
1543 {
1544   HeaderData *data = user_data;
1545   data->dts = GST_BUFFER_DTS (*buffer);
1546   data->pts = GST_BUFFER_PTS (*buffer);
1547   data->offset = GST_BUFFER_OFFSET (*buffer);
1548
1549   /* stop when we find a timestamp. We take whatever offset is associated with
1550    * the timestamp (if any) to do perfect timestamps when we need to. */
1551   if (data->pts != -1)
1552     return FALSE;
1553   else
1554     return TRUE;
1555 }
1556
1557 static void
1558 gst_rtp_base_payload_add_extension (GstRTPBasePayload * payload,
1559     GstRTPHeaderExtension * ext)
1560 {
1561   g_return_if_fail (GST_IS_RTP_HEADER_EXTENSION (ext));
1562   g_return_if_fail (gst_rtp_header_extension_get_id (ext) > 0);
1563
1564   /* XXX: check for duplicate ids? */
1565   GST_OBJECT_LOCK (payload);
1566   g_ptr_array_add (payload->priv->header_exts, gst_object_ref (ext));
1567   gst_pad_mark_reconfigure (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
1568   GST_OBJECT_UNLOCK (payload);
1569 }
1570
1571 static void
1572 gst_rtp_base_payload_clear_extensions (GstRTPBasePayload * payload)
1573 {
1574   GST_OBJECT_LOCK (payload);
1575   g_ptr_array_set_size (payload->priv->header_exts, 0);
1576   GST_OBJECT_UNLOCK (payload);
1577 }
1578
1579 typedef struct
1580 {
1581   GstRTPBasePayload *payload;
1582   GstRTPHeaderExtensionFlags flags;
1583   GstBuffer *output;
1584   guint8 *data;
1585   gsize allocated_size;
1586   gsize written_size;
1587   gsize hdr_unit_size;
1588   gboolean abort;
1589 } HeaderExt;
1590
1591 static void
1592 determine_header_extension_flags_size (GstRTPHeaderExtension * ext,
1593     gpointer user_data)
1594 {
1595   HeaderExt *hdr = user_data;
1596   guint ext_id;
1597   gsize max_size;
1598
1599   hdr->flags &= gst_rtp_header_extension_get_supported_flags (ext);
1600   max_size =
1601       gst_rtp_header_extension_get_max_size (ext,
1602       hdr->payload->priv->input_meta_buffer);
1603
1604   if (max_size > RTP_HEADER_EXT_ONE_BYTE_MAX_SIZE)
1605     hdr->flags &= ~GST_RTP_HEADER_EXTENSION_ONE_BYTE;
1606   if (max_size > RTP_HEADER_EXT_TWO_BYTE_MAX_SIZE)
1607     hdr->flags &= ~GST_RTP_HEADER_EXTENSION_TWO_BYTE;
1608
1609   ext_id = gst_rtp_header_extension_get_id (ext);
1610   if (ext_id > RTP_HEADER_EXT_ONE_BYTE_MAX_ID)
1611     hdr->flags &= ~GST_RTP_HEADER_EXTENSION_ONE_BYTE;
1612   if (ext_id > RTP_HEADER_EXT_TWO_BYTE_MAX_ID)
1613     hdr->flags &= ~GST_RTP_HEADER_EXTENSION_TWO_BYTE;
1614
1615   hdr->allocated_size += max_size;
1616 }
1617
1618 static void
1619 write_header_extension (GstRTPHeaderExtension * ext, gpointer user_data)
1620 {
1621   HeaderExt *hdr = user_data;
1622   gsize remaining =
1623       hdr->allocated_size - hdr->written_size - hdr->hdr_unit_size;
1624   gsize offset = hdr->written_size + hdr->hdr_unit_size;
1625   gsize written;
1626   guint ext_id;
1627
1628   if (hdr->abort)
1629     return;
1630
1631   written = gst_rtp_header_extension_write (ext,
1632       hdr->payload->priv->input_meta_buffer, hdr->flags, hdr->output,
1633       &hdr->data[offset], remaining);
1634
1635   if (written == 0) {
1636     /* extension wrote no data */
1637     return;
1638   } else if (written < 0) {
1639     GST_WARNING_OBJECT (hdr->payload, "%s failed to write extension data",
1640         GST_OBJECT_NAME (ext));
1641     goto error;
1642   } else if (written > remaining) {
1643     /* wrote too much! */
1644     g_error ("Overflow detected writing rtp header extensions. One of the "
1645         "instances likely did not report a large enough maximum size. "
1646         "Memory corruption has occured. Aborting");
1647     goto error;
1648   }
1649
1650   ext_id = gst_rtp_header_extension_get_id (ext);
1651
1652   /* move to the beginning of the extension header */
1653   offset -= hdr->hdr_unit_size;
1654
1655   /* write extension header */
1656   if (hdr->flags & GST_RTP_HEADER_EXTENSION_ONE_BYTE) {
1657     if (written > RTP_HEADER_EXT_ONE_BYTE_MAX_SIZE) {
1658       g_critical ("Amount of data written by %s is larger than allowed with "
1659           "a one byte header.", GST_OBJECT_NAME (ext));
1660       goto error;
1661     }
1662
1663     hdr->data[offset] = ((ext_id & 0x0F) << 4) | ((written - 1) & 0x0F);
1664   } else if (hdr->flags & GST_RTP_HEADER_EXTENSION_TWO_BYTE) {
1665     if (written > RTP_HEADER_EXT_TWO_BYTE_MAX_SIZE) {
1666       g_critical ("Amount of data written by %s is larger than allowed with "
1667           "a two byte header.", GST_OBJECT_NAME (ext));
1668       goto error;
1669     }
1670
1671     hdr->data[offset] = ext_id & 0xFF;
1672     hdr->data[offset + 1] = written & 0xFF;
1673   } else {
1674     g_critical ("Don't know how to write extension data with flags 0x%x!",
1675         hdr->flags);
1676     goto error;
1677   }
1678
1679   hdr->written_size += written + hdr->hdr_unit_size;
1680
1681   return;
1682
1683 error:
1684   hdr->abort = TRUE;
1685   return;
1686 }
1687
1688 static gboolean
1689 set_headers (GstBuffer ** buffer, guint idx, gpointer user_data)
1690 {
1691   HeaderData *data = user_data;
1692   HeaderExt hdrext = { NULL, };
1693   GstRTPBuffer rtp = { NULL, };
1694
1695   if (!gst_rtp_buffer_map (*buffer, GST_MAP_WRITE, &rtp))
1696     goto map_failed;
1697
1698   gst_rtp_buffer_set_ssrc (&rtp, data->ssrc);
1699   gst_rtp_buffer_set_payload_type (&rtp, data->pt);
1700   gst_rtp_buffer_set_seq (&rtp, data->seqnum);
1701   gst_rtp_buffer_set_timestamp (&rtp, data->rtptime);
1702
1703   GST_OBJECT_LOCK (data->payload);
1704   if (data->payload->priv->header_exts->len > 0) {
1705     guint wordlen;
1706     gsize extlen;
1707     guint16 bit_pattern;
1708
1709     /* write header extensions */
1710     hdrext.payload = data->payload;
1711     hdrext.output = *buffer;
1712     /* XXX: pre-calculate these flags and sizes? */
1713     hdrext.flags =
1714         GST_RTP_HEADER_EXTENSION_ONE_BYTE | GST_RTP_HEADER_EXTENSION_TWO_BYTE;
1715     g_ptr_array_foreach (data->payload->priv->header_exts,
1716         (GFunc) determine_header_extension_flags_size, &hdrext);
1717     hdrext.hdr_unit_size = 0;
1718     if (hdrext.flags & GST_RTP_HEADER_EXTENSION_ONE_BYTE) {
1719       /* prefer the one byte header */
1720       hdrext.hdr_unit_size = 1;
1721       /* TODO: support mixed size writing modes, i.e. RFC8285 */
1722       hdrext.flags &= ~GST_RTP_HEADER_EXTENSION_TWO_BYTE;
1723       bit_pattern = 0xBEDE;
1724     } else if (hdrext.flags & GST_RTP_HEADER_EXTENSION_TWO_BYTE) {
1725       hdrext.hdr_unit_size = 2;
1726       bit_pattern = 0x1000;
1727     } else {
1728       goto unsupported_flags;
1729     }
1730
1731     extlen =
1732         hdrext.hdr_unit_size * data->payload->priv->header_exts->len +
1733         hdrext.allocated_size;
1734     wordlen = extlen / 4 + ((extlen % 4) ? 1 : 0);
1735
1736     /* XXX: do we need to add to any existing extension data instead of
1737      * overwriting everything? */
1738     gst_rtp_buffer_set_extension_data (&rtp, bit_pattern, wordlen);
1739     gst_rtp_buffer_get_extension_data (&rtp, NULL, (gpointer) & hdrext.data,
1740         &wordlen);
1741
1742     /* from 32-bit words to bytes */
1743     hdrext.allocated_size = wordlen * 4;
1744
1745     g_ptr_array_foreach (data->payload->priv->header_exts,
1746         (GFunc) write_header_extension, &hdrext);
1747
1748     wordlen = hdrext.written_size / 4 + ((hdrext.written_size % 4) ? 1 : 0);
1749     gst_rtp_buffer_set_extension_data (&rtp, bit_pattern, wordlen);
1750   }
1751   GST_OBJECT_UNLOCK (data->payload);
1752   gst_rtp_buffer_unmap (&rtp);
1753
1754   /* increment the seqnum for each buffer */
1755   data->seqnum++;
1756
1757   return TRUE;
1758   /* ERRORS */
1759 map_failed:
1760   {
1761     GST_ERROR ("failed to map buffer %p", *buffer);
1762     return FALSE;
1763   }
1764
1765 unsupported_flags:
1766   {
1767     GST_OBJECT_UNLOCK (data->payload);
1768     gst_rtp_buffer_unmap (&rtp);
1769     GST_ERROR ("Cannot add rtp header extensions with mixed header types");
1770     return FALSE;
1771   }
1772 }
1773
1774 static gboolean
1775 foreach_metadata_drop (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
1776 {
1777   GType drop_api_type = (GType) user_data;
1778   const GstMetaInfo *info = (*meta)->info;
1779
1780   if (info->api == drop_api_type)
1781     *meta = NULL;
1782
1783   return TRUE;
1784 }
1785
1786 static gboolean
1787 filter_meta (GstBuffer ** buffer, guint idx, gpointer user_data)
1788 {
1789   return gst_buffer_foreach_meta (*buffer, foreach_metadata_drop,
1790       (gpointer) GST_RTP_SOURCE_META_API_TYPE);
1791 }
1792
1793 /* Updates the SSRC, payload type, seqnum and timestamp of the RTP buffer
1794  * before the buffer is pushed. */
1795 static GstFlowReturn
1796 gst_rtp_base_payload_prepare_push (GstRTPBasePayload * payload,
1797     gpointer obj, gboolean is_list)
1798 {
1799   GstRTPBasePayloadPrivate *priv;
1800   HeaderData data;
1801
1802   if (payload->clock_rate == 0)
1803     goto no_rate;
1804
1805   priv = payload->priv;
1806
1807   /* update first, so that the property is set to the last
1808    * seqnum pushed */
1809   payload->seqnum = priv->next_seqnum;
1810
1811   /* fill in the fields we want to set on all headers */
1812   data.payload = payload;
1813   data.seqnum = payload->seqnum;
1814   data.ssrc = payload->current_ssrc;
1815   data.pt = payload->pt;
1816
1817   /* find the first buffer with a timestamp */
1818   if (is_list) {
1819     data.dts = -1;
1820     data.pts = -1;
1821     data.offset = GST_BUFFER_OFFSET_NONE;
1822     gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj), find_timestamp, &data);
1823   } else {
1824     data.dts = GST_BUFFER_DTS (GST_BUFFER_CAST (obj));
1825     data.pts = GST_BUFFER_PTS (GST_BUFFER_CAST (obj));
1826     data.offset = GST_BUFFER_OFFSET (GST_BUFFER_CAST (obj));
1827   }
1828
1829   /* convert to RTP time */
1830   if (priv->perfect_rtptime && data.offset != GST_BUFFER_OFFSET_NONE &&
1831       priv->base_offset != GST_BUFFER_OFFSET_NONE) {
1832     /* generate perfect RTP time by adding together the base timestamp, the
1833      * running time of the first buffer and difference between the offset of the
1834      * first buffer and the offset of the current buffer. */
1835     guint64 offset = data.offset - priv->base_offset;
1836     data.rtptime = payload->ts_base + priv->base_rtime_hz + offset;
1837
1838     GST_LOG_OBJECT (payload,
1839         "Using offset %" G_GUINT64_FORMAT " for RTP timestamp", data.offset);
1840
1841     /* store buffer's running time */
1842     GST_LOG_OBJECT (payload,
1843         "setting running-time to %" G_GUINT64_FORMAT,
1844         data.offset - priv->base_offset);
1845     priv->running_time = priv->base_rtime + data.offset - priv->base_offset;
1846   } else if (GST_CLOCK_TIME_IS_VALID (data.pts)) {
1847     guint64 rtime_ns;
1848     guint64 rtime_hz;
1849
1850     /* no offset, use the gstreamer pts */
1851     if (priv->onvif_no_rate_control || !priv->scale_rtptime)
1852       rtime_ns = gst_segment_to_stream_time (&payload->segment,
1853           GST_FORMAT_TIME, data.pts);
1854     else
1855       rtime_ns =
1856           gst_segment_to_running_time (&payload->segment, GST_FORMAT_TIME,
1857           data.pts);
1858
1859     if (!GST_CLOCK_TIME_IS_VALID (rtime_ns)) {
1860       GST_LOG_OBJECT (payload, "Clipped pts, using base RTP timestamp");
1861       rtime_hz = 0;
1862     } else {
1863       GST_LOG_OBJECT (payload,
1864           "Using running_time %" GST_TIME_FORMAT " for RTP timestamp",
1865           GST_TIME_ARGS (rtime_ns));
1866       rtime_hz =
1867           gst_util_uint64_scale_int (rtime_ns, payload->clock_rate, GST_SECOND);
1868       priv->base_offset = data.offset;
1869       priv->base_rtime_hz = rtime_hz;
1870     }
1871
1872     /* add running_time in clock-rate units to the base timestamp */
1873     data.rtptime = payload->ts_base + rtime_hz;
1874
1875     /* store buffer's running time */
1876     if (priv->perfect_rtptime) {
1877       GST_LOG_OBJECT (payload,
1878           "setting running-time to %" G_GUINT64_FORMAT, rtime_hz);
1879       priv->running_time = rtime_hz;
1880     } else {
1881       GST_LOG_OBJECT (payload,
1882           "setting running-time to %" GST_TIME_FORMAT,
1883           GST_TIME_ARGS (rtime_ns));
1884       priv->running_time = rtime_ns;
1885     }
1886   } else {
1887     GST_LOG_OBJECT (payload,
1888         "Using previous RTP timestamp %" G_GUINT32_FORMAT, payload->timestamp);
1889     /* no timestamp to convert, take previous timestamp */
1890     data.rtptime = payload->timestamp;
1891   }
1892
1893   /* set ssrc, payload type, seq number, caps and rtptime */
1894   /* remove unwanted meta */
1895   if (is_list) {
1896     gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj), set_headers, &data);
1897     gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj), filter_meta, NULL);
1898     /* sequence number has increased more if this was a buffer list */
1899     payload->seqnum = data.seqnum - 1;
1900   } else {
1901     GstBuffer *buf = GST_BUFFER_CAST (obj);
1902     set_headers (&buf, 0, &data);
1903     filter_meta (&buf, 0, NULL);
1904   }
1905
1906   priv->next_seqnum = data.seqnum;
1907   payload->timestamp = data.rtptime;
1908
1909   GST_LOG_OBJECT (payload, "Preparing to push %s with size %"
1910       G_GSIZE_FORMAT ", seq=%d, rtptime=%u, pts %" GST_TIME_FORMAT,
1911       (is_list) ? "list" : "packet",
1912       (is_list) ? gst_buffer_list_length (GST_BUFFER_LIST_CAST (obj)) :
1913       gst_buffer_get_size (GST_BUFFER (obj)),
1914       payload->seqnum, data.rtptime, GST_TIME_ARGS (data.pts));
1915
1916   if (g_atomic_int_compare_and_exchange (&payload->priv->
1917           notified_first_timestamp, 1, 0)) {
1918     g_object_notify (G_OBJECT (payload), "timestamp");
1919     g_object_notify (G_OBJECT (payload), "seqnum");
1920   }
1921
1922   return GST_FLOW_OK;
1923
1924   /* ERRORS */
1925 no_rate:
1926   {
1927     GST_ELEMENT_ERROR (payload, STREAM, NOT_IMPLEMENTED, (NULL),
1928         ("subclass did not specify clock-rate"));
1929     return GST_FLOW_ERROR;
1930   }
1931 }
1932
1933 /**
1934  * gst_rtp_base_payload_push_list:
1935  * @payload: a #GstRTPBasePayload
1936  * @list: a #GstBufferList
1937  *
1938  * Push @list to the peer element of the payloader. The SSRC, payload type,
1939  * seqnum and timestamp of the RTP buffer will be updated first.
1940  *
1941  * This function takes ownership of @list.
1942  *
1943  * Returns: a #GstFlowReturn.
1944  */
1945 GstFlowReturn
1946 gst_rtp_base_payload_push_list (GstRTPBasePayload * payload,
1947     GstBufferList * list)
1948 {
1949   GstFlowReturn res;
1950
1951   res = gst_rtp_base_payload_prepare_push (payload, list, TRUE);
1952
1953   if (G_LIKELY (res == GST_FLOW_OK)) {
1954     if (G_UNLIKELY (payload->priv->pending_segment)) {
1955       gst_pad_push_event (payload->srcpad, payload->priv->pending_segment);
1956       payload->priv->pending_segment = FALSE;
1957       payload->priv->delay_segment = FALSE;
1958     }
1959     res = gst_pad_push_list (payload->srcpad, list);
1960   } else {
1961     gst_buffer_list_unref (list);
1962   }
1963
1964   return res;
1965 }
1966
1967 /**
1968  * gst_rtp_base_payload_push:
1969  * @payload: a #GstRTPBasePayload
1970  * @buffer: a #GstBuffer
1971  *
1972  * Push @buffer to the peer element of the payloader. The SSRC, payload type,
1973  * seqnum and timestamp of the RTP buffer will be updated first.
1974  *
1975  * This function takes ownership of @buffer.
1976  *
1977  * Returns: a #GstFlowReturn.
1978  */
1979 GstFlowReturn
1980 gst_rtp_base_payload_push (GstRTPBasePayload * payload, GstBuffer * buffer)
1981 {
1982   GstFlowReturn res;
1983
1984   res = gst_rtp_base_payload_prepare_push (payload, buffer, FALSE);
1985
1986   if (G_LIKELY (res == GST_FLOW_OK)) {
1987     if (G_UNLIKELY (payload->priv->pending_segment)) {
1988       gst_pad_push_event (payload->srcpad, payload->priv->pending_segment);
1989       payload->priv->pending_segment = FALSE;
1990       payload->priv->delay_segment = FALSE;
1991     }
1992     res = gst_pad_push (payload->srcpad, buffer);
1993   } else {
1994     gst_buffer_unref (buffer);
1995   }
1996
1997   return res;
1998 }
1999
2000 /**
2001  * gst_rtp_base_payload_allocate_output_buffer:
2002  * @payload: a #GstRTPBasePayload
2003  * @payload_len: the length of the payload
2004  * @pad_len: the amount of padding
2005  * @csrc_count: the minimum number of CSRC entries
2006  *
2007  * Allocate a new #GstBuffer with enough data to hold an RTP packet with
2008  * minimum @csrc_count CSRCs, a payload length of @payload_len and padding of
2009  * @pad_len. If @payload has #GstRTPBasePayload:source-info %TRUE additional
2010  * CSRCs may be allocated and filled with RTP source information.
2011  *
2012  * Returns: A newly allocated buffer that can hold an RTP packet with given
2013  * parameters.
2014  *
2015  * Since: 1.16
2016  */
2017 GstBuffer *
2018 gst_rtp_base_payload_allocate_output_buffer (GstRTPBasePayload * payload,
2019     guint payload_len, guint8 pad_len, guint8 csrc_count)
2020 {
2021   GstBuffer *buffer = NULL;
2022
2023   if (payload->priv->input_meta_buffer != NULL) {
2024     GstRTPSourceMeta *meta =
2025         gst_buffer_get_rtp_source_meta (payload->priv->input_meta_buffer);
2026     if (meta != NULL) {
2027       guint total_csrc_count, idx, i;
2028       GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
2029
2030       total_csrc_count = csrc_count + meta->csrc_count +
2031           (meta->ssrc_valid ? 1 : 0);
2032       total_csrc_count = MIN (total_csrc_count, 15);
2033       buffer = gst_rtp_buffer_new_allocate (payload_len, pad_len,
2034           total_csrc_count);
2035
2036       gst_rtp_buffer_map (buffer, GST_MAP_READWRITE, &rtp);
2037
2038       /* Skip CSRC fields requested by derived class and fill CSRCs from meta.
2039        * Finally append the SSRC as a new CSRC. */
2040       idx = csrc_count;
2041       for (i = 0; i < meta->csrc_count && idx < 15; i++, idx++)
2042         gst_rtp_buffer_set_csrc (&rtp, idx, meta->csrc[i]);
2043       if (meta->ssrc_valid && idx < 15)
2044         gst_rtp_buffer_set_csrc (&rtp, idx, meta->ssrc);
2045
2046       gst_rtp_buffer_unmap (&rtp);
2047     }
2048   }
2049
2050   if (buffer == NULL)
2051     buffer = gst_rtp_buffer_new_allocate (payload_len, pad_len, csrc_count);
2052
2053   return buffer;
2054 }
2055
2056 static GstStructure *
2057 gst_rtp_base_payload_create_stats (GstRTPBasePayload * rtpbasepayload)
2058 {
2059   GstRTPBasePayloadPrivate *priv;
2060   GstStructure *s;
2061
2062   priv = rtpbasepayload->priv;
2063
2064   s = gst_structure_new ("application/x-rtp-payload-stats",
2065       "clock-rate", G_TYPE_UINT, (guint) rtpbasepayload->clock_rate,
2066       "running-time", G_TYPE_UINT64, priv->running_time,
2067       "seqnum", G_TYPE_UINT, (guint) rtpbasepayload->seqnum,
2068       "timestamp", G_TYPE_UINT, (guint) rtpbasepayload->timestamp,
2069       "ssrc", G_TYPE_UINT, rtpbasepayload->current_ssrc,
2070       "pt", G_TYPE_UINT, rtpbasepayload->pt,
2071       "seqnum-offset", G_TYPE_UINT, (guint) rtpbasepayload->seqnum_base,
2072       "timestamp-offset", G_TYPE_UINT, (guint) rtpbasepayload->ts_base, NULL);
2073
2074   return s;
2075 }
2076
2077 static void
2078 gst_rtp_base_payload_set_property (GObject * object, guint prop_id,
2079     const GValue * value, GParamSpec * pspec)
2080 {
2081   GstRTPBasePayload *rtpbasepayload;
2082   GstRTPBasePayloadPrivate *priv;
2083   gint64 val;
2084
2085   rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
2086   priv = rtpbasepayload->priv;
2087
2088   switch (prop_id) {
2089     case PROP_MTU:
2090       rtpbasepayload->mtu = g_value_get_uint (value);
2091       break;
2092     case PROP_PT:
2093       rtpbasepayload->pt = g_value_get_uint (value);
2094       priv->pt_set = TRUE;
2095       break;
2096     case PROP_SSRC:
2097       val = g_value_get_uint (value);
2098       rtpbasepayload->ssrc = val;
2099       priv->ssrc_random = FALSE;
2100       break;
2101     case PROP_TIMESTAMP_OFFSET:
2102       val = g_value_get_uint (value);
2103       rtpbasepayload->ts_offset = val;
2104       priv->ts_offset_random = FALSE;
2105       break;
2106     case PROP_SEQNUM_OFFSET:
2107       val = g_value_get_int (value);
2108       rtpbasepayload->seqnum_offset = val;
2109       priv->seqnum_offset_random = (val == -1);
2110       GST_DEBUG_OBJECT (rtpbasepayload, "seqnum offset 0x%04x, random %d",
2111           rtpbasepayload->seqnum_offset, priv->seqnum_offset_random);
2112       break;
2113     case PROP_MAX_PTIME:
2114       rtpbasepayload->priv->prop_max_ptime = g_value_get_int64 (value);
2115       update_max_ptime (rtpbasepayload);
2116       break;
2117     case PROP_MIN_PTIME:
2118       rtpbasepayload->min_ptime = g_value_get_int64 (value);
2119       break;
2120     case PROP_PERFECT_RTPTIME:
2121       priv->perfect_rtptime = g_value_get_boolean (value);
2122       break;
2123     case PROP_PTIME_MULTIPLE:
2124       rtpbasepayload->ptime_multiple = g_value_get_int64 (value);
2125       break;
2126     case PROP_SOURCE_INFO:
2127       gst_rtp_base_payload_set_source_info_enabled (rtpbasepayload,
2128           g_value_get_boolean (value));
2129       break;
2130     case PROP_ONVIF_NO_RATE_CONTROL:
2131       priv->onvif_no_rate_control = g_value_get_boolean (value);
2132       break;
2133     case PROP_SCALE_RTPTIME:
2134       priv->scale_rtptime = g_value_get_boolean (value);
2135       break;
2136     case PROP_AUTO_HEADER_EXTENSION:
2137       priv->auto_hdr_ext = g_value_get_boolean (value);
2138       break;
2139     default:
2140       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2141       break;
2142   }
2143 }
2144
2145 static void
2146 gst_rtp_base_payload_get_property (GObject * object, guint prop_id,
2147     GValue * value, GParamSpec * pspec)
2148 {
2149   GstRTPBasePayload *rtpbasepayload;
2150   GstRTPBasePayloadPrivate *priv;
2151
2152   rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
2153   priv = rtpbasepayload->priv;
2154
2155   switch (prop_id) {
2156     case PROP_MTU:
2157       g_value_set_uint (value, rtpbasepayload->mtu);
2158       break;
2159     case PROP_PT:
2160       g_value_set_uint (value, rtpbasepayload->pt);
2161       break;
2162     case PROP_SSRC:
2163       if (priv->ssrc_random)
2164         g_value_set_uint (value, -1);
2165       else
2166         g_value_set_uint (value, rtpbasepayload->ssrc);
2167       break;
2168     case PROP_TIMESTAMP_OFFSET:
2169       if (priv->ts_offset_random)
2170         g_value_set_uint (value, -1);
2171       else
2172         g_value_set_uint (value, (guint32) rtpbasepayload->ts_offset);
2173       break;
2174     case PROP_SEQNUM_OFFSET:
2175       if (priv->seqnum_offset_random)
2176         g_value_set_int (value, -1);
2177       else
2178         g_value_set_int (value, (guint16) rtpbasepayload->seqnum_offset);
2179       break;
2180     case PROP_MAX_PTIME:
2181       g_value_set_int64 (value, rtpbasepayload->max_ptime);
2182       break;
2183     case PROP_MIN_PTIME:
2184       g_value_set_int64 (value, rtpbasepayload->min_ptime);
2185       break;
2186     case PROP_TIMESTAMP:
2187       g_value_set_uint (value, rtpbasepayload->timestamp);
2188       break;
2189     case PROP_SEQNUM:
2190       g_value_set_uint (value, rtpbasepayload->seqnum);
2191       break;
2192     case PROP_PERFECT_RTPTIME:
2193       g_value_set_boolean (value, priv->perfect_rtptime);
2194       break;
2195     case PROP_PTIME_MULTIPLE:
2196       g_value_set_int64 (value, rtpbasepayload->ptime_multiple);
2197       break;
2198     case PROP_STATS:
2199       g_value_take_boxed (value,
2200           gst_rtp_base_payload_create_stats (rtpbasepayload));
2201       break;
2202     case PROP_SOURCE_INFO:
2203       g_value_set_boolean (value,
2204           gst_rtp_base_payload_is_source_info_enabled (rtpbasepayload));
2205       break;
2206     case PROP_ONVIF_NO_RATE_CONTROL:
2207       g_value_set_boolean (value, priv->onvif_no_rate_control);
2208       break;
2209     case PROP_SCALE_RTPTIME:
2210       g_value_set_boolean (value, priv->scale_rtptime);
2211       break;
2212     case PROP_AUTO_HEADER_EXTENSION:
2213       g_value_set_boolean (value, priv->auto_hdr_ext);
2214       break;
2215     default:
2216       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2217       break;
2218   }
2219 }
2220
2221 static GstStateChangeReturn
2222 gst_rtp_base_payload_change_state (GstElement * element,
2223     GstStateChange transition)
2224 {
2225   GstRTPBasePayload *rtpbasepayload;
2226   GstRTPBasePayloadPrivate *priv;
2227   GstStateChangeReturn ret;
2228
2229   rtpbasepayload = GST_RTP_BASE_PAYLOAD (element);
2230   priv = rtpbasepayload->priv;
2231
2232   switch (transition) {
2233     case GST_STATE_CHANGE_NULL_TO_READY:
2234       break;
2235     case GST_STATE_CHANGE_READY_TO_PAUSED:
2236       gst_segment_init (&rtpbasepayload->segment, GST_FORMAT_UNDEFINED);
2237       rtpbasepayload->priv->delay_segment = TRUE;
2238       gst_event_replace (&rtpbasepayload->priv->pending_segment, NULL);
2239
2240       if (priv->seqnum_offset_random)
2241         rtpbasepayload->seqnum_base = g_random_int_range (0, G_MAXINT16);
2242       else
2243         rtpbasepayload->seqnum_base = rtpbasepayload->seqnum_offset;
2244       priv->next_seqnum = rtpbasepayload->seqnum_base;
2245       rtpbasepayload->seqnum = rtpbasepayload->seqnum_base;
2246
2247       if (priv->ssrc_random)
2248         rtpbasepayload->current_ssrc = g_random_int ();
2249       else
2250         rtpbasepayload->current_ssrc = rtpbasepayload->ssrc;
2251
2252       if (priv->ts_offset_random)
2253         rtpbasepayload->ts_base = g_random_int ();
2254       else
2255         rtpbasepayload->ts_base = rtpbasepayload->ts_offset;
2256       rtpbasepayload->timestamp = rtpbasepayload->ts_base;
2257       priv->running_time = DEFAULT_RUNNING_TIME;
2258       g_atomic_int_set (&rtpbasepayload->priv->notified_first_timestamp, 1);
2259       priv->base_offset = GST_BUFFER_OFFSET_NONE;
2260       priv->negotiated = FALSE;
2261       gst_caps_replace (&rtpbasepayload->priv->subclass_srccaps, NULL);
2262       gst_caps_replace (&rtpbasepayload->priv->sinkcaps, NULL);
2263       break;
2264     default:
2265       break;
2266   }
2267
2268   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2269
2270   switch (transition) {
2271     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2272       g_atomic_int_set (&rtpbasepayload->priv->notified_first_timestamp, 1);
2273       break;
2274     case GST_STATE_CHANGE_PAUSED_TO_READY:
2275       gst_event_replace (&rtpbasepayload->priv->pending_segment, NULL);
2276       break;
2277     default:
2278       break;
2279   }
2280   return ret;
2281 }
2282
2283 /**
2284  * gst_rtp_base_payload_set_source_info_enabled:
2285  * @payload: a #GstRTPBasePayload
2286  * @enable: whether to add contributing sources to RTP packets
2287  *
2288  * Enable or disable adding contributing sources to RTP packets from
2289  * #GstRTPSourceMeta.
2290  *
2291  * Since: 1.16
2292  **/
2293 void
2294 gst_rtp_base_payload_set_source_info_enabled (GstRTPBasePayload * payload,
2295     gboolean enable)
2296 {
2297   payload->priv->source_info = enable;
2298 }
2299
2300 /**
2301  * gst_rtp_base_payload_is_source_info_enabled:
2302  * @payload: a #GstRTPBasePayload
2303  *
2304  * Queries whether the payloader will add contributing sources (CSRCs) to the
2305  * RTP header from #GstRTPSourceMeta.
2306  *
2307  * Returns: %TRUE if source-info is enabled.
2308  *
2309  * Since: 1.16
2310  **/
2311 gboolean
2312 gst_rtp_base_payload_is_source_info_enabled (GstRTPBasePayload * payload)
2313 {
2314   return payload->priv->source_info;
2315 }
2316
2317
2318 /**
2319  * gst_rtp_base_payload_get_source_count:
2320  * @payload: a #GstRTPBasePayload
2321  * @buffer: (transfer none): a #GstBuffer, typically the buffer to payload
2322  *
2323  * Count the total number of RTP sources found in the meta of @buffer, which
2324  * will be automically added by gst_rtp_base_payload_allocate_output_buffer().
2325  * If #GstRTPBasePayload:source-info is %FALSE the count will be 0.
2326  *
2327  * Returns: The number of sources.
2328  *
2329  * Since: 1.16
2330  **/
2331 guint
2332 gst_rtp_base_payload_get_source_count (GstRTPBasePayload * payload,
2333     GstBuffer * buffer)
2334 {
2335   guint count = 0;
2336
2337   g_return_val_if_fail (buffer != NULL, 0);
2338
2339   if (gst_rtp_base_payload_is_source_info_enabled (payload)) {
2340     GstRTPSourceMeta *meta = gst_buffer_get_rtp_source_meta (buffer);
2341     if (meta != NULL)
2342       count = gst_rtp_source_meta_get_source_count (meta);
2343   }
2344
2345   return count;
2346 }