2012-05-10 15:58:21 +00:00
|
|
|
#include <gst/gst.h>
|
2013-10-31 09:01:26 +00:00
|
|
|
#include <gst/audio/audio.h>
|
2012-05-10 15:58:21 +00:00
|
|
|
#include <string.h>
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2022-12-13 17:42:11 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <TargetConditionals.h>
|
|
|
|
#endif
|
|
|
|
|
2019-02-07 19:32:58 +00:00
|
|
|
#define CHUNK_SIZE 1024 /* Amount of bytes we are sending in each buffer */
|
|
|
|
#define SAMPLE_RATE 44100 /* Samples per second we are sending */
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Structure to contain all our information, so we can pass it to callbacks */
|
2019-02-07 19:32:58 +00:00
|
|
|
typedef struct _CustomData
|
|
|
|
{
|
|
|
|
GstElement *pipeline, *app_source, *tee, *audio_queue, *audio_convert1,
|
|
|
|
*audio_resample, *audio_sink;
|
|
|
|
GstElement *video_queue, *audio_convert2, *visual, *video_convert,
|
|
|
|
*video_sink;
|
2012-05-10 15:58:21 +00:00
|
|
|
GstElement *app_queue, *app_sink;
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2019-02-07 19:32:58 +00:00
|
|
|
guint64 num_samples; /* Number of samples generated so far (for timestamp generation) */
|
|
|
|
gfloat a, b, c, d; /* For waveform generation */
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2019-02-07 19:32:58 +00:00
|
|
|
guint sourceid; /* To control the GSource */
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2019-02-07 19:32:58 +00:00
|
|
|
GMainLoop *main_loop; /* GLib's Main Loop */
|
2012-05-10 15:58:21 +00:00
|
|
|
} CustomData;
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* This method is called by the idle GSource in the mainloop, to feed CHUNK_SIZE bytes into appsrc.
|
2012-07-04 14:31:40 +00:00
|
|
|
* The idle handler is added to the mainloop when appsrc requests us to start sending data (need-data signal)
|
2012-06-19 12:01:48 +00:00
|
|
|
* and is removed when appsrc has enough data (enough-data signal).
|
2012-05-10 15:58:21 +00:00
|
|
|
*/
|
2019-02-07 19:32:58 +00:00
|
|
|
static gboolean
|
|
|
|
push_data (CustomData * data)
|
|
|
|
{
|
2012-05-10 15:58:21 +00:00
|
|
|
GstBuffer *buffer;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
int i;
|
2013-10-31 09:01:26 +00:00
|
|
|
GstMapInfo map;
|
2012-05-11 14:55:37 +00:00
|
|
|
gint16 *raw;
|
2019-02-07 19:32:58 +00:00
|
|
|
gint num_samples = CHUNK_SIZE / 2; /* Because each sample is 16 bits */
|
2012-05-10 15:58:21 +00:00
|
|
|
gfloat freq;
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Create a new empty buffer */
|
|
|
|
buffer = gst_buffer_new_and_alloc (CHUNK_SIZE);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Set its timestamp and duration */
|
2019-02-07 19:32:58 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (buffer) =
|
|
|
|
gst_util_uint64_scale (data->num_samples, GST_SECOND, SAMPLE_RATE);
|
|
|
|
GST_BUFFER_DURATION (buffer) =
|
|
|
|
gst_util_uint64_scale (num_samples, GST_SECOND, SAMPLE_RATE);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Generate some psychodelic waveforms */
|
2013-10-31 09:01:26 +00:00
|
|
|
gst_buffer_map (buffer, &map, GST_MAP_WRITE);
|
2019-02-07 19:32:58 +00:00
|
|
|
raw = (gint16 *) map.data;
|
2012-05-10 15:58:21 +00:00
|
|
|
data->c += data->d;
|
|
|
|
data->d -= data->c / 1000;
|
|
|
|
freq = 1100 + 1000 * data->d;
|
|
|
|
for (i = 0; i < num_samples; i++) {
|
|
|
|
data->a += data->b;
|
|
|
|
data->b -= data->a / freq;
|
2019-02-07 19:32:58 +00:00
|
|
|
raw[i] = (gint16) (500 * data->a);
|
2012-05-10 15:58:21 +00:00
|
|
|
}
|
2013-10-31 09:01:26 +00:00
|
|
|
gst_buffer_unmap (buffer, &map);
|
2012-05-11 14:55:37 +00:00
|
|
|
data->num_samples += num_samples;
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Push the buffer into the appsrc */
|
|
|
|
g_signal_emit_by_name (data->app_source, "push-buffer", buffer, &ret);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Free the buffer now that we are done with it */
|
|
|
|
gst_buffer_unref (buffer);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
/* We got some error, stop sending data */
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* This signal callback triggers when appsrc needs data. Here, we add an idle handler
|
|
|
|
* to the mainloop to start pushing data into the appsrc */
|
2019-02-07 19:32:58 +00:00
|
|
|
static void
|
|
|
|
start_feed (GstElement * source, guint size, CustomData * data)
|
|
|
|
{
|
2012-05-10 15:58:21 +00:00
|
|
|
if (data->sourceid == 0) {
|
|
|
|
g_print ("Start feeding\n");
|
|
|
|
data->sourceid = g_idle_add ((GSourceFunc) push_data, data);
|
|
|
|
}
|
|
|
|
}
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* This callback triggers when appsrc has enough data and we can stop sending.
|
|
|
|
* We remove the idle handler from the mainloop */
|
2019-02-07 19:32:58 +00:00
|
|
|
static void
|
|
|
|
stop_feed (GstElement * source, CustomData * data)
|
|
|
|
{
|
2012-05-10 15:58:21 +00:00
|
|
|
if (data->sourceid != 0) {
|
|
|
|
g_print ("Stop feeding\n");
|
|
|
|
g_source_remove (data->sourceid);
|
|
|
|
data->sourceid = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* The appsink has received a buffer */
|
2019-02-07 19:32:58 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
new_sample (GstElement * sink, CustomData * data)
|
|
|
|
{
|
2013-10-31 09:01:26 +00:00
|
|
|
GstSample *sample;
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Retrieve the buffer */
|
2013-10-31 09:01:26 +00:00
|
|
|
g_signal_emit_by_name (sink, "pull-sample", &sample);
|
|
|
|
if (sample) {
|
2012-05-10 15:58:21 +00:00
|
|
|
/* The only thing we do in this example is print a * to indicate a received buffer */
|
|
|
|
g_print ("*");
|
2013-10-31 09:01:26 +00:00
|
|
|
gst_sample_unref (sample);
|
2018-06-04 19:55:08 +00:00
|
|
|
return GST_FLOW_OK;
|
2012-05-10 15:58:21 +00:00
|
|
|
}
|
2017-06-05 04:32:36 +00:00
|
|
|
|
2018-06-04 19:55:08 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2012-05-10 15:58:21 +00:00
|
|
|
}
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* This function is called when an error message is posted on the bus */
|
2019-02-07 19:32:58 +00:00
|
|
|
static void
|
|
|
|
error_cb (GstBus * bus, GstMessage * msg, CustomData * data)
|
|
|
|
{
|
2012-05-10 15:58:21 +00:00
|
|
|
GError *err;
|
|
|
|
gchar *debug_info;
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Print error details on the screen */
|
|
|
|
gst_message_parse_error (msg, &err, &debug_info);
|
2019-02-07 19:32:58 +00:00
|
|
|
g_printerr ("Error received from element %s: %s\n",
|
|
|
|
GST_OBJECT_NAME (msg->src), err->message);
|
2012-05-10 15:58:21 +00:00
|
|
|
g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
|
|
|
|
g_clear_error (&err);
|
|
|
|
g_free (debug_info);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
g_main_loop_quit (data->main_loop);
|
|
|
|
}
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2019-02-07 19:32:58 +00:00
|
|
|
int
|
2022-12-13 17:42:11 +00:00
|
|
|
tutorial_main (int argc, char *argv[])
|
2019-02-07 19:32:58 +00:00
|
|
|
{
|
2012-05-10 15:58:21 +00:00
|
|
|
CustomData data;
|
|
|
|
GstPad *tee_audio_pad, *tee_video_pad, *tee_app_pad;
|
|
|
|
GstPad *queue_audio_pad, *queue_video_pad, *queue_app_pad;
|
2013-10-31 09:01:26 +00:00
|
|
|
GstAudioInfo info;
|
2012-05-10 15:58:21 +00:00
|
|
|
GstCaps *audio_caps;
|
|
|
|
GstBus *bus;
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Initialize cumstom data structure */
|
|
|
|
memset (&data, 0, sizeof (data));
|
2019-02-07 19:32:58 +00:00
|
|
|
data.b = 1; /* For waveform generation */
|
2012-05-10 15:58:21 +00:00
|
|
|
data.d = 1;
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Initialize GStreamer */
|
|
|
|
gst_init (&argc, &argv);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Create the elements */
|
|
|
|
data.app_source = gst_element_factory_make ("appsrc", "audio_source");
|
|
|
|
data.tee = gst_element_factory_make ("tee", "tee");
|
|
|
|
data.audio_queue = gst_element_factory_make ("queue", "audio_queue");
|
2019-02-07 19:32:58 +00:00
|
|
|
data.audio_convert1 =
|
|
|
|
gst_element_factory_make ("audioconvert", "audio_convert1");
|
|
|
|
data.audio_resample =
|
|
|
|
gst_element_factory_make ("audioresample", "audio_resample");
|
2012-05-10 15:58:21 +00:00
|
|
|
data.audio_sink = gst_element_factory_make ("autoaudiosink", "audio_sink");
|
|
|
|
data.video_queue = gst_element_factory_make ("queue", "video_queue");
|
2019-02-07 19:32:58 +00:00
|
|
|
data.audio_convert2 =
|
|
|
|
gst_element_factory_make ("audioconvert", "audio_convert2");
|
2012-05-10 15:58:21 +00:00
|
|
|
data.visual = gst_element_factory_make ("wavescope", "visual");
|
2019-02-07 19:32:58 +00:00
|
|
|
data.video_convert =
|
|
|
|
gst_element_factory_make ("videoconvert", "video_convert");
|
2012-05-10 15:58:21 +00:00
|
|
|
data.video_sink = gst_element_factory_make ("autovideosink", "video_sink");
|
|
|
|
data.app_queue = gst_element_factory_make ("queue", "app_queue");
|
|
|
|
data.app_sink = gst_element_factory_make ("appsink", "app_sink");
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Create the empty pipeline */
|
|
|
|
data.pipeline = gst_pipeline_new ("test-pipeline");
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2019-02-07 19:32:58 +00:00
|
|
|
if (!data.pipeline || !data.app_source || !data.tee || !data.audio_queue
|
|
|
|
|| !data.audio_convert1 || !data.audio_resample || !data.audio_sink
|
|
|
|
|| !data.video_queue || !data.audio_convert2 || !data.visual
|
|
|
|
|| !data.video_convert || !data.video_sink || !data.app_queue
|
|
|
|
|| !data.app_sink) {
|
2012-05-10 15:58:21 +00:00
|
|
|
g_printerr ("Not all elements could be created.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Configure wavescope */
|
|
|
|
g_object_set (data.visual, "shader", 0, "style", 0, NULL);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Configure appsrc */
|
2013-10-31 09:01:26 +00:00
|
|
|
gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, SAMPLE_RATE, 1, NULL);
|
|
|
|
audio_caps = gst_audio_info_to_caps (&info);
|
2019-02-07 19:32:58 +00:00
|
|
|
g_object_set (data.app_source, "caps", audio_caps, "format", GST_FORMAT_TIME,
|
|
|
|
NULL);
|
|
|
|
g_signal_connect (data.app_source, "need-data", G_CALLBACK (start_feed),
|
|
|
|
&data);
|
|
|
|
g_signal_connect (data.app_source, "enough-data", G_CALLBACK (stop_feed),
|
|
|
|
&data);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Configure appsink */
|
|
|
|
g_object_set (data.app_sink, "emit-signals", TRUE, "caps", audio_caps, NULL);
|
2019-02-07 19:32:58 +00:00
|
|
|
g_signal_connect (data.app_sink, "new-sample", G_CALLBACK (new_sample),
|
|
|
|
&data);
|
2012-05-10 15:58:21 +00:00
|
|
|
gst_caps_unref (audio_caps);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Link all elements that can be automatically linked because they have "Always" pads */
|
2019-02-07 19:32:58 +00:00
|
|
|
gst_bin_add_many (GST_BIN (data.pipeline), data.app_source, data.tee,
|
|
|
|
data.audio_queue, data.audio_convert1, data.audio_resample,
|
|
|
|
data.audio_sink, data.video_queue, data.audio_convert2, data.visual,
|
|
|
|
data.video_convert, data.video_sink, data.app_queue, data.app_sink, NULL);
|
|
|
|
if (gst_element_link_many (data.app_source, data.tee, NULL) != TRUE
|
|
|
|
|| gst_element_link_many (data.audio_queue, data.audio_convert1,
|
|
|
|
data.audio_resample, data.audio_sink, NULL) != TRUE
|
|
|
|
|| gst_element_link_many (data.video_queue, data.audio_convert2,
|
|
|
|
data.visual, data.video_convert, data.video_sink, NULL) != TRUE
|
|
|
|
|| gst_element_link_many (data.app_queue, data.app_sink, NULL) != TRUE) {
|
2012-05-10 15:58:21 +00:00
|
|
|
g_printerr ("Elements could not be linked.\n");
|
|
|
|
gst_object_unref (data.pipeline);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Manually link the Tee, which has "Request" pads */
|
2021-04-22 15:26:33 +00:00
|
|
|
tee_audio_pad = gst_element_request_pad_simple (data.tee, "src_%u");
|
2019-02-07 19:32:58 +00:00
|
|
|
g_print ("Obtained request pad %s for audio branch.\n",
|
|
|
|
gst_pad_get_name (tee_audio_pad));
|
2012-05-10 15:58:21 +00:00
|
|
|
queue_audio_pad = gst_element_get_static_pad (data.audio_queue, "sink");
|
2021-04-22 15:26:33 +00:00
|
|
|
tee_video_pad = gst_element_request_pad_simple (data.tee, "src_%u");
|
2019-02-07 19:32:58 +00:00
|
|
|
g_print ("Obtained request pad %s for video branch.\n",
|
|
|
|
gst_pad_get_name (tee_video_pad));
|
2012-05-10 15:58:21 +00:00
|
|
|
queue_video_pad = gst_element_get_static_pad (data.video_queue, "sink");
|
2021-04-22 15:26:33 +00:00
|
|
|
tee_app_pad = gst_element_request_pad_simple (data.tee, "src_%u");
|
2019-02-07 19:32:58 +00:00
|
|
|
g_print ("Obtained request pad %s for app branch.\n",
|
|
|
|
gst_pad_get_name (tee_app_pad));
|
2012-05-10 15:58:21 +00:00
|
|
|
queue_app_pad = gst_element_get_static_pad (data.app_queue, "sink");
|
|
|
|
if (gst_pad_link (tee_audio_pad, queue_audio_pad) != GST_PAD_LINK_OK ||
|
|
|
|
gst_pad_link (tee_video_pad, queue_video_pad) != GST_PAD_LINK_OK ||
|
|
|
|
gst_pad_link (tee_app_pad, queue_app_pad) != GST_PAD_LINK_OK) {
|
|
|
|
g_printerr ("Tee could not be linked\n");
|
|
|
|
gst_object_unref (data.pipeline);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
gst_object_unref (queue_audio_pad);
|
|
|
|
gst_object_unref (queue_video_pad);
|
|
|
|
gst_object_unref (queue_app_pad);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
|
|
|
|
bus = gst_element_get_bus (data.pipeline);
|
|
|
|
gst_bus_add_signal_watch (bus);
|
2019-02-07 19:32:58 +00:00
|
|
|
g_signal_connect (G_OBJECT (bus), "message::error", (GCallback) error_cb,
|
|
|
|
&data);
|
2012-05-10 15:58:21 +00:00
|
|
|
gst_object_unref (bus);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Start playing the pipeline */
|
|
|
|
gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Create a GLib Main Loop and set it to run */
|
|
|
|
data.main_loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
g_main_loop_run (data.main_loop);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Release the request pads from the Tee, and unref them */
|
|
|
|
gst_element_release_request_pad (data.tee, tee_audio_pad);
|
|
|
|
gst_element_release_request_pad (data.tee, tee_video_pad);
|
|
|
|
gst_element_release_request_pad (data.tee, tee_app_pad);
|
|
|
|
gst_object_unref (tee_audio_pad);
|
|
|
|
gst_object_unref (tee_video_pad);
|
|
|
|
gst_object_unref (tee_app_pad);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
2012-05-10 15:58:21 +00:00
|
|
|
/* Free resources */
|
|
|
|
gst_element_set_state (data.pipeline, GST_STATE_NULL);
|
|
|
|
gst_object_unref (data.pipeline);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-12-13 17:42:11 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
#if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE
|
|
|
|
return gst_macos_main (tutorial_main, argc, argv, NULL);
|
|
|
|
#else
|
|
|
|
return tutorial_main (argc, argv);
|
|
|
|
#endif
|
|
|
|
}
|