Blame tests/examples/dynamic/sprinkle.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
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 *element;
Packit 971217
  GstPad *srcpad;
Packit 971217
  GstPad *sinkpad;
Packit 971217
  gdouble freq;
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)
Packit 971217
{
Packit 971217
  SourceInfo *info;
Packit 971217
Packit 971217
  info = g_new0 (SourceInfo, 1);
Packit 971217
  info->freq = freq;
Packit 971217
Packit 971217
  /* make source with unique name */
Packit 971217
  info->element = gst_element_factory_make ("audiotestsrc", NULL);
Packit 971217
Packit 971217
  g_object_set (info->element, "freq", freq, NULL);
Packit 971217
Packit 971217
  /* add to the bin */
Packit 971217
  gst_bin_add (GST_BIN (pipeline), info->element);
Packit 971217
Packit 971217
  /* get pad from the element */
Packit 971217
  info->srcpad = gst_element_get_static_pad (info->element, "src");
Packit 971217
Packit 971217
  /* get new pad from adder, adder will now wait for data on this pad */
Packit 971217
  info->sinkpad = gst_element_get_request_pad (adder, "sink_%u");
Packit 971217
Packit 971217
  /* link pad to adder */
Packit 971217
  gst_pad_link (info->srcpad, info->sinkpad);
Packit 971217
Packit 971217
  /* and play the element */
Packit 971217
  gst_element_set_state (info->element, GST_STATE_PLAYING);
Packit 971217
Packit 971217
  g_print ("added freq %f\n", info->freq);
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 %f\n", info->freq);
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->element, 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 changing the state of a source makes it emit an EOS, which can
Packit 971217
   * make adder go EOS. */
Packit 971217
  gst_element_set_state (info->element, GST_STATE_NULL);
Packit 971217
Packit 971217
  /* unlink from adder */
Packit 971217
  gst_pad_unlink (info->srcpad, info->sinkpad);
Packit 971217
  gst_object_unref (info->srcpad);
Packit 971217
Packit 971217
  /* remove from the bin */
Packit 971217
  gst_bin_remove (GST_BIN (pipeline), info->element);
Packit 971217
Packit 971217
  /* give back the pad */
Packit 971217
  gst_element_release_request_pad (adder, info->sinkpad);
Packit 971217
  gst_object_unref (info->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 < 10) {
Packit 971217
    state->infos[0] = add_source ((state->count * 100) + 200);
Packit 971217
    state->count++;
Packit 971217
  } else {
Packit 971217
    state->infos[0] = NULL;
Packit 971217
Packit 971217
    /* if no more sources left, quit */
Packit 971217
    if (!state->infos[2])
Packit 971217
      g_main_loop_quit (loop);
Packit 971217
  }
Packit 971217
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 linked;
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, 1, "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
  linked = gst_element_link_many (adder, filter, convert, sink, NULL);
Packit 971217
  g_assert (linked);
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
}