Question

#include <gst/gst.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    GstElement *element;
    GstElement *element1;
    GstElement *element2;
    GstElement *pipeline;

    gst_init(&argc, &argv);

    if(argc != 2)
    {
        g_print("usage: %s argv[1] \n", argv[0]);

        exit(EXIT_FAILURE);
    }

    /* Creating a new pipeline */
    pipeline = gst_pipeline_new("pipeline");

    /* creating a new *.mp3 source element */
    element = gst_element_factory_make("filesrc", "source");
    if(element != NULL)
        g_object_set(G_OBJECT(element), "location", argv[1], NULL);
    else
    {
        g_print("Failed to create element \n");
        exit(EXIT_FAILURE);
    }

    /* creating a new *.mp3 de-coder element */
    element1 = gst_element_factory_make("decodebin2", "decoder");
    if(element1 != NULL)
        g_print("element1 success \n");
    else
    {
        g_print("Failed to create element1 \n");
        exit(EXIT_FAILURE);
    }

    /* creating a new *.mp3 sink element */
    element2 = gst_element_factory_make("autoaudiosink", "play_audio");
    if(element2 != NULL)
        g_print("element2 success \n");
    else
    {
        g_print("Failed to create element2 \n");
        exit(EXIT_FAILURE);
    }

    /* Adding elements to pipeline */
    gst_bin_add_many(GST_BIN(pipeline), element, element1, element2, NULL);

    /* Linking src to sink element */
    gst_element_link_many(element, element1, element2, NULL);

    /* start playing */
    gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING);

    while(gst_bin_iterate_recurse(GST_BIN(pipeline)));

    /* stop playing */
    gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_NULL);

    /* un-referencing all the elements in the pipeline */
    gst_object_unref(GST_OBJECT(pipeline));

    return 0;
}

1) Compilation step:- gcc gst6.c -o gst6 pk-config --cflags --libs gstreamer-0.10, it is compiling without any warnings and errors.
2) Exported PKG_CONFIG_PATH env variable i.e. export PKG_CONFIG_PATH=/usr/lin/pkgconfig. 3) Execution step: ./gst6 /home/user/Downloads/*.mp3 4) Output: element1 success, element2 success
killed.

Unable to play the audio file. Please let me know whats wrong in my program. This is my first program.

Was it helpful?

Solution

This line:

while(gst_bin_iterate_recurse(GST_BIN(pipeline)));

does not make any sense. It requests an iterator in a loop, but does not use it, neither frees it.

If you want to wait until playing is finished, you probably need to use the GstBus functions gst_bus_have_pending and gst_bus_pop to handle messages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top