3 * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
4 * Copyright (C) 2002 David A. Schleef <ds@schleef.org>
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.
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.
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.
35 #define MAP_ANONYMOUS MAP_ANON
37 /* assume we don't need it */
38 #define MAP_ANONYMOUS 0
42 GST_DEBUG_CATEGORY_STATIC (gst_efence_debug);
43 #define GST_CAT_DEFAULT gst_efence_debug
45 /* Filter signals and args */
58 static GstStaticPadTemplate gst_efence_sink_factory =
59 GST_STATIC_PAD_TEMPLATE ("sink",
64 static GstStaticPadTemplate gst_efence_src_factory =
65 GST_STATIC_PAD_TEMPLATE ("src",
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);
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);
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);
85 static GstElementClass *parent_class = NULL;
87 typedef struct _GstFencedBuffer GstFencedBuffer;
88 struct _GstFencedBuffer
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,
100 static GstFlowReturn gst_efence_buffer_alloc (GstPad * pad, guint64 offset,
101 guint size, GstCaps * caps, GstBuffer ** buf);
103 #define GST_TYPE_FENCED_BUFFER (gst_fenced_buffer_get_type())
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))
109 gst_gst_efence_get_type (void)
111 static GType plugin_type = 0;
114 static const GTypeInfo plugin_info = {
115 sizeof (GstEFenceClass),
116 gst_efence_base_init,
118 (GClassInitFunc) gst_efence_class_init,
123 (GInstanceInitFunc) gst_efence_init,
126 plugin_type = g_type_register_static (GST_TYPE_ELEMENT,
127 "GstEFence", &plugin_info, 0);
133 gst_efence_base_init (gpointer g_class)
135 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
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",
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>");
150 /* initialize the plugin's class */
152 gst_efence_class_init (GstEFenceClass * klass)
154 GObjectClass *gobject_class;
156 gobject_class = (GObjectClass *) klass;
158 parent_class = g_type_class_peek_parent (klass);
160 gobject_class->set_property = gst_efence_set_property;
161 gobject_class->get_property = gst_efence_get_property;
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, G_PARAM_READWRITE));
168 /* initialize the new element
169 * instantiate pads and add them to element
171 * initialize structure
174 gst_efence_init (GstEFence * filter)
177 gst_pad_new_from_static_template (&gst_efence_sink_factory, "sink");
178 gst_pad_set_getcaps_function (filter->sinkpad,
179 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
180 gst_pad_set_setcaps_function (filter->sinkpad,
181 GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
182 gst_pad_set_chain_function (filter->sinkpad,
183 GST_DEBUG_FUNCPTR (gst_efence_chain));
184 gst_pad_set_bufferalloc_function (filter->sinkpad,
185 GST_DEBUG_FUNCPTR (gst_efence_buffer_alloc));
186 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
189 gst_pad_new_from_static_template (&gst_efence_src_factory, "src");
190 gst_pad_set_getcaps_function (filter->srcpad,
191 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
192 gst_pad_set_setcaps_function (filter->srcpad,
193 GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
194 gst_pad_set_checkgetrange_function (filter->srcpad,
195 GST_DEBUG_FUNCPTR (gst_efence_checkgetrange));
196 gst_pad_set_getrange_function (filter->srcpad,
197 GST_DEBUG_FUNCPTR (gst_efence_getrange));
198 gst_pad_set_activatepull_function (filter->srcpad,
199 GST_DEBUG_FUNCPTR (gst_efence_activate_src_pull));
201 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
203 filter->fence_top = TRUE;
207 * this function does the actual processing
211 gst_efence_chain (GstPad * pad, GstBuffer * buffer)
216 efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
217 g_return_val_if_fail (GST_IS_EFENCE (efence), GST_FLOW_ERROR);
219 if (GST_IS_FENCED_BUFFER (buffer)) {
220 GST_DEBUG_OBJECT (efence, "Passing on existing fenced buffer with caps %"
221 GST_PTR_FORMAT, GST_BUFFER_CAPS (buffer));
222 return gst_pad_push (efence->srcpad, buffer);
225 copy = (GstBuffer *) gst_fenced_buffer_copy (buffer);
227 GST_DEBUG_OBJECT (efence, "Pushing newly fenced buffer with caps %"
228 GST_PTR_FORMAT ", data=%p, size=%u", GST_BUFFER_CAPS (copy),
229 GST_BUFFER_DATA (copy), GST_BUFFER_SIZE (copy));
231 gst_buffer_unref (buffer);
233 return gst_pad_push (efence->srcpad, copy);
237 gst_efence_getrange (GstPad * pad, guint64 offset,
238 guint length, GstBuffer ** buffer)
245 efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
247 peer = gst_pad_get_peer (efence->sinkpad);
249 return GST_FLOW_NOT_LINKED;
251 if ((ret = gst_pad_get_range (peer, offset, length, buffer)) != GST_FLOW_OK)
254 ownbuf = (GstBuffer *) gst_fenced_buffer_copy (*buffer);
255 gst_buffer_unref ((GstBuffer *) * buffer);
259 gst_object_unref (peer);
264 gst_efence_checkgetrange (GstPad * pad)
266 GstEFence *efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
268 return gst_pad_check_pull_range (efence->sinkpad);
272 gst_efence_activate_src_pull (GstPad * pad, gboolean active)
274 GstEFence *efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
276 return gst_pad_activate_pull (efence->sinkpad, active);
280 gst_efence_buffer_alloc (GstPad * pad, guint64 offset,
281 guint size, GstCaps * caps, GstBuffer ** buf)
286 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
287 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
289 efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
291 buffer = (GstBuffer *) gst_mini_object_new (GST_TYPE_FENCED_BUFFER);
293 GST_BUFFER_DATA (buffer) = gst_fenced_buffer_alloc (buffer, size,
295 GST_BUFFER_SIZE (buffer) = size;
296 GST_BUFFER_OFFSET (buffer) = offset;
299 gst_buffer_set_caps (buffer, caps);
303 GST_DEBUG_OBJECT (efence, "Allocated buffer of size %u, caps: %"
304 GST_PTR_FORMAT, GST_BUFFER_SIZE (buffer), GST_BUFFER_CAPS (buffer));
310 gst_efence_set_property (GObject * object, guint prop_id,
311 const GValue * value, GParamSpec * pspec)
315 g_return_if_fail (GST_IS_EFENCE (object));
316 filter = GST_EFENCE (object);
320 filter->fence_top = g_value_get_boolean (value);
323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
329 gst_efence_get_property (GObject * object, guint prop_id,
330 GValue * value, GParamSpec * pspec)
334 g_return_if_fail (GST_IS_EFENCE (object));
335 filter = GST_EFENCE (object);
339 g_value_set_boolean (value, filter->fence_top);
342 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
347 /* entry point to initialize the plug-in
348 * initialize the plug-in itself
349 * register the element factories and pad templates
350 * register the features
353 plugin_init (GstPlugin * plugin)
355 if (!gst_element_register (plugin, "efence", GST_RANK_NONE, GST_TYPE_EFENCE))
358 GST_DEBUG_CATEGORY_INIT (gst_efence_debug, "efence", 0,
359 "Debug output from the efence element");
361 /* plugin initialisation succeeded */
365 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
368 "This element converts a stream of normal GStreamer buffers into a "
369 "stream of buffers that are allocated in such a way that out-of-bounds "
370 "access to data in the buffer is more likely to cause segmentation "
371 "faults. This allocation method is very similar to the debugging tool "
372 "\"Electric Fence\".",
373 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
376 static GstBufferClass *fenced_buffer_parent_class = NULL;
379 gst_fenced_buffer_finalize (GstFencedBuffer * buffer)
381 GstFencedBuffer *fenced_buffer;
383 GST_DEBUG ("free buffer=%p", buffer);
385 fenced_buffer = GST_FENCED_BUFFER (buffer);
388 if (GST_BUFFER_DATA (buffer)) {
389 GST_DEBUG ("free region %p %d", fenced_buffer->region,
390 fenced_buffer->length);
391 munmap (fenced_buffer->region, fenced_buffer->length);
394 GST_MINI_OBJECT_CLASS (fenced_buffer_parent_class)->finalize (GST_MINI_OBJECT
398 static GstFencedBuffer *
399 gst_fenced_buffer_copy (const GstBuffer * buffer)
405 g_return_val_if_fail (buffer != NULL, NULL);
407 /* create a fresh new buffer */
408 copy = (GstBuffer *) gst_mini_object_new (GST_TYPE_FENCED_BUFFER);
410 /* we simply copy everything from our parent */
411 ptr = gst_fenced_buffer_alloc (GST_BUFFER (copy),
412 GST_BUFFER_SIZE (buffer), TRUE);
413 memcpy (ptr, GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
415 /* copy relevant flags */
416 mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
417 GST_BUFFER_FLAG_DELTA_UNIT;
418 GST_MINI_OBJECT (copy)->flags |= GST_MINI_OBJECT (buffer)->flags & mask;
420 GST_BUFFER_DATA (copy) = ptr;
421 GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (buffer);
422 GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
423 GST_BUFFER_DURATION (copy) = GST_BUFFER_DURATION (buffer);
424 GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
425 GST_BUFFER_OFFSET_END (copy) = GST_BUFFER_OFFSET_END (buffer);
427 if (GST_BUFFER_CAPS (buffer))
428 GST_BUFFER_CAPS (copy) = gst_caps_ref (GST_BUFFER_CAPS (buffer));
430 GST_BUFFER_CAPS (copy) = NULL;
432 GST_DEBUG ("Copied buffer %p with ts %" GST_TIME_FORMAT
433 ", caps: %" GST_PTR_FORMAT, buffer,
434 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (copy)), GST_BUFFER_CAPS (copy));
436 return GST_FENCED_BUFFER (copy);
440 gst_fenced_buffer_alloc (GstBuffer * buffer, unsigned int length,
445 GstFencedBuffer *fenced_buffer = (GstFencedBuffer *) buffer;
448 GST_DEBUG ("buffer=%p length=%d fence_top=%d", buffer, length, fence_top);
454 page_size = sysconf (_SC_PAGESIZE);
456 page_size = getpagesize ();
459 /* Allocate a complete page, and one on either side */
460 alloc_size = ((length - 1) & ~(page_size - 1)) + page_size;
461 alloc_size += 2 * page_size;
463 region = mmap (NULL, alloc_size, PROT_READ | PROT_WRITE,
464 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
465 if (region == MAP_FAILED) {
466 g_warning ("mmap failed");
470 munmap (region, page_size);
471 munmap (region + alloc_size - page_size, page_size);
473 fenced_buffer->region = region + page_size;
474 fenced_buffer->length = alloc_size - page_size;
476 mprotect (region, page_size, PROT_NONE);
477 mprotect ((char *) region + alloc_size - page_size, page_size, PROT_NONE);
479 fenced_buffer->region = region;
480 fenced_buffer->length = alloc_size;
483 GST_DEBUG ("new region %p %d", fenced_buffer->region, fenced_buffer->length);
488 /* Align to top of region, but force alignment to 4 bytes */
489 offset = alloc_size - page_size - length;
491 return (void *) ((char *) region + offset);
493 return (void *) ((char *) region + page_size);
498 gst_fenced_buffer_class_init (gpointer g_class, gpointer class_data)
500 GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
502 fenced_buffer_parent_class = g_type_class_peek_parent (g_class);
504 mini_object_class->finalize =
505 (GstMiniObjectFinalizeFunction) gst_fenced_buffer_finalize;
506 mini_object_class->copy = (GstMiniObjectCopyFunction) gst_fenced_buffer_copy;
510 gst_fenced_buffer_get_type (void)
512 static GType fenced_buf_type = 0;
514 if (G_UNLIKELY (!fenced_buf_type)) {
515 static const GTypeInfo fenced_buf_info = {
516 sizeof (GstBufferClass),
519 (GClassInitFunc) gst_fenced_buffer_class_init,
522 sizeof (GstFencedBuffer),
527 fenced_buf_type = g_type_register_static (GST_TYPE_BUFFER,
528 "GstFencedBuffer", &fenced_buf_info, 0);
530 return fenced_buf_type;