目标
本教程主要讲述一些和时间相关的内容。主要包括:
1. 如何问pipeline查询到流的总时间和当前播放的时间
2. 如何在流内部实现跳转功能
介绍
GstQuery是向一个element或者pad询问一些信息的机制。在这个例子中我们会问pipeline是否支持跳转功能(实时流是不支持跳转功能的),如果支持跳转功能,那么在播放了10s之后跳转到另一个位置。
在前面的教程里,我们一旦建立pipeline并运行后,我们就是在等待ERROR或者EOS消息。这个例子里面我们修改一下这个部分,改成定时唤醒并查询pipeline当前播放的位置并在屏幕上显示出来。这个已经和播放器比较类似了,定时刷新UI。
最后,我们会查询流的总时间并且在变化后刷新。
seek的例子
- #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, charchar *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. ");
- 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. ");
- 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, 1100 * 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. ");
- }
- /* 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. ");
- }
- }
- /* Print current position and total duration */
- g_print ("Position %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT " ",
- 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 > 110 * GST_SECOND) {
- g_print (" Reached 10s, performing seek... ");
- gst_element_seek_simple (data.playbin2, GST_FORMAT_TIME,
- GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, 330 * 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 ", GST_OBJECT_NAME (msg->src), err->message);
- g_printerr ("Debugging information: %s ", 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. ");
- 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: ",
- 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 " ",
- GST_TIME_ARGS (start), GST_TIME_ARGS (end));
- } else {
- g_print ("Seeking is DISABLED for this stream. ");
- }
- }
- else {
- g_printerr ("Seeking query failed.");
- }
- gst_query_unref (query);
- }
- }
- } break;
- default:
- /* We should not reach here */
- g_printerr ("Unexpected message received. ");
- 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;
我们建立一个仅仅包含playbin2的一个pipeline,就和我们在教程01里面做的一样。因为这里pipeline就包含一个playbin2,所以playbin2就是pipeline了,我们直接操作playbin2这个element就可以了。我们略过已经熟悉的诸如通过设置URI属性来传入播放地址这些细节。
- msg = gst_bus_timed_pop_filtered (bus, 1100 * GST_MSECOND,
- GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_DURATION);
如果我们获得的是一个消息,我们就调用handle_message来处理,否则:
UI刷新
- /* We got no message, this means the timeout expired */
- if (data.playing) {
- /* Query the current position of the stream */
- if (!gst_element_query_position (data.playbin2, &fmt, ¤t)) {
- g_printerr ("Could not query current 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.playbin2, &fmt, &data.duration)) {
- g_printerr ("Could not query current duration. ");
- }
- }
- /* Print current position and total duration */
- g_print ("Position %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT " ",
- 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 > 110 * GST_SECOND) {
- g_print (" Reached 10s, performing seek... ");
- gst_element_seek_simple (data.playbin2, GST_FORMAT_TIME,
- GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, 330 * GST_SECOND);
- data.seek_done = TRUE;
- }
让我们看一下这个方法的各个参数:
GST_FORMAT_TIME说明我们的操作(跳转)是针对时间来计算的(有两种计算方式,时间和数据)。
然后是GstSeekFlags,这里主要介绍常见的几个:
GST_SEEK_FLAG_FLUSH:跳转后会丢弃当前pipeline里面所有的数据,因为要重新解析一段数据,所以会有一个停顿,但在用户看来可以保证应用的快速响应;反之,就会继续播放一点内容,然后再跳转。
GST_SEEK_FLAG_KEY_UNIT:大部分编码的视频流是无法精确定位到某个特定时刻的,只能是到某些帧(称为key frame)。当这个标志被置时,会自动定位到最近的一个key frame然后开始播放。如果这个标志不置,那么pipeline会跳到最接近的一个key frame,然后开始播放,但此时不输出任何东西,直到到达设定的位置。综合来看,不设置这个标志会更加准确,但是响应时间会显得较长。
GST_SEEK_FLAG_ACCURATE:有些时候我们没法获得足够的索引信息,这个时候跳转到某个位置会非常耗时。在这种情况下,GStreamer通常就是估计一下大概的位置(一般都很准确)。如果你实际运行发现这个不够准确,那么可以置这个标志位。必须了解的是,这个标志位一旦设置,跳转的时间会大大增加。
最后,我们会跳转到某个地方,因为我们用的是GST_FORMAT_TIME,时间的单位是用纳秒来计算的,所以需要用GST_SECOND宏来转换一下。
消息泵
handle_message()会处理所有pipeline总线上收到的消息。ERROR和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->playbin2)) {
- g_print ("Pipeline state changed from %s to %s: ",
- 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 " ",
- GST_TIME_ARGS (start), GST_TIME_ARGS (end));
- } else {
- g_print ("Seeking is DISABLED for this stream. ");
- }
- }
- else {
- g_printerr ("Seeking query failed.");
- }
- gst_query_unref (query);
- }
这个查询对象会通过gst_element_query()传给pipeline,结果也存在这个query里面,通过gst_query_parse_seeking()可以很方便的获得。这个方法的返回值是个布尔量,用来表面流是否支持跳转这个功能。
在处理结束后不要忘记释放query资源。
通过这样的方法,播放器可以定时的刷新一个拖动条,并且可以支持拖动一个滑块来实现跳转功能。