【GStreamer开发】GStreamer播放教程04——既看式流
目的
在《GStreamer基础教程——流》里面我们展示了如何在较差的网络条件下使用缓冲这个机制来提升用户体验。本教程在《GStreamer基础教程——流》的基础上在扩展了一下,增加了把流的内容在本地存储。并且展示了:
如何开启既看式下载
如何知道下载的是什么
如何知道在哪里下载
如何限制下载数据的总量
介绍
当播放流的时候,从网络上获得的数据被锁住之后,会创建称为future-data的一个小的缓冲区。然而,在数据播放渲染之后就会被丢弃。这就意味着,如果用户想要倒回前面去看,相应地数据仍然需要再次下载。
像YouTube一样,播放流时播放器往往会裁剪,通常会把所有下载的数据都在本地保存,还会提供一个图形化的界面来显示已经下载了多少内容。
playbin2通过DOWNLOAD标志提供了一个比较类似的功能,它会把数据在本次临时保存起来用于在播放已经下载的部分时可以保持顺畅。
代码里面同时展示了如何使用缓冲查询,它可以让你知道哪部分的文件已经可用了。
一个适应网络并在本地存储数据的例子
- <span style="font-size:14px;">#include <gst/gst.h>
- #include <string.h>
- #define GRAPH_LENGTH 80
- /* playbin2 flags */
- typedef enum {
- GST_PLAY_FLAG_DOWNLOAD = (1 << 7) /* Enable progressive download (on selected formats) */
- } GstPlayFlags;
- typedef struct _CustomData {
- gboolean is_live;
- GstElement *pipeline;
- GMainLoop *loop;
- gint buffering_level;
- } CustomData;
- static void got_location (GstObject *gstobject, GstObject *prop_object, GParamSpec *prop, gpointer data) {
- gchar *location;
- g_object_get (G_OBJECT (prop_object), "temp-location", &location, NULL);
- g_print ("Temporary file: %s\n", location);
- /* Uncomment this line to keep the temporary file after the program exits */
- /* g_object_set (G_OBJECT (prop_object), "temp-remove", FALSE, NULL); */
- }
- static void cb_message (GstBus *bus, GstMessage *msg, CustomData *data) {
- switch (GST_MESSAGE_TYPE (msg)) {
- case GST_MESSAGE_ERROR: {
- GError *err;
- gchar *debug;
- gst_message_parse_error (msg, &err, &debug);
- g_print ("Error: %s\n", err->message);
- g_error_free (err);
- g_free (debug);
- gst_element_set_state (data->pipeline, GST_STATE_READY);
- g_main_loop_quit (data->loop);
- break;
- }
- case GST_MESSAGE_EOS:
- /* end-of-stream */
- gst_element_set_state (data->pipeline, GST_STATE_READY);
- g_main_loop_quit (data->loop);
- break;
- case GST_MESSAGE_BUFFERING:
- /* If the stream is live, we do not care about buffering. */
- if (data->is_live) break;
- gst_message_parse_buffering (msg, &data->buffering_level);
- /* Wait until buffering is complete before start/resume playing */
- if (data->buffering_level < 100)
- gst_element_set_state (data->pipeline, GST_STATE_PAUSED);
- else
- gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
- break;
- case GST_MESSAGE_CLOCK_LOST:
- /* Get a new clock */
- gst_element_set_state (data->pipeline, GST_STATE_PAUSED);
- gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
- break;
- default:
- /* Unhandled message */
- break;
- }
- }
- static gboolean refresh_ui (CustomData *data) {
- GstQuery *query;
- gboolean result;
- query = gst_query_new_buffering (GST_FORMAT_PERCENT);
- result = gst_element_query (data->pipeline, query);
- if (result) {
- gint n_ranges, range, i;
- gchar graph[GRAPH_LENGTH + 1];
- GstFormat format = GST_FORMAT_TIME;
- gint64 position = 0, duration = 0;
- memset (graph, ' ', GRAPH_LENGTH);
- graph[GRAPH_LENGTH] = '\0';
- n_ranges = gst_query_get_n_buffering_ranges (query);
- for (range = 0; range < n_ranges; range++) {
- gint64 start, stop;
- gst_query_parse_nth_buffering_range (query, range, &start, &stop);
- start = start * GRAPH_LENGTH / 100;
- stop = stop * GRAPH_LENGTH / 100;
- for (i = (gint)start; i < stop; i++)
- graph [i] = '-';
- }
- if (gst_element_query_position (data->pipeline, &format, &position) &&
- GST_CLOCK_TIME_IS_VALID (position) &&
- gst_element_query_duration (data->pipeline, &format, &duration) &&
- GST_CLOCK_TIME_IS_VALID (duration)) {
- i = (gint)(GRAPH_LENGTH * (double)position / (double)(duration + 1));
- graph [i] = data->buffering_level < 100 ? 'X' : '>';
- }
- g_print ("[%s]", graph);
- if (data->buffering_level < 100) {
- g_print (" Buffering: %3d%%", data->buffering_level);
- } else {
- g_print (" ");
- }
- g_print ("\r");
- }
- return TRUE;
- }
- int main(int argc, charchar *argv[]) {
- GstElement *pipeline;
- GstBus *bus;
- GstStateChangeReturn ret;
- GMainLoop *main_loop;
- CustomData data;
- guint flags;
- /* Initialize GStreamer */
- gst_init (&argc, &argv);
- /* Initialize our data structure */
- memset (&data, 0, sizeof (data));
- data.buffering_level = 100;
- /* Build the pipeline */
- pipeline = gst_parse_launch ("playbin2 uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm", NULL);
- bus = gst_element_get_bus (pipeline);
- /* Set the download flag */
- g_object_get (pipeline, "flags", &flags, NULL);
- flags |= GST_PLAY_FLAG_DOWNLOAD;
- g_object_set (pipeline, "flags", flags, NULL);
- /* Uncomment this line to limit the amount of downloaded data */
- /* g_object_set (pipeline, "ring-buffer-max-size", (guint64)4000000, NULL); */
- /* Start playing */
- ret = gst_element_set_state (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 (pipeline);
- return -1;
- } else if (ret == GST_STATE_CHANGE_NO_PREROLL) {
- data.is_live = TRUE;
- }
- main_loop = g_main_loop_new (NULL, FALSE);
- data.loop = main_loop;
- data.pipeline = pipeline;
- gst_bus_add_signal_watch (bus);
- g_signal_connect (bus, "message", G_CALLBACK (cb_message), &data);
- g_signal_connect (pipeline, "deep-notify::temp-location", G_CALLBACK (got_location), NULL);
- /* Register a function that GLib will call every second */
- g_timeout_add_seconds (1, (GSourceFunc)refresh_ui, &data);
- g_main_loop_run (main_loop);
- /* Free resources */
- g_main_loop_unref (main_loop);
- gst_object_unref (bus);
- gst_element_set_state (pipeline, GST_STATE_NULL);
- gst_object_unref (pipeline);
- g_print ("\n");
- return 0;
- }</span>
工作流程
这份代码是基于《GStreamer基础教程——流》里面例子的,我们仅仅看一下不同的地方即可。
创建
- <span style="font-size:14px;"> /* Set the download flag */
- g_object_get (pipeline, "flags", &flags, NULL);
- flags |= GST_PLAY_FLAG_DOWNLOAD;
- g_object_set (pipeline, "flags", flags, NULL);</span>
通过设置这个标志,playbin2通知它内部的queue存储所有下载的数据。
- <span style="font-size:14px;">g_signal_connect (pipeline, "deep-notify::temp-location", G_CALLBACK (got_location), NULL);</span>
- <span style="font-size:14px;">static void got_location (GstObject *gstobject, GstObject *prop_object, GParamSpec *prop, gpointer data) {
- gchar *location;
- g_object_get (G_OBJECT (prop_object), "temp-location", &location, NULL);
- g_print ("Temporary file: %s\n", location);
- /* Uncomment this line to keep the temporary file after the program exits */
- /* g_object_set (G_OBJECT (prop_object), "temp-remove", FALSE, NULL); */
- }</span>
这个temp-location属性是从发出信号的element那里获得的并打印出来。
当pipeline状态从PAUSED切换到READY时,这个文件会被删除。正如注释里面写的那样,你可以通过设置queue2的temp-remove属性位FALSE来保留下载的数据。
UI
在main函数里我们启动了一个1s的定时器,这样可以每秒刷新一下UI界面。
- /* Register a function that GLib will call every second */
- g_timeout_add_seconds (1, (GSourceFunc)refresh_ui, &data);
- [---->------- ]
- static gboolean refresh_ui (CustomData *data) {
- GstQuery *query;
- gboolean result;
- query = gst_query_new_buffering (GST_FORMAT_PERCENT);
- 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封装起来,可以用下面的方法来解析:
- n_ranges = gst_query_get_n_buffering_ranges (query);
- for (range = 0; range < n_ranges; range++) {
- gint64 start, stop;
- gst_query_parse_nth_buffering_range (query, range, &start, &stop);
- start = start * GRAPH_LENGTH / 100;
- stop = stop * GRAPH_LENGTH / 100;
- for (i = (gint)start; i < stop; i++)
- graph [i] = '-';
- }
数据并不需要保证被按照顺序从头开始下载,因为跳跃播放时会让下载从一个新的地方开始。因此,gst_query_get_n_buffering_ranges()返回下载块的数目或者范围,然后我们用gst_query_parse_nth_buffering_rang()方法来解析下载块的位置和大小。
我们在调用gst_query_new_buffering()的请求会决定返回数据的格式,在这个例子里面,返回值是比例。这些查询到得数据用来绘制UI的下载动画。
- if (gst_element_query_position (data->pipeline, &format, &position) &&
- GST_CLOCK_TIME_IS_VALID (position) &&
- gst_element_query_duration (data->pipeline, &format, &duration) &&
- GST_CLOCK_TIME_IS_VALID (duration)) {
- i = (gint)(GRAPH_LENGTH * (double)position / (double)(duration + 1));
- graph [i] = data->buffering_level < 100 ? 'X' : '>';
- }
下一步就是当前位置的查询。它也支持比例的格式,所以代码和前面应该比较类似。不过这部分目前支持不是很好,所以我们使用了时间这个格式。
当前位置使用'>'或者'X'来表示,如果缓冲不到100%,cb_message会让pipeline处于PAUSE状态,那样我们就显示'X',如果已经满了100%,那么pipeline就在PLAYING状态,我们就显示'>'。
- if (data->buffering_level < 100) {
- g_print (" Buffering: %3d%%", data->buffering_level);
- } else {
- g_print (" ");
- }
最后,如果缓冲时钟小于100%,我们就把这个数据显示出来。
限制下载文件的大小
- /* Uncomment this line to limit the amount of downloaded data */
- /* g_object_set (pipeline, "ring-buffer-max-size", (guint64)4000000, NULL); */
打开139行的注释,让我们看看这个是如何做到的。缩小临时文件的大小,这样播放过的区域就会被覆盖。