GStreamer播放教程03——pipeline的快捷访问

目的

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

如何把appsrc连接到playbin2

如何配置appsrc


一个playbin2波形发生器

#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;
  
  guint64 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;
  gint num_samples = CHUNK_SIZE / 2; /* 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 */
  GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (data->num_samples,GST_SECOND,SAMPLE_RATE);
  GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (CHUNK_SIZE,SAMPLE_RATE);
  
  /* Generate some psychodelic waveforms */
  raw = (gint16 *)GST_BUFFER_DATA (buffer);
  data->c += data->d;
  data->d -= data->c / 1000;
  freq = 1100 + 1000 * data->d;
  for (i = 0; i < num_samples; i++) {
    data->a += data->b;
    data->b -= data->a / freq;
    raw[i] = (gint16)(500 * 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) {
  if (data->sourceid == 0) {
    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) {
  if (data->sourceid != 0) {
    g_print ("Stop feeding\n");
    g_source_remove (data->sourceid);
    data->sourceid = 0;
  }
}
  
/* 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,char *argv[]) {
  CustomData data;
  GstBus *bus;
  
  /* Initialize cumstom data structure */
  memset (&data,sizeof (data));
  data.b = 1; /* For waveform generation */
  data.d = 1;
  
  /* 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);
  return 0;
}

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

  /* Create the playbin2 element */
  data.pipeline = gst_parse_launch ("playbin2 uri=appsrc://",NULL);

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

  g_signal_connect (data.pipeline,&data);

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

/* This function is called when playbin2 has created the appsrc element,data);
  gst_caps_unref (audio_caps);
  g_free (audio_caps_text);
}

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


什么是设计模式一套被反复使用、多数人知晓的、经过分类编目的、代码 设计经验 的总结;使用设计模式是为了 可重用 代码、让代码 更容易 被他人理解、保证代码 可靠性;设计模式使代码编制  真正工程化;设计模式使软件工程的 基石脉络, 如同大厦的结构一样;并不直接用来完成代码的编写,而是 描述 在各种不同情况下,要怎么解决问题的一种方案;能使不稳定依赖于相对稳定、具体依赖于相对抽象,避免引
单一职责原则定义(Single Responsibility Principle,SRP)一个对象应该只包含 单一的职责,并且该职责被完整地封装在一个类中。Every  Object should have  a single responsibility, and that responsibility should be entirely encapsulated by t
动态代理和CGLib代理分不清吗,看看这篇文章,写的非常好,强烈推荐。原文截图*************************************************************************************************************************原文文本************
适配器模式将一个类的接口转换成客户期望的另一个接口,使得原本接口不兼容的类可以相互合作。
策略模式定义了一系列算法族,并封装在类中,它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
设计模式讲的是如何编写可扩展、可维护、可读的高质量代码,它是针对软件开发中经常遇到的一些设计问题,总结出来的一套通用的解决方案。
模板方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中,使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。
迭代器模式提供了一种方法,用于遍历集合对象中的元素,而又不暴露其内部的细节。
外观模式又叫门面模式,它提供了一个统一的(高层)接口,用来访问子系统中的一群接口,使得子系统更容易使用。
单例模式(Singleton Design Pattern)保证一个类只能有一个实例,并提供一个全局访问点。
组合模式可以将对象组合成树形结构来表示“整体-部分”的层次结构,使得客户可以用一致的方式处理个别对象和对象组合。
装饰者模式能够更灵活的,动态的给对象添加其它功能,而不需要修改任何现有的底层代码。
观察者模式(Observer Design Pattern)定义了对象之间的一对多依赖,当对象状态改变的时候,所有依赖者都会自动收到通知。
代理模式为对象提供一个代理,来控制对该对象的访问。代理模式在不改变原始类代码的情况下,通过引入代理类来给原始类附加功能。
工厂模式(Factory Design Pattern)可细分为三种,分别是简单工厂,工厂方法和抽象工厂,它们都是为了更好的创建对象。
状态模式允许对象在内部状态改变时,改变它的行为,对象看起来好像改变了它的类。
命令模式将请求封装为对象,能够支持请求的排队执行、记录日志、撤销等功能。
备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象。备忘录模式属于行为型模式。 基本介绍 **意图:**在不破坏封装性的前提下,捕获一个对象的内部状态,并在该
顾名思义,责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为
享元模式(Flyweight Pattern)(轻量级)(共享元素)主要用于减少创建对象的数量,以减少内存占用和提高性能。这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结