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:gstbasertppayload
17 * @short_description: Base class for RTP payloader
19 * Provides a base class for RTP payloaders
28 #include <gst/rtp/gstrtpbuffer.h>
30 #include "gstbasertppayload.h"
32 GST_DEBUG_CATEGORY_STATIC (basertppayload_debug);
33 #define GST_CAT_DEFAULT (basertppayload_debug)
35 #define GST_BASE_RTP_PAYLOAD_GET_PRIVATE(obj) \
36 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_RTP_PAYLOAD, GstBaseRTPPayloadPrivate))
38 struct _GstBaseRTPPayloadPrivate
40 gboolean ts_offset_random;
41 gboolean seqnum_offset_random;
44 gboolean perfect_rtptime;
45 gint notified_first_timestamp;
47 gint64 prop_max_ptime;
48 gint64 caps_max_ptime;
51 /* BaseRTPPayload signals and args */
58 /* FIXME 0.11, a better default is the Ethernet MTU of
59 * 1500 - sizeof(headers) as pointed out by marcelm in IRC:
60 * So an Ethernet MTU of 1500, minus 60 for the max IP, minus 8 for UDP, gives
61 * 1432 bytes or so. And that should be adjusted downward further for other
62 * encapsulations like PPPoE, so 1400 at most.
64 #define DEFAULT_MTU 1400
66 #define DEFAULT_SSRC -1
67 #define DEFAULT_TIMESTAMP_OFFSET -1
68 #define DEFAULT_SEQNUM_OFFSET -1
69 #define DEFAULT_MAX_PTIME -1
70 #define DEFAULT_MIN_PTIME 0
71 #define DEFAULT_PERFECT_RTPTIME TRUE
72 #define DEFAULT_PTIME_MULTIPLE 0
80 PROP_TIMESTAMP_OFFSET,
91 static void gst_basertppayload_class_init (GstBaseRTPPayloadClass * klass);
92 static void gst_basertppayload_base_init (GstBaseRTPPayloadClass * klass);
93 static void gst_basertppayload_init (GstBaseRTPPayload * basertppayload,
95 static void gst_basertppayload_finalize (GObject * object);
97 static gboolean gst_basertppayload_sink_setcaps (GstPad * pad, GstCaps * caps);
98 static GstCaps *gst_basertppayload_sink_getcaps (GstPad * pad,
100 static gboolean gst_basertppayload_event (GstPad * pad, GstEvent * event);
101 static GstFlowReturn gst_basertppayload_chain (GstPad * pad,
104 static void gst_basertppayload_set_property (GObject * object, guint prop_id,
105 const GValue * value, GParamSpec * pspec);
106 static void gst_basertppayload_get_property (GObject * object, guint prop_id,
107 GValue * value, GParamSpec * pspec);
109 static GstStateChangeReturn gst_basertppayload_change_state (GstElement *
110 element, GstStateChange transition);
112 static GstElementClass *parent_class = NULL;
114 /* FIXME 0.11: API should be changed to gst_base_typ_payload_xyz */
117 gst_basertppayload_get_type (void)
119 static GType basertppayload_type = 0;
121 if (g_once_init_enter ((gsize *) & basertppayload_type)) {
122 static const GTypeInfo basertppayload_info = {
123 sizeof (GstBaseRTPPayloadClass),
124 (GBaseInitFunc) gst_basertppayload_base_init,
126 (GClassInitFunc) gst_basertppayload_class_init,
129 sizeof (GstBaseRTPPayload),
131 (GInstanceInitFunc) gst_basertppayload_init,
134 g_once_init_leave ((gsize *) & basertppayload_type,
135 g_type_register_static (GST_TYPE_ELEMENT, "GstBaseRTPPayload",
136 &basertppayload_info, G_TYPE_FLAG_ABSTRACT));
138 return basertppayload_type;
142 gst_basertppayload_base_init (GstBaseRTPPayloadClass * klass)
147 gst_basertppayload_class_init (GstBaseRTPPayloadClass * klass)
149 GObjectClass *gobject_class;
150 GstElementClass *gstelement_class;
152 gobject_class = (GObjectClass *) klass;
153 gstelement_class = (GstElementClass *) klass;
155 g_type_class_add_private (klass, sizeof (GstBaseRTPPayloadPrivate));
157 parent_class = g_type_class_peek_parent (klass);
159 gobject_class->finalize = gst_basertppayload_finalize;
161 gobject_class->set_property = gst_basertppayload_set_property;
162 gobject_class->get_property = gst_basertppayload_get_property;
164 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MTU,
165 g_param_spec_uint ("mtu", "MTU",
166 "Maximum size of one packet",
167 28, G_MAXUINT, DEFAULT_MTU,
168 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PT,
170 g_param_spec_uint ("pt", "payload type",
171 "The payload type of the packets", 0, 0x80, DEFAULT_PT,
172 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
173 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
174 g_param_spec_uint ("ssrc", "SSRC",
175 "The SSRC of the packets (default == random)", 0, G_MAXUINT32,
176 DEFAULT_SSRC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177 g_object_class_install_property (G_OBJECT_CLASS (klass),
178 PROP_TIMESTAMP_OFFSET, g_param_spec_uint ("timestamp-offset",
180 "Offset to add to all outgoing timestamps (default = random)", 0,
181 G_MAXUINT32, DEFAULT_TIMESTAMP_OFFSET,
182 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
184 g_param_spec_int ("seqnum-offset", "Sequence number Offset",
185 "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXUINT16,
186 DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_PTIME,
188 g_param_spec_int64 ("max-ptime", "Max packet time",
189 "Maximum duration of the packet data in ns (-1 = unlimited up to MTU)",
190 -1, G_MAXINT64, DEFAULT_MAX_PTIME,
191 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
193 * GstBaseRTPAudioPayload:min-ptime:
195 * Minimum duration of the packet data in ns (can't go above MTU)
199 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MIN_PTIME,
200 g_param_spec_int64 ("min-ptime", "Min packet time",
201 "Minimum duration of the packet data in ns (can't go above MTU)",
202 0, G_MAXINT64, DEFAULT_MIN_PTIME,
203 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
205 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMESTAMP,
206 g_param_spec_uint ("timestamp", "Timestamp",
207 "The RTP timestamp of the last processed packet",
208 0, G_MAXUINT32, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
209 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
210 g_param_spec_uint ("seqnum", "Sequence number",
211 "The RTP sequence number of the last processed packet",
212 0, G_MAXUINT16, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
215 * GstBaseRTPAudioPayload:perfect-rtptime:
217 * Try to use the offset fields to generate perfect RTP timestamps. when this
218 * option is disabled, RTP timestamps are generated from the GStreamer
219 * timestamps, which could result in RTP timestamps that don't increment with
220 * the amount of data in the packet.
224 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PERFECT_RTPTIME,
225 g_param_spec_boolean ("perfect-rtptime", "Perfect RTP Time",
226 "Generate perfect RTP timestamps when possible",
227 DEFAULT_PERFECT_RTPTIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 * GstBaseRTPAudioPayload:ptime-multiple:
231 * Force buffers to be multiples of this duration in ns (0 disables)
235 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PTIME_MULTIPLE,
236 g_param_spec_int64 ("ptime-multiple", "Packet time multiple",
237 "Force buffers to be multiples of this duration in ns (0 disables)",
238 0, G_MAXINT64, DEFAULT_PTIME_MULTIPLE,
239 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241 gstelement_class->change_state = gst_basertppayload_change_state;
243 GST_DEBUG_CATEGORY_INIT (basertppayload_debug, "basertppayload", 0,
244 "Base class for RTP Payloaders");
248 gst_basertppayload_init (GstBaseRTPPayload * basertppayload, gpointer g_class)
250 GstPadTemplate *templ;
251 GstBaseRTPPayloadPrivate *priv;
253 basertppayload->priv = priv =
254 GST_BASE_RTP_PAYLOAD_GET_PRIVATE (basertppayload);
257 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
258 g_return_if_fail (templ != NULL);
260 basertppayload->srcpad = gst_pad_new_from_template (templ, "src");
261 gst_element_add_pad (GST_ELEMENT (basertppayload), basertppayload->srcpad);
264 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
265 g_return_if_fail (templ != NULL);
267 basertppayload->sinkpad = gst_pad_new_from_template (templ, "sink");
268 gst_pad_set_setcaps_function (basertppayload->sinkpad,
269 gst_basertppayload_sink_setcaps);
270 gst_pad_set_getcaps_function (basertppayload->sinkpad,
271 gst_basertppayload_sink_getcaps);
272 gst_pad_set_event_function (basertppayload->sinkpad,
273 gst_basertppayload_event);
274 gst_pad_set_chain_function (basertppayload->sinkpad,
275 gst_basertppayload_chain);
276 gst_element_add_pad (GST_ELEMENT (basertppayload), basertppayload->sinkpad);
278 basertppayload->seq_rand = g_rand_new_with_seed (g_random_int ());
279 basertppayload->ssrc_rand = g_rand_new_with_seed (g_random_int ());
280 basertppayload->ts_rand = g_rand_new_with_seed (g_random_int ());
282 basertppayload->mtu = DEFAULT_MTU;
283 basertppayload->pt = DEFAULT_PT;
284 basertppayload->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
285 basertppayload->ssrc = DEFAULT_SSRC;
286 basertppayload->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
287 priv->seqnum_offset_random = (basertppayload->seqnum_offset == -1);
288 priv->ts_offset_random = (basertppayload->ts_offset == -1);
289 priv->ssrc_random = (basertppayload->ssrc == -1);
291 basertppayload->max_ptime = DEFAULT_MAX_PTIME;
292 basertppayload->min_ptime = DEFAULT_MIN_PTIME;
293 basertppayload->priv->perfect_rtptime = DEFAULT_PERFECT_RTPTIME;
294 basertppayload->abidata.ABI.ptime_multiple = DEFAULT_PTIME_MULTIPLE;
296 basertppayload->media = NULL;
297 basertppayload->encoding_name = NULL;
299 basertppayload->clock_rate = 0;
301 basertppayload->priv->caps_max_ptime = DEFAULT_MAX_PTIME;
302 basertppayload->priv->prop_max_ptime = DEFAULT_MAX_PTIME;
306 gst_basertppayload_finalize (GObject * object)
308 GstBaseRTPPayload *basertppayload;
310 basertppayload = GST_BASE_RTP_PAYLOAD (object);
312 g_rand_free (basertppayload->seq_rand);
313 basertppayload->seq_rand = NULL;
314 g_rand_free (basertppayload->ssrc_rand);
315 basertppayload->ssrc_rand = NULL;
316 g_rand_free (basertppayload->ts_rand);
317 basertppayload->ts_rand = NULL;
319 g_free (basertppayload->media);
320 basertppayload->media = NULL;
321 g_free (basertppayload->encoding_name);
322 basertppayload->encoding_name = NULL;
324 G_OBJECT_CLASS (parent_class)->finalize (object);
328 gst_basertppayload_sink_setcaps (GstPad * pad, GstCaps * caps)
330 GstBaseRTPPayload *basertppayload;
331 GstBaseRTPPayloadClass *basertppayload_class;
334 GST_DEBUG_OBJECT (pad, "setting caps %" GST_PTR_FORMAT, caps);
335 basertppayload = GST_BASE_RTP_PAYLOAD (gst_pad_get_parent (pad));
336 basertppayload_class = GST_BASE_RTP_PAYLOAD_GET_CLASS (basertppayload);
338 if (basertppayload_class->set_caps)
339 ret = basertppayload_class->set_caps (basertppayload, caps);
341 gst_object_unref (basertppayload);
347 gst_basertppayload_sink_getcaps (GstPad * pad, GstCaps * filter)
349 GstBaseRTPPayload *basertppayload;
350 GstBaseRTPPayloadClass *basertppayload_class;
351 GstCaps *caps = NULL;
353 GST_DEBUG_OBJECT (pad, "getting caps");
355 basertppayload = GST_BASE_RTP_PAYLOAD (gst_pad_get_parent (pad));
356 basertppayload_class = GST_BASE_RTP_PAYLOAD_GET_CLASS (basertppayload);
358 if (basertppayload_class->get_caps)
359 caps = basertppayload_class->get_caps (basertppayload, pad, filter);
362 caps = GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
363 GST_DEBUG_OBJECT (pad,
364 "using pad template %p with caps %p %" GST_PTR_FORMAT,
365 GST_PAD_PAD_TEMPLATE (pad), caps, caps);
368 caps = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
370 caps = gst_caps_ref (caps);
373 gst_object_unref (basertppayload);
379 gst_basertppayload_event (GstPad * pad, GstEvent * event)
381 GstBaseRTPPayload *basertppayload;
382 GstBaseRTPPayloadClass *basertppayload_class;
385 basertppayload = GST_BASE_RTP_PAYLOAD (gst_pad_get_parent (pad));
386 if (G_UNLIKELY (basertppayload == NULL)) {
387 gst_event_unref (event);
390 basertppayload_class = GST_BASE_RTP_PAYLOAD_GET_CLASS (basertppayload);
392 if (basertppayload_class->handle_event) {
393 res = basertppayload_class->handle_event (pad, event);
398 switch (GST_EVENT_TYPE (event)) {
399 case GST_EVENT_FLUSH_START:
400 res = gst_pad_event_default (pad, event);
402 case GST_EVENT_FLUSH_STOP:
403 res = gst_pad_event_default (pad, event);
404 gst_segment_init (&basertppayload->segment, GST_FORMAT_UNDEFINED);
406 case GST_EVENT_SEGMENT:
410 segment = &basertppayload->segment;
411 gst_event_copy_segment (event, segment);
413 GST_DEBUG_OBJECT (basertppayload,
414 "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
418 res = gst_pad_event_default (pad, event);
423 gst_object_unref (basertppayload);
430 gst_basertppayload_chain (GstPad * pad, GstBuffer * buffer)
432 GstBaseRTPPayload *basertppayload;
433 GstBaseRTPPayloadClass *basertppayload_class;
436 basertppayload = GST_BASE_RTP_PAYLOAD (gst_pad_get_parent (pad));
437 basertppayload_class = GST_BASE_RTP_PAYLOAD_GET_CLASS (basertppayload);
439 if (!basertppayload_class->handle_buffer)
442 ret = basertppayload_class->handle_buffer (basertppayload, buffer);
444 gst_object_unref (basertppayload);
451 GST_ELEMENT_ERROR (basertppayload, STREAM, NOT_IMPLEMENTED, (NULL),
452 ("subclass did not implement handle_buffer function"));
453 gst_object_unref (basertppayload);
454 gst_buffer_unref (buffer);
455 return GST_FLOW_ERROR;
460 * gst_basertppayload_set_options:
461 * @payload: a #GstBaseRTPPayload
462 * @media: the media type (typically "audio" or "video")
463 * @dynamic: if the payload type is dynamic
464 * @encoding_name: the encoding name
465 * @clock_rate: the clock rate of the media
467 * Set the rtp options of the payloader. These options will be set in the caps
468 * of the payloader. Subclasses must call this method before calling
469 * gst_basertppayload_push() or gst_basertppayload_set_outcaps().
472 gst_basertppayload_set_options (GstBaseRTPPayload * payload,
473 const gchar * media, gboolean dynamic, const gchar * encoding_name,
476 g_return_if_fail (payload != NULL);
477 g_return_if_fail (clock_rate != 0);
479 g_free (payload->media);
480 payload->media = g_strdup (media);
481 payload->dynamic = dynamic;
482 g_free (payload->encoding_name);
483 payload->encoding_name = g_strdup (encoding_name);
484 payload->clock_rate = clock_rate;
488 copy_fixed (GQuark field_id, const GValue * value, GstStructure * dest)
490 if (gst_value_is_fixed (value)) {
491 gst_structure_id_set_value (dest, field_id, value);
497 update_max_ptime (GstBaseRTPPayload * basertppayload)
499 if (basertppayload->priv->caps_max_ptime != -1 &&
500 basertppayload->priv->prop_max_ptime != -1)
501 basertppayload->max_ptime = MIN (basertppayload->priv->caps_max_ptime,
502 basertppayload->priv->prop_max_ptime);
503 else if (basertppayload->priv->caps_max_ptime != -1)
504 basertppayload->max_ptime = basertppayload->priv->caps_max_ptime;
505 else if (basertppayload->priv->prop_max_ptime != -1)
506 basertppayload->max_ptime = basertppayload->priv->prop_max_ptime;
508 basertppayload->max_ptime = DEFAULT_MAX_PTIME;
512 * gst_basertppayload_set_outcaps:
513 * @payload: a #GstBaseRTPPayload
514 * @fieldname: the first field name or %NULL
517 * Configure the output caps with the optional parameters.
519 * Variable arguments should be in the form field name, field type
520 * (as a GType), value(s). The last variable argument should be NULL.
522 * Returns: %TRUE if the caps could be set.
525 gst_basertppayload_set_outcaps (GstBaseRTPPayload * payload,
526 const gchar * fieldname, ...)
528 GstCaps *srccaps, *peercaps;
531 /* fill in the defaults, their properties cannot be negotiated. */
532 srccaps = gst_caps_new_simple ("application/x-rtp",
533 "media", G_TYPE_STRING, payload->media,
534 "clock-rate", G_TYPE_INT, payload->clock_rate,
535 "encoding-name", G_TYPE_STRING, payload->encoding_name, NULL);
537 GST_DEBUG_OBJECT (payload, "defaults: %" GST_PTR_FORMAT, srccaps);
542 /* override with custom properties */
543 va_start (varargs, fieldname);
544 gst_caps_set_simple_valist (srccaps, fieldname, varargs);
547 GST_DEBUG_OBJECT (payload, "custom added: %" GST_PTR_FORMAT, srccaps);
550 payload->priv->caps_max_ptime = DEFAULT_MAX_PTIME;
551 payload->abidata.ABI.ptime = 0;
553 /* the peer caps can override some of the defaults */
554 peercaps = gst_pad_peer_get_caps (payload->srcpad, srccaps);
555 if (peercaps == NULL) {
556 /* no peer caps, just add the other properties */
557 gst_caps_set_simple (srccaps,
558 "payload", G_TYPE_INT, GST_BASE_RTP_PAYLOAD_PT (payload),
559 "ssrc", G_TYPE_UINT, payload->current_ssrc,
560 "clock-base", G_TYPE_UINT, payload->ts_base,
561 "seqnum-base", G_TYPE_UINT, payload->seqnum_base, NULL);
563 GST_DEBUG_OBJECT (payload, "no peer caps: %" GST_PTR_FORMAT, srccaps);
569 guint max_ptime, ptime;
571 /* peer provides caps we can use to fixate. They are already intersected
572 * with our srccaps, just make them writable */
573 temp = gst_caps_make_writable (peercaps);
574 gst_caps_unref (srccaps);
576 if (gst_caps_is_empty (temp)) {
577 gst_caps_unref (temp);
581 /* now fixate, start by taking the first caps */
582 gst_caps_truncate (temp);
584 /* get first structure */
585 s = gst_caps_get_structure (temp, 0);
587 if (gst_structure_get_uint (s, "maxptime", &max_ptime))
588 payload->priv->caps_max_ptime = max_ptime * GST_MSECOND;
590 if (gst_structure_get_uint (s, "ptime", &ptime))
591 payload->abidata.ABI.ptime = ptime * GST_MSECOND;
593 if (gst_structure_get_int (s, "payload", &pt)) {
595 GST_BASE_RTP_PAYLOAD_PT (payload) = pt;
596 GST_LOG_OBJECT (payload, "using peer pt %d", pt);
598 if (gst_structure_has_field (s, "payload")) {
599 /* can only fixate if there is a field */
600 gst_structure_fixate_field_nearest_int (s, "payload",
601 GST_BASE_RTP_PAYLOAD_PT (payload));
602 gst_structure_get_int (s, "payload", &pt);
603 GST_LOG_OBJECT (payload, "using peer pt %d", pt);
605 /* no pt field, use the internal pt */
606 pt = GST_BASE_RTP_PAYLOAD_PT (payload);
607 gst_structure_set (s, "payload", G_TYPE_INT, pt, NULL);
608 GST_LOG_OBJECT (payload, "using internal pt %d", pt);
612 if (gst_structure_has_field_typed (s, "ssrc", G_TYPE_UINT)) {
613 value = gst_structure_get_value (s, "ssrc");
614 payload->current_ssrc = g_value_get_uint (value);
615 GST_LOG_OBJECT (payload, "using peer ssrc %08x", payload->current_ssrc);
617 /* FIXME, fixate_nearest_uint would be even better */
618 gst_structure_set (s, "ssrc", G_TYPE_UINT, payload->current_ssrc, NULL);
619 GST_LOG_OBJECT (payload, "using internal ssrc %08x",
620 payload->current_ssrc);
623 if (gst_structure_has_field_typed (s, "clock-base", G_TYPE_UINT)) {
624 value = gst_structure_get_value (s, "clock-base");
625 payload->ts_base = g_value_get_uint (value);
626 GST_LOG_OBJECT (payload, "using peer clock-base %u", payload->ts_base);
628 /* FIXME, fixate_nearest_uint would be even better */
629 gst_structure_set (s, "clock-base", G_TYPE_UINT, payload->ts_base, NULL);
630 GST_LOG_OBJECT (payload, "using internal clock-base %u",
633 if (gst_structure_has_field_typed (s, "seqnum-base", G_TYPE_UINT)) {
634 value = gst_structure_get_value (s, "seqnum-base");
635 payload->seqnum_base = g_value_get_uint (value);
636 GST_LOG_OBJECT (payload, "using peer seqnum-base %u",
637 payload->seqnum_base);
639 /* FIXME, fixate_nearest_uint would be even better */
640 gst_structure_set (s, "seqnum-base", G_TYPE_UINT, payload->seqnum_base,
642 GST_LOG_OBJECT (payload, "using internal seqnum-base %u",
643 payload->seqnum_base);
646 /* make the target caps by copying over all the fixed caps, removing the
648 srccaps = gst_caps_new_simple (gst_structure_get_name (s), NULL);
649 d = gst_caps_get_structure (srccaps, 0);
651 gst_structure_foreach (s, (GstStructureForeachFunc) copy_fixed, d);
653 gst_caps_unref (temp);
655 GST_DEBUG_OBJECT (payload, "with peer caps: %" GST_PTR_FORMAT, srccaps);
658 update_max_ptime (payload);
660 res = gst_pad_set_caps (GST_BASE_RTP_PAYLOAD_SRCPAD (payload), srccaps);
661 gst_caps_unref (srccaps);
667 * gst_basertppayload_is_filled:
668 * @payload: a #GstBaseRTPPayload
669 * @size: the size of the packet
670 * @duration: the duration of the packet
672 * Check if the packet with @size and @duration would exceed the configured
675 * Returns: %TRUE if the packet of @size and @duration would exceed the
676 * configured MTU or max_ptime.
679 gst_basertppayload_is_filled (GstBaseRTPPayload * payload,
680 guint size, GstClockTime duration)
682 if (size > payload->mtu)
685 if (payload->max_ptime != -1 && duration >= payload->max_ptime)
693 GstBaseRTPPayload *payload;
697 GstClockTime timestamp;
703 find_timestamp (GstBuffer ** buffer, guint idx, HeaderData * data)
705 data->timestamp = GST_BUFFER_TIMESTAMP (*buffer);
706 data->offset = GST_BUFFER_OFFSET (*buffer);
708 /* stop when we find a timestamp. We take whatever offset is associated with
709 * the timestamp (if any) to do perfect timestamps when we need to. */
710 if (data->timestamp != -1)
717 set_headers (GstBuffer ** buffer, guint group, guint idx, HeaderData * data)
721 gst_rtp_buffer_map (*buffer, GST_MAP_WRITE, &rtp);
722 gst_rtp_buffer_set_ssrc (&rtp, data->ssrc);
723 gst_rtp_buffer_set_payload_type (&rtp, data->pt);
724 gst_rtp_buffer_set_seq (&rtp, data->seqnum);
725 gst_rtp_buffer_set_timestamp (&rtp, data->rtptime);
726 gst_rtp_buffer_unmap (&rtp);
728 /* increment the seqnum for each buffer */
734 /* Updates the SSRC, payload type, seqnum and timestamp of the RTP buffer
735 * before the buffer is pushed. */
737 gst_basertppayload_prepare_push (GstBaseRTPPayload * payload,
738 gpointer obj, gboolean is_list)
740 GstBaseRTPPayloadPrivate *priv;
743 if (payload->clock_rate == 0)
746 priv = payload->priv;
748 /* update first, so that the property is set to the last
750 payload->seqnum = priv->next_seqnum;
752 /* fill in the fields we want to set on all headers */
753 data.payload = payload;
754 data.seqnum = payload->seqnum;
755 data.ssrc = payload->current_ssrc;
756 data.pt = payload->pt;
758 /* find the first buffer with a timestamp */
761 data.offset = GST_BUFFER_OFFSET_NONE;
762 gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj),
763 (GstBufferListFunc) find_timestamp, &data);
765 data.timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (obj));
766 data.offset = GST_BUFFER_OFFSET (GST_BUFFER_CAST (obj));
769 /* convert to RTP time */
770 if (priv->perfect_rtptime && data.offset != GST_BUFFER_OFFSET_NONE) {
771 /* if we have an offset, use that for making an RTP timestamp */
772 data.rtptime = payload->ts_base + data.offset;
773 GST_LOG_OBJECT (payload,
774 "Using offset %" G_GUINT64_FORMAT " for RTP timestamp", data.offset);
775 } else if (GST_CLOCK_TIME_IS_VALID (data.timestamp)) {
778 /* no offset, use the gstreamer timestamp */
779 rtime = gst_segment_to_running_time (&payload->segment, GST_FORMAT_TIME,
783 GST_LOG_OBJECT (payload, "Clipped timestamp, using base RTP timestamp");
786 GST_LOG_OBJECT (payload,
787 "Using running_time %" GST_TIME_FORMAT " for RTP timestamp",
788 GST_TIME_ARGS (rtime));
790 gst_util_uint64_scale_int (rtime, payload->clock_rate, GST_SECOND);
792 /* add running_time in clock-rate units to the base timestamp */
793 data.rtptime = payload->ts_base + rtime;
795 GST_LOG_OBJECT (payload,
796 "Using previous RTP timestamp %" G_GUINT32_FORMAT, payload->timestamp);
797 /* no timestamp to convert, take previous timestamp */
798 data.rtptime = payload->timestamp;
801 /* set ssrc, payload type, seq number, caps and rtptime */
803 gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj),
804 (GstBufferListFunc) set_headers, &data);
806 GstBuffer *buf = GST_BUFFER_CAST (obj);
807 set_headers (&buf, 0, 0, &data);
810 priv->next_seqnum = data.seqnum;
811 payload->timestamp = data.rtptime;
813 GST_LOG_OBJECT (payload,
814 "Preparing to push packet with size %d, seq=%d, rtptime=%u, timestamp %"
815 GST_TIME_FORMAT, (is_list) ? -1 :
816 gst_buffer_get_size (GST_BUFFER (obj)), payload->seqnum, data.rtptime,
817 GST_TIME_ARGS (data.timestamp));
819 if (g_atomic_int_compare_and_exchange (&payload->
820 priv->notified_first_timestamp, 1, 0)) {
821 g_object_notify (G_OBJECT (payload), "timestamp");
822 g_object_notify (G_OBJECT (payload), "seqnum");
830 GST_ELEMENT_ERROR (payload, STREAM, NOT_IMPLEMENTED, (NULL),
831 ("subclass did not specify clock-rate"));
832 return GST_FLOW_ERROR;
837 * gst_basertppayload_push_list:
838 * @payload: a #GstBaseRTPPayload
839 * @list: a #GstBufferList
841 * Push @list to the peer element of the payloader. The SSRC, payload type,
842 * seqnum and timestamp of the RTP buffer will be updated first.
844 * This function takes ownership of @list.
846 * Returns: a #GstFlowReturn.
851 gst_basertppayload_push_list (GstBaseRTPPayload * payload, GstBufferList * list)
855 res = gst_basertppayload_prepare_push (payload, list, TRUE);
857 if (G_LIKELY (res == GST_FLOW_OK))
858 res = gst_pad_push_list (payload->srcpad, list);
860 gst_buffer_list_unref (list);
866 * gst_basertppayload_push:
867 * @payload: a #GstBaseRTPPayload
868 * @buffer: a #GstBuffer
870 * Push @buffer to the peer element of the payloader. The SSRC, payload type,
871 * seqnum and timestamp of the RTP buffer will be updated first.
873 * This function takes ownership of @buffer.
875 * Returns: a #GstFlowReturn.
878 gst_basertppayload_push (GstBaseRTPPayload * payload, GstBuffer * buffer)
882 res = gst_basertppayload_prepare_push (payload, buffer, FALSE);
884 if (G_LIKELY (res == GST_FLOW_OK))
885 res = gst_pad_push (payload->srcpad, buffer);
887 gst_buffer_unref (buffer);
893 gst_basertppayload_set_property (GObject * object, guint prop_id,
894 const GValue * value, GParamSpec * pspec)
896 GstBaseRTPPayload *basertppayload;
897 GstBaseRTPPayloadPrivate *priv;
900 basertppayload = GST_BASE_RTP_PAYLOAD (object);
901 priv = basertppayload->priv;
905 basertppayload->mtu = g_value_get_uint (value);
908 basertppayload->pt = g_value_get_uint (value);
911 val = g_value_get_uint (value);
912 basertppayload->ssrc = val;
913 priv->ssrc_random = FALSE;
915 case PROP_TIMESTAMP_OFFSET:
916 val = g_value_get_uint (value);
917 basertppayload->ts_offset = val;
918 priv->ts_offset_random = FALSE;
920 case PROP_SEQNUM_OFFSET:
921 val = g_value_get_int (value);
922 basertppayload->seqnum_offset = val;
923 priv->seqnum_offset_random = (val == -1);
924 GST_DEBUG_OBJECT (basertppayload, "seqnum offset 0x%04x, random %d",
925 basertppayload->seqnum_offset, priv->seqnum_offset_random);
928 basertppayload->priv->prop_max_ptime = g_value_get_int64 (value);
929 update_max_ptime (basertppayload);
932 basertppayload->min_ptime = g_value_get_int64 (value);
934 case PROP_PERFECT_RTPTIME:
935 priv->perfect_rtptime = g_value_get_boolean (value);
937 case PROP_PTIME_MULTIPLE:
938 basertppayload->abidata.ABI.ptime_multiple = g_value_get_int64 (value);
941 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
947 gst_basertppayload_get_property (GObject * object, guint prop_id,
948 GValue * value, GParamSpec * pspec)
950 GstBaseRTPPayload *basertppayload;
951 GstBaseRTPPayloadPrivate *priv;
953 basertppayload = GST_BASE_RTP_PAYLOAD (object);
954 priv = basertppayload->priv;
958 g_value_set_uint (value, basertppayload->mtu);
961 g_value_set_uint (value, basertppayload->pt);
964 if (priv->ssrc_random)
965 g_value_set_uint (value, -1);
967 g_value_set_uint (value, basertppayload->ssrc);
969 case PROP_TIMESTAMP_OFFSET:
970 if (priv->ts_offset_random)
971 g_value_set_uint (value, -1);
973 g_value_set_uint (value, (guint32) basertppayload->ts_offset);
975 case PROP_SEQNUM_OFFSET:
976 if (priv->seqnum_offset_random)
977 g_value_set_int (value, -1);
979 g_value_set_int (value, (guint16) basertppayload->seqnum_offset);
982 g_value_set_int64 (value, basertppayload->max_ptime);
985 g_value_set_int64 (value, basertppayload->min_ptime);
988 g_value_set_uint (value, basertppayload->timestamp);
991 g_value_set_uint (value, basertppayload->seqnum);
993 case PROP_PERFECT_RTPTIME:
994 g_value_set_boolean (value, priv->perfect_rtptime);
996 case PROP_PTIME_MULTIPLE:
997 g_value_set_int64 (value, basertppayload->abidata.ABI.ptime_multiple);
1000 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1005 static GstStateChangeReturn
1006 gst_basertppayload_change_state (GstElement * element,
1007 GstStateChange transition)
1009 GstBaseRTPPayload *basertppayload;
1010 GstBaseRTPPayloadPrivate *priv;
1011 GstStateChangeReturn ret;
1013 basertppayload = GST_BASE_RTP_PAYLOAD (element);
1014 priv = basertppayload->priv;
1016 switch (transition) {
1017 case GST_STATE_CHANGE_NULL_TO_READY:
1019 case GST_STATE_CHANGE_READY_TO_PAUSED:
1020 gst_segment_init (&basertppayload->segment, GST_FORMAT_UNDEFINED);
1022 if (priv->seqnum_offset_random)
1023 basertppayload->seqnum_base = g_random_int_range (0, G_MAXUINT16);
1025 basertppayload->seqnum_base = basertppayload->seqnum_offset;
1026 priv->next_seqnum = basertppayload->seqnum_base;
1027 basertppayload->seqnum = basertppayload->seqnum_base;
1029 if (priv->ssrc_random)
1030 basertppayload->current_ssrc = g_random_int ();
1032 basertppayload->current_ssrc = basertppayload->ssrc;
1034 if (priv->ts_offset_random)
1035 basertppayload->ts_base = g_random_int ();
1037 basertppayload->ts_base = basertppayload->ts_offset;
1038 basertppayload->timestamp = basertppayload->ts_base;
1039 g_atomic_int_set (&basertppayload->priv->notified_first_timestamp, 1);
1045 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1047 switch (transition) {
1048 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1049 g_atomic_int_set (&basertppayload->priv->notified_first_timestamp, 1);
1051 case GST_STATE_CHANGE_READY_TO_NULL: