注:如果插件沒有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 <string>
#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,
"signed", G_TYPE_BOOLEAN, TRUE,
"width", G_TYPE_INT, 16,
"depth", G_TYPE_INT, 16,
"channels", G_TYPE_INT, 2,
"rate", G_TYPE_INT, 44100,
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);
g_main_loop_quit (loop);
break;
}
default:
break;
}
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;
GstCaps *caps;
// gboolean ret;
/* Initialisation */
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
/* Create gstreamer elements:
*/
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, -1);
sink = gst_element_factory_make ("alsasink", "audio_sink");
g_return_val_if_fail (sink, -1);
//g_object_set (G_OBJECT (sink), "device", "plughw:0,0", NULL);
/* 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);
gst_bin_add (GST_BIN (pipeline), queues);
gst_bin_add (GST_BIN (pipeline), sink);
/* Link the pipeline */
link_alsa_element_with_filter (source, queues);
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");
gst_element_set_state (pipeline, GST_STATE_NULL);
g_print ("Deleting pipeline\n");
gst_object_unref (GST_OBJECT (pipeline));
return 0;
}