• 【GStreamer开发】GStreamer基础教程13——播放速度


    目标

          快进,倒放和慢放是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事件给它,让它每次跳跃一帧。


    一个神奇模式的播放器

    [objc] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. <span style="font-size:14px;">#include <string.h>  
    2. #include <gst/gst.h>  
    3.     
    4. typedef struct _CustomData {  
    5.   GstElement *pipeline;  
    6.   GstElement *video_sink;  
    7.   GMainLoop *loop;  
    8.     
    9.   gboolean playing;  /* Playing or Paused */  
    10.   gdouble rate;      /* Current playback rate (can be negative) */  
    11. } CustomData;  
    12.     
    13. /* Send seek event to change rate */  
    14. static void send_seek_event (CustomData *data) {  
    15.   gint64 position;  
    16.   GstFormat format = GST_FORMAT_TIME;  
    17.   GstEvent *seek_event;  
    18.     
    19.   /* Obtain the current position, needed for the seek event */  
    20.   if (!gst_element_query_position (data->pipeline, &format, &position)) {  
    21.     g_printerr ("Unable to retrieve current position. ");  
    22.     return;  
    23.   }  
    24.     
    25.   /* Create the seek event */  
    26.   if (data->rate > 0) {  
    27.     seek_event = gst_event_new_seek (data->rate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,  
    28.         GST_SEEK_TYPE_SET, position, GST_SEEK_TYPE_SET, -1);  
    29.   } else {  
    30.     seek_event = gst_event_new_seek (data->rate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,  
    31.         GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, position);  
    32.   }  
    33.     
    34.   if (data->video_sink == NULL) {  
    35.     /* If we have not done so, obtain the sink through which we will send the seek events */  
    36.     g_object_get (data->pipeline, "video-sink", &data->video_sink, NULL);  
    37.   }  
    38.     
    39.   /* Send the event */  
    40.   gst_element_send_event (data->video_sink, seek_event);  
    41.     
    42.   g_print ("Current rate: %g ", data->rate);  
    43. }  
    44.     
    45. /* Process keyboard input */  
    46. static gboolean handle_keyboard (GIOChannel *source, GIOCondition cond, CustomData *data) {  
    47.   gchar *str = NULL;  
    48.     
    49.   if (g_io_channel_read_line (source, &str, NULLNULLNULL) != G_IO_STATUS_NORMAL) {  
    50.     return TRUE;  
    51.   }  
    52.     
    53.   switch (g_ascii_tolower (str[0])) {  
    54.   case 'p':  
    55.     data->playing = !data->playing;  
    56.     gst_element_set_state (data->pipeline, data->playing ? GST_STATE_PLAYING : GST_STATE_PAUSED);  
    57.     g_print ("Setting state to %s ", data->playing ? "PLAYING" : "PAUSE");  
    58.     break;  
    59.   case 's':  
    60.     if (g_ascii_isupper (str[0])) {  
    61.       data->rate *= 2.0;  
    62.     } else {  
    63.       data->rate /= 2.0;  
    64.     }  
    65.     send_seek_event (data);  
    66.     break;  
    67.   case 'd':  
    68.     data->rate *= -1.0;  
    69.     send_seek_event (data);  
    70.     break;  
    71.   case 'n':  
    72.     if (data->video_sink == NULL) {  
    73.       /* If we have not done so, obtain the sink through which we will send the step events */  
    74.       g_object_get (data->pipeline, "video-sink", &data->video_sink, NULL);  
    75.     }  
    76.       
    77.     gst_element_send_event (data->video_sink,  
    78.         gst_event_new_step (GST_FORMAT_BUFFERS, 1, data->rate, TRUE, FALSE));  
    79.     g_print ("Stepping one frame ");  
    80.     break;  
    81.   case 'q':  
    82.     g_main_loop_quit (data->loop);  
    83.     break;  
    84.   default:  
    85.     break;  
    86.   }  
    87.     
    88.   g_free (str);  
    89.     
    90.   return TRUE;  
    91. }  
    92.     
    93. int main(int argc, charchar *argv[]) {  
    94.   CustomData data;  
    95.   GstStateChangeReturn ret;  
    96.   GIOChannel *io_stdin;  
    97.     
    98.   /* Initialize GStreamer */  
    99.   gst_init (&argc, &argv);  
    100.     
    101.   /* Initialize our data structure */  
    102.   memset (&data, 0sizeof (data));  
    103.     
    104.   /* Print usage map */  
    105.   g_print (  
    106.     "USAGE: Choose one of the following options, then press enter: "  
    107.     " 'P' to toggle between PAUSE and PLAY "  
    108.     " 'S' to increase playback speed, 's' to decrease playback speed "  
    109.     " 'D' to toggle playback direction "  
    110.     " 'N' to move to next frame (in the current direction, better in PAUSE) "  
    111.     " 'Q' to quit ");  
    112.     
    113.   /* Build the pipeline */  
    114.   data.pipeline = gst_parse_launch ("playbin2 uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm"NULL);  
    115.     
    116.   /* Add a keyboard watch so we get notified of keystrokes */  
    117. #ifdef _WIN32  
    118.   io_stdin = g_io_channel_win32_new_fd (fileno (stdin));  
    119. #else  
    120.   io_stdin = g_io_channel_unix_new (fileno (stdin));  
    121. #endif  
    122.   g_io_add_watch (io_stdin, G_IO_IN, (GIOFunc)handle_keyboard, &data);  
    123.     
    124.   /* Start playing */  
    125.   ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);  
    126.   if (ret == GST_STATE_CHANGE_FAILURE) {  
    127.     g_printerr ("Unable to set the pipeline to the playing state. ");  
    128.     gst_object_unref (data.pipeline);  
    129.     return -1;  
    130.   }  
    131.   data.playing = TRUE;  
    132.   data.rate = 1.0;  
    133.     
    134.   /* Create a GLib Main Loop and set it to run */  
    135.   data.loop = g_main_loop_new (NULL, FALSE);  
    136.   g_main_loop_run (data.loop);  
    137.     
    138.   /* Free resources */  
    139.   g_main_loop_unref (data.loop);  
    140.   g_io_channel_unref (io_stdin);  
    141.   gst_element_set_state (data.pipeline, GST_STATE_NULL);  
    142.   if (data.video_sink != NULL)  
    143.     gst_object_unref (data.video_sink);  
    144.   gst_object_unref (data.pipeline);  
    145.   return 0;  
    146. }  
    147. </span>  

    工作流程

          在主函数里面的初始化代码没有任何新的东西:初始化一个playbin2,跟踪按键,运行一个GLib主循环。

          然后,在键盘处理函数中:

    [objc] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. <span style="font-size:14px;">/* Process keyboard input */  
    2. static gboolean handle_keyboard (GIOChannel *source, GIOCondition cond, CustomData *data) {  
    3.   gchar *str = NULL;  
    4.     
    5.   if (g_io_channel_read_line (source, &str, NULLNULLNULL) != G_IO_STATUS_NORMAL) {  
    6.     return TRUE;  
    7.   }  
    8.     
    9.   switch (g_ascii_tolower (str[0])) {  
    10.   case 'p':  
    11.     data->playing = !data->playing;  
    12.     gst_element_set_state (data->pipeline, data->playing ? GST_STATE_PLAYING : GST_STATE_PAUSED);  
    13.     g_print ("Setting state to %s ", data->playing ? "PLAYING" : "PAUSE");  
    14.     break;</span>  

          像前一讲一样用gst_element_set_state()来处理暂停/播放的交替。

    [objc] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. <span style="font-size:14px;">  case 's':  
    2.     if (g_ascii_isupper (str[0])) {  
    3.       data->rate *= 2.0;  
    4.     } else {  
    5.       data->rate /= 2.0;  
    6.     }  
    7.     send_seek_event (data);  
    8.     break;  
    9.   case 'd':  
    10.     data->rate *= -1.0;  
    11.     send_seek_event (data);  
    12.     break;</span>  

          用‘S’来加倍播放的速度,'s'来把播放速度降低一倍,用'd'来转换播放的方向。在这几种情况的任何一种里面,rate这个变量是需要更新的,然后调用send_seek_event()这个方法。让我们来看一下函数:

    [objc] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. <span style="font-size:14px;">/* Send seek event to change rate */  
    2. static void send_seek_event (CustomData *data) {  
    3.   gint64 position;  
    4.   GstFormat format = GST_FORMAT_TIME;  
    5.   GstEvent *seek_event;  
    6.     
    7.   /* Obtain the current position, needed for the seek event */  
    8.   if (!gst_element_query_position (data->pipeline, &format, &position)) {  
    9.     g_printerr ("Unable to retrieve current position. ");  
    10.     return;  
    11.   }</span>  

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

    [objc] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. <span style="font-size:14px;">  /* Create the seek event */  
    2.   if (data->rate > 0) {  
    3.     seek_event = gst_event_new_seek (data->rate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,  
    4.         GST_SEEK_TYPE_SET, position, GST_SEEK_TYPE_SET, -1);  
    5.   } else {  
    6.     seek_event = gst_event_new_seek (data->rate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,  
    7.         GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, position);  
    8.   }</span>  
          我们用gst_event_new_seek()来建立Seek事件。参数基本上就是新的播放速度,新的起始位置和新的结束位置。无论播放的方向,起始位置都要在结束位置之前。所以两个播放的方向分成了两段处理。

    [objc] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. <span style="font-size:14px;">  if (data->video_sink == NULL) {  
    2.     /* If we have not done so, obtain the sink through which we will send the seek events */  
    3.     g_object_get (data->pipeline, "video-sink", &data->video_sink, NULL);  
    4.   }</span>  

          正如前面解释的,为了避免对此进行Seek,事件只传给一个sink,在这里,就是视频的sink了。通过playbin2的video-sink属性来获得。这个动作在这里执行而不是在初始化时就执行是因为随着播放媒体的不同这个sink是不同的,所以在pipeline到PLAYING状态并已经读取了一些媒体数据前video-sink是不能确定的。

    [objc] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. <span style="font-size:14px;">  /* Send the event */  
    2.   gst_element_send_event (data->video_sink, seek_event);</span>  

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

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

    [objc] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. <span style="font-size:14px;">  case 'n':  
    2.     if (data->video_sink == NULL) {  
    3.       /* If we have not done so, obtain the sink through which we will send the step events */  
    4.       g_object_get (data->pipeline, "video-sink", &data->video_sink, NULL);  
    5.     }  
    6.       
    7.     gst_element_send_event (data->video_sink,  
    8.         gst_event_new_step (GST_FORMAT_BUFFERS, 1, data->rate, TRUE, FALSE));  
    9.     g_print ("Stepping one frame ");  
    10.     break;</span>  

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

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

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

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

  • 相关阅读:
    Linux初识(命令, 文件, 系统管理)
    【JavaScript学习整理】DOM对象(location history screen navigator)
    Charles进行HTTPS抓包(iOS为例)
    【JavaScript学习整理】js基础
    【css学习整理】浮动,清除
    【css学习整理】css基础(样式,语法,选择器)
    【html学习整理】meta,img,表格,表单
    【LeetCode】无重复字符的最长子串
    【html学习整理】常用标签
    【LeetCode】种花问题
  • 原文地址:https://www.cnblogs.com/huty/p/8517306.html
Copyright © 2020-2023  润新知