Convert 2 channel audio to 6 channel audio

diegoavila diego_javila at hotmail.com
Thu Oct 17 16:16:09 UTC 2019


I have made this pipeline and im trying to convert it to c code
<http://gstreamer-devel.966125.n4.nabble.com/file/t378976/pipeline.png> 
this is my current code :

#include <gst/gst.h>
#include <iostream>
GMainLoop* _loop;
GstElement* _pipeline;
static gboolean print_field (GQuark field, const GValue * value, gpointer
pfx) {

  gchar *str = gst_value_serialize (value);

  g_print ("%s  %15s: %s\n", (gchar *) pfx, g_quark_to_string (field), str);

  g_free (str);

  return TRUE;

}
void link_two_elements(GstElement* src_element, GstElement* sink_element)
{
        if(!gst_element_link(src_element, sink_element))
                g_debug("Error linking %s to %s",
gst_element_get_name(src_element),
gst_element_get_name(sink_element));
} 
static gboolean bus_call (G_GNUC_UNUSED 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;
}
static void print_caps (const GstCaps * caps, const gchar * pfx) {

  guint i;

  g_return_if_fail (caps != NULL);

  if (gst_caps_is_any (caps)) {

    g_print ("%sANY\n", pfx);

    return;

  }

  if (gst_caps_is_empty (caps)) {

    g_print ("%sEMPTY\n", pfx);

    return;

  }

  for (i = 0; i < gst_caps_get_size (caps); i++) {

    GstStructure *structure = gst_caps_get_structure (caps, i);

    g_print ("%s%s\n", pfx, gst_structure_get_name (structure));

    gst_structure_foreach (structure, print_field, (gpointer) pfx);

  }

}

static void print_pad_capabilities (GstElement *element, gchar *pad_name) {

  GstPad *pad = NULL;

  GstCaps *caps = NULL;

  /* Retrieve pad */

  pad = gst_element_get_static_pad (element, pad_name);

  if (!pad) {

    g_printerr ("Could not retrieve pad '%s'\n", pad_name);

    return;

  }

  /* Retrieve negotiated caps (or acceptable caps if negotiation is not
finished yet) */

  caps = gst_pad_get_current_caps (pad);

  if (!caps)

    caps = gst_pad_query_caps (pad, NULL);

  /* Print and free */

  g_print ("Caps for the %s pad:\n", pad_name);

  print_caps (caps, "      ");

  gst_caps_unref (caps);

  gst_object_unref (pad);

}
static void cb_new_pad (GstElement *element, GstPad *pad, gpointer data)
{
  gchar *name;
  GstElement *other = data;

  name = gst_pad_get_name (pad);
  g_print ("A new pad %s was created for %s\n", name,
gst_element_get_name(element));
  g_free (name);

  g_print ("element %s will be linked to %s\n",
           gst_element_get_name(element),
           gst_element_get_name(other));
  gst_element_link(element, other);
}
int _channels=0;

void il_new_pad (GstElement *decodebin, GstPad *pad, gpointer data)
{
   GstElement* element=0;
   std::cout<<_channels;
        if (_pipeline)
        {
                GstElement *queue, *aconv, *ares, *appsink;

                queue = gst_element_factory_make("queue",  NULL);

                aconv = gst_element_factory_make("audioconvert", NULL);

                appsink = gst_element_factory_make("alsasink", NULL);
               
                gst_bin_add_many (GST_BIN (_pipeline), queue, aconv,
appsink, NULL);

                link_two_elements(queue, aconv);
                link_two_elements(aconv,appsink);
       
                g_object_set(G_OBJECT (appsink), "sync", FALSE, NULL);
               
                element=queue;
                gst_element_sync_state_with_parent(element);

                ++_channels;

        }

        GstCaps *caps;
        GstStructure *str;
        GstPad *audiopad;

        /* only link once */
        audiopad = gst_element_get_static_pad (element, "sink");
        if (GST_PAD_IS_LINKED (audiopad))
        {
                g_object_unref (audiopad);
        }

        /* check media type */
        caps = gst_pad_query_caps (pad,NULL);
        print_caps(caps, "  ");
        str = gst_caps_get_structure (caps, 0);
        if (!g_strrstr (gst_structure_get_name (str), "audio"))
        {
                //std::cerr<<"won't connect!"<<std::endl;
                gst_caps_unref (caps);
                gst_object_unref (audiopad);
        }
        gst_caps_unref (caps);

        /* link'n'play */
        gst_pad_link (pad, audiopad);     
} 


gint main (gint   argc, gchar *argv[])
 {
 	GstElement *source, *decodebin, *audio_convert, *audio_resample,
*deint,*capsfilter;
 	GstBus* bus;

     gst_init (&argc, &argv);



 	if (_loop)
 	{
 		g_main_loop_quit(_loop);
 		g_main_loop_unref(_loop);
 	}
 	_loop = g_main_loop_new (NULL, FALSE);

 	/* Create gstreamer elements */
 	if (_pipeline)
 	{
 		gst_element_set_state (_pipeline, GST_STATE_NULL);
 		gst_object_unref (GST_OBJECT (_pipeline));
 		_pipeline=0;
 	}
 	_pipeline      = gst_pipeline_new("decode_to_app");
 	source        = gst_element_factory_make("filesrc",  "filesrc");
 	decodebin     = gst_element_factory_make("decodebin",  "decode_bin");
 	audio_convert = gst_element_factory_make("audioconvert","audio-convert");
    audio_resample=
gst_element_factory_make("audioresample","audio-resample");
    deint		  =gst_element_factory_make("deinterleave", "deint");
    capsfilter = gst_element_factory_make ("capsfilter", NULL);


 	if (!_pipeline || !source || !decodebin || !audio_convert || !deint)
 	{
 		//std::cerr<<"Elements could not be created. Exiting."<<std::endl;
 	}

 	/* Set up the pipeline */
    // GstCaps *caps = gst_caps_new_simple ("audio/x-raw", "format",
G_TYPE_STRING, "S16LE",  "layout",G_TYPE_STRING, "interleaved", "rate",
G_TYPE_INT, (int)44100, "channels", G_TYPE_INT, (int)6, NULL);
   GstCaps *caps = gst_caps_from_string("audio/x-raw,
layout=(string)interleaved, rate=(int)44100, format=(string)S16LE,
channels=(int)6, channel-mask=(bitmask)0x000000000000003f");
    g_object_set (capsfilter, "caps", caps, NULL);
    print_caps(caps, "  ");
    

    g_object_set(G_OBJECT (source), "location",
"/home/arion/Downloads/test2.mp3", NULL);

 	/* we add a message handler */
 	bus = gst_pipeline_get_bus (GST_PIPELINE (_pipeline));

 	gst_bus_add_watch (bus, bus_call, _loop);
 	gst_object_unref (bus);

 	/* we add all elements into the pipeline */
 	gst_bin_add_many (GST_BIN (_pipeline), source, decodebin,
audio_convert,audio_resample, deint,capsfilter, NULL);

 	/* we link all the elements together */
 	link_two_elements(source, decodebin);
    gst_element_link (decodebin, capsfilter); 
 	link_two_elements(audio_resample, audio_convert);
 	link_two_elements(audio_convert,deint);


 
 	g_signal_connect (decodebin, "pad-added", G_CALLBACK (cb_new_pad),
audio_resample);
 	g_signal_connect (deint, "pad-added", G_CALLBACK (il_new_pad), 0);

 	/* Set the pipeline to "playing" state*/
 	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 listening\n");
 	gst_element_set_state (_pipeline, GST_STATE_NULL);

 	g_print ("Deleting pipeline\n");
 	gst_object_unref (GST_OBJECT (_pipeline));
 } 
My main problem is that i can not convert my 2 channel audio to 6 channel
audio, i just got 2 could you help me please



--
Sent from: http://gstreamer-devel.966125.n4.nabble.com/


More information about the gstreamer-devel mailing list