upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.git] / gst / debugutils / efence.c
1 /*
2  * GStreamer
3  * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
4  * Copyright (C) 2002 David A. Schleef <ds@schleef.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <gst/gst.h>
26
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30
31 #include "efence.h"
32
33 #ifndef MAP_ANONYMOUS
34 #ifdef MAP_ANON
35 #define MAP_ANONYMOUS MAP_ANON
36 #else
37 /* assume we don't need it */
38 #define MAP_ANONYMOUS 0
39 #endif
40 #endif
41
42 GST_DEBUG_CATEGORY_STATIC (gst_efence_debug);
43 #define GST_CAT_DEFAULT  gst_efence_debug
44
45 /* Filter signals and args */
46 enum
47 {
48   /* FILL ME */
49   LAST_SIGNAL
50 };
51
52 enum
53 {
54   ARG_0,
55   ARG_FENCE_TOP
56 };
57
58 static GstStaticPadTemplate gst_efence_sink_factory =
59 GST_STATIC_PAD_TEMPLATE ("sink",
60     GST_PAD_SINK,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS_ANY);
63
64 static GstStaticPadTemplate gst_efence_src_factory =
65 GST_STATIC_PAD_TEMPLATE ("src",
66     GST_PAD_SRC,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS_ANY);
69
70 static void gst_efence_base_init (gpointer g_class);
71 static void gst_efence_class_init (GstEFenceClass * klass);
72 static void gst_efence_init (GstEFence * filter);
73
74 static void gst_efence_set_property (GObject * object, guint prop_id,
75     const GValue * value, GParamSpec * pspec);
76 static void gst_efence_get_property (GObject * object, guint prop_id,
77     GValue * value, GParamSpec * pspec);
78
79 static GstFlowReturn gst_efence_chain (GstPad * pad, GstBuffer * buf);
80 static GstFlowReturn gst_efence_getrange (GstPad * pad, guint64 offset,
81     guint length, GstBuffer ** buffer);
82 static gboolean gst_efence_checkgetrange (GstPad * pad);
83 static gboolean gst_efence_activate_src_pull (GstPad * pad, gboolean active);
84
85 static GstElementClass *parent_class = NULL;
86
87 typedef struct _GstFencedBuffer GstFencedBuffer;
88 struct _GstFencedBuffer
89 {
90   GstBuffer buffer;
91   void *region;
92   unsigned int length;
93 };
94
95 GType gst_fenced_buffer_get_type (void);
96 static void gst_fenced_buffer_finalize (GstFencedBuffer * buf);
97 static GstFencedBuffer *gst_fenced_buffer_copy (const GstBuffer * buffer);
98 static void *gst_fenced_buffer_alloc (GstBuffer * buffer, unsigned int length,
99     gboolean fence_top);
100 static GstFlowReturn gst_efence_buffer_alloc (GstPad * pad, guint64 offset,
101     guint size, GstCaps * caps, GstBuffer ** buf);
102
103 #define GST_TYPE_FENCED_BUFFER (gst_fenced_buffer_get_type())
104
105 #define GST_IS_FENCED_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_FENCED_BUFFER))
106 #define GST_FENCED_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_FENCED_BUFFER, GstFencedBuffer))
107
108 GType
109 gst_gst_efence_get_type (void)
110 {
111   static GType plugin_type = 0;
112
113   if (!plugin_type) {
114     static const GTypeInfo plugin_info = {
115       sizeof (GstEFenceClass),
116       gst_efence_base_init,
117       NULL,
118       (GClassInitFunc) gst_efence_class_init,
119       NULL,
120       NULL,
121       sizeof (GstEFence),
122       0,
123       (GInstanceInitFunc) gst_efence_init,
124     };
125
126     plugin_type = g_type_register_static (GST_TYPE_ELEMENT,
127         "GstEFence", &plugin_info, 0);
128   }
129   return plugin_type;
130 }
131
132 static void
133 gst_efence_base_init (gpointer g_class)
134 {
135   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
136
137   gst_element_class_add_pad_template (element_class,
138       gst_static_pad_template_get (&gst_efence_sink_factory));
139   gst_element_class_add_pad_template (element_class,
140       gst_static_pad_template_get (&gst_efence_src_factory));
141   gst_element_class_set_details_simple (element_class, "Electric Fence",
142       "Testing",
143       "This element converts a stream of normal GStreamer buffers into a "
144       "stream of buffers that are allocated in such a way that out-of-bounds "
145       "access to data in the buffer is more likely to cause segmentation "
146       "faults.  This allocation method is very similar to the debugging tool "
147       "\"Electric Fence\".", "David A. Schleef <ds@schleef.org>");
148 }
149
150 /* initialize the plugin's class */
151 static void
152 gst_efence_class_init (GstEFenceClass * klass)
153 {
154   GObjectClass *gobject_class;
155
156   gobject_class = (GObjectClass *) klass;
157
158   parent_class = g_type_class_peek_parent (klass);
159
160   gobject_class->set_property = gst_efence_set_property;
161   gobject_class->get_property = gst_efence_get_property;
162
163   g_object_class_install_property (gobject_class, ARG_FENCE_TOP,
164       g_param_spec_boolean ("fence-top", "Fence Top",
165           "Align buffers with top of fenced region", TRUE,
166           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167 }
168
169 /* initialize the new element
170  * instantiate pads and add them to element
171  * set functions
172  * initialize structure
173  */
174 static void
175 gst_efence_init (GstEFence * filter)
176 {
177   filter->sinkpad =
178       gst_pad_new_from_static_template (&gst_efence_sink_factory, "sink");
179   gst_pad_set_getcaps_function (filter->sinkpad,
180       GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
181   gst_pad_set_setcaps_function (filter->sinkpad,
182       GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
183   gst_pad_set_chain_function (filter->sinkpad,
184       GST_DEBUG_FUNCPTR (gst_efence_chain));
185   gst_pad_set_bufferalloc_function (filter->sinkpad,
186       GST_DEBUG_FUNCPTR (gst_efence_buffer_alloc));
187   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
188
189   filter->srcpad =
190       gst_pad_new_from_static_template (&gst_efence_src_factory, "src");
191   gst_pad_set_getcaps_function (filter->srcpad,
192       GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
193   gst_pad_set_setcaps_function (filter->srcpad,
194       GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
195   gst_pad_set_checkgetrange_function (filter->srcpad,
196       GST_DEBUG_FUNCPTR (gst_efence_checkgetrange));
197   gst_pad_set_getrange_function (filter->srcpad,
198       GST_DEBUG_FUNCPTR (gst_efence_getrange));
199   gst_pad_set_activatepull_function (filter->srcpad,
200       GST_DEBUG_FUNCPTR (gst_efence_activate_src_pull));
201
202   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
203
204   filter->fence_top = TRUE;
205 }
206
207 /* chain function
208  * this function does the actual processing
209  */
210
211 static GstFlowReturn
212 gst_efence_chain (GstPad * pad, GstBuffer * buffer)
213 {
214   GstEFence *efence;
215   GstBuffer *copy;
216
217   efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
218   g_return_val_if_fail (GST_IS_EFENCE (efence), GST_FLOW_ERROR);
219
220   if (GST_IS_FENCED_BUFFER (buffer)) {
221     GST_DEBUG_OBJECT (efence, "Passing on existing fenced buffer with caps %"
222         GST_PTR_FORMAT, GST_BUFFER_CAPS (buffer));
223     return gst_pad_push (efence->srcpad, buffer);
224   }
225
226   copy = (GstBuffer *) gst_fenced_buffer_copy (buffer);
227
228   GST_DEBUG_OBJECT (efence, "Pushing newly fenced buffer with caps %"
229       GST_PTR_FORMAT ", data=%p, size=%u", GST_BUFFER_CAPS (copy),
230       GST_BUFFER_DATA (copy), GST_BUFFER_SIZE (copy));
231
232   gst_buffer_unref (buffer);
233
234   return gst_pad_push (efence->srcpad, copy);
235 }
236
237 static GstFlowReturn
238 gst_efence_getrange (GstPad * pad, guint64 offset,
239     guint length, GstBuffer ** buffer)
240 {
241   GstEFence *efence;
242   GstFlowReturn ret;
243   GstBuffer *ownbuf;
244   GstPad *peer;
245
246   efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
247
248   peer = gst_pad_get_peer (efence->sinkpad);
249   if (!peer)
250     return GST_FLOW_NOT_LINKED;
251
252   if ((ret = gst_pad_get_range (peer, offset, length, buffer)) != GST_FLOW_OK)
253     goto beach;
254
255   ownbuf = (GstBuffer *) gst_fenced_buffer_copy (*buffer);
256   gst_buffer_unref ((GstBuffer *) * buffer);
257   *buffer = ownbuf;
258
259 beach:
260   gst_object_unref (peer);
261   return ret;
262 }
263
264 static gboolean
265 gst_efence_checkgetrange (GstPad * pad)
266 {
267   GstEFence *efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
268
269   return gst_pad_check_pull_range (efence->sinkpad);
270 }
271
272 static gboolean
273 gst_efence_activate_src_pull (GstPad * pad, gboolean active)
274 {
275   GstEFence *efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
276
277   return gst_pad_activate_pull (efence->sinkpad, active);
278 }
279
280 static GstFlowReturn
281 gst_efence_buffer_alloc (GstPad * pad, guint64 offset,
282     guint size, GstCaps * caps, GstBuffer ** buf)
283 {
284   GstBuffer *buffer;
285   GstEFence *efence;
286
287   g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
288   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
289
290   efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
291
292   buffer = (GstBuffer *) gst_mini_object_new (GST_TYPE_FENCED_BUFFER);
293
294   GST_BUFFER_DATA (buffer) = gst_fenced_buffer_alloc (buffer, size,
295       efence->fence_top);
296   GST_BUFFER_SIZE (buffer) = size;
297   GST_BUFFER_OFFSET (buffer) = offset;
298
299   if (caps)
300     gst_buffer_set_caps (buffer, caps);
301
302   *buf = buffer;
303
304   GST_DEBUG_OBJECT (efence, "Allocated buffer of size %u, caps: %"
305       GST_PTR_FORMAT, GST_BUFFER_SIZE (buffer), GST_BUFFER_CAPS (buffer));
306
307   return GST_FLOW_OK;
308 }
309
310 static void
311 gst_efence_set_property (GObject * object, guint prop_id,
312     const GValue * value, GParamSpec * pspec)
313 {
314   GstEFence *filter;
315
316   g_return_if_fail (GST_IS_EFENCE (object));
317   filter = GST_EFENCE (object);
318
319   switch (prop_id) {
320     case ARG_FENCE_TOP:
321       filter->fence_top = g_value_get_boolean (value);
322       break;
323     default:
324       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
325       break;
326   }
327 }
328
329 static void
330 gst_efence_get_property (GObject * object, guint prop_id,
331     GValue * value, GParamSpec * pspec)
332 {
333   GstEFence *filter;
334
335   g_return_if_fail (GST_IS_EFENCE (object));
336   filter = GST_EFENCE (object);
337
338   switch (prop_id) {
339     case ARG_FENCE_TOP:
340       g_value_set_boolean (value, filter->fence_top);
341       break;
342     default:
343       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344       break;
345   }
346 }
347
348 /* entry point to initialize the plug-in
349  * initialize the plug-in itself
350  * register the element factories and pad templates
351  * register the features
352  */
353 static gboolean
354 plugin_init (GstPlugin * plugin)
355 {
356   if (!gst_element_register (plugin, "efence", GST_RANK_NONE, GST_TYPE_EFENCE))
357     return FALSE;
358
359   GST_DEBUG_CATEGORY_INIT (gst_efence_debug, "efence", 0,
360       "Debug output from the efence element");
361
362   /* plugin initialisation succeeded */
363   return TRUE;
364 }
365
366 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
367     GST_VERSION_MINOR,
368     "efence",
369     "This element converts a stream of normal GStreamer buffers into a "
370     "stream of buffers that are allocated in such a way that out-of-bounds "
371     "access to data in the buffer is more likely to cause segmentation "
372     "faults.  This allocation method is very similar to the debugging tool "
373     "\"Electric Fence\".",
374     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
375
376
377 static GstBufferClass *fenced_buffer_parent_class = NULL;
378
379 static void
380 gst_fenced_buffer_finalize (GstFencedBuffer * buffer)
381 {
382   GstFencedBuffer *fenced_buffer;
383
384   GST_DEBUG ("free buffer=%p", buffer);
385
386   fenced_buffer = GST_FENCED_BUFFER (buffer);
387
388   /* free our data */
389   if (GST_BUFFER_DATA (buffer)) {
390     GST_DEBUG ("free region %p %d", fenced_buffer->region,
391         fenced_buffer->length);
392     munmap (fenced_buffer->region, fenced_buffer->length);
393   }
394
395   GST_MINI_OBJECT_CLASS (fenced_buffer_parent_class)->finalize (GST_MINI_OBJECT
396       (buffer));
397 }
398
399 static GstFencedBuffer *
400 gst_fenced_buffer_copy (const GstBuffer * buffer)
401 {
402   GstBuffer *copy;
403   void *ptr;
404   guint mask;
405
406   g_return_val_if_fail (buffer != NULL, NULL);
407
408   /* create a fresh new buffer */
409   copy = (GstBuffer *) gst_mini_object_new (GST_TYPE_FENCED_BUFFER);
410
411   /* we simply copy everything from our parent */
412   ptr = gst_fenced_buffer_alloc (GST_BUFFER (copy),
413       GST_BUFFER_SIZE (buffer), TRUE);
414   memcpy (ptr, GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
415
416   /* copy relevant flags */
417   mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
418       GST_BUFFER_FLAG_DELTA_UNIT;
419   GST_MINI_OBJECT (copy)->flags |= GST_MINI_OBJECT (buffer)->flags & mask;
420
421   GST_BUFFER_DATA (copy) = ptr;
422   GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (buffer);
423   GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
424   GST_BUFFER_DURATION (copy) = GST_BUFFER_DURATION (buffer);
425   GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
426   GST_BUFFER_OFFSET_END (copy) = GST_BUFFER_OFFSET_END (buffer);
427
428   if (GST_BUFFER_CAPS (buffer))
429     GST_BUFFER_CAPS (copy) = gst_caps_ref (GST_BUFFER_CAPS (buffer));
430   else
431     GST_BUFFER_CAPS (copy) = NULL;
432
433   GST_DEBUG ("Copied buffer %p with ts %" GST_TIME_FORMAT
434       ", caps: %" GST_PTR_FORMAT, buffer,
435       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (copy)), GST_BUFFER_CAPS (copy));
436
437   return GST_FENCED_BUFFER (copy);
438 }
439
440 void *
441 gst_fenced_buffer_alloc (GstBuffer * buffer, unsigned int length,
442     gboolean fence_top)
443 {
444   int alloc_size;
445   void *region;
446   GstFencedBuffer *fenced_buffer = (GstFencedBuffer *) buffer;
447   int page_size;
448
449   GST_DEBUG ("buffer=%p length=%d fence_top=%d", buffer, length, fence_top);
450
451   if (length == 0)
452     return NULL;
453
454 #ifdef _SC_PAGESIZE
455   page_size = sysconf (_SC_PAGESIZE);
456 #else
457   page_size = getpagesize ();
458 #endif
459
460   /* Allocate a complete page, and one on either side */
461   alloc_size = ((length - 1) & ~(page_size - 1)) + page_size;
462   alloc_size += 2 * page_size;
463
464   region = mmap (NULL, alloc_size, PROT_READ | PROT_WRITE,
465       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
466   if (region == MAP_FAILED) {
467     g_warning ("mmap failed");
468     return NULL;
469   }
470 #if 0
471   munmap (region, page_size);
472   munmap (region + alloc_size - page_size, page_size);
473
474   fenced_buffer->region = region + page_size;
475   fenced_buffer->length = alloc_size - page_size;
476 #else
477   mprotect (region, page_size, PROT_NONE);
478   mprotect ((char *) region + alloc_size - page_size, page_size, PROT_NONE);
479
480   fenced_buffer->region = region;
481   fenced_buffer->length = alloc_size;
482 #endif
483
484   GST_DEBUG ("new region %p %d", fenced_buffer->region, fenced_buffer->length);
485
486   if (fence_top) {
487     int offset;
488
489     /* Align to top of region, but force alignment to 4 bytes */
490     offset = alloc_size - page_size - length;
491     offset &= ~0x3;
492     return (void *) ((char *) region + offset);
493   } else {
494     return (void *) ((char *) region + page_size);
495   }
496 }
497
498 static void
499 gst_fenced_buffer_class_init (gpointer g_class, gpointer class_data)
500 {
501   GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
502
503   fenced_buffer_parent_class = g_type_class_peek_parent (g_class);
504
505   mini_object_class->finalize =
506       (GstMiniObjectFinalizeFunction) gst_fenced_buffer_finalize;
507   mini_object_class->copy = (GstMiniObjectCopyFunction) gst_fenced_buffer_copy;
508 }
509
510 GType
511 gst_fenced_buffer_get_type (void)
512 {
513   static GType fenced_buf_type = 0;
514
515   if (G_UNLIKELY (!fenced_buf_type)) {
516     static const GTypeInfo fenced_buf_info = {
517       sizeof (GstBufferClass),
518       NULL,
519       NULL,
520       (GClassInitFunc) gst_fenced_buffer_class_init,
521       NULL,
522       NULL,
523       sizeof (GstFencedBuffer),
524       0,
525       NULL,
526     };
527
528     fenced_buf_type = g_type_register_static (GST_TYPE_BUFFER,
529         "GstFencedBuffer", &fenced_buf_info, 0);
530   }
531   return fenced_buf_type;
532 }