gstreamer&&deepstream 学习笔记(四)——GLIB

8 篇文章 0 订阅

gstreamer 是基于glib实现的,所以,在使用gstreamer的过程中,会用到大量的glib的接口,什么是glib呢?

glib库是Linux平台下最常用的C语言函数库,它具有很好的可移植性和实用性。

glib是Gtk +库和Gnome的基础。glib可以在多个平台下使用,比如Linux、Unix、Windows等。glib为许多标准的、常用的C语言结构提供了相应的替代物。 (来源:百度百科)

以下是我在看Deepstream官方sample时不断总结的glib函数,当然也可以直接参考:https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html#g-malloc0 官方的api文档

 

g_strcmp0

来自 <https://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html#g-strcmp0>

比较字符串,如果字符串为两个NULL 指针或者相等 返回 0

g_strv_length (gchar **str_array);

来自 <https://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html#g-strv-length>

返回所给的 字符串数组长度。

g_malloc0

来自 <https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html#g-malloc0>

开辟 n bytes空间,以0初始化之

gboolean
g_key_file_load_from_file (GKeyFile *key_file,
                           const
gchar *file,
                          
GKeyFileFlags flags,
                          
GError **error);

来自 <https://developer.gnome.org/glib/stable/glib-Key-value-file-parser.html#g-key-file-load-from-file>

读取config 文件 到 GKeyFile (iini文件)

g_key_file_has_group (cfg_file,"bbb")

判断 “bbb” 这个group 在不在 ini文件中

获取某个group下的所有key值(得到的是指针数组)

从key_file 根据键取值:

g_key_file_get_XXX(key)

g_key_file_remove_group ()

来自 <https://developer.gnome.org/glib/stable/glib-Key-value-file-parser.html#g-key-file-remove-group>

删除一个group

guint64
g_ascii_strtoull (const gchar *nptr,
                 
gchar **endptr,
                 
guint base);

来自 <https://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html#g-ascii-strtoull>

将一个字符串转换成uint64

 g_key_file_get_groups (cfg_file, NULL);

从一个keyfile中获取所有的group

g_quark_from_static_string ()

来自 <https://developer.gnome.org/glib/stable/glib-Quarks.html#g-quark-from-static-string>

为一个字符串生成一个unique uint32 (GQuark)

g_timeout_add ()

来自 <https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#g-timeout-add>

设置一个函数,自动周期性调用

g_source_remove (gunit)

来自 <https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#g-source-remove>

从主上下文将一个source 删除(可以用于删除gstreamer 总线上的callback)

主要使用的函数有g_object_set_data与g_object_get_data两个函数

他们的主要功能是把某个指针(任意类型的[可以指向某个构件],原型为gpointer ,即空指针)与某个构件相关连。

关联后即可通过构件+相应的key获取到相应的指针。

g_return_val_if_fail()

#define             g_return_val_if_fail(expr,val)

Verifies that the expression expr , usually representing a precondition, evaluates to TRUE. If the function does not return a value, use g_return_if_fail() instead.

If expr evaluates to FALSE, the current function should be considered to have undefined behaviour (a programmer error). The only correct solution to such an error is to change the module that is calling the current function, so that it avoids this incorrect call.

To make this undefined behaviour visible, if expr evaluates to FALSE, the result is usually that a critical message is logged and val is returned from the current function.

If G_DISABLE_CHECKS is defined then the check is not performed. You should therefore not depend on any side effects of expr .

See g_return_if_fail() for guidance on how to debug failure of this check.

来自 <https://developer.gnome.org/glib/stable/glib-Warnings-and-Assertions.html#g-return-val-if-fail>

g_idle_add ()

guint
g_idle_add (GSourceFunc function,
           
gpointer data);

Adds a function to be called whenever there are no higher priority events pending to the default main loop. The function is given the default idle priority, G_PRIORITY_DEFAULT_IDLE. If the function returns FALSE it is automatically removed from the list of event sources and will not be called again.

See memory management of sources for details on how to handle the return value and memory management of data .

This internally creates a main loop source using g_idle_source_new() and attaches it to the global GMainContext using g_source_attach(), so the callback will be invoked in whichever thread is running that main context. You can do these steps manually if you need greater control or to use a custom main context.

Parameters

function

function to call

data

data to pass to function .

来自 <https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#g-idle-add>

g_source_remove ()

gboolean
g_source_remove (guint tag);

Removes the source with the given ID from the default main context. You must use g_source_destroy() for sources added to a non-default main context.

The ID of a GSource is given by g_source_get_id(), or will be returned by the functions g_source_attach()g_idle_add()g_idle_add_full()g_timeout_add()g_timeout_add_full()g_child_watch_add()g_child_watch_add_full()g_io_add_watch(), and g_io_add_watch_full().

It is a programmer error to attempt to remove a non-existent source.

More specifically: source IDs can be reissued after a source has been destroyed and therefore it is never valid to use this function with a source ID which may have already been removed. An example is when scheduling an idle to run in another thread with g_idle_add(): the idle may already have run and been removed by the time this function is called on its (now invalid) source ID. This source ID may have been reissued, leading to the operation being performed against the wrong source.

Parameters

来自 <https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#g-source-remove>

g_ascii_strtoll ()

gint64
g_ascii_strtoll (const gchar *nptr,
                
gchar **endptr,
                
guint base);

Converts a string to a gint64 value. This function behaves like the standard strtoll() function does in the C locale. It does this without actually changing the current locale, since that would not be thread-safe.

This function is typically used when reading configuration files or other non-user input that should be locale independent. To handle input from the user you should normally use the locale-sensitive system strtoll() function.

If the correct value would cause overflow, G_MAXINT64 or G_MININT64 is returned, and ERANGE is stored in errno. If the base is outside the valid range, zero is returned, and EINVAL is stored in errno. If the string conversion fails, zero is returned, and endptr returns nptr (if endptr is non-NULL).

Parameters

nptr

the string to convert to a numeric value.

 

endptr

if non-NULL, it returns the character after the last character used in the conversion.

[out][transfer none][optional]

base

to be used for the conversion, 2..36 or 0

 

来自 <https://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html#g-ascii-strtoll>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你的问题。关于通过UDP传输音视频,我了解一些相关的知识,下面是一些学习笔记: 1. gstreamer是一个流媒体框架,用于创建、处理和播放多媒体流。它支持多种音视频格式,可以通过插件扩展功能。 2. 通过gstreamer可以使用UDP协议传输音视频数据。UDP协议是一种无连接的协议,不保证数据传输的可靠性和顺序性,但是传输效率高。 3. 首先需要创建一个gstreamer的pipeline,包括音视频源、编码器、UDP发送端等组件。例如: ``` gst-launch-1.0 -v filesrc location=test.mp4 ! decodebin ! x264enc ! rtph264pay ! udpsink host=192.168.1.100 port=5000 ``` 这个pipeline的作用是从test.mp4文件读取音视频流,解码后使用x264编码器进行压缩,然后使用rtph264pay将数据打包成RTP数据包,最后通过udpsink发送到指定的IP地址和端口。 4. 接收端需要创建一个gstreamer的pipeline,包括UDP接收端、解包器、解码器等组件。例如: ``` gst-launch-1.0 -v udpsrc port=5000 ! application/x-rtp, payload=96 ! rtpjitterbuffer ! rtph264depay ! avdec_h264 ! autovideosink ``` 这个pipeline的作用是从UDP端口5000接收音视频数据,使用rtpjitterbuffer解决网络抖动问题,使用rtph264depay将RTP数据包解包成原始的H.264数据流,然后使用avdec_h264解码器进行解码,最后使用autovideosink播放视频。 5. 在实际使用过程中,还需要考虑数据的带宽限制、网络延迟等问题,以保证音视频传输的效果。 希望这些笔记能对你有帮助。如果你还有其他问题,可以继续问我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值