Blame tests/examples/dynamic/sprinkle3.c

Packit 971217
/* GStreamer
Packit 971217
 *
Packit 971217
 * sprinkle.c: sample application to dynamically mix tones with adder
Packit 971217
 *
Packit 971217
 * Copyright (C) <2009> Wim Taymans <wim dot taymans at gmail dot com>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
/*
Packit 971217
 * Produces a sweeping sprinkle of tones by dynamically adding and removing
Packit 971217
 * elements to adder.
Packit 971217
 *
Packit 971217
 * gcc `pkg-config --cflags --libs gstreamer-0.10` sprinkle3.c -osprinkle3
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include <gst/gst.h>
Packit 971217
Packit 971217
static GstElement *pipeline, *adder;
Packit 971217
static GMainLoop *loop;
Packit 971217
Packit 971217
typedef struct
Packit 971217
{
Packit 971217
  GstElement *bin, *src, *fx;
Packit 971217
  GstPad *src_srcpad;
Packit 971217
  GstPad *fx_sinkpad, *fx_srcpad;
Packit 971217
  GstPad *adder_sinkpad;
Packit 971217
  GstPad *bin_srcpad;
Packit 971217
  gdouble freq;
Packit 971217
  gfloat pos;
Packit 971217
} SourceInfo;
Packit 971217
Packit 971217
/* dynamically add the source to the pipeline and link it to a new pad on
Packit 971217
 * adder */
Packit 971217
static SourceInfo *
Packit 971217
add_source (gdouble freq, gfloat pos)
Packit 971217
{
Packit 971217
  SourceInfo *info;
Packit 971217
Packit 971217
  info = g_new0 (SourceInfo, 1);
Packit 971217
  info->freq = freq;
Packit 971217
  info->pos = pos;
Packit 971217
Packit 971217
  /* make source with unique name */
Packit 971217
  info->bin = gst_element_factory_make ("bin", NULL);
Packit 971217
  info->src = gst_element_factory_make ("audiotestsrc", NULL);
Packit 971217
  info->fx = gst_element_factory_make ("audiopanorama", NULL);
Packit 971217
Packit 971217
  g_object_set (info->src, "freq", freq, "volume", (gdouble) 0.35, NULL);
Packit 971217
  g_object_set (info->fx, "panorama", pos, NULL);
Packit 971217
Packit 971217
  /* add to the bin */
Packit 971217
  gst_bin_add (GST_BIN (info->bin), info->src);
Packit 971217
  gst_bin_add (GST_BIN (info->bin), info->fx);
Packit 971217
Packit 971217
  /* get pads from the elements */
Packit 971217
  info->src_srcpad = gst_element_get_static_pad (info->src, "src");
Packit 971217
  info->fx_srcpad = gst_element_get_static_pad (info->fx, "src");
Packit 971217
  info->fx_sinkpad = gst_element_get_static_pad (info->fx, "sink");
Packit 971217
Packit 971217
  /* create and add a pad for the bin */
Packit 971217
  info->bin_srcpad = gst_ghost_pad_new ("src", info->fx_srcpad);
Packit 971217
  gst_element_add_pad (info->bin, info->bin_srcpad);
Packit 971217
Packit 971217
  /* get new pad from adder, adder will now wait for data on this pad */
Packit 971217
  info->adder_sinkpad = gst_element_get_request_pad (adder, "sink_%u");
Packit 971217
Packit 971217
  /* link inside the bin */
Packit 971217
  gst_pad_link (info->src_srcpad, info->fx_sinkpad);
Packit 971217
Packit 971217
  /* add bin to pipeline */
Packit 971217
  gst_bin_add (GST_BIN (pipeline), info->bin);
Packit 971217
Packit 971217
  /* link bin to adder */
Packit 971217
  gst_pad_link (info->bin_srcpad, info->adder_sinkpad);
Packit 971217
Packit 971217
  /* and play the elements */
Packit 971217
  gst_element_set_state (info->bin, GST_STATE_PLAYING);
Packit 971217
Packit 971217
  g_print ("added  freq %5.0f, pos %3.1f\n", info->freq, info->pos);
Packit 971217
Packit 971217
  return info;
Packit 971217
}
Packit 971217
Packit 971217
/* remove the source from the pipeline after removing it from adder */
Packit 971217
static void
Packit 971217
remove_source (SourceInfo * info)
Packit 971217
{
Packit 971217
  g_print ("remove freq %5.0f, pos %3.1f\n", info->freq, info->pos);
Packit 971217
Packit 971217
  /* lock the state so that we can put it to NULL without the parent messing
Packit 971217
   * with our state */
Packit 971217
  gst_element_set_locked_state (info->bin, TRUE);
Packit 971217
Packit 971217
  /* first stop the source. Remember that this might block when in the PAUSED
Packit 971217
   * state. Alternatively one could send EOS to the source, install an event
Packit 971217
   * probe and schedule a state change/unlink/release from the mainthread. */
Packit 971217
  /* NOTE that the source inside the bin will emit EOS but it will not reach
Packit 971217
   * adder because the element after the source is shut down first. We will send
Packit 971217
   * EOS later */
Packit 971217
  gst_element_set_state (info->bin, GST_STATE_NULL);
Packit 971217
Packit 971217
  /* unlink bin from adder */
Packit 971217
  gst_pad_unlink (info->bin_srcpad, info->adder_sinkpad);
Packit 971217
Packit 971217
  /* release pads */
Packit 971217
  gst_object_unref (info->src_srcpad);
Packit 971217
  gst_object_unref (info->fx_srcpad);
Packit 971217
  gst_object_unref (info->fx_sinkpad);
Packit 971217
Packit 971217
  /* remove from the bin */
Packit 971217
  gst_bin_remove (GST_BIN (pipeline), info->bin);
Packit 971217
Packit 971217
  /* send EOS to the sinkpad to make adder EOS when needed */
Packit 971217
  gst_pad_send_event (info->adder_sinkpad, gst_event_new_eos ());
Packit 971217
Packit 971217
  /* give back the pad */
Packit 971217
  gst_element_release_request_pad (adder, info->adder_sinkpad);
Packit 971217
  gst_object_unref (info->adder_sinkpad);
Packit 971217
Packit 971217
  g_free (info);
Packit 971217
}
Packit 971217
Packit 971217
/* we'll keep the state of the sources in this structure. We keep 3 sources
Packit 971217
 * alive */
Packit 971217
typedef struct
Packit 971217
{
Packit 971217
  guint count;
Packit 971217
  SourceInfo *infos[3];
Packit 971217
} SprinkleState;
Packit 971217
Packit 971217
static SprinkleState *
Packit 971217
create_state (void)
Packit 971217
{
Packit 971217
  SprinkleState *state;
Packit 971217
Packit 971217
  state = g_new0 (SprinkleState, 1);
Packit 971217
Packit 971217
  return state;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
free_state (SprinkleState * state)
Packit 971217
{
Packit 971217
  SourceInfo *info;
Packit 971217
  gint i;
Packit 971217
Packit 971217
  for (i = 0; i < 3; i++) {
Packit 971217
    info = state->infos[i];
Packit 971217
    if (info)
Packit 971217
      remove_source (info);
Packit 971217
  }
Packit 971217
Packit 971217
  g_free (state);
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
do_sprinkle (SprinkleState * state)
Packit 971217
{
Packit 971217
  SourceInfo *info;
Packit 971217
  gint i;
Packit 971217
Packit 971217
  /* first remove the oldest info */
Packit 971217
  info = state->infos[2];
Packit 971217
Packit 971217
  if (info)
Packit 971217
    remove_source (info);
Packit 971217
Packit 971217
  /* move sources */
Packit 971217
  for (i = 2; i > 0; i--) {
Packit 971217
    state->infos[i] = state->infos[i - 1];
Packit 971217
  }
Packit 971217
Packit 971217
  /* add new source, stop adding sources after 10 rounds. */
Packit 971217
  if (state->count < 20) {
Packit 971217
    state->infos[0] = add_source (
Packit 971217
        (gdouble) ((state->count * 100) + 200),
Packit 971217
        ((gfloat) (state->count % 5) / 2.0 - 1.0));
Packit 971217
    state->count++;
Packit 971217
  } else {
Packit 971217
    state->infos[0] = NULL;
Packit 971217
  }
Packit 971217
Packit 971217
  GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
Packit 971217
      /*GST_DEBUG_GRAPH_SHOW_ALL, */
Packit 971217
      GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS | GST_DEBUG_GRAPH_SHOW_STATES,
Packit 971217
      "sprinkle3");
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
Packit 971217
{
Packit 971217
  const GstStructure *s;
Packit 971217
Packit 971217
  s = gst_message_get_structure (message);
Packit 971217
  g_print ("message from \"%s\" (%s): ",
Packit 971217
      GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
Packit 971217
      gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
Packit 971217
  if (s) {
Packit 971217
    gchar *sstr;
Packit 971217
Packit 971217
    sstr = gst_structure_to_string (s);
Packit 971217
    g_print ("%s\n", sstr);
Packit 971217
    g_free (sstr);
Packit 971217
  } else {
Packit 971217
    g_print ("no message details\n");
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
eos_message_received (GstBus * bus, GstMessage * message,
Packit 971217
    GstPipeline * pipeline)
Packit 971217
{
Packit 971217
  message_received (bus, message, pipeline);
Packit 971217
  g_main_loop_quit (loop);
Packit 971217
}
Packit 971217
Packit 971217
int
Packit 971217
main (int argc, char *argv[])
Packit 971217
{
Packit 971217
  GstBus *bus;
Packit 971217
  GstElement *filter, *convert, *sink;
Packit 971217
  GstCaps *caps;
Packit 971217
  gboolean res;
Packit 971217
  SprinkleState *state;
Packit 971217
Packit 971217
  gst_init (&argc, &argv);
Packit 971217
Packit 971217
  loop = g_main_loop_new (NULL, TRUE);
Packit 971217
Packit 971217
  pipeline = gst_pipeline_new ("pipeline");
Packit 971217
Packit 971217
  /* add the fixed part to the pipeline. Remember that we need a capsfilter
Packit 971217
   * after adder so that multiple sources are not racing to negotiate
Packit 971217
   * a format */
Packit 971217
  adder = gst_element_factory_make ("adder", "adder");
Packit 971217
  filter = gst_element_factory_make ("capsfilter", "filter");
Packit 971217
  convert = gst_element_factory_make ("audioconvert", "convert");
Packit 971217
  sink = gst_element_factory_make ("autoaudiosink", "sink");
Packit 971217
Packit 971217
  caps = gst_caps_new_simple ("audio/x-raw",
Packit 971217
      "format", G_TYPE_STRING, "S16LE",
Packit 971217
      "channels", G_TYPE_INT, 2, "rate", G_TYPE_INT, 44100, NULL);
Packit 971217
  g_object_set (filter, "caps", caps, NULL);
Packit 971217
  gst_caps_unref (caps);
Packit 971217
Packit 971217
  gst_bin_add_many (GST_BIN (pipeline), adder, filter, convert, sink, NULL);
Packit 971217
Packit 971217
  res = gst_element_link_many (adder, filter, convert, sink, NULL);
Packit 971217
  g_assert (res);
Packit 971217
Packit 971217
  /* setup message handling */
Packit 971217
  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
Packit 971217
  gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
Packit 971217
  g_signal_connect (bus, "message::error", (GCallback) message_received,
Packit 971217
      pipeline);
Packit 971217
  g_signal_connect (bus, "message::warning", (GCallback) message_received,
Packit 971217
      pipeline);
Packit 971217
  g_signal_connect (bus, "message::eos", (GCallback) eos_message_received,
Packit 971217
      pipeline);
Packit 971217
Packit 971217
  /* we set the pipeline to PLAYING, the pipeline will not yet preroll because
Packit 971217
   * there is no source providing data for it yet */
Packit 971217
  gst_element_set_state (pipeline, GST_STATE_PLAYING);
Packit 971217
Packit 971217
  /* and add the function that modifies the pipeline every 100ms */
Packit 971217
  state = create_state ();
Packit 971217
  g_timeout_add (100, (GSourceFunc) do_sprinkle, state);
Packit 971217
Packit 971217
  /* go to main loop */
Packit 971217
  g_main_loop_run (loop);
Packit 971217
Packit 971217
  gst_element_set_state (pipeline, GST_STATE_NULL);
Packit 971217
Packit 971217
  free_state (state);
Packit 971217
  gst_object_unref (bus);
Packit 971217
  gst_object_unref (pipeline);
Packit 971217
Packit 971217
  return 0;
Packit 971217
}