【GStreamer开发】GStreamer播放教程03——pipeline的快捷访问
目的
《GStreamer08——pipeline的快捷访问》展示了一个应用如何用appsrc和appsink这两个特殊的element在pipeline中手动输入/提取数据。playbin2也允许使用这两个element,但连接它们的方法有所不同。连接appsink到playbin2的方法在后面还会提到。这里我们主要讲述:
如何把appsrc连接到playbin2
如何配置appsrc
一个playbin2波形发生器
- <span style="font-size:14px;">#include <gst/gst.h>
- #include <string.h>
- #define CHUNK_SIZE 1024 /* Amount of bytes we are sending in each buffer */
- #define SAMPLE_RATE 44100 /* Samples per second we are sending */
- #define AUDIO_CAPS "audio/x-raw-int,channels=1,rate=%d,signed=(boolean)true,width=16,depth=16,endianness=BYTE_ORDER"
- /* Structure to contain all our information, so we can pass it to callbacks */
- typedef struct _CustomData {
- GstElement *pipeline;
- GstElement *app_source;
- 4 num_samples; /* Number of samples generated so far (for timestamp generation) */
- gfloat a, b, c, d; /* For waveform generation */
- guint sourceid; /* To control the GSource */
- GMainLoop *main_loop; /* GLib's Main Loop */
- } CustomData;
- /* This method is called by the idle GSource in the mainloop, to feed CHUNK_SIZE bytes into appsrc.
- * The ide handler is added to the mainloop when appsrc requests us to start sending data (need-data signal)
- * and is removed when appsrc has enough data (enough-data signal).
- */
- static gboolean push_data (CustomData *data) {
- GstBuffer *buffer;
- GstFlowReturn ret;
- int i;
- gint16 *raw;
- ; /* Because each sample is 16 bits */
- gfloat freq;
- /* Create a new empty buffer */
- buffer = gst_buffer_new_and_alloc (CHUNK_SIZE);
- /* Set its timestamp and duration */
- 4_scale (data->num_samples, GST_SECOND, SAMPLE_RATE);
- 4_scale (CHUNK_SIZE, GST_SECOND, SAMPLE_RATE);
- /* Generate some psychodelic waveforms */
- raw = (gint16 *)GST_BUFFER_DATA (buffer);
- data->c += data->d;
- 000;
- 100 + 11000 * data->d;
- ; i < num_samples; i++) {
- data->a += data->b;
- data->b -= data->a / freq;
- 6)(5500 * data->a);
- }
- data->num_samples += num_samples;
- /* Push the buffer into the appsrc */
- g_signal_emit_by_name (data->app_source, "push-buffer", buffer, &ret);
- /* Free the buffer now that we are done with it */
- gst_buffer_unref (buffer);
- if (ret != GST_FLOW_OK) {
- /* We got some error, stop sending data */
- return FALSE;
- }
- return TRUE;
- }
- /* This signal callback triggers when appsrc needs data. Here, we add an idle handler
- * to the mainloop to start pushing data into the appsrc */
- static void start_feed (GstElement *source, guint size, CustomData *data) {
- ) {
- g_print ("Start feeding\n");
- data->sourceid = g_idle_add ((GSourceFunc) push_data, data);
- }
- }
- /* This callback triggers when appsrc has enough data and we can stop sending.
- * We remove the idle handler from the mainloop */
- static void stop_feed (GstElement *source, CustomData *data) {
- ) {
- g_print ("Stop feeding\n");
- g_source_remove (data->sourceid);
- ;
- }
- }
- /* This function is called when an error message is posted on the bus */
- static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
- GError *err;
- gchar *debug_info;
- /* Print error details on the screen */
- gst_message_parse_error (msg, &err, &debug_info);
- g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
- g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
- g_clear_error (&err);
- g_free (debug_info);
- g_main_loop_quit (data->main_loop);
- }
- /* This function is called when playbin2 has created the appsrc element, so we have
- * a chance to configure it. */
- static void source_setup (GstElement *pipeline, GstElement *source, CustomData *data) {
- gchar *audio_caps_text;
- GstCaps *audio_caps;
- g_print ("Source has been created. Configuring.\n");
- data->app_source = source;
- /* Configure appsrc */
- audio_caps_text = g_strdup_printf (AUDIO_CAPS, SAMPLE_RATE);
- audio_caps = gst_caps_from_string (audio_caps_text);
- g_object_set (source, "caps", audio_caps, NULL);
- g_signal_connect (source, "need-data", G_CALLBACK (start_feed), data);
- g_signal_connect (source, "enough-data", G_CALLBACK (stop_feed), data);
- gst_caps_unref (audio_caps);
- g_free (audio_caps_text);
- }
- int main(int argc, charchar *argv[]) {
- CustomData data;
- GstBus *bus;
- /* Initialize cumstom data structure */
- , sizeof (data));
- ; /* For waveform generation */
- ;
- /* Initialize GStreamer */
- gst_init (&argc, &argv);
- /* Create the playbin2 element */
- data.pipeline = gst_parse_launch ("playbin2 uri=appsrc://", NULL);
- g_signal_connect (data.pipeline, "source-setup", G_CALLBACK (source_setup), &data);
- /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
- bus = gst_element_get_bus (data.pipeline);
- gst_bus_add_signal_watch (bus);
- g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, &data);
- gst_object_unref (bus);
- /* Start playing the pipeline */
- gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
- /* Create a GLib Main Loop and set it to run */
- data.main_loop = g_main_loop_new (NULL, FALSE);
- g_main_loop_run (data.main_loop);
- /* Free resources */
- gst_element_set_state (data.pipeline, GST_STATE_NULL);
- gst_object_unref (data.pipeline);
- ;
- }
- </span>
把appsrc用作pipeline的source,仅仅把playbin2的UIR设置成appsrc://即可。
- <span style="font-size:14px;"> /* Create the playbin2 element */
- data.pipeline = gst_parse_launch ("playbin2 uri=appsrc://", NULL);</span>
playbin2创建一个内部的appsrc element并且发送source-setup信号来通知应用进行设置。
- <span style="font-size:14px;"> g_signal_connect (data.pipeline, "source-setup", G_CALLBACK (source_setup), &data);</span>
特别地,设置appsrc的caps属性是很重要的,因为一旦这个信号的处理返回,playbin2就会根据返回值来初始化下一个element。
- <span style="font-size:14px;">/* This function is called when playbin2 has created the appsrc element, so we have
- * a chance to configure it. */
- static void source_setup (GstElement *pipeline, GstElement *source, CustomData *data) {
- gchar *audio_caps_text;
- GstCaps *audio_caps;
- g_print ("Source has been created. Configuring.\n");
- data->app_source = source;
- /* Configure appsrc */
- audio_caps_text = g_strdup_printf (AUDIO_CAPS, SAMPLE_RATE);
- audio_caps = gst_caps_from_string (audio_caps_text);
- g_object_set (source, "caps", audio_caps, NULL);
- g_signal_connect (source, "need-data", G_CALLBACK (start_feed), data);
- g_signal_connect (source, "enough-data", G_CALLBACK (stop_feed), data);
- gst_caps_unref (audio_caps);
- g_free (audio_caps_text);
- }</span>
appsrc的配置和《GStreamer08——pipeline的快捷访问》里面一样:caps设置成audio/x-raw-int,注册两个回调,这样element可以在需要/停止给它推送数据时通知应用。具体细节请参考《GStreamer08——pipeline的快捷访问》。
在这个点之后,playbin2接管处理了剩下的pipeline,应用仅仅需要生成数据即可。
至于使用appsink来从从playbin2里面提取数据,在后面的教程里面再讲述。
【GStreamer开发】GStreamer播放教程03——pipeline的快捷访问的更多相关文章
- 【GStreamer开发】GStreamer基础教程08——pipeline的快捷访问
目标 GStreamer建立的pipeline不需要完全关闭.有多种方法可以让数据在任何时候送到pipeline中或者从pipeline中取出.本教程会展示: 如何把外部数据送到pipeline中 如 ...
- 【GStreamer开发】GStreamer基础教程03——动态pipeline
本教程介绍pipeline的一种新的创建方式--在运行中创建,而不是在运行前一次性的创建结束. 介绍 在这篇教程里的pipeline并非在运行前就全部创建结束的.放松一下,这样做没有任何问题.如果我们 ...
- 【GStreamer开发】GStreamer播放教程07——自定义playbin2的sink
目标 通过手动选择音频和视频的sink,playbin2可以进一步定制.这允许使用playbin2的应用在解码后可以自行做最终的渲染和显示.本教程展示了: 如何替换playbin2选择的sink 如何 ...
- 【GStreamer开发】GStreamer基础教程07——多线程和Pad的有效性
目标 GStreamer会自动处理多线程这部分,但在有些情况下,你需要手动对线程做解耦.本教程会教你怎样才能做到这一点,另外也展示了Pad的有效性.主要内容包括: 如何针对部分的pipeline建立一 ...
- 【GStreamer开发】GStreamer基础教程01——Hello World
目标 对于一个软件库来说,没有比在屏幕上打印出Hello World更近直观的第一印象了.因为我们是在和一个多媒体的framework打交道,所以我们准备播放一段视频来代替Hello World.不要 ...
- gstreamer应用开发(播放器)之旅
GStreamer开发,主要分为两块:应用开发.插件开发. 插件开发人员,通常是编解码库的作者(做出了编解码库后,希望gstreamer能用起来这个库,因此增加这个适配层).芯片原厂人员(将自家的hw ...
- 安装gstreamer开发环境
ubuntu中安装gstreamer开发环境: * 安装gstreamer基本库,工具,以及插件 sudo apt--dev gstreamer-tools gstreamer0.-tools gst ...
- 【转】一步一步教你在Ubuntu12.04搭建gstreamer开发环境
原文网址:http://blog.csdn.net/xsl1990/article/details/8333062 闲得蛋疼 无聊寂寞冷 随便写写弄弄 看到网上蛮多搭建gstreamer开 ...
- [译]Vulkan教程(03)开发环境
[译]Vulkan教程(03)开发环境 这是我翻译(https://vulkan-tutorial.com)上的Vulkan教程的第3篇. In this chapter we'll set up y ...
随机推荐
- Win32 Error
一.Win32错误 也就是Win32子系统产生的错误.当我们在自己的代码里调用Windows系统的API函数,系统执行API内部代码,当API内部代码出现错误,会将预先定义好的错误代码写到调用这个AP ...
- Windows下串口编程
造冰箱的大熊猫@cnblogs 2019/1/27 将Windows下串口编程相关信息进行下简单小结,以备后用. 1.打开串口 打开串口使用CreateFile()函数.以打开COM6为例: HAN ...
- 本地存储API
一.定义 随着互联网的快速发展,基于网页的应用越来越普遍,同时也变得越来越复杂,为了满足各种各样的需求,会经常在本地存储大量的数据,HTML5规范提出了相关解决方案 本地存储设置读取方便,容量较大,s ...
- 转载 | Python AI 教学│k-means聚类算法及应用
关注我们的公众号哦!获取更多精彩哦! 1.问题导入 假如有这样一种情况,在一天你想去某个城市旅游,这个城市里你想去的有70个地方,现在你只有每一个地方的地址,这个地址列表很长,有70个位置.事先肯定要 ...
- 洛谷P5017摆渡车
题目 一道做法多种多样的题,DP做法的状态也很多. 我用\(dp[i]\)表示在第i秒发车的时间和,然后dp方程就很好写了 \(dp[i] = dp[j] + i车的等待时间\)j属于i-2m ~ i ...
- C# 百度API地址坐标互相转换
通过C#代码将地址字符串转为经纬度坐标,或者将经纬度转为具体的地址字符串,在不通外网的项目中是有需求的. 具体步骤: 一.创建BaiduMapHelper,用于定义地址信息和请求. public st ...
- 【caffe Layer】代码中文注释
src/caffe/proto/caffe.proto 中LayerParameter部分 // NOTE // Update the next available ID when you add a ...
- mysql 使用的三个小技巧
mysql 使用的三个小技巧 快速阅读 Mysql查询工具中如何查询多条语名,Mysql中如何设置变量,Mysql中如何查特定字段,后面再加* Mysql查询工具中如何查询多条语名 默认myslq只能 ...
- response.getWriter().println和@ResponseBody的比较及同时使用(用于回调函数)
@RequestMapping(value = "/test", method = { RequestMethod.GET, RequestMethod.POST }) @Resp ...
- 【Tomcat】本地域名访问配置
原路径:localhost:8080/jsja 1.把8080端口改为80端口 打开%TOMCAT_HOME%/conf/server.xml <Connector connectionTime ...
