2 * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
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.
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
16 * SECTION:gstrtpbasepayload
17 * @title: GstRTPBasePayload
18 * @short_description: Base class for RTP payloader
20 * Provides a base class for RTP payloaders
29 #include <gst/rtp/gstrtpbuffer.h>
31 #include "gstrtpbasepayload.h"
32 #include "gstrtpmeta.h"
33 #include "gstrtphdrext.h"
35 GST_DEBUG_CATEGORY_STATIC (rtpbasepayload_debug);
36 #define GST_CAT_DEFAULT (rtpbasepayload_debug)
38 static gboolean enable_experimental_twcc = FALSE;
40 struct _GstRTPBasePayloadPrivate
42 gboolean ts_offset_random;
43 gboolean seqnum_offset_random;
46 gboolean perfect_rtptime;
47 gint notified_first_timestamp;
52 GstBuffer *input_meta_buffer;
56 guint64 base_rtime_hz;
58 gboolean scale_rtptime;
59 gboolean auto_hdr_ext;
61 gint64 prop_max_ptime;
62 gint64 caps_max_ptime;
64 gboolean onvif_no_rate_control;
68 gboolean delay_segment;
69 GstEvent *pending_segment;
71 GstCaps *subclass_srccaps;
74 /* array of GstRTPHeaderExtension's * */
75 GPtrArray *header_exts;
78 /* RTPBasePayload signals and args */
82 SIGNAL_REQUEST_EXTENSION,
84 SIGNAL_CLEAR_EXTENSIONS,
88 static guint gst_rtp_base_payload_signals[LAST_SIGNAL] = { 0 };
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.
96 #define DEFAULT_MTU 1400
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
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
122 PROP_TIMESTAMP_OFFSET,
128 PROP_PERFECT_RTPTIME,
132 PROP_ONVIF_NO_RATE_CONTROL,
134 PROP_AUTO_HEADER_EXTENSION,
138 static void gst_rtp_base_payload_class_init (GstRTPBasePayloadClass * klass);
139 static void gst_rtp_base_payload_init (GstRTPBasePayload * rtpbasepayload,
141 static void gst_rtp_base_payload_finalize (GObject * object);
143 static GstCaps *gst_rtp_base_payload_getcaps_default (GstRTPBasePayload *
144 rtpbasepayload, GstPad * pad, GstCaps * filter);
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,
158 static GstFlowReturn gst_rtp_base_payload_chain (GstPad * pad,
159 GstObject * parent, GstBuffer * buffer);
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);
166 static GstStateChangeReturn gst_rtp_base_payload_change_state (GstElement *
167 element, GstStateChange transition);
169 static gboolean gst_rtp_base_payload_negotiate (GstRTPBasePayload * payload);
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);
175 static GstElementClass *parent_class = NULL;
176 static gint private_offset = 0;
179 gst_rtp_base_payload_get_type (void)
181 static GType rtpbasepayload_type = 0;
183 if (g_once_init_enter ((gsize *) & rtpbasepayload_type)) {
184 static const GTypeInfo rtpbasepayload_info = {
185 sizeof (GstRTPBasePayloadClass),
188 (GClassInitFunc) gst_rtp_base_payload_class_init,
191 sizeof (GstRTPBasePayload),
193 (GInstanceInitFunc) gst_rtp_base_payload_init,
197 _type = g_type_register_static (GST_TYPE_ELEMENT, "GstRTPBasePayload",
198 &rtpbasepayload_info, G_TYPE_FLAG_ABSTRACT);
201 g_type_add_instance_private (_type, sizeof (GstRTPBasePayloadPrivate));
203 g_once_init_leave ((gsize *) & rtpbasepayload_type, _type);
205 return rtpbasepayload_type;
208 static inline GstRTPBasePayloadPrivate *
209 gst_rtp_base_payload_get_instance_private (GstRTPBasePayload * self)
211 return (G_STRUCT_MEMBER_P (self, private_offset));
214 static GstRTPHeaderExtension *
215 gst_rtp_base_payload_request_extension_default (GstRTPBasePayload * payload,
216 guint ext_id, const gchar * uri)
218 GstRTPHeaderExtension *ext = NULL;
220 if (!payload->priv->auto_hdr_ext)
223 ext = gst_rtp_header_extension_create_from_uri (uri);
225 GST_DEBUG_OBJECT (payload,
226 "Automatically enabled extension %s for uri \'%s\'",
227 GST_ELEMENT_NAME (ext), uri);
229 gst_rtp_header_extension_set_id (ext, ext_id);
231 GST_DEBUG_OBJECT (payload,
232 "Didn't find any extension implementing uri \'%s\'", uri);
239 extension_accumulator (GSignalInvocationHint * ihint,
240 GValue * return_accu, const GValue * handler_return, gpointer data)
244 /* Call default handler if user callback didn't create the extension */
245 ext = g_value_get_object (handler_return);
249 g_value_set_object (return_accu, ext);
254 gst_rtp_base_payload_class_init (GstRTPBasePayloadClass * klass)
256 GObjectClass *gobject_class;
257 GstElementClass *gstelement_class;
259 gobject_class = (GObjectClass *) klass;
260 gstelement_class = (GstElementClass *) klass;
262 if (g_getenv ("GST_RTP_ENABLE_EXPERIMENTAL_TWCC_PROPERTY"))
263 enable_experimental_twcc = TRUE;
265 if (private_offset != 0)
266 g_type_class_adjust_private_offset (klass, &private_offset);
268 parent_class = g_type_class_peek_parent (klass);
270 gobject_class->finalize = gst_rtp_base_payload_finalize;
272 gobject_class->set_property = gst_rtp_base_payload_set_property;
273 gobject_class->get_property = gst_rtp_base_payload_get_property;
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",
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));
304 * GstRTPBasePayload:min-ptime:
306 * Minimum duration of the packet data in ns (can't go above MTU)
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));
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));
324 * GstRTPBasePayload:perfect-rtptime:
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.
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));
347 * GstRTPBasePayload:ptime-multiple:
349 * Force buffers to be multiples of this duration in ns (0 disables)
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));
358 * GstRTPBasePayload:stats:
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:
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
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));
380 * GstRTPBasePayload:source-info:
382 * Enable writing the CSRC field in allocated RTP header based on RTP source
383 * information found in the input buffer's #GstRTPSourceMeta.
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));
393 * GstRTPBasePayload:onvif-no-rate-control:
395 * Make the payloader timestamp packets according to the Rate-Control=no
396 * behaviour specified in the ONVIF replay spec.
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));
408 * GstRTPBasePayload:scale-rtptime:
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).
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.
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));
427 * GstRTPBasePayload:auto-header-extension:
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.
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));
444 * GstRTPBasePayload::add-extension:
445 * @object: the #GstRTPBasePayload
446 * @ext: (transfer full): the #GstRTPHeaderExtension
448 * Add @ext as an extension for writing part of an RTP header extension onto
449 * outgoing RTP packets.
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);
460 * GstRTPBasePayload::request-extension:
461 * @object: the #GstRTPBasePayload
462 * @ext_id: the extension id being requested
463 * @ext_uri: the extension URI being requested
465 * The returned @ext must be configured with the correct @ext_id and with the
466 * necessary attributes as required by the extension implementation.
468 * Returns: (transfer full): the #GstRTPHeaderExtension for @ext_id, or %NULL
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);
480 * GstRTPBasePayload::clear-extensions:
481 * @object: the #GstRTPBasePayload
483 * Clear all RTP header extensions used by this payloader.
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,
493 gstelement_class->change_state = gst_rtp_base_payload_change_state;
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;
500 GST_DEBUG_CATEGORY_INIT (rtpbasepayload_debug, "rtpbasepayload", 0,
501 "Base class for RTP Payloaders");
505 gst_rtp_base_payload_init (GstRTPBasePayload * rtpbasepayload, gpointer g_class)
507 GstPadTemplate *templ;
508 GstRTPBasePayloadPrivate *priv;
510 rtpbasepayload->priv = priv =
511 gst_rtp_base_payload_get_instance_private (rtpbasepayload);
514 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
515 g_return_if_fail (templ != NULL);
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);
523 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
524 g_return_if_fail (templ != NULL);
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);
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;
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;
557 rtpbasepayload->media = NULL;
558 rtpbasepayload->encoding_name = NULL;
560 rtpbasepayload->clock_rate = 0;
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);
569 gst_rtp_base_payload_finalize (GObject * object)
571 GstRTPBasePayload *rtpbasepayload;
573 rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
575 g_free (rtpbasepayload->media);
576 rtpbasepayload->media = NULL;
577 g_free (rtpbasepayload->encoding_name);
578 rtpbasepayload->encoding_name = NULL;
580 gst_caps_replace (&rtpbasepayload->priv->subclass_srccaps, NULL);
581 gst_caps_replace (&rtpbasepayload->priv->sinkcaps, NULL);
583 g_ptr_array_unref (rtpbasepayload->priv->header_exts);
584 rtpbasepayload->priv->header_exts = NULL;
586 G_OBJECT_CLASS (parent_class)->finalize (object);
590 gst_rtp_base_payload_getcaps_default (GstRTPBasePayload * rtpbasepayload,
591 GstPad * pad, GstCaps * filter)
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);
601 caps = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
603 caps = gst_caps_ref (caps);
609 gst_rtp_base_payload_sink_event_default (GstRTPBasePayload * rtpbasepayload,
612 GstObject *parent = GST_OBJECT_CAST (rtpbasepayload);
613 gboolean res = FALSE;
615 switch (GST_EVENT_TYPE (event)) {
616 case GST_EVENT_FLUSH_START:
617 res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
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);
626 GstRTPBasePayloadClass *rtpbasepayload_class;
629 gst_event_parse_caps (event, &caps);
630 GST_DEBUG_OBJECT (rtpbasepayload, "setting caps %" GST_PTR_FORMAT, caps);
632 gst_caps_replace (&rtpbasepayload->priv->sinkcaps, caps);
634 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
635 if (rtpbasepayload_class->set_caps)
636 res = rtpbasepayload_class->set_caps (rtpbasepayload, caps);
638 res = gst_rtp_base_payload_negotiate (rtpbasepayload);
640 rtpbasepayload->priv->negotiated = res;
642 gst_event_unref (event);
645 case GST_EVENT_SEGMENT:
649 segment = &rtpbasepayload->segment;
650 gst_event_copy_segment (event, segment);
652 rtpbasepayload->priv->base_offset = GST_BUFFER_OFFSET_NONE;
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);
661 res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
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;
673 res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
677 res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
684 gst_rtp_base_payload_sink_event (GstPad * pad, GstObject * parent,
687 GstRTPBasePayload *rtpbasepayload;
688 GstRTPBasePayloadClass *rtpbasepayload_class;
689 gboolean res = FALSE;
691 rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
692 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
694 if (rtpbasepayload_class->sink_event)
695 res = rtpbasepayload_class->sink_event (rtpbasepayload, event);
697 gst_event_unref (event);
703 gst_rtp_base_payload_src_event_default (GstRTPBasePayload * rtpbasepayload,
706 GstObject *parent = GST_OBJECT_CAST (rtpbasepayload);
707 gboolean res = TRUE, forward = TRUE;
709 switch (GST_EVENT_TYPE (event)) {
710 case GST_EVENT_CUSTOM_UPSTREAM:
712 const GstStructure *s = gst_event_get_structure (event);
714 if (gst_structure_has_name (s, "GstRTPCollision")) {
717 if (!gst_structure_get_uint (s, "ssrc", &ssrc))
720 GST_DEBUG_OBJECT (rtpbasepayload, "collided ssrc: %" G_GUINT32_FORMAT,
723 /* choose another ssrc for our stream */
724 if (ssrc == rtpbasepayload->current_ssrc) {
726 guint suggested_ssrc = 0;
728 if (gst_structure_get_uint (s, "suggested-ssrc", &suggested_ssrc))
729 rtpbasepayload->current_ssrc = suggested_ssrc;
731 while (ssrc == rtpbasepayload->current_ssrc)
732 rtpbasepayload->current_ssrc = g_random_int ();
734 caps = gst_pad_get_current_caps (rtpbasepayload->srcpad);
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);
743 /* the event was for us */
754 res = gst_pad_event_default (rtpbasepayload->srcpad, parent, event);
756 gst_event_unref (event);
762 gst_rtp_base_payload_src_event (GstPad * pad, GstObject * parent,
765 GstRTPBasePayload *rtpbasepayload;
766 GstRTPBasePayloadClass *rtpbasepayload_class;
767 gboolean res = FALSE;
769 rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
770 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
772 if (rtpbasepayload_class->src_event)
773 res = rtpbasepayload_class->src_event (rtpbasepayload, event);
775 gst_event_unref (event);
782 gst_rtp_base_payload_query_default (GstRTPBasePayload * rtpbasepayload,
783 GstPad * pad, GstQuery * query)
785 gboolean res = FALSE;
787 switch (GST_QUERY_TYPE (query)) {
790 GstRTPBasePayloadClass *rtpbasepayload_class;
791 GstCaps *filter, *caps;
793 gst_query_parse_caps (query, &filter);
794 GST_DEBUG_OBJECT (rtpbasepayload, "getting caps with filter %"
795 GST_PTR_FORMAT, filter);
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);
808 gst_pad_query_default (pad, GST_OBJECT_CAST (rtpbasepayload), query);
815 gst_rtp_base_payload_query (GstPad * pad, GstObject * parent, GstQuery * query)
817 GstRTPBasePayload *rtpbasepayload;
818 GstRTPBasePayloadClass *rtpbasepayload_class;
819 gboolean res = FALSE;
821 rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
822 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
824 if (rtpbasepayload_class->query)
825 res = rtpbasepayload_class->query (rtpbasepayload, pad, query);
831 gst_rtp_base_payload_chain (GstPad * pad, GstObject * parent,
834 GstRTPBasePayload *rtpbasepayload;
835 GstRTPBasePayloadClass *rtpbasepayload_class;
838 rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
839 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
841 if (!rtpbasepayload_class->handle_buffer)
844 if (!rtpbasepayload->priv->negotiated)
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
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);
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))) {
863 goto negotiate_failed;
868 ret = rtpbasepayload_class->handle_buffer (rtpbasepayload, buffer);
870 gst_buffer_replace (&rtpbasepayload->priv->input_meta_buffer, NULL);
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;
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;
892 GST_DEBUG_OBJECT (rtpbasepayload, "Not negotiated");
893 gst_buffer_unref (buffer);
894 return GST_FLOW_NOT_NEGOTIATED;
898 GST_DEBUG_OBJECT (rtpbasepayload, "we are flushing");
899 gst_buffer_unref (buffer);
900 return GST_FLOW_FLUSHING;
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
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().
917 gst_rtp_base_payload_set_options (GstRTPBasePayload * payload,
918 const gchar * media, gboolean dynamic, const gchar * encoding_name,
921 g_return_if_fail (payload != NULL);
922 g_return_if_fail (clock_rate != 0);
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;
933 copy_fixed (GQuark field_id, const GValue * value, GstStructure * dest)
935 if (gst_value_is_fixed (value)) {
936 gst_structure_id_set_value (dest, field_id, value);
942 update_max_ptime (GstRTPBasePayload * rtpbasepayload)
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;
953 rtpbasepayload->max_ptime = DEFAULT_MAX_PTIME;
957 _set_caps (GQuark field_id, const GValue * value, GstCaps * caps)
959 gst_caps_set_value (caps, g_quark_to_string (field_id), value);
965 * gst_rtp_base_payload_set_outcaps_structure:
966 * @payload: a #GstRTPBasePayload
967 * @s: (nullable): a #GstStructure with the caps fields
969 * Configure the output caps with the optional fields.
971 * Returns: %TRUE if the caps could be set.
976 gst_rtp_base_payload_set_outcaps_structure (GstRTPBasePayload * payload,
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);
987 GST_DEBUG_OBJECT (payload, "defaults: %" GST_PTR_FORMAT, srccaps);
989 if (s && gst_structure_n_fields (s) > 0) {
990 gst_structure_foreach (s, (GstStructureForeachFunc) _set_caps, srccaps);
992 GST_DEBUG_OBJECT (payload, "custom added: %" GST_PTR_FORMAT, srccaps);
995 gst_caps_replace (&payload->priv->subclass_srccaps, srccaps);
996 gst_caps_unref (srccaps);
998 return gst_rtp_base_payload_negotiate (payload);
1002 * gst_rtp_base_payload_set_outcaps:
1003 * @payload: a #GstRTPBasePayload
1004 * @fieldname: the first field name or %NULL
1005 * @...: field values
1007 * Configure the output caps with the optional parameters.
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.
1012 * Returns: %TRUE if the caps could be set.
1015 gst_rtp_base_payload_set_outcaps (GstRTPBasePayload * payload,
1016 const gchar * fieldname, ...)
1019 GstStructure *s = NULL;
1024 s = gst_structure_new_empty ("unused");
1026 /* override with custom properties */
1027 va_start (varargs, fieldname);
1028 gst_structure_set_valist (s, fieldname, varargs);
1032 result = gst_rtp_base_payload_set_outcaps_structure (payload, s);
1034 gst_clear_structure (&s);
1040 add_and_ref_item (GstRTPHeaderExtension * ext, GPtrArray * ret)
1042 g_ptr_array_add (ret, gst_object_ref (ext));
1046 remove_item_from (GstRTPHeaderExtension * ext, GPtrArray * ret)
1048 g_ptr_array_remove_fast (ret, ext);
1052 add_item_to (GstRTPHeaderExtension * ext, GPtrArray * ret)
1054 g_ptr_array_add (ret, ext);
1058 add_header_ext_to_caps (GstRTPHeaderExtension * ext, GstCaps * caps)
1060 if (!gst_rtp_header_extension_set_caps_from_attributes (ext, caps)) {
1061 GST_WARNING ("Failed to set caps from rtp header extension");
1066 gst_rtp_base_payload_negotiate (GstRTPBasePayload * payload)
1068 GstCaps *templ, *peercaps, *srccaps;
1069 GstStructure *s, *d;
1070 gboolean res = TRUE;
1072 payload->priv->caps_max_ptime = DEFAULT_MAX_PTIME;
1075 gst_pad_check_reconfigure (payload->srcpad);
1077 templ = gst_pad_get_pad_template_caps (payload->srcpad);
1079 if (payload->priv->subclass_srccaps) {
1080 GstCaps *tmp = gst_caps_intersect (payload->priv->subclass_srccaps,
1082 gst_caps_unref (templ);
1086 peercaps = gst_pad_peer_query_caps (payload->srcpad, templ);
1088 if (peercaps == NULL) {
1089 /* no peer caps, just add the other properties */
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);
1098 GST_DEBUG_OBJECT (payload, "no peer caps: %" GST_PTR_FORMAT, srccaps);
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;
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);
1112 if (gst_caps_is_empty (temp)) {
1113 gst_caps_unref (temp);
1114 gst_caps_unref (templ);
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
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.
1130 * FIXME 2.0: All these properties should go away and be negotiated
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;
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);
1143 if (!gst_caps_is_empty (intersection)) {
1144 GST_LOG_OBJECT (payload, "Using selected pt %d",
1145 GST_RTP_BASE_PAYLOAD_PT (payload));
1147 gst_caps_unref (temp);
1148 temp = intersection;
1150 GST_WARNING_OBJECT (payload, "Can't use selected pt %d",
1151 GST_RTP_BASE_PAYLOAD_PT (payload));
1152 gst_caps_unref (intersection);
1154 gst_caps_unref (probe_caps);
1157 /* If we got no pt above, select one now */
1161 /* get first structure */
1162 s = gst_caps_get_structure (temp, 0);
1164 if (gst_structure_get_int (s, "payload", &pt)) {
1166 GST_RTP_BASE_PAYLOAD_PT (payload) = pt;
1167 GST_LOG_OBJECT (payload, "using peer pt %d", pt);
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);
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);
1186 /* If we got no ssrc above, select one now */
1187 /* get first structure */
1188 s = gst_caps_get_structure (temp, 0);
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);
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);
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;
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);
1214 if (!gst_caps_is_empty (intersection)) {
1215 GST_LOG_OBJECT (payload, "Using selected timestamp-offset %u",
1217 gst_caps_unref (temp);
1218 temp = intersection;
1219 have_ts_offset = TRUE;
1221 GST_WARNING_OBJECT (payload, "Can't use selected timestamp-offset %u",
1223 gst_caps_unref (intersection);
1225 gst_caps_unref (probe_caps);
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);
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",
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,
1244 GST_LOG_OBJECT (payload, "using internal timestamp-offset %u",
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;
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);
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;
1267 GST_WARNING_OBJECT (payload, "Can't use selected seqnum-offset %u",
1268 payload->seqnum_base);
1269 gst_caps_unref (intersection);
1271 gst_caps_unref (probe_caps);
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);
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;
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);
1300 /* now fixate, start by taking the first caps */
1301 temp = gst_caps_truncate (temp);
1303 /* get first structure */
1304 s = gst_caps_get_structure (temp, 0);
1306 if (gst_structure_get_uint (s, "maxptime", &max_ptime))
1307 payload->priv->caps_max_ptime = max_ptime * GST_MSECOND;
1309 if (gst_structure_get_uint (s, "ptime", &ptime))
1310 payload->ptime = ptime * GST_MSECOND;
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);
1317 gst_structure_foreach (s, (GstStructureForeachFunc) copy_fixed, d);
1319 gst_caps_unref (temp);
1321 GST_DEBUG_OBJECT (payload, "with peer caps: %" GST_PTR_FORMAT, srccaps);
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;
1330 GST_DEBUG_OBJECT (payload, "video caps: %" GST_PTR_FORMAT,
1331 payload->priv->sinkcaps);
1333 has_framerate = gst_structure_get_fraction (s, "framerate", &num, &denom);
1334 if (has_framerate && num == 0 && denom == 1) {
1336 gst_structure_get_fraction (s, "max-framerate", &num, &denom);
1339 if (has_framerate) {
1340 gchar str[G_ASCII_DTOSTR_BUF_SIZE];
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);
1349 GST_DEBUG_OBJECT (payload, "with video caps: %" GST_PTR_FORMAT, srccaps);
1353 update_max_ptime (payload);
1356 /* try to find header extension implementations for the list in the
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 ();
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);
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-")) {
1373 const gchar *uri = NULL;
1376 GstRTPHeaderExtension *ext = NULL;
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",
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);
1399 GST_WARNING_OBJECT (payload, "could not get extmap uri for "
1400 "field %s", field_name);
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,
1414 GST_WARNING_OBJECT (payload,
1415 "Failed to configure rtp header " "extension %"
1416 GST_PTR_FORMAT " attributes from caps %" GST_PTR_FORMAT,
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);
1436 /* if no extension, attempt to request one */
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,
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);
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);
1455 if (ext && !gst_rtp_header_extension_set_attributes_from_caps (ext,
1457 GST_WARNING_OBJECT (payload,
1458 "Failed to configure rtp header " "extension %"
1459 GST_PTR_FORMAT " attributes from caps %" GST_PTR_FORMAT,
1462 g_clear_object (&ext);
1467 g_ptr_array_add (to_add, ext);
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);
1484 g_ptr_array_unref (to_add);
1485 g_ptr_array_unref (to_remove);
1486 g_ptr_array_unref (header_exts);
1489 GST_DEBUG_OBJECT (payload, "configuring caps %" GST_PTR_FORMAT, srccaps);
1492 res = gst_pad_set_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload), srccaps);
1493 gst_caps_unref (srccaps);
1494 gst_caps_unref (templ);
1499 gst_pad_mark_reconfigure (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
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
1510 * Check if the packet with @size and @duration would exceed the configured
1513 * Returns: %TRUE if the packet of @size and @duration would exceed the
1514 * configured MTU or max_ptime.
1517 gst_rtp_base_payload_is_filled (GstRTPBasePayload * payload,
1518 guint size, GstClockTime duration)
1520 if (size > payload->mtu)
1523 if (payload->max_ptime != -1 && duration >= payload->max_ptime)
1531 GstRTPBasePayload *payload;
1542 find_timestamp (GstBuffer ** buffer, guint idx, gpointer user_data)
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);
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)
1558 gst_rtp_base_payload_add_extension (GstRTPBasePayload * payload,
1559 GstRTPHeaderExtension * ext)
1561 g_return_if_fail (GST_IS_RTP_HEADER_EXTENSION (ext));
1562 g_return_if_fail (gst_rtp_header_extension_get_id (ext) > 0);
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);
1572 gst_rtp_base_payload_clear_extensions (GstRTPBasePayload * payload)
1574 GST_OBJECT_LOCK (payload);
1575 g_ptr_array_set_size (payload->priv->header_exts, 0);
1576 GST_OBJECT_UNLOCK (payload);
1581 GstRTPBasePayload *payload;
1582 GstRTPHeaderExtensionFlags flags;
1585 gsize allocated_size;
1587 gsize hdr_unit_size;
1592 determine_header_extension_flags_size (GstRTPHeaderExtension * ext,
1595 HeaderExt *hdr = user_data;
1599 hdr->flags &= gst_rtp_header_extension_get_supported_flags (ext);
1601 gst_rtp_header_extension_get_max_size (ext,
1602 hdr->payload->priv->input_meta_buffer);
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;
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;
1615 hdr->allocated_size += max_size;
1619 write_header_extension (GstRTPHeaderExtension * ext, gpointer user_data)
1621 HeaderExt *hdr = user_data;
1623 hdr->allocated_size - hdr->written_size - hdr->hdr_unit_size;
1624 gsize offset = hdr->written_size + hdr->hdr_unit_size;
1631 written = gst_rtp_header_extension_write (ext,
1632 hdr->payload->priv->input_meta_buffer, hdr->flags, hdr->output,
1633 &hdr->data[offset], remaining);
1636 /* extension wrote no data */
1638 } else if (written < 0) {
1639 GST_WARNING_OBJECT (hdr->payload, "%s failed to write extension data",
1640 GST_OBJECT_NAME (ext));
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");
1650 ext_id = gst_rtp_header_extension_get_id (ext);
1652 /* move to the beginning of the extension header */
1653 offset -= hdr->hdr_unit_size;
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));
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));
1671 hdr->data[offset] = ext_id & 0xFF;
1672 hdr->data[offset + 1] = written & 0xFF;
1674 g_critical ("Don't know how to write extension data with flags 0x%x!",
1679 hdr->written_size += written + hdr->hdr_unit_size;
1689 set_headers (GstBuffer ** buffer, guint idx, gpointer user_data)
1691 HeaderData *data = user_data;
1692 HeaderExt hdrext = { NULL, };
1693 GstRTPBuffer rtp = { NULL, };
1695 if (!gst_rtp_buffer_map (*buffer, GST_MAP_WRITE, &rtp))
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);
1703 GST_OBJECT_LOCK (data->payload);
1704 if (data->payload->priv->header_exts->len > 0) {
1707 guint16 bit_pattern;
1709 /* write header extensions */
1710 hdrext.payload = data->payload;
1711 hdrext.output = *buffer;
1712 /* XXX: pre-calculate these flags and sizes? */
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;
1728 goto unsupported_flags;
1732 hdrext.hdr_unit_size * data->payload->priv->header_exts->len +
1733 hdrext.allocated_size;
1734 wordlen = extlen / 4 + ((extlen % 4) ? 1 : 0);
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,
1742 /* from 32-bit words to bytes */
1743 hdrext.allocated_size = wordlen * 4;
1745 g_ptr_array_foreach (data->payload->priv->header_exts,
1746 (GFunc) write_header_extension, &hdrext);
1748 wordlen = hdrext.written_size / 4 + ((hdrext.written_size % 4) ? 1 : 0);
1749 gst_rtp_buffer_set_extension_data (&rtp, bit_pattern, wordlen);
1751 GST_OBJECT_UNLOCK (data->payload);
1752 gst_rtp_buffer_unmap (&rtp);
1754 /* increment the seqnum for each buffer */
1761 GST_ERROR ("failed to map buffer %p", *buffer);
1767 GST_OBJECT_UNLOCK (data->payload);
1768 gst_rtp_buffer_unmap (&rtp);
1769 GST_ERROR ("Cannot add rtp header extensions with mixed header types");
1775 foreach_metadata_drop (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
1777 GType drop_api_type = (GType) user_data;
1778 const GstMetaInfo *info = (*meta)->info;
1780 if (info->api == drop_api_type)
1787 filter_meta (GstBuffer ** buffer, guint idx, gpointer user_data)
1789 return gst_buffer_foreach_meta (*buffer, foreach_metadata_drop,
1790 (gpointer) GST_RTP_SOURCE_META_API_TYPE);
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)
1799 GstRTPBasePayloadPrivate *priv;
1802 if (payload->clock_rate == 0)
1805 priv = payload->priv;
1807 /* update first, so that the property is set to the last
1809 payload->seqnum = priv->next_seqnum;
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;
1817 /* find the first buffer with a timestamp */
1821 data.offset = GST_BUFFER_OFFSET_NONE;
1822 gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj), find_timestamp, &data);
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));
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;
1838 GST_LOG_OBJECT (payload,
1839 "Using offset %" G_GUINT64_FORMAT " for RTP timestamp", data.offset);
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)) {
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);
1856 gst_segment_to_running_time (&payload->segment, GST_FORMAT_TIME,
1859 if (!GST_CLOCK_TIME_IS_VALID (rtime_ns)) {
1860 GST_LOG_OBJECT (payload, "Clipped pts, using base RTP timestamp");
1863 GST_LOG_OBJECT (payload,
1864 "Using running_time %" GST_TIME_FORMAT " for RTP timestamp",
1865 GST_TIME_ARGS (rtime_ns));
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;
1872 /* add running_time in clock-rate units to the base timestamp */
1873 data.rtptime = payload->ts_base + rtime_hz;
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;
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;
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;
1893 /* set ssrc, payload type, seq number, caps and rtptime */
1894 /* remove unwanted meta */
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;
1901 GstBuffer *buf = GST_BUFFER_CAST (obj);
1902 set_headers (&buf, 0, &data);
1903 filter_meta (&buf, 0, NULL);
1906 priv->next_seqnum = data.seqnum;
1907 payload->timestamp = data.rtptime;
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));
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");
1927 GST_ELEMENT_ERROR (payload, STREAM, NOT_IMPLEMENTED, (NULL),
1928 ("subclass did not specify clock-rate"));
1929 return GST_FLOW_ERROR;
1934 * gst_rtp_base_payload_push_list:
1935 * @payload: a #GstRTPBasePayload
1936 * @list: a #GstBufferList
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.
1941 * This function takes ownership of @list.
1943 * Returns: a #GstFlowReturn.
1946 gst_rtp_base_payload_push_list (GstRTPBasePayload * payload,
1947 GstBufferList * list)
1951 res = gst_rtp_base_payload_prepare_push (payload, list, TRUE);
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;
1959 res = gst_pad_push_list (payload->srcpad, list);
1961 gst_buffer_list_unref (list);
1968 * gst_rtp_base_payload_push:
1969 * @payload: a #GstRTPBasePayload
1970 * @buffer: a #GstBuffer
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.
1975 * This function takes ownership of @buffer.
1977 * Returns: a #GstFlowReturn.
1980 gst_rtp_base_payload_push (GstRTPBasePayload * payload, GstBuffer * buffer)
1984 res = gst_rtp_base_payload_prepare_push (payload, buffer, FALSE);
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;
1992 res = gst_pad_push (payload->srcpad, buffer);
1994 gst_buffer_unref (buffer);
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
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.
2012 * Returns: A newly allocated buffer that can hold an RTP packet with given
2018 gst_rtp_base_payload_allocate_output_buffer (GstRTPBasePayload * payload,
2019 guint payload_len, guint8 pad_len, guint8 csrc_count)
2021 GstBuffer *buffer = NULL;
2023 if (payload->priv->input_meta_buffer != NULL) {
2024 GstRTPSourceMeta *meta =
2025 gst_buffer_get_rtp_source_meta (payload->priv->input_meta_buffer);
2027 guint total_csrc_count, idx, i;
2028 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
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,
2036 gst_rtp_buffer_map (buffer, GST_MAP_READWRITE, &rtp);
2038 /* Skip CSRC fields requested by derived class and fill CSRCs from meta.
2039 * Finally append the SSRC as a new CSRC. */
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);
2046 gst_rtp_buffer_unmap (&rtp);
2051 buffer = gst_rtp_buffer_new_allocate (payload_len, pad_len, csrc_count);
2056 static GstStructure *
2057 gst_rtp_base_payload_create_stats (GstRTPBasePayload * rtpbasepayload)
2059 GstRTPBasePayloadPrivate *priv;
2062 priv = rtpbasepayload->priv;
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);
2078 gst_rtp_base_payload_set_property (GObject * object, guint prop_id,
2079 const GValue * value, GParamSpec * pspec)
2081 GstRTPBasePayload *rtpbasepayload;
2082 GstRTPBasePayloadPrivate *priv;
2085 rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
2086 priv = rtpbasepayload->priv;
2090 rtpbasepayload->mtu = g_value_get_uint (value);
2093 rtpbasepayload->pt = g_value_get_uint (value);
2094 priv->pt_set = TRUE;
2097 val = g_value_get_uint (value);
2098 rtpbasepayload->ssrc = val;
2099 priv->ssrc_random = FALSE;
2101 case PROP_TIMESTAMP_OFFSET:
2102 val = g_value_get_uint (value);
2103 rtpbasepayload->ts_offset = val;
2104 priv->ts_offset_random = FALSE;
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);
2113 case PROP_MAX_PTIME:
2114 rtpbasepayload->priv->prop_max_ptime = g_value_get_int64 (value);
2115 update_max_ptime (rtpbasepayload);
2117 case PROP_MIN_PTIME:
2118 rtpbasepayload->min_ptime = g_value_get_int64 (value);
2120 case PROP_PERFECT_RTPTIME:
2121 priv->perfect_rtptime = g_value_get_boolean (value);
2123 case PROP_PTIME_MULTIPLE:
2124 rtpbasepayload->ptime_multiple = g_value_get_int64 (value);
2126 case PROP_SOURCE_INFO:
2127 gst_rtp_base_payload_set_source_info_enabled (rtpbasepayload,
2128 g_value_get_boolean (value));
2130 case PROP_ONVIF_NO_RATE_CONTROL:
2131 priv->onvif_no_rate_control = g_value_get_boolean (value);
2133 case PROP_SCALE_RTPTIME:
2134 priv->scale_rtptime = g_value_get_boolean (value);
2136 case PROP_AUTO_HEADER_EXTENSION:
2137 priv->auto_hdr_ext = g_value_get_boolean (value);
2140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2146 gst_rtp_base_payload_get_property (GObject * object, guint prop_id,
2147 GValue * value, GParamSpec * pspec)
2149 GstRTPBasePayload *rtpbasepayload;
2150 GstRTPBasePayloadPrivate *priv;
2152 rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
2153 priv = rtpbasepayload->priv;
2157 g_value_set_uint (value, rtpbasepayload->mtu);
2160 g_value_set_uint (value, rtpbasepayload->pt);
2163 if (priv->ssrc_random)
2164 g_value_set_uint (value, -1);
2166 g_value_set_uint (value, rtpbasepayload->ssrc);
2168 case PROP_TIMESTAMP_OFFSET:
2169 if (priv->ts_offset_random)
2170 g_value_set_uint (value, -1);
2172 g_value_set_uint (value, (guint32) rtpbasepayload->ts_offset);
2174 case PROP_SEQNUM_OFFSET:
2175 if (priv->seqnum_offset_random)
2176 g_value_set_int (value, -1);
2178 g_value_set_int (value, (guint16) rtpbasepayload->seqnum_offset);
2180 case PROP_MAX_PTIME:
2181 g_value_set_int64 (value, rtpbasepayload->max_ptime);
2183 case PROP_MIN_PTIME:
2184 g_value_set_int64 (value, rtpbasepayload->min_ptime);
2186 case PROP_TIMESTAMP:
2187 g_value_set_uint (value, rtpbasepayload->timestamp);
2190 g_value_set_uint (value, rtpbasepayload->seqnum);
2192 case PROP_PERFECT_RTPTIME:
2193 g_value_set_boolean (value, priv->perfect_rtptime);
2195 case PROP_PTIME_MULTIPLE:
2196 g_value_set_int64 (value, rtpbasepayload->ptime_multiple);
2199 g_value_take_boxed (value,
2200 gst_rtp_base_payload_create_stats (rtpbasepayload));
2202 case PROP_SOURCE_INFO:
2203 g_value_set_boolean (value,
2204 gst_rtp_base_payload_is_source_info_enabled (rtpbasepayload));
2206 case PROP_ONVIF_NO_RATE_CONTROL:
2207 g_value_set_boolean (value, priv->onvif_no_rate_control);
2209 case PROP_SCALE_RTPTIME:
2210 g_value_set_boolean (value, priv->scale_rtptime);
2212 case PROP_AUTO_HEADER_EXTENSION:
2213 g_value_set_boolean (value, priv->auto_hdr_ext);
2216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2221 static GstStateChangeReturn
2222 gst_rtp_base_payload_change_state (GstElement * element,
2223 GstStateChange transition)
2225 GstRTPBasePayload *rtpbasepayload;
2226 GstRTPBasePayloadPrivate *priv;
2227 GstStateChangeReturn ret;
2229 rtpbasepayload = GST_RTP_BASE_PAYLOAD (element);
2230 priv = rtpbasepayload->priv;
2232 switch (transition) {
2233 case GST_STATE_CHANGE_NULL_TO_READY:
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);
2240 if (priv->seqnum_offset_random)
2241 rtpbasepayload->seqnum_base = g_random_int_range (0, G_MAXINT16);
2243 rtpbasepayload->seqnum_base = rtpbasepayload->seqnum_offset;
2244 priv->next_seqnum = rtpbasepayload->seqnum_base;
2245 rtpbasepayload->seqnum = rtpbasepayload->seqnum_base;
2247 if (priv->ssrc_random)
2248 rtpbasepayload->current_ssrc = g_random_int ();
2250 rtpbasepayload->current_ssrc = rtpbasepayload->ssrc;
2252 if (priv->ts_offset_random)
2253 rtpbasepayload->ts_base = g_random_int ();
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);
2268 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2270 switch (transition) {
2271 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2272 g_atomic_int_set (&rtpbasepayload->priv->notified_first_timestamp, 1);
2274 case GST_STATE_CHANGE_PAUSED_TO_READY:
2275 gst_event_replace (&rtpbasepayload->priv->pending_segment, NULL);
2284 * gst_rtp_base_payload_set_source_info_enabled:
2285 * @payload: a #GstRTPBasePayload
2286 * @enable: whether to add contributing sources to RTP packets
2288 * Enable or disable adding contributing sources to RTP packets from
2289 * #GstRTPSourceMeta.
2294 gst_rtp_base_payload_set_source_info_enabled (GstRTPBasePayload * payload,
2297 payload->priv->source_info = enable;
2301 * gst_rtp_base_payload_is_source_info_enabled:
2302 * @payload: a #GstRTPBasePayload
2304 * Queries whether the payloader will add contributing sources (CSRCs) to the
2305 * RTP header from #GstRTPSourceMeta.
2307 * Returns: %TRUE if source-info is enabled.
2312 gst_rtp_base_payload_is_source_info_enabled (GstRTPBasePayload * payload)
2314 return payload->priv->source_info;
2319 * gst_rtp_base_payload_get_source_count:
2320 * @payload: a #GstRTPBasePayload
2321 * @buffer: (transfer none): a #GstBuffer, typically the buffer to payload
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.
2327 * Returns: The number of sources.
2332 gst_rtp_base_payload_get_source_count (GstRTPBasePayload * payload,
2337 g_return_val_if_fail (buffer != NULL, 0);
2339 if (gst_rtp_base_payload_is_source_info_enabled (payload)) {
2340 GstRTPSourceMeta *meta = gst_buffer_get_rtp_source_meta (buffer);
2342 count = gst_rtp_source_meta_get_source_count (meta);