摘要

在以前的文章中,我们了解到了2种播放文件的方式:一种是在知道了文件的类型及编码方式后,手动创建所需Element并构造Pipeline;另一种是直接使用playbin,由playbin内部动态创建所需Element并连接Pipeline。很明显使用playbin的方式更加灵活,我们不需要在一开始就创建各种Pipeline,只需由playbin内部根据文件类型,自动构造Pipeline。 在了解了Pad的作用后,本文通过一个例子来了解如何通过Pad事件动态的连接Pipeline,为了解playbin内部是如何动态创建Pipeline打下基础。

动态连接Pipeline

在本章的例子中,我们在将Pipeline设置为PLAYING状态之前,不会将所有的Element都连接起来,这种处理方式是可以的,但需要额外的处理。如果在设置PLAYING状态后不做任何操作,数据无法到达Sink,Pipeline会直接抛出一个错误并退出。如果在收到相应事件后,对其进行处理,并将Pipeline连接起来,Pipeline就可以正常工作。

我们常见的媒体,音频和视频都是通过某一种容器格式被包含中同一个文件中。播放时,我们需要将音视频数据分离出来,通常将具备这种功能的模块称为分离器(demuxer)。

GStreamer针对常见的容器提供了相应的demuxer,如果一个容器文件中包含多种媒体数据(例如:一路视频,两路音频),这种情况下,demuxer会为些数据分别创建不同的Source Pad,每一个Source Pad可以被认为一个处理分支,可以创建多个分支分别处理相应的数据。

gst-launch-1.0 filesrc location=sintel_trailer-480p.ogv ! oggdemux name=demux ! queue ! vorbisdec ! autoaudiosink demux. ! queue ! theoradec ! videoconvert ! autovideosink
通过上面的命令播放文件时,会创建具有2个分支的Pipeline:

使用demuxer需要注意的一点是:demuxer只有在收到足够的数据时才能确定容器中包含哪些媒体信息,因此demuxer开始没有Source Pad,所以其他的Element无法在Pipeline创建时就连接到demuxer。
解决这种问题的办法是:在创建Pipeline时,我们只将Source Element到demuxer之间的Elements连接好,然后设置Pipeline状态为PLAYING,当demuxer收到足够的数据可以确定文件总包含哪些媒体流时,demuxer会创建相应的Source Pad,并通过事件告诉应用程序。我们可以通过监听demuxer的事件,在新的Source Pad被创建时,我们根据数据类型,创建相应的Element,再将其连接到Source Pad,形成完整的Pipeline。

示例代码

为了简化逻辑,我们在本示例中会忽略视频的Source Pad,仅连接音频的Source Pad。

#include <gst/gst.h>/* Structure to contain all our information, so we can pass it to callbacks */
typedef struct _CustomData {GstElement *pipeline;GstElement *source;GstElement *convert;GstElement *sink;
} CustomData;/* Handler for the pad-added signal */
static void pad_added_handler (GstElement *src, GstPad *pad, CustomData *data);int main(int argc, char *argv[]) {CustomData data;GstBus *bus;GstMessage *msg;GstStateChangeReturn ret;gboolean terminate = FALSE;/* Initialize GStreamer */gst_init (&argc, &argv);/* Create the elements */data.source = gst_element_factory_make ("uridecodebin", "source");data.convert = gst_element_factory_make ("audioconvert", "convert");data.sink = gst_element_factory_make ("autoaudiosink", "sink");/* Create the empty pipeline */data.pipeline = gst_pipeline_new ("test-pipeline");if (!data.pipeline || !data.source || !data.convert || !data.sink) {g_printerr ("Not all elements could be created.\n");return -1;}/* Build the pipeline. Note that we are NOT linking the source at this* point. We will do it later. */gst_bin_add_many (GST_BIN (data.pipeline), data.source, data.convert , data.sink, NULL);if (!gst_element_link (data.convert, data.sink)) {g_printerr ("Elements could not be linked.\n");gst_object_unref (data.pipeline);return -1;}/* Set the URI to play */g_object_set (data.source, "uri", "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);/* Connect to the pad-added signal */g_signal_connect (data.source, "pad-added", G_CALLBACK (pad_added_handler), &data);/* Start playing */ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);if (ret == GST_STATE_CHANGE_FAILURE) {g_printerr ("Unable to set the pipeline to the playing state.\n");gst_object_unref (data.pipeline);return -1;}/* Listen to the bus */bus = gst_element_get_bus (data.pipeline);do {msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS);/* Parse message */if (msg != NULL) {GError *err;gchar *debug_info;switch (GST_MESSAGE_TYPE (msg)) {case GST_MESSAGE_ERROR:gst_message_parse_error (msg, &err, &debug_info);g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");g_clear_error (&err);g_free (debug_info);terminate = TRUE;break;case GST_MESSAGE_EOS:g_print ("End-Of-Stream reached.\n");terminate = TRUE;break;case GST_MESSAGE_STATE_CHANGED:/* We are only interested in state-changed messages from the pipeline */if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data.pipeline)) {GstState old_state, new_state, pending_state;gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);g_print ("Pipeline state changed from %s to %s:\n",gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));}break;default:/* We should not reach here */g_printerr ("Unexpected message received.\n");break;}gst_message_unref (msg);}} while (!terminate);/* Free resources */gst_object_unref (bus);gst_element_set_state (data.pipeline, GST_STATE_NULL);gst_object_unref (data.pipeline);return 0;
}/* This function will be called by the pad-added signal */
static void pad_added_handler (GstElement *src, GstPad *new_pad, CustomData *data) {GstPad *sink_pad = gst_element_get_static_pad (data->convert, "sink");GstPadLinkReturn ret;GstCaps *new_pad_caps = NULL;GstStructure *new_pad_struct = NULL;const gchar *new_pad_type = NULL;g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (new_pad), GST_ELEMENT_NAME (src));/* If our converter is already linked, we have nothing to do here */if (gst_pad_is_linked (sink_pad)) {g_print ("We are already linked. Ignoring.\n");goto exit;}/* Check the new pad's type */new_pad_caps = gst_pad_get_current_caps (new_pad);new_pad_struct = gst_caps_get_structure (new_pad_caps, 0);new_pad_type = gst_structure_get_name (new_pad_struct);if (!g_str_has_prefix (new_pad_type, "audio/x-raw")) {g_print ("It has type '%s' which is not raw audio. Ignoring.\n", new_pad_type);goto exit;}/* Attempt the link */ret = gst_pad_link (new_pad, sink_pad);if (GST_PAD_LINK_FAILED (ret)) {g_print ("Type is '%s' but link failed.\n", new_pad_type);} else {g_print ("Link succeeded (type '%s').\n", new_pad_type);}exit:/* Unreference the new pad's caps, if we got them */if (new_pad_caps != NULL)gst_caps_unref (new_pad_caps);/* Unreference the sink pad */gst_object_unref (sink_pad);
}

将源码保存为basic-tutorial-4.c,执行下列命令可得到编译结果:
gcc basic-tutorial-4.c -o basic-tutorial-4 `pkg-config --cflags --libs gstreamer-1.0`

源码分析

/* Create the elements */
data.source = gst_element_factory_make ("uridecodebin", "source");
data.convert = gst_element_factory_make ("audioconvert", "convert");
data.sink = gst_element_factory_make ("autoaudiosink", "sink");

首先创建了所需的Element:

  • uridecodebin会中内部实例化所需的Elements(source,demuxer,decoder)将URI所指向的媒体文件中的各种媒体数据分别提取出来。因为其包含了demuxer,所以Source Pad在初始化阶段无法访问,只有在收到相应事件后去动态连接Pad。
  • audioconvert用于在不同的音频数据格式之间进行转换。由于不同的声卡支持的数据类型不尽相同,所以在某些平台需要对音频数据类型进行转换。
  • autoaudiosink会自动查找声卡设备,并将音频数据传输到声卡上进行输出。
if (!gst_element_link (data.convert, data.sink)) {g_printerr ("Elements could not be linked.\n");gst_object_unref (data.pipeline);return -1;
}

接着将converter和sink连接起来,注意,这里我们没有连接source与convert,是因为uridecode bin在Pipeline初始阶段还没有Source Pad。

/* Set the URI to play */
g_object_set (data.source, "uri", "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);

这里设置了播放文件的uri,uridecodebin会自动解析该地址,并读取媒体数据。

监听事件

/* Connect to the pad-added signal */
g_signal_connect (data.source, "pad-added", G_CALLBACK (pad_added_handler), &data);

GSignals在GStreamer中扮演着至关重要的角色。信号使你能在你所关心到事件发生后得到通知。在GLib中的信号通过信号名来进行识别,每个GObject对象都有其自己的信号。
在上面这行代码中,我们通过g_signal_connect将pad_added_handler回调连接到uridecodebin的“pad-added”信号上,同时附带回调函数的私有参数。GStreamer不会处理我们传入到data指针,只会将其作为参数传递给回调函数,这是传递私有数据给回调函数的常用方式。
一个GstElement可能会发出多个信号,可以使用gst-inspect工具查看具体到信号及参数。

在我们连接了“pad-added”的信号后,我们就可以将Pipeline的状态设置为PLAYING并按原有方式处理自己所关心到消息。

回调处理

当Source Element收集到足够到信息,能产生数据时,它会创建Source Pad并且触发“pad-added”信号。这时,我们的回调函数就会被调用。

static void pad_added_handler (GstElement *src, GstPad *new_pad, CustomData *data) {

这里是我们实现到回调函数,为什么我们的回调函数需要定义成这种格式呢?
因为我们的回调函数是为了处理信号所携带到信息,所以必须用符合信号的数据类型,否则不能正确到处理相应数据。通过gst-inspect查看uridecodebin可以看到信号所需要到回调函数格式:

$ gst-inspect-1.0 uridecodebin
...
Element Signals:"pad-added" :  void user_function (GstElement* object,GstPad* arg0,gpointer user_data);
...                              

  • src指针,指向触发这个事件的GstElement对象实例,这里是uridecodebin。GStreamer中的信号处理函数的第一个参数均为触发事件到对象指针。
  • new_pad指针,指向被创建的src中被创建的GstPad对象实例。这通常是我们需要连接的Pad。
  • data指针,指向我们在连接信号时所传的CustomData对象。
GstPad *sink_pad = gst_element_get_static_pad (data->convert, "sink");

我们首先从CustomData中取得convert指针,并通过gst_element_get_static_pad()获取其Sink Pad。我们需要将这个Sink Pad连接到uridecodebin新创建的new_pad中。

/* If our converter is already linked, we have nothing to do here */
if (gst_pad_is_linked (sink_pad)) {g_print ("We are already linked. Ignoring.\n");goto exit;
}

由于uridecodebin可能会创建多个Pad,在每次有Pad被创建时,我们的回调函数都会被调用。上面这段代码就是为了避免重复连接Pad。

/* Check the new pad's type */
new_pad_caps = gst_pad_get_current_caps (new_pad, NULL);
new_pad_struct = gst_caps_get_structure (new_pad_caps, 0);
new_pad_type = gst_structure_get_name (new_pad_struct);
if (!g_str_has_prefix (new_pad_type, "audio/x-raw")) {g_print ("It has type '%s' which is not raw audio. Ignoring.\n", new_pad_type);goto exit;
}

由于我们在当前示例中只处理audio相关的数据(我们开始只创建了autoaudiosink),所以我们这里对Pad所产生的数据类型进行了过滤,对于非音频Pad(视频及字幕)直接忽略。
gst_pad_get_current_caps()可以获取当前Pad的能力(这里是new_pad输出数据的能力),所有的能力被存储在GstCaps结构体中。Pad所支持的所有Caps可以通过gst_pad_query_caps()得到,由于一个Pad可能包含多个Caps,因此GstCaps可以包含一个或多个GstStructure,每个都代表所支持的不同数据的能力。通过gst_pad_get_current_caps()获取到的当前Caps只会包含一个GstStructure用于表示唯一的数据类型,如果无法获取到当前所使用到Caps,该函数会直接返回NULL。
由于我们已知在本例中new_pad只包含一个音频Cap,所以我们直接通过gst_caps_get_structure()来取得第一个GstStructure。接着再通过gst_structure_get_name() 获取该Cap支持的数据类型,如果不是音频(audio/x-raw),我们直接忽略。

/* Attempt the link */
ret = gst_pad_link (new_pad, sink_pad);
if (GST_PAD_LINK_FAILED (ret)) {g_print ("Type is '%s' but link failed.\n", new_pad_type);
} else {g_print ("Link succeeded (type '%s').\n", new_pad_type);
}

对于音频的Source Pad,我们使用gst_pad_link()将其与Sink Pad进行连接,使用方式与gst_element_link()相同,指定Source和Sink Pad,其所属的Element必须位于同一个Bin或Pipeline。

到目前为止,我们完成了Pipeline的建立,数据会继续在后续的Element中进行音频的播放,直到产生ERROR或EOS。

GStreamer的状态

我们已经知道Pipeline在我们将状态设置为PLAYING之前是不会进入播放状态,实际上PLAYING状态只是GStreamer状态中的一个,GStreamer总共包含4个状态:

  1. NULL:NULL状态是所有Element被创建后的初始状态。
  2. READY:READY状态表明GStreamer已经完成所需资源的检查,可以进入PAUSED状态。
  3. PAUSED:Element处于暂停状态,表明其可以开始接收数据。Sink Element在接收了一个buffer后就会进入等待状态。
  4. PLAYING:Element处于播放状态,时钟处于运行中,数据被依次处理。

GStreamer的状态必须按上面的顺序进行切换,例如:不能直接从NULL切换到PLAYING状态,NULL必须依次切换到READY,PAUSED后才能切换到PLAYING状态,当我们直接设置Pipeline的状态为PLAYING时,GStreamer内部会依次为我们切换到PLAYING状态。

case GST_MESSAGE_STATE_CHANGED:/* We are only interested in state-changed messages from the pipeline */if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data.pipeline)) {GstState old_state, new_state, pending_state;gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);g_print ("Pipeline state changed from %s to %s:\n",gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));}break;

总结

在本教程中,我们学习了:

  • 如何通过GSignals在事件发生时得到通知。
  • 如何直接连接位于两个Element中的Pad。
  • GStreamer中的四种状态。
  • 如何动态连接Pipeline。
  • 后续文章将继续介绍GStreamer时间控制的知识。

引用

https://gstreamer.freedesktop.org/documentation/tutorials/basic/dynamic-pipelines.html?gi-language=c
https://gstreamer.freedesktop.org/documentation/additional/design/states.html?gi-language=c

作者:John.Leng
出处:http://www.cnblogs.com/xleng/
本文版权归作者所有,欢迎转载。商业转载请联系作者获得授权,非商业转载请在文章页面明显位置给出原文连接.

转载于:https://www.cnblogs.com/xleng/p/11194226.html

GStreamer基础教程04 - 动态连接Pipeline相关推荐

  1. 【GStreamer开发】GStreamer基础教程07——多线程和Pad的有效性

    目标 GStreamer会自动处理多线程这部分,但在有些情况下,你需要手动对线程做解耦.本教程会教你怎样才能做到这一点,另外也展示了Pad的有效性.主要内容包括: 如何针对部分的pipeline建立一 ...

  2. GStreamer基础教程10——GStreamer工具

    目标 GStreamer提供了一系列方便使用的工具.这篇教程里不牵涉任何代码,但还是会讲一些有用的内容: 如何在命令行下建立一个pipeline--完全不使用C 如何找出一个element的Capab ...

  3. ArcGIS二次开发基础教程(04):有关字段的操作和简单属性及空间查询

    ArcGIS二次开发基础教程(04):有关字段的操作和简单属性及空间查询 属性 字段的添加.删除和查找 IFeatureLayer GetLayerByName(string name) {ILaye ...

  4. 【GStreamer学习】之GStreamer基础教程

    目标 没有什么比在屏幕上打印出"Hello World"更能获得对软件库的第一印象了! 但是由于我们正在学习多媒体框架,所以我们将输出"Hello World!" ...

  5. GStreamer基础教程01 - Hello World

    摘要 在面对一个新的软件库时,第一步通常实现一个"hello world"程序,来了解库的用法.对于GStreamer,我们可以实现一个极简的播放器,来了解GStreamer的使用 ...

  6. Gstreamer基础教程13:Playback Speed

    文章目录 1.Goal 2.介绍 3. A Trick mode player 3.1 Compile 3.2 Code 4.Analyze 5.讨论 1.Goal 快进.倒放和慢动作都是所谓的技巧模 ...

  7. Gstreamer基础教程10: Gstreamer 工具

    文章目录 1. Goal 2. 介绍 3. gst-lanuch-1.0 3.1 Elements 3.2 Properties 3.3 Named elements 3.4 Pads 3.5 Cap ...

  8. Gstreamer基础教程10:GStreamer tools

    文章目录 目标 一.Introduction(简介) 二.gst-launch-1.0 1.Elements 2.Properties(属性) 3.Named elements(元素重命名) 4.Pa ...

  9. GStreamer基础教程02——GStreamer概念

    上一个教程演示了如何自动生成一个pipeline.这次我们打算用一个个element来手动搭建一个pipeline.我们这个教程会演示: 1. 什么是GStreamer的element以及如何建立一个 ...

最新文章

  1. 图解yolo目标检测如何进行运动估计
  2. VTK:图表之GraphToPolyData
  3. UidGenerator:百度开源的分布式ID服务(解决了时钟回拨问题)
  4. JAVA 编程-张晨光-专题视频课程
  5. 什么是VB.NET的结构化异常处理
  6. 用高等数学“铲雪”!这个200多年前的证明太厉害了,有城市用它省了2000多万..........
  7. 【机器学习】 - CNN
  8. socket网络编程udp
  9. 全国高等学校计算机等级用处,全国计算机等级考试一级有什么用
  10. Ecstore 2.0 报表显示空白
  11. 十个利用矩阵解决的经典题目
  12. 微软DotNet平台升温
  13. Hibernate配置文件与关联映射介绍
  14. Ajax:异步JavaScript和XML的笔记略解,不作为知识参考
  15. 手把手教你学项目管理软件project
  16. docker安装jdk1.8
  17. Android字体设置,Roboto字体使用
  18. 2.三种前端跨域的解决方法
  19. Linux命令行大全(第二版)
  20. java语言数据库课程设计_数据库课程设计 人事管理系统 (一)

热门文章

  1. 解决vue视图不渲染
  2. RabbitMQ之五种消息模型
  3. D: Starry的神奇魔法(矩阵快速幂)
  4. 使用Beautifulsoup去除特定标签
  5. 网络编程-Socket介绍
  6. 运输层的多路复用于多路分解
  7. 通过关闭UseDNS和GSSAPIAuthentication选项加速SSH登录
  8. IOS学习笔记 -- scrollView和tableView整理
  9. python交互解释器_Python 交互解释器
  10. python 多核并行计算_嫌Python太慢?并行运算Process Pools三行代码给你4倍提速!