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

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 (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宏定义,强烈推荐。

如果我们得到了一个消息,我们在 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 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宏简单标示。

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 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通过移动滑块!

Conclusion

本教程显示:

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

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

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


转载于:https://my.oschina.net/u/735973/blog/203226

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值