GStreamer基础教程13——播放速度

原文:https://gstreamer.freedesktop.org/documentation/tutorials/basic/index.html

译文原文:http://blog.csdn.net/sakulafly/article/details/21648155

原码:Git clone git://anongit.freedesktop.org/gstreamer/gst-docs

编译方式:gcc basic-tutorial-13.c -o basic-tutorial-13 `pkg-config --cflags --libs gstreamer-1.0`


Need help?

If you need help to compile this code, refer to the Building the tutorials section for your platform: LinuxMac OS X or Windows, or use this specific command on Linux:

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

If you need help to run this code, refer to the Running the tutorials section for your platform: LinuxMac OS X or Windows.

This tutorial opens a window and displays a movie, with accompanying audio. The media is fetched from the Internet, so the window might take a few seconds to appear, depending on your connection speed. The console shows the available commands, composed of a single upper-case or lower-case letter, which you should input followed by the Enter key.

Required libraries: gstreamer-1.0


目标

      快进,倒放和慢放是trick模式的共同技巧,它们有一个共同点就是它们都修改了播放的速度。本教程会展示如何来获得这些效果和如何进行逐帧的跳跃。主要内容是:

      如何来变换播放的速度,变快或者变慢,前进或者后退

      如何一帧一帧的播放视频


介绍

      快进是以超过正常速度播放媒体的一项技术,反之,慢放是以低于正常速度播放的技术。倒放和播放是一样的,只不过是从后面朝前面播放。

      所有这些技术做的都是修改播放速度这件事,如果说正常播放速度是1.0的话,那么超过1.0这个数字就是快进,低于1.0这个数字就是慢放了,正数就是从前朝后放,负数就是从后朝前放了。

      GStreamer提供了两种来变换播放的速度:Step事件和Seek事件。Step事件可以在改变后面的播放速度的情况下跳过一个指定的间隔(只能向前播放)。Seek事件,就可以跳转到任意一个地方并且可以设置播放速度(正向反向都可以)。

      在《GStreamer基础教程04——时间管理》里面已经演示过Seek事件了,使用了一个帮助函数来隐藏起复杂性。本教程会做更详细的解释。

      Step事件因为需要的参数比较少,用来改变播放速度更加方便一点。但是,他们在GStreamer的实现还需要再做一点工作,所以这里用了Seek事件。

      为了使用这些事件,需要先建立它们然后把它们传给pipeline,它们会向上游传播直到遇到能处理这些事件的element。如果一个事件传给了一个bin element(比如playbin),它会简单地把事件给到它所有的sink,这可能会导致操作执行很多次。常见的做法是通过video-sink或者audio-sink属性找到一个playbin的sink,然后直接把事件传给这个sink。

      逐帧步进就是一帧一帧的播放视频,它是让pipeline暂停,然后发送Step事件给它,让它每次跳跃一帧。


一个神奇模式的播放器

#include <string.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;
  GstFormat format = GST_FORMAT_TIME;
  GstEvent *seek_event;
  
  /* Obtain the current position, needed for the seek event */
  if (!gst_element_query_position (data->pipeline, &format, &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_SET, -1);
  } 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, 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=http://docs.gstreamer.com/media/sintel_trailer-480p.webm", NULL);
  
  /* Add a keyboard watch so we get notified of keystrokes */
#ifdef _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;
}

工作流程

      在主函数里面的初始化代码没有任何新的东西:初始化一个playbin,跟踪按键,运行一个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;
  GstFormat format = GST_FORMAT_TIME;
  GstEvent *seek_event;
  
  /* Obtain the current position, needed for the seek event */
  if (!gst_element_query_position (data->pipeline, &format, &position)) {
    g_printerr ("Unable to retrieve current position.\n");
    return;
  }

      这个函数新创建了一个Seek时间,并发送给pipeline来更新播放速度。首先,用gst_element_query_position()方法来记录当前位置。这样做是因为Seek事件会跳转其他位置,如果我们后面不希望进行移动,那么就需要返回到原来的位置。用Step事件会简单一点,但Step事件现在还没完全完成,这个在前面已经介绍过了。

  /* 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_SET, -1);
  } 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()来建立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,事件只传给一个sink,在这里,就是视频的sink了。通过playbin的video-sink属性来获得。这个动作在这里执行而不是在初始化时就执行是因为随着播放媒体的不同这个sink是不同的,所以在pipeline到PLAYING状态并已经读取了一些媒体数据前video-sink是不能确定的。

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

      最后,我们用gst_element_send_event()把新建的这个事件传给选中的sink。

      回到键盘处理函数来,我们还没有提到帧步进的代码的是在这里实现的:

  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, data->rate, TRUE, FALSE));
    g_print ("Stepping one frame\n");
    break;

      用gst_event_new_step()来创建一个新的Step事件,参数主要是指定的步长(这里是1帧)和速度(这里我们没改)。

      获取一下playbin的视频sink,就像前面提到过地那样。

      大功告成!不过在测试本教程时,请记住回放是很多element不支持的。

      (对于本地文件来说也可以修改播放速度,如果你要试验这一点,那么只要把传给playbin的URL改成本地的URL即可。请注意,是用file:///来作为开头的)


Conclusion

This tutorial has shown:

  • How to change the playback rate using a Seek Event, created with gst_event_new_seek() and fed to the pipeline with gst_element_send_event().
  • How to advance a video frame-by-frame by using Step Events, created with gst_event_new_step().
It has been a pleasure having you here, and see you soon!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值