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, \
68 #define gst_gdp_depay_parent_class parent_class
69 G_DEFINE_TYPE_WITH_CODE (GstGDPDepay, gst_gdp_depay,
70 GST_TYPE_ELEMENT, _do_init);
72 static gboolean gst_gdp_depay_sink_event (GstPad * pad, GstObject * parent,
74 static gboolean gst_gdp_depay_src_event (GstPad * pad, GstObject * parent,
77 static GstFlowReturn gst_gdp_depay_chain (GstPad * pad, GstObject * parent,
80 static GstStateChangeReturn gst_gdp_depay_change_state (GstElement *
81 element, GstStateChange transition);
83 static void gst_gdp_depay_finalize (GObject * object);
86 gst_gdp_depay_class_init (GstGDPDepayClass * klass)
88 GObjectClass *gobject_class;
89 GstElementClass *gstelement_class;
91 gobject_class = (GObjectClass *) klass;
92 gstelement_class = (GstElementClass *) klass;
94 gst_element_class_set_details_simple (gstelement_class,
95 "GDP Depayloader", "GDP/Depayloader",
96 "Depayloads GStreamer Data Protocol buffers",
97 "Thomas Vander Stichele <thomas at apestaart dot org>");
99 gst_element_class_add_pad_template (gstelement_class,
100 gst_static_pad_template_get (&gdp_depay_sink_template));
101 gst_element_class_add_pad_template (gstelement_class,
102 gst_static_pad_template_get (&gdp_depay_src_template));
104 gstelement_class->change_state =
105 GST_DEBUG_FUNCPTR (gst_gdp_depay_change_state);
106 gobject_class->finalize = gst_gdp_depay_finalize;
110 gst_gdp_depay_init (GstGDPDepay * gdpdepay)
113 gst_pad_new_from_static_template (&gdp_depay_sink_template, "sink");
114 gst_pad_set_chain_function (gdpdepay->sinkpad,
115 GST_DEBUG_FUNCPTR (gst_gdp_depay_chain));
116 gst_pad_set_event_function (gdpdepay->sinkpad,
117 GST_DEBUG_FUNCPTR (gst_gdp_depay_sink_event));
118 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->sinkpad);
121 gst_pad_new_from_static_template (&gdp_depay_src_template, "src");
122 gst_pad_set_event_function (gdpdepay->srcpad,
123 GST_DEBUG_FUNCPTR (gst_gdp_depay_src_event));
124 /* our caps will always be decided by the incoming GDP caps buffers */
125 gst_pad_use_fixed_caps (gdpdepay->srcpad);
126 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->srcpad);
128 gdpdepay->adapter = gst_adapter_new ();
132 gst_gdp_depay_finalize (GObject * gobject)
136 this = GST_GDP_DEPAY (gobject);
138 gst_caps_unref (this->caps);
139 g_free (this->header);
140 gst_adapter_clear (this->adapter);
141 g_object_unref (this->adapter);
143 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (gobject));
147 gst_gdp_depay_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
152 this = GST_GDP_DEPAY (parent);
154 switch (GST_EVENT_TYPE (event)) {
155 case GST_EVENT_FLUSH_START:
156 /* forward flush start */
157 res = gst_pad_push_event (this->srcpad, event);
159 case GST_EVENT_FLUSH_STOP:
160 /* clear adapter on flush */
161 gst_adapter_clear (this->adapter);
162 /* forward flush stop */
163 res = gst_pad_push_event (this->srcpad, event);
166 /* after EOS, we don't expect to output anything anymore */
167 res = gst_pad_push_event (this->srcpad, event);
169 case GST_EVENT_SEGMENT:
171 case GST_EVENT_BUFFERSIZE:
173 /* we unref most events as we take them from the datastream */
174 gst_event_unref (event);
182 gst_gdp_depay_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
187 this = GST_GDP_DEPAY (parent);
189 switch (GST_EVENT_TYPE (event)) {
191 /* we refuse seek for now. */
192 gst_event_unref (event);
196 case GST_EVENT_NAVIGATION:
198 /* everything else is passed */
199 res = gst_pad_push_event (this->sinkpad, event);
207 gst_gdp_depay_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
210 GstFlowReturn ret = GST_FLOW_OK;
216 this = GST_GDP_DEPAY (parent);
218 /* On DISCONT, get rid of accumulated data. We assume a buffer after the
219 * DISCONT contains (part of) a new valid header, if not we error because we
221 if (GST_BUFFER_IS_DISCONT (buffer)) {
222 gst_adapter_clear (this->adapter);
223 this->state = GST_GDP_DEPAY_STATE_HEADER;
225 gst_adapter_push (this->adapter, buffer);
228 switch (this->state) {
229 case GST_GDP_DEPAY_STATE_HEADER:
233 /* collect a complete header, validate and store the header. Figure out
234 * the payload length and switch to the PAYLOAD state */
235 available = gst_adapter_available (this->adapter);
236 if (available < GST_DP_HEADER_LENGTH)
239 GST_LOG_OBJECT (this, "reading GDP header from adapter");
240 header = gst_adapter_take (this->adapter, GST_DP_HEADER_LENGTH);
241 if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header)) {
243 goto header_validate_error;
246 /* store types and payload length. Also store the header, which we need
247 * to make the payload. */
248 this->payload_length = gst_dp_header_payload_length (header);
249 this->payload_type = gst_dp_header_payload_type (header);
250 /* free previous header and store new one. */
251 g_free (this->header);
252 this->header = header;
254 GST_LOG_OBJECT (this,
255 "read GDP header, payload size %d, payload type %d, switching to state PAYLOAD",
256 this->payload_length, this->payload_type);
257 this->state = GST_GDP_DEPAY_STATE_PAYLOAD;
260 case GST_GDP_DEPAY_STATE_PAYLOAD:
262 /* in this state we wait for all the payload data to be available in the
263 * adapter. Then we switch to the state where we actually process the
265 available = gst_adapter_available (this->adapter);
266 if (available < this->payload_length)
269 /* change state based on type */
270 if (this->payload_type == GST_DP_PAYLOAD_BUFFER) {
271 GST_LOG_OBJECT (this, "switching to state BUFFER");
272 this->state = GST_GDP_DEPAY_STATE_BUFFER;
273 } else if (this->payload_type == GST_DP_PAYLOAD_CAPS) {
274 GST_LOG_OBJECT (this, "switching to state CAPS");
275 this->state = GST_GDP_DEPAY_STATE_CAPS;
276 } else if (this->payload_type >= GST_DP_PAYLOAD_EVENT_NONE) {
277 GST_LOG_OBJECT (this, "switching to state EVENT");
278 this->state = GST_GDP_DEPAY_STATE_EVENT;
283 if (this->payload_length) {
287 data = gst_adapter_map (this->adapter, this->payload_length);
288 res = gst_dp_validate_payload (GST_DP_HEADER_LENGTH, this->header,
290 gst_adapter_unmap (this->adapter);
293 goto payload_validate_error;
298 case GST_GDP_DEPAY_STATE_BUFFER:
300 /* if we receive a buffer without caps first, we error out */
304 GST_LOG_OBJECT (this, "reading GDP buffer from adapter");
305 buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, this->header);
309 /* now take the payload if there is any */
310 if (this->payload_length > 0) {
313 payload = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
314 gst_adapter_copy (this->adapter, payload, 0, this->payload_length);
315 gst_buffer_unmap (buf, payload, this->payload_length);
317 gst_adapter_flush (this->adapter, this->payload_length);
320 /* set caps and push */
321 GST_LOG_OBJECT (this, "deserialized buffer %p, pushing, timestamp %"
322 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
323 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT
324 ", size %" G_GSIZE_FORMAT ", flags 0x%x",
326 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
327 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
328 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
329 gst_buffer_get_size (buf), GST_BUFFER_FLAGS (buf));
330 ret = gst_pad_push (this->srcpad, buf);
331 if (ret != GST_FLOW_OK)
334 GST_LOG_OBJECT (this, "switching to state HEADER");
335 this->state = GST_GDP_DEPAY_STATE_HEADER;
338 case GST_GDP_DEPAY_STATE_CAPS:
342 /* take the payload of the caps */
343 GST_LOG_OBJECT (this, "reading GDP caps from adapter");
344 payload = gst_adapter_take (this->adapter, this->payload_length);
345 caps = gst_dp_caps_from_packet (GST_DP_HEADER_LENGTH, this->header,
351 GST_DEBUG_OBJECT (this, "deserialized caps %" GST_PTR_FORMAT, caps);
352 gst_caps_replace (&(this->caps), caps);
353 gst_pad_set_caps (this->srcpad, caps);
354 /* drop the creation ref we still have */
355 gst_caps_unref (caps);
357 GST_LOG_OBJECT (this, "switching to state HEADER");
358 this->state = GST_GDP_DEPAY_STATE_HEADER;
361 case GST_GDP_DEPAY_STATE_EVENT:
365 GST_LOG_OBJECT (this, "reading GDP event from adapter");
367 /* adapter doesn't like 0 length payload */
368 if (this->payload_length > 0)
369 payload = gst_adapter_take (this->adapter, this->payload_length);
372 event = gst_dp_event_from_packet (GST_DP_HEADER_LENGTH, this->header,
378 GST_DEBUG_OBJECT (this, "deserialized event %p of type %s, pushing",
379 event, gst_event_type_get_name (event->type));
380 gst_pad_push_event (this->srcpad, event);
382 GST_LOG_OBJECT (this, "switching to state HEADER");
383 this->state = GST_GDP_DEPAY_STATE_HEADER;
393 header_validate_error:
395 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
396 ("GDP packet header does not validate"));
397 ret = GST_FLOW_ERROR;
400 payload_validate_error:
402 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
403 ("GDP packet payload does not validate"));
404 ret = GST_FLOW_ERROR;
409 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
410 ("GDP packet header is of wrong type"));
411 ret = GST_FLOW_ERROR;
416 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
417 ("Received a buffer without first receiving caps"));
418 ret = GST_FLOW_NOT_NEGOTIATED;
423 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
424 ("could not create buffer from GDP packet"));
425 ret = GST_FLOW_ERROR;
430 GST_WARNING_OBJECT (this, "pushing depayloaded buffer returned %d", ret);
435 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
436 ("could not create caps from GDP packet"));
437 ret = GST_FLOW_ERROR;
442 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
443 ("could not create event from GDP packet"));
444 ret = GST_FLOW_ERROR;
449 static GstStateChangeReturn
450 gst_gdp_depay_change_state (GstElement * element, GstStateChange transition)
452 GstStateChangeReturn ret;
453 GstGDPDepay *this = GST_GDP_DEPAY (element);
455 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
457 switch (transition) {
458 case GST_STATE_CHANGE_PAUSED_TO_READY:
460 gst_caps_unref (this->caps);
463 gst_adapter_clear (this->adapter);
472 gst_gdp_depay_plugin_init (GstPlugin * plugin)
474 if (!gst_element_register (plugin, "gdpdepay", GST_RANK_NONE,