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 * @short_description: Base class for RTP payloader
19 * Provides a base class for RTP payloaders
28 #include <gst/rtp/gstrtpbuffer.h>
30 #include "gstrtpbasepayload.h"
32 GST_DEBUG_CATEGORY_STATIC (rtpbasepayload_debug);
33 #define GST_CAT_DEFAULT (rtpbasepayload_debug)
35 #define GST_RTP_BASE_PAYLOAD_GET_PRIVATE(obj) \
36 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BASE_PAYLOAD, GstRTPBasePayloadPrivate))
38 struct _GstRTPBasePayloadPrivate
40 gboolean ts_offset_random;
41 gboolean seqnum_offset_random;
44 gboolean perfect_rtptime;
45 gint notified_first_timestamp;
50 gint64 prop_max_ptime;
51 gint64 caps_max_ptime;
54 /* RTPBasePayload signals and args */
61 /* FIXME 0.11, a better default is the Ethernet MTU of
62 * 1500 - sizeof(headers) as pointed out by marcelm in IRC:
63 * So an Ethernet MTU of 1500, minus 60 for the max IP, minus 8 for UDP, gives
64 * 1432 bytes or so. And that should be adjusted downward further for other
65 * encapsulations like PPPoE, so 1400 at most.
67 #define DEFAULT_MTU 1400
69 #define DEFAULT_SSRC -1
70 #define DEFAULT_TIMESTAMP_OFFSET -1
71 #define DEFAULT_SEQNUM_OFFSET -1
72 #define DEFAULT_MAX_PTIME -1
73 #define DEFAULT_MIN_PTIME 0
74 #define DEFAULT_PERFECT_RTPTIME TRUE
75 #define DEFAULT_PTIME_MULTIPLE 0
83 PROP_TIMESTAMP_OFFSET,
94 static void gst_rtp_base_payload_class_init (GstRTPBasePayloadClass * klass);
95 static void gst_rtp_base_payload_init (GstRTPBasePayload * rtpbasepayload,
97 static void gst_rtp_base_payload_finalize (GObject * object);
99 static GstCaps *gst_rtp_base_payload_sink_getcaps (GstPad * pad,
101 static gboolean gst_rtp_base_payload_event_default (GstRTPBasePayload *
102 rtpbasepayload, GstEvent * event);
103 static gboolean gst_rtp_base_payload_event (GstPad * pad, GstEvent * event);
104 static GstFlowReturn gst_rtp_base_payload_chain (GstPad * pad,
106 static GstCaps *gst_rtp_base_payload_getcaps_default (GstRTPBasePayload *
107 rtpbasepayload, GstPad * pad, GstCaps * filter);
109 static void gst_rtp_base_payload_set_property (GObject * object, guint prop_id,
110 const GValue * value, GParamSpec * pspec);
111 static void gst_rtp_base_payload_get_property (GObject * object, guint prop_id,
112 GValue * value, GParamSpec * pspec);
114 static GstStateChangeReturn gst_rtp_base_payload_change_state (GstElement *
115 element, GstStateChange transition);
117 static GstElementClass *parent_class = NULL;
119 /* FIXME 0.11: API should be changed to gst_base_typ_payload_xyz */
122 gst_rtp_base_payload_get_type (void)
124 static GType rtpbasepayload_type = 0;
126 if (g_once_init_enter ((gsize *) & rtpbasepayload_type)) {
127 static const GTypeInfo rtpbasepayload_info = {
128 sizeof (GstRTPBasePayloadClass),
131 (GClassInitFunc) gst_rtp_base_payload_class_init,
134 sizeof (GstRTPBasePayload),
136 (GInstanceInitFunc) gst_rtp_base_payload_init,
139 g_once_init_leave ((gsize *) & rtpbasepayload_type,
140 g_type_register_static (GST_TYPE_ELEMENT, "GstRTPBasePayload",
141 &rtpbasepayload_info, G_TYPE_FLAG_ABSTRACT));
143 return rtpbasepayload_type;
147 gst_rtp_base_payload_class_init (GstRTPBasePayloadClass * 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 (GstRTPBasePayloadPrivate));
157 parent_class = g_type_class_peek_parent (klass);
159 gobject_class->finalize = gst_rtp_base_payload_finalize;
161 gobject_class->set_property = gst_rtp_base_payload_set_property;
162 gobject_class->get_property = gst_rtp_base_payload_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 * GstRTPBaseAudioPayload: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 * GstRTPBaseAudioPayload: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 * GstRTPBaseAudioPayload: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_rtp_base_payload_change_state;
243 klass->get_caps = gst_rtp_base_payload_getcaps_default;
244 klass->handle_event = gst_rtp_base_payload_event_default;
246 GST_DEBUG_CATEGORY_INIT (rtpbasepayload_debug, "rtpbasepayload", 0,
247 "Base class for RTP Payloaders");
251 gst_rtp_base_payload_init (GstRTPBasePayload * rtpbasepayload, gpointer g_class)
253 GstPadTemplate *templ;
254 GstRTPBasePayloadPrivate *priv;
256 rtpbasepayload->priv = priv =
257 GST_RTP_BASE_PAYLOAD_GET_PRIVATE (rtpbasepayload);
260 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
261 g_return_if_fail (templ != NULL);
263 rtpbasepayload->srcpad = gst_pad_new_from_template (templ, "src");
264 gst_element_add_pad (GST_ELEMENT (rtpbasepayload), rtpbasepayload->srcpad);
267 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
268 g_return_if_fail (templ != NULL);
270 rtpbasepayload->sinkpad = gst_pad_new_from_template (templ, "sink");
271 gst_pad_set_getcaps_function (rtpbasepayload->sinkpad,
272 gst_rtp_base_payload_sink_getcaps);
273 gst_pad_set_event_function (rtpbasepayload->sinkpad,
274 gst_rtp_base_payload_event);
275 gst_pad_set_chain_function (rtpbasepayload->sinkpad,
276 gst_rtp_base_payload_chain);
277 gst_element_add_pad (GST_ELEMENT (rtpbasepayload), rtpbasepayload->sinkpad);
279 rtpbasepayload->mtu = DEFAULT_MTU;
280 rtpbasepayload->pt = DEFAULT_PT;
281 rtpbasepayload->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
282 rtpbasepayload->ssrc = DEFAULT_SSRC;
283 rtpbasepayload->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
284 priv->seqnum_offset_random = (rtpbasepayload->seqnum_offset == -1);
285 priv->ts_offset_random = (rtpbasepayload->ts_offset == -1);
286 priv->ssrc_random = (rtpbasepayload->ssrc == -1);
288 rtpbasepayload->max_ptime = DEFAULT_MAX_PTIME;
289 rtpbasepayload->min_ptime = DEFAULT_MIN_PTIME;
290 rtpbasepayload->priv->perfect_rtptime = DEFAULT_PERFECT_RTPTIME;
291 rtpbasepayload->ptime_multiple = DEFAULT_PTIME_MULTIPLE;
292 rtpbasepayload->priv->base_offset = GST_BUFFER_OFFSET_NONE;
293 rtpbasepayload->priv->base_rtime = GST_BUFFER_OFFSET_NONE;
295 rtpbasepayload->media = NULL;
296 rtpbasepayload->encoding_name = NULL;
298 rtpbasepayload->clock_rate = 0;
300 rtpbasepayload->priv->caps_max_ptime = DEFAULT_MAX_PTIME;
301 rtpbasepayload->priv->prop_max_ptime = DEFAULT_MAX_PTIME;
305 gst_rtp_base_payload_finalize (GObject * object)
307 GstRTPBasePayload *rtpbasepayload;
309 rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
311 g_free (rtpbasepayload->media);
312 rtpbasepayload->media = NULL;
313 g_free (rtpbasepayload->encoding_name);
314 rtpbasepayload->encoding_name = NULL;
316 G_OBJECT_CLASS (parent_class)->finalize (object);
320 gst_rtp_base_payload_getcaps_default (GstRTPBasePayload * rtpbasepayload,
321 GstPad * pad, GstCaps * filter)
325 caps = GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
326 GST_DEBUG_OBJECT (pad,
327 "using pad template %p with caps %p %" GST_PTR_FORMAT,
328 GST_PAD_PAD_TEMPLATE (pad), caps, caps);
331 caps = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
333 caps = gst_caps_ref (caps);
339 gst_rtp_base_payload_sink_getcaps (GstPad * pad, GstCaps * filter)
341 GstRTPBasePayload *rtpbasepayload;
342 GstRTPBasePayloadClass *rtpbasepayload_class;
343 GstCaps *caps = NULL;
345 GST_DEBUG_OBJECT (pad, "getting caps");
347 rtpbasepayload = GST_RTP_BASE_PAYLOAD (gst_pad_get_parent (pad));
348 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
350 if (rtpbasepayload_class->get_caps)
351 caps = rtpbasepayload_class->get_caps (rtpbasepayload, pad, filter);
353 gst_object_unref (rtpbasepayload);
359 gst_rtp_base_payload_event_default (GstRTPBasePayload * rtpbasepayload,
362 gboolean res = FALSE;
364 switch (GST_EVENT_TYPE (event)) {
365 case GST_EVENT_FLUSH_START:
366 res = gst_pad_event_default (rtpbasepayload->sinkpad, event);
368 case GST_EVENT_FLUSH_STOP:
369 res = gst_pad_event_default (rtpbasepayload->sinkpad, event);
370 gst_segment_init (&rtpbasepayload->segment, GST_FORMAT_UNDEFINED);
374 GstRTPBasePayloadClass *rtpbasepayload_class;
377 gst_event_parse_caps (event, &caps);
378 GST_DEBUG_OBJECT (rtpbasepayload, "setting caps %" GST_PTR_FORMAT, caps);
380 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
381 if (rtpbasepayload_class->set_caps)
382 res = rtpbasepayload_class->set_caps (rtpbasepayload, caps);
384 gst_event_unref (event);
387 case GST_EVENT_SEGMENT:
391 segment = &rtpbasepayload->segment;
392 gst_event_copy_segment (event, segment);
394 rtpbasepayload->priv->base_offset = GST_BUFFER_OFFSET_NONE;
396 GST_DEBUG_OBJECT (rtpbasepayload,
397 "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
398 res = gst_pad_event_default (rtpbasepayload->sinkpad, event);
402 res = gst_pad_event_default (rtpbasepayload->sinkpad, event);
409 gst_rtp_base_payload_event (GstPad * pad, GstEvent * event)
411 GstRTPBasePayload *rtpbasepayload;
412 GstRTPBasePayloadClass *rtpbasepayload_class;
413 gboolean res = FALSE;
415 rtpbasepayload = GST_RTP_BASE_PAYLOAD (gst_pad_get_parent (pad));
416 if (G_UNLIKELY (rtpbasepayload == NULL)) {
417 gst_event_unref (event);
421 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
423 if (rtpbasepayload_class->handle_event)
424 res = rtpbasepayload_class->handle_event (rtpbasepayload, event);
426 gst_event_unref (event);
428 gst_object_unref (rtpbasepayload);
435 gst_rtp_base_payload_chain (GstPad * pad, GstBuffer * buffer)
437 GstRTPBasePayload *rtpbasepayload;
438 GstRTPBasePayloadClass *rtpbasepayload_class;
441 rtpbasepayload = GST_RTP_BASE_PAYLOAD (gst_pad_get_parent (pad));
442 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
444 if (!rtpbasepayload_class->handle_buffer)
447 ret = rtpbasepayload_class->handle_buffer (rtpbasepayload, buffer);
449 gst_object_unref (rtpbasepayload);
456 GST_ELEMENT_ERROR (rtpbasepayload, STREAM, NOT_IMPLEMENTED, (NULL),
457 ("subclass did not implement handle_buffer function"));
458 gst_object_unref (rtpbasepayload);
459 gst_buffer_unref (buffer);
460 return GST_FLOW_ERROR;
465 * gst_rtp_base_payload_set_options:
466 * @payload: a #GstRTPBasePayload
467 * @media: the media type (typically "audio" or "video")
468 * @dynamic: if the payload type is dynamic
469 * @encoding_name: the encoding name
470 * @clock_rate: the clock rate of the media
472 * Set the rtp options of the payloader. These options will be set in the caps
473 * of the payloader. Subclasses must call this method before calling
474 * gst_rtp_base_payload_push() or gst_rtp_base_payload_set_outcaps().
477 gst_rtp_base_payload_set_options (GstRTPBasePayload * payload,
478 const gchar * media, gboolean dynamic, const gchar * encoding_name,
481 g_return_if_fail (payload != NULL);
482 g_return_if_fail (clock_rate != 0);
484 g_free (payload->media);
485 payload->media = g_strdup (media);
486 payload->dynamic = dynamic;
487 g_free (payload->encoding_name);
488 payload->encoding_name = g_strdup (encoding_name);
489 payload->clock_rate = clock_rate;
493 copy_fixed (GQuark field_id, const GValue * value, GstStructure * dest)
495 if (gst_value_is_fixed (value)) {
496 gst_structure_id_set_value (dest, field_id, value);
502 update_max_ptime (GstRTPBasePayload * rtpbasepayload)
504 if (rtpbasepayload->priv->caps_max_ptime != -1 &&
505 rtpbasepayload->priv->prop_max_ptime != -1)
506 rtpbasepayload->max_ptime = MIN (rtpbasepayload->priv->caps_max_ptime,
507 rtpbasepayload->priv->prop_max_ptime);
508 else if (rtpbasepayload->priv->caps_max_ptime != -1)
509 rtpbasepayload->max_ptime = rtpbasepayload->priv->caps_max_ptime;
510 else if (rtpbasepayload->priv->prop_max_ptime != -1)
511 rtpbasepayload->max_ptime = rtpbasepayload->priv->prop_max_ptime;
513 rtpbasepayload->max_ptime = DEFAULT_MAX_PTIME;
517 * gst_rtp_base_payload_set_outcaps:
518 * @payload: a #GstRTPBasePayload
519 * @fieldname: the first field name or %NULL
522 * Configure the output caps with the optional parameters.
524 * Variable arguments should be in the form field name, field type
525 * (as a GType), value(s). The last variable argument should be NULL.
527 * Returns: %TRUE if the caps could be set.
530 gst_rtp_base_payload_set_outcaps (GstRTPBasePayload * payload,
531 const gchar * fieldname, ...)
533 GstCaps *srccaps, *peercaps;
536 /* fill in the defaults, their properties cannot be negotiated. */
537 srccaps = gst_caps_new_simple ("application/x-rtp",
538 "media", G_TYPE_STRING, payload->media,
539 "clock-rate", G_TYPE_INT, payload->clock_rate,
540 "encoding-name", G_TYPE_STRING, payload->encoding_name, NULL);
542 GST_DEBUG_OBJECT (payload, "defaults: %" GST_PTR_FORMAT, srccaps);
547 /* override with custom properties */
548 va_start (varargs, fieldname);
549 gst_caps_set_simple_valist (srccaps, fieldname, varargs);
552 GST_DEBUG_OBJECT (payload, "custom added: %" GST_PTR_FORMAT, srccaps);
555 payload->priv->caps_max_ptime = DEFAULT_MAX_PTIME;
558 /* the peer caps can override some of the defaults */
559 peercaps = gst_pad_peer_get_caps (payload->srcpad, srccaps);
560 if (peercaps == NULL) {
561 /* no peer caps, just add the other properties */
562 gst_caps_set_simple (srccaps,
563 "payload", G_TYPE_INT, GST_RTP_BASE_PAYLOAD_PT (payload),
564 "ssrc", G_TYPE_UINT, payload->current_ssrc,
565 "timestamp-offset", G_TYPE_UINT, payload->ts_base,
566 "seqnum-offset", G_TYPE_UINT, payload->seqnum_base, NULL);
568 GST_DEBUG_OBJECT (payload, "no peer caps: %" GST_PTR_FORMAT, srccaps);
574 guint max_ptime, ptime;
576 /* peer provides caps we can use to fixate. They are already intersected
577 * with our srccaps, just make them writable */
578 temp = gst_caps_make_writable (peercaps);
579 gst_caps_unref (srccaps);
581 if (gst_caps_is_empty (temp)) {
582 gst_caps_unref (temp);
586 /* now fixate, start by taking the first caps */
587 gst_caps_truncate (temp);
589 /* get first structure */
590 s = gst_caps_get_structure (temp, 0);
592 if (gst_structure_get_uint (s, "maxptime", &max_ptime))
593 payload->priv->caps_max_ptime = max_ptime * GST_MSECOND;
595 if (gst_structure_get_uint (s, "ptime", &ptime))
596 payload->ptime = ptime * GST_MSECOND;
598 if (gst_structure_get_int (s, "payload", &pt)) {
600 GST_RTP_BASE_PAYLOAD_PT (payload) = pt;
601 GST_LOG_OBJECT (payload, "using peer pt %d", pt);
603 if (gst_structure_has_field (s, "payload")) {
604 /* can only fixate if there is a field */
605 gst_structure_fixate_field_nearest_int (s, "payload",
606 GST_RTP_BASE_PAYLOAD_PT (payload));
607 gst_structure_get_int (s, "payload", &pt);
608 GST_LOG_OBJECT (payload, "using peer pt %d", pt);
610 /* no pt field, use the internal pt */
611 pt = GST_RTP_BASE_PAYLOAD_PT (payload);
612 gst_structure_set (s, "payload", G_TYPE_INT, pt, NULL);
613 GST_LOG_OBJECT (payload, "using internal pt %d", pt);
617 if (gst_structure_has_field_typed (s, "ssrc", G_TYPE_UINT)) {
618 value = gst_structure_get_value (s, "ssrc");
619 payload->current_ssrc = g_value_get_uint (value);
620 GST_LOG_OBJECT (payload, "using peer ssrc %08x", payload->current_ssrc);
622 /* FIXME, fixate_nearest_uint would be even better */
623 gst_structure_set (s, "ssrc", G_TYPE_UINT, payload->current_ssrc, NULL);
624 GST_LOG_OBJECT (payload, "using internal ssrc %08x",
625 payload->current_ssrc);
628 if (gst_structure_has_field_typed (s, "timestamp-offset", G_TYPE_UINT)) {
629 value = gst_structure_get_value (s, "timestamp-offset");
630 payload->ts_base = g_value_get_uint (value);
631 GST_LOG_OBJECT (payload, "using peer timestamp-offset %u",
634 /* FIXME, fixate_nearest_uint would be even better */
635 gst_structure_set (s, "timestamp-offset", G_TYPE_UINT, payload->ts_base,
637 GST_LOG_OBJECT (payload, "using internal timestamp-offset %u",
640 if (gst_structure_has_field_typed (s, "seqnum-offset", G_TYPE_UINT)) {
641 value = gst_structure_get_value (s, "seqnum-offset");
642 payload->seqnum_base = g_value_get_uint (value);
643 GST_LOG_OBJECT (payload, "using peer seqnum-offset %u",
644 payload->seqnum_base);
646 /* FIXME, fixate_nearest_uint would be even better */
647 gst_structure_set (s, "seqnum-offset", G_TYPE_UINT, payload->seqnum_base,
649 GST_LOG_OBJECT (payload, "using internal seqnum-offset %u",
650 payload->seqnum_base);
653 /* make the target caps by copying over all the fixed caps, removing the
655 srccaps = gst_caps_new_empty_simple (gst_structure_get_name (s));
656 d = gst_caps_get_structure (srccaps, 0);
658 gst_structure_foreach (s, (GstStructureForeachFunc) copy_fixed, d);
660 gst_caps_unref (temp);
662 GST_DEBUG_OBJECT (payload, "with peer caps: %" GST_PTR_FORMAT, srccaps);
665 update_max_ptime (payload);
667 res = gst_pad_set_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload), srccaps);
668 gst_caps_unref (srccaps);
674 * gst_rtp_base_payload_is_filled:
675 * @payload: a #GstRTPBasePayload
676 * @size: the size of the packet
677 * @duration: the duration of the packet
679 * Check if the packet with @size and @duration would exceed the configured
682 * Returns: %TRUE if the packet of @size and @duration would exceed the
683 * configured MTU or max_ptime.
686 gst_rtp_base_payload_is_filled (GstRTPBasePayload * payload,
687 guint size, GstClockTime duration)
689 if (size > payload->mtu)
692 if (payload->max_ptime != -1 && duration >= payload->max_ptime)
700 GstRTPBasePayload *payload;
704 GstClockTime timestamp;
710 find_timestamp (GstBuffer ** buffer, guint idx, HeaderData * data)
712 data->timestamp = GST_BUFFER_TIMESTAMP (*buffer);
713 data->offset = GST_BUFFER_OFFSET (*buffer);
715 /* stop when we find a timestamp. We take whatever offset is associated with
716 * the timestamp (if any) to do perfect timestamps when we need to. */
717 if (data->timestamp != -1)
724 set_headers (GstBuffer ** buffer, guint group, guint idx, HeaderData * data)
728 gst_rtp_buffer_map (*buffer, GST_MAP_WRITE, &rtp);
729 gst_rtp_buffer_set_ssrc (&rtp, data->ssrc);
730 gst_rtp_buffer_set_payload_type (&rtp, data->pt);
731 gst_rtp_buffer_set_seq (&rtp, data->seqnum);
732 gst_rtp_buffer_set_timestamp (&rtp, data->rtptime);
733 gst_rtp_buffer_unmap (&rtp);
735 /* increment the seqnum for each buffer */
741 /* Updates the SSRC, payload type, seqnum and timestamp of the RTP buffer
742 * before the buffer is pushed. */
744 gst_rtp_base_payload_prepare_push (GstRTPBasePayload * payload,
745 gpointer obj, gboolean is_list)
747 GstRTPBasePayloadPrivate *priv;
750 if (payload->clock_rate == 0)
753 priv = payload->priv;
755 /* update first, so that the property is set to the last
757 payload->seqnum = priv->next_seqnum;
759 /* fill in the fields we want to set on all headers */
760 data.payload = payload;
761 data.seqnum = payload->seqnum;
762 data.ssrc = payload->current_ssrc;
763 data.pt = payload->pt;
765 /* find the first buffer with a timestamp */
768 data.offset = GST_BUFFER_OFFSET_NONE;
769 gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj),
770 (GstBufferListFunc) find_timestamp, &data);
772 data.timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (obj));
773 data.offset = GST_BUFFER_OFFSET (GST_BUFFER_CAST (obj));
776 /* convert to RTP time */
777 if (priv->perfect_rtptime && data.offset != GST_BUFFER_OFFSET_NONE &&
778 priv->base_offset != GST_BUFFER_OFFSET_NONE) {
779 /* if we have an offset, use that for making an RTP timestamp */
780 data.rtptime = payload->ts_base + priv->base_rtime +
781 data.offset - priv->base_offset;
782 GST_LOG_OBJECT (payload,
783 "Using offset %" G_GUINT64_FORMAT " for RTP timestamp", data.offset);
784 } else if (GST_CLOCK_TIME_IS_VALID (data.timestamp)) {
787 /* no offset, use the gstreamer timestamp */
788 rtime = gst_segment_to_running_time (&payload->segment, GST_FORMAT_TIME,
792 GST_LOG_OBJECT (payload, "Clipped timestamp, using base RTP timestamp");
795 GST_LOG_OBJECT (payload,
796 "Using running_time %" GST_TIME_FORMAT " for RTP timestamp",
797 GST_TIME_ARGS (rtime));
799 gst_util_uint64_scale_int (rtime, payload->clock_rate, GST_SECOND);
800 priv->base_offset = data.offset;
801 priv->base_rtime = rtime;
803 /* add running_time in clock-rate units to the base timestamp */
804 data.rtptime = payload->ts_base + rtime;
806 GST_LOG_OBJECT (payload,
807 "Using previous RTP timestamp %" G_GUINT32_FORMAT, payload->timestamp);
808 /* no timestamp to convert, take previous timestamp */
809 data.rtptime = payload->timestamp;
812 /* set ssrc, payload type, seq number, caps and rtptime */
814 gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj),
815 (GstBufferListFunc) set_headers, &data);
817 GstBuffer *buf = GST_BUFFER_CAST (obj);
818 set_headers (&buf, 0, 0, &data);
821 priv->next_seqnum = data.seqnum;
822 payload->timestamp = data.rtptime;
824 GST_LOG_OBJECT (payload, "Preparing to push packet with size %"
825 G_GSIZE_FORMAT ", seq=%d, rtptime=%u, timestamp %" GST_TIME_FORMAT,
826 (is_list) ? -1 : gst_buffer_get_size (GST_BUFFER (obj)),
827 payload->seqnum, data.rtptime, GST_TIME_ARGS (data.timestamp));
829 if (g_atomic_int_compare_and_exchange (&payload->priv->
830 notified_first_timestamp, 1, 0)) {
831 g_object_notify (G_OBJECT (payload), "timestamp");
832 g_object_notify (G_OBJECT (payload), "seqnum");
840 GST_ELEMENT_ERROR (payload, STREAM, NOT_IMPLEMENTED, (NULL),
841 ("subclass did not specify clock-rate"));
842 return GST_FLOW_ERROR;
847 * gst_rtp_base_payload_push_list:
848 * @payload: a #GstRTPBasePayload
849 * @list: a #GstBufferList
851 * Push @list to the peer element of the payloader. The SSRC, payload type,
852 * seqnum and timestamp of the RTP buffer will be updated first.
854 * This function takes ownership of @list.
856 * Returns: a #GstFlowReturn.
861 gst_rtp_base_payload_push_list (GstRTPBasePayload * payload,
862 GstBufferList * list)
866 res = gst_rtp_base_payload_prepare_push (payload, list, TRUE);
868 if (G_LIKELY (res == GST_FLOW_OK))
869 res = gst_pad_push_list (payload->srcpad, list);
871 gst_buffer_list_unref (list);
877 * gst_rtp_base_payload_push:
878 * @payload: a #GstRTPBasePayload
879 * @buffer: a #GstBuffer
881 * Push @buffer to the peer element of the payloader. The SSRC, payload type,
882 * seqnum and timestamp of the RTP buffer will be updated first.
884 * This function takes ownership of @buffer.
886 * Returns: a #GstFlowReturn.
889 gst_rtp_base_payload_push (GstRTPBasePayload * payload, GstBuffer * buffer)
893 res = gst_rtp_base_payload_prepare_push (payload, buffer, FALSE);
895 if (G_LIKELY (res == GST_FLOW_OK))
896 res = gst_pad_push (payload->srcpad, buffer);
898 gst_buffer_unref (buffer);
904 gst_rtp_base_payload_set_property (GObject * object, guint prop_id,
905 const GValue * value, GParamSpec * pspec)
907 GstRTPBasePayload *rtpbasepayload;
908 GstRTPBasePayloadPrivate *priv;
911 rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
912 priv = rtpbasepayload->priv;
916 rtpbasepayload->mtu = g_value_get_uint (value);
919 rtpbasepayload->pt = g_value_get_uint (value);
922 val = g_value_get_uint (value);
923 rtpbasepayload->ssrc = val;
924 priv->ssrc_random = FALSE;
926 case PROP_TIMESTAMP_OFFSET:
927 val = g_value_get_uint (value);
928 rtpbasepayload->ts_offset = val;
929 priv->ts_offset_random = FALSE;
931 case PROP_SEQNUM_OFFSET:
932 val = g_value_get_int (value);
933 rtpbasepayload->seqnum_offset = val;
934 priv->seqnum_offset_random = (val == -1);
935 GST_DEBUG_OBJECT (rtpbasepayload, "seqnum offset 0x%04x, random %d",
936 rtpbasepayload->seqnum_offset, priv->seqnum_offset_random);
939 rtpbasepayload->priv->prop_max_ptime = g_value_get_int64 (value);
940 update_max_ptime (rtpbasepayload);
943 rtpbasepayload->min_ptime = g_value_get_int64 (value);
945 case PROP_PERFECT_RTPTIME:
946 priv->perfect_rtptime = g_value_get_boolean (value);
948 case PROP_PTIME_MULTIPLE:
949 rtpbasepayload->ptime_multiple = g_value_get_int64 (value);
952 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
958 gst_rtp_base_payload_get_property (GObject * object, guint prop_id,
959 GValue * value, GParamSpec * pspec)
961 GstRTPBasePayload *rtpbasepayload;
962 GstRTPBasePayloadPrivate *priv;
964 rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
965 priv = rtpbasepayload->priv;
969 g_value_set_uint (value, rtpbasepayload->mtu);
972 g_value_set_uint (value, rtpbasepayload->pt);
975 if (priv->ssrc_random)
976 g_value_set_uint (value, -1);
978 g_value_set_uint (value, rtpbasepayload->ssrc);
980 case PROP_TIMESTAMP_OFFSET:
981 if (priv->ts_offset_random)
982 g_value_set_uint (value, -1);
984 g_value_set_uint (value, (guint32) rtpbasepayload->ts_offset);
986 case PROP_SEQNUM_OFFSET:
987 if (priv->seqnum_offset_random)
988 g_value_set_int (value, -1);
990 g_value_set_int (value, (guint16) rtpbasepayload->seqnum_offset);
993 g_value_set_int64 (value, rtpbasepayload->max_ptime);
996 g_value_set_int64 (value, rtpbasepayload->min_ptime);
999 g_value_set_uint (value, rtpbasepayload->timestamp);
1002 g_value_set_uint (value, rtpbasepayload->seqnum);
1004 case PROP_PERFECT_RTPTIME:
1005 g_value_set_boolean (value, priv->perfect_rtptime);
1007 case PROP_PTIME_MULTIPLE:
1008 g_value_set_int64 (value, rtpbasepayload->ptime_multiple);
1011 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1016 static GstStateChangeReturn
1017 gst_rtp_base_payload_change_state (GstElement * element,
1018 GstStateChange transition)
1020 GstRTPBasePayload *rtpbasepayload;
1021 GstRTPBasePayloadPrivate *priv;
1022 GstStateChangeReturn ret;
1024 rtpbasepayload = GST_RTP_BASE_PAYLOAD (element);
1025 priv = rtpbasepayload->priv;
1027 switch (transition) {
1028 case GST_STATE_CHANGE_NULL_TO_READY:
1030 case GST_STATE_CHANGE_READY_TO_PAUSED:
1031 gst_segment_init (&rtpbasepayload->segment, GST_FORMAT_UNDEFINED);
1033 if (priv->seqnum_offset_random)
1034 rtpbasepayload->seqnum_base = g_random_int_range (0, G_MAXUINT16);
1036 rtpbasepayload->seqnum_base = rtpbasepayload->seqnum_offset;
1037 priv->next_seqnum = rtpbasepayload->seqnum_base;
1038 rtpbasepayload->seqnum = rtpbasepayload->seqnum_base;
1040 if (priv->ssrc_random)
1041 rtpbasepayload->current_ssrc = g_random_int ();
1043 rtpbasepayload->current_ssrc = rtpbasepayload->ssrc;
1045 if (priv->ts_offset_random)
1046 rtpbasepayload->ts_base = g_random_int ();
1048 rtpbasepayload->ts_base = rtpbasepayload->ts_offset;
1049 rtpbasepayload->timestamp = rtpbasepayload->ts_base;
1050 g_atomic_int_set (&rtpbasepayload->priv->notified_first_timestamp, 1);
1051 priv->base_offset = GST_BUFFER_OFFSET_NONE;
1057 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1059 switch (transition) {
1060 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1061 g_atomic_int_set (&rtpbasepayload->priv->notified_first_timestamp, 1);
1063 case GST_STATE_CHANGE_READY_TO_NULL: