目的

GStreamer08——pipeline的快捷访问》展示了一个应用如何用appsrc和appsink这两个特殊的element在pipeline中手动输入/提取数据。playbin2也允许使用这两个element,但连接它们的方法有所不同。连接appsink到playbin2的方法在后面还会提到。这里我们主要讲述:

如何把appsrc连接到playbin2

如何配置appsrc

一个playbin2波形发生器

[objc] view
plain
 copy

  1. <span style="font-size:14px;">#include <gst/gst.h>
  2. #include <string.h>
  3. #define CHUNK_SIZE 1024   /* Amount of bytes we are sending in each buffer */
  4. #define SAMPLE_RATE 44100 /* Samples per second we are sending */
  5. #define AUDIO_CAPS "audio/x-raw-int,channels=1,rate=%d,signed=(boolean)true,width=16,depth=16,endianness=BYTE_ORDER"
  6. /* Structure to contain all our information, so we can pass it to callbacks */
  7. typedef struct _CustomData {
  8. GstElement *pipeline;
  9. GstElement *app_source;
  10. 4 num_samples;   /* Number of samples generated so far (for timestamp generation) */
  11. gfloat a, b, c, d;     /* For waveform generation */
  12. guint sourceid;        /* To control the GSource */
  13. GMainLoop *main_loop;  /* GLib's Main Loop */
  14. } CustomData;
  15. /* This method is called by the idle GSource in the mainloop, to feed CHUNK_SIZE bytes into appsrc.
  16. * The ide handler is added to the mainloop when appsrc requests us to start sending data (need-data signal)
  17. * and is removed when appsrc has enough data (enough-data signal).
  18. */
  19. static gboolean push_data (CustomData *data) {
  20. GstBuffer *buffer;
  21. GstFlowReturn ret;
  22. int i;
  23. gint16 *raw;
  24. ; /* Because each sample is 16 bits */
  25. gfloat freq;
  26. /* Create a new empty buffer */
  27. buffer = gst_buffer_new_and_alloc (CHUNK_SIZE);
  28. /* Set its timestamp and duration */
  29. 4_scale (data->num_samples, GST_SECOND, SAMPLE_RATE);
  30. 4_scale (CHUNK_SIZE, GST_SECOND, SAMPLE_RATE);
  31. /* Generate some psychodelic waveforms */
  32. raw = (gint16 *)GST_BUFFER_DATA (buffer);
  33. data->c += data->d;
  34. 000;
  35. 100 + 11000 * data->d;
  36. ; i < num_samples; i++) {
  37. data->a += data->b;
  38. data->b -= data->a / freq;
  39. 6)(5500 * data->a);
  40. }
  41. data->num_samples += num_samples;
  42. /* Push the buffer into the appsrc */
  43. g_signal_emit_by_name (data->app_source, "push-buffer", buffer, &ret);
  44. /* Free the buffer now that we are done with it */
  45. gst_buffer_unref (buffer);
  46. if (ret != GST_FLOW_OK) {
  47. /* We got some error, stop sending data */
  48. return FALSE;
  49. }
  50. return TRUE;
  51. }
  52. /* This signal callback triggers when appsrc needs data. Here, we add an idle handler
  53. * to the mainloop to start pushing data into the appsrc */
  54. static void start_feed (GstElement *source, guint size, CustomData *data) {
  55. ) {
  56. g_print ("Start feeding\n");
  57. data->sourceid = g_idle_add ((GSourceFunc) push_data, data);
  58. }
  59. }
  60. /* This callback triggers when appsrc has enough data and we can stop sending.
  61. * We remove the idle handler from the mainloop */
  62. static void stop_feed (GstElement *source, CustomData *data) {
  63. ) {
  64. g_print ("Stop feeding\n");
  65. g_source_remove (data->sourceid);
  66. ;
  67. }
  68. }
  69. /* This function is called when an error message is posted on the bus */
  70. static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
  71. GError *err;
  72. gchar *debug_info;
  73. /* Print error details on the screen */
  74. gst_message_parse_error (msg, &err, &debug_info);
  75. g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
  76. g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
  77. g_clear_error (&err);
  78. g_free (debug_info);
  79. g_main_loop_quit (data->main_loop);
  80. }
  81. /* This function is called when playbin2 has created the appsrc element, so we have
  82. * a chance to configure it. */
  83. static void source_setup (GstElement *pipeline, GstElement *source, CustomData *data) {
  84. gchar *audio_caps_text;
  85. GstCaps *audio_caps;
  86. g_print ("Source has been created. Configuring.\n");
  87. data->app_source = source;
  88. /* Configure appsrc */
  89. audio_caps_text = g_strdup_printf (AUDIO_CAPS, SAMPLE_RATE);
  90. audio_caps = gst_caps_from_string (audio_caps_text);
  91. g_object_set (source, "caps", audio_caps, NULL);
  92. g_signal_connect (source, "need-data", G_CALLBACK (start_feed), data);
  93. g_signal_connect (source, "enough-data", G_CALLBACK (stop_feed), data);
  94. gst_caps_unref (audio_caps);
  95. g_free (audio_caps_text);
  96. }
  97. int main(int argc, charchar *argv[]) {
  98. CustomData data;
  99. GstBus *bus;
  100. /* Initialize cumstom data structure */
  101. , sizeof (data));
  102. ; /* For waveform generation */
  103. ;
  104. /* Initialize GStreamer */
  105. gst_init (&argc, &argv);
  106. /* Create the playbin2 element */
  107. data.pipeline = gst_parse_launch ("playbin2 uri=appsrc://", NULL);
  108. g_signal_connect (data.pipeline, "source-setup", G_CALLBACK (source_setup), &data);
  109. /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
  110. bus = gst_element_get_bus (data.pipeline);
  111. gst_bus_add_signal_watch (bus);
  112. g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, &data);
  113. gst_object_unref (bus);
  114. /* Start playing the pipeline */
  115. gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
  116. /* Create a GLib Main Loop and set it to run */
  117. data.main_loop = g_main_loop_new (NULL, FALSE);
  118. g_main_loop_run (data.main_loop);
  119. /* Free resources */
  120. gst_element_set_state (data.pipeline, GST_STATE_NULL);
  121. gst_object_unref (data.pipeline);
  122. ;
  123. }
  124. </span>

把appsrc用作pipeline的source,仅仅把playbin2的UIR设置成appsrc://即可。

[objc] view
plain
 copy

  1. <span style="font-size:14px;">  /* Create the playbin2 element */
  2. data.pipeline = gst_parse_launch ("playbin2 uri=appsrc://", NULL);</span>

playbin2创建一个内部的appsrc element并且发送source-setup信号来通知应用进行设置。

[objc] view
plain
 copy

  1. <span style="font-size:14px;">  g_signal_connect (data.pipeline, "source-setup", G_CALLBACK (source_setup), &data);</span>

特别地,设置appsrc的caps属性是很重要的,因为一旦这个信号的处理返回,playbin2就会根据返回值来初始化下一个element。

[objc] view
plain
 copy

  1. <span style="font-size:14px;">/* This function is called when playbin2 has created the appsrc element, so we have
  2. * a chance to configure it. */
  3. static void source_setup (GstElement *pipeline, GstElement *source, CustomData *data) {
  4. gchar *audio_caps_text;
  5. GstCaps *audio_caps;
  6. g_print ("Source has been created. Configuring.\n");
  7. data->app_source = source;
  8. /* Configure appsrc */
  9. audio_caps_text = g_strdup_printf (AUDIO_CAPS, SAMPLE_RATE);
  10. audio_caps = gst_caps_from_string (audio_caps_text);
  11. g_object_set (source, "caps", audio_caps, NULL);
  12. g_signal_connect (source, "need-data", G_CALLBACK (start_feed), data);
  13. g_signal_connect (source, "enough-data", G_CALLBACK (stop_feed), data);
  14. gst_caps_unref (audio_caps);
  15. g_free (audio_caps_text);
  16. }</span>

appsrc的配置和《GStreamer08——pipeline的快捷访问》里面一样:caps设置成audio/x-raw-int,注册两个回调,这样element可以在需要/停止给它推送数据时通知应用。具体细节请参考《GStreamer08——pipeline的快捷访问》。

在这个点之后,playbin2接管处理了剩下的pipeline,应用仅仅需要生成数据即可。

至于使用appsink来从从playbin2里面提取数据,在后面的教程里面再讲述。

【GStreamer开发】GStreamer播放教程03——pipeline的快捷访问的更多相关文章

  1. 【GStreamer开发】GStreamer基础教程08——pipeline的快捷访问

    目标 GStreamer建立的pipeline不需要完全关闭.有多种方法可以让数据在任何时候送到pipeline中或者从pipeline中取出.本教程会展示: 如何把外部数据送到pipeline中 如 ...

  2. 【GStreamer开发】GStreamer基础教程03——动态pipeline

    本教程介绍pipeline的一种新的创建方式--在运行中创建,而不是在运行前一次性的创建结束. 介绍 在这篇教程里的pipeline并非在运行前就全部创建结束的.放松一下,这样做没有任何问题.如果我们 ...

  3. 【GStreamer开发】GStreamer播放教程07——自定义playbin2的sink

    目标 通过手动选择音频和视频的sink,playbin2可以进一步定制.这允许使用playbin2的应用在解码后可以自行做最终的渲染和显示.本教程展示了: 如何替换playbin2选择的sink 如何 ...

  4. 【GStreamer开发】GStreamer基础教程07——多线程和Pad的有效性

    目标 GStreamer会自动处理多线程这部分,但在有些情况下,你需要手动对线程做解耦.本教程会教你怎样才能做到这一点,另外也展示了Pad的有效性.主要内容包括: 如何针对部分的pipeline建立一 ...

  5. 【GStreamer开发】GStreamer基础教程01——Hello World

    目标 对于一个软件库来说,没有比在屏幕上打印出Hello World更近直观的第一印象了.因为我们是在和一个多媒体的framework打交道,所以我们准备播放一段视频来代替Hello World.不要 ...

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

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

  7. 安装gstreamer开发环境

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

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

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

  9. [译]Vulkan教程(03)开发环境

    [译]Vulkan教程(03)开发环境 这是我翻译(https://vulkan-tutorial.com)上的Vulkan教程的第3篇. In this chapter we'll set up y ...

随机推荐

  1. 99: AGC-018C 堆+思维

    $des$有 $X + Y + Z$ 个人,第 $i$ 个人有 $A_i$ 个金币,$B_i$ 个银币,$C_i$ 个铜币.选出 $X$ 个人获得其金币,选出 $Y$ 个人获得其银币,选出 $Z$ 个 ...

  2. 浅析python迭代器及生成器函数

    1. 什么是迭代协议? 迭代协议主要包括两方面的协议集,一种是迭代器协议,另一种是可迭代协议.对于迭代器协议来说,其要求迭代器对象在能够在迭代环境中一次产生一个结果.对于可迭代协议来说,就是一个对象序 ...

  3. 数据结构Java版之堆&堆排序(九)

    堆分为大顶堆,和小顶堆. 什么是堆? 堆可以看成是一棵二叉树,二叉树的元素是一个数组不断的从左到右轮训放置.如果是大顶堆,则大的数放上面一层,小的数放下面一层.上一层的数,一定大于下一层的数.小顶堆则 ...

  4. 详解Azure的权限控制

    注意:本文档仅限于Azure国际版,国内版略有不同   Azure中的角色分配相对来说是比较复杂的的,对于任何云组织来说,云的资源访问管理权限都是一项非常重要的功能,azure中的授权系统叫做基于角色 ...

  5. Ibatis自动解决sql注入机制

    疑问1:为什么IBatis解决了大部分的sql注入?(实际上还有部分sql语句需要关心sql注入,比如like) 之前写Java web,一直使用IBatis,从来没有考虑过sql注入:最近写php( ...

  6. Leet Code 771.宝石与石头

    Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S  ...

  7. python中字符截取[-1]、[:-1]、[::-1]、[n::-1]等使用方法的详细讲解(建议留存)

    python涉及字符截取的详细功能讲解: str = ' print("str[0:3]:"+str[0:3]) # 正向截取字符串 0~3(不包含3) 即 012 print(& ...

  8. 第2课第2节_Java面向对象编程_封装性_P【学习笔记】

    摘要:韦东山android视频学习笔记  面向对象程序的三大特性之封装性:把属性和方法封装在一个整体,同时添加权限访问. 1.封装性的简单程序如下,看一下第19行,如果我们不对age变量进行权限的管控 ...

  9. 微信小程序之自定义底部弹出框动画

    最近做小程序时,会经常用到各种弹框.直接做显示和隐藏虽然也能达到效果,但是体验性太差,也比较简单粗暴.想要美美地玩,添加点动画还是非常有必要的.下面做一个底部上滑的弹框. wxml <view ...

  10. 性能分析 函数粒度 函数里的一条语句 汇编 反编译 机器指令 %rbx,%rbp

    在Linux下做性能分析3:perf - 知乎 https://zhuanlan.zhihu.com/p/22194920 Linux Perf 性能分析工具及火焰图浅析 - 知乎 https://z ...