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.
31 * gst-launch -v -m videotestsrc num-buffers=50 ! gdppay ! filesink location=test.gdp
33 * This pipeline creates a serialized video stream that can be played back
34 * with the example shown in gdpdepay.
43 #include <gst/dataprotocol/dataprotocol.h>
45 #include "gstgdppay.h"
47 /* elementfactory information */
48 static const GstElementDetails gdp_pay_details =
49 GST_ELEMENT_DETAILS ("GDP Payloader",
51 "Payloads GStreamer Data Protocol buffers",
52 "Thomas Vander Stichele <thomas at apestaart dot org>");
54 static GstStaticPadTemplate gdp_pay_sink_template =
55 GST_STATIC_PAD_TEMPLATE ("sink",
60 static GstStaticPadTemplate gdp_pay_src_template =
61 GST_STATIC_PAD_TEMPLATE ("src",
64 GST_STATIC_CAPS ("application/x-gdp"));
66 GST_DEBUG_CATEGORY_STATIC (gst_gdp_pay_debug);
67 #define GST_CAT_DEFAULT gst_gdp_pay_debug
69 #define DEFAULT_CRC_HEADER TRUE
70 #define DEFAULT_CRC_PAYLOAD FALSE
71 #define DEFAULT_VERSION GST_DP_VERSION_1_0
82 GST_DEBUG_CATEGORY_INIT (gst_gdp_pay_debug, "gdppay", 0, \
85 GST_BOILERPLATE_FULL (GstGDPPay, gst_gdp_pay, GstElement,
86 GST_TYPE_ELEMENT, _do_init);
88 static GstFlowReturn gst_gdp_pay_chain (GstPad * pad, GstBuffer * buffer);
89 static gboolean gst_gdp_pay_sink_event (GstPad * pad, GstEvent * event);
90 static GstStateChangeReturn gst_gdp_pay_change_state (GstElement *
91 element, GstStateChange transition);
93 static void gst_gdp_pay_set_property (GObject * object, guint prop_id,
94 const GValue * value, GParamSpec * pspec);
95 static void gst_gdp_pay_get_property (GObject * object, guint prop_id,
96 GValue * value, GParamSpec * pspec);
98 static void gst_gdp_pay_dispose (GObject * gobject);
101 gst_gdp_pay_base_init (gpointer g_class)
103 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
105 gst_element_class_set_details (element_class, &gdp_pay_details);
107 gst_element_class_add_pad_template (element_class,
108 gst_static_pad_template_get (&gdp_pay_sink_template));
109 gst_element_class_add_pad_template (element_class,
110 gst_static_pad_template_get (&gdp_pay_src_template));
114 gst_gdp_pay_class_init (GstGDPPayClass * klass)
116 GObjectClass *gobject_class;
117 GstElementClass *gstelement_class;
119 gobject_class = (GObjectClass *) klass;
120 gstelement_class = (GstElementClass *) klass;
122 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_gdp_pay_set_property);
123 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_gdp_pay_get_property);
124 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_gdp_pay_dispose);
125 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_gdp_pay_change_state);
127 g_object_class_install_property (gobject_class, PROP_CRC_HEADER,
128 g_param_spec_boolean ("crc-header", "CRC Header",
129 "Calculate and store a CRC checksum on the header",
130 DEFAULT_CRC_HEADER, G_PARAM_READWRITE));
131 g_object_class_install_property (gobject_class, PROP_CRC_PAYLOAD,
132 g_param_spec_boolean ("crc-payload", "CRC Payload",
133 "Calculate and store a CRC checksum on the payload",
134 DEFAULT_CRC_PAYLOAD, G_PARAM_READWRITE));
135 g_object_class_install_property (gobject_class, PROP_VERSION,
136 g_param_spec_enum ("version", "Version",
137 "Version of the GStreamer Data Protocol",
138 GST_TYPE_DP_VERSION, DEFAULT_VERSION, G_PARAM_READWRITE));
142 gst_gdp_pay_init (GstGDPPay * gdppay, GstGDPPayClass * g_class)
145 gst_pad_new_from_static_template (&gdp_pay_sink_template, "sink");
146 gst_pad_set_chain_function (gdppay->sinkpad,
147 GST_DEBUG_FUNCPTR (gst_gdp_pay_chain));
148 gst_pad_set_event_function (gdppay->sinkpad,
149 GST_DEBUG_FUNCPTR (gst_gdp_pay_sink_event));
150 gst_element_add_pad (GST_ELEMENT (gdppay), gdppay->sinkpad);
153 gst_pad_new_from_static_template (&gdp_pay_src_template, "src");
154 gst_element_add_pad (GST_ELEMENT (gdppay), gdppay->srcpad);
158 gdppay->crc_header = DEFAULT_CRC_HEADER;
159 gdppay->crc_payload = DEFAULT_CRC_PAYLOAD;
160 gdppay->header_flag = gdppay->crc_header | gdppay->crc_payload;
161 gdppay->version = DEFAULT_VERSION;
165 gst_gdp_pay_dispose (GObject * gobject)
167 GstGDPPay *this = GST_GDP_PAY (gobject);
169 if (this->caps_buf) {
170 gst_buffer_unref (this->caps_buf);
171 this->caps_buf = NULL;
173 if (this->new_segment_buf) {
174 gst_buffer_unref (this->new_segment_buf);
175 this->new_segment_buf = NULL;
177 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (gobject));
180 /* set OFFSET and OFFSET_END with running count */
182 gst_gdp_stamp_buffer (GstGDPPay * this, GstBuffer * buffer)
184 GST_BUFFER_OFFSET (buffer) = this->offset;
185 GST_BUFFER_OFFSET_END (buffer) = this->offset + GST_BUFFER_SIZE (buffer);
186 this->offset = GST_BUFFER_OFFSET_END (buffer);
190 gst_gdp_buffer_from_caps (GstGDPPay * this, GstCaps * caps)
192 GstBuffer *headerbuf;
193 GstBuffer *payloadbuf;
194 guint8 *header, *payload;
197 if (!this->packetizer->packet_from_caps (caps, this->header_flag, &len,
198 &header, &payload)) {
199 GST_WARNING_OBJECT (this, "could not create GDP header from caps");
203 GST_LOG_OBJECT (this, "creating GDP header and payload buffer from caps");
204 headerbuf = gst_buffer_new ();
205 gst_buffer_set_data (headerbuf, header, len);
206 GST_BUFFER_MALLOCDATA (headerbuf) = header;
208 payloadbuf = gst_buffer_new ();
209 gst_buffer_set_data (payloadbuf, payload,
210 gst_dp_header_payload_length (header));
211 GST_BUFFER_MALLOCDATA (payloadbuf) = payload;
213 return gst_buffer_join (headerbuf, payloadbuf);
217 gst_gdp_pay_buffer_from_buffer (GstGDPPay * this, GstBuffer * buffer)
219 GstBuffer *headerbuf;
223 if (!this->packetizer->header_from_buffer (buffer, this->header_flag, &len,
225 GST_WARNING_OBJECT (this, "could not create GDP header from buffer");
229 GST_LOG_OBJECT (this, "creating GDP header and payload buffer from buffer");
230 headerbuf = gst_buffer_new ();
231 gst_buffer_set_data (headerbuf, header, len);
232 GST_BUFFER_MALLOCDATA (headerbuf) = header;
234 /* we do not want to lose the ref on the incoming buffer */
235 gst_buffer_ref (buffer);
236 return gst_buffer_join (headerbuf, buffer);
240 gst_gdp_buffer_from_event (GstGDPPay * this, GstEvent * event)
242 GstBuffer *headerbuf;
243 GstBuffer *payloadbuf;
244 guint8 *header, *payload;
249 this->packetizer->packet_from_event (event, this->header_flag, &len,
253 GST_WARNING_OBJECT (this, "could not create GDP header from event %s (%d)",
254 gst_event_type_get_name (event->type), event->type);
258 GST_LOG_OBJECT (this, "creating GDP header and payload buffer from event");
259 headerbuf = gst_buffer_new ();
260 gst_buffer_set_data (headerbuf, header, len);
261 GST_BUFFER_MALLOCDATA (headerbuf) = header;
263 payloadbuf = gst_buffer_new ();
264 gst_buffer_set_data (payloadbuf, payload,
265 gst_dp_header_payload_length (header));
266 GST_BUFFER_MALLOCDATA (payloadbuf) = payload;
268 return gst_buffer_join (headerbuf, payloadbuf);
272 /* set our caps with streamheader, based on the latest newsegment and caps,
273 * and (possibly) GDP-serialized buffers of the streamheaders on the src pad */
275 gst_gdp_pay_reset_streamheader (GstGDPPay * this)
278 GstStructure *structure;
279 GstBuffer *new_segment_buf = NULL, *caps_buf, *tag_buf = NULL;
280 GstFlowReturn r = GST_FLOW_OK;
281 gboolean version_one_zero = TRUE;
283 GValue array = { 0 };
284 GValue value = { 0 };
286 /* In version 0.2, we didn't need or send new segment or tags */
287 if (this->version == GST_DP_VERSION_0_2)
288 version_one_zero = FALSE;
290 if (version_one_zero) {
291 if (!this->new_segment_buf || !this->caps_buf)
298 /* put copies of the buffers in a fixed list */
299 g_value_init (&array, GST_TYPE_ARRAY);
301 if (version_one_zero) {
302 new_segment_buf = gst_buffer_copy (this->new_segment_buf);
303 g_value_init (&value, GST_TYPE_BUFFER);
304 gst_value_set_buffer (&value, new_segment_buf);
305 gst_value_array_append_value (&array, &value);
306 g_value_unset (&value);
309 tag_buf = gst_buffer_copy (this->tag_buf);
310 g_value_init (&value, GST_TYPE_BUFFER);
311 gst_value_set_buffer (&value, tag_buf);
312 gst_value_array_append_value (&array, &value);
313 g_value_unset (&value);
317 caps_buf = gst_buffer_copy (this->caps_buf);
318 g_value_init (&value, GST_TYPE_BUFFER);
319 gst_value_set_buffer (&value, caps_buf);
320 gst_value_array_append_value (&array, &value);
321 g_value_unset (&value);
323 /* we also need to add GDP serializations of the streamheaders of the
325 structure = gst_caps_get_structure (this->caps, 0);
326 if (gst_structure_has_field (structure, "streamheader")) {
332 sh = gst_structure_get_value (structure, "streamheader");
333 buffers = g_value_peek_pointer (sh);
334 GST_DEBUG_OBJECT (this,
335 "Need to serialize %d incoming streamheader buffers on ours",
337 for (i = 0; i < buffers->len; ++i) {
339 GstBuffer *outbuffer;
341 bufval = &g_array_index (buffers, GValue, i);
342 buffer = g_value_peek_pointer (bufval);
343 outbuffer = gst_gdp_pay_buffer_from_buffer (this, buffer);
345 g_value_init (&value, GST_TYPE_BUFFER);
346 gst_value_set_buffer (&value, outbuffer);
347 gst_value_array_append_value (&array, &value);
348 g_value_unset (&value);
350 /* FIXME: if one or more in this loop fail to produce an outbuffer,
351 * should we error out ? Once ? Every time ? */
355 caps = gst_caps_from_string ("application/x-gdp");
356 structure = gst_caps_get_structure (caps, 0);
358 gst_structure_set_value (structure, "streamheader", &array);
359 g_value_unset (&array);
361 /* Unref our copies */
363 gst_buffer_unref (new_segment_buf);
364 gst_buffer_unref (caps_buf);
366 GST_DEBUG_OBJECT (this, "Setting caps on src pad %" GST_PTR_FORMAT, caps);
367 gst_pad_set_caps (this->srcpad, caps);
368 gst_buffer_set_caps (this->caps_buf, caps);
369 gst_buffer_set_caps (this->new_segment_buf, caps);
371 /* if these are our first ever buffers, send out new_segment first */
372 if (!this->sent_streamheader) {
374 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
375 GST_DEBUG_OBJECT (this, "Sending out new_segment event %p", event);
376 if (!gst_pad_push_event (this->srcpad, event)) {
377 GST_WARNING_OBJECT (this, "pushing new segment failed");
378 return GST_FLOW_ERROR;
382 /* push out these streamheader buffers, then flush our internal queue */
383 GST_DEBUG_OBJECT (this, "Pushing GDP new_segment buffer %p",
384 this->new_segment_buf);
385 /* we stored these bufs with refcount 1, so make sure we keep a ref */
386 r = gst_pad_push (this->srcpad, gst_buffer_ref (this->new_segment_buf));
387 if (r != GST_FLOW_OK) {
388 GST_WARNING_OBJECT (this, "pushing GDP newsegment buffer returned %d", r);
392 GST_DEBUG_OBJECT (this, "Pushing GDP tag buffer %p", this->tag_buf);
393 /* we stored these bufs with refcount 1, so make sure we keep a ref */
394 r = gst_pad_push (this->srcpad, gst_buffer_ref (this->tag_buf));
395 if (r != GST_FLOW_OK) {
396 GST_WARNING_OBJECT (this, "pushing GDP tag buffer returned %d", r);
400 GST_DEBUG_OBJECT (this, "Pushing GDP caps buffer %p", this->new_segment_buf);
401 r = gst_pad_push (this->srcpad, gst_buffer_ref (this->caps_buf));
402 if (r != GST_FLOW_OK) {
403 GST_WARNING_OBJECT (this, "pushing GDP caps buffer returned %d", r);
406 this->sent_streamheader = TRUE;
407 GST_DEBUG_OBJECT (this, "need to push %d queued buffers",
408 g_list_length (this->queue));
412 for (l = this->queue; l; l = g_list_next (l)) {
413 GST_DEBUG_OBJECT (this, "Pushing queued GDP buffer %p", l->data);
414 gst_buffer_set_caps (l->data, caps);
415 r = gst_pad_push (this->srcpad, l->data);
416 if (r != GST_FLOW_OK) {
417 GST_WARNING_OBJECT (this, "pushing queued GDP buffer returned %d", r);
426 /* queue a buffer internally if we haven't sent streamheader buffers yet;
427 * otherwise, just push on */
429 gst_gdp_queue_buffer (GstGDPPay * this, GstBuffer * buffer)
431 if (this->sent_streamheader) {
432 GST_LOG_OBJECT (this, "Pushing GDP buffer %p", buffer);
433 GST_LOG_OBJECT (this, "set caps %" GST_PTR_FORMAT, this->caps);
434 return gst_pad_push (this->srcpad, buffer);
437 /* store it on an internal queue */
438 this->queue = g_list_append (this->queue, buffer);
439 GST_DEBUG_OBJECT (this, "queued buffer %p, now %d buffers queued",
440 buffer, g_list_length (this->queue));
445 gst_gdp_pay_chain (GstPad * pad, GstBuffer * buffer)
449 GstBuffer *outbuffer;
452 this = GST_GDP_PAY (gst_pad_get_parent (pad));
454 /* we should have received a new_segment before, otherwise it's a bug.
455 * fake one in that case */
456 if (!this->new_segment_buf) {
459 GST_WARNING_OBJECT (this,
460 "did not receive new-segment before first buffer");
461 event = gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
462 outbuffer = gst_gdp_buffer_from_event (this, event);
463 gst_event_unref (event);
465 /* GDP 0.2 doesn't know about new-segment, so this is not fatal */
467 GST_ELEMENT_WARNING (this, STREAM, ENCODE, (NULL),
468 ("Could not create GDP buffer from new segment event"));
470 ret = GST_FLOW_ERROR;
475 gst_gdp_stamp_buffer (this, outbuffer);
476 GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
477 GST_BUFFER_DURATION (outbuffer) = 0;
478 GST_DEBUG_OBJECT (this, "Storing buffer %p as new_segment_buf",
480 this->new_segment_buf = outbuffer;
484 /* make sure we've received caps before */
485 caps = gst_buffer_get_caps (buffer);
486 if (!this->caps && !caps) {
487 GST_WARNING_OBJECT (this, "first received buffer does not have caps set");
489 gst_caps_unref (caps);
490 ret = GST_FLOW_NOT_NEGOTIATED;
494 /* if the caps have changed, process caps first */
495 if (caps && !gst_caps_is_equal (this->caps, caps)) {
496 GST_LOG_OBJECT (this, "caps changed to %p, %" GST_PTR_FORMAT, caps, caps);
497 gst_caps_replace (&(this->caps), caps);
498 outbuffer = gst_gdp_buffer_from_caps (this, caps);
500 GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
501 ("Could not create GDP buffer from caps %" GST_PTR_FORMAT, caps));
502 gst_caps_unref (caps);
503 ret = GST_FLOW_ERROR;
507 gst_gdp_stamp_buffer (this, outbuffer);
508 GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
509 GST_BUFFER_DURATION (outbuffer) = 0;
510 GST_BUFFER_FLAG_SET (outbuffer, GST_BUFFER_FLAG_IN_CAPS);
511 this->caps_buf = outbuffer;
512 gst_gdp_pay_reset_streamheader (this);
515 /* create a GDP header packet,
516 * then create a GST buffer of the header packet and the buffer contents */
517 outbuffer = gst_gdp_pay_buffer_from_buffer (this, buffer);
519 GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
520 ("Could not create GDP buffer from buffer"));
521 ret = GST_FLOW_ERROR;
525 gst_gdp_stamp_buffer (this, outbuffer);
526 GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
527 GST_BUFFER_DURATION (outbuffer) = GST_BUFFER_DURATION (buffer);
529 ret = gst_gdp_queue_buffer (this, outbuffer);
532 gst_buffer_unref (buffer);
533 gst_object_unref (this);
538 gst_gdp_pay_sink_event (GstPad * pad, GstEvent * event)
540 GstBuffer *outbuffer;
541 GstGDPPay *this = GST_GDP_PAY (gst_pad_get_parent (pad));
542 GstFlowReturn flowret;
545 GST_DEBUG_OBJECT (this, "received event %p of type %s (%d)",
546 event, gst_event_type_get_name (event->type), event->type);
548 /* now turn the event into a buffer */
549 outbuffer = gst_gdp_buffer_from_event (this, event);
551 GST_ELEMENT_WARNING (this, STREAM, ENCODE, (NULL),
552 ("Could not create GDP buffer from received event (type %s)",
553 gst_event_type_get_name (event->type)));
557 gst_gdp_stamp_buffer (this, outbuffer);
558 GST_BUFFER_TIMESTAMP (outbuffer) = GST_EVENT_TIMESTAMP (event);
559 GST_BUFFER_DURATION (outbuffer) = 0;
561 /* if we got a new segment, we should put it on our streamheader,
562 * and not send it on */
563 switch (GST_EVENT_TYPE (event)) {
564 case GST_EVENT_NEWSEGMENT:
565 GST_DEBUG_OBJECT (this, "received new_segment event");
566 if (this->new_segment_buf) {
567 gst_buffer_unref (this->new_segment_buf);
569 GST_DEBUG_OBJECT (this, "Storing buffer %p as new_segment_buf",
571 this->new_segment_buf = outbuffer;
572 gst_gdp_pay_reset_streamheader (this);
575 GST_DEBUG_OBJECT (this, "received tag event");
577 gst_buffer_unref (this->tag_buf);
579 GST_DEBUG_OBJECT (this, "Storing buffer %p as tag_buf", outbuffer);
580 this->tag_buf = outbuffer;
581 gst_gdp_pay_reset_streamheader (this);
584 GST_DEBUG_OBJECT (this, "queuing GDP buffer %p of event %p", outbuffer,
586 flowret = gst_gdp_queue_buffer (this, outbuffer);
587 if (flowret != GST_FLOW_OK) {
588 GST_WARNING_OBJECT (this, "queueing GDP event buffer returned %d",
596 /* if we have EOS, we should send on EOS ourselves */
597 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
598 GST_DEBUG_OBJECT (this, "Sending on EOS event %p", event);
599 return gst_pad_push_event (this->srcpad, event);
603 gst_object_unref (this);
604 gst_event_unref (event);
609 gst_gdp_pay_set_property (GObject * object, guint prop_id,
610 const GValue * value, GParamSpec * pspec)
614 g_return_if_fail (GST_IS_GDP_PAY (object));
615 this = GST_GDP_PAY (object);
618 case PROP_CRC_HEADER:
620 g_value_get_boolean (value) ? GST_DP_HEADER_FLAG_CRC_HEADER : 0;
621 this->header_flag = this->crc_header | this->crc_payload;
623 case PROP_CRC_PAYLOAD:
625 g_value_get_boolean (value) ? GST_DP_HEADER_FLAG_CRC_PAYLOAD : 0;
626 this->header_flag = this->crc_header | this->crc_payload;
629 this->version = g_value_get_enum (value);
632 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
638 gst_gdp_pay_get_property (GObject * object, guint prop_id,
639 GValue * value, GParamSpec * pspec)
643 g_return_if_fail (GST_IS_GDP_PAY (object));
644 this = GST_GDP_PAY (object);
647 case PROP_CRC_HEADER:
648 g_value_set_boolean (value, this->crc_header);
650 case PROP_CRC_PAYLOAD:
651 g_value_set_boolean (value, this->crc_payload);
654 g_value_set_enum (value, this->version);
657 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
662 static GstStateChangeReturn
663 gst_gdp_pay_change_state (GstElement * element, GstStateChange transition)
665 GstStateChangeReturn ret;
666 GstGDPPay *this = GST_GDP_PAY (element);
668 switch (transition) {
669 case GST_STATE_CHANGE_READY_TO_PAUSED:
670 this->packetizer = gst_dp_packetizer_new (this->version);
676 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
678 switch (transition) {
679 case GST_STATE_CHANGE_PAUSED_TO_READY:
680 if (this->packetizer) {
681 gst_dp_packetizer_free (this->packetizer);
682 this->packetizer = NULL;
685 case GST_STATE_CHANGE_READY_TO_NULL:
687 gst_caps_unref (this->caps);
699 gst_gdp_pay_plugin_init (GstPlugin * plugin)
701 if (!gst_element_register (plugin, "gdppay", GST_RANK_NONE, GST_TYPE_GDP_PAY))