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-gdpdepay
24 * This element depayloads GStreamer Data Protocol buffers back to deserialized
29 * gst-launch -v -m filesrc location=test.gdp ! gdpdepay ! xvimagesink
30 * ]| This pipeline plays back a serialized video stream as created in the
41 #include <gst/dataprotocol/dataprotocol.h>
43 #include "gstgdpdepay.h"
50 static GstStaticPadTemplate gdp_depay_sink_template =
51 GST_STATIC_PAD_TEMPLATE ("sink",
54 GST_STATIC_CAPS ("application/x-gdp"));
56 static GstStaticPadTemplate gdp_depay_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
62 GST_DEBUG_CATEGORY_STATIC (gst_gdp_depay_debug);
63 #define GST_CAT_DEFAULT gst_gdp_depay_debug
66 GST_DEBUG_CATEGORY_INIT (gst_gdp_depay_debug, "gdpdepay", 0, \
69 GST_BOILERPLATE_FULL (GstGDPDepay, gst_gdp_depay, GstElement,
70 GST_TYPE_ELEMENT, _do_init);
72 static gboolean gst_gdp_depay_sink_event (GstPad * pad, GstEvent * event);
73 static gboolean gst_gdp_depay_src_event (GstPad * pad, GstEvent * event);
75 static GstFlowReturn gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer);
77 static GstStateChangeReturn gst_gdp_depay_change_state (GstElement *
78 element, GstStateChange transition);
80 static void gst_gdp_depay_finalize (GObject * object);
83 gst_gdp_depay_base_init (gpointer g_class)
85 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
87 gst_element_class_set_details_simple (element_class,
88 "GDP Depayloader", "GDP/Depayloader",
89 "Depayloads GStreamer Data Protocol buffers",
90 "Thomas Vander Stichele <thomas at apestaart dot org>");
92 gst_element_class_add_pad_template (element_class,
93 gst_static_pad_template_get (&gdp_depay_sink_template));
94 gst_element_class_add_pad_template (element_class,
95 gst_static_pad_template_get (&gdp_depay_src_template));
99 gst_gdp_depay_class_init (GstGDPDepayClass * klass)
101 GObjectClass *gobject_class;
102 GstElementClass *gstelement_class;
104 gobject_class = (GObjectClass *) klass;
105 gstelement_class = (GstElementClass *) klass;
107 gstelement_class->change_state =
108 GST_DEBUG_FUNCPTR (gst_gdp_depay_change_state);
109 gobject_class->finalize = gst_gdp_depay_finalize;
113 gst_gdp_depay_init (GstGDPDepay * gdpdepay, GstGDPDepayClass * g_class)
116 gst_pad_new_from_static_template (&gdp_depay_sink_template, "sink");
117 gst_pad_set_chain_function (gdpdepay->sinkpad,
118 GST_DEBUG_FUNCPTR (gst_gdp_depay_chain));
119 gst_pad_set_event_function (gdpdepay->sinkpad,
120 GST_DEBUG_FUNCPTR (gst_gdp_depay_sink_event));
121 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->sinkpad);
124 gst_pad_new_from_static_template (&gdp_depay_src_template, "src");
125 gst_pad_set_event_function (gdpdepay->srcpad,
126 GST_DEBUG_FUNCPTR (gst_gdp_depay_src_event));
127 /* our caps will always be decided by the incoming GDP caps buffers */
128 gst_pad_use_fixed_caps (gdpdepay->srcpad);
129 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->srcpad);
131 gdpdepay->adapter = gst_adapter_new ();
135 gst_gdp_depay_finalize (GObject * gobject)
139 this = GST_GDP_DEPAY (gobject);
141 gst_caps_unref (this->caps);
142 g_free (this->header);
143 gst_adapter_clear (this->adapter);
144 g_object_unref (this->adapter);
146 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (gobject));
150 gst_gdp_depay_sink_event (GstPad * pad, GstEvent * event)
155 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
157 switch (GST_EVENT_TYPE (event)) {
158 case GST_EVENT_FLUSH_START:
159 /* forward flush start */
160 res = gst_pad_push_event (this->srcpad, event);
162 case GST_EVENT_FLUSH_STOP:
163 /* clear adapter on flush */
164 gst_adapter_clear (this->adapter);
165 /* forward flush stop */
166 res = gst_pad_push_event (this->srcpad, event);
169 /* after EOS, we don't expect to output anything anymore */
170 res = gst_pad_push_event (this->srcpad, event);
172 case GST_EVENT_NEWSEGMENT:
174 case GST_EVENT_BUFFERSIZE:
176 /* we unref most events as we take them from the datastream */
177 gst_event_unref (event);
180 gst_object_unref (this);
186 gst_gdp_depay_src_event (GstPad * pad, GstEvent * event)
191 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
193 switch (GST_EVENT_TYPE (event)) {
195 /* we refuse seek for now. */
196 gst_event_unref (event);
200 case GST_EVENT_NAVIGATION:
202 /* everything else is passed */
203 res = gst_pad_push_event (this->sinkpad, event);
206 gst_object_unref (this);
212 gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer)
215 GstFlowReturn ret = GST_FLOW_OK;
221 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
223 /* On DISCONT, get rid of accumulated data. We assume a buffer after the
224 * DISCONT contains (part of) a new valid header, if not we error because we
226 if (GST_BUFFER_IS_DISCONT (buffer)) {
227 gst_adapter_clear (this->adapter);
228 this->state = GST_GDP_DEPAY_STATE_HEADER;
230 gst_adapter_push (this->adapter, buffer);
233 switch (this->state) {
234 case GST_GDP_DEPAY_STATE_HEADER:
238 /* collect a complete header, validate and store the header. Figure out
239 * the payload length and switch to the PAYLOAD state */
240 available = gst_adapter_available (this->adapter);
241 if (available < GST_DP_HEADER_LENGTH)
244 GST_LOG_OBJECT (this, "reading GDP header from adapter");
245 header = gst_adapter_take (this->adapter, GST_DP_HEADER_LENGTH);
246 if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header)) {
248 goto header_validate_error;
251 /* store types and payload length. Also store the header, which we need
252 * to make the payload. */
253 this->payload_length = gst_dp_header_payload_length (header);
254 this->payload_type = gst_dp_header_payload_type (header);
255 /* free previous header and store new one. */
256 g_free (this->header);
257 this->header = header;
259 GST_LOG_OBJECT (this,
260 "read GDP header, payload size %d, payload type %d, switching to state PAYLOAD",
261 this->payload_length, this->payload_type);
262 this->state = GST_GDP_DEPAY_STATE_PAYLOAD;
265 case GST_GDP_DEPAY_STATE_PAYLOAD:
267 /* in this state we wait for all the payload data to be available in the
268 * adapter. Then we switch to the state where we actually process the
270 available = gst_adapter_available (this->adapter);
271 if (available < this->payload_length)
274 /* change state based on type */
275 if (this->payload_type == GST_DP_PAYLOAD_BUFFER) {
276 GST_LOG_OBJECT (this, "switching to state BUFFER");
277 this->state = GST_GDP_DEPAY_STATE_BUFFER;
278 } else if (this->payload_type == GST_DP_PAYLOAD_CAPS) {
279 GST_LOG_OBJECT (this, "switching to state CAPS");
280 this->state = GST_GDP_DEPAY_STATE_CAPS;
281 } else if (this->payload_type >= GST_DP_PAYLOAD_EVENT_NONE) {
282 GST_LOG_OBJECT (this, "switching to state EVENT");
283 this->state = GST_GDP_DEPAY_STATE_EVENT;
288 if (this->payload_length) {
292 data = gst_adapter_map (this->adapter, this->payload_length);
293 res = gst_dp_validate_payload (GST_DP_HEADER_LENGTH, this->header,
295 gst_adapter_unmap (this->adapter, 0);
298 goto payload_validate_error;
303 case GST_GDP_DEPAY_STATE_BUFFER:
305 /* if we receive a buffer without caps first, we error out */
309 GST_LOG_OBJECT (this, "reading GDP buffer from adapter");
310 buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, this->header);
314 /* now take the payload if there is any */
315 if (this->payload_length > 0) {
318 payload = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
319 gst_adapter_copy (this->adapter, payload, 0, this->payload_length);
320 gst_buffer_unmap (buf, payload, this->payload_length);
322 gst_adapter_flush (this->adapter, this->payload_length);
325 /* set caps and push */
326 gst_buffer_set_caps (buf, this->caps);
327 GST_LOG_OBJECT (this, "deserialized buffer %p, pushing, timestamp %"
328 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
329 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT
330 ", size %" G_GSIZE_FORMAT ", flags 0x%x",
332 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
333 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
334 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
335 gst_buffer_get_size (buf), GST_BUFFER_FLAGS (buf));
336 ret = gst_pad_push (this->srcpad, buf);
337 if (ret != GST_FLOW_OK)
340 GST_LOG_OBJECT (this, "switching to state HEADER");
341 this->state = GST_GDP_DEPAY_STATE_HEADER;
344 case GST_GDP_DEPAY_STATE_CAPS:
348 /* take the payload of the caps */
349 GST_LOG_OBJECT (this, "reading GDP caps from adapter");
350 payload = gst_adapter_take (this->adapter, this->payload_length);
351 caps = gst_dp_caps_from_packet (GST_DP_HEADER_LENGTH, this->header,
357 GST_DEBUG_OBJECT (this, "deserialized caps %" GST_PTR_FORMAT, caps);
358 gst_caps_replace (&(this->caps), caps);
359 gst_pad_set_caps (this->srcpad, caps);
360 /* drop the creation ref we still have */
361 gst_caps_unref (caps);
363 GST_LOG_OBJECT (this, "switching to state HEADER");
364 this->state = GST_GDP_DEPAY_STATE_HEADER;
367 case GST_GDP_DEPAY_STATE_EVENT:
371 GST_LOG_OBJECT (this, "reading GDP event from adapter");
373 /* adapter doesn't like 0 length payload */
374 if (this->payload_length > 0)
375 payload = gst_adapter_take (this->adapter, this->payload_length);
378 event = gst_dp_event_from_packet (GST_DP_HEADER_LENGTH, this->header,
384 GST_DEBUG_OBJECT (this, "deserialized event %p of type %s, pushing",
385 event, gst_event_type_get_name (event->type));
386 gst_pad_push_event (this->srcpad, event);
388 GST_LOG_OBJECT (this, "switching to state HEADER");
389 this->state = GST_GDP_DEPAY_STATE_HEADER;
396 gst_object_unref (this);
400 header_validate_error:
402 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
403 ("GDP packet header does not validate"));
404 ret = GST_FLOW_ERROR;
407 payload_validate_error:
409 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
410 ("GDP packet payload does not validate"));
411 ret = GST_FLOW_ERROR;
416 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
417 ("GDP packet header is of wrong type"));
418 ret = GST_FLOW_ERROR;
423 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
424 ("Received a buffer without first receiving caps"));
425 ret = GST_FLOW_NOT_NEGOTIATED;
430 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
431 ("could not create buffer from GDP packet"));
432 ret = GST_FLOW_ERROR;
437 GST_WARNING_OBJECT (this, "pushing depayloaded buffer returned %d", ret);
442 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
443 ("could not create caps from GDP packet"));
444 ret = GST_FLOW_ERROR;
449 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
450 ("could not create event from GDP packet"));
451 ret = GST_FLOW_ERROR;
456 static GstStateChangeReturn
457 gst_gdp_depay_change_state (GstElement * element, GstStateChange transition)
459 GstStateChangeReturn ret;
460 GstGDPDepay *this = GST_GDP_DEPAY (element);
462 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
464 switch (transition) {
465 case GST_STATE_CHANGE_PAUSED_TO_READY:
467 gst_caps_unref (this->caps);
470 gst_adapter_clear (this->adapter);
479 gst_gdp_depay_plugin_init (GstPlugin * plugin)
481 if (!gst_element_register (plugin, "gdpdepay", GST_RANK_NONE,