2016-11-05 08:18:49 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
|
2022-12-13 17:42:11 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <TargetConditionals.h>
|
|
|
|
#endif
|
|
|
|
|
2016-11-05 08:18:49 +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
|
|
|
|
{
|
2016-11-05 08:18:49 +00:00
|
|
|
GstElement *pipeline;
|
|
|
|
GstElement *source;
|
|
|
|
GstElement *convert;
|
2020-01-27 13:53:27 +00:00
|
|
|
GstElement *resample;
|
2016-11-05 08:18:49 +00:00
|
|
|
GstElement *sink;
|
|
|
|
} CustomData;
|
|
|
|
|
|
|
|
/* Handler for the pad-added signal */
|
2019-02-07 19:32:58 +00:00
|
|
|
static void pad_added_handler (GstElement * src, GstPad * pad,
|
|
|
|
CustomData * data);
|
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
|
|
|
{
|
2016-11-05 08:18:49 +00:00
|
|
|
CustomData data;
|
|
|
|
GstBus *bus;
|
|
|
|
GstMessage *msg;
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
gboolean terminate = FALSE;
|
|
|
|
|
|
|
|
/* Initialize GStreamer */
|
|
|
|
gst_init (&argc, &argv);
|
|
|
|
|
|
|
|
/* Create the elements */
|
|
|
|
data.source = gst_element_factory_make ("uridecodebin", "source");
|
|
|
|
data.convert = gst_element_factory_make ("audioconvert", "convert");
|
2020-01-27 13:53:27 +00:00
|
|
|
data.resample = gst_element_factory_make ("audioresample", "resample");
|
2016-11-05 08:18:49 +00:00
|
|
|
data.sink = gst_element_factory_make ("autoaudiosink", "sink");
|
|
|
|
|
|
|
|
/* Create the empty pipeline */
|
|
|
|
data.pipeline = gst_pipeline_new ("test-pipeline");
|
|
|
|
|
2020-01-27 13:53:27 +00:00
|
|
|
if (!data.pipeline || !data.source || !data.convert || !data.resample
|
|
|
|
|| !data.sink) {
|
2016-11-05 08:18:49 +00:00
|
|
|
g_printerr ("Not all elements could be created.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Build the pipeline. Note that we are NOT linking the source at this
|
|
|
|
* point. We will do it later. */
|
2019-02-07 19:32:58 +00:00
|
|
|
gst_bin_add_many (GST_BIN (data.pipeline), data.source, data.convert,
|
2020-01-27 13:53:27 +00:00
|
|
|
data.resample, data.sink, NULL);
|
|
|
|
if (!gst_element_link_many (data.convert, data.resample, data.sink, NULL)) {
|
2016-11-05 08:18:49 +00:00
|
|
|
g_printerr ("Elements could not be linked.\n");
|
|
|
|
gst_object_unref (data.pipeline);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the URI to play */
|
2019-02-07 19:32:58 +00:00
|
|
|
g_object_set (data.source, "uri",
|
2023-04-06 17:28:33 +00:00
|
|
|
"https://gstreamer.freedesktop.org/data/media/sintel_trailer-480p.webm",
|
2019-02-07 19:32:58 +00:00
|
|
|
NULL);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
|
|
|
/* Connect to the pad-added signal */
|
2019-02-07 19:32:58 +00:00
|
|
|
g_signal_connect (data.source, "pad-added", G_CALLBACK (pad_added_handler),
|
|
|
|
&data);
|
2016-11-05 08:18:49 +00:00
|
|
|
|
|
|
|
/* Start playing */
|
|
|
|
ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
|
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE) {
|
|
|
|
g_printerr ("Unable to set the pipeline to the playing state.\n");
|
|
|
|
gst_object_unref (data.pipeline);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Listen to the bus */
|
|
|
|
bus = gst_element_get_bus (data.pipeline);
|
|
|
|
do {
|
|
|
|
msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
|
|
|
|
GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
|
|
|
|
|
|
|
|
/* Parse message */
|
|
|
|
if (msg != NULL) {
|
|
|
|
GError *err;
|
|
|
|
gchar *debug_info;
|
|
|
|
|
|
|
|
switch (GST_MESSAGE_TYPE (msg)) {
|
|
|
|
case GST_MESSAGE_ERROR:
|
|
|
|
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);
|
|
|
|
g_printerr ("Debugging information: %s\n",
|
|
|
|
debug_info ? debug_info : "none");
|
2016-11-05 08:18:49 +00:00
|
|
|
g_clear_error (&err);
|
|
|
|
g_free (debug_info);
|
|
|
|
terminate = TRUE;
|
|
|
|
break;
|
|
|
|
case GST_MESSAGE_EOS:
|
|
|
|
g_print ("End-Of-Stream reached.\n");
|
|
|
|
terminate = TRUE;
|
|
|
|
break;
|
|
|
|
case GST_MESSAGE_STATE_CHANGED:
|
|
|
|
/* We are only interested in state-changed messages from the pipeline */
|
|
|
|
if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data.pipeline)) {
|
|
|
|
GstState old_state, new_state, pending_state;
|
2019-02-07 19:32:58 +00:00
|
|
|
gst_message_parse_state_changed (msg, &old_state, &new_state,
|
|
|
|
&pending_state);
|
2016-11-05 08:18:49 +00:00
|
|
|
g_print ("Pipeline state changed from %s to %s:\n",
|
2019-02-07 19:32:58 +00:00
|
|
|
gst_element_state_get_name (old_state),
|
|
|
|
gst_element_state_get_name (new_state));
|
2016-11-05 08:18:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* We should not reach here */
|
|
|
|
g_printerr ("Unexpected message received.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gst_message_unref (msg);
|
|
|
|
}
|
|
|
|
} while (!terminate);
|
|
|
|
|
|
|
|
/* Free resources */
|
|
|
|
gst_object_unref (bus);
|
|
|
|
gst_element_set_state (data.pipeline, GST_STATE_NULL);
|
|
|
|
gst_object_unref (data.pipeline);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function will be called by the pad-added signal */
|
2019-02-07 19:32:58 +00:00
|
|
|
static void
|
|
|
|
pad_added_handler (GstElement * src, GstPad * new_pad, CustomData * data)
|
|
|
|
{
|
2016-11-05 08:18:49 +00:00
|
|
|
GstPad *sink_pad = gst_element_get_static_pad (data->convert, "sink");
|
|
|
|
GstPadLinkReturn ret;
|
|
|
|
GstCaps *new_pad_caps = NULL;
|
|
|
|
GstStructure *new_pad_struct = NULL;
|
|
|
|
const gchar *new_pad_type = NULL;
|
|
|
|
|
2019-02-07 19:32:58 +00:00
|
|
|
g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (new_pad),
|
|
|
|
GST_ELEMENT_NAME (src));
|
2016-11-05 08:18:49 +00:00
|
|
|
|
|
|
|
/* If our converter is already linked, we have nothing to do here */
|
|
|
|
if (gst_pad_is_linked (sink_pad)) {
|
2017-10-10 19:31:11 +00:00
|
|
|
g_print ("We are already linked. Ignoring.\n");
|
2016-11-05 08:18:49 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the new pad's type */
|
2017-09-14 10:38:22 +00:00
|
|
|
new_pad_caps = gst_pad_get_current_caps (new_pad);
|
2016-11-05 08:18:49 +00:00
|
|
|
new_pad_struct = gst_caps_get_structure (new_pad_caps, 0);
|
|
|
|
new_pad_type = gst_structure_get_name (new_pad_struct);
|
|
|
|
if (!g_str_has_prefix (new_pad_type, "audio/x-raw")) {
|
2019-02-07 19:32:58 +00:00
|
|
|
g_print ("It has type '%s' which is not raw audio. Ignoring.\n",
|
|
|
|
new_pad_type);
|
2016-11-05 08:18:49 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attempt the link */
|
|
|
|
ret = gst_pad_link (new_pad, sink_pad);
|
|
|
|
if (GST_PAD_LINK_FAILED (ret)) {
|
2017-10-10 19:31:11 +00:00
|
|
|
g_print ("Type is '%s' but link failed.\n", new_pad_type);
|
2016-11-05 08:18:49 +00:00
|
|
|
} else {
|
2017-10-10 19:31:11 +00:00
|
|
|
g_print ("Link succeeded (type '%s').\n", new_pad_type);
|
2016-11-05 08:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
/* Unreference the new pad's caps, if we got them */
|
|
|
|
if (new_pad_caps != NULL)
|
|
|
|
gst_caps_unref (new_pad_caps);
|
|
|
|
|
|
|
|
/* Unreference the sink pad */
|
|
|
|
gst_object_unref (sink_pad);
|
|
|
|
}
|
2022-12-13 17:42:11 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
#if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE
|
2024-02-02 11:54:32 +00:00
|
|
|
return gst_macos_main ((GstMainFunc) tutorial_main, argc, argv, NULL);
|
2022-12-13 17:42:11 +00:00
|
|
|
#else
|
|
|
|
return tutorial_main (argc, argv);
|
|
|
|
#endif
|
|
|
|
}
|