当前位置:网站首页>Gstreamer中的task
Gstreamer中的task
2022-07-05 19:34:00 【HUI的技術筆記】
在gstreamer中,可以在需要的时候方便的创建一个task作为独立线程,task可以是普通的task,也可以是pad task,这个需要根据具体需求来定,下面先看下v4l2和avdemux里面task的代码。
task的创建
gst-plugins-good/sys/v4l2/gstv4l2videodec.c
通过gst_pad_start_task开始一个pad task,task函数是gst_v4l2_video_dec_loop。作用是启动v4l2的解码task,并且当task退出时,将禁用输入处理以在drain时unlock输入,防止阻塞:
/* start task */
if (!gst_pad_start_task (decoder->srcpad, (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL))
goto start_task_failed;
gst-libav/ext/libav/gstavdemux.c
创建一个demux->task,task函数是gst_ffmpegdemux_loop,gst_task_init会通过gst_task_new间接调用,在init函数中调用ensure_klass_pool确保_global_task_pool的线程池可用,start_task的时候将task通过gst_task_pool_push放到task的线程池里面,下面gst_pad_start_task的调用栈就可以看到。
/* static GstTaskPool *_global_task_pool = NULL; */
/* new task */
demux->task = gst_task_new ((GstTaskFunction) gst_ffmpegdemux_loop, demux, NULL);
g_rec_mutex_init (&demux->task_lock);
/* set lock */
gst_task_set_lock (demux->task, &demux->task_lock);
gst_ffmpegdemux_loop和gst_v4l2_video_dec_loop两个函数是task的loop函数,在gstreamer中,task不像pthread的中的task,需要有一个循环来保证常驻task一直存在。而是通过前面的线程池来运行。
task的运行
看gst_pad_start_task的代码和栈就可以清楚的看到task的new,init,start都是在gst_pad_start_task里面实现的,这些在如果不是通过gst_pad_start_task启动的task,都需要手动调用。
1 gst_task_init gsttask.c 249 0x7ffff7b49fa3
2 g_type_create_instance 0x7ffff75559c5
4 g_object_new_with_properties 0x7ffff7537ee5
5 g_object_new 0x7ffff7538961
6 gst_task_new gsttask.c 482 0x7ffff7b4a5ad
7 gst_pad_start_task gstpad.c 6287 0x7ffff7b1d7fe
调用start_task的栈:
1 gst_task_pool_push gsttaskpool.c 252 0x7ffff7b4b820
2 start_task gsttask.c 712 0x7ffff7b49f19
3 gst_task_set_state_unlocked gsttask.c 744 0x7ffff7b4ad3e
4 gst_task_set_state gsttask.c 792 0x7ffff7b4ad3e
7 gst_pad_start_task gstpad.c 6287 0x7ffff7b1d7fe
gst_task_pool_push是在start_task函数中调用的,将task放到线程池里面,指定taskpool函数为gst_task_func:
/* push on the thread pool, we remember the original pool because the user * could change it later on and then we join to the wrong pool. */
priv->pool_id = gst_object_ref (priv->pool);
priv->id =
gst_task_pool_push (priv->pool_id, (GstTaskPoolFunction) gst_task_func,
task, &error);
gst_task_pool_push中继续调用GstTaskPoolClass的push函数default_push,在default_push函数中会调用g_thread_pool_push函数放到g_thread_pool里面,后面task的运行就是GLib的g_thread_pool相关的实现了。
gpointer
gst_task_pool_push (GstTaskPool * pool, GstTaskPoolFunction func,
gpointer user_data, GError ** error)
{
GstTaskPoolClass *klass;
g_return_val_if_fail (GST_IS_TASK_POOL (pool), NULL);
klass = GST_TASK_POOL_GET_CLASS (pool);
if (klass->push == NULL)
goto not_supported;
/* gsttaskpool_class->push = default_push; */
return klass->push (pool, func, user_data, error);
}
注意:task不允许没有lock而使用,否则会报错:
task without a lock can't be set to state 0
通过gst_task_set_lock函数原型可以看到,这里task需要的lock是GRecMutex类型。
下面代码是curlhttpsrc的代码,可以有个初步的认识:
subprojects/gst-plugins-bad/ext/curl/gstcurlhttpsrc.c
/* init RecMutex */
g_rec_mutex_init (&klass->multi_task_context.task_rec_mutex);
klass->multi_task_context.state = GSTCURL_MULTI_LOOP_STATE_RUNNING;
/* new task */
klass->multi_task_context.task = gst_task_new (
(GstTaskFunction) gst_curl_http_src_curl_multi_loop,
(gpointer) & klass->multi_task_context, NULL);
/* set lock */
gst_task_set_lock (klass->multi_task_context.task,
&klass->multi_task_context.task_rec_mutex);
/* start task */
if (gst_task_start (klass->multi_task_context.task) == FALSE) {
GSTCURL_ERROR_PRINT ("Couldn't start curl_multi task! Aborting.");
abort ();
}
GMutex
GMutex结构是一个不透明的数据结构,代表一个互斥量(mutex),它可以用来保护数据免受共享访问。
GRecMutex
GRecMutex结构是一个不透明的数据结构,代表一个递归互斥。它类似于GMutex,不同的是,它可以在同一线程中多次锁定GRecMutex而不造成死锁。在这样做的时候,必须注意在递归互斥被锁定的时候解锁它。
边栏推荐
- 40000 word Wenshuo operator new & operator delete
- That's awesome. It's enough to read this article
- Do you know several assertion methods commonly used by JMeter?
- JS solution force deduction daily question (12) - 556 Next larger element III (2022-7-3)
- How MySQL queries and modifies JSON data
- The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
- Shell编程基础(第8篇:分支语句-case in)
- 微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
- 100million single men and women supported an IPO with a valuation of 13billion
- Tutoriel de téléchargement et d'installation du progiciel fuzor 2020
猜你喜欢

如何在2022年更明智地应用智能合约?

XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西

四万字长文说operator new & operator delete

建议收藏,我的腾讯Android面试经历分享

40000 word Wenshuo operator new & operator delete

MMO project learning 1: preheating

HiEngine:可媲美本地的云原生内存数据库引擎

Can Leica capture the high-end market offered by Huawei for Xiaomi 12s?

webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件

大厂面试必备技能,2022Android不死我不倒
随机推荐
14、用户、组和权限(14)
acm入门day1
Postman核心功能解析-参数化和测试报告
四万字长文说operator new & operator delete
Go语言 | 02 for循环及常用函数的使用
UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
毫米波雷达人体感应器,智能感知静止存在,人体存在检测应用
Debezium系列之:记录mariadb数据库删除多张临时表debezium解析到的消息以及解决方法
JS solution force deduction daily question (12) - 556 Next larger element III (2022-7-3)
word如何转换成pdf?word转pdf简单的方法分享!
How to realize the Online timer and offline timer in the game
Common - Hero Minesweeper
Oracle故障处理:Ora-10873:file * needs to be either taken out of backup or media recovered
What is the core value of testing?
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
数学分析_笔记_第9章:曲线积分与曲面积分
aggregate
Go语言 | 01 WSL+VSCode环境搭建避坑指南
MMO项目学习一:预热