tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.git] / gst / gdp / gstgdpdepay.c
1 /* GStreamer
2  * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /**
21  * SECTION:element-gdpdepay
22  * @see_also: gdppay
23  *
24  * This element depayloads GStreamer Data Protocol buffers back to deserialized
25  * buffers and events.
26  *
27  * <refsect2>
28  * |[
29  * gst-launch -v -m filesrc location=test.gdp ! gdpdepay ! xvimagesink
30  * ]| This pipeline plays back a serialized video stream as created in the
31  * example for gdppay.
32  * </refsect2>
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <string.h>
40
41 #include <gst/dataprotocol/dataprotocol.h>
42
43 #include "gstgdpdepay.h"
44
45 enum
46 {
47   PROP_0,
48 };
49
50 static GstStaticPadTemplate gdp_depay_sink_template =
51 GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("application/x-gdp"));
55
56 static GstStaticPadTemplate gdp_depay_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS_ANY);
61
62 GST_DEBUG_CATEGORY_STATIC (gst_gdp_depay_debug);
63 #define GST_CAT_DEFAULT gst_gdp_depay_debug
64
65 #define _do_init(x) \
66     GST_DEBUG_CATEGORY_INIT (gst_gdp_depay_debug, "gdpdepay", 0, \
67     "GDP depayloader");
68
69 GST_BOILERPLATE_FULL (GstGDPDepay, gst_gdp_depay, GstElement,
70     GST_TYPE_ELEMENT, _do_init);
71
72 static gboolean gst_gdp_depay_sink_event (GstPad * pad, GstEvent * event);
73 static gboolean gst_gdp_depay_src_event (GstPad * pad, GstEvent * event);
74
75 static GstFlowReturn gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer);
76
77 static GstStateChangeReturn gst_gdp_depay_change_state (GstElement *
78     element, GstStateChange transition);
79
80 static void gst_gdp_depay_finalize (GObject * object);
81
82 static void
83 gst_gdp_depay_base_init (gpointer g_class)
84 {
85   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
86
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>");
91
92   gst_element_class_add_static_pad_template (element_class,
93       &gdp_depay_sink_template);
94   gst_element_class_add_static_pad_template (element_class,
95       &gdp_depay_src_template);
96 }
97
98 static void
99 gst_gdp_depay_class_init (GstGDPDepayClass * klass)
100 {
101   GObjectClass *gobject_class;
102   GstElementClass *gstelement_class;
103
104   gobject_class = (GObjectClass *) klass;
105   gstelement_class = (GstElementClass *) klass;
106
107   gstelement_class->change_state =
108       GST_DEBUG_FUNCPTR (gst_gdp_depay_change_state);
109   gobject_class->finalize = gst_gdp_depay_finalize;
110 }
111
112 static void
113 gst_gdp_depay_init (GstGDPDepay * gdpdepay, GstGDPDepayClass * g_class)
114 {
115   gdpdepay->sinkpad =
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);
122
123   gdpdepay->srcpad =
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);
130
131   gdpdepay->adapter = gst_adapter_new ();
132 }
133
134 static void
135 gst_gdp_depay_finalize (GObject * gobject)
136 {
137   GstGDPDepay *this;
138
139   this = GST_GDP_DEPAY (gobject);
140   if (this->caps)
141     gst_caps_unref (this->caps);
142   g_free (this->header);
143   gst_adapter_clear (this->adapter);
144   g_object_unref (this->adapter);
145
146   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (gobject));
147 }
148
149 static gboolean
150 gst_gdp_depay_sink_event (GstPad * pad, GstEvent * event)
151 {
152   GstGDPDepay *this;
153   gboolean res = TRUE;
154
155   this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
156
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);
161       break;
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);
167       break;
168     case GST_EVENT_EOS:
169       /* after EOS, we don't expect to output anything anymore */
170       res = gst_pad_push_event (this->srcpad, event);
171       break;
172     case GST_EVENT_NEWSEGMENT:
173     case GST_EVENT_TAG:
174     case GST_EVENT_BUFFERSIZE:
175     default:
176       /* we unref most events as we take them from the datastream */
177       gst_event_unref (event);
178       break;
179   }
180   gst_object_unref (this);
181
182   return res;
183 }
184
185 static gboolean
186 gst_gdp_depay_src_event (GstPad * pad, GstEvent * event)
187 {
188   GstGDPDepay *this;
189   gboolean res = TRUE;
190
191   this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
192
193   switch (GST_EVENT_TYPE (event)) {
194     case GST_EVENT_SEEK:
195       /* we refuse seek for now. */
196       gst_event_unref (event);
197       res = FALSE;
198       break;
199     case GST_EVENT_QOS:
200     case GST_EVENT_NAVIGATION:
201     default:
202       /* everything else is passed */
203       res = gst_pad_push_event (this->sinkpad, event);
204       break;
205   }
206   gst_object_unref (this);
207
208   return res;
209 }
210
211 static GstFlowReturn
212 gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer)
213 {
214   GstGDPDepay *this;
215   GstFlowReturn ret = GST_FLOW_OK;
216   GstCaps *caps;
217   GstBuffer *buf;
218   GstEvent *event;
219   guint available;
220
221   this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
222
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
225    * lost sync */
226   if (GST_BUFFER_IS_DISCONT (buffer)) {
227     gst_adapter_clear (this->adapter);
228     this->state = GST_GDP_DEPAY_STATE_HEADER;
229   }
230   gst_adapter_push (this->adapter, buffer);
231
232   while (TRUE) {
233     switch (this->state) {
234       case GST_GDP_DEPAY_STATE_HEADER:
235       {
236         guint8 *header;
237
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)
242           goto done;
243
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)) {
247           g_free (header);
248           goto header_validate_error;
249         }
250
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;
258
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;
263         break;
264       }
265       case GST_GDP_DEPAY_STATE_PAYLOAD:
266       {
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
269          * payload. */
270         available = gst_adapter_available (this->adapter);
271         if (available < this->payload_length)
272           goto done;
273
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;
284         } else {
285           goto wrong_type;
286         }
287
288         if (this->payload_length
289             && (!gst_dp_validate_payload (GST_DP_HEADER_LENGTH, this->header,
290                     gst_adapter_peek (this->adapter, this->payload_length)))) {
291           goto payload_validate_error;
292         }
293
294         break;
295       }
296       case GST_GDP_DEPAY_STATE_BUFFER:
297       {
298
299         /* if we receive a buffer without caps first, we error out */
300         if (!this->caps)
301           goto no_caps;
302
303         GST_LOG_OBJECT (this, "reading GDP buffer from adapter");
304         buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, this->header);
305         if (!buf)
306           goto buffer_failed;
307
308         /* now take the payload if there is any */
309         if (this->payload_length > 0) {
310           guint8 *payload;
311
312           payload = gst_adapter_take (this->adapter, this->payload_length);
313           memcpy (GST_BUFFER_DATA (buf), payload, this->payload_length);
314           g_free (payload);
315         }
316
317         /* set caps and push */
318         gst_buffer_set_caps (buf, this->caps);
319         GST_LOG_OBJECT (this, "deserialized buffer %p, pushing, timestamp %"
320             GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
321             ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT
322             ", size %d, flags 0x%x",
323             buf,
324             GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
325             GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
326             GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
327             GST_BUFFER_SIZE (buf), GST_BUFFER_FLAGS (buf));
328         ret = gst_pad_push (this->srcpad, buf);
329         if (ret != GST_FLOW_OK)
330           goto push_error;
331
332         GST_LOG_OBJECT (this, "switching to state HEADER");
333         this->state = GST_GDP_DEPAY_STATE_HEADER;
334         break;
335       }
336       case GST_GDP_DEPAY_STATE_CAPS:
337       {
338         guint8 *payload;
339
340         /* take the payload of the caps */
341         GST_LOG_OBJECT (this, "reading GDP caps from adapter");
342         payload = gst_adapter_take (this->adapter, this->payload_length);
343         caps = gst_dp_caps_from_packet (GST_DP_HEADER_LENGTH, this->header,
344             payload);
345         g_free (payload);
346         if (!caps)
347           goto caps_failed;
348
349         GST_DEBUG_OBJECT (this, "deserialized caps %" GST_PTR_FORMAT, caps);
350         gst_caps_replace (&(this->caps), caps);
351         gst_pad_set_caps (this->srcpad, caps);
352         /* drop the creation ref we still have */
353         gst_caps_unref (caps);
354
355         GST_LOG_OBJECT (this, "switching to state HEADER");
356         this->state = GST_GDP_DEPAY_STATE_HEADER;
357         break;
358       }
359       case GST_GDP_DEPAY_STATE_EVENT:
360       {
361         guint8 *payload;
362
363         GST_LOG_OBJECT (this, "reading GDP event from adapter");
364
365         /* adapter doesn't like 0 length payload */
366         if (this->payload_length > 0)
367           payload = gst_adapter_take (this->adapter, this->payload_length);
368         else
369           payload = NULL;
370         event = gst_dp_event_from_packet (GST_DP_HEADER_LENGTH, this->header,
371             payload);
372         g_free (payload);
373         if (!event)
374           goto event_failed;
375
376         GST_DEBUG_OBJECT (this, "deserialized event %p of type %s, pushing",
377             event, gst_event_type_get_name (event->type));
378         gst_pad_push_event (this->srcpad, event);
379
380         GST_LOG_OBJECT (this, "switching to state HEADER");
381         this->state = GST_GDP_DEPAY_STATE_HEADER;
382         break;
383       }
384     }
385   }
386
387 done:
388   gst_object_unref (this);
389   return ret;
390
391   /* ERRORS */
392 header_validate_error:
393   {
394     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
395         ("GDP packet header does not validate"));
396     ret = GST_FLOW_ERROR;
397     goto done;
398   }
399 payload_validate_error:
400   {
401     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
402         ("GDP packet payload does not validate"));
403     ret = GST_FLOW_ERROR;
404     goto done;
405   }
406 wrong_type:
407   {
408     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
409         ("GDP packet header is of wrong type"));
410     ret = GST_FLOW_ERROR;
411     goto done;
412   }
413 no_caps:
414   {
415     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
416         ("Received a buffer without first receiving caps"));
417     ret = GST_FLOW_NOT_NEGOTIATED;
418     goto done;
419   }
420 buffer_failed:
421   {
422     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
423         ("could not create buffer from GDP packet"));
424     ret = GST_FLOW_ERROR;
425     goto done;
426   }
427 push_error:
428   {
429     GST_WARNING_OBJECT (this, "pushing depayloaded buffer returned %d", ret);
430     goto done;
431   }
432 caps_failed:
433   {
434     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
435         ("could not create caps from GDP packet"));
436     ret = GST_FLOW_ERROR;
437     goto done;
438   }
439 event_failed:
440   {
441     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
442         ("could not create event from GDP packet"));
443     ret = GST_FLOW_ERROR;
444     goto done;
445   }
446 }
447
448 static GstStateChangeReturn
449 gst_gdp_depay_change_state (GstElement * element, GstStateChange transition)
450 {
451   GstStateChangeReturn ret;
452   GstGDPDepay *this = GST_GDP_DEPAY (element);
453
454   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
455
456   switch (transition) {
457     case GST_STATE_CHANGE_PAUSED_TO_READY:
458       if (this->caps) {
459         gst_caps_unref (this->caps);
460         this->caps = NULL;
461       }
462       gst_adapter_clear (this->adapter);
463       break;
464     default:
465       break;
466   }
467   return ret;
468 }
469
470 gboolean
471 gst_gdp_depay_plugin_init (GstPlugin * plugin)
472 {
473   if (!gst_element_register (plugin, "gdpdepay", GST_RANK_NONE,
474           GST_TYPE_GDP_DEPAY))
475     return FALSE;
476
477   return TRUE;
478 }