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"
45 /* elementfactory information */
46 static const GstElementDetails gdp_depay_details =
47 GST_ELEMENT_DETAILS ("GDP Depayloader",
49 "Depayloads GStreamer Data Protocol buffers",
50 "Thomas Vander Stichele <thomas at apestaart dot org>");
57 static GstStaticPadTemplate gdp_depay_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("sink",
61 GST_STATIC_CAPS ("application/x-gdp"));
63 static GstStaticPadTemplate gdp_depay_src_template =
64 GST_STATIC_PAD_TEMPLATE ("src",
69 GST_DEBUG_CATEGORY_STATIC (gst_gdp_depay_debug);
70 #define GST_CAT_DEFAULT gst_gdp_depay_debug
73 GST_DEBUG_CATEGORY_INIT (gst_gdp_depay_debug, "gdpdepay", 0, \
76 GST_BOILERPLATE_FULL (GstGDPDepay, gst_gdp_depay, GstElement,
77 GST_TYPE_ELEMENT, _do_init);
79 static gboolean gst_gdp_depay_sink_event (GstPad * pad, GstEvent * event);
80 static gboolean gst_gdp_depay_src_event (GstPad * pad, GstEvent * event);
82 static GstFlowReturn gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer);
84 static GstStateChangeReturn gst_gdp_depay_change_state (GstElement *
85 element, GstStateChange transition);
87 static void gst_gdp_depay_finalize (GObject * object);
90 gst_gdp_depay_base_init (gpointer g_class)
92 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
94 gst_element_class_set_details (element_class, &gdp_depay_details);
96 gst_element_class_add_pad_template (element_class,
97 gst_static_pad_template_get (&gdp_depay_sink_template));
98 gst_element_class_add_pad_template (element_class,
99 gst_static_pad_template_get (&gdp_depay_src_template));
103 gst_gdp_depay_class_init (GstGDPDepayClass * klass)
105 GObjectClass *gobject_class;
106 GstElementClass *gstelement_class;
108 gobject_class = (GObjectClass *) klass;
109 gstelement_class = (GstElementClass *) klass;
111 gstelement_class->change_state =
112 GST_DEBUG_FUNCPTR (gst_gdp_depay_change_state);
113 gobject_class->finalize = gst_gdp_depay_finalize;
117 gst_gdp_depay_init (GstGDPDepay * gdpdepay, GstGDPDepayClass * g_class)
120 gst_pad_new_from_static_template (&gdp_depay_sink_template, "sink");
121 gst_pad_set_chain_function (gdpdepay->sinkpad,
122 GST_DEBUG_FUNCPTR (gst_gdp_depay_chain));
123 gst_pad_set_event_function (gdpdepay->sinkpad,
124 GST_DEBUG_FUNCPTR (gst_gdp_depay_sink_event));
125 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->sinkpad);
128 gst_pad_new_from_static_template (&gdp_depay_src_template, "src");
129 gst_pad_set_event_function (gdpdepay->srcpad,
130 GST_DEBUG_FUNCPTR (gst_gdp_depay_src_event));
131 /* our caps will always be decided by the incoming GDP caps buffers */
132 gst_pad_use_fixed_caps (gdpdepay->srcpad);
133 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->srcpad);
135 gdpdepay->adapter = gst_adapter_new ();
139 gst_gdp_depay_finalize (GObject * gobject)
143 this = GST_GDP_DEPAY (gobject);
145 gst_caps_unref (this->caps);
146 g_free (this->header);
147 gst_adapter_clear (this->adapter);
148 g_object_unref (this->adapter);
150 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (gobject));
154 gst_gdp_depay_sink_event (GstPad * pad, GstEvent * event)
159 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
161 switch (GST_EVENT_TYPE (event)) {
162 case GST_EVENT_FLUSH_START:
163 case GST_EVENT_FLUSH_STOP:
164 /* forward flush start and stop */
165 res = gst_pad_push_event (this->srcpad, event);
168 /* after EOS, we don't expect to output anything anymore */
169 res = gst_pad_push_event (this->srcpad, event);
171 case GST_EVENT_NEWSEGMENT:
173 case GST_EVENT_BUFFERSIZE:
175 /* we unref most events as we take them from the datastream */
176 gst_event_unref (event);
179 gst_object_unref (this);
185 gst_gdp_depay_src_event (GstPad * pad, GstEvent * event)
190 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
192 switch (GST_EVENT_TYPE (event)) {
194 /* we refuse seek for now. */
195 gst_event_unref (event);
199 case GST_EVENT_NAVIGATION:
201 /* everything else is passed */
202 res = gst_pad_push_event (this->sinkpad, event);
205 gst_object_unref (this);
211 gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer)
214 GstFlowReturn ret = GST_FLOW_OK;
220 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
222 /* On DISCONT, get rid of accumulated data. We assume a buffer after the
223 * DISCONT contains (part of) a new valid header, if not we error because we
225 if (GST_BUFFER_IS_DISCONT (buffer)) {
226 gst_adapter_clear (this->adapter);
227 this->state = GST_GDP_DEPAY_STATE_HEADER;
229 gst_adapter_push (this->adapter, buffer);
232 switch (this->state) {
233 case GST_GDP_DEPAY_STATE_HEADER:
237 /* collect a complete header, validate and store the header. Figure out
238 * the payload length and switch to the PAYLOAD state */
239 available = gst_adapter_available (this->adapter);
240 if (available < GST_DP_HEADER_LENGTH)
243 GST_LOG_OBJECT (this, "reading GDP header from adapter");
244 header = gst_adapter_take (this->adapter, GST_DP_HEADER_LENGTH);
245 if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header)) {
247 goto header_validate_error;
250 /* store types and payload length. Also store the header, which we need
251 * to make the payload. */
252 this->payload_length = gst_dp_header_payload_length (header);
253 this->payload_type = gst_dp_header_payload_type (header);
254 /* free previous header and store new one. */
255 g_free (this->header);
256 this->header = header;
258 GST_LOG_OBJECT (this,
259 "read GDP header, payload size %d, payload type %d, switching to state PAYLOAD",
260 this->payload_length, this->payload_type);
261 this->state = GST_GDP_DEPAY_STATE_PAYLOAD;
264 case GST_GDP_DEPAY_STATE_PAYLOAD:
266 /* in this state we wait for all the payload data to be available in the
267 * adapter. Then we switch to the state where we actually process the
269 available = gst_adapter_available (this->adapter);
270 if (available < this->payload_length)
273 /* change state based on type */
274 if (this->payload_type == GST_DP_PAYLOAD_BUFFER) {
275 GST_LOG_OBJECT (this, "switching to state BUFFER");
276 this->state = GST_GDP_DEPAY_STATE_BUFFER;
277 } else if (this->payload_type == GST_DP_PAYLOAD_CAPS) {
278 GST_LOG_OBJECT (this, "switching to state CAPS");
279 this->state = GST_GDP_DEPAY_STATE_CAPS;
280 } else if (this->payload_type >= GST_DP_PAYLOAD_EVENT_NONE) {
281 GST_LOG_OBJECT (this, "switching to state EVENT");
282 this->state = GST_GDP_DEPAY_STATE_EVENT;
287 if (this->payload_length
288 && (!gst_dp_validate_payload (GST_DP_HEADER_LENGTH, this->header,
289 gst_adapter_peek (this->adapter, this->payload_length)))) {
290 goto payload_validate_error;
295 case GST_GDP_DEPAY_STATE_BUFFER:
298 /* if we receive a buffer without caps first, we error out */
302 GST_LOG_OBJECT (this, "reading GDP buffer from adapter");
303 buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, this->header);
307 /* now take the payload if there is any */
308 if (this->payload_length > 0) {
311 payload = gst_adapter_take (this->adapter, this->payload_length);
312 memcpy (GST_BUFFER_DATA (buf), payload, this->payload_length);
316 /* set caps and push */
317 gst_buffer_set_caps (buf, this->caps);
318 GST_LOG_OBJECT (this, "deserialized buffer %p, pushing, timestamp %"
319 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
320 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT
321 ", size %d, flags 0x%x",
323 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
324 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
325 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
326 GST_BUFFER_SIZE (buf), GST_BUFFER_FLAGS (buf));
327 ret = gst_pad_push (this->srcpad, buf);
328 if (ret != GST_FLOW_OK)
331 GST_LOG_OBJECT (this, "switching to state HEADER");
332 this->state = GST_GDP_DEPAY_STATE_HEADER;
335 case GST_GDP_DEPAY_STATE_CAPS:
339 /* take the payload of the caps */
340 GST_LOG_OBJECT (this, "reading GDP caps from adapter");
341 payload = gst_adapter_take (this->adapter, this->payload_length);
342 caps = gst_dp_caps_from_packet (GST_DP_HEADER_LENGTH, this->header,
348 GST_DEBUG_OBJECT (this, "deserialized caps %" GST_PTR_FORMAT, caps);
349 gst_caps_replace (&(this->caps), caps);
350 gst_pad_set_caps (this->srcpad, caps);
351 /* drop the creation ref we still have */
352 gst_caps_unref (caps);
354 GST_LOG_OBJECT (this, "switching to state HEADER");
355 this->state = GST_GDP_DEPAY_STATE_HEADER;
358 case GST_GDP_DEPAY_STATE_EVENT:
362 GST_LOG_OBJECT (this, "reading GDP event from adapter");
364 /* adapter doesn't like 0 length payload */
365 if (this->payload_length > 0)
366 payload = gst_adapter_take (this->adapter, this->payload_length);
369 event = gst_dp_event_from_packet (GST_DP_HEADER_LENGTH, this->header,
375 GST_DEBUG_OBJECT (this, "deserialized event %p of type %s, pushing",
376 event, gst_event_type_get_name (event->type));
377 gst_pad_push_event (this->srcpad, event);
379 GST_LOG_OBJECT (this, "switching to state HEADER");
380 this->state = GST_GDP_DEPAY_STATE_HEADER;
387 gst_object_unref (this);
391 header_validate_error:
393 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
394 ("GDP packet header does not validate"));
395 ret = GST_FLOW_ERROR;
398 payload_validate_error:
400 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
401 ("GDP packet payload does not validate"));
402 ret = GST_FLOW_ERROR;
407 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
408 ("GDP packet header is of wrong type"));
409 ret = GST_FLOW_ERROR;
414 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
415 ("Received a buffer without first receiving caps"));
416 ret = GST_FLOW_NOT_NEGOTIATED;
421 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
422 ("could not create buffer from GDP packet"));
423 ret = GST_FLOW_ERROR;
428 GST_WARNING_OBJECT (this, "pushing depayloaded buffer returned %d", ret);
433 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
434 ("could not create caps from GDP packet"));
435 ret = GST_FLOW_ERROR;
440 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
441 ("could not create event from GDP packet"));
442 ret = GST_FLOW_ERROR;
447 static GstStateChangeReturn
448 gst_gdp_depay_change_state (GstElement * element, GstStateChange transition)
450 GstStateChangeReturn ret;
451 GstGDPDepay *this = GST_GDP_DEPAY (element);
453 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
455 switch (transition) {
456 case GST_STATE_CHANGE_PAUSED_TO_READY:
458 gst_caps_unref (this->caps);
469 gst_gdp_depay_plugin_init (GstPlugin * plugin)
471 if (!gst_element_register (plugin, "gdpdepay", GST_RANK_NONE,