From 54687a3e611ead4e0d0036d79c335b95e44fd231 Mon Sep 17 00:00:00 2001 From: Xavi Artigas Date: Thu, 10 May 2012 17:58:21 +0200 Subject: [PATCH] Added basic tutorial 8 --- gst-sdk/tutorials/basic-tutorial-8.c | 231 +++++++++++++++++++++ .../basic-tutorial-8/basic-tutorial-8.vcxproj | 85 ++++++++ .../basic-tutorial-8.vcxproj.filters | 6 + vs/2010/tutorials/tutorials.sln | 6 + 4 files changed, 328 insertions(+) create mode 100644 gst-sdk/tutorials/basic-tutorial-8.c create mode 100644 vs/2010/tutorials/basic-tutorial-8/basic-tutorial-8.vcxproj create mode 100644 vs/2010/tutorials/basic-tutorial-8/basic-tutorial-8.vcxproj.filters diff --git a/gst-sdk/tutorials/basic-tutorial-8.c b/gst-sdk/tutorials/basic-tutorial-8.c new file mode 100644 index 0000000..48e3e81 --- /dev/null +++ b/gst-sdk/tutorials/basic-tutorial-8.c @@ -0,0 +1,231 @@ +#include +#include +#include + +#define CHUNK_SIZE 1024 /* Amount of bytes we are sending in each buffer */ +#define SAMPLE_RATE 44100 /* Samples per second we are sending */ +#define AUDIO_CAPS "audio/x-raw-int,channels=1,rate=%d,signed=(boolean)true,width=16,depth=16,endianness=1234" + +/* Structure to contain all our information, so we can pass it to callbacks */ +typedef struct _CustomData { + GstElement *pipeline, *app_source, *tee, *audio_queue, *audio_convert1, *audio_sink; + GstElement *video_queue, *audio_convert2, *visual, *video_convert, *video_sink; + GstElement *app_queue, *app_sink; + + gint time; /* For timestamp generation */ + gfloat a, b, c, d; /* For waveform generation */ + + guint sourceid; /* To control the GSource */ + + GMainLoop *main_loop; /* GLib's Main Loop */ +} CustomData; + +/* 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; + gshort *raw; + gint num_samples = CHUNK_SIZE / 2; /* Because each sample is 16 bits */ + gfloat freq; + + /* Create a new empty buffer */ + buffer = gst_buffer_new_and_alloc (CHUNK_SIZE); + + /* Set its timestamp and duration */ + GST_BUFFER_TIMESTAMP (buffer) = GST_SECOND * data->time / SAMPLE_RATE; + GST_BUFFER_DURATION (buffer) = GST_SECOND * CHUNK_SIZE / SAMPLE_RATE; + + /* Generate some psychodelic waveforms */ + raw = (gshort *)GST_BUFFER_DATA (buffer); + 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] = (gshort)(500 * data->a); + } + data->time += num_samples; + + /* Push the buffer into the appsrc */ + g_signal_emit_by_name (data->app_source, "push-buffer", buffer, &ret); + + /* Free the buffer now that we are done with it */ + gst_buffer_unref (buffer); + + if (ret != GST_FLOW_OK) { + /* We got some error, stop sending data */ + return FALSE; + } + + return TRUE; +} + +/* 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 *playbin, guint size, CustomData *data) { + if (data->sourceid == 0) { + g_print ("Start feeding\n"); + data->sourceid = g_idle_add ((GSourceFunc) push_data, data); + } +} + +/* 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 *playbin, CustomData *data) { + if (data->sourceid != 0) { + g_print ("Stop feeding\n"); + g_source_remove (data->sourceid); + data->sourceid = 0; + } +} + +/* The appsink has received a buffer */ +static void new_buffer (GstElement *sink, CustomData *data) { + GstBuffer *buffer; + + /* Retrieve the buffer */ + g_signal_emit_by_name (sink, "pull-buffer", &buffer); + if (buffer) { + /* The only thing we do in this example is print a * to indicate a received buffer */ + g_print ("*"); + gst_buffer_unref (buffer); + } +} + +/* 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; + + /* 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); + + g_main_loop_quit (data->main_loop); +} + +int main(int argc, char *argv[]) { + CustomData data; + GstPadTemplate *tee_src_pad_template; + GstPad *tee_audio_pad, *tee_video_pad, *tee_app_pad; + GstPad *queue_audio_pad, *queue_video_pad, *queue_app_pad; + gchar *audio_caps_text; + GstCaps *audio_caps; + GstBus *bus; + + /* Initialize cumstom data structure */ + memset (&data, 0, sizeof (data)); + data.b = 1; /* For waveform generation */ + data.d = 1; + + /* Initialize GStreamer */ + gst_init (&argc, &argv); + + /* 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"); + data.audio_convert1 = gst_element_factory_make ("audioconvert", "audio_convert1"); + data.audio_sink = gst_element_factory_make ("autoaudiosink", "audio_sink"); + data.video_queue = gst_element_factory_make ("queue", "video_queue"); + data.audio_convert2 = gst_element_factory_make ("audioconvert", "audio_convert2"); + data.visual = gst_element_factory_make ("wavescope", "visual"); + data.video_convert = gst_element_factory_make ("ffmpegcolorspace", "csp"); + 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"); + + /* Create the empty pipeline */ + data.pipeline = gst_pipeline_new ("test-pipeline"); + + if (!data.pipeline || !data.app_source || !data.tee || !data.audio_queue || !data.audio_convert1 || !data.audio_sink || + !data.video_queue || !data.audio_convert2 || !data.visual || !data.video_convert || !data.video_sink || !data.app_queue || !data.app_sink) { + g_printerr ("Not all elements could be created.\n"); + return -1; + } + + /* Configure wavescope */ + g_object_set (data.visual, "shader", 0, "style", 0, NULL); + + /* Configure appsrc */ + audio_caps_text = g_strdup_printf (AUDIO_CAPS, SAMPLE_RATE); + audio_caps = gst_caps_from_string (audio_caps_text); + g_object_set (data.app_source, "caps", audio_caps, 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); + + /* Configure appsink */ + g_object_set (data.app_sink, "emit-signals", TRUE, "caps", audio_caps, NULL); + g_signal_connect (data.app_sink, "new-buffer", G_CALLBACK (new_buffer), &data); + gst_caps_unref (audio_caps); + g_free (audio_caps_text); + + /* Link all elements that can be automatically linked because they have "Always" pads */ + gst_bin_add_many (GST_BIN (data.pipeline), data.app_source, data.tee, data.audio_queue, data.audio_convert1, + 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_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) { + g_printerr ("Elements could not be linked.\n"); + gst_object_unref (data.pipeline); + return -1; + } + + /* Manually link the Tee, which has "Request" pads */ + tee_src_pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (data.tee), "src%d"); + tee_audio_pad = gst_element_request_pad (data.tee, tee_src_pad_template, NULL, NULL); + g_print ("Obtained request pad %s for audio branch.\n", gst_pad_get_name (tee_audio_pad)); + queue_audio_pad = gst_element_get_static_pad (data.audio_queue, "sink"); + tee_video_pad = gst_element_request_pad (data.tee, tee_src_pad_template, NULL, NULL); + g_print ("Obtained request pad %s for video branch.\n", gst_pad_get_name (tee_video_pad)); + queue_video_pad = gst_element_get_static_pad (data.video_queue, "sink"); + tee_app_pad = gst_element_request_pad (data.tee, tee_src_pad_template, NULL, NULL); + g_print ("Obtained request pad %s for app branch.\n", gst_pad_get_name (tee_app_pad)); + 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); + + /* 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); + + /* Start playing the pipeline */ + gst_element_set_state (data.pipeline, GST_STATE_PLAYING); + + /* 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); + + /* 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); + + /* Free resources */ + gst_element_set_state (data.pipeline, GST_STATE_NULL); + gst_object_unref (data.pipeline); + return 0; +} diff --git a/vs/2010/tutorials/basic-tutorial-8/basic-tutorial-8.vcxproj b/vs/2010/tutorials/basic-tutorial-8/basic-tutorial-8.vcxproj new file mode 100644 index 0000000..9d1d4eb --- /dev/null +++ b/vs/2010/tutorials/basic-tutorial-8/basic-tutorial-8.vcxproj @@ -0,0 +1,85 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {12F59BBB-208F-4F78-BB9C-4A15D2A9AB2E} + Win32Proj + basictutorial8 + + + + Application + true + Unicode + + + Application + false + true + Unicode + + + + + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + + + + + + + + + \ No newline at end of file diff --git a/vs/2010/tutorials/basic-tutorial-8/basic-tutorial-8.vcxproj.filters b/vs/2010/tutorials/basic-tutorial-8/basic-tutorial-8.vcxproj.filters new file mode 100644 index 0000000..368db13 --- /dev/null +++ b/vs/2010/tutorials/basic-tutorial-8/basic-tutorial-8.vcxproj.filters @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/vs/2010/tutorials/tutorials.sln b/vs/2010/tutorials/tutorials.sln index 3e2720f..238a658 100644 --- a/vs/2010/tutorials/tutorials.sln +++ b/vs/2010/tutorials/tutorials.sln @@ -19,6 +19,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "basic-tutorial-6", "basic-t EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "basic-tutorial-7", "basic-tutorial-7\basic-tutorial-7.vcxproj", "{B1666543-AE05-46BF-9528-95916A02A9D4}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "basic-tutorial-8", "basic-tutorial-8\basic-tutorial-8.vcxproj", "{12F59BBB-208F-4F78-BB9C-4A15D2A9AB2E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -61,6 +63,10 @@ Global {B1666543-AE05-46BF-9528-95916A02A9D4}.Debug|Win32.Build.0 = Debug|Win32 {B1666543-AE05-46BF-9528-95916A02A9D4}.Release|Win32.ActiveCfg = Release|Win32 {B1666543-AE05-46BF-9528-95916A02A9D4}.Release|Win32.Build.0 = Release|Win32 + {12F59BBB-208F-4F78-BB9C-4A15D2A9AB2E}.Debug|Win32.ActiveCfg = Debug|Win32 + {12F59BBB-208F-4F78-BB9C-4A15D2A9AB2E}.Debug|Win32.Build.0 = Debug|Win32 + {12F59BBB-208F-4F78-BB9C-4A15D2A9AB2E}.Release|Win32.ActiveCfg = Release|Win32 + {12F59BBB-208F-4F78-BB9C-4A15D2A9AB2E}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- 2.7.4