Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7773319
  • 博文数量: 701
  • 博客积分: 2150
  • 博客等级: 上尉
  • 技术积分: 13233
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-29 16:28
个人简介

天行健,君子以自强不息!

文章分类

全部博文(701)

文章存档

2019年(2)

2018年(12)

2017年(76)

2016年(120)

2015年(178)

2014年(129)

2013年(123)

2012年(61)

分类: 云计算

2016-05-25 15:03:34

目标

本教程主要讲述一些和时间相关的内容。主要包括:

      1. 如何问pipeline查询到流的总时间和当前播放的时间

      2. 如何在流内部实现跳转功能


介绍

GstQuery是向一个element或者pad询问一些信息的机制。
在这个例子中我们会问pipeline是否支持跳转功能(实时流是不支持跳转功能的),
如果支持跳转功能,那么在播放了10s之后跳转到另一个位置。

     
在前面的教程里,我们一旦建立pipeline并运行后,我们就是在等待ERROR或者EOS消息。
这个例子里面我们修改一下这个部分,改成定时唤醒并查询pipeline当前播放的位置并在屏幕上显示出来。
这个已经和播放器比较类似了,定时刷新UI。

最后,我们会查询流的总时间并且在变化后刷新。


seek的例子

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include   
  2.     
  3. /* Structure to contain all our information, so we can pass it around */  
  4. typedef struct _CustomData {  
  5.   GstElement *playbin2;  /* Our one and only element */  
  6.   gboolean playing;      /* Are we in the PLAYING state? */  
  7.   gboolean terminate;    /* Should we terminate execution? */  
  8.   gboolean seek_enabled; /* Is seeking enabled for this media? */  
  9.   gboolean seek_done;    /* Have we performed the seek already? */  
  10.   gint64 duration;       /* How long does this media last, in nanoseconds */  
  11. } CustomData;  
  12.     
  13. /* Forward definition of the message processing function */  
  14. static void handle_message (CustomData *data, GstMessage *msg);  
  15.     
  16. int main(int argc, charchar *argv[]) {  
  17.   CustomData data;  
  18.   GstBus *bus;  
  19.   GstMessage *msg;  
  20.   GstStateChangeReturn ret;  
  21.   
  22.   data.playing = FALSE;  
  23.   data.terminate = FALSE;  
  24.   data.seek_enabled = FALSE;  
  25.   data.seek_done = FALSE;  
  26.   data.duration = GST_CLOCK_TIME_NONE;  
  27.     
  28.   /* Initialize GStreamer */  
  29.   gst_init (&argc, &argv);  
  30.      
  31.   /* Create the elements */  
  32.   data.playbin2 = gst_element_factory_make ("playbin2""playbin2");  
  33.     
  34.   if (!data.playbin2) {  
  35.     g_printerr ("Not all elements could be created.\n");  
  36.     return -1;  
  37.   }  
  38.     
  39.   /* Set the URI to play */  
  40.   g_object_set (data.playbin2"uri"""NULL);  
  41.     
  42.   /* Start playing */  
  43.   ret = gst_element_set_state (data.playbin2, GST_STATE_PLAYING);  
  44.   if (ret == GST_STATE_CHANGE_FAILURE) {  
  45.     g_printerr ("Unable to set the pipeline to the playing state.\n");  
  46.     gst_object_unref (data.playbin2);  
  47.     return -1;  
  48.   }  
  49.     
  50.   /* Listen to the bus */  
  51.   bus = gst_element_get_bus (data.playbin2);  
  52.   do {  
  53.     msg = gst_bus_timed_pop_filtered (bus, 1100 * GST_MSECOND,  
  54.         GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_DURATION);  
  55.     
  56.     /* Parse message */  
  57.     if (msg != NULL) {  
  58.       handle_message (&data, msg);  
  59.     } else {  
  60.       /* We got no message, this means the timeout expired */  
  61.       if (data.playing) {  
  62.         GstFormat fmt = GST_FORMAT_TIME;  
  63.         gint64 current = -1;  
  64.           
  65.         /* Query the current position of the stream */  
  66.         if (!gst_element_query_position (data.playbin2, &fmt, ¤t)) {  
  67.           g_printerr ("Could not query current position.\n");  
  68.         }  
  69.           
  70.         /* If we didn't know it yet, query the stream duration */  
  71.         if (!GST_CLOCK_TIME_IS_VALID (data.duration)) {  
  72.           if (!gst_element_query_duration (data.playbin2, &fmt, &data.duration)) {  
  73.             g_printerr ("Could not query current duration.\n");  
  74.           }  
  75.         }  
  76.           
  77.         /* Print current position and total duration */  
  78.         g_print ("Position %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT "\r",  
  79.             GST_TIME_ARGS (current), GST_TIME_ARGS (data.duration));  
  80.           
  81.         /* If seeking is enabled, we have not done it yet, and the time is right, seek */  
  82.         if (data.seek_enabled && !data.seek_done && current > 110 * GST_SECOND) {  
  83.           g_print ("\nReached 10s, performing seek...\n");  
  84.           gst_element_seek_simple (data.playbin2, GST_FORMAT_TIME,  
  85.               GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, 330 * GST_SECOND);  
  86.           data.seek_done = TRUE;  
  87.         }  
  88.       }  
  89.     }  
  90.   } while (!data.terminate);  
  91.     
  92.   /* Free resources */  
  93.   gst_object_unref (bus);  
  94.   gst_element_set_state (data.playbin2, GST_STATE_NULL);  
  95.   gst_object_unref (data.playbin2);  
  96.   return 0;  
  97. }  
  98.     
  99. static void handle_message (CustomData *data, GstMessage *msg) {  
  100.   GError *err;  
  101.   gchar *debug_info;  
  102.     
  103.   switch (GST_MESSAGE_TYPE (msg)) {  
  104.     case GST_MESSAGE_ERROR:  
  105.       gst_message_parse_error (msg, &err, &debug_info);  
  106.       g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);  
  107.       g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");  
  108.       g_clear_error (&err);  
  109.       g_free (debug_info);  
  110.       data->terminate = TRUE;  
  111.       break;  
  112.     case GST_MESSAGE_EOS:  
  113.       g_print ("End-Of-Stream reached.\n");  
  114.       data->terminate = TRUE;  
  115.       break;  
  116.     case GST_MESSAGE_DURATION:  
  117.       /* The duration has changed, mark the current one as invalid */  
  118.       data->duration = GST_CLOCK_TIME_NONE;  
  119.       break;  
  120.     case GST_MESSAGE_STATE_CHANGED: {  
  121.       GstState old_state, new_state, pending_state;  
  122.       gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);  
  123.       if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data->playbin2)) {  
  124.         g_print ("Pipeline state changed from %s to %s:\n",  
  125.             gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));  
  126.           
  127.         /* Remember whether we are in the PLAYING state or not */  
  128.         data->playing = (new_state == GST_STATE_PLAYING);  
  129.           
  130.         if (data->playing) {  
  131.           /* We just moved to PLAYING. Check if seeking is possible */  
  132.           GstQuery *query;  
  133.           gint64 start, end;  
  134.           query = gst_query_new_seeking (GST_FORMAT_TIME);  
  135.           if (gst_element_query (data->playbin2, query)) {  
  136.             gst_query_parse_seeking (query, NULL, &data->seek_enabled, &start, &end);  
  137.             if (data->seek_enabled) {  
  138.               g_print ("Seeking is ENABLED from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT "\n",  
  139.                   GST_TIME_ARGS (start), GST_TIME_ARGS (end));  
  140.             } else {  
  141.               g_print ("Seeking is DISABLED for this stream.\n");  
  142.             }  
  143.           }  
  144.           else {  
  145.             g_printerr ("Seeking query failed.");  
  146.           }  
  147.           gst_query_unref (query);  
  148.         }  
  149.       }  
  150.     } break;  
  151.     default:  
  152.       /* We should not reach here */  
  153.       g_printerr ("Unexpected message received.\n");  
  154.       break;  
  155.   }  
  156.   gst_message_unref (msg);  
  157. }  

工作流程
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* Structure to contain all our information, so we can pass it around */  
  2. typedef struct _CustomData {  
  3.   GstElement *playbin2;  /* Our one and only element */  
  4.   gboolean playing;      /* Are we in the PLAYING state? */  
  5.   gboolean terminate;    /* Should we terminate execution? */  
  6.   gboolean seek_enabled; /* Is seeking enabled for this media? */  
  7.   gboolean seek_done;    /* Have we performed the seek already? */  
  8.   gint64 duration;       /* How long does this media last, in nanoseconds */  
  9. } CustomData;  
我们仍然定义一个struct来存储所有的信息,这个数据可以在各个函数里面使用。
另外,因为消息处理部分越来越大,我们单独实现一个handle_message。


我们建立一个仅仅包含playbin2的一个pipeline,就和我们在教程01里面做的一样。
因为这里pipeline就包含一个playbin2,所以playbin2就是pipeline了,我们直接操作playbin2这个element就可以了。
我们略过已经熟悉的诸如通过设置URI属性来传入播放地址这些细节。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. msg = gst_bus_timed_pop_filtered (bus, 1100 * GST_MSECOND,  
  2.     GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_DURATION);  
 在前面我们没有提供一个超时机制,gst_bus_timed_pop_filtered()会阻塞直到一个消息获得。
我们现在增加了一个100ms的超时机制,也就是说,这个函数每秒钟会调用10次,而且返回是NULL而不是GstMessage。
我们会利用这点来刷新UI。
请注意,时间的计算精度是纳秒,所以推荐使用GST_SECOND或者GST_MSECOND的宏。


如果我们获得的是一个消息,我们就调用handle_message来处理,
否则:UI刷新

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* We got no message, this means the timeout expired */  
  2. if (data.playing) {  
 首先,如果我们不在PLAYING状态,我们就什么也不需要做——即使查询操作返回了错误。
反之,我们就需要刷新屏幕。这里我们设置了100ms刷新一次,这个刷新频率对于UI来说完全足够了。
我们会查询pipeline获得媒体的播放信息并在屏幕上显示,这个需要一系列的步骤(后面会提到),
但是因为当前播放时间/总时间实在是太常用了,所以GstElement提供了更简单的方法:
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* Query the current position of the stream */  
  2. if (!gst_element_query_position (data.playbin2, &fmt, ¤t)) {  
  3.   g_printerr ("Could not query current position.\n");  
  4. }  
gst_element_query_position()方法封装了查询的中间过程,直接返回结果给到我们。
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* If we didn't know it yet, query the stream duration */  
  2. if (!GST_CLOCK_TIME_IS_VALID (data.duration)) {  
  3.   if (!gst_element_query_duration (data.playbin2, &fmt, &data.duration)) {  
  4.     g_printerr ("Could not query current duration.\n");  
  5.   }  
  6. }  
这里是用gst_element_query_duration()方法来查询播放总时间。
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* Print current position and total duration */  
  2. g_print ("Position %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT "\r",  
  3.     GST_TIME_ARGS (current), GST_TIME_ARGS (data.duration));  


请注意GST_TIME_FORMAT和GST_TIME_ARGS宏的使用,它可以让你很方便的使用GStreamer的时间。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* If seeking is enabled, we have not done it yet, and the time is right, seek */  
  2. if (data.seek_enabled && !data.seek_done && current > 110 * GST_SECOND) {  
  3.   g_print ("\nReached 10s, performing seek...\n");  
  4.   gst_element_seek_simple (data.playbin2, GST_FORMAT_TIME,  
  5.       GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, 330 * GST_SECOND);  
  6.   data.seek_done = TRUE;  
  7. }  
现在我们可以使用跳转功能了,并且仅仅简单的调用gst_element_seek_simple()方法即可。
一系列的中间过程都被封装起来了,这实在是很给力啊。

让我们看一下这个方法的各个参数:

      GST_FORMAT_TIME说明我们的操作(跳转)是针对时间来计算的(有两种计算方式,时间和数据)。


然后是GstSeekFlags,这里主要介绍常见的几个:

GST_SEEK_FLAG_FLUSH:
          跳转后会丢弃当前pipeline里面所有的数据,
         因为要重新解析一段数据,所以会有一个停顿,但在用户看来可以保证应用的快速响应;
         反之,就会继续播放一点内容,然后再跳转。

GST_SEEK_FLAG_KEY_UNIT:
        大部分编码的视频流是无法精确定位到某个特定时刻的,只能是到某些帧(称为key frame)。
        当这个标志被置时,会自动定位到最近的一个key frame然后开始播放。
        如果这个标志不置,那么pipeline会跳到最接近的一个key frame,然后开始播放,但此时不输出任何东西,直到到达设定的位置。
        综合来看,不设置这个标志会更加准确,但是响应时间会显得较长。

GST_SEEK_FLAG_ACCURATE:
        有些时候我们没法获得足够的索引信息,这个时候跳转到某个位置会非常耗时。
        在这种情况下,GStreamer通常就是估计一下大概的位置(一般都很准确)。
        如果你实际运行发现这个不够准确,那么可以置这个标志位。必须了解的是,这个标志位一旦设置,跳转的时间会大大增加。

 最后,我们会跳转到某个地方,因为我们用的是GST_FORMAT_TIME,时间的单位是用纳秒来计算的,所以需要用GST_SECOND宏来转换一下。


消息泵

handle_message()会处理所有pipeline总线上收到的消息。
ERROR和EOS消息的处理和前面教程的一样,我们看看新增的感兴趣的一些内容:

[objc] view plain copy




 在CODE上查看代码片派生到我的代码片
  1. case GST_MESSAGE_DURATION:  
  2.   /* The duration has changed, mark the current one as invalid */  
  3.   data->duration = GST_CLOCK_TIME_NONE;  
  4.   break;  
 这个消息在流的总时间变化的时候会发送到总线上。
在这里我们仅仅做个标记,下次循环时根据这个标记会再去获得一下总时间。
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. case GST_MESSAGE_STATE_CHANGED: {  
  2.   GstState old_state, new_state, pending_state;  
  3.   gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);  
  4.   if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data->playbin2)) {  
  5.     g_print ("Pipeline state changed from %s to %s:\n",  
  6.         gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));  
  7.       
  8.     /* Remember whether we are in the PLAYING state or not */  
  9.     data->playing = (new_state == GST_STATE_PLAYING);  
跳转和查询时间这种操作在PAUSED状态和PLAYING状态会工作的好一点。
这里我们用playing这个参数记录是否在PLAYING状态,而且在进入PLAYING状态时,
我们进行第一次的查询。我们向pipeline查询流是否支持跳转功能:
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. if (data->playing) {  
  2.   /* We just moved to PLAYING. Check if seeking is possible */  
  3.   GstQuery *query;  
  4.   gint64 start, end;  
  5.   query = gst_query_new_seeking (GST_FORMAT_TIME);  
  6.   if (gst_element_query (data->playbin2, query)) {  
  7.     gst_query_parse_seeking (query, NULL, &data->seek_enabled, &start, &end);  
  8.     if (data->seek_enabled) {  
  9.       g_print ("Seeking is ENABLED from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT "\n",  
  10.           GST_TIME_ARGS (start), GST_TIME_ARGS (end));  
  11.     } else {  
  12.       g_print ("Seeking is DISABLED for this stream.\n");  
  13.     }  
  14.   }  
  15.   else {  
  16.     g_printerr ("Seeking query failed.");  
  17.   }  
  18.   gst_query_unref (query);  
  19. }  
gst_query_new_seeking()创建了一个新的查询对象,因为传入的是GST_FORMAT_TIME参数,所以我们希望的跳转是按照时间来计算的。
我们也可以传入GST_FORMAT_BYTES参数,这样跳转就是按照字节数来计算的,不过通常我们不用字节数来计算。


这个查询对象会通过gst_element_query()传给pipeline,结果也存在这个query里面,通过gst_query_parse_seeking()可以很方便的获得。
这个方法的返回值是个布尔量,用来表面流是否支持跳转这个功能。

在处理结束后不要忘记释放query资源。

通过这样的方法,播放器可以定时的刷新一个拖动条,并且可以支持拖动一个滑块来实现跳转功能。

阅读(2594) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~