Gstreamer官方教程汇总基本教程4---Time management

2024-02-05 09:38

本文主要是介绍Gstreamer官方教程汇总基本教程4---Time management,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

https://my.oschina.net/u/735973/blog/203226


摘要: 将gstreamer的官方教程做了一下整理,分享给需要的人们。

Goal

本教程介绍如何使用GStreamer的时间相关的设置。特别是:

  • 如何查询管道的信息比如持流的当前位置和持续时间。

  • 如何寻求(跳跃)到流的不同的位置(时刻)。

Introduction

GstQuery  是一种机制,允许向一个元素或衬垫请求一条信息。在这个例子中,我们询问管道是否可以seeking(对于一些源,如实时流,不允许seeking)。如果允许的话,一旦电影已经运行了十秒钟,我们使用a seek 跳到一个不同的位置。

在前面的教程中,一旦我们有管道安装和运行,我们的主要功能就坐着等通过总线接收错误或EOS。在这里,我们修改这个函数来周期性地唤醒和查询管道来获取流的位置,这样我们就可以在屏幕上打印出来。这是类似于一个媒体播放器,定期更新了用户界面。

最后,流持续时间查询和更新,每当它改变。

Seeking example

将此代码复制到名为basic-tutorial-4.c 一个文本文件:

#include <gst/gst.h>/* Structure to contain all our information, so we can pass it around */
typedef struct _CustomData {GstElement *playbin2;  /* Our one and only element */gboolean playing;      /* Are we in the PLAYING state? */gboolean terminate;    /* Should we terminate execution? */gboolean seek_enabled; /* Is seeking enabled for this media? */gboolean seek_done;    /* Have we performed the seek already? */gint64 duration;       /* How long does this media last, in nanoseconds */
} CustomData;/* Forward definition of the message processing function */
static void handle_message (CustomData *data, GstMessage *msg);int main(int argc, char *argv[]) {CustomData data;GstBus *bus;GstMessage *msg;GstStateChangeReturn ret;data.playing = FALSE;data.terminate = FALSE;data.seek_enabled = FALSE;data.seek_done = FALSE;data.duration = GST_CLOCK_TIME_NONE;/* Initialize GStreamer */gst_init (&argc, &argv);/* Create the elements */data.playbin2 = gst_element_factory_make ("playbin2""playbin2");if (!data.playbin2) {g_printerr ("Not all elements could be created.\n");return -1;}/* Set the URI to play */g_object_set (data.playbin2, "uri""http://docs.gstreamer.com/media/sintel_trailer-480p.webm", NULL);/* Start playing */ret = gst_element_set_state (data.playbin2, 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.playbin2);return -1;}/* Listen to the bus */bus = gst_element_get_bus (data.playbin2);do {msg = gst_bus_timed_pop_filtered (bus, 100 * GST_MSECOND,GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_DURATION);/* Parse message */if (msg != NULL) {handle_message (&data, msg);} else {/* We got no message, this means the timeout expired */if (data.playing) {GstFormat fmt = GST_FORMAT_TIME;gint64 current = -1;/* Query the current position of the stream */if (!gst_element_query_position (data.playbin2, &fmt, &current)) {g_printerr ("Could not query current position.\n");}/* If we didn't know it yet, query the stream duration */if (!GST_CLOCK_TIME_IS_VALID (data.duration)) {if (!gst_element_query_duration (data.playbin2, &fmt, &data.duration)) {g_printerr ("Could not query current duration.\n");}}/* Print current position and total duration */g_print ("Position %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT "\r",GST_TIME_ARGS (current), GST_TIME_ARGS (data.duration));/* If seeking is enabled, we have not done it yet, and the time is right, seek */if (data.seek_enabled && !data.seek_done && current > 10 * GST_SECOND) {g_print ("\nReached 10s, performing seek...\n");gst_element_seek_simple (data.playbin2, GST_FORMAT_TIME,GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, 30 * GST_SECOND);data.seek_done = TRUE;}}}} while (!data.terminate);/* Free resources */gst_object_unref (bus);gst_element_set_state (data.playbin2, GST_STATE_NULL);gst_object_unref (data.playbin2);return 0;
}static void handle_message (CustomData *data, GstMessage *msg) {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);data->terminate = TRUE;break;case GST_MESSAGE_EOS:g_print ("End-Of-Stream reached.\n");data->terminate = TRUE;break;case GST_MESSAGE_DURATION:/* The duration has changed, mark the current one as invalid */data->duration = GST_CLOCK_TIME_NONE;break;case GST_MESSAGE_STATE_CHANGED: {GstState old_state, new_state, pending_state;gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data->playbin2)) {g_print ("Pipeline state changed from %s to %s:\n",gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));/* Remember whether we are in the PLAYING state or not */data->playing = (new_state == GST_STATE_PLAYING);if (data->playing) {/* We just moved to PLAYING. Check if seeking is possible */GstQuery *query;gint64 start, end;query = gst_query_new_seeking (GST_FORMAT_TIME);if (gst_element_query (data->playbin2, query)) {gst_query_parse_seeking (query, NULL, &data->seek_enabled, &start, &end);if (data->seek_enabled) {g_print ("Seeking is ENABLED from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT "\n",GST_TIME_ARGS (start), GST_TIME_ARGS (end));} else {g_print ("Seeking is DISABLED for this stream.\n");}}else {g_printerr ("Seeking query failed.");}gst_query_unref (query);}}} break;default:/* We should not reach here */g_printerr ("Unexpected message received.\n");break;}gst_message_unref (msg);
}

Walkthrough

/* Structure to contain all our information, so we can pass it around */
typedef struct _CustomData {GstElement *playbin2;  /* Our one and only element */gboolean playing;      /* Are we in the PLAYING state? */gboolean terminate;    /* Should we terminate execution? */gboolean seek_enabled; /* Is seeking enabled for this media? */gboolean seek_done;    /* Have we performed the seek already? */gint64 duration;       /* How long does this media last, in nanoseconds */
} CustomData;/* Forward definition of the message processing function */
static void handle_message (CustomData *data, GstMessage *msg);

我们从定义包含所有我们的信息的结构体开始,所以我们可以围绕它传递给其他函数。特别是,在这个例子中,我们将消息处理代码移动到它自己的方法 handle_message 中,因为它的代码太多了。

然后,我们将建立一个单一元素组成的流水线,一个 playbin2,这是我们在 Basic tutorial 1: Hello world! 已经看到。然而,playbin2 本身是一个管道,并且在这种情况下,它是在管道中的唯一的元素,所以我们使用直接playbin2元件。我们将跳过细节:clip的URI通过URI属性给予 playbin2 和将管道设置为播放状态。

msg = gst_bus_timed_pop_filtered (bus100 * GST_MSECOND,GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_DURATION);

以前我们没有提供一个超时信号给 gst_bus_timed_pop_filtered(),这意味着它没有返回,直到收到一个消息。现在我们设置100毫秒为超时时间,所以,如果没有接收到消息时,每秒中有10次将一个NULL代替GstMessage返回。我们将用它来更新我们的“用户界面”。请注意,超时时间被指定在纳秒,所以使用 GST_SECOND或GST_MSECOND宏定义,强烈推荐。

如果我们得到了一个消息,我们在 handle_message 方法中处理它(下一小节),否则:

User interface resfreshing
/* We got no message, this means the timeout expired */
if (data.playing) {

首先,如果我们不是在播放状态下,我们不想在这里做任何事,因为大多数的查询会失败。否则,那就该刷新屏幕了。

我们在这里大约每秒10次,对于我们的UI来说时一个足够好的刷新率。我们将在屏幕上打印当前的媒体位置,这是我们可以学习可以查询管道。这涉及到将在下一小节要显示了几步,但由于位置和持续时间时很常见的查询,继承 GstElement 则更简单,现成的选择:

/* Query the current position of the stream */
if (!gst_element_query_position (data.pipeline, &fmt, &current)) {g_printerr ("Could not query current position.\n");
}

gst_element_query_position() 隐藏了查询对象的管理,并直接为我们提供的结果。

/* If we didn't know it yet, query the stream duration */
if (!GST_CLOCK_TIME_IS_VALID (data.duration)) {if (!gst_element_query_duration (data.pipeline, &fmt, &data.duration)) {g_printerr ("Could not query current duration.\n");}
}

现在是一个很好的时机,知道流长度,与另一继承 GstElement 辅助函数: gst_element_query_duration()

/* Print current position and total duration */
g_print ("Position %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT "\r",GST_TIME_ARGS (current), GST_TIME_ARGS (data.duration));

注意GST_TIME_FORMAT和GST_TIME_ARGS提供的将GStreamer时间转换为对用户友好的表示。

/* If seeking is enabled, we have not done it yet, and the time is right, seek */
if (data.seek_enabled && !data.seek_done && current > 10 * GST_SECOND) {g_print ("\nReached 10sperforming seek...\n");gst_element_seek_simple (data.pipelineGST_FORMAT_TIME,GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, 30 * GST_SECOND);data.seek_done = TRUE;
}

现在我们进行seek,“简单的”在管道上调用 gst_element_seek_simple() 。很多seeking的复杂性都隐藏在这种方法中,这是一件好事!

让我们回顾一下参数:

GST_FORMAT_TIME表明我们正在指定目的地的时间,因为对面的字节(和其他比较模糊的机制)。

随之而来的GstSeekFlags,让我们回顾一下最常见的:

GST_SEEK_FLAG_FLUSH:在seeking之前丢弃目前在管道中的所有数据。可能稍微暂停,而管道回填和新的数据开始到来,但大大增加了应用程序的“可响应性”。如果不提供这个标志,“过时”的数据可能会显示一段时间,直到新的位置出现在管道的末端。

GST_SEEK_FLAG_KEY_UNIT:大多数编码的视频流无法寻求到任意位置,只有某些帧称为关键帧。当使用该标志时,seek将实际移动到最近的关键帧,并开始生产数据,立竿见影。如果不使用这个标志,管道将在内部移动至最近的关键帧(它没有其他选择),但数据不会被显示,直到达到要求的位置。不提供该标志是更准确的,但可能需要更长的时间进行反应。

GST_SEEK_FLAG_ACCURATE:有些媒体剪辑不能提供足够的索引信息,这意味着寻求任意位置非常耗时。在这些情况下,GStreamer的通常在估计的位置寻求,通常工作得很好。如果精度的要求对你来说无所谓,然后提供该标志。被警告,它可能需要更长的时间来计算(非常长,对一些文件)。

最后,我们提供seek的位置。由于我们要求GST_FORMAT_TIME,这个位置是在纳秒,所以我们使用GST_SECOND宏简单标示。

Message Pump

handle_message函数处理通过管道的总线上接收的所有消息。错误和EOS的处理是一样的在前面的教程,所以我们跳到感兴趣的部分:

case GST_MESSAGE_DURATION:/* The duration has changed, mark the current one as invalid */data->duration = GST_CLOCK_TIME_NONE;break;

此消息发布到总线上,不论流的持续时间是否变化。在这里,我们简单地将目前的持续时间为无效,那么它就会被后来重新查询。

case GST_MESSAGE_STATE_CHANGED: {GstState old_state, new_state, pending_state;gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data->pipeline)) {g_print ("Pipeline state changed from %s to %s:\n",gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));/* Remember whether we are in the PLAYING state or not */data->playing = (new_state == GST_STATE_PLAYING);

Seeks和时间查询在处于暂停或播放状态下工作得更好,因为所有的元素都有一个不得不接受信息和配置自己的机会。在这里,我们注意到,我们通过的playing变量可以判定是否处于播放状态。

此外,如果我们刚进入播放状态时,我们做我们的第一个查询。我们询问管道,在当前流中是否支持seeking:

if (data->playing) {/* We just moved to PLAYINGCheck if seeking is possible */GstQuery *query;gint64 startend;query = gst_query_new_seeking (GST_FORMAT_TIME);if (gst_element_query (data->pipelinequery)) {gst_query_parse_seeking (queryNULL, &data->seek_enabled, &start, &end);if (data->seek_enabled) {g_print ("Seeking is ENABLED from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT "\n",GST_TIME_ARGS (start), GST_TIME_ARGS (end));} else {g_print ("Seeking is DISABLED for this stream.\n");}}else {g_printerr ("Seeking query failed.");}gst_query_unref (query);
}

gst_query_new_seeking() 创建一个“seeking”型的、GST_FORMAT_TIME格式的新的查询对象。这表明,我们感兴趣的是通过指定一个新的我们要移动的时间来seeking。我们也可以要求GST_FORMAT_BYTES,并寻求到源文件中特定的字节位置,但正常情况下这没多少益处。

那么这个查询对象通过方法 gst_element_query() 传递到管道。结果被存储在相同的查询,并且可以很容易地通过 gst_query_parse_seeking()检索到。它提取一个表示是否允许seeking的布尔值,和可seeking的范围。

不要忘了释放查询对象。

就是这样!有了这些知识,我们可以构建一个周期性更新滑块的媒体播放器,并可以在当前流位置,允许seeking通过移动滑块!

Conclusion

本教程显示:

  • 如何使用  GstQuery 查询管道信息

  • 如何使用 gst_element_query_position() 和 gst_element_query_duration(),以获得类似的位置和持续时间共同信息

  • 如何使用  gst_element_seek_simple() 寻求在流中的任意位置

  • 在其中规定所有这些操作都可以进行

接下来的教程将介绍如何使用GStreamer的一个图形用户界面工具包。

请记住,此页你应该找到本教程的完整源代码,并建立它需要的任何附件文件。

很高兴在此与你一起度过,并希望在以后的教程继续见到你!






这篇关于Gstreamer官方教程汇总基本教程4---Time management的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/680512

相关文章

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

如何为Yarn配置国内源的详细教程

《如何为Yarn配置国内源的详细教程》在使用Yarn进行项目开发时,由于网络原因,直接使用官方源可能会导致下载速度慢或连接失败,配置国内源可以显著提高包的下载速度和稳定性,本文将详细介绍如何为Yarn... 目录一、查询当前使用的镜像源二、设置国内源1. 设置为淘宝镜像源2. 设置为其他国内源三、还原为官方

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

MySQL 中的 LIMIT 语句及基本用法

《MySQL中的LIMIT语句及基本用法》LIMIT语句用于限制查询返回的行数,常用于分页查询或取部分数据,提高查询效率,:本文主要介绍MySQL中的LIMIT语句,需要的朋友可以参考下... 目录mysql 中的 LIMIT 语句1. LIMIT 语法2. LIMIT 基本用法(1) 获取前 N 行数据(

Python虚拟环境终极(含PyCharm的使用教程)

《Python虚拟环境终极(含PyCharm的使用教程)》:本文主要介绍Python虚拟环境终极(含PyCharm的使用教程),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录一、为什么需要虚拟环境?二、虚拟环境创建方式对比三、命令行创建虚拟环境(venv)3.1 基础命令3

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

python连接本地SQL server详细图文教程

《python连接本地SQLserver详细图文教程》在数据分析领域,经常需要从数据库中获取数据进行分析和处理,下面:本文主要介绍python连接本地SQLserver的相关资料,文中通过代码... 目录一.设置本地账号1.新建用户2.开启双重验证3,开启TCP/IP本地服务二js.python连接实例1.

Python Faker库基本用法详解

《PythonFaker库基本用法详解》Faker是一个非常强大的库,适用于生成各种类型的伪随机数据,可以帮助开发者在测试、数据生成、或其他需要随机数据的场景中提高效率,本文给大家介绍PythonF... 目录安装基本用法主要功能示例代码语言和地区生成多条假数据自定义字段小结Faker 是一个 python

Python 安装和配置flask, flask_cors的图文教程

《Python安装和配置flask,flask_cors的图文教程》:本文主要介绍Python安装和配置flask,flask_cors的图文教程,本文通过图文并茂的形式给大家介绍的非常详细,... 目录一.python安装:二,配置环境变量,三:检查Python安装和环境变量,四:安装flask和flas