add parent to query function
[platform/upstream/gst-plugins-good.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 _GstMetaFenced
88 {
89   GstMeta meta;
90
91   void *region;
92   unsigned int length;
93 } GstMetaFenced;
94
95 static const GstMetaInfo *
96 gst_meta_fenced_get_info (void)
97 {
98   static const GstMetaInfo *meta_fenced_info = NULL;
99
100   if (meta_fenced_info == NULL) {
101     meta_fenced_info = gst_meta_register ("GstMetaFenced", "GstMetaFenced",
102         sizeof (GstMetaFenced),
103         (GstMetaInitFunction) NULL,
104         (GstMetaFreeFunction) NULL,
105         (GstMetaCopyFunction) NULL, (GstMetaTransformFunction) NULL);
106   }
107   return meta_fenced_info;
108 }
109
110 #define GST_META_FENCED_GET(buf) ((GstMetaFenced *)gst_buffer_get_meta(buf,gst_meta_fenced_get_info()))
111 #define GST_META_FENCED_ADD(buf) ((GstMetaFenced *)gst_buffer_add_meta(buf,gst_meta_fenced_get_info(),NULL))
112
113 static void gst_fenced_buffer_dispose (GstBuffer * buf);
114 static GstBuffer *gst_fenced_buffer_copy (const GstBuffer * buffer);
115 static void *gst_fenced_buffer_alloc (GstBuffer * buffer, unsigned int length,
116     gboolean fence_top);
117 #if 0
118 static GstFlowReturn gst_efence_buffer_alloc (GstPad * pad, guint64 offset,
119     guint size, GstCaps * caps, GstBuffer ** buf);
120 #endif
121
122 GType
123 gst_gst_efence_get_type (void)
124 {
125   static GType plugin_type = 0;
126
127   if (!plugin_type) {
128     static const GTypeInfo plugin_info = {
129       sizeof (GstEFenceClass),
130       gst_efence_base_init,
131       NULL,
132       (GClassInitFunc) gst_efence_class_init,
133       NULL,
134       NULL,
135       sizeof (GstEFence),
136       0,
137       (GInstanceInitFunc) gst_efence_init,
138     };
139
140     plugin_type = g_type_register_static (GST_TYPE_ELEMENT,
141         "GstEFence", &plugin_info, 0);
142   }
143   return plugin_type;
144 }
145
146 static void
147 gst_efence_base_init (gpointer g_class)
148 {
149   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
150
151   gst_element_class_add_pad_template (element_class,
152       gst_static_pad_template_get (&gst_efence_sink_factory));
153   gst_element_class_add_pad_template (element_class,
154       gst_static_pad_template_get (&gst_efence_src_factory));
155   gst_element_class_set_details_simple (element_class, "Electric Fence",
156       "Testing",
157       "This element converts a stream of normal GStreamer buffers into a "
158       "stream of buffers that are allocated in such a way that out-of-bounds "
159       "access to data in the buffer is more likely to cause segmentation "
160       "faults.  This allocation method is very similar to the debugging tool "
161       "\"Electric Fence\".", "David A. Schleef <ds@schleef.org>");
162 }
163
164 /* initialize the plugin's class */
165 static void
166 gst_efence_class_init (GstEFenceClass * klass)
167 {
168   GObjectClass *gobject_class;
169
170   gobject_class = (GObjectClass *) klass;
171
172   parent_class = g_type_class_peek_parent (klass);
173
174   gobject_class->set_property = gst_efence_set_property;
175   gobject_class->get_property = gst_efence_get_property;
176
177   g_object_class_install_property (gobject_class, ARG_FENCE_TOP,
178       g_param_spec_boolean ("fence-top", "Fence Top",
179           "Align buffers with top of fenced region", TRUE,
180           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
181 }
182
183 /* initialize the new element
184  * instantiate pads and add them to element
185  * set functions
186  * initialize structure
187  */
188 static void
189 gst_efence_init (GstEFence * filter)
190 {
191   filter->sinkpad =
192       gst_pad_new_from_static_template (&gst_efence_sink_factory, "sink");
193   gst_pad_set_getcaps_function (filter->sinkpad,
194       GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
195   gst_pad_set_setcaps_function (filter->sinkpad,
196       GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
197   gst_pad_set_chain_function (filter->sinkpad,
198       GST_DEBUG_FUNCPTR (gst_efence_chain));
199 #if 0
200   gst_pad_set_bufferalloc_function (filter->sinkpad,
201       GST_DEBUG_FUNCPTR (gst_efence_buffer_alloc));
202 #endif
203   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
204
205   filter->srcpad =
206       gst_pad_new_from_static_template (&gst_efence_src_factory, "src");
207   gst_pad_set_getcaps_function (filter->srcpad,
208       GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
209   gst_pad_set_setcaps_function (filter->srcpad,
210       GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
211   gst_pad_set_checkgetrange_function (filter->srcpad,
212       GST_DEBUG_FUNCPTR (gst_efence_checkgetrange));
213   gst_pad_set_getrange_function (filter->srcpad,
214       GST_DEBUG_FUNCPTR (gst_efence_getrange));
215   gst_pad_set_activatepull_function (filter->srcpad,
216       GST_DEBUG_FUNCPTR (gst_efence_activate_src_pull));
217
218   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
219
220   filter->fence_top = TRUE;
221 }
222
223 /* chain function
224  * this function does the actual processing
225  */
226
227 static GstFlowReturn
228 gst_efence_chain (GstPad * pad, GstBuffer * buffer)
229 {
230   GstEFence *efence;
231   GstBuffer *copy;
232
233   efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
234   g_return_val_if_fail (GST_IS_EFENCE (efence), GST_FLOW_ERROR);
235
236 #if 0
237   if (GST_IS_FENCED_BUFFER (buffer)) {
238     GST_DEBUG_OBJECT (efence, "Passing on existing fenced buffer with caps %"
239         GST_PTR_FORMAT, GST_BUFFER_CAPS (buffer));
240     return gst_pad_push (efence->srcpad, buffer);
241   }
242 #endif
243
244   copy = (GstBuffer *) gst_fenced_buffer_copy (buffer);
245
246   GST_DEBUG_OBJECT (efence, "Pushing newly fenced buffer with caps %"
247       GST_PTR_FORMAT ", data=%p, size=%u", GST_BUFFER_CAPS (copy),
248       GST_BUFFER_DATA (copy), GST_BUFFER_SIZE (copy));
249
250   gst_buffer_unref (buffer);
251
252   return gst_pad_push (efence->srcpad, copy);
253 }
254
255 static GstFlowReturn
256 gst_efence_getrange (GstPad * pad, guint64 offset,
257     guint length, GstBuffer ** buffer)
258 {
259   GstEFence *efence;
260   GstFlowReturn ret;
261   GstBuffer *ownbuf;
262   GstPad *peer;
263
264   efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
265
266   peer = gst_pad_get_peer (efence->sinkpad);
267   if (!peer)
268     return GST_FLOW_NOT_LINKED;
269
270   if ((ret = gst_pad_get_range (peer, offset, length, buffer)) != GST_FLOW_OK)
271     goto beach;
272
273   ownbuf = (GstBuffer *) gst_fenced_buffer_copy (*buffer);
274   gst_buffer_unref ((GstBuffer *) * buffer);
275   *buffer = ownbuf;
276
277 beach:
278   gst_object_unref (peer);
279   return ret;
280 }
281
282 static gboolean
283 gst_efence_checkgetrange (GstPad * pad)
284 {
285   GstEFence *efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
286
287   return gst_pad_check_pull_range (efence->sinkpad);
288 }
289
290 static gboolean
291 gst_efence_activate_src_pull (GstPad * pad, gboolean active)
292 {
293   GstEFence *efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
294
295   return gst_pad_activate_pull (efence->sinkpad, active);
296 }
297
298 #if 0
299 static GstFlowReturn
300 gst_efence_buffer_alloc (GstPad * pad, guint64 offset,
301     guint size, GstCaps * caps, GstBuffer ** buf)
302 {
303   GstBuffer *buffer;
304   GstEFence *efence;
305
306   g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
307   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
308
309   efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
310
311   buffer = (GstBuffer *) gst_mini_object_new (GST_TYPE_FENCED_BUFFER);
312
313   GST_BUFFER_DATA (buffer) = gst_fenced_buffer_alloc (buffer, size,
314       efence->fence_top);
315   GST_BUFFER_SIZE (buffer) = size;
316   GST_BUFFER_OFFSET (buffer) = offset;
317
318   if (caps)
319     gst_buffer_set_caps (buffer, caps);
320
321   *buf = buffer;
322
323   GST_DEBUG_OBJECT (efence, "Allocated buffer of size %u, caps: %"
324       GST_PTR_FORMAT, GST_BUFFER_SIZE (buffer), GST_BUFFER_CAPS (buffer));
325
326   return GST_FLOW_OK;
327 }
328 #endif
329
330 static void
331 gst_efence_set_property (GObject * object, guint prop_id,
332     const GValue * value, GParamSpec * pspec)
333 {
334   GstEFence *filter;
335
336   g_return_if_fail (GST_IS_EFENCE (object));
337   filter = GST_EFENCE (object);
338
339   switch (prop_id) {
340     case ARG_FENCE_TOP:
341       filter->fence_top = g_value_get_boolean (value);
342       break;
343     default:
344       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
345       break;
346   }
347 }
348
349 static void
350 gst_efence_get_property (GObject * object, guint prop_id,
351     GValue * value, GParamSpec * pspec)
352 {
353   GstEFence *filter;
354
355   g_return_if_fail (GST_IS_EFENCE (object));
356   filter = GST_EFENCE (object);
357
358   switch (prop_id) {
359     case ARG_FENCE_TOP:
360       g_value_set_boolean (value, filter->fence_top);
361       break;
362     default:
363       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
364       break;
365   }
366 }
367
368 /* entry point to initialize the plug-in
369  * initialize the plug-in itself
370  * register the element factories and pad templates
371  * register the features
372  */
373 static gboolean
374 plugin_init (GstPlugin * plugin)
375 {
376   if (!gst_element_register (plugin, "efence", GST_RANK_NONE, GST_TYPE_EFENCE))
377     return FALSE;
378
379   GST_DEBUG_CATEGORY_INIT (gst_efence_debug, "efence", 0,
380       "Debug output from the efence element");
381
382   /* plugin initialisation succeeded */
383   return TRUE;
384 }
385
386 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
387     GST_VERSION_MINOR,
388     "efence",
389     "This element converts a stream of normal GStreamer buffers into a "
390     "stream of buffers that are allocated in such a way that out-of-bounds "
391     "access to data in the buffer is more likely to cause segmentation "
392     "faults.  This allocation method is very similar to the debugging tool "
393     "\"Electric Fence\".",
394     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
395
396
397 static void
398 gst_fenced_buffer_dispose (GstBuffer * buffer)
399 {
400   GstMetaFenced *meta;
401
402   meta = GST_META_FENCED_GET (buffer);
403
404   GST_DEBUG ("free buffer=%p", buffer);
405
406   /* free our data */
407   if (GST_BUFFER_DATA (buffer)) {
408     GST_DEBUG ("free region %p %d", meta->region, meta->length);
409     munmap (meta->region, meta->length);
410   }
411 }
412
413 static GstBuffer *
414 gst_fenced_buffer_copy (const GstBuffer * buffer)
415 {
416   GstBuffer *copy;
417   void *ptr;
418   guint mask;
419
420   g_return_val_if_fail (buffer != NULL, NULL);
421
422   /* create a fresh new buffer */
423   copy = gst_buffer_new ();
424
425   /* we simply copy everything from our parent */
426   ptr = gst_fenced_buffer_alloc (copy, GST_BUFFER_SIZE (buffer), TRUE);
427   memcpy (ptr, GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
428
429   /* copy relevant flags */
430   mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
431       GST_BUFFER_FLAG_DELTA_UNIT;
432   GST_MINI_OBJECT (copy)->flags |= GST_MINI_OBJECT (buffer)->flags & mask;
433
434   GST_BUFFER_DATA (copy) = ptr;
435   GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (buffer);
436   GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
437   GST_BUFFER_DURATION (copy) = GST_BUFFER_DURATION (buffer);
438   GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
439   GST_BUFFER_OFFSET_END (copy) = GST_BUFFER_OFFSET_END (buffer);
440
441   if (GST_BUFFER_CAPS (buffer))
442     GST_BUFFER_CAPS (copy) = gst_caps_ref (GST_BUFFER_CAPS (buffer));
443   else
444     GST_BUFFER_CAPS (copy) = NULL;
445
446   GST_DEBUG ("Copied buffer %p with ts %" GST_TIME_FORMAT
447       ", caps: %" GST_PTR_FORMAT, buffer,
448       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (copy)), GST_BUFFER_CAPS (copy));
449
450   return copy;
451 }
452
453 void *
454 gst_fenced_buffer_alloc (GstBuffer * buffer, unsigned int length,
455     gboolean fence_top)
456 {
457   int alloc_size;
458   void *region;
459   GstMetaFenced *meta;
460   int page_size;
461
462   GST_DEBUG ("buffer=%p length=%d fence_top=%d", buffer, length, fence_top);
463
464   if (length == 0)
465     return NULL;
466
467 #ifdef _SC_PAGESIZE
468   page_size = sysconf (_SC_PAGESIZE);
469 #else
470   page_size = getpagesize ();
471 #endif
472
473   /* Allocate a complete page, and one on either side */
474   alloc_size = ((length - 1) & ~(page_size - 1)) + page_size;
475   alloc_size += 2 * page_size;
476
477   region = mmap (NULL, alloc_size, PROT_READ | PROT_WRITE,
478       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
479   if (region == MAP_FAILED) {
480     g_warning ("mmap failed");
481     return NULL;
482   }
483
484   GST_MINI_OBJECT_CAST (buffer)->dispose =
485       (GstMiniObjectDisposeFunction) gst_fenced_buffer_dispose;
486   GST_MINI_OBJECT_CAST (buffer)->copy =
487       (GstMiniObjectCopyFunction) gst_fenced_buffer_copy;
488
489   meta = GST_META_FENCED_ADD (buffer);
490
491 #if 0
492   munmap (region, page_size);
493   munmap (region + alloc_size - page_size, page_size);
494
495   meta->region = region + page_size;
496   meta->length = alloc_size - page_size;
497 #else
498   mprotect (region, page_size, PROT_NONE);
499   mprotect ((char *) region + alloc_size - page_size, page_size, PROT_NONE);
500
501   meta->region = region;
502   meta->length = alloc_size;
503 #endif
504
505   GST_DEBUG ("new region %p %d", meta->region, meta->length);
506
507   if (fence_top) {
508     int offset;
509
510     /* Align to top of region, but force alignment to 4 bytes */
511     offset = alloc_size - page_size - length;
512     offset &= ~0x3;
513     return (void *) ((char *) region + offset);
514   } else {
515     return (void *) ((char *) region + page_size);
516   }
517 }