2016-05-27 02:21:04 +00:00
|
|
|
# Playback tutorial 3: Short-cutting the pipeline
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2020-12-05 00:46:02 +00:00
|
|
|
|
|
|
|
{{ ALERT_PY.md }}
|
|
|
|
|
|
|
|
{{ ALERT_JS.md }}
|
|
|
|
|
2016-06-16 01:26:59 +00:00
|
|
|
## Goal
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2016-11-05 08:18:49 +00:00
|
|
|
[](tutorials/basic/short-cutting-the-pipeline.md) showed
|
2016-05-16 14:30:34 +00:00
|
|
|
how an application can manually extract or inject data into a pipeline
|
2016-06-16 01:26:59 +00:00
|
|
|
by using two special elements called `appsrc` and `appsink`.
|
|
|
|
`playbin` allows using these elements too, but the method to connect
|
2016-11-05 08:18:49 +00:00
|
|
|
them is different. To connect an `appsink` to `playbin` see [](tutorials/playback/custom-playbin-sinks.md).
|
2016-05-16 14:30:34 +00:00
|
|
|
This tutorial shows:
|
|
|
|
|
2016-06-16 01:26:59 +00:00
|
|
|
- How to connect `appsrc` with `playbin`
|
|
|
|
- How to configure the `appsrc`
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2016-06-16 01:26:59 +00:00
|
|
|
## A playbin waveform generator
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2016-06-16 01:26:59 +00:00
|
|
|
Copy this code into a text file named `playback-tutorial-3.c`.
|
2016-05-16 14:30:34 +00:00
|
|
|
|
|
|
|
**playback-tutorial-3.c**
|
|
|
|
|
2016-06-06 00:58:09 +00:00
|
|
|
``` c
|
2016-05-16 14:30:34 +00:00
|
|
|
#include <gst/gst.h>
|
2016-06-16 01:26:59 +00:00
|
|
|
#include <gst/audio/audio.h>
|
2016-05-16 14:30:34 +00:00
|
|
|
#include <string.h>
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +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-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* Structure to contain all our information, so we can pass it to callbacks */
|
|
|
|
typedef struct _CustomData {
|
|
|
|
GstElement *pipeline;
|
|
|
|
GstElement *app_source;
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
guint64 num_samples; /* Number of samples generated so far (for timestamp generation) */
|
|
|
|
gfloat a, b, c, d; /* For waveform generation */
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
guint sourceid; /* To control the GSource */
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
GMainLoop *main_loop; /* GLib's Main Loop */
|
|
|
|
} CustomData;
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* This method is called by the idle GSource in the mainloop, to feed CHUNK_SIZE bytes into appsrc.
|
|
|
|
* The ide handler is added to the mainloop when appsrc requests us to start sending data (need-data signal)
|
|
|
|
* and is removed when appsrc has enough data (enough-data signal).
|
|
|
|
*/
|
|
|
|
static gboolean push_data (CustomData *data) {
|
|
|
|
GstBuffer *buffer;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
int i;
|
2016-06-16 01:26:59 +00:00
|
|
|
GstMapInfo map;
|
2016-05-16 14:30:34 +00:00
|
|
|
gint16 *raw;
|
|
|
|
gint num_samples = CHUNK_SIZE / 2; /* Because each sample is 16 bits */
|
|
|
|
gfloat freq;
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* Create a new empty buffer */
|
|
|
|
buffer = gst_buffer_new_and_alloc (CHUNK_SIZE);
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* Set its timestamp and duration */
|
|
|
|
GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (data->num_samples, GST_SECOND, SAMPLE_RATE);
|
2017-12-09 12:01:51 +00:00
|
|
|
GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (num_samples, GST_SECOND, SAMPLE_RATE);
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* Generate some psychodelic waveforms */
|
2016-06-16 01:26:59 +00:00
|
|
|
gst_buffer_map (buffer, &map, GST_MAP_WRITE);
|
|
|
|
raw = (gint16 *)map.data;
|
2016-05-16 14:30:34 +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;
|
|
|
|
raw[i] = (gint16)(500 * data->a);
|
|
|
|
}
|
2016-06-16 01:26:59 +00:00
|
|
|
gst_buffer_unmap (buffer, &map);
|
2016-05-16 14:30:34 +00:00
|
|
|
data->num_samples += num_samples;
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* Push the buffer into the appsrc */
|
|
|
|
g_signal_emit_by_name (data->app_source, "push-buffer", buffer, &ret);
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* Free the buffer now that we are done with it */
|
|
|
|
gst_buffer_unref (buffer);
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
/* We got some error, stop sending data */
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +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 */
|
|
|
|
static void start_feed (GstElement *source, guint size, CustomData *data) {
|
|
|
|
if (data->sourceid == 0) {
|
|
|
|
g_print ("Start feeding\n");
|
|
|
|
data->sourceid = g_idle_add ((GSourceFunc) push_data, data);
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* This callback triggers when appsrc has enough data and we can stop sending.
|
|
|
|
* We remove the idle handler from the mainloop */
|
|
|
|
static void stop_feed (GstElement *source, CustomData *data) {
|
|
|
|
if (data->sourceid != 0) {
|
|
|
|
g_print ("Stop feeding\n");
|
|
|
|
g_source_remove (data->sourceid);
|
|
|
|
data->sourceid = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* This function is called when an error message is posted on the bus */
|
|
|
|
static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
|
|
|
|
GError *err;
|
|
|
|
gchar *debug_info;
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* Print error details on the screen */
|
|
|
|
gst_message_parse_error (msg, &err, &debug_info);
|
|
|
|
g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
|
|
|
|
g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
|
|
|
|
g_clear_error (&err);
|
|
|
|
g_free (debug_info);
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
g_main_loop_quit (data->main_loop);
|
|
|
|
}
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-27 18:19:02 +00:00
|
|
|
/* This function is called when playbin has created the appsrc element, so we have
|
2016-05-16 14:30:34 +00:00
|
|
|
* a chance to configure it. */
|
|
|
|
static void source_setup (GstElement *pipeline, GstElement *source, CustomData *data) {
|
2016-06-16 01:26:59 +00:00
|
|
|
GstAudioInfo info;
|
2016-05-16 14:30:34 +00:00
|
|
|
GstCaps *audio_caps;
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
g_print ("Source has been created. Configuring.\n");
|
|
|
|
data->app_source = source;
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* Configure appsrc */
|
2016-06-16 01:26:59 +00:00
|
|
|
gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, SAMPLE_RATE, 1, NULL);
|
|
|
|
audio_caps = gst_audio_info_to_caps (&info);
|
|
|
|
g_object_set (source, "caps", audio_caps, "format", GST_FORMAT_TIME, NULL);
|
2016-05-16 14:30:34 +00:00
|
|
|
g_signal_connect (source, "need-data", G_CALLBACK (start_feed), data);
|
|
|
|
g_signal_connect (source, "enough-data", G_CALLBACK (stop_feed), data);
|
|
|
|
gst_caps_unref (audio_caps);
|
|
|
|
}
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
CustomData data;
|
|
|
|
GstBus *bus;
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2021-12-01 07:45:14 +00:00
|
|
|
/* Initialize custom data structure */
|
2016-05-16 14:30:34 +00:00
|
|
|
memset (&data, 0, sizeof (data));
|
|
|
|
data.b = 1; /* For waveform generation */
|
|
|
|
data.d = 1;
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* Initialize GStreamer */
|
|
|
|
gst_init (&argc, &argv);
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-27 18:19:02 +00:00
|
|
|
/* Create the playbin element */
|
|
|
|
data.pipeline = gst_parse_launch ("playbin uri=appsrc://", NULL);
|
2016-05-16 14:30:34 +00:00
|
|
|
g_signal_connect (data.pipeline, "source-setup", G_CALLBACK (source_setup), &data);
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +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);
|
|
|
|
g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, &data);
|
|
|
|
gst_object_unref (bus);
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* Start playing the pipeline */
|
|
|
|
gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +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-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* Free resources */
|
|
|
|
gst_element_set_state (data.pipeline, GST_STATE_NULL);
|
|
|
|
gst_object_unref (data.pipeline);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-08-17 09:56:41 +00:00
|
|
|
|
|
|
|
> ![information] If you need help to compile this code, refer to the
|
|
|
|
> **Building the tutorials** section for your platform: [Mac] or
|
|
|
|
> [Windows] or use this specific command on Linux:
|
|
|
|
>
|
|
|
|
> `` gcc playback-tutorial-3.c -o playback-tutorial-3 `pkg-config --cflags --libs gstreamer-1.0 gstreamer-audio-1.0` ``
|
|
|
|
>
|
|
|
|
> If you need help to run this code, refer to the **Running the
|
|
|
|
> tutorials** section for your platform: [Mac OS X], [Windows][1], for
|
|
|
|
> [iOS] or for [android].
|
|
|
|
>
|
|
|
|
> This tutorial opens a window and displays a movie, with accompanying
|
|
|
|
> audio. The media is fetched from the Internet, so the window might
|
|
|
|
> take a few seconds to appear, depending on your connection
|
|
|
|
> speed. In the console window, you should see a message indicating
|
|
|
|
> where the media is being stored, and a text graph representing the
|
|
|
|
> downloaded portions and the current position. A buffering message
|
|
|
|
> appears whenever buffering is required, which might never happen is
|
|
|
|
> your network connection is fast enough
|
|
|
|
>
|
|
|
|
> Required libraries: `gstreamer-1.0` `gstreamer-audio-1.0`
|
|
|
|
|
|
|
|
|
2016-06-16 01:26:59 +00:00
|
|
|
To use an `appsrc` as the source for the pipeline, simply instantiate a
|
|
|
|
`playbin` and set its URI to `appsrc://`
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2016-06-06 00:58:09 +00:00
|
|
|
``` c
|
2016-05-27 18:19:02 +00:00
|
|
|
/* Create the playbin element */
|
|
|
|
data.pipeline = gst_parse_launch ("playbin uri=appsrc://", NULL);
|
2016-05-16 14:30:34 +00:00
|
|
|
```
|
|
|
|
|
2016-06-16 01:26:59 +00:00
|
|
|
`playbin` will create an internal `appsrc` element and fire the
|
|
|
|
`source-setup` signal to allow the application to configure
|
2016-05-16 14:30:34 +00:00
|
|
|
it:
|
|
|
|
|
2016-06-06 00:58:09 +00:00
|
|
|
``` c
|
2016-05-16 14:30:34 +00:00
|
|
|
g_signal_connect (data.pipeline, "source-setup", G_CALLBACK (source_setup), &data);
|
|
|
|
```
|
|
|
|
|
|
|
|
In particular, it is important to set the caps property of `appsrc`,
|
2016-06-16 01:26:59 +00:00
|
|
|
since, once the signal handler returns, `playbin` will instantiate the
|
2016-05-16 14:30:34 +00:00
|
|
|
next element in the pipeline according to these
|
|
|
|
caps:
|
|
|
|
|
2016-06-06 00:58:09 +00:00
|
|
|
``` c
|
2016-05-27 18:19:02 +00:00
|
|
|
/* This function is called when playbin has created the appsrc element, so we have
|
2016-05-16 14:30:34 +00:00
|
|
|
* a chance to configure it. */
|
|
|
|
static void source_setup (GstElement *pipeline, GstElement *source, CustomData *data) {
|
2016-06-16 01:26:59 +00:00
|
|
|
GstAudioInfo info;
|
2016-05-16 14:30:34 +00:00
|
|
|
GstCaps *audio_caps;
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
g_print ("Source has been created. Configuring.\n");
|
|
|
|
data->app_source = source;
|
2016-05-27 02:21:04 +00:00
|
|
|
|
2016-05-16 14:30:34 +00:00
|
|
|
/* Configure appsrc */
|
2016-06-16 01:26:59 +00:00
|
|
|
gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, SAMPLE_RATE, 1, NULL);
|
|
|
|
audio_caps = gst_audio_info_to_caps (&info);
|
|
|
|
g_object_set (source, "caps", audio_caps, "format", GST_FORMAT_TIME, NULL);
|
2016-05-16 14:30:34 +00:00
|
|
|
g_signal_connect (source, "need-data", G_CALLBACK (start_feed), data);
|
|
|
|
g_signal_connect (source, "enough-data", G_CALLBACK (stop_feed), data);
|
|
|
|
gst_caps_unref (audio_caps);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-06-16 01:26:59 +00:00
|
|
|
The configuration of the `appsrc` is exactly the same as in
|
2016-11-05 08:18:49 +00:00
|
|
|
[](tutorials/basic/short-cutting-the-pipeline.md):
|
2016-06-16 01:26:59 +00:00
|
|
|
the caps are set to `audio/x-raw`, and two callbacks are registered,
|
2016-05-16 14:30:34 +00:00
|
|
|
so the element can tell the application when it needs to start and stop
|
2016-11-05 08:18:49 +00:00
|
|
|
pushing data. See [](tutorials/basic/short-cutting-the-pipeline.md)
|
2016-05-16 14:30:34 +00:00
|
|
|
for more details.
|
|
|
|
|
2016-06-16 01:26:59 +00:00
|
|
|
From this point onwards, `playbin` takes care of the rest of the
|
2016-05-16 14:30:34 +00:00
|
|
|
pipeline, and the application only needs to worry about generating more
|
|
|
|
data when told so.
|
|
|
|
|
2016-06-16 01:26:59 +00:00
|
|
|
To learn how data can be extracted from `playbin` using the
|
2016-11-05 08:18:49 +00:00
|
|
|
`appsink` element, see [](tutorials/playback/custom-playbin-sinks.md).
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2016-06-16 01:26:59 +00:00
|
|
|
## Conclusion
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2016-06-16 01:26:59 +00:00
|
|
|
This tutorial applies the concepts shown in
|
2016-11-05 08:18:49 +00:00
|
|
|
[](tutorials/basic/short-cutting-the-pipeline.md) to
|
2016-05-27 18:19:02 +00:00
|
|
|
`playbin`. In particular, it has shown:
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2016-06-16 01:26:59 +00:00
|
|
|
- How to connect `appsrc` with `playbin` using the special
|
|
|
|
URI `appsrc://`
|
|
|
|
- How to configure the `appsrc` using the `source-setup` signal
|
2016-05-16 14:30:34 +00:00
|
|
|
|
2017-12-03 10:57:25 +00:00
|
|
|
It has been a pleasure having you here, and see you soon!
|