Blame tests/examples/dynamic/sprinkle2.c

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