Core Library-GstObject

GInitiallyUnowned

我查了GInitiallyUnowned的相关资料,在gobject.h文件中

typedef struct _GObject                  GObject;
typedef struct _GObjectClass             GObjectClass;
typedef struct _GObject                  GInitiallyUnowned;
typedef struct _GObjectClass             GInitiallyUnownedClass;

从语法角度理解,GObject和GInitiallyUnowned指向的结构体是相同的,也就是两者并无区别。但是从GObject Reference Manual中了解到:

GInitiallyUnowned is derived from GObject. The only difference between the two is that the initial reference of a GInitiallyUnowned is flagged as a floating reference. This means that it is not specifically claimed to be “owned” by any code portion. The main motivation for providing floating references is C convenience. In particular, it allows code to be written as:

GInitiallyUnowned源自GObject。两者之间唯一的区别是GInitiallyUnowned的初始引用被标记为floating引用。这意味着它不被任何代码部分明确声明为“拥有”。提供floating引用的主要动机是C语言的便利性。特别是,它允许代码写成:

关于floating引用我不明白作用是什么

GstObject

GstObject为GStreamer库提供了一个根基。它目前是GInitiallyUnowned之上的一个薄包装。它是一个抽象类,单独使用不是很方便。

GstObject为我们提供了基本的引用计数、父类方法和锁。大多数函数只是为特定的GStreamer需求而扩展,可以在GstObject的基类GObject下找到相同的名称(例如g_object_ref成为gst_object_ref)。

由于GstObject继承于 GInitiallyUnowned,因此它也继承了浮动引用(我的理解:浮动引用传入指针的时候,不会refcount,比如gst_bin_addd)。请注意,gst_bin_add和gst_element_add_pad等函数拥有浮动引用。

与GObject实例不同,GstObject添加了name属性。函数gst_object_set_namegst_object_get_name用于设置/获取对象的名称。

浮动引用理解

主要以GtkWidget为例子。

1.具有浮动引用的对象,未被其他具有g_object_ref_sink的函数调用,不能直接解引用

G_DEFINE_TYPE (VpfDialog, vpf_dialog, GTK_TYPE_WIDGET)
GtkWidget *
vpf_dialog_new () {
  return g_object_new (VPF_TYPE_DIALOGS, NULL);
}

GtkWidget *test = vpf_dialog_new ();
g_print ("initial after: test ref count = %d\n", G_OBJECT(test)->ref_count);
g_object_unref (test);

请添加图片描述

虽然 initial after: test ref count = 1,但是不能直接解引用。但是,如果不进行g_object_unref(test)。该对象内存并不会被释放。

所以,具有浮动引用的对象,未被其他具有g_object_ref_sink的函数调用,应该

  • 使用g_object_ref_sink,浮动引用变成正常引用 (并不会增加引用计数),如果变成普通引用后,再调用g_object_ref_sink,会增加一个引用计数。
  • 解引用,释放内存g_object_unref
  GtkWidget *test = vpf_dialog_new ();
  g_print ("initial after: test ref count = %d\n", G_OBJECT(test)->ref_count);
  g_print ("ref sink after: test ref count = %d\n", G_OBJECT(test)->ref_count);
  g_object_unref (test);
  g_print ("G_IS_OBJECT (test) = %d\n", G_IS_OBJECT (test));

initial after: test ref count = 1
ref sink after: test ref count = 1
DEBUG: vpf_dialog_dispose
G_IS_OBJECT (test) = 0

GtkWidget *test = vpf_dialog_new ();
  g_print ("initial after: test ref count = %d\n", G_OBJECT(test)->ref_count);
  g_object_ref_sink (test);
  g_object_ref_sink (test);
  g_print ("ref sink after: test ref count = %d\n", G_OBJECT(test)->ref_count);
  g_object_unref (test);
  g_print ("G_IS_OBJECT (test) = %d\n", G_IS_OBJECT (test));

输出结果:
initial after: test ref count = 1
ref sink after: test ref count = 2
G_IS_OBJECT (test) = 1

2.浮动引用的作用,提供便利性

被标记为浮动引用,这意味着它不被任何代码部分明确声明为“拥有”。提供浮动引用的主要动机是C语言的便利性。特别是,它允许代码写成:

container = create_container();
container_add_child (container, create_child());

如果container_add_child()将g_object_ref_sink()传递给child,则不会泄漏新创建的child的引用。没有浮动引用,container_add_child()只能g_object_ref()新子元素,因此要实现这段代码而不发生引用泄漏,它必须写成:

 Child *child;
 container = create_container();
 child = create_child();
 container_add_child (container, child);
 g_object_unref (child);

意思就是,因为有了浮动引用,主程序调用其他函数使用对象的时候,主程序不需要解应用,因为浮动引用变成正常引用不会增加引用计数。

在那些体具有自动引用,内存回收的语言中,就不需浮动引用。通过进行以下操作:(如果是浮动引用,变成正常引用,使用完之后,再变成浮动引用)

  /* save floating state */
  gboolean was_floating = g_object_is_floating (object);
  g_object_ref_sink (object);
  /* protected code portion */
  ...;
  /* restore floating state */
  if (was_floating)
    g_object_force_floating (object);
  g_obejct_unref (object); /* release previously acquired reference */

controlled properties

不理解这部分的作用
controlled properties

Controlled properties offers a lightweight way to adjust gobject properties over stream-time. It works by using time-stamped value pairs that are queued for element-properties. At run-time the elements continuously pull value changes for the current stream-time.

What needs to be changed in a GstElement? Very little - it is just two steps to make a plugin controllable!

  • mark gobject-properties paramspecs that make sense to be controlled, by GST_PARAM_CONTROLLABLE.

  • when processing data (get, chain, loop function) at the beginning call gst_object_sync_values(element,timestamp). This will make the controller update all GObject properties that are under its control with the current values based on the timestamp.

What needs to be done in applications? Again it’s not a lot to change.

  • create a GstControlSource. csource = gst_interpolation_control_source_new (); g_object_set (csource, “mode”, GST_INTERPOLATION_MODE_LINEAR, NULL);

  • Attach the GstControlSource on the controller to a property. gst_object_add_control_binding (object, gst_direct_control_binding_new (object, “prop1”, csource));

  • Set the control values gst_timed_value_control_source_set ((GstTimedValueControlSource *)csource,0 * GST_SECOND, value1); gst_timed_value_control_source_set ((GstTimedValueControlSource *)csource,1 * GST_SECOND, value2);

  • start your pipeline

GstObject

GObject
    ╰──GInitiallyUnowned
        ╰──GstObject
            ╰──GstAllocator
            ╰──GstBufferPool
            ╰──GstBus
            ╰──GstClock
            ╰──GstControlBinding
            ╰──GstControlSource
            ╰──GstDevice
            ╰──GstDeviceMonitor
            ╰──GstDeviceProvider
            ╰──GstElement
            ╰──GstPad
            ╰──GstPadTemplate
            ╰──GstPlugin
            ╰──GstPluginFeature
            ╰──GstRegistry
            ╰──GstStream
            ╰──GstStreamCollection
            ╰──GstTask
            ╰──GstTaskPool
            ╰──GstTracer
            ╰──GstTracerRecord
typedef struct _GstObject GstObject;
struct _GstObject {
  GInitiallyUnowned object;

  /*< public >*/ /* with LOCK */
  GMutex         lock;        /* object LOCK */
  gchar         *name;        /* object name */
  GstObject     *parent;      /* this object's parent, weak ref */
  guint32        flags;

  /*< private >*/
  GList         *control_bindings;  /* List of GstControlBinding */
  guint64        control_rate;
  guint64        last_sync;

  gpointer _gst_reserved;
};

属性

name

“name” gchar *

parent

“parent” GstObject * /*这个对象的父类*/

参考:GstObject

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值