开发者

Unable to play .wav file using gstreamer apis

开发者 https://www.devze.com 2023-01-15 15:54 出处:网络
Following code is written to play a .wav file but it doesn\'t seem to work. I would like to know if i am missing something in it.

Following code is written to play a .wav file but it doesn't seem to work. I would like to know if i am missing something in it.

Code:

开发者_StackOverflow中文版#include <gst/gst.h>
#include <glib.h>

int main(int argc , char *argv[])
{
GMainLoop *loop;
GstElement *source,*audioparser,*sink,*pipeline;
GstBus *bus;

gst_init(&argc,&argv);

// create a pipeline
loop = g_main_loop_new (NULL, FALSE);
pipeline = gst_pipeline_new ("wav-player");
source = gst_element_factory_make("filesrc","file-source");
audioparser = gst_element_factory_make("wavparse","wav-parser");
sink = gst_element_factory_make("alsasink","sink1");
g_object_set (G_OBJECT (source), "location", argv[1], NULL);
gst_element_set_state (pipeline, GST_STATE_NULL);
// set location to current sourceg_object_set(G_OBJECT(source),"location",argv[1],NULL);

// add elements to bin
gst_bin_add_many(GST_BIN(pipeline),source,audioparser,sink,NULL);

gst_element_link_many(source,audioparser,sink,NULL);

// create bus
bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
gst_bus_add_watch (bus, bus_call, loop);
gst_object_unref (bus);
gst_element_set_state(pipeline, GST_STATE_PLAYING);
g_main_loop_run (loop);
return 1;
}

Please compile this using following command:

 gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) wav.c -o wavparser

Thanks in advance


Just use playbin2 instead of a hand-crafted pipeline

that is, replace everything from "pipeline = gst_pipeline_new()" to "gst_element_link_many" by:

pipeline = gst_element_factory_make("playbin2", NULL);
g_object_set(pipeline, "uri", "file:///the/file/I/want.wav", NULL);


maybe playbin2 is exactly what you need, but answerring question: wavparse does not have static src-pad, so you must handle "pad-added" signal from this element on runtime. something like the code:

gst_bin_add_many (GST_BIN (pipeline), wavsrc, wavparse,audioconvert, audiosink, NULL);
g_object_set (G_OBJECT (wavsrc), "location", "sound.wav", NULL);
gst_element_link(wavsrc, wavparse);
gst_element_link(audioconvert, audiosink);
g_signal_connect (wavparse, "pad-added", G_CALLBACK (on_pad_added), audioconvert);

where: wavsrc is filesrc, wavparse is wavparse, audioconvert is audioconvert, audiosink is alsasink (i'm not sure alsasink works for you, so you can choose another)

void on_pad_added (GstElement *src_element, GstPad *src_pad, gpointer data)
{
    g_print ("Linking dynamic pad...\n");

    GstElement *sink_element = (GstElement *) data; // is audioconvert
    GstPad *sink_pad = gst_element_get_static_pad (sink_element, "sink");
    gst_pad_link (src_pad, sink_pad);

    gst_object_unref (sink_pad);
    src_element = NULL; // yup, i don't want "unused" warning here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号