GStreamer编程实例4:时间管理

目标

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

  • 如何查询管道的信息,如流位置或持续时间.
  • 如何寻找(跳跃)到一个不同的位置(时间)在流.

导言

GstQuery是一种机制,它允许要求元素或输入板提供每个信息。在这个例子中,我们问管道是否允许寻找(有些来源,如活流,不允许寻找)。如果允许的话,那么,一旦这部电影运行了10秒钟,我们就会用寻索来换一个位置。

在以前的教程中,一旦我们安装并运行了管道,我们的主要功能就会坐等着接收ERROR 或者EOS 从公车上穿过去。在这里,我们修改这个函数来定期唤醒并查询管道的流位置,这样我们就可以在屏幕上打印它。这类似于媒体播放器会做什么,定期更新用户界面。

最后,每当流发生变化时,都会查询和更新流持续时间。

例子

把这个代码复制到一个命名的文本文件中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 , Macosx 或 窗户 ,或在Linux上使用此特定命令:

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

如果您需要帮助来运行此代码,请参阅 运行教程 平台部分: Linux , Macosx 或 窗户 .

本教程打开一个窗口,并显示一个电影,随附音频.媒体是从互联网上获得的,所以窗口可能需要几秒钟才能出现,这取决于您的连接速度。在电影开始前的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);

我们首先定义一个包含我们所有信息的结构,因此wecan将它传递给其他函数。特别是,在这个示例中,将消息处理代码转移到自己的函数中 handle_message 因为它越来越大了。

然后我们建立一个由单一元素组成的管道, playbin 我们已经看到了 基本教程1:海洛威! . However, 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_SECOND 或GST_MSECOND .这也使代码更具可读性。

如果我们收到消息,我们会在handle_message 职能(下一小节),否则:

用户界面更新

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

如果管道进入PLAYING 状态,是时候刷新屏幕了。如果我们不在的话我们什么也不想做PLAYING 声明,因为大多数查询都会失败。

我们大约每秒到这里10次,足够我们的用户界面。我们将在屏幕上打印当前的媒体,我们可以通过查询管道来学习。这涉及一些步骤,将在下一个小节显示,但是,由于位置和持续时间是足够常见的查询,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_FORMAT 和GST_TIME_ARGS 宏提供了一个方便用户的表现格兰姆时代。

/* 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;
}

现在,我们通过呼叫来"简单地"进行搜寻gst_element_seek_simple() 在管道上。在这种方法中隐藏了许多寻求的复杂性,这是一件好事!

让我们回顾一下参数:

GST_FORMAT_TIME表示我们正在指定目标时间单元。其他的搜索型使用不同的单位。

接下来是GstSeekFlags ,让我们回顾一下最常见的:

GST_SEEK_FLAG_FLUSH:在进行搜寻之前,此方法会抛弃所有目前在管道中的数据。当管道重新填充,新数据开始出现时,可能会暂停一点点,但会大大提高应用程序的"响应性"。如果不提供这个标志,那么"陈旧"的数据可能会显示一段时间,直到新的位置出现在管道的尽头。

GST_SEEK_FLAG_KEY_UNIT:对于大多数编码视频流,寻求任意位置是不可能的,只对某些被称为键帧的帧。当这个标志被使用时,搜索将实际上移动到最近的键框架,并立即开始生成数据。如果不使用这个标志,则该管道将在内部移动到最近的键框(它没有其他选择),但在达到所需时才会显示数据。最后一种选择更准确,但可能需要更长的时间。

GST_SEEK_FLAG_ACCURATE:有些媒体片段没有提供足够的索引信息,这意味着试图任意设置会消耗资源。在这些情况下,格莱韦勒通常估计要寻求的位置,通常工作很好。如果这个精确度对你的情况不合适(你要知道,不适合你要求的精确时间),那么提供这个标志。请注意,可能需要较长时间才能计算出寻找的位置(在某些文件上很长)。

最后,我们提供了寻求的位置。因为我们要求GST_FORMAT_TIME ,该值必须以纳秒为单位,所以我们用秒表示时间,为了简单起见,然后乘以GST_SECOND .

信息

handle_message 功能处理通过管道总线接收的所有消息。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->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);

查询和时间查询通常只在 PAUSED 或PLAYING 声明,因为所有元素都有机会接收信息并进行自我配置。在这里,我们使用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 格式。这表明我们有兴趣通过指定我们想要的新时间来寻找。我们还可以要求GST_FORMAT_BYTES ,然后在源文件中寻求特定的字节位置,但这通常没有用处。

然后将此查询对象传递到管道中 gst_element_query() .结果存储在同一个查询中,可以很容易地通过gst_query_parse_seeking() .它提取阿博勒值,指示是否允许寻找,以及可以寻找的范围。

当您完成了查询对象时,不要忘记将它恢复为查询对象。

就这样!有了这些知识,媒体播放器就可以建立起来,它可以根据流的位置定期更新一个滑块,并允许通过移动滑块来寻找!

结论

本教程显示:

  • 如何查询管道中的信息GstQuery

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

  • 如何寻求在GStreamer的任意位置gst_element_seek_simple()

  • 在其中可以执行所有这些操作。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值