Remove GST_DEBUG_FUNCPTR where they're pointless
[platform/upstream/gstreamer.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 /* elementfactory information */
46 static const GstElementDetails gdp_depay_details =
47 GST_ELEMENT_DETAILS ("GDP Depayloader",
48     "GDP/Depayloader",
49     "Depayloads GStreamer Data Protocol buffers",
50     "Thomas Vander Stichele <thomas at apestaart dot org>");
51
52 enum
53 {
54   PROP_0,
55 };
56
57 static GstStaticPadTemplate gdp_depay_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("application/x-gdp"));
62
63 static GstStaticPadTemplate gdp_depay_src_template =
64 GST_STATIC_PAD_TEMPLATE ("src",
65     GST_PAD_SRC,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS_ANY);
68
69 GST_DEBUG_CATEGORY_STATIC (gst_gdp_depay_debug);
70 #define GST_CAT_DEFAULT gst_gdp_depay_debug
71
72 #define _do_init(x) \
73     GST_DEBUG_CATEGORY_INIT (gst_gdp_depay_debug, "gdpdepay", 0, \
74     "GDP depayloader");
75
76 GST_BOILERPLATE_FULL (GstGDPDepay, gst_gdp_depay, GstElement,
77     GST_TYPE_ELEMENT, _do_init);
78
79 static gboolean gst_gdp_depay_sink_event (GstPad * pad, GstEvent * event);
80 static gboolean gst_gdp_depay_src_event (GstPad * pad, GstEvent * event);
81
82 static GstFlowReturn gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer);
83
84 static GstStateChangeReturn gst_gdp_depay_change_state (GstElement *
85     element, GstStateChange transition);
86
87 static void gst_gdp_depay_finalize (GObject * object);
88
89 static void
90 gst_gdp_depay_base_init (gpointer g_class)
91 {
92   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
93
94   gst_element_class_set_details (element_class, &gdp_depay_details);
95
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));
100 }
101
102 static void
103 gst_gdp_depay_class_init (GstGDPDepayClass * klass)
104 {
105   GObjectClass *gobject_class;
106   GstElementClass *gstelement_class;
107
108   gobject_class = (GObjectClass *) klass;
109   gstelement_class = (GstElementClass *) klass;
110
111   gstelement_class->change_state =
112       GST_DEBUG_FUNCPTR (gst_gdp_depay_change_state);
113   gobject_class->finalize = gst_gdp_depay_finalize;
114 }
115
116 static void
117 gst_gdp_depay_init (GstGDPDepay * gdpdepay, GstGDPDepayClass * g_class)
118 {
119   gdpdepay->sinkpad =
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);
126
127   gdpdepay->srcpad =
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);
134
135   gdpdepay->adapter = gst_adapter_new ();
136 }
137
138 static void
139 gst_gdp_depay_finalize (GObject * gobject)
140 {
141   GstGDPDepay *this;
142
143   this = GST_GDP_DEPAY (gobject);
144   if (this->caps)
145     gst_caps_unref (this->caps);
146   g_free (this->header);
147   gst_adapter_clear (this->adapter);
148   g_object_unref (this->adapter);
149
150   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (gobject));
151 }
152
153 static gboolean
154 gst_gdp_depay_sink_event (GstPad * pad, GstEvent * event)
155 {
156   GstGDPDepay *this;
157   gboolean res = TRUE;
158
159   this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
160
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);
166       break;
167     case GST_EVENT_EOS:
168       /* after EOS, we don't expect to output anything anymore */
169       res = gst_pad_push_event (this->srcpad, event);
170       break;
171     case GST_EVENT_NEWSEGMENT:
172     case GST_EVENT_TAG:
173     case GST_EVENT_BUFFERSIZE:
174     default:
175       /* we unref most events as we take them from the datastream */
176       gst_event_unref (event);
177       break;
178   }
179   gst_object_unref (this);
180
181   return res;
182 }
183
184 static gboolean
185 gst_gdp_depay_src_event (GstPad * pad, GstEvent * event)
186 {
187   GstGDPDepay *this;
188   gboolean res = TRUE;
189
190   this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
191
192   switch (GST_EVENT_TYPE (event)) {
193     case GST_EVENT_SEEK:
194       /* we refuse seek for now. */
195       gst_event_unref (event);
196       res = FALSE;
197       break;
198     case GST_EVENT_QOS:
199     case GST_EVENT_NAVIGATION:
200     default:
201       /* everything else is passed */
202       res = gst_pad_push_event (this->sinkpad, event);
203       break;
204   }
205   gst_object_unref (this);
206
207   return res;
208 }
209
210 static GstFlowReturn
211 gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer)
212 {
213   GstGDPDepay *this;
214   GstFlowReturn ret = GST_FLOW_OK;
215   GstCaps *caps;
216   GstBuffer *buf;
217   GstEvent *event;
218   guint available;
219
220   this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
221
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
224    * lost sync */
225   if (GST_BUFFER_IS_DISCONT (buffer)) {
226     gst_adapter_clear (this->adapter);
227     this->state = GST_GDP_DEPAY_STATE_HEADER;
228   }
229   gst_adapter_push (this->adapter, buffer);
230
231   while (TRUE) {
232     switch (this->state) {
233       case GST_GDP_DEPAY_STATE_HEADER:
234       {
235         guint8 *header;
236
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)
241           goto done;
242
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)) {
246           g_free (header);
247           goto header_validate_error;
248         }
249
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;
257
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;
262         break;
263       }
264       case GST_GDP_DEPAY_STATE_PAYLOAD:
265       {
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
268          * payload. */
269         available = gst_adapter_available (this->adapter);
270         if (available < this->payload_length)
271           goto done;
272
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;
283         } else {
284           goto wrong_type;
285         }
286
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;
291         }
292
293         break;
294       }
295       case GST_GDP_DEPAY_STATE_BUFFER:
296       {
297
298         /* if we receive a buffer without caps first, we error out */
299         if (!this->caps)
300           goto no_caps;
301
302         GST_LOG_OBJECT (this, "reading GDP buffer from adapter");
303         buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, this->header);
304         if (!buf)
305           goto buffer_failed;
306
307         /* now take the payload if there is any */
308         if (this->payload_length > 0) {
309           guint8 *payload;
310
311           payload = gst_adapter_take (this->adapter, this->payload_length);
312           memcpy (GST_BUFFER_DATA (buf), payload, this->payload_length);
313           g_free (payload);
314         }
315
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",
322             buf,
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)
329           goto push_error;
330
331         GST_LOG_OBJECT (this, "switching to state HEADER");
332         this->state = GST_GDP_DEPAY_STATE_HEADER;
333         break;
334       }
335       case GST_GDP_DEPAY_STATE_CAPS:
336       {
337         guint8 *payload;
338
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,
343             payload);
344         g_free (payload);
345         if (!caps)
346           goto caps_failed;
347
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);
353
354         GST_LOG_OBJECT (this, "switching to state HEADER");
355         this->state = GST_GDP_DEPAY_STATE_HEADER;
356         break;
357       }
358       case GST_GDP_DEPAY_STATE_EVENT:
359       {
360         guint8 *payload;
361
362         GST_LOG_OBJECT (this, "reading GDP event from adapter");
363
364         /* adapter doesn't like 0 length payload */
365         if (this->payload_length > 0)
366           payload = gst_adapter_take (this->adapter, this->payload_length);
367         else
368           payload = NULL;
369         event = gst_dp_event_from_packet (GST_DP_HEADER_LENGTH, this->header,
370             payload);
371         g_free (payload);
372         if (!event)
373           goto event_failed;
374
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);
378
379         GST_LOG_OBJECT (this, "switching to state HEADER");
380         this->state = GST_GDP_DEPAY_STATE_HEADER;
381         break;
382       }
383     }
384   }
385
386 done:
387   gst_object_unref (this);
388   return ret;
389
390   /* ERRORS */
391 header_validate_error:
392   {
393     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
394         ("GDP packet header does not validate"));
395     ret = GST_FLOW_ERROR;
396     goto done;
397   }
398 payload_validate_error:
399   {
400     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
401         ("GDP packet payload does not validate"));
402     ret = GST_FLOW_ERROR;
403     goto done;
404   }
405 wrong_type:
406   {
407     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
408         ("GDP packet header is of wrong type"));
409     ret = GST_FLOW_ERROR;
410     goto done;
411   }
412 no_caps:
413   {
414     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
415         ("Received a buffer without first receiving caps"));
416     ret = GST_FLOW_NOT_NEGOTIATED;
417     goto done;
418   }
419 buffer_failed:
420   {
421     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
422         ("could not create buffer from GDP packet"));
423     ret = GST_FLOW_ERROR;
424     goto done;
425   }
426 push_error:
427   {
428     GST_WARNING_OBJECT (this, "pushing depayloaded buffer returned %d", ret);
429     goto done;
430   }
431 caps_failed:
432   {
433     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
434         ("could not create caps from GDP packet"));
435     ret = GST_FLOW_ERROR;
436     goto done;
437   }
438 event_failed:
439   {
440     GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
441         ("could not create event from GDP packet"));
442     ret = GST_FLOW_ERROR;
443     goto done;
444   }
445 }
446
447 static GstStateChangeReturn
448 gst_gdp_depay_change_state (GstElement * element, GstStateChange transition)
449 {
450   GstStateChangeReturn ret;
451   GstGDPDepay *this = GST_GDP_DEPAY (element);
452
453   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
454
455   switch (transition) {
456     case GST_STATE_CHANGE_PAUSED_TO_READY:
457       if (this->caps) {
458         gst_caps_unref (this->caps);
459         this->caps = NULL;
460       }
461       break;
462     default:
463       break;
464   }
465   return ret;
466 }
467
468 gboolean
469 gst_gdp_depay_plugin_init (GstPlugin * plugin)
470 {
471   if (!gst_element_register (plugin, "gdpdepay", GST_RANK_NONE,
472           GST_TYPE_GDP_DEPAY))
473     return FALSE;
474
475   return TRUE;
476 }