目标

GStreamer有一系列把音频转换成视频的element。他们可以用于科学的目的或者增加音乐播放器的趣味性。本教程展示:

如何允许音频的可视化

如何选择可视化element

介绍

在playbin2里面运行音频可视化是非常容易的。当遇到一个只有音频的流时,只需要正确地设置playbin2的一些标志就行了。它会自己创建必要的element然后显示的。

如果你想要增加实际的element的趣味性,你要自己实例化它然后通过vis-plug属性来告诉playbin2。

本教程GStreamer注册的所有关于可视化的element,选择了goom并传给了playbin2

一个有趣的音乐播放器

[objc] view
plain
 copy

  1. <span style="font-size:14px;">#include <gst/gst.h>
  2. /* playbin2 flags */
  3. typedef enum {
  4. << 3) /* Enable rendering of visualizations when there is no video stream. */
  5. } GstPlayFlags;
  6. /* Return TRUE if this is a Visualization element */
  7. static gboolean filter_vis_features (GstPluginFeature *feature, gpointer data) {
  8. GstElementFactory *factory;
  9. if (!GST_IS_ELEMENT_FACTORY (feature))
  10. return FALSE;
  11. factory = GST_ELEMENT_FACTORY (feature);
  12. if (!g_strrstr (gst_element_factory_get_klass (factory), "Visualization"))
  13. return FALSE;
  14. return TRUE;
  15. }
  16. int main(int argc, charchar *argv[]) {
  17. GstElement *pipeline, *vis_plugin;
  18. GstBus *bus;
  19. GstMessage *msg;
  20. GList *list, *walk;
  21. GstElementFactory *selected_factory = NULL;
  22. guint flags;
  23. /* Initialize GStreamer */
  24. gst_init (&argc, &argv);
  25. /* Get a list of all visualization plugins */
  26. list = gst_registry_feature_filter (gst_registry_get_default (), filter_vis_features, FALSE, NULL);
  27. /* Print their names */
  28. g_print("Available visualization plugins:\n");
  29. for (walk = list; walk != NULL; walk = g_list_next (walk)) {
  30. const gchar *name;
  31. GstElementFactory *factory;
  32. factory = GST_ELEMENT_FACTORY (walk->data);
  33. name = gst_element_factory_get_longname (factory);
  34. g_print("  %s\n", name);
  35. if (selected_factory == NULL || g_str_has_prefix (name, "GOOM")) {
  36. selected_factory = factory;
  37. }
  38. }
  39. /* Don't use the factory if it's still empty */
  40. /* e.g. no visualization plugins found */
  41. if (!selected_factory) {
  42. g_print ("No visualization plugins found!\n");
  43. ;
  44. }
  45. /* We have now selected a factory for the visualization element */
  46. g_print ("Selected '%s'\n", gst_element_factory_get_longname (selected_factory));
  47. vis_plugin = gst_element_factory_create (selected_factory, NULL);
  48. if (!vis_plugin)
  49. ;
  50. /* Build the pipeline */
  51. pipeline = gst_parse_launch ("playbin2 uri=http://radio.hbr1.com:19800/ambient.ogg", NULL);
  52. /* Set the visualization flag */
  53. g_object_get (pipeline, "flags", &flags, NULL);
  54. flags |= GST_PLAY_FLAG_VIS;
  55. g_object_set (pipeline, "flags", flags, NULL);
  56. /* set vis plugin for playbin2 */
  57. g_object_set (pipeline, "vis-plugin", vis_plugin, NULL);
  58. /* Start playing */
  59. gst_element_set_state (pipeline, GST_STATE_PLAYING);
  60. /* Wait until error or EOS */
  61. bus = gst_element_get_bus (pipeline);
  62. msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
  63. /* Free resources */
  64. if (msg != NULL)
  65. gst_message_unref (msg);
  66. gst_plugin_feature_list_free (list);
  67. gst_object_unref (bus);
  68. gst_element_set_state (pipeline, GST_STATE_NULL);
  69. gst_object_unref (pipeline);
  70. ;
  71. }
  72. </span>

工作流程

首先,我们都知道通过设置GST_PLAY_FLAG_VIS标志可以让playbin2的音频可视化。如果媒体文件里面已经包含了视频,那么这个标志就不起作用。

[objc] view
plain
 copy

  1. /* Set the visualization flag */
  2. g_object_get (pipeline, "flags", &flags, NULL);
  3. flags |= GST_PLAY_FLAG_VIS;
  4. g_object_set (pipeline, "flags", flags, NULL);

如果用户没有指定可视化插件,playbin2会使用goom来做(如果没有这个element就不能进行音频可视化)。本教程剩下的部分会显示如何来发现一个可用的可视化element并指定playbin2使用。

[objc] view
plain
 copy

  1. /* Get a list of all visualization plugins */
  2. list = gst_registry_feature_filter (gst_registry_get_default (), filter_vis_features, FALSE, NULL);

gst_registry_feature_filter()检查GStreamer当前所有注册的element并选择那些filter_vis_features函数返回TRUE的element。

[objc] view
plain
 copy

  1. /* Return TRUE if this is a Visualization element */
  2. static gboolean filter_vis_features (GstPluginFeature *feature, gpointer data) {
  3. GstElementFactory *factory;
  4. if (!GST_IS_ELEMENT_FACTORY (feature))
  5. return FALSE;
  6. factory = GST_ELEMENT_FACTORY (feature);
  7. if (!g_strrstr (gst_element_factory_get_klass (factory), "Visualization"))
  8. return FALSE;
  9. return TRUE;
  10. }

这里牵涉到一点关于GStreamer element的理论:每一个GStreamer在运行时加载的文件都被认为是插件。一个插件可以有多个功能,不同种类的功能。

这个函数简单的丢弃了所有不能批量复现的功能以及不包含“可视化”这个功能的插件。

[objc] view
plain
 copy

  1. /* Print their names */
  2. g_print("Available visualization plugins:\n");
  3. for (walk = list; walk != NULL; walk = g_list_next (walk)) {
  4. const gchar *name;
  5. GstElementFactory *factory;
  6. factory = GST_ELEMENT_FACTORY (walk->data);
  7. name = gst_element_factory_get_longname (factory);
  8. g_print("  %s\n", name);
  9. if (selected_factory == NULL || g_str_has_prefix (name, "GOOM")) {
  10. selected_factory = factory;
  11. }
  12. }

一旦我们有了可视化插件的列表,我们会打印出它们的名字(gst_element_factory_get_longname())并选择一个(比如:GOOM)。

[objc] view
plain
 copy

  1. /* We have now selected a factory for the visualization element */
  2. g_print ("Selected '%s'\n", gst_element_factory_get_longname (selected_factory));
  3. vis_plugin = gst_element_factory_create (selected_factory, NULL);
  4. if (!vis_plugin)
  5. ;

选中的工厂用来生成一个GstElement,通过vis-plugin属性来传给playbin2。

[objc] view
plain
 copy

  1. /* set vis plugin for playbin2 */
  2. g_object_set (pipeline, "vis-plugin", vis_plugin, NULL);

Bingo,就这么简单。

最新文章

  1. 亡命之徒aaaaaa.......chao
  2. python-函数
  3. Titanium系列--利用Titanium开发android App实战总结
  4. [转载] C++11中的右值引用
  5. javascript实现数据结构:线性表--简单示例及线性表的顺序表示和实现
  6. 数位DP初步 bzoj1026 hdu2089 hdu3555
  7. Linux-C语言中gettimeofday()函数的使用方法(转载)
  8. NSMutableDictionary
  9. RabbitMQ之工作队列
  10. day4 liaoxuefeng--调试、线程、正则表达式
  11. iOS开源加密相册Agony的实现(一)
  12. [Swift]LeetCode729. 我的日程安排表 I | My Calendar I
  13. vue+cordova构建跨平台应用集成并使用Cordova plugin
  14. Python——Django-settings.py的内容
  15. mybatis支持jdk8等localdate类型
  16. js arrayBuffer 字节序问题,小端法,大端法
  17. MySQL 分区间进行数据展示 实例
  18. JS案例 - 可自动伸缩高度的textarea文本框
  19. 【精】C语言之变量存储类型
  20. Winform学习之随笔一:Log4net

热门文章

  1. 【mysql】知识点
  2. leetcode解题报告(33): Find All Numbers Disappeared in an Array
  3. leetcode解题报告(29):Student Attendance Record I
  4. 洛谷 P1257 平面上的最接近点对 题解
  5. UOJ449. 【集训队作业2018】喂鸽子 [概率期望,min-max容斥,生成函数]
  6. java学习笔记(2)注释、public lcass、class、标识符、字面值、变量
  7. nvarchar(MAX) 、ntext的差别
  8. MySQL:服务无法启动(1067)问题
  9. IIS错误代码500.21 ,Nhibernate更新报错,委托的使用。action传参数
  10. T-MAX组--项目冲刺(第三天)