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;
55 gboolean delay_segment;
56 GstEvent *pending_segment;
59 /* RTPBasePayload signals and args */
66 /* FIXME 0.11, a better default is the Ethernet MTU of
67 * 1500 - sizeof(headers) as pointed out by marcelm in IRC:
68 * So an Ethernet MTU of 1500, minus 60 for the max IP, minus 8 for UDP, gives
69 * 1432 bytes or so. And that should be adjusted downward further for other
70 * encapsulations like PPPoE, so 1400 at most.
72 #define DEFAULT_MTU 1400
74 #define DEFAULT_SSRC -1
75 #define DEFAULT_TIMESTAMP_OFFSET -1
76 #define DEFAULT_SEQNUM_OFFSET -1
77 #define DEFAULT_MAX_PTIME -1
78 #define DEFAULT_MIN_PTIME 0
79 #define DEFAULT_PERFECT_RTPTIME TRUE
80 #define DEFAULT_PTIME_MULTIPLE 0
88 PROP_TIMESTAMP_OFFSET,
99 static void gst_rtp_base_payload_class_init (GstRTPBasePayloadClass * klass);
100 static void gst_rtp_base_payload_init (GstRTPBasePayload * rtpbasepayload,
102 static void gst_rtp_base_payload_finalize (GObject * object);
104 static GstCaps *gst_rtp_base_payload_getcaps_default (GstRTPBasePayload *
105 rtpbasepayload, GstPad * pad, GstCaps * filter);
107 static gboolean gst_rtp_base_payload_sink_event_default (GstRTPBasePayload *
108 rtpbasepayload, GstEvent * event);
109 static gboolean gst_rtp_base_payload_sink_event (GstPad * pad,
110 GstObject * parent, GstEvent * event);
111 static gboolean gst_rtp_base_payload_src_event_default (GstRTPBasePayload *
112 rtpbasepayload, GstEvent * event);
113 static gboolean gst_rtp_base_payload_src_event (GstPad * pad,
114 GstObject * parent, GstEvent * event);
115 static gboolean gst_rtp_base_payload_query_default (GstRTPBasePayload *
116 rtpbasepayload, GstPad * pad, GstQuery * query);
117 static gboolean gst_rtp_base_payload_query (GstPad * pad, GstObject * parent,
119 static GstFlowReturn gst_rtp_base_payload_chain (GstPad * pad,
120 GstObject * parent, GstBuffer * buffer);
122 static void gst_rtp_base_payload_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec);
124 static void gst_rtp_base_payload_get_property (GObject * object, guint prop_id,
125 GValue * value, GParamSpec * pspec);
127 static GstStateChangeReturn gst_rtp_base_payload_change_state (GstElement *
128 element, GstStateChange transition);
130 static GstElementClass *parent_class = NULL;
133 gst_rtp_base_payload_get_type (void)
135 static GType rtpbasepayload_type = 0;
137 if (g_once_init_enter ((gsize *) & rtpbasepayload_type)) {
138 static const GTypeInfo rtpbasepayload_info = {
139 sizeof (GstRTPBasePayloadClass),
142 (GClassInitFunc) gst_rtp_base_payload_class_init,
145 sizeof (GstRTPBasePayload),
147 (GInstanceInitFunc) gst_rtp_base_payload_init,
150 g_once_init_leave ((gsize *) & rtpbasepayload_type,
151 g_type_register_static (GST_TYPE_ELEMENT, "GstRTPBasePayload",
152 &rtpbasepayload_info, G_TYPE_FLAG_ABSTRACT));
154 return rtpbasepayload_type;
158 gst_rtp_base_payload_class_init (GstRTPBasePayloadClass * klass)
160 GObjectClass *gobject_class;
161 GstElementClass *gstelement_class;
163 gobject_class = (GObjectClass *) klass;
164 gstelement_class = (GstElementClass *) klass;
166 g_type_class_add_private (klass, sizeof (GstRTPBasePayloadPrivate));
168 parent_class = g_type_class_peek_parent (klass);
170 gobject_class->finalize = gst_rtp_base_payload_finalize;
172 gobject_class->set_property = gst_rtp_base_payload_set_property;
173 gobject_class->get_property = gst_rtp_base_payload_get_property;
175 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MTU,
176 g_param_spec_uint ("mtu", "MTU",
177 "Maximum size of one packet",
178 28, G_MAXUINT, DEFAULT_MTU,
179 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
180 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PT,
181 g_param_spec_uint ("pt", "payload type",
182 "The payload type of the packets", 0, 0x80, DEFAULT_PT,
183 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
185 g_param_spec_uint ("ssrc", "SSRC",
186 "The SSRC of the packets (default == random)", 0, G_MAXUINT32,
187 DEFAULT_SSRC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 g_object_class_install_property (G_OBJECT_CLASS (klass),
189 PROP_TIMESTAMP_OFFSET, g_param_spec_uint ("timestamp-offset",
191 "Offset to add to all outgoing timestamps (default = random)", 0,
192 G_MAXUINT32, DEFAULT_TIMESTAMP_OFFSET,
193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
195 g_param_spec_int ("seqnum-offset", "Sequence number Offset",
196 "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXUINT16,
197 DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_PTIME,
199 g_param_spec_int64 ("max-ptime", "Max packet time",
200 "Maximum duration of the packet data in ns (-1 = unlimited up to MTU)",
201 -1, G_MAXINT64, DEFAULT_MAX_PTIME,
202 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 * GstRTPBaseAudioPayload:min-ptime:
206 * Minimum duration of the packet data in ns (can't go above MTU)
208 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MIN_PTIME,
209 g_param_spec_int64 ("min-ptime", "Min packet time",
210 "Minimum duration of the packet data in ns (can't go above MTU)",
211 0, G_MAXINT64, DEFAULT_MIN_PTIME,
212 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMESTAMP,
215 g_param_spec_uint ("timestamp", "Timestamp",
216 "The RTP timestamp of the last processed packet",
217 0, G_MAXUINT32, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
218 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
219 g_param_spec_uint ("seqnum", "Sequence number",
220 "The RTP sequence number of the last processed packet",
221 0, G_MAXUINT16, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
224 * GstRTPBaseAudioPayload:perfect-rtptime:
226 * Try to use the offset fields to generate perfect RTP timestamps. when this
227 * option is disabled, RTP timestamps are generated from the GStreamer
228 * timestamps, which could result in RTP timestamps that don't increment with
229 * the amount of data in the packet.
231 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PERFECT_RTPTIME,
232 g_param_spec_boolean ("perfect-rtptime", "Perfect RTP Time",
233 "Generate perfect RTP timestamps when possible",
234 DEFAULT_PERFECT_RTPTIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 * GstRTPBaseAudioPayload:ptime-multiple:
238 * Force buffers to be multiples of this duration in ns (0 disables)
240 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PTIME_MULTIPLE,
241 g_param_spec_int64 ("ptime-multiple", "Packet time multiple",
242 "Force buffers to be multiples of this duration in ns (0 disables)",
243 0, G_MAXINT64, DEFAULT_PTIME_MULTIPLE,
244 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 gstelement_class->change_state = gst_rtp_base_payload_change_state;
248 klass->get_caps = gst_rtp_base_payload_getcaps_default;
249 klass->sink_event = gst_rtp_base_payload_sink_event_default;
250 klass->src_event = gst_rtp_base_payload_src_event_default;
251 klass->query = gst_rtp_base_payload_query_default;
253 GST_DEBUG_CATEGORY_INIT (rtpbasepayload_debug, "rtpbasepayload", 0,
254 "Base class for RTP Payloaders");
258 gst_rtp_base_payload_init (GstRTPBasePayload * rtpbasepayload, gpointer g_class)
260 GstPadTemplate *templ;
261 GstRTPBasePayloadPrivate *priv;
263 rtpbasepayload->priv = priv =
264 GST_RTP_BASE_PAYLOAD_GET_PRIVATE (rtpbasepayload);
267 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
268 g_return_if_fail (templ != NULL);
270 rtpbasepayload->srcpad = gst_pad_new_from_template (templ, "src");
271 gst_pad_set_event_function (rtpbasepayload->srcpad,
272 gst_rtp_base_payload_src_event);
273 gst_element_add_pad (GST_ELEMENT (rtpbasepayload), rtpbasepayload->srcpad);
276 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
277 g_return_if_fail (templ != NULL);
279 rtpbasepayload->sinkpad = gst_pad_new_from_template (templ, "sink");
280 gst_pad_set_chain_function (rtpbasepayload->sinkpad,
281 gst_rtp_base_payload_chain);
282 gst_pad_set_event_function (rtpbasepayload->sinkpad,
283 gst_rtp_base_payload_sink_event);
284 gst_pad_set_query_function (rtpbasepayload->sinkpad,
285 gst_rtp_base_payload_query);
286 gst_element_add_pad (GST_ELEMENT (rtpbasepayload), rtpbasepayload->sinkpad);
288 rtpbasepayload->mtu = DEFAULT_MTU;
289 rtpbasepayload->pt = DEFAULT_PT;
290 rtpbasepayload->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
291 rtpbasepayload->ssrc = DEFAULT_SSRC;
292 rtpbasepayload->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
293 priv->seqnum_offset_random = (rtpbasepayload->seqnum_offset == -1);
294 priv->ts_offset_random = (rtpbasepayload->ts_offset == -1);
295 priv->ssrc_random = (rtpbasepayload->ssrc == -1);
297 rtpbasepayload->max_ptime = DEFAULT_MAX_PTIME;
298 rtpbasepayload->min_ptime = DEFAULT_MIN_PTIME;
299 rtpbasepayload->priv->perfect_rtptime = DEFAULT_PERFECT_RTPTIME;
300 rtpbasepayload->ptime_multiple = DEFAULT_PTIME_MULTIPLE;
301 rtpbasepayload->priv->base_offset = GST_BUFFER_OFFSET_NONE;
302 rtpbasepayload->priv->base_rtime = GST_BUFFER_OFFSET_NONE;
304 rtpbasepayload->media = NULL;
305 rtpbasepayload->encoding_name = NULL;
307 rtpbasepayload->clock_rate = 0;
309 rtpbasepayload->priv->caps_max_ptime = DEFAULT_MAX_PTIME;
310 rtpbasepayload->priv->prop_max_ptime = DEFAULT_MAX_PTIME;
314 gst_rtp_base_payload_finalize (GObject * object)
316 GstRTPBasePayload *rtpbasepayload;
318 rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
320 g_free (rtpbasepayload->media);
321 rtpbasepayload->media = NULL;
322 g_free (rtpbasepayload->encoding_name);
323 rtpbasepayload->encoding_name = NULL;
325 G_OBJECT_CLASS (parent_class)->finalize (object);
329 gst_rtp_base_payload_getcaps_default (GstRTPBasePayload * rtpbasepayload,
330 GstPad * pad, GstCaps * filter)
334 caps = GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
335 GST_DEBUG_OBJECT (pad,
336 "using pad template %p with caps %p %" GST_PTR_FORMAT,
337 GST_PAD_PAD_TEMPLATE (pad), caps, caps);
340 caps = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
342 caps = gst_caps_ref (caps);
348 gst_rtp_base_payload_sink_event_default (GstRTPBasePayload * rtpbasepayload,
351 GstObject *parent = GST_OBJECT_CAST (rtpbasepayload);
352 gboolean res = FALSE;
354 switch (GST_EVENT_TYPE (event)) {
355 case GST_EVENT_FLUSH_START:
356 res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
358 case GST_EVENT_FLUSH_STOP:
359 res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
360 gst_segment_init (&rtpbasepayload->segment, GST_FORMAT_UNDEFINED);
361 gst_event_replace (&rtpbasepayload->priv->pending_segment, NULL);
365 GstRTPBasePayloadClass *rtpbasepayload_class;
368 gst_event_parse_caps (event, &caps);
369 GST_DEBUG_OBJECT (rtpbasepayload, "setting caps %" GST_PTR_FORMAT, caps);
371 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
372 if (rtpbasepayload_class->set_caps)
373 res = rtpbasepayload_class->set_caps (rtpbasepayload, caps);
377 rtpbasepayload->priv->negotiated = res;
379 gst_event_unref (event);
382 case GST_EVENT_SEGMENT:
386 segment = &rtpbasepayload->segment;
387 gst_event_copy_segment (event, segment);
389 rtpbasepayload->priv->base_offset = GST_BUFFER_OFFSET_NONE;
391 GST_DEBUG_OBJECT (rtpbasepayload,
392 "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
393 if (rtpbasepayload->priv->delay_segment) {
394 gst_event_replace (&rtpbasepayload->priv->pending_segment, event);
395 gst_event_unref (event);
398 res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
403 res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
410 gst_rtp_base_payload_sink_event (GstPad * pad, GstObject * parent,
413 GstRTPBasePayload *rtpbasepayload;
414 GstRTPBasePayloadClass *rtpbasepayload_class;
415 gboolean res = FALSE;
417 rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
418 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
420 if (rtpbasepayload_class->sink_event)
421 res = rtpbasepayload_class->sink_event (rtpbasepayload, event);
423 gst_event_unref (event);
429 gst_rtp_base_payload_src_event_default (GstRTPBasePayload * rtpbasepayload,
432 GstObject *parent = GST_OBJECT_CAST (rtpbasepayload);
433 gboolean res = FALSE;
435 switch (GST_EVENT_TYPE (event)) {
437 res = gst_pad_event_default (rtpbasepayload->srcpad, parent, event);
444 gst_rtp_base_payload_src_event (GstPad * pad, GstObject * parent,
447 GstRTPBasePayload *rtpbasepayload;
448 GstRTPBasePayloadClass *rtpbasepayload_class;
449 gboolean res = FALSE;
451 rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
452 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
454 if (rtpbasepayload_class->src_event)
455 res = rtpbasepayload_class->src_event (rtpbasepayload, event);
457 gst_event_unref (event);
464 gst_rtp_base_payload_query_default (GstRTPBasePayload * rtpbasepayload,
465 GstPad * pad, GstQuery * query)
467 gboolean res = FALSE;
469 switch (GST_QUERY_TYPE (query)) {
472 GstRTPBasePayloadClass *rtpbasepayload_class;
473 GstCaps *filter, *caps;
475 gst_query_parse_caps (query, &filter);
476 GST_DEBUG_OBJECT (rtpbasepayload, "getting caps with filter %"
477 GST_PTR_FORMAT, filter);
479 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
480 if (rtpbasepayload_class->get_caps) {
481 caps = rtpbasepayload_class->get_caps (rtpbasepayload, pad, filter);
482 gst_query_set_caps_result (query, caps);
483 gst_caps_unref (caps);
490 gst_pad_query_default (pad, GST_OBJECT_CAST (rtpbasepayload), query);
497 gst_rtp_base_payload_query (GstPad * pad, GstObject * parent, GstQuery * query)
499 GstRTPBasePayload *rtpbasepayload;
500 GstRTPBasePayloadClass *rtpbasepayload_class;
501 gboolean res = FALSE;
503 rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
504 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
506 if (rtpbasepayload_class->query)
507 res = rtpbasepayload_class->query (rtpbasepayload, pad, query);
513 gst_rtp_base_payload_chain (GstPad * pad, GstObject * parent,
516 GstRTPBasePayload *rtpbasepayload;
517 GstRTPBasePayloadClass *rtpbasepayload_class;
520 rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
521 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
523 if (!rtpbasepayload_class->handle_buffer)
526 if (!rtpbasepayload->priv->negotiated)
529 ret = rtpbasepayload_class->handle_buffer (rtpbasepayload, buffer);
536 GST_ELEMENT_ERROR (rtpbasepayload, STREAM, NOT_IMPLEMENTED, (NULL),
537 ("subclass did not implement handle_buffer function"));
538 gst_buffer_unref (buffer);
539 return GST_FLOW_ERROR;
543 GST_ELEMENT_ERROR (rtpbasepayload, CORE, NEGOTIATION, (NULL),
544 ("No input format was negotiated, i.e. no caps event was received. "
545 "Perhaps you need a parser or typefind element before the payloader"));
546 gst_buffer_unref (buffer);
547 return GST_FLOW_NOT_NEGOTIATED;
552 * gst_rtp_base_payload_set_options:
553 * @payload: a #GstRTPBasePayload
554 * @media: the media type (typically "audio" or "video")
555 * @dynamic: if the payload type is dynamic
556 * @encoding_name: the encoding name
557 * @clock_rate: the clock rate of the media
559 * Set the rtp options of the payloader. These options will be set in the caps
560 * of the payloader. Subclasses must call this method before calling
561 * gst_rtp_base_payload_push() or gst_rtp_base_payload_set_outcaps().
564 gst_rtp_base_payload_set_options (GstRTPBasePayload * payload,
565 const gchar * media, gboolean dynamic, const gchar * encoding_name,
568 g_return_if_fail (payload != NULL);
569 g_return_if_fail (clock_rate != 0);
571 g_free (payload->media);
572 payload->media = g_strdup (media);
573 payload->dynamic = dynamic;
574 g_free (payload->encoding_name);
575 payload->encoding_name = g_strdup (encoding_name);
576 payload->clock_rate = clock_rate;
580 copy_fixed (GQuark field_id, const GValue * value, GstStructure * dest)
582 if (gst_value_is_fixed (value)) {
583 gst_structure_id_set_value (dest, field_id, value);
589 update_max_ptime (GstRTPBasePayload * rtpbasepayload)
591 if (rtpbasepayload->priv->caps_max_ptime != -1 &&
592 rtpbasepayload->priv->prop_max_ptime != -1)
593 rtpbasepayload->max_ptime = MIN (rtpbasepayload->priv->caps_max_ptime,
594 rtpbasepayload->priv->prop_max_ptime);
595 else if (rtpbasepayload->priv->caps_max_ptime != -1)
596 rtpbasepayload->max_ptime = rtpbasepayload->priv->caps_max_ptime;
597 else if (rtpbasepayload->priv->prop_max_ptime != -1)
598 rtpbasepayload->max_ptime = rtpbasepayload->priv->prop_max_ptime;
600 rtpbasepayload->max_ptime = DEFAULT_MAX_PTIME;
604 * gst_rtp_base_payload_set_outcaps:
605 * @payload: a #GstRTPBasePayload
606 * @fieldname: the first field name or %NULL
609 * Configure the output caps with the optional parameters.
611 * Variable arguments should be in the form field name, field type
612 * (as a GType), value(s). The last variable argument should be NULL.
614 * Returns: %TRUE if the caps could be set.
617 gst_rtp_base_payload_set_outcaps (GstRTPBasePayload * payload,
618 const gchar * fieldname, ...)
620 GstCaps *srccaps, *peercaps;
623 /* fill in the defaults, their properties cannot be negotiated. */
624 srccaps = gst_caps_new_simple ("application/x-rtp",
625 "media", G_TYPE_STRING, payload->media,
626 "clock-rate", G_TYPE_INT, payload->clock_rate,
627 "encoding-name", G_TYPE_STRING, payload->encoding_name, NULL);
629 GST_DEBUG_OBJECT (payload, "defaults: %" GST_PTR_FORMAT, srccaps);
634 /* override with custom properties */
635 va_start (varargs, fieldname);
636 gst_caps_set_simple_valist (srccaps, fieldname, varargs);
639 GST_DEBUG_OBJECT (payload, "custom added: %" GST_PTR_FORMAT, srccaps);
642 payload->priv->caps_max_ptime = DEFAULT_MAX_PTIME;
645 /* the peer caps can override some of the defaults */
646 peercaps = gst_pad_peer_query_caps (payload->srcpad, srccaps);
647 if (peercaps == NULL) {
648 /* no peer caps, just add the other properties */
649 gst_caps_set_simple (srccaps,
650 "payload", G_TYPE_INT, GST_RTP_BASE_PAYLOAD_PT (payload),
651 "ssrc", G_TYPE_UINT, payload->current_ssrc,
652 "timestamp-offset", G_TYPE_UINT, payload->ts_base,
653 "seqnum-offset", G_TYPE_UINT, payload->seqnum_base, NULL);
655 GST_DEBUG_OBJECT (payload, "no peer caps: %" GST_PTR_FORMAT, srccaps);
661 guint max_ptime, ptime;
663 /* peer provides caps we can use to fixate. They are already intersected
664 * with our srccaps, just make them writable */
665 temp = gst_caps_make_writable (peercaps);
666 gst_caps_unref (srccaps);
668 if (gst_caps_is_empty (temp)) {
669 gst_caps_unref (temp);
673 /* now fixate, start by taking the first caps */
674 temp = gst_caps_truncate (temp);
676 /* get first structure */
677 s = gst_caps_get_structure (temp, 0);
679 if (gst_structure_get_uint (s, "maxptime", &max_ptime))
680 payload->priv->caps_max_ptime = max_ptime * GST_MSECOND;
682 if (gst_structure_get_uint (s, "ptime", &ptime))
683 payload->ptime = ptime * GST_MSECOND;
685 if (gst_structure_get_int (s, "payload", &pt)) {
687 GST_RTP_BASE_PAYLOAD_PT (payload) = pt;
688 GST_LOG_OBJECT (payload, "using peer pt %d", pt);
690 if (gst_structure_has_field (s, "payload")) {
691 /* can only fixate if there is a field */
692 gst_structure_fixate_field_nearest_int (s, "payload",
693 GST_RTP_BASE_PAYLOAD_PT (payload));
694 gst_structure_get_int (s, "payload", &pt);
695 GST_LOG_OBJECT (payload, "using peer pt %d", pt);
697 /* no pt field, use the internal pt */
698 pt = GST_RTP_BASE_PAYLOAD_PT (payload);
699 gst_structure_set (s, "payload", G_TYPE_INT, pt, NULL);
700 GST_LOG_OBJECT (payload, "using internal pt %d", pt);
704 if (gst_structure_has_field_typed (s, "ssrc", G_TYPE_UINT)) {
705 value = gst_structure_get_value (s, "ssrc");
706 payload->current_ssrc = g_value_get_uint (value);
707 GST_LOG_OBJECT (payload, "using peer ssrc %08x", payload->current_ssrc);
709 /* FIXME, fixate_nearest_uint would be even better */
710 gst_structure_set (s, "ssrc", G_TYPE_UINT, payload->current_ssrc, NULL);
711 GST_LOG_OBJECT (payload, "using internal ssrc %08x",
712 payload->current_ssrc);
715 if (gst_structure_has_field_typed (s, "timestamp-offset", G_TYPE_UINT)) {
716 value = gst_structure_get_value (s, "timestamp-offset");
717 payload->ts_base = g_value_get_uint (value);
718 GST_LOG_OBJECT (payload, "using peer timestamp-offset %u",
721 /* FIXME, fixate_nearest_uint would be even better */
722 gst_structure_set (s, "timestamp-offset", G_TYPE_UINT, payload->ts_base,
724 GST_LOG_OBJECT (payload, "using internal timestamp-offset %u",
727 if (gst_structure_has_field_typed (s, "seqnum-offset", G_TYPE_UINT)) {
728 value = gst_structure_get_value (s, "seqnum-offset");
729 payload->seqnum_base = g_value_get_uint (value);
730 GST_LOG_OBJECT (payload, "using peer seqnum-offset %u",
731 payload->seqnum_base);
733 /* FIXME, fixate_nearest_uint would be even better */
734 gst_structure_set (s, "seqnum-offset", G_TYPE_UINT, payload->seqnum_base,
736 GST_LOG_OBJECT (payload, "using internal seqnum-offset %u",
737 payload->seqnum_base);
740 /* make the target caps by copying over all the fixed caps, removing the
742 srccaps = gst_caps_new_empty_simple (gst_structure_get_name (s));
743 d = gst_caps_get_structure (srccaps, 0);
745 gst_structure_foreach (s, (GstStructureForeachFunc) copy_fixed, d);
747 gst_caps_unref (temp);
749 GST_DEBUG_OBJECT (payload, "with peer caps: %" GST_PTR_FORMAT, srccaps);
752 update_max_ptime (payload);
754 res = gst_pad_set_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload), srccaps);
755 gst_caps_unref (srccaps);
761 * gst_rtp_base_payload_is_filled:
762 * @payload: a #GstRTPBasePayload
763 * @size: the size of the packet
764 * @duration: the duration of the packet
766 * Check if the packet with @size and @duration would exceed the configured
769 * Returns: %TRUE if the packet of @size and @duration would exceed the
770 * configured MTU or max_ptime.
773 gst_rtp_base_payload_is_filled (GstRTPBasePayload * payload,
774 guint size, GstClockTime duration)
776 if (size > payload->mtu)
779 if (payload->max_ptime != -1 && duration >= payload->max_ptime)
787 GstRTPBasePayload *payload;
798 find_timestamp (GstBuffer ** buffer, guint idx, gpointer user_data)
800 HeaderData *data = user_data;
801 data->dts = GST_BUFFER_DTS (*buffer);
802 data->pts = GST_BUFFER_PTS (*buffer);
803 data->offset = GST_BUFFER_OFFSET (*buffer);
805 /* stop when we find a timestamp. We take whatever offset is associated with
806 * the timestamp (if any) to do perfect timestamps when we need to. */
814 set_headers (GstBuffer ** buffer, guint idx, gpointer user_data)
816 HeaderData *data = user_data;
817 GstRTPBuffer rtp = { NULL, };
819 if (!gst_rtp_buffer_map (*buffer, GST_MAP_WRITE, &rtp))
822 gst_rtp_buffer_set_ssrc (&rtp, data->ssrc);
823 gst_rtp_buffer_set_payload_type (&rtp, data->pt);
824 gst_rtp_buffer_set_seq (&rtp, data->seqnum);
825 gst_rtp_buffer_set_timestamp (&rtp, data->rtptime);
826 gst_rtp_buffer_unmap (&rtp);
828 /* increment the seqnum for each buffer */
835 GST_ERROR ("failed to map buffer %p", *buffer);
840 /* Updates the SSRC, payload type, seqnum and timestamp of the RTP buffer
841 * before the buffer is pushed. */
843 gst_rtp_base_payload_prepare_push (GstRTPBasePayload * payload,
844 gpointer obj, gboolean is_list)
846 GstRTPBasePayloadPrivate *priv;
849 if (payload->clock_rate == 0)
852 priv = payload->priv;
854 /* update first, so that the property is set to the last
856 payload->seqnum = priv->next_seqnum;
858 /* fill in the fields we want to set on all headers */
859 data.payload = payload;
860 data.seqnum = payload->seqnum;
861 data.ssrc = payload->current_ssrc;
862 data.pt = payload->pt;
864 /* find the first buffer with a timestamp */
868 data.offset = GST_BUFFER_OFFSET_NONE;
869 gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj), find_timestamp, &data);
871 data.dts = GST_BUFFER_DTS (GST_BUFFER_CAST (obj));
872 data.pts = GST_BUFFER_PTS (GST_BUFFER_CAST (obj));
873 data.offset = GST_BUFFER_OFFSET (GST_BUFFER_CAST (obj));
876 /* convert to RTP time */
877 if (priv->perfect_rtptime && data.offset != GST_BUFFER_OFFSET_NONE &&
878 priv->base_offset != GST_BUFFER_OFFSET_NONE) {
879 /* if we have an offset, use that for making an RTP timestamp */
880 data.rtptime = payload->ts_base + priv->base_rtime +
881 data.offset - priv->base_offset;
882 GST_LOG_OBJECT (payload,
883 "Using offset %" G_GUINT64_FORMAT " for RTP timestamp", data.offset);
884 } else if (GST_CLOCK_TIME_IS_VALID (data.pts)) {
887 /* no offset, use the gstreamer pts */
888 rtime = gst_segment_to_running_time (&payload->segment, GST_FORMAT_TIME,
892 GST_LOG_OBJECT (payload, "Clipped pts, using base RTP timestamp");
895 GST_LOG_OBJECT (payload,
896 "Using running_time %" GST_TIME_FORMAT " for RTP timestamp",
897 GST_TIME_ARGS (rtime));
899 gst_util_uint64_scale_int (rtime, payload->clock_rate, GST_SECOND);
900 priv->base_offset = data.offset;
901 priv->base_rtime = rtime;
903 /* add running_time in clock-rate units to the base timestamp */
904 data.rtptime = payload->ts_base + rtime;
906 GST_LOG_OBJECT (payload,
907 "Using previous RTP timestamp %" G_GUINT32_FORMAT, payload->timestamp);
908 /* no timestamp to convert, take previous timestamp */
909 data.rtptime = payload->timestamp;
912 /* set ssrc, payload type, seq number, caps and rtptime */
914 gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj), set_headers, &data);
916 GstBuffer *buf = GST_BUFFER_CAST (obj);
917 set_headers (&buf, 0, &data);
920 priv->next_seqnum = data.seqnum;
921 payload->timestamp = data.rtptime;
923 GST_LOG_OBJECT (payload, "Preparing to push packet with size %"
924 G_GSIZE_FORMAT ", seq=%d, rtptime=%u, pts %" GST_TIME_FORMAT,
925 (is_list) ? -1 : gst_buffer_get_size (GST_BUFFER (obj)),
926 payload->seqnum, data.rtptime, GST_TIME_ARGS (data.pts));
928 if (g_atomic_int_compare_and_exchange (&payload->priv->
929 notified_first_timestamp, 1, 0)) {
930 g_object_notify (G_OBJECT (payload), "timestamp");
931 g_object_notify (G_OBJECT (payload), "seqnum");
939 GST_ELEMENT_ERROR (payload, STREAM, NOT_IMPLEMENTED, (NULL),
940 ("subclass did not specify clock-rate"));
941 return GST_FLOW_ERROR;
946 * gst_rtp_base_payload_push_list:
947 * @payload: a #GstRTPBasePayload
948 * @list: a #GstBufferList
950 * Push @list to the peer element of the payloader. The SSRC, payload type,
951 * seqnum and timestamp of the RTP buffer will be updated first.
953 * This function takes ownership of @list.
955 * Returns: a #GstFlowReturn.
958 gst_rtp_base_payload_push_list (GstRTPBasePayload * payload,
959 GstBufferList * list)
963 res = gst_rtp_base_payload_prepare_push (payload, list, TRUE);
965 if (G_LIKELY (res == GST_FLOW_OK)) {
966 if (G_UNLIKELY (payload->priv->pending_segment)) {
967 gst_pad_push_event (payload->srcpad, payload->priv->pending_segment);
968 payload->priv->pending_segment = FALSE;
969 payload->priv->delay_segment = FALSE;
971 res = gst_pad_push_list (payload->srcpad, list);
973 gst_buffer_list_unref (list);
980 * gst_rtp_base_payload_push:
981 * @payload: a #GstRTPBasePayload
982 * @buffer: a #GstBuffer
984 * Push @buffer to the peer element of the payloader. The SSRC, payload type,
985 * seqnum and timestamp of the RTP buffer will be updated first.
987 * This function takes ownership of @buffer.
989 * Returns: a #GstFlowReturn.
992 gst_rtp_base_payload_push (GstRTPBasePayload * payload, GstBuffer * buffer)
996 res = gst_rtp_base_payload_prepare_push (payload, buffer, FALSE);
998 if (G_LIKELY (res == GST_FLOW_OK)) {
999 if (G_UNLIKELY (payload->priv->pending_segment)) {
1000 gst_pad_push_event (payload->srcpad, payload->priv->pending_segment);
1001 payload->priv->pending_segment = FALSE;
1002 payload->priv->delay_segment = FALSE;
1004 res = gst_pad_push (payload->srcpad, buffer);
1006 gst_buffer_unref (buffer);
1013 gst_rtp_base_payload_set_property (GObject * object, guint prop_id,
1014 const GValue * value, GParamSpec * pspec)
1016 GstRTPBasePayload *rtpbasepayload;
1017 GstRTPBasePayloadPrivate *priv;
1020 rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
1021 priv = rtpbasepayload->priv;
1025 rtpbasepayload->mtu = g_value_get_uint (value);
1028 rtpbasepayload->pt = g_value_get_uint (value);
1031 val = g_value_get_uint (value);
1032 rtpbasepayload->ssrc = val;
1033 priv->ssrc_random = FALSE;
1035 case PROP_TIMESTAMP_OFFSET:
1036 val = g_value_get_uint (value);
1037 rtpbasepayload->ts_offset = val;
1038 priv->ts_offset_random = FALSE;
1040 case PROP_SEQNUM_OFFSET:
1041 val = g_value_get_int (value);
1042 rtpbasepayload->seqnum_offset = val;
1043 priv->seqnum_offset_random = (val == -1);
1044 GST_DEBUG_OBJECT (rtpbasepayload, "seqnum offset 0x%04x, random %d",
1045 rtpbasepayload->seqnum_offset, priv->seqnum_offset_random);
1047 case PROP_MAX_PTIME:
1048 rtpbasepayload->priv->prop_max_ptime = g_value_get_int64 (value);
1049 update_max_ptime (rtpbasepayload);
1051 case PROP_MIN_PTIME:
1052 rtpbasepayload->min_ptime = g_value_get_int64 (value);
1054 case PROP_PERFECT_RTPTIME:
1055 priv->perfect_rtptime = g_value_get_boolean (value);
1057 case PROP_PTIME_MULTIPLE:
1058 rtpbasepayload->ptime_multiple = g_value_get_int64 (value);
1061 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1067 gst_rtp_base_payload_get_property (GObject * object, guint prop_id,
1068 GValue * value, GParamSpec * pspec)
1070 GstRTPBasePayload *rtpbasepayload;
1071 GstRTPBasePayloadPrivate *priv;
1073 rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
1074 priv = rtpbasepayload->priv;
1078 g_value_set_uint (value, rtpbasepayload->mtu);
1081 g_value_set_uint (value, rtpbasepayload->pt);
1084 if (priv->ssrc_random)
1085 g_value_set_uint (value, -1);
1087 g_value_set_uint (value, rtpbasepayload->ssrc);
1089 case PROP_TIMESTAMP_OFFSET:
1090 if (priv->ts_offset_random)
1091 g_value_set_uint (value, -1);
1093 g_value_set_uint (value, (guint32) rtpbasepayload->ts_offset);
1095 case PROP_SEQNUM_OFFSET:
1096 if (priv->seqnum_offset_random)
1097 g_value_set_int (value, -1);
1099 g_value_set_int (value, (guint16) rtpbasepayload->seqnum_offset);
1101 case PROP_MAX_PTIME:
1102 g_value_set_int64 (value, rtpbasepayload->max_ptime);
1104 case PROP_MIN_PTIME:
1105 g_value_set_int64 (value, rtpbasepayload->min_ptime);
1107 case PROP_TIMESTAMP:
1108 g_value_set_uint (value, rtpbasepayload->timestamp);
1111 g_value_set_uint (value, rtpbasepayload->seqnum);
1113 case PROP_PERFECT_RTPTIME:
1114 g_value_set_boolean (value, priv->perfect_rtptime);
1116 case PROP_PTIME_MULTIPLE:
1117 g_value_set_int64 (value, rtpbasepayload->ptime_multiple);
1120 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1125 static GstStateChangeReturn
1126 gst_rtp_base_payload_change_state (GstElement * element,
1127 GstStateChange transition)
1129 GstRTPBasePayload *rtpbasepayload;
1130 GstRTPBasePayloadPrivate *priv;
1131 GstStateChangeReturn ret;
1133 rtpbasepayload = GST_RTP_BASE_PAYLOAD (element);
1134 priv = rtpbasepayload->priv;
1136 switch (transition) {
1137 case GST_STATE_CHANGE_NULL_TO_READY:
1139 case GST_STATE_CHANGE_READY_TO_PAUSED:
1140 gst_segment_init (&rtpbasepayload->segment, GST_FORMAT_UNDEFINED);
1141 rtpbasepayload->priv->delay_segment = TRUE;
1142 gst_event_replace (&rtpbasepayload->priv->pending_segment, NULL);
1144 if (priv->seqnum_offset_random)
1145 rtpbasepayload->seqnum_base = g_random_int_range (0, G_MAXUINT16);
1147 rtpbasepayload->seqnum_base = rtpbasepayload->seqnum_offset;
1148 priv->next_seqnum = rtpbasepayload->seqnum_base;
1149 rtpbasepayload->seqnum = rtpbasepayload->seqnum_base;
1151 if (priv->ssrc_random)
1152 rtpbasepayload->current_ssrc = g_random_int ();
1154 rtpbasepayload->current_ssrc = rtpbasepayload->ssrc;
1156 if (priv->ts_offset_random)
1157 rtpbasepayload->ts_base = g_random_int ();
1159 rtpbasepayload->ts_base = rtpbasepayload->ts_offset;
1160 rtpbasepayload->timestamp = rtpbasepayload->ts_base;
1161 g_atomic_int_set (&rtpbasepayload->priv->notified_first_timestamp, 1);
1162 priv->base_offset = GST_BUFFER_OFFSET_NONE;
1163 priv->negotiated = FALSE;
1169 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1171 switch (transition) {
1172 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1173 g_atomic_int_set (&rtpbasepayload->priv->notified_first_timestamp, 1);
1175 case GST_STATE_CHANGE_PAUSED_TO_READY:
1176 gst_event_replace (&rtpbasepayload->priv->pending_segment, NULL);