All pastes #1885182 Raw Edit

Stuff

public text v1 · immutable
#1885182 ·published 2010-06-17 17:14 UTC
rendered paste body
/* GStreamer
 *
 * sprinkle.c: sample application to dynamically mix tones with adder
 *
 * Copyright (C) <2009> Wim Taymans <wim dot taymans at gmail dot com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * Produces a sweeping sprinkle of tones by dynamically adding and removing
 * elements to adder.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>

static GstElement *pipeline, *adder;
static GMainLoop *loop;

typedef struct
{
  GstElement *element;
  GstPad *srcpad;
  GstPad *sinkpad;
} SourceInfo;

/* dynamically add the source to the pipeline and link it to a new pad on
 * adder */
static SourceInfo *
add_source (char *file_location)
{
  GstElement *audio_decoder, *convert1;
  SourceInfo *info;

  info = g_new0 (SourceInfo, 1);

  /* make source with unique name */
  info->element = gst_element_factory_make ("filesrc", "filesrc1");
  g_object_set (info->element, "location", file_location, NULL);
  audio_decoder = gst_element_factory_make ("mad", "mad2"); 
  convert1 = gst_element_factory_make ("audioconvert", "convert1");

  /* add to the bin */
  gst_bin_add_many (GST_BIN (pipeline), info->element, audio_decoder, convert1, NULL);

  gst_element_link_many (info->element, audio_decoder, convert1, NULL);

  /* get pad from the element */
  info->srcpad = gst_element_get_static_pad (convert1, "src");

  /* get new pad from adder, adder will now wait for data on this pad */
  info->sinkpad = gst_element_get_request_pad (adder, "sink%d");

  /* link pad to adder */
  gst_pad_link (info->srcpad, info->sinkpad);

  /* and play the element */
  gst_element_set_state (info->element, GST_STATE_PLAYING);

  return info;
}

/* remove the source from the pipeline after removing it from adder */
static void
remove_source (SourceInfo * info)
{

  /* lock the state so that we can put it to NULL without the parent messing
   * with our state */
  gst_element_set_locked_state (info->element, TRUE);

  /* first stop the source. Remember that this might block when in the PAUSED
   * state. Alternatively one could send EOS to the source, install an event
   * probe and schedule a state change/unlink/release from the mainthread.
   * Note that changing the state of a source makes it emit an EOS, which can
   * make adder go EOS. */
  gst_element_set_state (info->element, GST_STATE_NULL);

  /* unlink from adder */
  gst_pad_unlink (info->srcpad, info->sinkpad);
  gst_object_unref (info->srcpad);

  /* remove from the bin */
  gst_bin_remove (GST_BIN (pipeline), info->element);

  /* give back the pad */
  gst_element_release_request_pad (adder, info->sinkpad);
  gst_object_unref (info->sinkpad);

  g_free (info);
}

static void
message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
{
  const GstStructure *s;

  s = gst_message_get_structure (message);
  g_print ("message from \"%s\" (%s): ",
      GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
      gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
  if (s) {
    gchar *sstr;

    sstr = gst_structure_to_string (s);
    g_print ("%s\n", sstr);
    g_free (sstr);
  } else {
    g_print ("no message details\n");
  }
}

static void
eos_message_received (GstBus * bus, GstMessage * message,
    GstPipeline * pipeline)
{
  message_received (bus, message, pipeline);
  g_main_loop_quit (loop);
}

int
main (int argc, char *argv[])
{
  GstBus *bus;
  GstElement *filter, *convert, *convert1,  *sink, *src1, *audio_decoder;
  GstCaps *caps;
  GstPad  *srcpad;
  GstPad  *sinkpad;
  gboolean res;

  gst_init (&argc, &argv);

  loop = g_main_loop_new (NULL, TRUE);

  pipeline = gst_pipeline_new ("pipeline");

  /* add the fixed part to the pipeline. Remember that we need a capsfilter
   * after adder so that multiple sources are not racing to negotiate
   * a format */
  adder = gst_element_factory_make ("adder", "adder");
  filter = gst_element_factory_make ("capsfilter", "filter");
  convert = gst_element_factory_make ("audioconvert", "convert");
  sink = gst_element_factory_make ("pulsesink", "sink");
  g_object_set (G_OBJECT(sink), "sync", FALSE, NULL);
 

  caps = gst_caps_new_simple ("audio/x-raw-int",
      "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
      "channels", G_TYPE_INT, 1,
      "width", G_TYPE_INT, 16,
      "depth", G_TYPE_INT, 16,
      "rate", G_TYPE_INT, 44100, "signed", G_TYPE_BOOLEAN, TRUE, NULL);
  g_object_set (filter, "caps", caps, NULL);
  gst_caps_unref (caps);

  src1 = gst_element_factory_make ("filesrc", "filesrc0");
  g_object_set (src1, "location", argv[1], NULL);

  audio_decoder = gst_element_factory_make ("mad", "mad"); 
  convert1 = gst_element_factory_make ("audioconvert", "convert2");

  gst_bin_add_many (GST_BIN (pipeline), adder, filter, convert, sink, src1, audio_decoder, convert1, NULL);

  res = gst_element_link (src1, audio_decoder); 
  g_assert (res);
  res = gst_element_link (audio_decoder, convert1); 
  g_assert (res);
/*
  res = gst_element_link (src1, convert1); 
  g_assert (res);
*/  
  res = gst_element_link_many (adder, filter, convert, sink, NULL);
  g_assert (res);

  /* get pad from the element */
  srcpad = gst_element_get_static_pad (convert1, "src");
  /* get new pad from adder, adder will now wait for data on this pad */
  sinkpad = gst_element_get_request_pad (adder, "sink%d");
  /* link pad to adder */
  gst_pad_link (srcpad, sinkpad);

  /* setup message handling */
  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
  g_signal_connect (bus, "message::error", (GCallback) message_received,
      pipeline);
  g_signal_connect (bus, "message::warning", (GCallback) message_received,
      pipeline);
  g_signal_connect (bus, "message::eos", (GCallback) eos_message_received,
      pipeline);

  /* we set the pipeline to PLAYING, the pipeline will not yet preroll because
   * there is no source providing data for it yet */
  gst_element_set_state (pipeline, GST_STATE_PLAYING);

  sleep (10);
  add_source (argv[2]);
  /* go to main loop */
  g_main_loop_run (loop);

  gst_element_set_state (pipeline, GST_STATE_NULL);

  gst_object_unref (bus);
  gst_object_unref (pipeline);

  return 0;
}