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
25 * This element payloads GStreamer buffers and events using the
26 * GStreamer Data Protocol.
35 #include <gst/dataprotocol/dataprotocol.h>
37 #include "gstgdppay.h"
39 /* elementfactory information */
40 static const GstElementDetails gdp_pay_details =
41 GST_ELEMENT_DETAILS ("GDP Payloader",
43 "Payloads GStreamer Data Protocol buffers",
44 "Thomas Vander Stichele <thomas at apestaart dot org>");
52 static GstStaticPadTemplate gdp_pay_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
58 static GstStaticPadTemplate gdp_pay_src_template =
59 GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS ("application/x-gdp"));
64 GST_DEBUG_CATEGORY (gst_gdp_pay_debug);
65 #define GST_CAT_DEFAULT gst_gdp_pay_debug
68 GST_DEBUG_CATEGORY_INIT (gst_gdp_pay_debug, "gdppay", 0, \
71 GST_BOILERPLATE_FULL (GstGDPPay, gst_gdp_pay, GstElement,
72 GST_TYPE_ELEMENT, _do_init);
74 static GstFlowReturn gst_gdp_pay_chain (GstPad * pad, GstBuffer * buffer);
75 static gboolean gst_gdp_pay_sink_event (GstPad * pad, GstEvent * event);
76 static GstStateChangeReturn gst_gdp_pay_change_state (GstElement *
77 element, GstStateChange transition);
79 static void gst_gdp_pay_dispose (GObject * gobject);
82 gst_gdp_pay_base_init (gpointer g_class)
84 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
86 gst_element_class_set_details (element_class, &gdp_pay_details);
88 gst_element_class_add_pad_template (element_class,
89 gst_static_pad_template_get (&gdp_pay_sink_template));
90 gst_element_class_add_pad_template (element_class,
91 gst_static_pad_template_get (&gdp_pay_src_template));
95 gst_gdp_pay_class_init (GstGDPPayClass * klass)
97 GObjectClass *gobject_class;
98 GstElementClass *gstelement_class;
100 gobject_class = (GObjectClass *) klass;
101 gstelement_class = (GstElementClass *) klass;
103 parent_class = g_type_class_peek_parent (klass);
105 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_gdp_pay_dispose);
106 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_gdp_pay_change_state);
110 gst_gdp_pay_init (GstGDPPay * gdppay, GstGDPPayClass * g_class)
113 gst_pad_new_from_static_template (&gdp_pay_sink_template, "sink");
114 gst_pad_set_chain_function (gdppay->sinkpad,
115 GST_DEBUG_FUNCPTR (gst_gdp_pay_chain));
116 gst_pad_set_event_function (gdppay->sinkpad,
117 GST_DEBUG_FUNCPTR (gst_gdp_pay_sink_event));
118 gst_element_add_pad (GST_ELEMENT (gdppay), gdppay->sinkpad);
121 gst_pad_new_from_static_template (&gdp_pay_src_template, "src");
122 gst_element_add_pad (GST_ELEMENT (gdppay), gdppay->srcpad);
128 gst_gdp_pay_dispose (GObject * gobject)
130 GstGDPPay *this = GST_GDP_PAY (gobject);
132 if (this->caps_buf) {
133 gst_buffer_unref (this->caps_buf);
134 this->caps_buf = NULL;
136 if (this->new_segment_buf) {
137 gst_buffer_unref (this->new_segment_buf);
138 this->new_segment_buf = NULL;
140 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (gobject));
143 /* set OFFSET and OFFSET_END with running count */
145 gst_gdp_stamp_buffer (GstGDPPay * this, GstBuffer * buffer)
147 GST_BUFFER_OFFSET (buffer) = this->offset;
148 GST_BUFFER_OFFSET_END (buffer) = this->offset + GST_BUFFER_SIZE (buffer);
149 this->offset = GST_BUFFER_OFFSET_END (buffer);
153 gst_gdp_buffer_from_caps (GstGDPPay * this, GstCaps * caps)
155 GstBuffer *headerbuf;
156 GstBuffer *payloadbuf;
157 guint8 *header, *payload;
160 if (!gst_dp_packet_from_caps (caps, 0, &len, &header, &payload)) {
161 GST_WARNING_OBJECT (this, "could not create GDP header from caps");
165 GST_LOG_OBJECT (this, "creating GDP header and payload buffer from caps");
166 headerbuf = gst_buffer_new ();
167 gst_buffer_set_data (headerbuf, header, len);
168 GST_BUFFER_MALLOCDATA (headerbuf) = header;
170 payloadbuf = gst_buffer_new ();
171 gst_buffer_set_data (payloadbuf, payload,
172 gst_dp_header_payload_length (header));
173 GST_BUFFER_MALLOCDATA (payloadbuf) = payload;
175 return gst_buffer_join (headerbuf, payloadbuf);
179 gst_gdp_pay_buffer_from_buffer (GstGDPPay * this, GstBuffer * buffer)
181 GstBuffer *headerbuf;
185 if (!gst_dp_header_from_buffer (buffer, 0, &len, &header)) {
186 GST_WARNING_OBJECT (this, "could not create GDP header from buffer");
190 GST_LOG_OBJECT (this, "creating GDP header and payload buffer from buffer");
191 headerbuf = gst_buffer_new ();
192 gst_buffer_set_data (headerbuf, header, len);
193 GST_BUFFER_MALLOCDATA (headerbuf) = header;
195 /* we do not want to lose the ref on the incoming buffer */
196 gst_buffer_ref (buffer);
197 return gst_buffer_join (headerbuf, buffer);
201 gst_gdp_buffer_from_event (GstGDPPay * this, GstEvent * event)
203 GstBuffer *headerbuf;
204 GstBuffer *payloadbuf;
205 guint8 *header, *payload;
208 if (!gst_dp_packet_from_event (event, 0, &len, &header, &payload)) {
209 GST_WARNING_OBJECT (this, "could not create GDP header from event");
213 GST_LOG_OBJECT (this, "creating GDP header and payload buffer from event");
214 headerbuf = gst_buffer_new ();
215 gst_buffer_set_data (headerbuf, header, len);
216 GST_BUFFER_MALLOCDATA (headerbuf) = header;
218 payloadbuf = gst_buffer_new ();
219 gst_buffer_set_data (payloadbuf, payload,
220 gst_dp_header_payload_length (header));
221 GST_BUFFER_MALLOCDATA (payloadbuf) = payload;
223 return gst_buffer_join (headerbuf, payloadbuf);
227 /* set our caps with streamheader, based on the latest newsegment and caps,
228 * and (possibly) GDP-serialized buffers of the streamheaders on the src pad */
230 gst_gdp_pay_reset_streamheader (GstGDPPay * this)
233 GstStructure *structure;
234 GstBuffer *new_segment_buf, *caps_buf;
235 GstFlowReturn r = GST_FLOW_OK;
237 GValue array = { 0 };
238 GValue value = { 0 };
240 /* we need both new segment and caps before we can set streamheader */
241 if (!this->new_segment_buf || !this->caps_buf)
244 /* we copy to avoid circular refcounts */
245 new_segment_buf = gst_buffer_copy (this->new_segment_buf);
246 caps_buf = gst_buffer_copy (this->caps_buf);
248 /* put copies of the buffers in a fixed list */
249 g_value_init (&array, GST_TYPE_ARRAY);
251 g_value_init (&value, GST_TYPE_BUFFER);
252 gst_value_set_buffer (&value, new_segment_buf);
253 gst_value_array_append_value (&array, &value);
254 g_value_unset (&value);
256 g_value_init (&value, GST_TYPE_BUFFER);
257 gst_value_set_buffer (&value, caps_buf);
258 gst_value_array_append_value (&array, &value);
259 g_value_unset (&value);
261 /* we also need to add GDP serializations of the streamheaders of the
263 structure = gst_caps_get_structure (this->caps, 0);
264 if (gst_structure_has_field (structure, "streamheader")) {
270 sh = gst_structure_get_value (structure, "streamheader");
271 buffers = g_value_peek_pointer (sh);
272 GST_DEBUG_OBJECT (this,
273 "Need to serialize %d incoming streamheader buffers on our streamheader",
275 for (i = 0; i < buffers->len; ++i) {
277 GstBuffer *outbuffer;
279 bufval = &g_array_index (buffers, GValue, i);
280 buffer = g_value_peek_pointer (bufval);
281 outbuffer = gst_gdp_pay_buffer_from_buffer (this, buffer);
283 g_value_init (&value, GST_TYPE_BUFFER);
284 gst_value_set_buffer (&value, outbuffer);
285 gst_value_array_append_value (&array, &value);
286 g_value_unset (&value);
288 /* FIXME: if one or more in this loop fail to produce and outbuffer,
289 * should we error out ? Once ? Every time ? */
293 caps = gst_caps_from_string ("application/x-gdp");
294 structure = gst_caps_get_structure (caps, 0);
296 gst_structure_set_value (structure, "streamheader", &array);
297 g_value_unset (&array);
299 /* Unref our copies */
300 gst_buffer_unref (new_segment_buf);
301 gst_buffer_unref (caps_buf);
303 GST_DEBUG_OBJECT (this, "Setting caps on src pad %" GST_PTR_FORMAT, caps);
304 gst_pad_set_caps (this->srcpad, caps);
305 gst_buffer_set_caps (this->caps_buf, caps);
306 gst_buffer_set_caps (this->new_segment_buf, caps);
308 /* if these are our first ever buffers, send out new_segment first */
309 if (!this->sent_streamheader) {
311 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
312 GST_DEBUG_OBJECT (this, "Sending out new_segment event %p", event);
313 if (!gst_pad_push_event (this->srcpad, event)) {
314 GST_WARNING_OBJECT (this, "pushing new segment failed");
315 return GST_FLOW_ERROR;
319 /* push out these streamheader buffers, then flush our internal queue */
320 GST_DEBUG_OBJECT (this, "Pushing GDP new_segment buffer %p",
321 this->new_segment_buf);
322 /* we stored these bufs with refcount 1, so make sure we keep a ref */
323 r = gst_pad_push (this->srcpad, gst_buffer_ref (this->new_segment_buf));
324 if (r != GST_FLOW_OK) {
325 GST_WARNING_OBJECT (this, "pushing GDP newsegment buffer returned %d", r);
328 GST_DEBUG_OBJECT (this, "Pushing GDP caps buffer %p", this->new_segment_buf);
329 r = gst_pad_push (this->srcpad, gst_buffer_ref (this->caps_buf));
330 if (r != GST_FLOW_OK) {
331 GST_WARNING_OBJECT (this, "pushing GDP caps buffer returned %d", r);
334 this->sent_streamheader = TRUE;
335 GST_DEBUG_OBJECT (this, "need to push %d queued buffers",
336 g_list_length (this->queue));
340 for (l = this->queue; l; l = g_list_next (l)) {
341 GST_DEBUG_OBJECT (this, "Pushing queued GDP buffer %p", l->data);
342 gst_buffer_set_caps (l->data, caps);
343 r = gst_pad_push (this->srcpad, l->data);
344 if (r != GST_FLOW_OK) {
345 GST_WARNING_OBJECT (this, "pushing queued GDP buffer returned %d", r);
354 /* queue a buffer internally if we haven't sent streamheader buffers yet;
355 * otherwise, just push on */
357 gst_gdp_queue_buffer (GstGDPPay * this, GstBuffer * buffer)
359 if (this->sent_streamheader) {
360 GST_LOG_OBJECT (this, "Pushing GDP buffer %p", buffer);
361 GST_LOG_OBJECT (this, "set caps %" GST_PTR_FORMAT, this->caps);
362 return gst_pad_push (this->srcpad, buffer);
365 /* store it on an internal queue */
366 this->queue = g_list_append (this->queue, buffer);
367 GST_DEBUG_OBJECT (this, "queued buffer %p, now %d buffers queued",
368 buffer, g_list_length (this->queue));
373 gst_gdp_pay_chain (GstPad * pad, GstBuffer * buffer)
377 GstBuffer *outbuffer;
380 this = GST_GDP_PAY (gst_pad_get_parent (pad));
382 /* we should have received a new_segment before, otherwise it's a bug.
383 * fake one in that case */
384 if (!this->new_segment_buf) {
387 GST_WARNING_OBJECT (this,
388 "did not receive new-segment before first buffer");
389 event = gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
390 outbuffer = gst_gdp_buffer_from_event (this, event);
391 gst_event_unref (event);
394 GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
395 ("Could not create GDP buffer from new segment event"));
396 ret = GST_FLOW_ERROR;
400 gst_gdp_stamp_buffer (this, outbuffer);
401 GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
402 GST_BUFFER_DURATION (outbuffer) = 0;
403 this->new_segment_buf = outbuffer;
406 /* make sure we've received caps before */
407 caps = gst_buffer_get_caps (buffer);
408 if (!this->caps && !caps) {
409 GST_WARNING_OBJECT (this, "first received buffer does not have caps set");
411 gst_caps_unref (caps);
412 ret = GST_FLOW_NOT_NEGOTIATED;
416 /* if the caps have changed, process caps first */
417 if (caps && !gst_caps_is_equal (this->caps, caps)) {
418 GST_LOG_OBJECT (this, "caps changed to %p, %" GST_PTR_FORMAT, caps, caps);
419 gst_caps_replace (&(this->caps), caps);
420 outbuffer = gst_gdp_buffer_from_caps (this, caps);
422 GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
423 ("Could not create GDP buffer from caps %" GST_PTR_FORMAT, caps));
424 gst_caps_unref (caps);
425 ret = GST_FLOW_ERROR;
429 gst_gdp_stamp_buffer (this, outbuffer);
430 GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
431 GST_BUFFER_DURATION (outbuffer) = 0;
432 GST_BUFFER_FLAG_SET (outbuffer, GST_BUFFER_FLAG_IN_CAPS);
433 this->caps_buf = outbuffer;
434 gst_gdp_pay_reset_streamheader (this);
437 /* create a GDP header packet,
438 * then create a GST buffer of the header packet and the buffer contents */
439 outbuffer = gst_gdp_pay_buffer_from_buffer (this, buffer);
441 GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
442 ("Could not create GDP buffer from buffer"));
443 ret = GST_FLOW_ERROR;
447 gst_gdp_stamp_buffer (this, outbuffer);
448 GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
449 GST_BUFFER_DURATION (outbuffer) = GST_BUFFER_DURATION (buffer);
451 ret = gst_gdp_queue_buffer (this, outbuffer);
454 gst_buffer_unref (buffer);
455 gst_object_unref (this);
460 gst_gdp_pay_sink_event (GstPad * pad, GstEvent * event)
462 GstBuffer *outbuffer;
463 GstGDPPay *this = GST_GDP_PAY (gst_pad_get_parent (pad));
464 GstFlowReturn flowret;
467 /* now turn the event into a buffer */
468 outbuffer = gst_gdp_buffer_from_event (this, event);
470 GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
471 ("Could not create GDP buffer from event"));
475 gst_gdp_stamp_buffer (this, outbuffer);
476 GST_BUFFER_TIMESTAMP (outbuffer) = GST_EVENT_TIMESTAMP (event);
477 GST_BUFFER_DURATION (outbuffer) = 0;
479 /* if we got a new segment, we should put it on our streamheader,
480 * and not send it on */
481 if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
482 if (this->new_segment_buf) {
483 gst_buffer_unref (this->new_segment_buf);
485 this->new_segment_buf = outbuffer;
486 gst_gdp_pay_reset_streamheader (this);
488 flowret = gst_gdp_queue_buffer (this, outbuffer);
489 if (flowret != GST_FLOW_OK) {
490 GST_WARNING_OBJECT (this, "queueing GDP caps buffer returned %d",
497 /* if we have EOS, we should send on EOS ourselves */
498 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
499 GST_DEBUG_OBJECT (this, "Sending on EOS event %p", event);
500 return gst_pad_push_event (this->srcpad, event);
504 gst_object_unref (this);
505 gst_event_unref (event);
509 static GstStateChangeReturn
510 gst_gdp_pay_change_state (GstElement * element, GstStateChange transition)
512 GstStateChangeReturn ret;
513 GstGDPPay *this = GST_GDP_PAY (element);
515 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
517 switch (transition) {
518 case GST_STATE_CHANGE_READY_TO_NULL:
520 gst_caps_unref (this->caps);
532 gst_gdp_pay_plugin_init (GstPlugin * plugin)
534 if (!gst_element_register (plugin, "gdppay", GST_RANK_NONE, GST_TYPE_GDP_PAY))