GStreamer Tutorial 中文翻译:Basic tutorial 13: Playback speed

GStreamer Tutorial 13中文翻译



前言

由于工作原因,用的GStreamer的图像解码库,所以记录GStreamer Tutorial 的中文翻译和个人的理解以便学习。若有不足请多指教。侵删。


Basic tutorial 13: Playback speed

目的

快进、倒放和慢动作都是被统称为技巧模式的技术,它们都有共同之处,可以修改正常的播放速度。本教程将展示如何实现这些效果,并将步进与步退添加到程序中。本例程展示了:

  • 如何改变播放速度,比正常速度快和慢、快进和快退;
  • 如何逐帧推进视频。

简介

快进是一种以高于正常(预期)速度播放媒体的技术;而慢动作使用的速度比预期的要低。反向播放也做同样的事情,但是是反向的,从流的末尾到开始。
所有这些技术所做的就是改变播放速率,这个变量在正常播放时等于1.0,在快模式时大于1.0(绝对值),在慢模式时小于1.0(绝对值),正向播放为正,反向播放为负。
GStreamer提供两种机制来更改回放速率:步骤事件和查找事件。Step Events允许跳过给定数量的媒体,除了改变随后的播放速率(仅为正值)。另外,Seek Events允许跳转到流中的任何位置,并设置正和负播放速率。
在基本教程4:时间管理搜索事件中已经展示了使用helper函数来隐藏它们的复杂性。本教程进一步解释了如何使用这些事件。
Step Events是一种更方便的改变播放速率的方法,因为它减少了创建它们所需的参数数量;然而,它们也有一些缺点,所以本教程使用的是Seek Events。步骤事件只影响sink(在pipeline的末端),所以它们只有在pipeline的其余部分能够支持以不同的速度运行时才会起作用,而Seek事件则贯穿整个pipeline,因此每个元素都可以对它们做出反应。Step事件的好处是,它们可以更快地采取行动。Step事件也不能更改回放方向。
要使用这些事件,需要先创建它们,然后将它们传递到pipeline中,在pipeline中它们向上游传播,直到到达能够处理它们的元素为止。如果一个事件被传递到像playbin这样的bin元素,它将简单地将该事件提供给它的所有接收器,这将导致执行多个搜索。常见的方法是通过视频接收器或音频接收器属性检索playbin的接收器,并将事件直接输入接收器。
帧步进是一种允许逐帧播放视频的技术。它是通过暂停pipeline来实现的,然后每次发送Step Events来跳过一帧。

技巧模式播放器示例

将代码复制到basic-tutorial-13.c中,或在你的GStreamer路径中找到它。
basic-tutorial-13.c

#include <string.h>
#include <stdio.h>
#include <gst/gst.h>

typedef struct _CustomData
{
  GstElement *pipeline;
  GstElement *video_sink;
  GMainLoop *loop;

  gboolean playing;             /* Playing or Paused */
  gdouble rate;                 /* Current playback rate (can be negative) */
} CustomData;

/* Send seek event to change rate */
static void
send_seek_event (CustomData * data)
{
  gint64 position;
  GstEvent *seek_event;

  /* Obtain the current position, needed for the seek event */
  if (!gst_element_query_position (data->pipeline, GST_FORMAT_TIME, &position)) {
    g_printerr ("Unable to retrieve current position.\n");
    return;
  }

  /* Create the seek event */
  if (data->rate > 0) {
    seek_event =
        gst_event_new_seek (data->rate, GST_FORMAT_TIME,
        GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET,
        position, GST_SEEK_TYPE_END, 0);
  } else {
    seek_event =
        gst_event_new_seek (data->rate, GST_FORMAT_TIME,
        GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, 0,
        GST_SEEK_TYPE_SET, position);
  }

  if (data->video_sink == NULL) {
    /* If we have not done so, obtain the sink through which we will send the seek events */
    g_object_get (data->pipeline, "video-sink", &data->video_sink, NULL);
  }

  /* Send the event */
  gst_element_send_event (data->video_sink, seek_event);

  g_print ("Current rate: %g\n", data->rate);
}

/* Process keyboard input */
static gboolean
handle_keyboard (GIOChannel * source, GIOCondition cond, CustomData * data)
{
  gchar *str = NULL;

  if (g_io_channel_read_line (source, &str, NULL, NULL,
          NULL) != G_IO_STATUS_NORMAL) {
    return TRUE;
  }

  switch (g_ascii_tolower (str[0])) {
    case 'p':
      data->playing = !data->playing;
      gst_element_set_state (data->pipeline,
          data->playing ? GST_STATE_PLAYING : GST_STATE_PAUSED);
      g_print ("Setting state to %s\n", data->playing ? "PLAYING" : "PAUSE");
      break;
    case 's':
      if (g_ascii_isupper (str[0])) {
        data->rate *= 2.0;
      } else {
        data->rate /= 2.0;
      }
      send_seek_event (data);
      break;
    case 'd':
      data->rate *= -1.0;
      send_seek_event (data);
      break;
    case 'n':
      if (data->video_sink == NULL) {
        /* If we have not done so, obtain the sink through which we will send the step events */
        g_object_get (data->pipeline, "video-sink", &data->video_sink, NULL);
      }

      gst_element_send_event (data->video_sink,
          gst_event_new_step (GST_FORMAT_BUFFERS, 1, ABS (data->rate), TRUE,
              FALSE));
      g_print ("Stepping one frame\n");
      break;
    case 'q':
      g_main_loop_quit (data->loop);
      break;
    default:
      break;
  }

  g_free (str);

  return TRUE;
}

int
main (int argc, char *argv[])
{
  CustomData data;
  GstStateChangeReturn ret;
  GIOChannel *io_stdin;

  /* Initialize GStreamer */
  gst_init (&argc, &argv);

  /* Initialize our data structure */
  memset (&data, 0, sizeof (data));

  /* Print usage map */
  g_print ("USAGE: Choose one of the following options, then press enter:\n"
      " 'P' to toggle between PAUSE and PLAY\n"
      " 'S' to increase playback speed, 's' to decrease playback speed\n"
      " 'D' to toggle playback direction\n"
      " 'N' to move to next frame (in the current direction, better in PAUSE)\n"
      " 'Q' to quit\n");

  /* Build the pipeline */
  data.pipeline =
      gst_parse_launch
      ("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm",
      NULL);

  /* Add a keyboard watch so we get notified of keystrokes */
#ifdef G_OS_WIN32
  io_stdin = g_io_channel_win32_new_fd (fileno (stdin));
#else
  io_stdin = g_io_channel_unix_new (fileno (stdin));
#endif
  g_io_add_watch (io_stdin, G_IO_IN, (GIOFunc) handle_keyboard, &data);

  /* Start playing */
  ret = gst_element_set_state (data.pipeline, 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.pipeline);
    return -1;
  }
  data.playing = TRUE;
  data.rate = 1.0;

  /* Create a GLib Main Loop and set it to run */
  data.loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (data.loop);

  /* Free resources */
  g_main_loop_unref (data.loop);
  g_io_channel_unref (io_stdin);
  gst_element_set_state (data.pipeline, GST_STATE_NULL);
  if (data.video_sink != NULL)
    gst_object_unref (data.video_sink);
  gst_object_unref (data.pipeline);
  return 0;
}

代码走读

main函数中的初始化代码中没有什么新内容:实例化了一个playbin pipeline,安装了一个I/O监视器来跟踪按键点击,并执行了一个GLib主循环。
然后,在键盘处理函数中:

/* Process keyboard input */
static gboolean handle_keyboard (GIOChannel *source, GIOCondition cond, CustomData *data) {
  gchar *str = NULL;

  if (g_io_channel_read_line (source, &str, NULL, NULL, NULL) != G_IO_STATUS_NORMAL) {
    return TRUE;
  }

  switch (g_ascii_tolower (str[0])) {
  case 'p':
    data->playing = !data->playing;
    gst_element_set_state (data->pipeline, data->playing ? GST_STATE_PLAYING : GST_STATE_PAUSED);
    g_print ("Setting state to %s\n", data->playing ? "PLAYING" : "PAUSE");
    break;

暂停/播放切换由gst_element_set_state()处理,如前面教程中所示。

case 's':
  if (g_ascii_isupper (str[0])) {
    data->rate *= 2.0;
  } else {
    data->rate /= 2.0;
  }
  send_seek_event (data);
  break;
case 'd':
  data->rate *= -1.0;
  send_seek_event (data);
  break;

使用’ S ‘和’ S ‘将当前播放速率加倍或减半,而’ d '将当前播放方向反转。在这两种情况下,都会更新rate变量并调用send_seek_event。让我们回顾一下这个函数。

/* Send seek event to change rate */
static void send_seek_event (CustomData *data) {
  gint64 position;
  GstEvent *seek_event;

  /* Obtain the current position, needed for the seek event */
  if (!gst_element_query_position (data->pipeline, GST_FORMAT_TIME, &position)) {
    g_printerr ("Unable to retrieve current position.\n");
    return;
  }

这个函数创建一个新的Seek事件,并将其发送到管道以更新速率。首先,使用gst_element_query_position()恢复当前位置。这是必需的,因为Seek Event跳转到流中的另一个位置,并且,由于我们实际上不想移动,我们跳转到当前位置。使用步骤事件会更简单,但该事件目前还没有完全功能,如简介中所解释的那样。

/* Create the seek event */
if (data->rate > 0) {
  seek_event = gst_event_new_seek (data->rate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
      GST_SEEK_TYPE_SET, position, GST_SEEK_TYPE_END, 0);
} else {
  seek_event = gst_event_new_seek (data->rate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
      GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, position);
}

查找事件是用gst_event_new_seek()创建的。它的参数基本上是新的速率,新的开始位置和新的停止位置。无论回放方向如何,开始位置必须小于停止位置,所以两个回放方向是不同的。

if (data->video_sink == NULL) {
  /* If we have not done so, obtain the sink through which we will send the seek events */
  g_object_get (data->pipeline, "video-sink", &data->video_sink, NULL);
}

如简介中所述,为了避免执行多个seek, Event只被发送到一个接收器,在本例中是视频接收器。它是从playbin获得的视频接收器属性。它在初始化的时候被读取,因为实际的接收器可能会根据媒体内容的变化而变化,这直到pipeline正在播放和一些媒体被读取时才会知道。

/* Send the event */
gst_element_send_event (data->video_sink, seek_event);

新的事件最终通过gst_element_send_event()发送到选定的接收器。
回到键盘处理程序,我们仍然忽略了帧步进代码,这非常简单:

case 'n':
  if (data->video_sink == NULL) {
    /* If we have not done so, obtain the sink through which we will send the step events */
    g_object_get (data->pipeline, "video-sink", &data->video_sink, NULL);
  }

  gst_element_send_event (data->video_sink,
      gst_event_new_step (GST_FORMAT_BUFFERS, 1, ABS (data->rate), TRUE, FALSE));
  g_print ("Stepping one frame\n");
  break;

使用gst_event_new_step()创建一个新的Step Event,它的参数基本上指定了跳过的数量(在示例中为1帧)和新的速率(我们没有更改)。
视频接收器是从playbin里拿出来的以防我们还没有,就像以前一样。
这样我们就完成了。在测试本教程时,请记住,在许多元素中反向播放并不是最佳的。
更改回放速率可能只对本地文件有效。如果您不能修改它,请尝试将第114行传递给playbin的URI更改为本地URI,以file:///开头。

结论

这个例程展示了:
-如何使用一个Seek事件来改变播放速率,该事件由gst_event_new_seek()创建,并由gst_element_send_event()提供给pipeline;

  • 如何使用使用gst_event_new_step()创建的Step Events逐帧推进视频。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值