gstreamer pipeline添加 caps 于插件之间

注:如果插件没有caps属性,则需使用link_alsa_element_with_filter将caps置于两插件之间来实现。

转自:http://e2e.ti.com/support/embedded/linux/f/354/p/569574/2087961


#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#include <gst/gst.h>
static gboolean link_alsa_element_with_filter (GstElement *element1,GstElement *element2)
{
/* CAPS to be linked:
* audio/x-raw-int,signed=true,width=32,depth=32,format=S32LE,rate=96000,channels=4
* */
gboolean link_ok;
GstCaps *caps;
caps = gst_caps_new_simple ("audio/x-raw-int",
"endianness",G_TYPE_INT,1234,255);">"signed",G_TYPE_BOOLEAN,TRUE,255);">"width",16,255);">"depth",255);">"channels",2,255);">"rate",44100,255);">NULL);
link_ok = gst_element_link_filtered (element1,element2,caps);
gst_caps_unref (caps);
if (!link_ok) {
g_warning ("Failed to link element1 and element2!(source->queue)");
}
return link_ok;
static gboolean bus_call (GstBus *bus,GstMessage *msg,gpointer data)
GMainLoop *loop = (GMainLoop *) data;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_EOS:
g_print ("End of stream\n");
g_main_loop_quit (loop);
break;
case GST_MESSAGE_ERROR: {
gchar *debug;
GError *error;
gst_message_parse_error (msg,&error,&debug);
g_free (debug);
g_printerr ("Error: %s\n",error->message);
g_error_free (error);
default:
return TRUE;
/* Main function for audio pipeline initialization and looping streaming process */
gint main (gint argc,gchar **argv) {
GMainLoop *loop;
GstElement *pipeline;
GstElement *source;
GstElement *queues;
GstElement *sink;
GstBus *bus;
// guint bus_watch_id;
// gboolean ret;
/* Initialisation */
gst_init (&argc,&argv);
loop = g_main_loop_new (NULL,FALSE);
/* Creategstreamerelements:
*/
pipeline = gst_pipeline_new ("audio_stream");
source = gst_element_factory_make ("alsasrc","audio_source");
g_return_val_if_fail (source,-1);
g_object_set (G_OBJECT (source),"device","plughw:0,0",NULL);
queues = gst_element_factory_make ("queue","queues");
g_return_val_if_fail (queues,255);">sink = gst_element_factory_make ("alsasink","audio_sink");
g_return_val_if_fail (sink,255);">//g_object_set (G_OBJECT (sink),255);">/* Add a message handler */
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
gst_bus_add_watch (bus,bus_call,loop);
gst_object_unref (bus);
/* Add elements to the bin before linking them. */
gst_bin_add (GST_BIN (pipeline),source);

/* Link the pipeline */
link_alsa_element_with_filter (source,255);">gst_element_link_pads (queues,"src",sink,"sink");
/* Set the pipeline to "playing" state */
g_print ("Playing: %s\n",argv[1]);
gst_element_set_state (pipeline,GST_STATE_PLAYING);
/* Iterate */
g_print ("Running...\n");
g_main_loop_run (loop);
/* Out of the main loop,clean up nicely */
g_print ("Returned,stopping playback\n");
g_print ("Deleting pipeline\n");
gst_object_unref (GST_OBJECT (pipeline));
return 0;
}

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...