本教程介绍如何使用GStreamer的时间相关的设置。特别是:html
如何查询管道的信息好比持流的当前位置和持续时间。web
如何寻求(跳跃)到流的不一样的位置(时刻)。函数
GstQuery
是一种机制,容许向一个元素或衬垫请求一条信息。在这个例子中,咱们询问管道是否能够seeking(对于一些源,如实时流,不容许seeking)。若是容许的话,一旦电影已经运行了十秒钟,咱们使用a seek 跳到一个不一样的位置。工具
在前面的教程中,一旦咱们有管道安装和运行,咱们的主要功能就坐着等经过总线接收错误或EOS。在这里,咱们修改这个函数来周期性地唤醒和查询管道来获取流的位置,这样咱们就能够在屏幕上打印出来。这是相似于一个媒体播放器,按期更新了用户界面。学习
最后,流持续时间查询和更新,每当它改变。this
将此代码复制到名为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, ¤t)) { 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); }
/* 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 中,由于它的代码太多了。spa
而后,咱们将创建一个单一元素组成的流水线,一个 playbin2
,这是咱们在 Basic tutorial 1: Hello world! 已经看到。然而,playbin2 自己是一个管道,而且在这种状况下,它是在管道中的惟一的元素,因此咱们使用直接playbin2元件。咱们将跳过细节:clip的URI经过URI属性给予 playbin2 和将管道设置为播放状态。debug
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毫秒为超时时间,因此,若是没有接收到消息时,每秒中有10次将一个NULL代替GstMessage返回。咱们将用它来更新咱们的“用户界面”。请注意,超时时间被指定在纳秒,因此使用 GST_SECOND或GST_MSECOND宏定义,强烈推荐。code
若是咱们获得了一个消息,咱们在 handle_message 方法中处理它(下一小节),不然:
/* 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, ¤t)) { 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 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; }
如今咱们进行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宏简单标示。
该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 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()
建立一个“seeking”型的、GST_FORMAT_TIME格式的新的查询对象。这代表,咱们感兴趣的是经过指定一个新的咱们要移动的时间来seeking。咱们也能够要求GST_FORMAT_BYTES,并寻求到源文件中特定的字节位置,但正常状况下这没多少益处。
那么这个查询对象经过方法 gst_element_query()
传递到管道。结果被存储在相同的查询,而且能够很容易地经过 gst_query_parse_seeking()
检索到。它提取一个表示是否容许seeking的布尔值,和可seeking的范围。
不要忘了释放查询对象。
就是这样!有了这些知识,咱们能够构建一个周期性更新滑块的媒体播放器,并能够在当前流位置,容许seeking经过移动滑块!
本教程显示:
如何使用 GstQuery
查询管道信息
如何使用 gst_element_query_position() 和 gst_element_query_duration(),以得到相似的位置和持续时间共同信息
如何使用 gst_element_seek_simple()
寻求在流中的任意位置
在其中规定全部这些操做均可以进行
接下来的教程将介绍如何使用GStreamer的一个图形用户界面工具包。
请记住,此页你应该找到本教程的完整源代码,并创建它须要的任何附件文件。
很高兴在此与你一块儿度过,并但愿在之后的教程继续见到你!