【GStreamer开发】GStreamer播放教程04——既看式流

目的

      在《GStreamer基础教程——流》里面我们展示了如何在较差的网络条件下使用缓冲这个机制来提升用户体验。本教程在《GStreamer基础教程——流》的基础上在扩展了一下,增加了把流的内容在本地存储。并且展示了:

      如何开启既看式下载

      如何知道下载的是什么

      如何知道在哪里下载

      如何限制下载数据的总量


介绍

      当播放流的时候,从网络上获得的数据被锁住之后,会创建称为future-data的一个小的缓冲区。然而,在数据播放渲染之后就会被丢弃。这就意味着,如果用户想要倒回前面去看,相应地数据仍然需要再次下载。

      像YouTube一样,播放流时播放器往往会裁剪,通常会把所有下载的数据都在本地保存,还会提供一个图形化的界面来显示已经下载了多少内容。

      playbin2通过DOWNLOAD标志提供了一个比较类似的功能,它会把数据在本次临时保存起来用于在播放已经下载的部分时可以保持顺畅。

      代码里面同时展示了如何使用缓冲查询,它可以让你知道哪部分的文件已经可用了。


一个适应网络并在本地存储数据的例子

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">#include <gst/gst.h>  
  2. #include <string.h>  
  3.     
  4. #define GRAPH_LENGTH 80  
  5.     
  6. /* playbin2 flags */  
  7. typedef enum {  
  8.   GST_PLAY_FLAG_DOWNLOAD      = (1 << 7/* Enable progressive download (on selected formats) */  
  9. } GstPlayFlags;  
  10.     
  11. typedef struct _CustomData {  
  12.   gboolean is_live;  
  13.   GstElement *pipeline;  
  14.   GMainLoop *loop;  
  15.   gint buffering_level;  
  16. } CustomData;  
  17.     
  18. static void got_location (GstObject *gstobject, GstObject *prop_object, GParamSpec *prop, gpointer data) {  
  19.   gchar *location;  
  20.   g_object_get (G_OBJECT (prop_object), "temp-location", &location, NULL);  
  21.   g_print ("Temporary file: %s ", location);  
  22.   /* Uncomment this line to keep the temporary file after the program exits */  
  23.   /* g_object_set (G_OBJECT (prop_object), "temp-remove", FALSE, NULL); */  
  24. }  
  25.     
  26. static void cb_message (GstBus *bus, GstMessage *msg, CustomData *data) {  
  27.     
  28.   switch (GST_MESSAGE_TYPE (msg)) {  
  29.     case GST_MESSAGE_ERROR: {  
  30.       GError *err;  
  31.       gchar *debug;  
  32.         
  33.       gst_message_parse_error (msg, &err, &debug);  
  34.       g_print ("Error: %s ", err->message);  
  35.       g_error_free (err);  
  36.       g_free (debug);  
  37.         
  38.       gst_element_set_state (data->pipeline, GST_STATE_READY);  
  39.       g_main_loop_quit (data->loop);  
  40.       break;  
  41.     }  
  42.     case GST_MESSAGE_EOS:  
  43.       /* end-of-stream */  
  44.       gst_element_set_state (data->pipeline, GST_STATE_READY);  
  45.       g_main_loop_quit (data->loop);  
  46.       break;  
  47.     case GST_MESSAGE_BUFFERING:  
  48.       /* If the stream is live, we do not care about buffering. */  
  49.       if (data->is_live) break;  
  50.         
  51.       gst_message_parse_buffering (msg, &data->buffering_level);  
  52.         
  53.       /* Wait until buffering is complete before start/resume playing */  
  54.       if (data->buffering_level < 100)  
  55.         gst_element_set_state (data->pipeline, GST_STATE_PAUSED);  
  56.       else  
  57.         gst_element_set_state (data->pipeline, GST_STATE_PLAYING);  
  58.       break;  
  59.     case GST_MESSAGE_CLOCK_LOST:  
  60.       /* Get a new clock */  
  61.       gst_element_set_state (data->pipeline, GST_STATE_PAUSED);  
  62.       gst_element_set_state (data->pipeline, GST_STATE_PLAYING);  
  63.       break;  
  64.     default:  
  65.       /* Unhandled message */  
  66.       break;  
  67.     }  
  68. }  
  69.     
  70. static gboolean refresh_ui (CustomData *data) {  
  71.   GstQuery *query;  
  72.   gboolean result;  
  73.     
  74.   query = gst_query_new_buffering (GST_FORMAT_PERCENT);  
  75.   result = gst_element_query (data->pipeline, query);  
  76.   if (result) {  
  77.     gint n_ranges, range, i;  
  78.     gchar graph[GRAPH_LENGTH + 1];  
  79.     GstFormat format = GST_FORMAT_TIME;  
  80.     gint64 position = 0, duration = 0;  
  81.       
  82.     memset (graph, ' ', GRAPH_LENGTH);  
  83.     graph[GRAPH_LENGTH] = '';  
  84.       
  85.     n_ranges = gst_query_get_n_buffering_ranges (query);  
  86.     for (range = 0; range < n_ranges; range++) {  
  87.       gint64 start, stop;  
  88.       gst_query_parse_nth_buffering_range (query, range, &start, &stop);  
  89.       start = start * GRAPH_LENGTH / 100;  
  90.       stop = stop * GRAPH_LENGTH / 100;  
  91.       for (i = (gint)start; i < stop; i++)  
  92.         graph [i] = '-';  
  93.     }  
  94.     if (gst_element_query_position (data->pipeline, &format, &position) &&  
  95.         GST_CLOCK_TIME_IS_VALID (position) &&  
  96.         gst_element_query_duration (data->pipeline, &format, &duration) &&  
  97.         GST_CLOCK_TIME_IS_VALID (duration)) {  
  98.       i = (gint)(GRAPH_LENGTH * (double)position / (double)(duration + 1));  
  99.       graph [i] = data->buffering_level < 100 ? 'X' : '>';  
  100.     }  
  101.     g_print ("[%s]", graph);  
  102.     if (data->buffering_level < 100) {  
  103.       g_print (" Buffering: %3d%%", data->buffering_level);  
  104.     } else {  
  105.       g_print ("                ");  
  106.     }  
  107.     g_print (" ");  
  108.   }  
  109.     
  110.   return TRUE;  
  111.     
  112. }  
  113.     
  114. int main(int argc, charchar *argv[]) {  
  115.   GstElement *pipeline;  
  116.   GstBus *bus;  
  117.   GstStateChangeReturn ret;  
  118.   GMainLoop *main_loop;  
  119.   CustomData data;  
  120.   guint flags;  
  121.     
  122.   /* Initialize GStreamer */  
  123.   gst_init (&argc, &argv);  
  124.     
  125.   /* Initialize our data structure */  
  126.   memset (&data, 0sizeof (data));  
  127.   data.buffering_level = 100;  
  128.     
  129.   /* Build the pipeline */  
  130.   pipeline = gst_parse_launch ("playbin2 uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm"NULL);  
  131.   bus = gst_element_get_bus (pipeline);  
  132.     
  133.   /* Set the download flag */  
  134.   g_object_get (pipeline, "flags", &flags, NULL);  
  135.   flags |= GST_PLAY_FLAG_DOWNLOAD;  
  136.   g_object_set (pipeline, "flags", flags, NULL);  
  137.     
  138.   /* Uncomment this line to limit the amount of downloaded data */  
  139.   /* g_object_set (pipeline, "ring-buffer-max-size", (guint64)4000000, NULL); */  
  140.     
  141.   /* Start playing */  
  142.   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);  
  143.   if (ret == GST_STATE_CHANGE_FAILURE) {  
  144.     g_printerr ("Unable to set the pipeline to the playing state. ");  
  145.     gst_object_unref (pipeline);  
  146.     return -1;  
  147.   } else if (ret == GST_STATE_CHANGE_NO_PREROLL) {  
  148.     data.is_live = TRUE;  
  149.   }  
  150.     
  151.   main_loop = g_main_loop_new (NULL, FALSE);  
  152.   data.loop = main_loop;  
  153.   data.pipeline = pipeline;  
  154.     
  155.   gst_bus_add_signal_watch (bus);  
  156.   g_signal_connect (bus, "message", G_CALLBACK (cb_message), &data);  
  157.   g_signal_connect (pipeline, "deep-notify::temp-location", G_CALLBACK (got_location), NULL);  
  158.     
  159.   /* Register a function that GLib will call every second */  
  160.   g_timeout_add_seconds (1, (GSourceFunc)refresh_ui, &data);  
  161.     
  162.   g_main_loop_run (main_loop);  
  163.     
  164.   /* Free resources */  
  165.   g_main_loop_unref (main_loop);  
  166.   gst_object_unref (bus);  
  167.   gst_element_set_state (pipeline, GST_STATE_NULL);  
  168.   gst_object_unref (pipeline);  
  169.   
  170.   g_print (" ");  
  171.   return 0;  
  172. }</span>  

工作流程

      这份代码是基于《GStreamer基础教程——流》里面例子的,我们仅仅看一下不同的地方即可。

创建

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">  /* Set the download flag */  
  2.   g_object_get (pipeline, "flags", &flags, NULL);  
  3.   flags |= GST_PLAY_FLAG_DOWNLOAD;  
  4.   g_object_set (pipeline, "flags", flags, NULL);</span>  

      通过设置这个标志,playbin2通知它内部的queue存储所有下载的数据。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">g_signal_connect (pipeline, "deep-notify::temp-location", G_CALLBACK (got_location), NULL);</span>  
      当它们的子element属性发生变化时,playbin2就会发出deep-notify信号。在这里我们希望知道temp-location属性是什么时候变化的,了解queue2会把下载的数据存在哪里。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">static void got_location (GstObject *gstobject, GstObject *prop_object, GParamSpec *prop, gpointer data) {  
  2.   gchar *location;  
  3.   g_object_get (G_OBJECT (prop_object), "temp-location", &location, NULL);  
  4.   g_print ("Temporary file: %s ", location);  
  5.   /* Uncomment this line to keep the temporary file after the program exits */  
  6.   /* g_object_set (G_OBJECT (prop_object), "temp-remove", FALSE, NULL); */  
  7. }</span>  

      这个temp-location属性是从发出信号的element那里获得的并打印出来。

      当pipeline状态从PAUSED切换到READY时,这个文件会被删除。正如注释里面写的那样,你可以通过设置queue2的temp-remove属性位FALSE来保留下载的数据。

UI

      在main函数里我们启动了一个1s的定时器,这样可以每秒刷新一下UI界面。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* Register a function that GLib will call every second */  
  2. g_timeout_add_seconds (1, (GSourceFunc)refresh_ui, &data);  
      refresh_ui方法会查询pipeline来了解当前下载的数据在文件的位置以及当前播放的位置。并且用一种动画的方式在屏幕上显示出来。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [---->-------                ]  
      这里的'-'代表的是下载的部分,'>'代表的是当前播放的位置(当暂停的时候变成'X'位置)。当你的网络速度足够快得时候你可能会看不到下载的动画,在开始的时候就下载结束了。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static gboolean refresh_ui (CustomData *data) {  
  2.   GstQuery *query;  
  3.   gboolean result;  
  4.     
  5.   query = gst_query_new_buffering (GST_FORMAT_PERCENT);  
  6.   result = gst_element_query (data->pipeline, query);  

      我们在refresh_ui里面做的第一件事是就是用gst_query_new_buffering()创建一个GstQuery对象,并用gst_element_query()传给playbin2。在《GStreamer基础教程04——时间管理》里面我们展示了如何用明确的方法来查询位置/播放总时间等,如果要查询更复杂一些的内容(比如缓冲),那么我们会用更通用的gst_element_query()方法。

      缓冲的查询可以基于不同的GstFormat,并非所有的element都可以响应所有格式的查询,所以需要检查在pipeline里支持哪些格式。如果gst_element_query()返回TRUE,那么查询是成功的。查询的结果用GstQuery封装起来,可以用下面的方法来解析:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. n_ranges = gst_query_get_n_buffering_ranges (query);  
  2. for (range = 0; range < n_ranges; range++) {  
  3.   gint64 start, stop;  
  4.   gst_query_parse_nth_buffering_range (query, range, &start, &stop);  
  5.   start = start * GRAPH_LENGTH / 100;  
  6.   stop = stop * GRAPH_LENGTH / 100;  
  7.   for (i = (gint)start; i < stop; i++)  
  8.     graph [i] = '-';  
  9. }  

      数据并不需要保证被按照顺序从头开始下载,因为跳跃播放时会让下载从一个新的地方开始。因此,gst_query_get_n_buffering_ranges()返回下载块的数目或者范围,然后我们用gst_query_parse_nth_buffering_rang()方法来解析下载块的位置和大小。

      我们在调用gst_query_new_buffering()的请求会决定返回数据的格式,在这个例子里面,返回值是比例。这些查询到得数据用来绘制UI的下载动画。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. if (gst_element_query_position (data->pipeline, &format, &position) &&  
  2.     GST_CLOCK_TIME_IS_VALID (position) &&  
  3.     gst_element_query_duration (data->pipeline, &format, &duration) &&  
  4.     GST_CLOCK_TIME_IS_VALID (duration)) {  
  5.   i = (gint)(GRAPH_LENGTH * (double)position / (double)(duration + 1));  
  6.   graph [i] = data->buffering_level < 100 ? 'X' : '>';  
  7. }  

      下一步就是当前位置的查询。它也支持比例的格式,所以代码和前面应该比较类似。不过这部分目前支持不是很好,所以我们使用了时间这个格式。

      当前位置使用'>'或者'X'来表示,如果缓冲不到100%,cb_message会让pipeline处于PAUSE状态,那样我们就显示'X',如果已经满了100%,那么pipeline就在PLAYING状态,我们就显示'>'。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. if (data->buffering_level < 100) {  
  2.   g_print (" Buffering: %3d%%", data->buffering_level);  
  3. else {  
  4.   g_print ("                ");  
  5. }  

      最后,如果缓冲时钟小于100%,我们就把这个数据显示出来。

限制下载文件的大小

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* Uncomment this line to limit the amount of downloaded data */  
  2. /* g_object_set (pipeline, "ring-buffer-max-size", (guint64)4000000, NULL); */  

      打开139行的注释,让我们看看这个是如何做到的。缩小临时文件的大小,这样播放过的区域就会被覆盖。

原文地址:https://www.cnblogs.com/huty/p/8517343.html