[gstreamermm] Gst::Allocator: wrapped GstAllocator struct.



commit 3337da70bf962be64b3c5237c7173322ff4b047b
Author: Marcin Kolny <marcin kolny gmail com>
Date:   Tue Aug 5 00:34:23 2014 +0200

    Gst::Allocator: wrapped GstAllocator struct.
    
        * .gitignore: added allocator generated files, test *.log
        and *.trs files to ignore list
        * gstreamer/gstreamermm.h: add allocator header to core includes
        * gstreamer/src/allocator.ccg:
        * gstreamer/src/allocator.hg: implemented GstAllocator wrapper
        * gstreamer/src/filelist.am:
        * gstreamer/src/gst_vfuncs.defs: added allocator's virtual
        functions definitions
        * tests/Makefile.am:
        * tests/test-allocator.cc: added a few tests for Allocator wrapper
        * tools/m4/convert_gst.m4: added conversions definitions between
        AllocationParams and GstAllocationParams, Memory and GstMemory

 .gitignore                    |    6 ++
 gstreamer/gstreamermm.h       |    1 +
 gstreamer/src/allocator.ccg   |  124 +++++++++++++++++++++++++++++++++++++++++
 gstreamer/src/allocator.hg    |  113 +++++++++++++++++++++++++++++++++++++
 gstreamer/src/filelist.am     |    1 +
 gstreamer/src/gst_vfuncs.defs |   19 ++++++
 tests/Makefile.am             |    2 +
 tests/test-allocator.cc       |   44 ++++++++++++++
 tools/m4/convert_gst.m4       |    6 ++
 9 files changed, 316 insertions(+), 0 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 7feee4f..f8b65eb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -40,6 +40,8 @@ stamp-h?
 /examples/*/example
 
 # gstreamer/
+gstreamer/gstreamermm/allocator.cc
+gstreamer/gstreamermm/allocator.h
 gstreamer/gstreamermm/audioclock.cc
 gstreamer/gstreamermm/audioclock.h
 gstreamer/gstreamermm/audiofilter.cc
@@ -474,6 +476,7 @@ gstreamer/src/xvimagesink.ccg
 gstreamer/src/xvimagesink.hg
 
 #tests/
+tests/test-allocator
 tests/test-buffer
 tests/test-bus
 tests/test-caps
@@ -496,6 +499,9 @@ test-regression-rewritefile
 test-regression-seekonstartup
 test-regression-videoduration
 
+tests/*.log
+tests/*.trs
+
 #tests/resources/
 tests/resources/test-regression-bininpipeline-output-image.jpg
 tests/resources/test-regression-binplugin-output-image.jpg
diff --git a/gstreamer/gstreamermm.h b/gstreamer/gstreamermm.h
index 68c4861..96d3cf9 100644
--- a/gstreamer/gstreamermm.h
+++ b/gstreamer/gstreamermm.h
@@ -64,6 +64,7 @@
 #include <gstreamermm/register.h>
 
 // Core includes
+#include <gstreamermm/allocator.h>
 #include <gstreamermm/bin.h>
 #include <gstreamermm/buffer.h>
 #include <gstreamermm/bufferlist.h>
diff --git a/gstreamer/src/allocator.ccg b/gstreamer/src/allocator.ccg
new file mode 100644
index 0000000..37aa11d
--- /dev/null
+++ b/gstreamer/src/allocator.ccg
@@ -0,0 +1,124 @@
+/* gstreamermm - a C++ wrapper for gstreamer
+ *
+ * Copyright 2014 The gstreamermm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <gstreamermm/allocator.h>
+
+_PINCLUDE(gstreamermm/private/object_p.h)
+
+namespace Gst
+{
+AllocationParams::AllocationParams()
+{
+  GstAllocationParams params;
+  gst_allocation_params_init(&params);
+  gobject_ = gst_allocation_params_copy(&params);
+}
+
+Glib::RefPtr<Allocator> Allocator::find(const Glib::ustring& name)
+{
+  return Glib::wrap(gst_allocator_find(name.c_str()), false);
+}
+
+void Allocator::register_allocator(const Glib::ustring& name)
+{
+  reference();
+  gst_allocator_register(name.c_str(), gobj());
+}
+
+Glib::RefPtr<Gst::Allocator> Allocator::get_default_allocator()
+{
+  return Glib::wrap(gst_allocator_find(NULL), false);
+}
+
+void Allocator::set_default()
+{
+  reference();
+  gst_allocator_set_default(gobj());
+}
+
+void Allocator::free(Glib::RefPtr<Gst::Memory>& memory)
+{
+  GstMemory* memory_gobj = memory->gobj();
+  memory->reference();
+  memory.reset();
+
+  gst_allocator_free(gobj(), memory_gobj);
+}
+
+void Allocator_Class::free_vfunc_callback(GstAllocator* self, GstMemory* memory)
+{
+  Glib::ObjectBase *const obj_base = static_cast<Glib::ObjectBase*>(
+      Glib::ObjectBase::_get_current_wrapper((GObject*)self));
+
+  // Non-gtkmmproc-generated custom classes implicitly call the default
+  // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
+  // generated classes can use this optimisation, which avoids the unnecessary
+  // parameter conversions if there is no possibility of the virtual function
+  // being overridden:
+  if(obj_base && obj_base->is_derived_())
+  {
+    CppObjectType *const obj = dynamic_cast<CppObjectType* const>(obj_base);
+    if(obj) // This can be NULL during destruction.
+    {
+      #ifdef GLIBMM_EXCEPTIONS_ENABLED
+      try // Trap C++ exceptions which would normally be lost because this is a C callback.
+      {
+      #endif //GLIBMM_EXCEPTIONS_ENABLED
+        // Call the virtual member method, which derived classes might override.
+       Glib::RefPtr<Gst::Memory> mem = Glib::wrap(memory);
+        obj->free_vfunc(mem);
+        return;
+      #ifdef GLIBMM_EXCEPTIONS_ENABLED
+      }
+      catch(...)
+      {
+        Glib::exception_handlers_invoke();
+      }
+      #endif //GLIBMM_EXCEPTIONS_ENABLED
+    }
+  }
+
+  BaseClassType *const base = static_cast<BaseClassType*>(
+      g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)) // Get the parent class of the object class (The 
original underlying C class).
+  );
+
+  // Call the original underlying C function:
+  if(base && base->free)
+  {
+    (*base->free)(self, memory);
+  }
+
+}
+
+void Gst::Allocator::free_vfunc(Glib::RefPtr<Gst::Memory>& memory)
+{
+  BaseClassType *const base = static_cast<BaseClassType*>(
+      g_type_class_peek_parent(G_OBJECT_GET_CLASS(gobject_))
+  );
+
+  if(base && base->free)
+  {
+    GstMemory* mem_gobj = memory->gobj();
+    memory->reference();
+    memory.reset();
+    (*base->free)(gobj(), mem_gobj);
+  }
+}
+
+}
diff --git a/gstreamer/src/allocator.hg b/gstreamer/src/allocator.hg
new file mode 100644
index 0000000..e778c8a
--- /dev/null
+++ b/gstreamer/src/allocator.hg
@@ -0,0 +1,113 @@
+/* gstreamermm - a C++ wrapper for gstreamer
+ *
+ * Copyright 2014 The gstreamermm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <gst/gst.h>
+#include <gstreamermm/memory.h>
+#include <gstreamermm/object.h>
+
+_DEFS(gstreamermm,gst)
+
+namespace Gst
+{
+
+class AllocationParams
+{
+  _CLASS_BOXEDTYPE(AllocationParams, GstAllocationParams, NONE, gst_allocation_params_copy, 
gst_allocation_params_free)
+
+public:
+  _CUSTOM_DEFAULT_CTOR
+
+  AllocationParams();
+
+  _WRAP_METHOD(void init(), gst_allocation_params_init)
+
+  _MEMBER_GET(flags, flags, MemoryFlags, GstMemoryFlags)
+  _MEMBER_SET(flags, flags, MemoryFlags, GstMemoryFlags)
+
+  _MEMBER_GET(align, align, gsize, gsize)
+  _MEMBER_SET(align, align, gsize, gsize)
+
+  _MEMBER_GET(prefix, prefix, gsize, gsize)
+  _MEMBER_SET(prefix, prefix, gsize, gsize)
+
+  _MEMBER_GET(padding, padding, gsize, gsize)
+  _MEMBER_SET(padding, padding, gsize, gsize)
+};
+
+class Allocator : public Gst::Object
+{
+  _CLASS_GOBJECT(Allocator, GstAllocator, GST_ALLOCATOR, Gst::Object, GstObject)
+
+public:
+  /**
+   * Find a previously registered allocator with @name.
+   *
+   * @name: the name of the allocator
+   *
+   * @return: a #Glib::RefPtr<Gst::Allocator>.
+   */
+  static Glib::RefPtr<Gst::Allocator> find(const Glib::ustring& name);
+  _IGNORE(gst_allocator_find)
+
+  /**
+   * Registers the memory @allocator with @name.
+   *
+   * @name: the name of the allocator
+   * @allocator: (transfer full): #GstAllocator
+   */
+  void register_allocator(const Glib::ustring& name);
+  _IGNORE(gst_allocator_register)
+
+  /**
+   * Set the default allocator.
+   */
+  void set_default();
+  _IGNORE(gst_allocator_set_default)
+
+  /**
+   * Find default allocator.
+   *
+   * @return: default allocator.
+   */
+  static Glib::RefPtr<Gst::Allocator> get_default_allocator();
+
+  /** Free @a memory that was previously allocated with alloc().
+   * @param memory The memory to free.
+   */
+  void free(Glib::RefPtr<Gst::Memory>& memory);
+  _IGNORE(gst_allocator_free)
+
+  _WRAP_METHOD(Glib::RefPtr<Gst::Memory> alloc(gsize size, AllocationParams params), gst_allocator_alloc)
+  _WRAP_VFUNC(Glib::RefPtr<Gst::Memory> alloc(gsize size, AllocationParams params), "alloc")
+
+  // This vfunc is hand-coded because it takes reference to a memory.
+  // In generally, arguments are passed to a function by copy, so
+  // custom wrapper is necessary.
+  virtual void free_vfunc(Glib::RefPtr<Gst::Memory>& memory);
+
+protected:
+#m4begin
+  _PUSH(SECTION_PCC_CLASS_INIT_VFUNCS)
+  klass->free = &free_vfunc_callback;
+  _SECTION(SECTION_PH_VFUNCS)
+  static void free_vfunc_callback(GstAllocator* self, GstMemory* memory);
+  _POP()
+#m4end
+};
+}
diff --git a/gstreamer/src/filelist.am b/gstreamer/src/filelist.am
index 5508ae1..78729fd 100644
--- a/gstreamer/src/filelist.am
+++ b/gstreamer/src/filelist.am
@@ -81,6 +81,7 @@ plugins_hg =                    \
 plugins_ccg = $(plugins_hg:.hg=.ccg)
 
 files_hg  =                     \
+        allocator.hg            \
         audiobasesink.hg        \
         audiobasesrc.hg         \
         audioclock.hg           \
diff --git a/gstreamer/src/gst_vfuncs.defs b/gstreamer/src/gst_vfuncs.defs
index 56b82ae..359e61f 100644
--- a/gstreamer/src/gst_vfuncs.defs
+++ b/gstreamer/src/gst_vfuncs.defs
@@ -3,6 +3,25 @@
 ; define-vfunc is g*mm-specific
 ; These are hand-written.
 
+; GstAllocator
+
+(define-vfunc alloc
+  (of-object "GstAllocator")
+  (return-type "GstMemory*")
+  (parameters
+   '("gsize" "size")
+   '("GstAllocationParams*" "params")
+  )
+)
+
+(define-vfunc free
+  (of-object "GstAllocator")
+  (return-type "void")
+  (parameters
+   '("GstMemory*" "memory")
+  )
+)
+
 ; GstAudioFilter
 
 (define-vfunc setup
diff --git a/tests/Makefile.am b/tests/Makefile.am
index eba8a47..de76e0f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -21,6 +21,7 @@ AM_CXXFLAGS = $(GSTREAMERMM_WXXFLAGS) -g
 LDADD = $(GSTREAMERMM_LIBS) $(local_libgstreamermm) -lgtest -lpthread
 
 check_PROGRAMS = test-caps test-buffer test-bus test-caps test-pad \
+                 test-allocator \
                  test-urihandler test-ghostpad test-init \
                  test-query test-structure test-taglist test-plugin-appsink \
                  test-plugin-appsrc test-plugin-register test-plugin-pushsrc \
@@ -33,6 +34,7 @@ TESTS = $(check_PROGRAMS)
 TEST_MAIN_SOURCE = main.cc
 TEST_REGRESSION_UTILS = regression/utils.cc
 
+test_allocator_SOURCES         = test-allocator.cc $(TEST_MAIN_SOURCE)
 test_caps_SOURCES                      = test-caps.cc $(TEST_MAIN_SOURCE)
 test_buffer_SOURCES                    = test-buffer.cc $(TEST_MAIN_SOURCE)
 test_bus_SOURCES                       = test-bus.cc $(TEST_MAIN_SOURCE)
diff --git a/tests/test-allocator.cc b/tests/test-allocator.cc
new file mode 100644
index 0000000..b820518
--- /dev/null
+++ b/tests/test-allocator.cc
@@ -0,0 +1,44 @@
+/*
+ * test-allocator.cc
+ *
+ *  Created on: Aug 4, 2014
+ *      Author: loganek
+ */
+
+#include <gtest/gtest.h>
+#include <gstreamermm.h>
+
+using namespace Gst;
+
+TEST(AllocatorTest, ShouldFindSystemAllocator)
+{
+  Glib::RefPtr<Allocator> allocator = Allocator::find(GST_ALLOCATOR_SYSMEM);
+
+  ASSERT_TRUE(allocator);
+}
+
+TEST(AllocatorTest, DefaultAllocatorTheSameAsSystemAllocator)
+{
+  Glib::RefPtr<Allocator> allocator1 = Allocator::find(GST_ALLOCATOR_SYSMEM),
+               allocator2 = Allocator::get_default_allocator();
+
+  ASSERT_TRUE(allocator1);
+  ASSERT_EQ(allocator1, allocator2);
+}
+
+TEST(AllocatorTest, ShouldCorrectAllocateMemory)
+{
+  AllocationParams params;
+  params.set_align(7);
+  MemoryFlags flags = MEMORY_FLAG_NO_SHARE | MEMORY_FLAG_LAST;
+  params.set_flags(flags);
+  Glib::RefPtr<Allocator> allocator = Allocator::get_default_allocator();
+  Glib::RefPtr<Memory> mem = allocator->alloc(10, params);
+
+  ASSERT_TRUE(mem);
+  EXPECT_EQ(10, mem->get_size());
+  EXPECT_EQ(7, mem->get_align());
+  EXPECT_TRUE(flags & mem->get_flags());
+
+  allocator->free(mem);
+}
diff --git a/tools/m4/convert_gst.m4 b/tools/m4/convert_gst.m4
index c5634d9..08c280f 100644
--- a/tools/m4/convert_gst.m4
+++ b/tools/m4/convert_gst.m4
@@ -27,6 +27,7 @@ _CONV_ENUM(Gst,IndexLookupMethod)
 _CONV_ENUM(Gst,IndexResolverMethod)
 _CONV_ENUM(Gst,LockFlags)
 _CONV_ENUM(Gst,MapFlags)
+_CONV_ENUM(Gst,MemoryFlags)
 _CONV_ENUM(Gst,MessageType)
 _CONV_ENUM(Gst,MixerFlags)
 _CONV_ENUM(Gst,MixerType)
@@ -60,6 +61,10 @@ _CONV_GLIB_ENUM(ThreadPriority)
 
 dnl ############### gstreamermm Class Conversions ######################
 
+dnl AllocationParams
+_CONVERSION(`AllocationParams', `GstAllocationParams*', `$3.gobj()')
+_CONVERSION(`GstAllocationParams*', `AllocationParams', `AllocationParams($3, true)')
+
 dnl Buffer
 _CONVERSION(`GstBuffer*',`Glib::RefPtr<Gst::Buffer>',`Glib::wrap($3)')
 _CONVERSION(`const Glib::RefPtr<Gst::Buffer>&',`GstBuffer*', `Glib::unwrap($3)')
@@ -145,6 +150,7 @@ dnl MapInfo
 _CONVERSION(`const Glib::RefPtr<Gst::MapInfo>&', `GstMapInfo*', `$3->gobj()')
 
 dnl Memory
+_CONVERSION(`GstMemory*',`Glib::RefPtr<Gst::Memory>&',`Glib::wrap($3)')
 _CONVERSION(`GstMemory*',`Glib::RefPtr<Gst::Memory>',`Glib::wrap($3)')
 _CONVERSION(`Glib::RefPtr<Gst::Memory>&',`GstMemory*', `Glib::unwrap($3)')
 _CONVERSION(`Glib::RefPtr<Gst::Memory>',`GstMemory*', `Glib::unwrap($3)')


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]