目标
快进,倒放和慢放是trick模式的共同技巧,它们有一个共同点就是它们都修改了播放的速度。本教程会展示如何来获得这些效果和如何进行逐帧的跳跃。主要内容是:
如何来变换播放的速度,变快或者变慢,前进或者后退
如何一帧一帧的播放视频
介绍
快进是以超过正常速度播放媒体的一项技术,反之,慢放是以低于正常速度播放的技术。倒放和播放是一样的,只不过是从后面朝前面播放。
所有这些技术做的都是修改播放速度这件事,如果说正常播放速度是1.0的话,那么超过1.0这个数字就是快进,低于1.0这个数字就是慢放了,正数就是从前朝后放,负数就是从后超前放了。
GStreamer提供了两种来变换播放的速度:Step事件和Seek事件。Step事件可以在改变后面的播放速度的情况下跳过一个指定的间隔(只能向前播放)。Seek事件,就可以跳转到任意一个地方并且可以设置播放速度(正向反向都可以)。
在《GStreamer基础教程04——时间管理》里面已经演示过Seek事件了,使用了一个帮助函数来隐藏起复杂性。本教程会做更详细的解释。
Step事件因为需要的参数比较少,用来改变播放速度更加方便一点。但是,他们在GStreamer的实现还需要再做一点工作,所以这里用了Seek事件。
为了使用这些事件,需要先建立它们然后把它们传给pipeline,它们会向上游传播知道遇到能处理这些事件的element。如果一个事件传给了一个bin element(比如playbin2),它会简单地把事件给到它所有的sink,这可能会导致操作执行很多次。常见的做法是通过video-sink或者audio-sink属性找到一个playbin2的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 ("playbin2 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;
}
在主函数里面的初始化代码没有任何新的东西:初始化一个playbin2,跟踪按键,运行一个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了。通过playbin2的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帧)和速度(这里我们没改)。
获取一下playbin2的视频sink,就像前面提到过地那样。
大功告成!不过在测试本教程时,请记住回放是很多element不支持的。
(对于本地文件来说也可以修改播放速度,如果你要试验这一点,那么只要把传给playbin2的URL改成本地的URL即可。请注意,是用file:///来作为开头的)