GStreamer——教程——基础教程4:Time management

2024-06-17 12:20

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

基础教程4:Time management(时间管理)

目标

本教程展示了如何使用GStreamer时间相关工具。特别是:

  • 如何查询管道以获取流位置或持续时间等信息。
  • 如何寻找(跳转)到流内的不同位置(时间)。

介绍

GstQuery是一种机制,允许向元素或 pad 询问 一条信息。在此示例中,我们询问 pipeline 是否正在寻找允许(某些来源,如直播,不允许查找)。如果它是允许的,那么,一旦movie 播放了十秒钟,我们使用查找跳到不同的位置。

在前面的教程中,一旦我们设置并运行了pipeline , 我们的主要功能只是等待接收 ERROR 或 EOS 通过 bus 。在这里,我们修改这个函数以定期唤醒并查询pipeline 中的流位置,以便我们可以打印它 屏幕。这类似于媒体播放器会做的事情,更新定期的用户界面。

最后,只要听歌时长发生变化,就会进行查询和更新。

寻求的例子

将此代码复制到名为basic-tutorial-4.c文本文件中(或找到它 在您的GStreamer安装中)。

basic-tutorial-4.c

#include <gst/gst.h>/* Structure to contain all our information, so we can pass it around */
typedef struct _CustomData {GstElement *playbin;  /* 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.playbin = gst_element_factory_make ("playbin", "playbin");if (!data.playbin) {g_printerr ("Not all elements could be created.\n");return -1;}/* Set the URI to play */g_object_set (data.playbin, "uri", "https://gstreamer.freedesktop.org/data/media/sintel_trailer-480p.webm", NULL);/* Start playing */ret = gst_element_set_state (data.playbin, 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.playbin);return -1;}/* Listen to the bus */bus = gst_element_get_bus (data.playbin);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) {gint64 current = -1;/* Query the current position of the stream */if (!gst_element_query_position (data.playbin, GST_FORMAT_TIME, &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.playbin, GST_FORMAT_TIME, &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.playbin, 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.playbin, GST_STATE_NULL);gst_object_unref (data.playbin);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 ("\nEnd-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->playbin)) {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->playbin, 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);
}

需要帮忙吗?

如果您需要帮助来编译此代码,请参阅为您的平台构建教程部分:Linux、Mac OS X或Windows,或在Linux上使用此特定命令:

gcc basic-tutorial-4.c -o basic-tutorial-4 `pkg-config --cflags --libs gstreamer-1.0`

如果您需要帮助来运行此代码,请参阅为您的平台运行教程部分:Linux、Mac OS X或Windows。

本教程打开一个窗口并显示一个带有音频的电影。媒体是从Internet获取的,因此窗口可能需要几秒钟才能出现,具体取决于您的连接速度。进入电影10秒后,它会跳到一个新位置

所需库:gstreamer-1.0

工作流

/* Structure to contain all our information, so we can pass it around */
typedef struct _CustomData {GstElement *playbin;  /* 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因为它长得有点太大了。

然后我们构建一个由单个元素组成的管道 playbin,我们已经在基本教程1:Hello world!了解。playbin本身就是一个管道,在这种情况下,它是唯一的 元素,所以我们直接使用playbin元素。我们将跳过细节:剪辑的URI通过playbin提供给 URI属性和管道设置为播放状态。

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

以前我们没有提供超时 gst_bus_timed_pop_filtered(),这意味着它直到消息已收到。现在我们使用100毫秒的超时,所以,如果十分之一秒内没有收到消息,函数将返回 NULL。我们将使用这个逻辑来更新我们的"UI"。

请注意,所需的超时必须指定为GstClockTime,因此, 以纳秒为单位。表示不同时间单位的数字应该是 乘以GST_SECONDGST_MSECOND之类的宏 您的代码更具可读性。

如果我们收到消息,我们在handle_message函数中处理它 (下一小节),否则:

用户接口刷新

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

如果pipeline 处于PLAYING状态,则是刷新屏幕的时候了。 如果我们不在PLAYING状态,我们什么都不想做,因为大多数查询都会失败。

我们到达这里大约每秒10次,足够刷新了为我们的UI评分。我们将在屏幕上打印当前媒体位置,我们可以通过查询管道来学习。这涉及一个下一个小节将显示的几个步骤,但是,由于位置 和持续时间是常见的查询,GstElement提供更容易, 现成的替代品:

/* Query the current position of the stream */
if (!gst_element_query_position (data.pipeline, GST_FORMAT_TIME, &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, GST_FORMAT_TIME, &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_FORMATGST_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 10s, performing seek...\n");gst_element_seek_simple (data.pipeline, GST_FORMAT_TIME,GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, 30 * GST_SECOND);data.seek_done = TRUE;
}

现在我们执行寻找,“simply”通过在pipeline上调用gst_element_seek_simple()。很多寻找的复杂性隐藏在这种方法中,这是一个很好的东西!

让我们回顾一下参数:

GST_FORMAT_TIME 表示我们正在指定目的地时间单位。其他查找格式使用不同的单位。

然后是 GstSeekFlags,让我们回顾一下最常见的:

GST_SEEK_FLAG_FLUSH:这将丢弃当前在pipeline 中的所有数据在寻找之前。pipeline 重新填充时可能会暂停一点新的数据开始出现,但大大增加了应用程序的“响应能力”。如果未提供此标志, “stale”数据可能会显示一段时间,直到新位置出现在pipeline 的末端。

GST_SEEK_FLAG_KEY_UNIT:对于大多数编码视频流,寻求任意位置是不可能的,但只能用于称为关键帧的某些帧。当这使用标志,查找实际上会移动到最近的关键帧和立即开始生成数据。如果不使用此标志,则pipeline 将在内部移动到最近的关键帧(它没有其他替代)但数据在达到请求之前不会显示 位置。最后一个选择更准确,但可能需要更长时间。

GST_SEEK_FLAG_ACCURATE:某些媒体剪辑不提供足够的索引信息,这意味着定位到任意位置很耗时。在这些情况下,GStreamer通常会估计要定位的位置,通常效果还不错。如果这种精度对你的案例来说不够好(你看到的定位没有到达你要求确切的时间),那么就提供这个标志。请注意,计算定位位置可能需要更长的时间(在某些文件上可能会非常长)。

最后,我们提供要定位的位置。由于我们要求使用GST_FORMAT_TIME,因此该值必须以纳秒为单位,因此为了简单起见,我们将时间表示为秒,然后再乘以GST_SECOND。

消息Pump

handle_message函数处理通过管道总线接收到的所有消息。错误和结束的处理与以前的教程相同,所以我们跳到有趣的部分:

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);

查找和时间查询通常只在 PAUSEDPLAYING状态,因为所有元素都有机会接收信息并配置自己。在这里,我们使用playing 变量来跟踪管道是否处于PLAYING状态。 此外,如果我们刚刚进入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->pipeline, 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);
}

gst_query_new_seeking()以GST_FORMAT_TIME格式创建一个新的“seeking”类型查询对象。这表明我们有兴趣通过指定我们想要移动的新时间来进行定位。我们也可以选择询问GST_FORMAT_BYTES,然后定位到源文件内的特定字节位置,但这通常较少使用。

然后,这个查询对象通过gst_element_query()传递给管道。结果存储在同一个查询中,可以用gst_query_parse_seeking()轻松检索。它提取一个布尔值,表示是否允许定位,以及可能进行定位的范围。

当你完成查询对象的使用时,别忘了取消引用它。

就这样!有了这些知识,就可以构建一个媒体播放器,它可以根据当前流位置定期更新滑块,并通过移动滑块来实现定位!

结论

本教程显示:

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

  • 如何使用gst_element_query_position()和gst_element_query_duration()获取常见信息,如位置和持续时间

  • 如何使用gst_element_seek_simple()定位到流中的任意位置

  • 所有这些操作可以在哪些状态下执行。

这篇关于GStreamer——教程——基础教程4:Time management的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1069430

相关文章

MySQL 安装配置超完整教程

《MySQL安装配置超完整教程》MySQL是一款广泛使用的开源关系型数据库管理系统(RDBMS),由瑞典MySQLAB公司开发,目前属于Oracle公司旗下产品,:本文主要介绍MySQL安装配置... 目录一、mysql 简介二、下载 MySQL三、安装 MySQL四、配置环境变量五、配置 MySQL5.1

MQTT SpringBoot整合实战教程

《MQTTSpringBoot整合实战教程》:本文主要介绍MQTTSpringBoot整合实战教程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录MQTT-SpringBoot创建简单 SpringBoot 项目导入必须依赖增加MQTT相关配置编写

在Java中基于Geotools对PostGIS数据库的空间查询实践教程

《在Java中基于Geotools对PostGIS数据库的空间查询实践教程》本文将深入探讨这一实践,从连接配置到复杂空间查询操作,包括点查询、区域范围查询以及空间关系判断等,全方位展示如何在Java环... 目录前言一、相关技术背景介绍1、评价对象AOI2、数据处理流程二、对AOI空间范围查询实践1、空间查

Logback在SpringBoot中的详细配置教程

《Logback在SpringBoot中的详细配置教程》SpringBoot默认会加载classpath下的logback-spring.xml(推荐)或logback.xml作为Logback的配置... 目录1. Logback 配置文件2. 基础配置示例3. 关键配置项说明Appender(日志输出器

Kali Linux安装实现教程(亲测有效)

《KaliLinux安装实现教程(亲测有效)》:本文主要介绍KaliLinux安装实现教程(亲测有效),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、下载二、安装总结一、下载1、点http://www.chinasem.cn击链接 Get Kali | Kal

Web技术与Nginx网站环境部署教程

《Web技术与Nginx网站环境部署教程》:本文主要介绍Web技术与Nginx网站环境部署教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Web基础1.域名系统DNS2.Hosts文件3.DNS4.域名注册二.网页与html1.网页概述2.HTML概述3.

spring security 超详细使用教程及如何接入springboot、前后端分离

《springsecurity超详细使用教程及如何接入springboot、前后端分离》SpringSecurity是一个强大且可扩展的框架,用于保护Java应用程序,尤其是基于Spring的应用... 目录1、准备工作1.1 引入依赖1.2 用户认证的配置1.3 基本的配置1.4 常用配置2、加密1. 密

WinForms中主要控件的详细使用教程

《WinForms中主要控件的详细使用教程》WinForms(WindowsForms)是Microsoft提供的用于构建Windows桌面应用程序的框架,它提供了丰富的控件集合,可以满足各种UI设计... 目录一、基础控件1. Button (按钮)2. Label (标签)3. TextBox (文本框

C#实现访问远程硬盘的图文教程

《C#实现访问远程硬盘的图文教程》在现实场景中,我们经常用到远程桌面功能,而在某些场景下,我们需要使用类似的远程硬盘功能,这样能非常方便地操作对方电脑磁盘的目录、以及传送文件,这次我们将给出一个完整的... 目录引言一. 远程硬盘功能展示二. 远程硬盘代码实现1. 底层业务通信实现2. UI 实现三. De

ubuntu20.0.4系统中安装Anaconda的超详细图文教程

《ubuntu20.0.4系统中安装Anaconda的超详细图文教程》:本文主要介绍了在Ubuntu系统中如何下载和安装Anaconda,提供了两种方法,详细内容请阅读本文,希望能对你有所帮助... 本文介绍了在Ubuntu系统中如何下载和安装Anaconda。提供了两种方法,包括通过网页手动下载和使用wg