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, 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_class_init (GstGDPDepayClass * klass)
85 GObjectClass *gobject_class;
86 GstElementClass *gstelement_class;
88 gobject_class = (GObjectClass *) klass;
89 gstelement_class = (GstElementClass *) klass;
91 gst_element_class_set_details_simple (gstelement_class,
92 "GDP Depayloader", "GDP/Depayloader",
93 "Depayloads GStreamer Data Protocol buffers",
94 "Thomas Vander Stichele <thomas at apestaart dot org>");
96 gst_element_class_add_pad_template (gstelement_class,
97 gst_static_pad_template_get (&gdp_depay_sink_template));
98 gst_element_class_add_pad_template (gstelement_class,
99 gst_static_pad_template_get (&gdp_depay_src_template));
101 gstelement_class->change_state =
102 GST_DEBUG_FUNCPTR (gst_gdp_depay_change_state);
103 gobject_class->finalize = gst_gdp_depay_finalize;
107 gst_gdp_depay_init (GstGDPDepay * gdpdepay)
110 gst_pad_new_from_static_template (&gdp_depay_sink_template, "sink");
111 gst_pad_set_chain_function (gdpdepay->sinkpad,
112 GST_DEBUG_FUNCPTR (gst_gdp_depay_chain));
113 gst_pad_set_event_function (gdpdepay->sinkpad,
114 GST_DEBUG_FUNCPTR (gst_gdp_depay_sink_event));
115 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->sinkpad);
118 gst_pad_new_from_static_template (&gdp_depay_src_template, "src");
119 gst_pad_set_event_function (gdpdepay->srcpad,
120 GST_DEBUG_FUNCPTR (gst_gdp_depay_src_event));
121 /* our caps will always be decided by the incoming GDP caps buffers */
122 gst_pad_use_fixed_caps (gdpdepay->srcpad);
123 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->srcpad);
125 gdpdepay->adapter = gst_adapter_new ();
129 gst_gdp_depay_finalize (GObject * gobject)
133 this = GST_GDP_DEPAY (gobject);
135 gst_caps_unref (this->caps);
136 g_free (this->header);
137 gst_adapter_clear (this->adapter);
138 g_object_unref (this->adapter);
140 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (gobject));
144 gst_gdp_depay_sink_event (GstPad * pad, GstEvent * event)
149 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
151 switch (GST_EVENT_TYPE (event)) {
152 case GST_EVENT_FLUSH_START:
153 /* forward flush start */
154 res = gst_pad_push_event (this->srcpad, event);
156 case GST_EVENT_FLUSH_STOP:
157 /* clear adapter on flush */
158 gst_adapter_clear (this->adapter);
159 /* forward flush stop */
160 res = gst_pad_push_event (this->srcpad, event);
163 /* after EOS, we don't expect to output anything anymore */
164 res = gst_pad_push_event (this->srcpad, event);
166 case GST_EVENT_SEGMENT:
168 case GST_EVENT_BUFFERSIZE:
170 /* we unref most events as we take them from the datastream */
171 gst_event_unref (event);
174 gst_object_unref (this);
180 gst_gdp_depay_src_event (GstPad * pad, GstEvent * event)
185 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
187 switch (GST_EVENT_TYPE (event)) {
189 /* we refuse seek for now. */
190 gst_event_unref (event);
194 case GST_EVENT_NAVIGATION:
196 /* everything else is passed */
197 res = gst_pad_push_event (this->sinkpad, event);
200 gst_object_unref (this);
206 gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer)
209 GstFlowReturn ret = GST_FLOW_OK;
215 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
217 /* On DISCONT, get rid of accumulated data. We assume a buffer after the
218 * DISCONT contains (part of) a new valid header, if not we error because we
220 if (GST_BUFFER_IS_DISCONT (buffer)) {
221 gst_adapter_clear (this->adapter);
222 this->state = GST_GDP_DEPAY_STATE_HEADER;
224 gst_adapter_push (this->adapter, buffer);
227 switch (this->state) {
228 case GST_GDP_DEPAY_STATE_HEADER:
232 /* collect a complete header, validate and store the header. Figure out
233 * the payload length and switch to the PAYLOAD state */
234 available = gst_adapter_available (this->adapter);
235 if (available < GST_DP_HEADER_LENGTH)
238 GST_LOG_OBJECT (this, "reading GDP header from adapter");
239 header = gst_adapter_take (this->adapter, GST_DP_HEADER_LENGTH);
240 if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header)) {
242 goto header_validate_error;
245 /* store types and payload length. Also store the header, which we need
246 * to make the payload. */
247 this->payload_length = gst_dp_header_payload_length (header);
248 this->payload_type = gst_dp_header_payload_type (header);
249 /* free previous header and store new one. */
250 g_free (this->header);
251 this->header = header;
253 GST_LOG_OBJECT (this,
254 "read GDP header, payload size %d, payload type %d, switching to state PAYLOAD",
255 this->payload_length, this->payload_type);
256 this->state = GST_GDP_DEPAY_STATE_PAYLOAD;
259 case GST_GDP_DEPAY_STATE_PAYLOAD:
261 /* in this state we wait for all the payload data to be available in the
262 * adapter. Then we switch to the state where we actually process the
264 available = gst_adapter_available (this->adapter);
265 if (available < this->payload_length)
268 /* change state based on type */
269 if (this->payload_type == GST_DP_PAYLOAD_BUFFER) {
270 GST_LOG_OBJECT (this, "switching to state BUFFER");
271 this->state = GST_GDP_DEPAY_STATE_BUFFER;
272 } else if (this->payload_type == GST_DP_PAYLOAD_CAPS) {
273 GST_LOG_OBJECT (this, "switching to state CAPS");
274 this->state = GST_GDP_DEPAY_STATE_CAPS;
275 } else if (this->payload_type >= GST_DP_PAYLOAD_EVENT_NONE) {
276 GST_LOG_OBJECT (this, "switching to state EVENT");
277 this->state = GST_GDP_DEPAY_STATE_EVENT;
282 if (this->payload_length) {
286 data = gst_adapter_map (this->adapter, this->payload_length);
287 res = gst_dp_validate_payload (GST_DP_HEADER_LENGTH, this->header,
289 gst_adapter_unmap (this->adapter, 0);
292 goto payload_validate_error;
297 case GST_GDP_DEPAY_STATE_BUFFER:
299 /* if we receive a buffer without caps first, we error out */
303 GST_LOG_OBJECT (this, "reading GDP buffer from adapter");
304 buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, this->header);
308 /* now take the payload if there is any */
309 if (this->payload_length > 0) {
312 payload = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
313 gst_adapter_copy (this->adapter, payload, 0, this->payload_length);
314 gst_buffer_unmap (buf, payload, this->payload_length);
316 gst_adapter_flush (this->adapter, this->payload_length);
319 /* set caps and push */
320 GST_LOG_OBJECT (this, "deserialized buffer %p, pushing, timestamp %"
321 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
322 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT
323 ", size %" G_GSIZE_FORMAT ", flags 0x%x",
325 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
326 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
327 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
328 gst_buffer_get_size (buf), GST_BUFFER_FLAGS (buf));
329 ret = gst_pad_push (this->srcpad, buf);
330 if (ret != GST_FLOW_OK)
333 GST_LOG_OBJECT (this, "switching to state HEADER");
334 this->state = GST_GDP_DEPAY_STATE_HEADER;
337 case GST_GDP_DEPAY_STATE_CAPS:
341 /* take the payload of the caps */
342 GST_LOG_OBJECT (this, "reading GDP caps from adapter");
343 payload = gst_adapter_take (this->adapter, this->payload_length);
344 caps = gst_dp_caps_from_packet (GST_DP_HEADER_LENGTH, this->header,
350 GST_DEBUG_OBJECT (this, "deserialized caps %" GST_PTR_FORMAT, caps);
351 gst_caps_replace (&(this->caps), caps);
352 gst_pad_set_caps (this->srcpad, caps);
353 /* drop the creation ref we still have */
354 gst_caps_unref (caps);
356 GST_LOG_OBJECT (this, "switching to state HEADER");
357 this->state = GST_GDP_DEPAY_STATE_HEADER;
360 case GST_GDP_DEPAY_STATE_EVENT:
364 GST_LOG_OBJECT (this, "reading GDP event from adapter");
366 /* adapter doesn't like 0 length payload */
367 if (this->payload_length > 0)
368 payload = gst_adapter_take (this->adapter, this->payload_length);
371 event = gst_dp_event_from_packet (GST_DP_HEADER_LENGTH, this->header,
377 GST_DEBUG_OBJECT (this, "deserialized event %p of type %s, pushing",
378 event, gst_event_type_get_name (event->type));
379 gst_pad_push_event (this->srcpad, event);
381 GST_LOG_OBJECT (this, "switching to state HEADER");
382 this->state = GST_GDP_DEPAY_STATE_HEADER;
389 gst_object_unref (this);
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,