2 * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
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 details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:element-gdppay
26 * This element payloads GStreamer buffers and events using the
27 * GStreamer Data Protocol.
36 #include <gst/dataprotocol/dataprotocol.h>
38 #include "gstgdppay.h"
40 /* elementfactory information */
41 static const GstElementDetails gdp_pay_details =
42 GST_ELEMENT_DETAILS ("GDP Payloader",
44 "Payloads GStreamer Data Protocol buffers",
45 "Thomas Vander Stichele <thomas at apestaart dot org>");
47 static GstStaticPadTemplate gdp_pay_sink_template =
48 GST_STATIC_PAD_TEMPLATE ("sink",
53 static GstStaticPadTemplate gdp_pay_src_template =
54 GST_STATIC_PAD_TEMPLATE ("src",
57 GST_STATIC_CAPS ("application/x-gdp"));
59 GST_DEBUG_CATEGORY_STATIC (gst_gdp_pay_debug);
60 #define GST_CAT_DEFAULT gst_gdp_pay_debug
62 #define DEFAULT_CRC_HEADER TRUE
63 #define DEFAULT_CRC_PAYLOAD FALSE
64 #define DEFAULT_VERSION GST_DP_VERSION_1_0
75 GST_DEBUG_CATEGORY_INIT (gst_gdp_pay_debug, "gdppay", 0, \
78 GST_BOILERPLATE_FULL (GstGDPPay, gst_gdp_pay, GstElement,
79 GST_TYPE_ELEMENT, _do_init);
81 static GstFlowReturn gst_gdp_pay_chain (GstPad * pad, GstBuffer * buffer);
82 static gboolean gst_gdp_pay_sink_event (GstPad * pad, GstEvent * event);
83 static GstStateChangeReturn gst_gdp_pay_change_state (GstElement *
84 element, GstStateChange transition);
86 static void gst_gdp_pay_set_property (GObject * object, guint prop_id,
87 const GValue * value, GParamSpec * pspec);
88 static void gst_gdp_pay_get_property (GObject * object, guint prop_id,
89 GValue * value, GParamSpec * pspec);
91 static void gst_gdp_pay_dispose (GObject * gobject);
94 gst_gdp_pay_base_init (gpointer g_class)
96 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
98 gst_element_class_set_details (element_class, &gdp_pay_details);
100 gst_element_class_add_pad_template (element_class,
101 gst_static_pad_template_get (&gdp_pay_sink_template));
102 gst_element_class_add_pad_template (element_class,
103 gst_static_pad_template_get (&gdp_pay_src_template));
107 gst_gdp_pay_class_init (GstGDPPayClass * klass)
109 GObjectClass *gobject_class;
110 GstElementClass *gstelement_class;
112 gobject_class = (GObjectClass *) klass;
113 gstelement_class = (GstElementClass *) klass;
115 parent_class = g_type_class_peek_parent (klass);
117 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_gdp_pay_set_property);
118 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_gdp_pay_get_property);
119 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_gdp_pay_dispose);
120 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_gdp_pay_change_state);
122 g_object_class_install_property (gobject_class, PROP_CRC_HEADER,
123 g_param_spec_boolean ("crc-header", "CRC Header",
124 "Calculate and store a CRC checksum on the header",
125 DEFAULT_CRC_HEADER, G_PARAM_READWRITE));
126 g_object_class_install_property (gobject_class, PROP_CRC_PAYLOAD,
127 g_param_spec_boolean ("crc-payload", "CRC Payload",
128 "Calculate and store a CRC checksum on the payload",
129 DEFAULT_CRC_PAYLOAD, G_PARAM_READWRITE));
130 g_object_class_install_property (gobject_class, PROP_VERSION,
131 g_param_spec_enum ("version", "Version",
132 "Version of the GStreamer Data Protocol",
133 GST_TYPE_DP_VERSION, DEFAULT_VERSION, G_PARAM_READWRITE));
137 gst_gdp_pay_init (GstGDPPay * gdppay, GstGDPPayClass * g_class)
140 gst_pad_new_from_static_template (&gdp_pay_sink_template, "sink");
141 gst_pad_set_chain_function (gdppay->sinkpad,
142 GST_DEBUG_FUNCPTR (gst_gdp_pay_chain));
143 gst_pad_set_event_function (gdppay->sinkpad,
144 GST_DEBUG_FUNCPTR (gst_gdp_pay_sink_event));
145 gst_element_add_pad (GST_ELEMENT (gdppay), gdppay->sinkpad);
148 gst_pad_new_from_static_template (&gdp_pay_src_template, "src");
149 gst_element_add_pad (GST_ELEMENT (gdppay), gdppay->srcpad);
153 gdppay->crc_header = DEFAULT_CRC_HEADER;
154 gdppay->crc_payload = DEFAULT_CRC_PAYLOAD;
155 gdppay->header_flag = gdppay->crc_header | gdppay->crc_payload;
156 gdppay->version = DEFAULT_VERSION;
160 gst_gdp_pay_dispose (GObject * gobject)
162 GstGDPPay *this = GST_GDP_PAY (gobject);
164 if (this->caps_buf) {
165 gst_buffer_unref (this->caps_buf);
166 this->caps_buf = NULL;
168 if (this->new_segment_buf) {
169 gst_buffer_unref (this->new_segment_buf);
170 this->new_segment_buf = NULL;
172 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (gobject));
175 /* set OFFSET and OFFSET_END with running count */
177 gst_gdp_stamp_buffer (GstGDPPay * this, GstBuffer * buffer)
179 GST_BUFFER_OFFSET (buffer) = this->offset;
180 GST_BUFFER_OFFSET_END (buffer) = this->offset + GST_BUFFER_SIZE (buffer);
181 this->offset = GST_BUFFER_OFFSET_END (buffer);
185 gst_gdp_buffer_from_caps (GstGDPPay * this, GstCaps * caps)
187 GstBuffer *headerbuf;
188 GstBuffer *payloadbuf;
189 guint8 *header, *payload;
192 if (!this->packetizer->packet_from_caps (caps, this->header_flag, &len,
193 &header, &payload)) {
194 GST_WARNING_OBJECT (this, "could not create GDP header from caps");
198 GST_LOG_OBJECT (this, "creating GDP header and payload buffer from caps");
199 headerbuf = gst_buffer_new ();
200 gst_buffer_set_data (headerbuf, header, len);
201 GST_BUFFER_MALLOCDATA (headerbuf) = header;
203 payloadbuf = gst_buffer_new ();
204 gst_buffer_set_data (payloadbuf, payload,
205 gst_dp_header_payload_length (header));
206 GST_BUFFER_MALLOCDATA (payloadbuf) = payload;
208 return gst_buffer_join (headerbuf, payloadbuf);
212 gst_gdp_pay_buffer_from_buffer (GstGDPPay * this, GstBuffer * buffer)
214 GstBuffer *headerbuf;
218 if (!this->packetizer->header_from_buffer (buffer, this->header_flag, &len,
220 GST_WARNING_OBJECT (this, "could not create GDP header from buffer");
224 GST_LOG_OBJECT (this, "creating GDP header and payload buffer from buffer");
225 headerbuf = gst_buffer_new ();
226 gst_buffer_set_data (headerbuf, header, len);
227 GST_BUFFER_MALLOCDATA (headerbuf) = header;
229 /* we do not want to lose the ref on the incoming buffer */
230 gst_buffer_ref (buffer);
231 return gst_buffer_join (headerbuf, buffer);
235 gst_gdp_buffer_from_event (GstGDPPay * this, GstEvent * event)
237 GstBuffer *headerbuf;
238 GstBuffer *payloadbuf;
239 guint8 *header, *payload;
244 this->packetizer->packet_from_event (event, this->header_flag, &len,
248 GST_WARNING_OBJECT (this, "could not create GDP header from event %s (%d)",
249 gst_event_type_get_name (event->type), event->type);
253 GST_LOG_OBJECT (this, "creating GDP header and payload buffer from event");
254 headerbuf = gst_buffer_new ();
255 gst_buffer_set_data (headerbuf, header, len);
256 GST_BUFFER_MALLOCDATA (headerbuf) = header;
258 payloadbuf = gst_buffer_new ();
259 gst_buffer_set_data (payloadbuf, payload,
260 gst_dp_header_payload_length (header));
261 GST_BUFFER_MALLOCDATA (payloadbuf) = payload;
263 return gst_buffer_join (headerbuf, payloadbuf);
267 /* set our caps with streamheader, based on the latest newsegment and caps,
268 * and (possibly) GDP-serialized buffers of the streamheaders on the src pad */
270 gst_gdp_pay_reset_streamheader (GstGDPPay * this)
273 GstStructure *structure;
274 GstBuffer *new_segment_buf = NULL, *caps_buf, *tag_buf = NULL;
275 GstFlowReturn r = GST_FLOW_OK;
276 gboolean version_one_zero = TRUE;
278 GValue array = { 0 };
279 GValue value = { 0 };
281 /* In version 0.2, we didn't need or send new segment or tags */
282 if (this->version == GST_DP_VERSION_0_2)
283 version_one_zero = FALSE;
285 if (version_one_zero) {
286 if (!this->new_segment_buf || !this->caps_buf)
293 /* put copies of the buffers in a fixed list */
294 g_value_init (&array, GST_TYPE_ARRAY);
296 if (version_one_zero) {
297 new_segment_buf = gst_buffer_copy (this->new_segment_buf);
298 g_value_init (&value, GST_TYPE_BUFFER);
299 gst_value_set_buffer (&value, new_segment_buf);
300 gst_value_array_append_value (&array, &value);
301 g_value_unset (&value);
304 tag_buf = gst_buffer_copy (this->tag_buf);
305 g_value_init (&value, GST_TYPE_BUFFER);
306 gst_value_set_buffer (&value, tag_buf);
307 gst_value_array_append_value (&array, &value);
308 g_value_unset (&value);
312 caps_buf = gst_buffer_copy (this->caps_buf);
313 g_value_init (&value, GST_TYPE_BUFFER);
314 gst_value_set_buffer (&value, caps_buf);
315 gst_value_array_append_value (&array, &value);
316 g_value_unset (&value);
318 /* we also need to add GDP serializations of the streamheaders of the
320 structure = gst_caps_get_structure (this->caps, 0);
321 if (gst_structure_has_field (structure, "streamheader")) {
327 sh = gst_structure_get_value (structure, "streamheader");
328 buffers = g_value_peek_pointer (sh);
329 GST_DEBUG_OBJECT (this,
330 "Need to serialize %d incoming streamheader buffers on ours",
332 for (i = 0; i < buffers->len; ++i) {
334 GstBuffer *outbuffer;
336 bufval = &g_array_index (buffers, GValue, i);
337 buffer = g_value_peek_pointer (bufval);
338 outbuffer = gst_gdp_pay_buffer_from_buffer (this, buffer);
340 g_value_init (&value, GST_TYPE_BUFFER);
341 gst_value_set_buffer (&value, outbuffer);
342 gst_value_array_append_value (&array, &value);
343 g_value_unset (&value);
345 /* FIXME: if one or more in this loop fail to produce and outbuffer,
346 * should we error out ? Once ? Every time ? */
350 caps = gst_caps_from_string ("application/x-gdp");
351 structure = gst_caps_get_structure (caps, 0);
353 gst_structure_set_value (structure, "streamheader", &array);
354 g_value_unset (&array);
356 /* Unref our copies */
358 gst_buffer_unref (new_segment_buf);
359 gst_buffer_unref (caps_buf);
361 GST_DEBUG_OBJECT (this, "Setting caps on src pad %" GST_PTR_FORMAT, caps);
362 gst_pad_set_caps (this->srcpad, caps);
363 gst_buffer_set_caps (this->caps_buf, caps);
364 gst_buffer_set_caps (this->new_segment_buf, caps);
366 /* if these are our first ever buffers, send out new_segment first */
367 if (!this->sent_streamheader) {
369 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
370 GST_DEBUG_OBJECT (this, "Sending out new_segment event %p", event);
371 if (!gst_pad_push_event (this->srcpad, event)) {
372 GST_WARNING_OBJECT (this, "pushing new segment failed");
373 return GST_FLOW_ERROR;
377 /* push out these streamheader buffers, then flush our internal queue */
378 GST_DEBUG_OBJECT (this, "Pushing GDP new_segment buffer %p",
379 this->new_segment_buf);
380 /* we stored these bufs with refcount 1, so make sure we keep a ref */
381 r = gst_pad_push (this->srcpad, gst_buffer_ref (this->new_segment_buf));
382 if (r != GST_FLOW_OK) {
383 GST_WARNING_OBJECT (this, "pushing GDP newsegment buffer returned %d", r);
387 GST_DEBUG_OBJECT (this, "Pushing GDP tag buffer %p", this->tag_buf);
388 /* we stored these bufs with refcount 1, so make sure we keep a ref */
389 r = gst_pad_push (this->srcpad, gst_buffer_ref (this->tag_buf));
390 if (r != GST_FLOW_OK) {
391 GST_WARNING_OBJECT (this, "pushing GDP tag buffer returned %d", r);
395 GST_DEBUG_OBJECT (this, "Pushing GDP caps buffer %p", this->new_segment_buf);
396 r = gst_pad_push (this->srcpad, gst_buffer_ref (this->caps_buf));
397 if (r != GST_FLOW_OK) {
398 GST_WARNING_OBJECT (this, "pushing GDP caps buffer returned %d", r);
401 this->sent_streamheader = TRUE;
402 GST_DEBUG_OBJECT (this, "need to push %d queued buffers",
403 g_list_length (this->queue));
407 for (l = this->queue; l; l = g_list_next (l)) {
408 GST_DEBUG_OBJECT (this, "Pushing queued GDP buffer %p", l->data);
409 gst_buffer_set_caps (l->data, caps);
410 r = gst_pad_push (this->srcpad, l->data);
411 if (r != GST_FLOW_OK) {
412 GST_WARNING_OBJECT (this, "pushing queued GDP buffer returned %d", r);
421 /* queue a buffer internally if we haven't sent streamheader buffers yet;
422 * otherwise, just push on */
424 gst_gdp_queue_buffer (GstGDPPay * this, GstBuffer * buffer)
426 if (this->sent_streamheader) {
427 GST_LOG_OBJECT (this, "Pushing GDP buffer %p", buffer);
428 GST_LOG_OBJECT (this, "set caps %" GST_PTR_FORMAT, this->caps);
429 return gst_pad_push (this->srcpad, buffer);
432 /* store it on an internal queue */
433 this->queue = g_list_append (this->queue, buffer);
434 GST_DEBUG_OBJECT (this, "queued buffer %p, now %d buffers queued",
435 buffer, g_list_length (this->queue));
440 gst_gdp_pay_chain (GstPad * pad, GstBuffer * buffer)
444 GstBuffer *outbuffer;
447 this = GST_GDP_PAY (gst_pad_get_parent (pad));
449 /* we should have received a new_segment before, otherwise it's a bug.
450 * fake one in that case */
451 if (!this->new_segment_buf) {
454 GST_WARNING_OBJECT (this,
455 "did not receive new-segment before first buffer");
456 event = gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
457 outbuffer = gst_gdp_buffer_from_event (this, event);
458 gst_event_unref (event);
460 /* GDP 0.2 doesn't know about new-segment, so this is not fatal */
462 GST_ELEMENT_WARNING (this, STREAM, ENCODE, (NULL),
463 ("Could not create GDP buffer from new segment event"));
465 ret = GST_FLOW_ERROR;
470 gst_gdp_stamp_buffer (this, outbuffer);
471 GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
472 GST_BUFFER_DURATION (outbuffer) = 0;
473 GST_DEBUG_OBJECT (this, "Storing buffer %p as new_segment_buf",
475 this->new_segment_buf = outbuffer;
479 /* make sure we've received caps before */
480 caps = gst_buffer_get_caps (buffer);
481 if (!this->caps && !caps) {
482 GST_WARNING_OBJECT (this, "first received buffer does not have caps set");
484 gst_caps_unref (caps);
485 ret = GST_FLOW_NOT_NEGOTIATED;
489 /* if the caps have changed, process caps first */
490 if (caps && !gst_caps_is_equal (this->caps, caps)) {
491 GST_LOG_OBJECT (this, "caps changed to %p, %" GST_PTR_FORMAT, caps, caps);
492 gst_caps_replace (&(this->caps), caps);
493 outbuffer = gst_gdp_buffer_from_caps (this, caps);
495 GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
496 ("Could not create GDP buffer from caps %" GST_PTR_FORMAT, caps));
497 gst_caps_unref (caps);
498 ret = GST_FLOW_ERROR;
502 gst_gdp_stamp_buffer (this, outbuffer);
503 GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
504 GST_BUFFER_DURATION (outbuffer) = 0;
505 GST_BUFFER_FLAG_SET (outbuffer, GST_BUFFER_FLAG_IN_CAPS);
506 this->caps_buf = outbuffer;
507 gst_gdp_pay_reset_streamheader (this);
510 /* create a GDP header packet,
511 * then create a GST buffer of the header packet and the buffer contents */
512 outbuffer = gst_gdp_pay_buffer_from_buffer (this, buffer);
514 GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
515 ("Could not create GDP buffer from buffer"));
516 ret = GST_FLOW_ERROR;
520 gst_gdp_stamp_buffer (this, outbuffer);
521 GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
522 GST_BUFFER_DURATION (outbuffer) = GST_BUFFER_DURATION (buffer);
524 ret = gst_gdp_queue_buffer (this, outbuffer);
527 gst_buffer_unref (buffer);
528 gst_object_unref (this);
533 gst_gdp_pay_sink_event (GstPad * pad, GstEvent * event)
535 GstBuffer *outbuffer;
536 GstGDPPay *this = GST_GDP_PAY (gst_pad_get_parent (pad));
537 GstFlowReturn flowret;
540 GST_DEBUG_OBJECT (this, "received event %p of type %s (%d)",
541 event, gst_event_type_get_name (event->type), event->type);
543 /* now turn the event into a buffer */
544 outbuffer = gst_gdp_buffer_from_event (this, event);
546 GST_ELEMENT_WARNING (this, STREAM, ENCODE, (NULL),
547 ("Could not create GDP buffer from received event (type %s)",
548 gst_event_type_get_name (event->type)));
552 gst_gdp_stamp_buffer (this, outbuffer);
553 GST_BUFFER_TIMESTAMP (outbuffer) = GST_EVENT_TIMESTAMP (event);
554 GST_BUFFER_DURATION (outbuffer) = 0;
556 /* if we got a new segment, we should put it on our streamheader,
557 * and not send it on */
558 switch (GST_EVENT_TYPE (event)) {
559 case GST_EVENT_NEWSEGMENT:
560 GST_DEBUG_OBJECT (this, "received new_segment event");
561 if (this->new_segment_buf) {
562 gst_buffer_unref (this->new_segment_buf);
564 GST_DEBUG_OBJECT (this, "Storing buffer %p as new_segment_buf",
566 this->new_segment_buf = outbuffer;
567 gst_gdp_pay_reset_streamheader (this);
570 GST_DEBUG_OBJECT (this, "received tag event");
572 gst_buffer_unref (this->tag_buf);
574 GST_DEBUG_OBJECT (this, "Storing buffer %p as tag_buf", outbuffer);
575 this->tag_buf = outbuffer;
576 gst_gdp_pay_reset_streamheader (this);
579 GST_DEBUG_OBJECT (this, "queuing GDP buffer %p of event %p", outbuffer,
581 flowret = gst_gdp_queue_buffer (this, outbuffer);
582 if (flowret != GST_FLOW_OK) {
583 GST_WARNING_OBJECT (this, "queueing GDP event buffer returned %d",
591 /* if we have EOS, we should send on EOS ourselves */
592 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
593 GST_DEBUG_OBJECT (this, "Sending on EOS event %p", event);
594 return gst_pad_push_event (this->srcpad, event);
598 gst_object_unref (this);
599 gst_event_unref (event);
604 gst_gdp_pay_set_property (GObject * object, guint prop_id,
605 const GValue * value, GParamSpec * pspec)
609 g_return_if_fail (GST_IS_GDP_PAY (object));
610 this = GST_GDP_PAY (object);
613 case PROP_CRC_HEADER:
615 g_value_get_boolean (value) ? GST_DP_HEADER_FLAG_CRC_HEADER : 0;
616 this->header_flag = this->crc_header | this->crc_payload;
618 case PROP_CRC_PAYLOAD:
620 g_value_get_boolean (value) ? GST_DP_HEADER_FLAG_CRC_PAYLOAD : 0;
621 this->header_flag = this->crc_header | this->crc_payload;
624 this->version = g_value_get_enum (value);
627 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
633 gst_gdp_pay_get_property (GObject * object, guint prop_id,
634 GValue * value, GParamSpec * pspec)
638 g_return_if_fail (GST_IS_GDP_PAY (object));
639 this = GST_GDP_PAY (object);
642 case PROP_CRC_HEADER:
643 g_value_set_boolean (value, this->crc_header);
645 case PROP_CRC_PAYLOAD:
646 g_value_set_boolean (value, this->crc_payload);
649 g_value_set_enum (value, this->version);
652 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
657 static GstStateChangeReturn
658 gst_gdp_pay_change_state (GstElement * element, GstStateChange transition)
660 GstStateChangeReturn ret;
661 GstGDPPay *this = GST_GDP_PAY (element);
663 switch (transition) {
664 case GST_STATE_CHANGE_READY_TO_PAUSED:
665 this->packetizer = gst_dp_packetizer_new (this->version);
671 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
673 switch (transition) {
674 case GST_STATE_CHANGE_PAUSED_TO_READY:
675 if (this->packetizer) {
676 gst_dp_packetizer_free (this->packetizer);
677 this->packetizer = NULL;
680 case GST_STATE_CHANGE_READY_TO_NULL:
682 gst_caps_unref (this->caps);
694 gst_gdp_pay_plugin_init (GstPlugin * plugin)
696 if (!gst_element_register (plugin, "gdppay", GST_RANK_NONE, GST_TYPE_GDP_PAY))