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 (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
73 GST_DEBUG_CATEGORY_INIT (gst_gdp_pay_debug, "gdppay", 0, \
76 GST_BOILERPLATE_FULL (GstGDPPay, gst_gdp_pay, GstElement,
77 GST_TYPE_ELEMENT, _do_init);
79 static GstFlowReturn gst_gdp_pay_chain (GstPad * pad, GstBuffer * buffer);
80 static gboolean gst_gdp_pay_sink_event (GstPad * pad, GstEvent * event);
81 static GstStateChangeReturn gst_gdp_pay_change_state (GstElement *
82 element, GstStateChange transition);
84 static void gst_gdp_pay_set_property (GObject * object, guint prop_id,
85 const GValue * value, GParamSpec * pspec);
86 static void gst_gdp_pay_get_property (GObject * object, guint prop_id,
87 GValue * value, GParamSpec * pspec);
89 static void gst_gdp_pay_dispose (GObject * gobject);
92 gst_gdp_pay_base_init (gpointer g_class)
94 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
96 gst_element_class_set_details (element_class, &gdp_pay_details);
98 gst_element_class_add_pad_template (element_class,
99 gst_static_pad_template_get (&gdp_pay_sink_template));
100 gst_element_class_add_pad_template (element_class,
101 gst_static_pad_template_get (&gdp_pay_src_template));
105 gst_gdp_pay_class_init (GstGDPPayClass * klass)
107 GObjectClass *gobject_class;
108 GstElementClass *gstelement_class;
110 gobject_class = (GObjectClass *) klass;
111 gstelement_class = (GstElementClass *) klass;
113 parent_class = g_type_class_peek_parent (klass);
115 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_gdp_pay_set_property);
116 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_gdp_pay_get_property);
117 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_gdp_pay_dispose);
118 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_gdp_pay_change_state);
120 g_object_class_install_property (gobject_class, PROP_CRC_HEADER,
121 g_param_spec_boolean ("crc-header", "CRC Header",
122 "Calculate and store a CRC checksum on the header",
123 DEFAULT_CRC_HEADER, G_PARAM_READWRITE));
124 g_object_class_install_property (gobject_class, PROP_CRC_PAYLOAD,
125 g_param_spec_boolean ("crc-payload", "CRC Payload",
126 "Calculate and store a CRC checksum on the payload",
127 DEFAULT_CRC_PAYLOAD, G_PARAM_READWRITE));
131 gst_gdp_pay_init (GstGDPPay * gdppay, GstGDPPayClass * g_class)
134 gst_pad_new_from_static_template (&gdp_pay_sink_template, "sink");
135 gst_pad_set_chain_function (gdppay->sinkpad,
136 GST_DEBUG_FUNCPTR (gst_gdp_pay_chain));
137 gst_pad_set_event_function (gdppay->sinkpad,
138 GST_DEBUG_FUNCPTR (gst_gdp_pay_sink_event));
139 gst_element_add_pad (GST_ELEMENT (gdppay), gdppay->sinkpad);
142 gst_pad_new_from_static_template (&gdp_pay_src_template, "src");
143 gst_element_add_pad (GST_ELEMENT (gdppay), gdppay->srcpad);
147 gdppay->crc_header = DEFAULT_CRC_HEADER;
148 gdppay->crc_payload = DEFAULT_CRC_PAYLOAD;
149 gdppay->header_flag = gdppay->crc_header | gdppay->crc_payload;
153 gst_gdp_pay_dispose (GObject * gobject)
155 GstGDPPay *this = GST_GDP_PAY (gobject);
157 if (this->caps_buf) {
158 gst_buffer_unref (this->caps_buf);
159 this->caps_buf = NULL;
161 if (this->new_segment_buf) {
162 gst_buffer_unref (this->new_segment_buf);
163 this->new_segment_buf = NULL;
165 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (gobject));
168 /* set OFFSET and OFFSET_END with running count */
170 gst_gdp_stamp_buffer (GstGDPPay * this, GstBuffer * buffer)
172 GST_BUFFER_OFFSET (buffer) = this->offset;
173 GST_BUFFER_OFFSET_END (buffer) = this->offset + GST_BUFFER_SIZE (buffer);
174 this->offset = GST_BUFFER_OFFSET_END (buffer);
178 gst_gdp_buffer_from_caps (GstGDPPay * this, GstCaps * caps)
180 GstBuffer *headerbuf;
181 GstBuffer *payloadbuf;
182 guint8 *header, *payload;
185 if (!gst_dp_packet_from_caps (caps, this->header_flag, &len, &header,
187 GST_WARNING_OBJECT (this, "could not create GDP header from caps");
191 GST_LOG_OBJECT (this, "creating GDP header and payload buffer from caps");
192 headerbuf = gst_buffer_new ();
193 gst_buffer_set_data (headerbuf, header, len);
194 GST_BUFFER_MALLOCDATA (headerbuf) = header;
196 payloadbuf = gst_buffer_new ();
197 gst_buffer_set_data (payloadbuf, payload,
198 gst_dp_header_payload_length (header));
199 GST_BUFFER_MALLOCDATA (payloadbuf) = payload;
201 return gst_buffer_join (headerbuf, payloadbuf);
205 gst_gdp_pay_buffer_from_buffer (GstGDPPay * this, GstBuffer * buffer)
207 GstBuffer *headerbuf;
211 if (!gst_dp_header_from_buffer (buffer, this->header_flag, &len, &header)) {
212 GST_WARNING_OBJECT (this, "could not create GDP header from buffer");
216 GST_LOG_OBJECT (this, "creating GDP header and payload buffer from buffer");
217 headerbuf = gst_buffer_new ();
218 gst_buffer_set_data (headerbuf, header, len);
219 GST_BUFFER_MALLOCDATA (headerbuf) = header;
221 /* we do not want to lose the ref on the incoming buffer */
222 gst_buffer_ref (buffer);
223 return gst_buffer_join (headerbuf, buffer);
227 gst_gdp_buffer_from_event (GstGDPPay * this, GstEvent * event)
229 GstBuffer *headerbuf;
230 GstBuffer *payloadbuf;
231 guint8 *header, *payload;
234 if (!gst_dp_packet_from_event (event, this->header_flag, &len, &header,
236 GST_WARNING_OBJECT (this, "could not create GDP header from event %s (%d)",
237 gst_event_type_get_name (event->type), event->type);
241 GST_LOG_OBJECT (this, "creating GDP header and payload buffer from event");
242 headerbuf = gst_buffer_new ();
243 gst_buffer_set_data (headerbuf, header, len);
244 GST_BUFFER_MALLOCDATA (headerbuf) = header;
246 payloadbuf = gst_buffer_new ();
247 gst_buffer_set_data (payloadbuf, payload,
248 gst_dp_header_payload_length (header));
249 GST_BUFFER_MALLOCDATA (payloadbuf) = payload;
251 return gst_buffer_join (headerbuf, payloadbuf);
255 /* set our caps with streamheader, based on the latest newsegment and caps,
256 * and (possibly) GDP-serialized buffers of the streamheaders on the src pad */
258 gst_gdp_pay_reset_streamheader (GstGDPPay * this)
261 GstStructure *structure;
262 GstBuffer *new_segment_buf, *caps_buf;
263 GstFlowReturn r = GST_FLOW_OK;
265 GValue array = { 0 };
266 GValue value = { 0 };
268 /* we need both new segment and caps before we can set streamheader */
269 if (!this->new_segment_buf || !this->caps_buf)
272 /* we copy to avoid circular refcounts */
273 new_segment_buf = gst_buffer_copy (this->new_segment_buf);
274 caps_buf = gst_buffer_copy (this->caps_buf);
276 /* put copies of the buffers in a fixed list */
277 g_value_init (&array, GST_TYPE_ARRAY);
279 g_value_init (&value, GST_TYPE_BUFFER);
280 gst_value_set_buffer (&value, new_segment_buf);
281 gst_value_array_append_value (&array, &value);
282 g_value_unset (&value);
284 g_value_init (&value, GST_TYPE_BUFFER);
285 gst_value_set_buffer (&value, caps_buf);
286 gst_value_array_append_value (&array, &value);
287 g_value_unset (&value);
289 /* we also need to add GDP serializations of the streamheaders of the
291 structure = gst_caps_get_structure (this->caps, 0);
292 if (gst_structure_has_field (structure, "streamheader")) {
298 sh = gst_structure_get_value (structure, "streamheader");
299 buffers = g_value_peek_pointer (sh);
300 GST_DEBUG_OBJECT (this,
301 "Need to serialize %d incoming streamheader buffers on ours",
303 for (i = 0; i < buffers->len; ++i) {
305 GstBuffer *outbuffer;
307 bufval = &g_array_index (buffers, GValue, i);
308 buffer = g_value_peek_pointer (bufval);
309 outbuffer = gst_gdp_pay_buffer_from_buffer (this, buffer);
311 g_value_init (&value, GST_TYPE_BUFFER);
312 gst_value_set_buffer (&value, outbuffer);
313 gst_value_array_append_value (&array, &value);
314 g_value_unset (&value);
316 /* FIXME: if one or more in this loop fail to produce and outbuffer,
317 * should we error out ? Once ? Every time ? */
321 caps = gst_caps_from_string ("application/x-gdp");
322 structure = gst_caps_get_structure (caps, 0);
324 gst_structure_set_value (structure, "streamheader", &array);
325 g_value_unset (&array);
327 /* Unref our copies */
328 gst_buffer_unref (new_segment_buf);
329 gst_buffer_unref (caps_buf);
331 GST_DEBUG_OBJECT (this, "Setting caps on src pad %" GST_PTR_FORMAT, caps);
332 gst_pad_set_caps (this->srcpad, caps);
333 gst_buffer_set_caps (this->caps_buf, caps);
334 gst_buffer_set_caps (this->new_segment_buf, caps);
336 /* if these are our first ever buffers, send out new_segment first */
337 if (!this->sent_streamheader) {
339 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
340 GST_DEBUG_OBJECT (this, "Sending out new_segment event %p", event);
341 if (!gst_pad_push_event (this->srcpad, event)) {
342 GST_WARNING_OBJECT (this, "pushing new segment failed");
343 return GST_FLOW_ERROR;
347 /* push out these streamheader buffers, then flush our internal queue */
348 GST_DEBUG_OBJECT (this, "Pushing GDP new_segment buffer %p",
349 this->new_segment_buf);
350 /* we stored these bufs with refcount 1, so make sure we keep a ref */
351 r = gst_pad_push (this->srcpad, gst_buffer_ref (this->new_segment_buf));
352 if (r != GST_FLOW_OK) {
353 GST_WARNING_OBJECT (this, "pushing GDP newsegment buffer returned %d", r);
356 GST_DEBUG_OBJECT (this, "Pushing GDP caps buffer %p", this->new_segment_buf);
357 r = gst_pad_push (this->srcpad, gst_buffer_ref (this->caps_buf));
358 if (r != GST_FLOW_OK) {
359 GST_WARNING_OBJECT (this, "pushing GDP caps buffer returned %d", r);
362 this->sent_streamheader = TRUE;
363 GST_DEBUG_OBJECT (this, "need to push %d queued buffers",
364 g_list_length (this->queue));
368 for (l = this->queue; l; l = g_list_next (l)) {
369 GST_DEBUG_OBJECT (this, "Pushing queued GDP buffer %p", l->data);
370 gst_buffer_set_caps (l->data, caps);
371 r = gst_pad_push (this->srcpad, l->data);
372 if (r != GST_FLOW_OK) {
373 GST_WARNING_OBJECT (this, "pushing queued GDP buffer returned %d", r);
382 /* queue a buffer internally if we haven't sent streamheader buffers yet;
383 * otherwise, just push on */
385 gst_gdp_queue_buffer (GstGDPPay * this, GstBuffer * buffer)
387 if (this->sent_streamheader) {
388 GST_LOG_OBJECT (this, "Pushing GDP buffer %p", buffer);
389 GST_LOG_OBJECT (this, "set caps %" GST_PTR_FORMAT, this->caps);
390 return gst_pad_push (this->srcpad, buffer);
393 /* store it on an internal queue */
394 this->queue = g_list_append (this->queue, buffer);
395 GST_DEBUG_OBJECT (this, "queued buffer %p, now %d buffers queued",
396 buffer, g_list_length (this->queue));
401 gst_gdp_pay_chain (GstPad * pad, GstBuffer * buffer)
405 GstBuffer *outbuffer;
408 this = GST_GDP_PAY (gst_pad_get_parent (pad));
410 /* we should have received a new_segment before, otherwise it's a bug.
411 * fake one in that case */
412 if (!this->new_segment_buf) {
415 GST_WARNING_OBJECT (this,
416 "did not receive new-segment before first buffer");
417 event = gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
418 outbuffer = gst_gdp_buffer_from_event (this, event);
419 gst_event_unref (event);
421 /* GDP 0.2 doesn't know about new-segment, so this is not fatal */
423 GST_ELEMENT_WARNING (this, STREAM, ENCODE, (NULL),
424 ("Could not create GDP buffer from new segment event"));
426 ret = GST_FLOW_ERROR;
431 gst_gdp_stamp_buffer (this, outbuffer);
432 GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
433 GST_BUFFER_DURATION (outbuffer) = 0;
434 GST_DEBUG_OBJECT (this, "Storing buffer %p as new_segment_buf",
436 this->new_segment_buf = outbuffer;
440 /* make sure we've received caps before */
441 caps = gst_buffer_get_caps (buffer);
442 if (!this->caps && !caps) {
443 GST_WARNING_OBJECT (this, "first received buffer does not have caps set");
445 gst_caps_unref (caps);
446 ret = GST_FLOW_NOT_NEGOTIATED;
450 /* if the caps have changed, process caps first */
451 if (caps && !gst_caps_is_equal (this->caps, caps)) {
452 GST_LOG_OBJECT (this, "caps changed to %p, %" GST_PTR_FORMAT, caps, caps);
453 gst_caps_replace (&(this->caps), caps);
454 outbuffer = gst_gdp_buffer_from_caps (this, caps);
456 GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
457 ("Could not create GDP buffer from caps %" GST_PTR_FORMAT, caps));
458 gst_caps_unref (caps);
459 ret = GST_FLOW_ERROR;
463 gst_gdp_stamp_buffer (this, outbuffer);
464 GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
465 GST_BUFFER_DURATION (outbuffer) = 0;
466 GST_BUFFER_FLAG_SET (outbuffer, GST_BUFFER_FLAG_IN_CAPS);
467 this->caps_buf = outbuffer;
468 gst_gdp_pay_reset_streamheader (this);
471 /* create a GDP header packet,
472 * then create a GST buffer of the header packet and the buffer contents */
473 outbuffer = gst_gdp_pay_buffer_from_buffer (this, buffer);
475 GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
476 ("Could not create GDP buffer from buffer"));
477 ret = GST_FLOW_ERROR;
481 gst_gdp_stamp_buffer (this, outbuffer);
482 GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
483 GST_BUFFER_DURATION (outbuffer) = GST_BUFFER_DURATION (buffer);
485 ret = gst_gdp_queue_buffer (this, outbuffer);
488 gst_buffer_unref (buffer);
489 gst_object_unref (this);
494 gst_gdp_pay_sink_event (GstPad * pad, GstEvent * event)
496 GstBuffer *outbuffer;
497 GstGDPPay *this = GST_GDP_PAY (gst_pad_get_parent (pad));
498 GstFlowReturn flowret;
501 GST_DEBUG_OBJECT (this, "received event %s (%d)",
502 gst_event_type_get_name (event->type), event->type);
504 /* now turn the event into a buffer */
505 outbuffer = gst_gdp_buffer_from_event (this, event);
507 GST_ELEMENT_WARNING (this, STREAM, ENCODE, (NULL),
508 ("Could not create GDP buffer from received event"));
512 gst_gdp_stamp_buffer (this, outbuffer);
513 GST_BUFFER_TIMESTAMP (outbuffer) = GST_EVENT_TIMESTAMP (event);
514 GST_BUFFER_DURATION (outbuffer) = 0;
516 /* if we got a new segment, we should put it on our streamheader,
517 * and not send it on */
518 if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
519 GST_DEBUG_OBJECT (this, "received new_segment event");
520 if (this->new_segment_buf) {
521 gst_buffer_unref (this->new_segment_buf);
523 GST_DEBUG_OBJECT (this, "Storing buffer %p as new_segment_buf", outbuffer);
524 this->new_segment_buf = outbuffer;
525 gst_gdp_pay_reset_streamheader (this);
527 flowret = gst_gdp_queue_buffer (this, outbuffer);
528 if (flowret != GST_FLOW_OK) {
529 GST_WARNING_OBJECT (this, "queueing GDP caps buffer returned %d",
536 /* if we have EOS, we should send on EOS ourselves */
537 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
538 GST_DEBUG_OBJECT (this, "Sending on EOS event %p", event);
539 return gst_pad_push_event (this->srcpad, event);
543 gst_object_unref (this);
544 gst_event_unref (event);
549 gst_gdp_pay_set_property (GObject * object, guint prop_id,
550 const GValue * value, GParamSpec * pspec)
554 g_return_if_fail (GST_IS_GDP_PAY (object));
555 this = GST_GDP_PAY (object);
558 case PROP_CRC_HEADER:
559 this->crc_header = g_value_get_boolean (value);
560 this->header_flag = this->crc_header | this->crc_payload;
562 case PROP_CRC_PAYLOAD:
563 this->crc_payload = g_value_get_boolean (value);
564 this->header_flag = this->crc_header | this->crc_payload;
567 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
573 gst_gdp_pay_get_property (GObject * object, guint prop_id,
574 GValue * value, GParamSpec * pspec)
578 g_return_if_fail (GST_IS_GDP_PAY (object));
579 this = GST_GDP_PAY (object);
582 case PROP_CRC_HEADER:
583 g_value_set_boolean (value, this->crc_header);
585 case PROP_CRC_PAYLOAD:
586 g_value_set_boolean (value, this->crc_payload);
589 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
594 static GstStateChangeReturn
595 gst_gdp_pay_change_state (GstElement * element, GstStateChange transition)
597 GstStateChangeReturn ret;
598 GstGDPPay *this = GST_GDP_PAY (element);
600 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
602 switch (transition) {
603 case GST_STATE_CHANGE_READY_TO_NULL:
605 gst_caps_unref (this->caps);
617 gst_gdp_pay_plugin_init (GstPlugin * plugin)
619 if (!gst_element_register (plugin, "gdppay", GST_RANK_NONE, GST_TYPE_GDP_PAY))