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

相关文章

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文

SpringBoot连接Redis集群教程

《SpringBoot连接Redis集群教程》:本文主要介绍SpringBoot连接Redis集群教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 依赖2. 修改配置文件3. 创建RedisClusterConfig4. 测试总结1. 依赖 <de

SQL BETWEEN 语句的基本用法详解

《SQLBETWEEN语句的基本用法详解》SQLBETWEEN语句是一个用于在SQL查询中指定查询条件的重要工具,它允许用户指定一个范围,用于筛选符合特定条件的记录,本文将详细介绍BETWEEN语... 目录概述BETWEEN 语句的基本用法BETWEEN 语句的示例示例 1:查询年龄在 20 到 30 岁

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根