This commit is contained in:
Akkariin Meiko
2022-03-12 03:16:09 +08:00
Unverified
parent 12b76e0c7a
commit 27c4ec74a1
10075 changed files with 5122287 additions and 1 deletions
@@ -0,0 +1,258 @@
/* GStreamer
*
* addstream.c: sample application to dynamically add streams to a running
* pipeline
*
* Copyright (C) <2007> 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
static GstElement *pipeline;
static GstClock *theclock;
static GMainLoop *loop;
static GstElement *bin1, *bin2, *bin3, *bin4, *bin5;
/* start a bin with the given description */
static GstElement *
create_stream (const gchar * descr)
{
GstElement *bin;
GError *error = NULL;
bin = gst_parse_launch (descr, &error);
if (error) {
g_print ("pipeline could not be constructed: %s\n", error->message);
g_error_free (error);
return NULL;
}
/* add the bin to the pipeline now, this will set the current base_time of the
* pipeline on the new bin. */
gst_bin_add (GST_BIN_CAST (pipeline), bin);
return bin;
}
static void
pause_play_stream (GstElement * bin, gint seconds)
{
gboolean punch_in;
GstStateChangeReturn ret;
GstClockTime now, base_time, running_time;
/* get current running time, we need this value to continue playback of
* non-live pipelines. */
now = gst_clock_get_time (theclock);
base_time = gst_element_get_base_time (bin);
running_time = now - base_time;
/* set the new bin to PAUSED, the parent bin will notice (because of the ASYNC
* message and will perform latency calculations again when going to PLAYING
* later. */
ret = gst_element_set_state (bin, GST_STATE_PAUSED);
switch (ret) {
case GST_STATE_CHANGE_NO_PREROLL:
/* live source, timestamps are running_time of the pipeline clock. */
punch_in = FALSE;
break;
case GST_STATE_CHANGE_SUCCESS:
/* success, no async state changes, same as async, timestamps start
* from 0 */
case GST_STATE_CHANGE_ASYNC:
/* no live source, bin will preroll. We have to punch it in because in
* this situation timestamps start from 0. */
punch_in = TRUE;
break;
case GST_STATE_CHANGE_FAILURE:
/* fall through to return */
default:
return;
}
if (seconds)
g_usleep (seconds * G_USEC_PER_SEC);
if (punch_in) {
/* new bin has to be aligned with previous running_time. We do this by taking
* the current absolute clock time and calculating the base time that would
* give the previous running_time. We set this base_time on the bin before
* setting it to PLAYING. */
now = gst_clock_get_time (theclock);
base_time = now - running_time;
gst_element_set_base_time (bin, base_time);
}
/* now set the pipeline to PLAYING */
gst_element_set_state (bin, GST_STATE_PLAYING);
}
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);
}
static gboolean
perform_step (gpointer pstep)
{
gint step = GPOINTER_TO_INT (pstep);
switch (step) {
case 0:
/* live stream locks on to running_time, pipeline configures latency. */
g_print ("creating bin1\n");
bin1 =
create_stream
("( v4l2src ! videoconvert ! timeoverlay ! queue ! xvimagesink name=v4llive )");
pause_play_stream (bin1, 0);
g_timeout_add_seconds (1, (GSourceFunc) perform_step,
GINT_TO_POINTER (1));
break;
case 1:
/* live stream locks on to running_time, pipeline reconfigures latency
* together with the previously added bin so that they run synchronized. */
g_print ("creating bin2\n");
bin2 = create_stream ("( alsasrc ! queue ! alsasink name=alsalive )");
pause_play_stream (bin2, 0);
g_timeout_add_seconds (1, (GSourceFunc) perform_step,
GINT_TO_POINTER (2));
break;
case 2:
/* non-live stream, need base_time to align with current running live sources. */
g_print ("creating bin3\n");
bin3 = create_stream ("( audiotestsrc ! alsasink name=atnonlive )");
pause_play_stream (bin3, 0);
g_timeout_add_seconds (1, (GSourceFunc) perform_step,
GINT_TO_POINTER (3));
break;
case 3:
g_print ("creating bin4\n");
bin4 =
create_stream
("( videotestsrc ! timeoverlay ! videoconvert ! ximagesink name=vtnonlive )");
pause_play_stream (bin4, 0);
g_timeout_add_seconds (1, (GSourceFunc) perform_step,
GINT_TO_POINTER (4));
break;
case 4:
/* live stream locks on to running_time */
g_print ("creating bin5\n");
bin5 =
create_stream
("( videotestsrc is-live=1 ! timeoverlay ! videoconvert ! ximagesink name=vtlive )");
pause_play_stream (bin5, 0);
g_timeout_add_seconds (1, (GSourceFunc) perform_step,
GINT_TO_POINTER (5));
break;
case 5:
/* pause the fist live stream for 2 seconds */
g_print ("PAUSE bin1 for 2 seconds\n");
pause_play_stream (bin1, 2);
/* pause the non-live stream for 2 seconds */
g_print ("PAUSE bin4 for 2 seconds\n");
pause_play_stream (bin4, 2);
/* pause the pseudo live stream for 2 seconds */
g_print ("PAUSE bin5 for 2 seconds\n");
pause_play_stream (bin5, 2);
g_print ("Waiting 5 seconds\n");
g_timeout_add_seconds (5, (GSourceFunc) perform_step,
GINT_TO_POINTER (6));
break;
case 6:
g_print ("quitting\n");
g_main_loop_quit (loop);
break;
default:
break;
}
return FALSE;
}
int
main (int argc, char *argv[])
{
GstBus *bus;
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, TRUE);
pipeline = gst_pipeline_new ("pipeline");
/* 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, this will distribute a default clock and
* start running. no preroll is needed */
gst_element_set_state (pipeline, GST_STATE_PLAYING);
/* get the clock now. Since we never set the pipeline to PAUSED again, the
* clock will not change, even when we add new clock providers later. */
theclock = gst_element_get_clock (pipeline);
/* start our actions while we are in the mainloop so that we can catch errors
* and other messages. */
g_idle_add ((GSourceFunc) perform_step, GINT_TO_POINTER (0));
/* 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);
gst_object_unref (theclock);
return 0;
}
@@ -0,0 +1,296 @@
/* GStreamer
*
* codec-select.c: sample application to dynamically select a codec
*
* Copyright (C) <2008> 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/*
* This example sets up a pipeline to 'encode' an audiotestsrc into 3 different
* formats. The format can be selected dynamically at runtime.
*
* Each of the encoders require the audio in a specific different format.
*
* This example uses identity as the encoder and enforces the caps on identity
* with a capsfilter.
*
* This is a good example of input and output selector and how these elements
* preserve segment and timing information while switching between streams.
*/
#include <string.h>
#include <gst/gst.h>
/* Create an encoder element.
* We make a bin containing:
*
* audioresample ! <enccaps> ! identity
*
* The sinkpad of audioresample and source pad of identity are ghosted on the
* bin.
*/
static GstElement *
make_encoder (const GstCaps * caps)
{
GstElement *result;
GstElement *audioresample;
GstElement *capsfilter;
GstElement *identity;
GstPad *pad;
/* create result bin */
result = gst_bin_new (NULL);
g_assert (result);
/* create elements */
audioresample = gst_element_factory_make ("audioresample", NULL);
g_assert (audioresample);
capsfilter = gst_element_factory_make ("capsfilter", NULL);
g_assert (capsfilter);
g_object_set (capsfilter, "caps", caps, NULL);
identity = gst_element_factory_make ("identity", NULL);
g_assert (identity);
g_object_set (identity, "silent", TRUE, NULL);
/* add elements to result bin */
gst_bin_add (GST_BIN (result), audioresample);
gst_bin_add (GST_BIN (result), capsfilter);
gst_bin_add (GST_BIN (result), identity);
/* link elements */
gst_element_link_pads (audioresample, "src", capsfilter, "sink");
gst_element_link_pads (capsfilter, "src", identity, "sink");
/* ghost src and sink pads */
pad = gst_element_get_static_pad (audioresample, "sink");
gst_element_add_pad (result, gst_ghost_pad_new ("sink", pad));
gst_object_unref (pad);
pad = gst_element_get_static_pad (identity, "src");
gst_element_add_pad (result, gst_ghost_pad_new ("src", pad));
gst_object_unref (pad);
return result;
}
/*
* We generate:
*
* audiotestsrc ! <audiocaps> ! output-selector ! [enc1 .. enc3] ! input-selector
* select-all = true ! fakesink
*
* <audiocaps> makes sure we only produce one format from the audiotestsrc.
*
* Each encX element consists of:
*
* audioresample ! <enccaps> ! identity !
*
* This way we can simply switch encoders without having to renegotiate.
*/
static GstElement *
make_pipeline (void)
{
GstElement *result;
GstElement *audiotestsrc;
GstElement *audiocaps;
GstElement *outputselect;
GstElement *inputselect;
GstElement *sink;
GstCaps *caps;
GstCaps *capslist[3];
gint i;
/* create result pipeline */
result = gst_pipeline_new (NULL);
g_assert (result);
/* create various elements */
audiotestsrc = gst_element_factory_make ("audiotestsrc", NULL);
g_object_set (audiotestsrc, "num-buffers", 1000, NULL);
g_assert (audiotestsrc);
audiocaps = gst_element_factory_make ("capsfilter", NULL);
g_assert (audiocaps);
caps =
gst_caps_from_string ("audio/x-raw,format=S16LE,rate=48000,channels=1");
g_object_set (audiocaps, "caps", caps, NULL);
gst_caps_unref (caps);
outputselect = gst_element_factory_make ("output-selector", "select");
g_assert (outputselect);
inputselect = gst_element_factory_make ("input-selector", NULL);
g_assert (inputselect);
g_object_set (inputselect, "select-all", TRUE, NULL);
sink = gst_element_factory_make ("fakesink", NULL);
g_object_set (sink, "sync", TRUE, NULL);
g_object_set (sink, "silent", TRUE, NULL);
g_assert (sink);
/* add elements */
gst_bin_add (GST_BIN (result), audiotestsrc);
gst_bin_add (GST_BIN (result), audiocaps);
gst_bin_add (GST_BIN (result), outputselect);
gst_bin_add (GST_BIN (result), inputselect);
gst_bin_add (GST_BIN (result), sink);
/* link elements */
gst_element_link_pads (audiotestsrc, "src", audiocaps, "sink");
gst_element_link_pads (audiocaps, "src", outputselect, "sink");
gst_element_link_pads (inputselect, "src", sink, "sink");
/* make caps */
capslist[0] =
gst_caps_from_string ("audio/x-raw,format=S16LE,rate=48000,channels=1");
capslist[1] =
gst_caps_from_string ("audio/x-raw,format=S16LE,rate=16000,channels=1");
capslist[2] =
gst_caps_from_string ("audio/x-raw,format=S16LE,rate=8000,channels=1");
/* create encoder elements */
for (i = 0; i < 3; i++) {
GstElement *encoder;
GstPad *srcpad, *sinkpad;
encoder = make_encoder (capslist[i]);
g_assert (encoder);
gst_bin_add (GST_BIN (result), encoder);
srcpad = gst_element_request_pad_simple (outputselect, "src_%u");
sinkpad = gst_element_get_static_pad (encoder, "sink");
gst_pad_link (srcpad, sinkpad);
gst_object_unref (srcpad);
gst_object_unref (sinkpad);
srcpad = gst_element_get_static_pad (encoder, "src");
sinkpad = gst_element_request_pad_simple (inputselect, "sink_%u");
gst_pad_link (srcpad, sinkpad);
gst_object_unref (srcpad);
gst_object_unref (sinkpad);
}
return result;
}
static gboolean
do_switch (GstElement * pipeline)
{
gint rand;
GstElement *select;
gchar *name;
GstPad *pad;
rand = g_random_int_range (0, 3);
g_print ("switching to %d\n", rand);
/* find the selector */
select = gst_bin_get_by_name (GST_BIN (pipeline), "select");
/* get the named pad */
name = g_strdup_printf ("src_%u", rand);
pad = gst_element_get_static_pad (select, name);
g_free (name);
/* set the active pad */
g_object_set (select, "active-pad", pad, NULL);
gst_object_unref (select);
return TRUE;
}
static gboolean
my_bus_callback (GstBus * bus, GstMessage * message, gpointer data)
{
GstElement *sender = (GstElement *) GST_MESSAGE_SRC (message);
gchar *name = gst_element_get_name (sender);
GMainLoop *loop = (GMainLoop *) data;
g_print ("Got %s message from %s\n", GST_MESSAGE_TYPE_NAME (message), name);
g_free (name);
switch (GST_MESSAGE_TYPE (message)) {
case GST_MESSAGE_ERROR:{
GError *err;
gchar *debug;
gst_message_parse_error (message, &err, &debug);
g_print ("Error: %s (%s)\n", err->message, debug);
g_error_free (err);
g_free (debug);
g_main_loop_quit (loop);
break;
}
case GST_MESSAGE_EOS:
/* end-of-stream */
g_main_loop_quit (loop);
break;
default:
/* unhandled message */
break;
}
return TRUE;
}
gint
main (gint argc, gchar * argv[])
{
GstElement *pipeline;
GstBus *bus;
GMainLoop *loop;
/* init GStreamer */
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
/* set up */
pipeline = make_pipeline ();
g_signal_connect (pipeline, "deep_notify",
G_CALLBACK (gst_object_default_deep_notify), NULL);
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
gst_bus_add_watch (bus, my_bus_callback, loop);
gst_object_unref (bus);
g_print ("Starting pipeline\n");
gst_element_set_state (pipeline, GST_STATE_PLAYING);
/* add a timeout to cycle between the formats */
g_timeout_add_seconds (1, (GSourceFunc) do_switch, pipeline);
/* now run */
g_main_loop_run (loop);
g_print ("Nulling pipeline\n");
/* also clean up */
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
return 0;
}
@@ -0,0 +1,9 @@
dynamic_examples = ['addstream', 'codec-select', 'sprinkle', 'sprinkle2', 'sprinkle3']
foreach example : dynamic_examples
executable(example, '@0@.c'.format(example),
c_args : gst_plugins_base_args,
include_directories: [configinc, libsinc],
dependencies : [glib_deps, gst_dep],
install: false)
endforeach
@@ -0,0 +1,266 @@
/* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, 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;
gdouble freq;
} SourceInfo;
/* dynamically add the source to the pipeline and link it to a new pad on
* adder */
static SourceInfo *
add_source (gdouble freq)
{
SourceInfo *info;
info = g_new0 (SourceInfo, 1);
info->freq = freq;
/* make source with unique name */
info->element = gst_element_factory_make ("audiotestsrc", NULL);
g_object_set (info->element, "freq", freq, NULL);
/* add to the bin */
gst_bin_add (GST_BIN (pipeline), info->element);
/* get pad from the element */
info->srcpad = gst_element_get_static_pad (info->element, "src");
/* get new pad from adder, adder will now wait for data on this pad */
info->sinkpad = gst_element_request_pad_simple (adder, "sink_%u");
/* link pad to adder */
gst_pad_link (info->srcpad, info->sinkpad);
/* and play the element */
gst_element_set_state (info->element, GST_STATE_PLAYING);
g_print ("added freq %f\n", info->freq);
return info;
}
/* remove the source from the pipeline after removing it from adder */
static void
remove_source (SourceInfo * info)
{
g_print ("remove freq %f\n", info->freq);
/* 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);
}
/* we'll keep the state of the sources in this structure. We keep 3 sources
* alive */
typedef struct
{
guint count;
SourceInfo *infos[3];
} SprinkleState;
static SprinkleState *
create_state (void)
{
SprinkleState *state;
state = g_new0 (SprinkleState, 1);
return state;
}
static void
free_state (SprinkleState * state)
{
SourceInfo *info;
gint i;
for (i = 0; i < 3; i++) {
info = state->infos[i];
if (info)
remove_source (info);
}
g_free (state);
}
static gboolean
do_sprinkle (SprinkleState * state)
{
SourceInfo *info;
gint i;
/* first remove the oldest info */
info = state->infos[2];
if (info)
remove_source (info);
/* move sources */
for (i = 2; i > 0; i--) {
state->infos[i] = state->infos[i - 1];
}
/* add new source, stop adding sources after 10 rounds. */
if (state->count < 10) {
state->infos[0] = add_source ((state->count * 100) + 200);
state->count++;
} else {
state->infos[0] = NULL;
/* if no more sources left, quit */
if (!state->infos[2])
g_main_loop_quit (loop);
}
return TRUE;
}
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, *sink;
GstCaps *caps;
gboolean linked;
SprinkleState *state;
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 ("autoaudiosink", "sink");
caps = gst_caps_new_simple ("audio/x-raw",
"format", G_TYPE_STRING, "S16LE",
"channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 44100, NULL);
g_object_set (filter, "caps", caps, NULL);
gst_caps_unref (caps);
gst_bin_add_many (GST_BIN (pipeline), adder, filter, convert, sink, NULL);
linked = gst_element_link_many (adder, filter, convert, sink, NULL);
g_assert (linked);
/* 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);
/* and add the function that modifies the pipeline every 100ms */
state = create_state ();
g_timeout_add (100, (GSourceFunc) do_sprinkle, state);
/* go to main loop */
g_main_loop_run (loop);
gst_element_set_state (pipeline, GST_STATE_NULL);
free_state (state);
gst_object_unref (bus);
gst_object_unref (pipeline);
return 0;
}
@@ -0,0 +1,285 @@
/* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/*
* Produces a sweeping sprinkle of tones by dynamically adding and removing
* elements to adder.
*
* gcc `pkg-config --cflags --libs gstreamer-1.0` sprinkle2.c -osprinkle2
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
static GstElement *pipeline, *adder;
static GMainLoop *loop;
typedef struct
{
GstElement *src, *fx;
GstPad *src_srcpad;
GstPad *fx_sinkpad, *fx_srcpad;
GstPad *adder_sinkpad;
gdouble freq;
gfloat pos;
} SourceInfo;
/* dynamically add the source to the pipeline and link it to a new pad on
* adder */
static SourceInfo *
add_source (gdouble freq, gfloat pos)
{
SourceInfo *info;
info = g_new0 (SourceInfo, 1);
info->freq = freq;
info->pos = pos;
/* make source with unique name */
info->src = gst_element_factory_make ("audiotestsrc", NULL);
info->fx = gst_element_factory_make ("audiopanorama", NULL);
g_object_set (info->src, "freq", freq, "volume", (gdouble) 0.35, NULL);
g_object_set (info->fx, "panorama", pos, NULL);
/* add to the bin */
gst_bin_add (GST_BIN (pipeline), info->src);
gst_bin_add (GST_BIN (pipeline), info->fx);
/* get pads from the elements */
info->src_srcpad = gst_element_get_static_pad (info->src, "src");
info->fx_srcpad = gst_element_get_static_pad (info->fx, "src");
info->fx_sinkpad = gst_element_get_static_pad (info->fx, "sink");
/* get new pad from adder, adder will now wait for data on this pad */
info->adder_sinkpad = gst_element_request_pad_simple (adder, "sink_%u");
/* link src to fx and fx to adder */
gst_pad_link (info->fx_srcpad, info->adder_sinkpad);
gst_pad_link (info->src_srcpad, info->fx_sinkpad);
/* and play the elements, change the state from sink to source */
gst_element_set_state (info->fx, GST_STATE_PLAYING);
gst_element_set_state (info->src, GST_STATE_PLAYING);
g_print ("added freq %5.0f, pos %3.1f\n", info->freq, info->pos);
return info;
}
/* remove the source from the pipeline after removing it from adder */
static void
remove_source (SourceInfo * info)
{
g_print ("remove freq %5.0f, pos %3.1f\n", info->freq, info->pos);
/* lock the state so that we can put it to NULL without the parent messing
* with our state */
gst_element_set_locked_state (info->src, TRUE);
gst_element_set_locked_state (info->fx, 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. */
gst_element_set_state (info->fx, GST_STATE_NULL);
/* NOTE that the source emits EOS when shutting down but the EOS will not
* reach the adder sinkpad because the effect is in the NULL state. We will
* send an EOS to adder later. */
gst_element_set_state (info->src, GST_STATE_NULL);
/* unlink from adder */
gst_pad_unlink (info->src_srcpad, info->fx_sinkpad);
gst_pad_unlink (info->fx_srcpad, info->adder_sinkpad);
gst_object_unref (info->src_srcpad);
gst_object_unref (info->fx_srcpad);
gst_object_unref (info->fx_sinkpad);
/* remove from the bin */
gst_bin_remove (GST_BIN (pipeline), info->src);
gst_bin_remove (GST_BIN (pipeline), info->fx);
/* send EOS to the sinkpad to make adder EOS when needed */
gst_pad_send_event (info->adder_sinkpad, gst_event_new_eos ());
/* give back the pad */
gst_element_release_request_pad (adder, info->adder_sinkpad);
gst_object_unref (info->adder_sinkpad);
g_free (info);
}
/* we'll keep the state of the sources in this structure. We keep 3 sources
* alive */
typedef struct
{
guint count;
SourceInfo *infos[3];
} SprinkleState;
static SprinkleState *
create_state (void)
{
SprinkleState *state;
state = g_new0 (SprinkleState, 1);
return state;
}
static void
free_state (SprinkleState * state)
{
SourceInfo *info;
gint i;
for (i = 0; i < 3; i++) {
info = state->infos[i];
if (info)
remove_source (info);
}
g_free (state);
}
static gboolean
do_sprinkle (SprinkleState * state)
{
SourceInfo *info;
gint i;
/* first remove the oldest info */
info = state->infos[2];
if (info)
remove_source (info);
/* move sources */
for (i = 2; i > 0; i--) {
state->infos[i] = state->infos[i - 1];
}
/* add new source, stop adding sources after 10 rounds. */
if (state->count < 20) {
state->infos[0] = add_source (
(gdouble) ((state->count * 100) + 200),
((gfloat) (state->count % 5) / 2.0 - 1.0));
state->count++;
} else {
state->infos[0] = NULL;
}
return TRUE;
}
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, *sink;
GstCaps *caps;
gboolean res;
SprinkleState *state;
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 ("autoaudiosink", "sink");
caps = gst_caps_new_simple ("audio/x-raw",
"format", G_TYPE_STRING, "S16LE",
"channels", G_TYPE_INT, 2, "rate", G_TYPE_INT, 44100, NULL);
g_object_set (filter, "caps", caps, NULL);
gst_caps_unref (caps);
gst_bin_add_many (GST_BIN (pipeline), adder, filter, convert, sink, NULL);
res = gst_element_link_many (adder, filter, convert, sink, NULL);
g_assert (res);
/* 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);
/* and add the function that modifies the pipeline every 100ms */
state = create_state ();
g_timeout_add (100, (GSourceFunc) do_sprinkle, state);
/* go to main loop */
g_main_loop_run (loop);
gst_element_set_state (pipeline, GST_STATE_NULL);
free_state (state);
gst_object_unref (bus);
gst_object_unref (pipeline);
return 0;
}
@@ -0,0 +1,298 @@
/* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/*
* Produces a sweeping sprinkle of tones by dynamically adding and removing
* elements to adder.
*
* gcc `pkg-config --cflags --libs gstreamer-1.0` sprinkle3.c -osprinkle3
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
static GstElement *pipeline, *adder;
static GMainLoop *loop;
typedef struct
{
GstElement *bin, *src, *fx;
GstPad *src_srcpad;
GstPad *fx_sinkpad, *fx_srcpad;
GstPad *adder_sinkpad;
GstPad *bin_srcpad;
gdouble freq;
gfloat pos;
} SourceInfo;
/* dynamically add the source to the pipeline and link it to a new pad on
* adder */
static SourceInfo *
add_source (gdouble freq, gfloat pos)
{
SourceInfo *info;
info = g_new0 (SourceInfo, 1);
info->freq = freq;
info->pos = pos;
/* make source with unique name */
info->bin = gst_element_factory_make ("bin", NULL);
info->src = gst_element_factory_make ("audiotestsrc", NULL);
info->fx = gst_element_factory_make ("audiopanorama", NULL);
g_object_set (info->src, "freq", freq, "volume", (gdouble) 0.35, NULL);
g_object_set (info->fx, "panorama", pos, NULL);
/* add to the bin */
gst_bin_add (GST_BIN (info->bin), info->src);
gst_bin_add (GST_BIN (info->bin), info->fx);
/* get pads from the elements */
info->src_srcpad = gst_element_get_static_pad (info->src, "src");
info->fx_srcpad = gst_element_get_static_pad (info->fx, "src");
info->fx_sinkpad = gst_element_get_static_pad (info->fx, "sink");
/* create and add a pad for the bin */
info->bin_srcpad = gst_ghost_pad_new ("src", info->fx_srcpad);
gst_element_add_pad (info->bin, info->bin_srcpad);
/* get new pad from adder, adder will now wait for data on this pad */
info->adder_sinkpad = gst_element_request_pad_simple (adder, "sink_%u");
/* link inside the bin */
gst_pad_link (info->src_srcpad, info->fx_sinkpad);
/* add bin to pipeline */
gst_bin_add (GST_BIN (pipeline), info->bin);
/* link bin to adder */
gst_pad_link (info->bin_srcpad, info->adder_sinkpad);
/* and play the elements */
gst_element_set_state (info->bin, GST_STATE_PLAYING);
g_print ("added freq %5.0f, pos %3.1f\n", info->freq, info->pos);
return info;
}
/* remove the source from the pipeline after removing it from adder */
static void
remove_source (SourceInfo * info)
{
g_print ("remove freq %5.0f, pos %3.1f\n", info->freq, info->pos);
/* lock the state so that we can put it to NULL without the parent messing
* with our state */
gst_element_set_locked_state (info->bin, 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 the source inside the bin will emit EOS but it will not reach
* adder because the element after the source is shut down first. We will send
* EOS later */
gst_element_set_state (info->bin, GST_STATE_NULL);
/* unlink bin from adder */
gst_pad_unlink (info->bin_srcpad, info->adder_sinkpad);
/* release pads */
gst_object_unref (info->src_srcpad);
gst_object_unref (info->fx_srcpad);
gst_object_unref (info->fx_sinkpad);
/* remove from the bin */
gst_bin_remove (GST_BIN (pipeline), info->bin);
/* send EOS to the sinkpad to make adder EOS when needed */
gst_pad_send_event (info->adder_sinkpad, gst_event_new_eos ());
/* give back the pad */
gst_element_release_request_pad (adder, info->adder_sinkpad);
gst_object_unref (info->adder_sinkpad);
g_free (info);
}
/* we'll keep the state of the sources in this structure. We keep 3 sources
* alive */
typedef struct
{
guint count;
SourceInfo *infos[3];
} SprinkleState;
static SprinkleState *
create_state (void)
{
SprinkleState *state;
state = g_new0 (SprinkleState, 1);
return state;
}
static void
free_state (SprinkleState * state)
{
SourceInfo *info;
gint i;
for (i = 0; i < 3; i++) {
info = state->infos[i];
if (info)
remove_source (info);
}
g_free (state);
}
static gboolean
do_sprinkle (SprinkleState * state)
{
SourceInfo *info;
gint i;
/* first remove the oldest info */
info = state->infos[2];
if (info)
remove_source (info);
/* move sources */
for (i = 2; i > 0; i--) {
state->infos[i] = state->infos[i - 1];
}
/* add new source, stop adding sources after 10 rounds. */
if (state->count < 20) {
state->infos[0] = add_source (
(gdouble) ((state->count * 100) + 200),
((gfloat) (state->count % 5) / 2.0 - 1.0));
state->count++;
} else {
state->infos[0] = NULL;
}
GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
/*GST_DEBUG_GRAPH_SHOW_ALL, */
GST_DEBUG_GRAPH_SHOW_CAPS_DETAILS | GST_DEBUG_GRAPH_SHOW_STATES,
"sprinkle3");
return TRUE;
}
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, *sink;
GstCaps *caps;
gboolean res;
SprinkleState *state;
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 ("autoaudiosink", "sink");
caps = gst_caps_new_simple ("audio/x-raw",
"format", G_TYPE_STRING, "S16LE",
"channels", G_TYPE_INT, 2, "rate", G_TYPE_INT, 44100, NULL);
g_object_set (filter, "caps", caps, NULL);
gst_caps_unref (caps);
gst_bin_add_many (GST_BIN (pipeline), adder, filter, convert, sink, NULL);
res = gst_element_link_many (adder, filter, convert, sink, NULL);
g_assert (res);
/* 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);
/* and add the function that modifies the pipeline every 100ms */
state = create_state ();
g_timeout_add (100, (GSourceFunc) do_sprinkle, state);
/* go to main loop */
g_main_loop_run (loop);
gst_element_set_state (pipeline, GST_STATE_NULL);
free_state (state);
gst_object_unref (bus);
gst_object_unref (pipeline);
return 0;
}