目标

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,就这么简单。

【GStreamer开发】GStreamer播放教程06——可视化音频的更多相关文章

  1. 【GStreamer开发】GStreamer播放教程09——数字音频传输

    目标 本教程展示GStreamer是如何进行数字音频传输的. 介绍 在常见的模拟格式外,高端的音频系统通常都接受数字格式,压缩的非压缩的都能接受.因为音频信号是从电脑传到音箱,用一种更有弹性的形态会更 ...

  2. 【GStreamer开发】GStreamer基础教程16——平台相关的element

    目标 虽然GStreamer是跨平台的framework,但不是所有的element都是在所有平台下都有的.比如,音频和视频的sink都非常依赖于当前的window系统,根据当前的平台需要选择不同的e ...

  3. gstreamer应用开发(播放器)之旅

    GStreamer开发,主要分为两块:应用开发.插件开发. 插件开发人员,通常是编解码库的作者(做出了编解码库后,希望gstreamer能用起来这个库,因此增加这个适配层).芯片原厂人员(将自家的hw ...

  4. 安装gstreamer开发环境

    ubuntu中安装gstreamer开发环境: * 安装gstreamer基本库,工具,以及插件 sudo apt--dev gstreamer-tools gstreamer0.-tools gst ...

  5. 【转】一步一步教你在Ubuntu12.04搭建gstreamer开发环境

    原文网址:http://blog.csdn.net/xsl1990/article/details/8333062 闲得蛋疼    无聊寂寞冷    随便写写弄弄 看到网上蛮多搭建gstreamer开 ...

  6. FLV提取AAC音频单独播放并实现可视化的频谱

    如上图,要实现对FLV直播流中音频的识别,并展示成一个音频相关的动态频谱. 一. 首先了解下什么是声音? 能量波,有频率有振幅,频率高低就是音调,振幅大小就是音量:采样率是对频率采样,采样精度是对幅度 ...

  7. [C#] NAudio 库的各种常用使用方式: 播放 录制 转码 音频可视化

    概述 在 NAudio 中, 常用类型有 WaveIn, WaveOut, WaveStream, WaveFileWriter, WaveFileReader 以及接口: IWaveProvider ...

  8. Apple Watch开发快速入门教程

     Apple Watch开发快速入门教程  试读下载地址:http://pan.baidu.com/s/1eQ8JdR0 介绍:苹果为Watch提供全新的开发框架WatchKit.本教程是国内第一本A ...

  9. 开发快平台(M302I小e开发板系列教程)

    开发快平台(M302I小e开发板系列教程) 开发块平台ESP8266模块相关理解 一. M302I小e开发板源码注释,源码基于:v1.4.0.8-u34.zip 1. user_main.c /*** ...

随机推荐

  1. aix 10代oracle zabbix2.4.4 日志监控

    同一类型的监控项,zabbix 2.4的客户端也支持日志监控,可是在参数个数上有问题,如果把所有参数都放满,监控项会提示too mant parameters,无法 生效取数据, 对于不同的正则式.m ...

  2. PageHelper的问题

    如果分页语句没有被消耗掉,它一直保留着,直到被织入到下一次查询语句,如果 被织入的查询语句自己有LIMIT限制,那么两个LIMIT就导致语法错误了. PageHelper.startPage(page ...

  3. fork()函数 图解

    code #include<stdio.h> #include <getopt.h> #include<iostream> #include<string&g ...

  4. [洛谷 P4556] 雨天的尾巴

    传送门 Solution 线段树合并的入门题 lca可以在dfs的时候离线求(用并查集) 更新的点有每条链的两个端点,它们的lca和dad[lca] 为了节省空间,lca和dad[lca]的更新可以先 ...

  5. LeetCode之最大子段和

    1.原问题 给定一个数组,求这个数组的连续子数组中,最大的那一段的和.如数组[-2,1,-3,4,-1,2,1,-5,4] 的子段为:[-2,1].[1,-3,4,-1].[4,-1,2,1].….[ ...

  6. Oracle语法 及 SQL题目(三)

    目录 SQL题目六 第一个问题思路(查询酒类商品的总点击量) 第二个问题思路(查询每个类别所属商品的总点击量,并按降序排列) 第三个问题思路(查询所有类别中最热门的品种(点击量最高),并按点击量降顺序 ...

  7. 深度学习面试题12:LeNet(手写数字识别)

    目录 神经网络的卷积.池化.拉伸 LeNet网络结构 LeNet在MNIST数据集上应用 参考资料 LeNet是卷积神经网络的祖师爷LeCun在1998年提出,用于解决手写数字识别的视觉任务.自那时起 ...

  8. JWT签名算法

    JWT签名算法 JWT签名算法中,一般有两个选择,一个采用HS256,另外一个就是采用RS256. 签名实际上是一个加密的过程,生成一段标识(也是JWT的一部分)作为接收方验证信息是否被篡改的依据. ...

  9. gitlab搭建与基本使用【转】

    一.git.github.gitlab的区别Git是版本控制系统,Github是在线的基于Git的代码托管服务.GitHub是2008年由Ruby on Rails编写而成.GitHub同时提供付费账 ...

  10. Visual C++2010的使用

    Tools->Settings>Rest... 还原所有设置 运行程序:"D:\Program Files\VCExpress\Install\Microsoft Visual ...