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_getcaps_default (GstRTPBasePayload *
100 rtpbasepayload, GstPad * pad, GstCaps * filter);
102 static gboolean gst_rtp_base_payload_sink_event_default (GstRTPBasePayload *
103 rtpbasepayload, GstEvent * event);
104 static gboolean gst_rtp_base_payload_sink_event (GstPad * pad,
105 GstObject * parent, GstEvent * event);
106 static gboolean gst_rtp_base_payload_query_default (GstRTPBasePayload *
107 rtpbasepayload, GstPad * pad, GstQuery * query);
108 static gboolean gst_rtp_base_payload_query (GstPad * pad, GstObject * parent,
110 static GstFlowReturn gst_rtp_base_payload_chain (GstPad * pad,
111 GstObject * parent, GstBuffer * buffer);
113 static void gst_rtp_base_payload_set_property (GObject * object, guint prop_id,
114 const GValue * value, GParamSpec * pspec);
115 static void gst_rtp_base_payload_get_property (GObject * object, guint prop_id,
116 GValue * value, GParamSpec * pspec);
118 static GstStateChangeReturn gst_rtp_base_payload_change_state (GstElement *
119 element, GstStateChange transition);
121 static GstElementClass *parent_class = NULL;
124 gst_rtp_base_payload_get_type (void)
126 static GType rtpbasepayload_type = 0;
128 if (g_once_init_enter ((gsize *) & rtpbasepayload_type)) {
129 static const GTypeInfo rtpbasepayload_info = {
130 sizeof (GstRTPBasePayloadClass),
133 (GClassInitFunc) gst_rtp_base_payload_class_init,
136 sizeof (GstRTPBasePayload),
138 (GInstanceInitFunc) gst_rtp_base_payload_init,
141 g_once_init_leave ((gsize *) & rtpbasepayload_type,
142 g_type_register_static (GST_TYPE_ELEMENT, "GstRTPBasePayload",
143 &rtpbasepayload_info, G_TYPE_FLAG_ABSTRACT));
145 return rtpbasepayload_type;
149 gst_rtp_base_payload_class_init (GstRTPBasePayloadClass * klass)
151 GObjectClass *gobject_class;
152 GstElementClass *gstelement_class;
154 gobject_class = (GObjectClass *) klass;
155 gstelement_class = (GstElementClass *) klass;
157 g_type_class_add_private (klass, sizeof (GstRTPBasePayloadPrivate));
159 parent_class = g_type_class_peek_parent (klass);
161 gobject_class->finalize = gst_rtp_base_payload_finalize;
163 gobject_class->set_property = gst_rtp_base_payload_set_property;
164 gobject_class->get_property = gst_rtp_base_payload_get_property;
166 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MTU,
167 g_param_spec_uint ("mtu", "MTU",
168 "Maximum size of one packet",
169 28, G_MAXUINT, DEFAULT_MTU,
170 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PT,
172 g_param_spec_uint ("pt", "payload type",
173 "The payload type of the packets", 0, 0x80, DEFAULT_PT,
174 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
176 g_param_spec_uint ("ssrc", "SSRC",
177 "The SSRC of the packets (default == random)", 0, G_MAXUINT32,
178 DEFAULT_SSRC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
179 g_object_class_install_property (G_OBJECT_CLASS (klass),
180 PROP_TIMESTAMP_OFFSET, g_param_spec_uint ("timestamp-offset",
182 "Offset to add to all outgoing timestamps (default = random)", 0,
183 G_MAXUINT32, DEFAULT_TIMESTAMP_OFFSET,
184 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
185 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
186 g_param_spec_int ("seqnum-offset", "Sequence number Offset",
187 "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXUINT16,
188 DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_PTIME,
190 g_param_spec_int64 ("max-ptime", "Max packet time",
191 "Maximum duration of the packet data in ns (-1 = unlimited up to MTU)",
192 -1, G_MAXINT64, DEFAULT_MAX_PTIME,
193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195 * GstRTPBaseAudioPayload:min-ptime:
197 * Minimum duration of the packet data in ns (can't go above MTU)
201 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MIN_PTIME,
202 g_param_spec_int64 ("min-ptime", "Min packet time",
203 "Minimum duration of the packet data in ns (can't go above MTU)",
204 0, G_MAXINT64, DEFAULT_MIN_PTIME,
205 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMESTAMP,
208 g_param_spec_uint ("timestamp", "Timestamp",
209 "The RTP timestamp of the last processed packet",
210 0, G_MAXUINT32, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
211 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
212 g_param_spec_uint ("seqnum", "Sequence number",
213 "The RTP sequence number of the last processed packet",
214 0, G_MAXUINT16, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
217 * GstRTPBaseAudioPayload:perfect-rtptime:
219 * Try to use the offset fields to generate perfect RTP timestamps. when this
220 * option is disabled, RTP timestamps are generated from the GStreamer
221 * timestamps, which could result in RTP timestamps that don't increment with
222 * the amount of data in the packet.
226 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PERFECT_RTPTIME,
227 g_param_spec_boolean ("perfect-rtptime", "Perfect RTP Time",
228 "Generate perfect RTP timestamps when possible",
229 DEFAULT_PERFECT_RTPTIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
231 * GstRTPBaseAudioPayload:ptime-multiple:
233 * Force buffers to be multiples of this duration in ns (0 disables)
237 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PTIME_MULTIPLE,
238 g_param_spec_int64 ("ptime-multiple", "Packet time multiple",
239 "Force buffers to be multiples of this duration in ns (0 disables)",
240 0, G_MAXINT64, DEFAULT_PTIME_MULTIPLE,
241 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243 gstelement_class->change_state = gst_rtp_base_payload_change_state;
245 klass->get_caps = gst_rtp_base_payload_getcaps_default;
246 klass->sink_event = gst_rtp_base_payload_sink_event_default;
247 klass->query = gst_rtp_base_payload_query_default;
249 GST_DEBUG_CATEGORY_INIT (rtpbasepayload_debug, "rtpbasepayload", 0,
250 "Base class for RTP Payloaders");
254 gst_rtp_base_payload_init (GstRTPBasePayload * rtpbasepayload, gpointer g_class)
256 GstPadTemplate *templ;
257 GstRTPBasePayloadPrivate *priv;
259 rtpbasepayload->priv = priv =
260 GST_RTP_BASE_PAYLOAD_GET_PRIVATE (rtpbasepayload);
263 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
264 g_return_if_fail (templ != NULL);
266 rtpbasepayload->srcpad = gst_pad_new_from_template (templ, "src");
267 gst_element_add_pad (GST_ELEMENT (rtpbasepayload), rtpbasepayload->srcpad);
270 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
271 g_return_if_fail (templ != NULL);
273 rtpbasepayload->sinkpad = gst_pad_new_from_template (templ, "sink");
274 gst_pad_set_chain_function (rtpbasepayload->sinkpad,
275 gst_rtp_base_payload_chain);
276 gst_pad_set_event_function (rtpbasepayload->sinkpad,
277 gst_rtp_base_payload_sink_event);
278 gst_pad_set_query_function (rtpbasepayload->sinkpad,
279 gst_rtp_base_payload_query);
280 gst_element_add_pad (GST_ELEMENT (rtpbasepayload), rtpbasepayload->sinkpad);
282 rtpbasepayload->mtu = DEFAULT_MTU;
283 rtpbasepayload->pt = DEFAULT_PT;
284 rtpbasepayload->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
285 rtpbasepayload->ssrc = DEFAULT_SSRC;
286 rtpbasepayload->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
287 priv->seqnum_offset_random = (rtpbasepayload->seqnum_offset == -1);
288 priv->ts_offset_random = (rtpbasepayload->ts_offset == -1);
289 priv->ssrc_random = (rtpbasepayload->ssrc == -1);
291 rtpbasepayload->max_ptime = DEFAULT_MAX_PTIME;
292 rtpbasepayload->min_ptime = DEFAULT_MIN_PTIME;
293 rtpbasepayload->priv->perfect_rtptime = DEFAULT_PERFECT_RTPTIME;
294 rtpbasepayload->ptime_multiple = DEFAULT_PTIME_MULTIPLE;
295 rtpbasepayload->priv->base_offset = GST_BUFFER_OFFSET_NONE;
296 rtpbasepayload->priv->base_rtime = GST_BUFFER_OFFSET_NONE;
298 rtpbasepayload->media = NULL;
299 rtpbasepayload->encoding_name = NULL;
301 rtpbasepayload->clock_rate = 0;
303 rtpbasepayload->priv->caps_max_ptime = DEFAULT_MAX_PTIME;
304 rtpbasepayload->priv->prop_max_ptime = DEFAULT_MAX_PTIME;
308 gst_rtp_base_payload_finalize (GObject * object)
310 GstRTPBasePayload *rtpbasepayload;
312 rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
314 g_free (rtpbasepayload->media);
315 rtpbasepayload->media = NULL;
316 g_free (rtpbasepayload->encoding_name);
317 rtpbasepayload->encoding_name = NULL;
319 G_OBJECT_CLASS (parent_class)->finalize (object);
323 gst_rtp_base_payload_getcaps_default (GstRTPBasePayload * rtpbasepayload,
324 GstPad * pad, GstCaps * filter)
328 caps = GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
329 GST_DEBUG_OBJECT (pad,
330 "using pad template %p with caps %p %" GST_PTR_FORMAT,
331 GST_PAD_PAD_TEMPLATE (pad), caps, caps);
334 caps = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
336 caps = gst_caps_ref (caps);
342 gst_rtp_base_payload_sink_event_default (GstRTPBasePayload * rtpbasepayload,
345 GstObject *parent = GST_OBJECT_CAST (rtpbasepayload);
346 gboolean res = FALSE;
348 switch (GST_EVENT_TYPE (event)) {
349 case GST_EVENT_FLUSH_START:
350 res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
352 case GST_EVENT_FLUSH_STOP:
353 res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
354 gst_segment_init (&rtpbasepayload->segment, GST_FORMAT_UNDEFINED);
358 GstRTPBasePayloadClass *rtpbasepayload_class;
361 gst_event_parse_caps (event, &caps);
362 GST_DEBUG_OBJECT (rtpbasepayload, "setting caps %" GST_PTR_FORMAT, caps);
364 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
365 if (rtpbasepayload_class->set_caps)
366 res = rtpbasepayload_class->set_caps (rtpbasepayload, caps);
368 gst_event_unref (event);
371 case GST_EVENT_SEGMENT:
375 segment = &rtpbasepayload->segment;
376 gst_event_copy_segment (event, segment);
378 rtpbasepayload->priv->base_offset = GST_BUFFER_OFFSET_NONE;
380 GST_DEBUG_OBJECT (rtpbasepayload,
381 "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
382 res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
386 res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
393 gst_rtp_base_payload_sink_event (GstPad * pad, GstObject * parent,
396 GstRTPBasePayload *rtpbasepayload;
397 GstRTPBasePayloadClass *rtpbasepayload_class;
398 gboolean res = FALSE;
400 rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
401 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
403 if (rtpbasepayload_class->sink_event)
404 res = rtpbasepayload_class->sink_event (rtpbasepayload, event);
406 gst_event_unref (event);
412 gst_rtp_base_payload_query_default (GstRTPBasePayload * rtpbasepayload,
413 GstPad * pad, GstQuery * query)
415 gboolean res = FALSE;
417 switch (GST_QUERY_TYPE (query)) {
420 GstRTPBasePayloadClass *rtpbasepayload_class;
421 GstCaps *filter, *caps;
423 gst_query_parse_caps (query, &filter);
424 GST_DEBUG_OBJECT (rtpbasepayload, "getting caps with filter %"
425 GST_PTR_FORMAT, filter);
427 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
428 if (rtpbasepayload_class->get_caps) {
429 caps = rtpbasepayload_class->get_caps (rtpbasepayload, pad, filter);
430 gst_query_set_caps_result (query, caps);
437 gst_pad_query_default (pad, GST_OBJECT_CAST (rtpbasepayload), query);
444 gst_rtp_base_payload_query (GstPad * pad, GstObject * parent, GstQuery * query)
446 GstRTPBasePayload *rtpbasepayload;
447 GstRTPBasePayloadClass *rtpbasepayload_class;
448 gboolean res = FALSE;
450 rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
451 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
453 if (rtpbasepayload_class->query)
454 res = rtpbasepayload_class->query (rtpbasepayload, pad, query);
460 gst_rtp_base_payload_chain (GstPad * pad, GstObject * parent,
463 GstRTPBasePayload *rtpbasepayload;
464 GstRTPBasePayloadClass *rtpbasepayload_class;
467 rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
468 rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
470 if (!rtpbasepayload_class->handle_buffer)
473 ret = rtpbasepayload_class->handle_buffer (rtpbasepayload, buffer);
480 GST_ELEMENT_ERROR (rtpbasepayload, STREAM, NOT_IMPLEMENTED, (NULL),
481 ("subclass did not implement handle_buffer function"));
482 gst_buffer_unref (buffer);
483 return GST_FLOW_ERROR;
488 * gst_rtp_base_payload_set_options:
489 * @payload: a #GstRTPBasePayload
490 * @media: the media type (typically "audio" or "video")
491 * @dynamic: if the payload type is dynamic
492 * @encoding_name: the encoding name
493 * @clock_rate: the clock rate of the media
495 * Set the rtp options of the payloader. These options will be set in the caps
496 * of the payloader. Subclasses must call this method before calling
497 * gst_rtp_base_payload_push() or gst_rtp_base_payload_set_outcaps().
500 gst_rtp_base_payload_set_options (GstRTPBasePayload * payload,
501 const gchar * media, gboolean dynamic, const gchar * encoding_name,
504 g_return_if_fail (payload != NULL);
505 g_return_if_fail (clock_rate != 0);
507 g_free (payload->media);
508 payload->media = g_strdup (media);
509 payload->dynamic = dynamic;
510 g_free (payload->encoding_name);
511 payload->encoding_name = g_strdup (encoding_name);
512 payload->clock_rate = clock_rate;
516 copy_fixed (GQuark field_id, const GValue * value, GstStructure * dest)
518 if (gst_value_is_fixed (value)) {
519 gst_structure_id_set_value (dest, field_id, value);
525 update_max_ptime (GstRTPBasePayload * rtpbasepayload)
527 if (rtpbasepayload->priv->caps_max_ptime != -1 &&
528 rtpbasepayload->priv->prop_max_ptime != -1)
529 rtpbasepayload->max_ptime = MIN (rtpbasepayload->priv->caps_max_ptime,
530 rtpbasepayload->priv->prop_max_ptime);
531 else if (rtpbasepayload->priv->caps_max_ptime != -1)
532 rtpbasepayload->max_ptime = rtpbasepayload->priv->caps_max_ptime;
533 else if (rtpbasepayload->priv->prop_max_ptime != -1)
534 rtpbasepayload->max_ptime = rtpbasepayload->priv->prop_max_ptime;
536 rtpbasepayload->max_ptime = DEFAULT_MAX_PTIME;
540 * gst_rtp_base_payload_set_outcaps:
541 * @payload: a #GstRTPBasePayload
542 * @fieldname: the first field name or %NULL
545 * Configure the output caps with the optional parameters.
547 * Variable arguments should be in the form field name, field type
548 * (as a GType), value(s). The last variable argument should be NULL.
550 * Returns: %TRUE if the caps could be set.
553 gst_rtp_base_payload_set_outcaps (GstRTPBasePayload * payload,
554 const gchar * fieldname, ...)
556 GstCaps *srccaps, *peercaps;
559 /* fill in the defaults, their properties cannot be negotiated. */
560 srccaps = gst_caps_new_simple ("application/x-rtp",
561 "media", G_TYPE_STRING, payload->media,
562 "clock-rate", G_TYPE_INT, payload->clock_rate,
563 "encoding-name", G_TYPE_STRING, payload->encoding_name, NULL);
565 GST_DEBUG_OBJECT (payload, "defaults: %" GST_PTR_FORMAT, srccaps);
570 /* override with custom properties */
571 va_start (varargs, fieldname);
572 gst_caps_set_simple_valist (srccaps, fieldname, varargs);
575 GST_DEBUG_OBJECT (payload, "custom added: %" GST_PTR_FORMAT, srccaps);
578 payload->priv->caps_max_ptime = DEFAULT_MAX_PTIME;
581 /* the peer caps can override some of the defaults */
582 peercaps = gst_pad_peer_query_caps (payload->srcpad, srccaps);
583 if (peercaps == NULL) {
584 /* no peer caps, just add the other properties */
585 gst_caps_set_simple (srccaps,
586 "payload", G_TYPE_INT, GST_RTP_BASE_PAYLOAD_PT (payload),
587 "ssrc", G_TYPE_UINT, payload->current_ssrc,
588 "timestamp-offset", G_TYPE_UINT, payload->ts_base,
589 "seqnum-offset", G_TYPE_UINT, payload->seqnum_base, NULL);
591 GST_DEBUG_OBJECT (payload, "no peer caps: %" GST_PTR_FORMAT, srccaps);
597 guint max_ptime, ptime;
599 /* peer provides caps we can use to fixate. They are already intersected
600 * with our srccaps, just make them writable */
601 temp = gst_caps_make_writable (peercaps);
602 gst_caps_unref (srccaps);
604 if (gst_caps_is_empty (temp)) {
605 gst_caps_unref (temp);
609 /* now fixate, start by taking the first caps */
610 gst_caps_truncate (temp);
612 /* get first structure */
613 s = gst_caps_get_structure (temp, 0);
615 if (gst_structure_get_uint (s, "maxptime", &max_ptime))
616 payload->priv->caps_max_ptime = max_ptime * GST_MSECOND;
618 if (gst_structure_get_uint (s, "ptime", &ptime))
619 payload->ptime = ptime * GST_MSECOND;
621 if (gst_structure_get_int (s, "payload", &pt)) {
623 GST_RTP_BASE_PAYLOAD_PT (payload) = pt;
624 GST_LOG_OBJECT (payload, "using peer pt %d", pt);
626 if (gst_structure_has_field (s, "payload")) {
627 /* can only fixate if there is a field */
628 gst_structure_fixate_field_nearest_int (s, "payload",
629 GST_RTP_BASE_PAYLOAD_PT (payload));
630 gst_structure_get_int (s, "payload", &pt);
631 GST_LOG_OBJECT (payload, "using peer pt %d", pt);
633 /* no pt field, use the internal pt */
634 pt = GST_RTP_BASE_PAYLOAD_PT (payload);
635 gst_structure_set (s, "payload", G_TYPE_INT, pt, NULL);
636 GST_LOG_OBJECT (payload, "using internal pt %d", pt);
640 if (gst_structure_has_field_typed (s, "ssrc", G_TYPE_UINT)) {
641 value = gst_structure_get_value (s, "ssrc");
642 payload->current_ssrc = g_value_get_uint (value);
643 GST_LOG_OBJECT (payload, "using peer ssrc %08x", payload->current_ssrc);
645 /* FIXME, fixate_nearest_uint would be even better */
646 gst_structure_set (s, "ssrc", G_TYPE_UINT, payload->current_ssrc, NULL);
647 GST_LOG_OBJECT (payload, "using internal ssrc %08x",
648 payload->current_ssrc);
651 if (gst_structure_has_field_typed (s, "timestamp-offset", G_TYPE_UINT)) {
652 value = gst_structure_get_value (s, "timestamp-offset");
653 payload->ts_base = g_value_get_uint (value);
654 GST_LOG_OBJECT (payload, "using peer timestamp-offset %u",
657 /* FIXME, fixate_nearest_uint would be even better */
658 gst_structure_set (s, "timestamp-offset", G_TYPE_UINT, payload->ts_base,
660 GST_LOG_OBJECT (payload, "using internal timestamp-offset %u",
663 if (gst_structure_has_field_typed (s, "seqnum-offset", G_TYPE_UINT)) {
664 value = gst_structure_get_value (s, "seqnum-offset");
665 payload->seqnum_base = g_value_get_uint (value);
666 GST_LOG_OBJECT (payload, "using peer seqnum-offset %u",
667 payload->seqnum_base);
669 /* FIXME, fixate_nearest_uint would be even better */
670 gst_structure_set (s, "seqnum-offset", G_TYPE_UINT, payload->seqnum_base,
672 GST_LOG_OBJECT (payload, "using internal seqnum-offset %u",
673 payload->seqnum_base);
676 /* make the target caps by copying over all the fixed caps, removing the
678 srccaps = gst_caps_new_empty_simple (gst_structure_get_name (s));
679 d = gst_caps_get_structure (srccaps, 0);
681 gst_structure_foreach (s, (GstStructureForeachFunc) copy_fixed, d);
683 gst_caps_unref (temp);
685 GST_DEBUG_OBJECT (payload, "with peer caps: %" GST_PTR_FORMAT, srccaps);
688 update_max_ptime (payload);
690 res = gst_pad_set_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload), srccaps);
691 gst_caps_unref (srccaps);
697 * gst_rtp_base_payload_is_filled:
698 * @payload: a #GstRTPBasePayload
699 * @size: the size of the packet
700 * @duration: the duration of the packet
702 * Check if the packet with @size and @duration would exceed the configured
705 * Returns: %TRUE if the packet of @size and @duration would exceed the
706 * configured MTU or max_ptime.
709 gst_rtp_base_payload_is_filled (GstRTPBasePayload * payload,
710 guint size, GstClockTime duration)
712 if (size > payload->mtu)
715 if (payload->max_ptime != -1 && duration >= payload->max_ptime)
723 GstRTPBasePayload *payload;
727 GstClockTime timestamp;
733 find_timestamp (GstBuffer ** buffer, guint idx, HeaderData * data)
735 data->timestamp = GST_BUFFER_TIMESTAMP (*buffer);
736 data->offset = GST_BUFFER_OFFSET (*buffer);
738 /* stop when we find a timestamp. We take whatever offset is associated with
739 * the timestamp (if any) to do perfect timestamps when we need to. */
740 if (data->timestamp != -1)
747 set_headers (GstBuffer ** buffer, guint group, guint idx, HeaderData * data)
749 GstRTPBuffer rtp = { NULL, };
751 gst_rtp_buffer_map (*buffer, GST_MAP_WRITE, &rtp);
752 gst_rtp_buffer_set_ssrc (&rtp, data->ssrc);
753 gst_rtp_buffer_set_payload_type (&rtp, data->pt);
754 gst_rtp_buffer_set_seq (&rtp, data->seqnum);
755 gst_rtp_buffer_set_timestamp (&rtp, data->rtptime);
756 gst_rtp_buffer_unmap (&rtp);
758 /* increment the seqnum for each buffer */
764 /* Updates the SSRC, payload type, seqnum and timestamp of the RTP buffer
765 * before the buffer is pushed. */
767 gst_rtp_base_payload_prepare_push (GstRTPBasePayload * payload,
768 gpointer obj, gboolean is_list)
770 GstRTPBasePayloadPrivate *priv;
773 if (payload->clock_rate == 0)
776 priv = payload->priv;
778 /* update first, so that the property is set to the last
780 payload->seqnum = priv->next_seqnum;
782 /* fill in the fields we want to set on all headers */
783 data.payload = payload;
784 data.seqnum = payload->seqnum;
785 data.ssrc = payload->current_ssrc;
786 data.pt = payload->pt;
788 /* find the first buffer with a timestamp */
791 data.offset = GST_BUFFER_OFFSET_NONE;
792 gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj),
793 (GstBufferListFunc) find_timestamp, &data);
795 data.timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (obj));
796 data.offset = GST_BUFFER_OFFSET (GST_BUFFER_CAST (obj));
799 /* convert to RTP time */
800 if (priv->perfect_rtptime && data.offset != GST_BUFFER_OFFSET_NONE &&
801 priv->base_offset != GST_BUFFER_OFFSET_NONE) {
802 /* if we have an offset, use that for making an RTP timestamp */
803 data.rtptime = payload->ts_base + priv->base_rtime +
804 data.offset - priv->base_offset;
805 GST_LOG_OBJECT (payload,
806 "Using offset %" G_GUINT64_FORMAT " for RTP timestamp", data.offset);
807 } else if (GST_CLOCK_TIME_IS_VALID (data.timestamp)) {
810 /* no offset, use the gstreamer timestamp */
811 rtime = gst_segment_to_running_time (&payload->segment, GST_FORMAT_TIME,
815 GST_LOG_OBJECT (payload, "Clipped timestamp, using base RTP timestamp");
818 GST_LOG_OBJECT (payload,
819 "Using running_time %" GST_TIME_FORMAT " for RTP timestamp",
820 GST_TIME_ARGS (rtime));
822 gst_util_uint64_scale_int (rtime, payload->clock_rate, GST_SECOND);
823 priv->base_offset = data.offset;
824 priv->base_rtime = rtime;
826 /* add running_time in clock-rate units to the base timestamp */
827 data.rtptime = payload->ts_base + rtime;
829 GST_LOG_OBJECT (payload,
830 "Using previous RTP timestamp %" G_GUINT32_FORMAT, payload->timestamp);
831 /* no timestamp to convert, take previous timestamp */
832 data.rtptime = payload->timestamp;
835 /* set ssrc, payload type, seq number, caps and rtptime */
837 gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj),
838 (GstBufferListFunc) set_headers, &data);
840 GstBuffer *buf = GST_BUFFER_CAST (obj);
841 set_headers (&buf, 0, 0, &data);
844 priv->next_seqnum = data.seqnum;
845 payload->timestamp = data.rtptime;
847 GST_LOG_OBJECT (payload, "Preparing to push packet with size %"
848 G_GSIZE_FORMAT ", seq=%d, rtptime=%u, timestamp %" GST_TIME_FORMAT,
849 (is_list) ? -1 : gst_buffer_get_size (GST_BUFFER (obj)),
850 payload->seqnum, data.rtptime, GST_TIME_ARGS (data.timestamp));
852 if (g_atomic_int_compare_and_exchange (&payload->
853 priv->notified_first_timestamp, 1, 0)) {
854 g_object_notify (G_OBJECT (payload), "timestamp");
855 g_object_notify (G_OBJECT (payload), "seqnum");
863 GST_ELEMENT_ERROR (payload, STREAM, NOT_IMPLEMENTED, (NULL),
864 ("subclass did not specify clock-rate"));
865 return GST_FLOW_ERROR;
870 * gst_rtp_base_payload_push_list:
871 * @payload: a #GstRTPBasePayload
872 * @list: a #GstBufferList
874 * Push @list to the peer element of the payloader. The SSRC, payload type,
875 * seqnum and timestamp of the RTP buffer will be updated first.
877 * This function takes ownership of @list.
879 * Returns: a #GstFlowReturn.
884 gst_rtp_base_payload_push_list (GstRTPBasePayload * payload,
885 GstBufferList * list)
889 res = gst_rtp_base_payload_prepare_push (payload, list, TRUE);
891 if (G_LIKELY (res == GST_FLOW_OK))
892 res = gst_pad_push_list (payload->srcpad, list);
894 gst_buffer_list_unref (list);
900 * gst_rtp_base_payload_push:
901 * @payload: a #GstRTPBasePayload
902 * @buffer: a #GstBuffer
904 * Push @buffer to the peer element of the payloader. The SSRC, payload type,
905 * seqnum and timestamp of the RTP buffer will be updated first.
907 * This function takes ownership of @buffer.
909 * Returns: a #GstFlowReturn.
912 gst_rtp_base_payload_push (GstRTPBasePayload * payload, GstBuffer * buffer)
916 res = gst_rtp_base_payload_prepare_push (payload, buffer, FALSE);
918 if (G_LIKELY (res == GST_FLOW_OK))
919 res = gst_pad_push (payload->srcpad, buffer);
921 gst_buffer_unref (buffer);
927 gst_rtp_base_payload_set_property (GObject * object, guint prop_id,
928 const GValue * value, GParamSpec * pspec)
930 GstRTPBasePayload *rtpbasepayload;
931 GstRTPBasePayloadPrivate *priv;
934 rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
935 priv = rtpbasepayload->priv;
939 rtpbasepayload->mtu = g_value_get_uint (value);
942 rtpbasepayload->pt = g_value_get_uint (value);
945 val = g_value_get_uint (value);
946 rtpbasepayload->ssrc = val;
947 priv->ssrc_random = FALSE;
949 case PROP_TIMESTAMP_OFFSET:
950 val = g_value_get_uint (value);
951 rtpbasepayload->ts_offset = val;
952 priv->ts_offset_random = FALSE;
954 case PROP_SEQNUM_OFFSET:
955 val = g_value_get_int (value);
956 rtpbasepayload->seqnum_offset = val;
957 priv->seqnum_offset_random = (val == -1);
958 GST_DEBUG_OBJECT (rtpbasepayload, "seqnum offset 0x%04x, random %d",
959 rtpbasepayload->seqnum_offset, priv->seqnum_offset_random);
962 rtpbasepayload->priv->prop_max_ptime = g_value_get_int64 (value);
963 update_max_ptime (rtpbasepayload);
966 rtpbasepayload->min_ptime = g_value_get_int64 (value);
968 case PROP_PERFECT_RTPTIME:
969 priv->perfect_rtptime = g_value_get_boolean (value);
971 case PROP_PTIME_MULTIPLE:
972 rtpbasepayload->ptime_multiple = g_value_get_int64 (value);
975 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
981 gst_rtp_base_payload_get_property (GObject * object, guint prop_id,
982 GValue * value, GParamSpec * pspec)
984 GstRTPBasePayload *rtpbasepayload;
985 GstRTPBasePayloadPrivate *priv;
987 rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
988 priv = rtpbasepayload->priv;
992 g_value_set_uint (value, rtpbasepayload->mtu);
995 g_value_set_uint (value, rtpbasepayload->pt);
998 if (priv->ssrc_random)
999 g_value_set_uint (value, -1);
1001 g_value_set_uint (value, rtpbasepayload->ssrc);
1003 case PROP_TIMESTAMP_OFFSET:
1004 if (priv->ts_offset_random)
1005 g_value_set_uint (value, -1);
1007 g_value_set_uint (value, (guint32) rtpbasepayload->ts_offset);
1009 case PROP_SEQNUM_OFFSET:
1010 if (priv->seqnum_offset_random)
1011 g_value_set_int (value, -1);
1013 g_value_set_int (value, (guint16) rtpbasepayload->seqnum_offset);
1015 case PROP_MAX_PTIME:
1016 g_value_set_int64 (value, rtpbasepayload->max_ptime);
1018 case PROP_MIN_PTIME:
1019 g_value_set_int64 (value, rtpbasepayload->min_ptime);
1021 case PROP_TIMESTAMP:
1022 g_value_set_uint (value, rtpbasepayload->timestamp);
1025 g_value_set_uint (value, rtpbasepayload->seqnum);
1027 case PROP_PERFECT_RTPTIME:
1028 g_value_set_boolean (value, priv->perfect_rtptime);
1030 case PROP_PTIME_MULTIPLE:
1031 g_value_set_int64 (value, rtpbasepayload->ptime_multiple);
1034 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1039 static GstStateChangeReturn
1040 gst_rtp_base_payload_change_state (GstElement * element,
1041 GstStateChange transition)
1043 GstRTPBasePayload *rtpbasepayload;
1044 GstRTPBasePayloadPrivate *priv;
1045 GstStateChangeReturn ret;
1047 rtpbasepayload = GST_RTP_BASE_PAYLOAD (element);
1048 priv = rtpbasepayload->priv;
1050 switch (transition) {
1051 case GST_STATE_CHANGE_NULL_TO_READY:
1053 case GST_STATE_CHANGE_READY_TO_PAUSED:
1054 gst_segment_init (&rtpbasepayload->segment, GST_FORMAT_UNDEFINED);
1056 if (priv->seqnum_offset_random)
1057 rtpbasepayload->seqnum_base = g_random_int_range (0, G_MAXUINT16);
1059 rtpbasepayload->seqnum_base = rtpbasepayload->seqnum_offset;
1060 priv->next_seqnum = rtpbasepayload->seqnum_base;
1061 rtpbasepayload->seqnum = rtpbasepayload->seqnum_base;
1063 if (priv->ssrc_random)
1064 rtpbasepayload->current_ssrc = g_random_int ();
1066 rtpbasepayload->current_ssrc = rtpbasepayload->ssrc;
1068 if (priv->ts_offset_random)
1069 rtpbasepayload->ts_base = g_random_int ();
1071 rtpbasepayload->ts_base = rtpbasepayload->ts_offset;
1072 rtpbasepayload->timestamp = rtpbasepayload->ts_base;
1073 g_atomic_int_set (&rtpbasepayload->priv->notified_first_timestamp, 1);
1074 priv->base_offset = GST_BUFFER_OFFSET_NONE;
1080 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1082 switch (transition) {
1083 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1084 g_atomic_int_set (&rtpbasepayload->priv->notified_first_timestamp, 1);
1086 case GST_STATE_CHANGE_READY_TO_NULL: