diff options
Diffstat (limited to 'tests/examples/app')
-rw-r--r-- | tests/examples/app/.gitignore | 6 | ||||
-rw-r--r-- | tests/examples/app/Makefile.am | 32 | ||||
-rw-r--r-- | tests/examples/app/appsink-src.c | 192 | ||||
-rw-r--r-- | tests/examples/app/appsrc-ra.c | 223 | ||||
-rw-r--r-- | tests/examples/app/appsrc-seekable.c | 229 | ||||
-rw-r--r-- | tests/examples/app/appsrc-stream.c | 249 | ||||
-rw-r--r-- | tests/examples/app/appsrc-stream2.c | 219 | ||||
-rw-r--r-- | tests/examples/app/appsrc_ex.c | 89 |
8 files changed, 1239 insertions, 0 deletions
diff --git a/tests/examples/app/.gitignore b/tests/examples/app/.gitignore new file mode 100644 index 00000000..8a2c7615 --- /dev/null +++ b/tests/examples/app/.gitignore @@ -0,0 +1,6 @@ +appsrc_ex +appsrc-ra +appsrc-seekable +appsrc-stream +appsrc-stream2 +appsink-src diff --git a/tests/examples/app/Makefile.am b/tests/examples/app/Makefile.am new file mode 100644 index 00000000..4f3df777 --- /dev/null +++ b/tests/examples/app/Makefile.am @@ -0,0 +1,32 @@ + +noinst_PROGRAMS = appsrc_ex appsrc-stream appsrc-stream2 appsrc-ra \ + appsrc-seekable appsink-src + +appsrc_ex_SOURCES = appsrc_ex.c +appsrc_ex_CFLAGS = $(GST_CFLAGS) $(GCONF_CFLAGS) +appsrc_ex_LDFLAGS = \ + $(GST_LIBS) \ + $(top_builddir)/gst-libs/gst/app/libgstapp-@GST_MAJORMINOR@.la + +appsrc_stream_SOURCES = appsrc-stream.c +appsrc_stream_CFLAGS = $(GST_CFLAGS) $(GCONF_CFLAGS) +appsrc_stream_LDFLAGS = $(GST_LIBS) + +appsrc_stream2_SOURCES = appsrc-stream2.c +appsrc_stream2_CFLAGS = $(GST_CFLAGS) $(GCONF_CFLAGS) +appsrc_stream2_LDFLAGS = $(GST_LIBS) + +appsrc_ra_SOURCES = appsrc-ra.c +appsrc_ra_CFLAGS = $(GST_CFLAGS) $(GCONF_CFLAGS) +appsrc_ra_LDFLAGS = $(GST_LIBS) + +appsrc_seekable_SOURCES = appsrc-seekable.c +appsrc_seekable_CFLAGS = $(GST_CFLAGS) $(GCONF_CFLAGS) +appsrc_seekable_LDFLAGS = $(GST_LIBS) + +appsink_src_SOURCES = appsink-src.c +appsink_src_CFLAGS = $(GST_CFLAGS) $(GCONF_CFLAGS) +appsink_src_LDFLAGS = \ + $(GST_LIBS) \ + $(top_builddir)/gst-libs/gst/app/libgstapp-@GST_MAJORMINOR@.la + diff --git a/tests/examples/app/appsink-src.c b/tests/examples/app/appsink-src.c new file mode 100644 index 00000000..a92dfb51 --- /dev/null +++ b/tests/examples/app/appsink-src.c @@ -0,0 +1,192 @@ +#include <gst/gst.h> + +#include <string.h> + +#include <gst/app/gstappsrc.h> +#include <gst/app/gstappsink.h> +#include <gst/app/gstappbuffer.h> + +/* these are the caps we are going to pass through the appsink and appsrc */ +const gchar *audio_caps = + "audio/x-raw-int,channels=1,rate=8000,signed=(boolean)true,width=16,depth=16,endianness=1234"; + +typedef struct +{ + GMainLoop *loop; + GstElement *source; + GstElement *sink; +} ProgramData; + +/* called when the appsink notifies us that there is a new buffer ready for + * processing */ +static void +on_new_buffer_from_source (GstElement * elt, ProgramData * data) +{ + guint size; + gpointer raw_buffer; + GstBuffer *app_buffer, *buffer; + GstElement *source; + + /* get the buffer from appsink */ + buffer = gst_app_sink_pull_buffer (GST_APP_SINK (elt)); + + /* turn it into an app buffer, it's not really needed, we could simply push + * the retrieved buffer from appsink into appsrc just fine. */ + size = GST_BUFFER_SIZE (buffer); + g_print ("Pushing a buffer of size %d\n", size); + raw_buffer = g_malloc0 (size); + memcpy (raw_buffer, GST_BUFFER_DATA (buffer), size); + app_buffer = gst_app_buffer_new (raw_buffer, size, g_free, raw_buffer); + + /* newer basesrc will set caps for use automatically but it does not really + * hurt to set it on the buffer again */ + gst_buffer_set_caps (app_buffer, GST_BUFFER_CAPS (buffer)); + + /* we don't need the appsink buffer anymore */ + gst_buffer_unref (buffer); + + /* get source an push new buffer */ + source = gst_bin_get_by_name (GST_BIN (data->sink), "testsource"); + gst_app_src_push_buffer (GST_APP_SRC (source), app_buffer); +} + +/* called when we get a GstMessage from the source pipeline when we get EOS, we + * notify the appsrc of it. */ +static gboolean +on_source_message (GstBus * bus, GstMessage * message, ProgramData * data) +{ + GstElement *source; + + switch (GST_MESSAGE_TYPE (message)) { + case GST_MESSAGE_EOS: + g_print ("The source got dry\n"); + source = gst_bin_get_by_name (GST_BIN (data->sink), "testsource"); + gst_app_src_end_of_stream (GST_APP_SRC (source)); + break; + case GST_MESSAGE_ERROR: + g_print ("Received error\n"); + g_main_loop_quit (data->loop); + break; + default: + break; + } + return TRUE; +} + +/* called when we get a GstMessage from the sink pipeline when we get EOS, we + * exit the mainloop and this testapp. */ +static gboolean +on_sink_message (GstBus * bus, GstMessage * message, ProgramData * data) +{ + /* nil */ + switch (GST_MESSAGE_TYPE (message)) { + case GST_MESSAGE_EOS: + g_print ("Finished playback\n"); + g_main_loop_quit (data->loop); + break; + case GST_MESSAGE_ERROR: + g_print ("Received error\n"); + g_main_loop_quit (data->loop); + break; + default: + break; + } + return TRUE; +} + +int +main (int argc, char *argv[]) +{ + gchar *filename = NULL; + ProgramData *data = NULL; + gchar *string = NULL; + GstBus *bus = NULL; + GstElement *testsink = NULL; + GstElement *testsource = NULL; + + gst_init (&argc, &argv); + + if (argc == 2) + filename = g_strdup (argv[1]); + else + filename = g_strdup ("/usr/share/sounds/ekiga/ring.wav"); + + data = g_new0 (ProgramData, 1); + + data->loop = g_main_loop_new (NULL, FALSE); + + /* setting up source pipeline, we read from a file and convert to our desired + * caps. */ + string = + g_strdup_printf + ("filesrc location=\"%s\" ! wavparse ! audioconvert ! audioresample ! appsink caps=\"%s\" name=testsink", + filename, audio_caps); + g_free (filename); + data->source = gst_parse_launch (string, NULL); + g_free (string); + + if (data->source == NULL) { + g_print ("Bad source\n"); + return -1; + } + + /* to be notified of messages from this pipeline, mostly EOS */ + bus = gst_element_get_bus (data->source); + gst_bus_add_watch (bus, (GstBusFunc) on_source_message, data); + gst_object_unref (bus); + + /* we use appsink in push mode, it sends us a signal when data is available + * and we pull out the data in the signal callback. We want the appsink to + * push as fast as it can, hence the sync=false */ + testsink = gst_bin_get_by_name (GST_BIN (data->source), "testsink"); + g_object_set (G_OBJECT (testsink), "emit-signals", TRUE, "sync", FALSE, NULL); + g_signal_connect (testsink, "new-buffer", + G_CALLBACK (on_new_buffer_from_source), data); + gst_object_unref (testsink); + + /* setting up sink pipeline, we push audio data into this pipeline that will + * then play it back using the default audio sink. We have no blocking + * behaviour on the src which means that we will push the entire file into + * memory. */ + string = + g_strdup_printf ("appsrc name=testsource caps=\"%s\" ! autoaudiosink", + audio_caps); + data->sink = gst_parse_launch (string, NULL); + g_free (string); + + if (data->sink == NULL) { + g_print ("Bad sink\n"); + return -1; + } + + testsource = gst_bin_get_by_name (GST_BIN (data->sink), "testsource"); + /* configure for time-based format */ + g_object_set (testsource, "format", GST_FORMAT_TIME, NULL); + /* uncomment the next line to block when appsrc has buffered enough */ + /* g_object_set (testsource, "block", TRUE, NULL); */ + gst_object_unref (testsource); + + bus = gst_element_get_bus (data->sink); + gst_bus_add_watch (bus, (GstBusFunc) on_sink_message, data); + gst_object_unref (bus); + + /* launching things */ + gst_element_set_state (data->sink, GST_STATE_PLAYING); + gst_element_set_state (data->source, GST_STATE_PLAYING); + + /* let's run !, this loop will quit when the sink pipeline goes EOS or when an + * error occurs in the source or sink pipelines. */ + g_print ("Let's run!\n"); + g_main_loop_run (data->loop); + g_print ("Going out\n"); + + gst_element_set_state (data->source, GST_STATE_NULL); + gst_element_set_state (data->sink, GST_STATE_NULL); + + gst_object_unref (data->source); + gst_object_unref (data->sink); + g_main_loop_unref (data->loop); + g_free (data); + + return 0; +} diff --git a/tests/examples/app/appsrc-ra.c b/tests/examples/app/appsrc-ra.c new file mode 100644 index 00000000..aa4962fe --- /dev/null +++ b/tests/examples/app/appsrc-ra.c @@ -0,0 +1,223 @@ +/* GStreamer + * + * appsrc-ra.c: example for using appsrc in random-access mode. + * + * Copyright (C) 2008 Wim Taymans <wim.taymans@gmail.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., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gst/gst.h> + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +GST_DEBUG_CATEGORY (appsrc_playbin_debug); +#define GST_CAT_DEFAULT appsrc_playbin_debug + +/* + * an example application of using appsrc in random-access mode. When the + * appsrc requests data with the need-data signal, we retrieve a buffer of the + * requested size and push it to appsrc. + * + * This is a good example how one would deal with a local file resource. + * + * Appsrc in random-access mode needs seeking support and we must thus connect + * to the seek signal to perform any seeks when requested. + * + * In random-access mode we must set the size of the source material. + */ +typedef struct _App App; + +struct _App +{ + GstElement *playbin; + GstElement *appsrc; + + GMainLoop *loop; + + GMappedFile *file; + guint8 *data; + gsize length; + guint64 offset; +}; + +App s_app; + +/* This method is called by the need-data signal callback, we feed data into the + * appsrc with the requested size. + */ +static void +feed_data (GstElement * appsrc, guint size, App * app) +{ + GstBuffer *buffer; + GstFlowReturn ret; + + buffer = gst_buffer_new (); + + if (app->offset >= app->length) { + /* we are EOS, send end-of-stream */ + g_signal_emit_by_name (app->appsrc, "end-of-stream", &ret); + return; + } + + /* read the amount of data, we are allowed to return less if we are EOS */ + if (app->offset + size > app->length) + size = app->length - app->offset; + + GST_BUFFER_DATA (buffer) = app->data + app->offset; + GST_BUFFER_SIZE (buffer) = size; + /* we need to set an offset for random access */ + GST_BUFFER_OFFSET (buffer) = app->offset; + GST_BUFFER_OFFSET_END (buffer) = app->offset + size; + + GST_DEBUG ("feed buffer %p, offset %" G_GUINT64_FORMAT "-%u", buffer, + app->offset, size); + g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret); + gst_buffer_unref (buffer); + + app->offset += size; + + return; +} + +/* called when appsrc wants us to return data from a new position with the next + * call to push-buffer. */ +static gboolean +seek_data (GstElement * appsrc, guint64 position, App * app) +{ + GST_DEBUG ("seek to offset %" G_GUINT64_FORMAT, position); + app->offset = position; + + return TRUE; +} + +/* this callback is called when playbin2 has constructed a source object to read + * from. Since we provided the appsrc:// uri to playbin2, this will be the + * appsrc that we must handle. We set up some signals to push data into appsrc + * and one to perform a seek. */ +static void +found_source (GObject * object, GObject * orig, GParamSpec * pspec, App * app) +{ + /* get a handle to the appsrc */ + g_object_get (orig, pspec->name, &app->appsrc, NULL); + + GST_DEBUG ("got appsrc %p", app->appsrc); + + /* we can set the length in appsrc. This allows some elements to estimate the + * total duration of the stream. It's a good idea to set the property when you + * can but it's not required. */ + g_object_set (app->appsrc, "size", app->length, NULL); + g_object_set (app->appsrc, "stream-type", 2, NULL); + + /* configure the appsrc, we will push a buffer to appsrc when it needs more + * data */ + g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data), app); + g_signal_connect (app->appsrc, "seek-data", G_CALLBACK (seek_data), app); +} + +static gboolean +bus_message (GstBus * bus, GstMessage * message, App * app) +{ + GST_DEBUG ("got message %s", + gst_message_type_get_name (GST_MESSAGE_TYPE (message))); + + switch (GST_MESSAGE_TYPE (message)) { + case GST_MESSAGE_ERROR: + g_error ("received error"); + g_main_loop_quit (app->loop); + break; + case GST_MESSAGE_EOS: + g_main_loop_quit (app->loop); + break; + default: + break; + } + return TRUE; +} + +int +main (int argc, char *argv[]) +{ + App *app = &s_app; + GError *error = NULL; + GstBus *bus; + + gst_init (&argc, &argv); + + GST_DEBUG_CATEGORY_INIT (appsrc_playbin_debug, "appsrc-playbin", 0, + "appsrc playbin example"); + + if (argc < 2) { + g_print ("usage: %s <filename>\n", argv[0]); + return -1; + } + + /* try to open the file as an mmapped file */ + app->file = g_mapped_file_new (argv[1], FALSE, &error); + if (error) { + g_print ("failed to open file: %s\n", error->message); + g_error_free (error); + return -2; + } + /* get some vitals, this will be used to read data from the mmapped file and + * feed it to appsrc. */ + app->length = g_mapped_file_get_length (app->file); + app->data = (guint8 *) g_mapped_file_get_contents (app->file); + app->offset = 0; + + /* create a mainloop to get messages */ + app->loop = g_main_loop_new (NULL, TRUE); + + app->playbin = gst_element_factory_make ("playbin2", NULL); + g_assert (app->playbin); + + bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin)); + + /* add watch for messages */ + gst_bus_add_watch (bus, (GstBusFunc) bus_message, app); + + /* set to read from appsrc */ + g_object_set (app->playbin, "uri", "appsrc://", NULL); + + /* get notification when the source is created so that we get a handle to it + * and can configure it */ + g_signal_connect (app->playbin, "deep-notify::source", + (GCallback) found_source, app); + + /* go to playing and wait in a mainloop. */ + gst_element_set_state (app->playbin, GST_STATE_PLAYING); + + /* this mainloop is stopped when we receive an error or EOS */ + g_main_loop_run (app->loop); + + GST_DEBUG ("stopping"); + + gst_element_set_state (app->playbin, GST_STATE_NULL); + + /* free the file */ + g_mapped_file_free (app->file); + + gst_object_unref (bus); + g_main_loop_unref (app->loop); + + return 0; +} diff --git a/tests/examples/app/appsrc-seekable.c b/tests/examples/app/appsrc-seekable.c new file mode 100644 index 00000000..7137d13e --- /dev/null +++ b/tests/examples/app/appsrc-seekable.c @@ -0,0 +1,229 @@ +/* GStreamer + * + * appsrc-seekable.c: example for using appsrc in seekable mode. + * + * Copyright (C) 2008 Wim Taymans <wim.taymans@gmail.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., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gst/gst.h> + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +GST_DEBUG_CATEGORY (appsrc_playbin_debug); +#define GST_CAT_DEFAULT appsrc_playbin_debug + +/* + * an example application of using appsrc in seekable mode. When the + * appsrc requests data with the need-data signal, we retrieve a buffer and + * push it to appsrc. We can also use the method as demonstrated in + * appsrc-stream.c, ie. pushing buffers when we can. + * + * This is a good example how one would deal with a remote http server that + * supports range requests. + * + * Appsrc in seekable mode needs seeking support and we must thus connect + * to the seek signal to perform any seeks when requested. + * + * In seekable mode we should set the size of the source material. + */ +typedef struct _App App; + +struct _App +{ + GstElement *playbin; + GstElement *appsrc; + + GMainLoop *loop; + + GMappedFile *file; + guint8 *data; + gsize length; + guint64 offset; +}; + +App s_app; + +#define CHUNK_SIZE 4096 + +/* This method is called by the need-data signal callback, we feed data into the + * appsrc with an arbitrary size. + */ +static void +feed_data (GstElement * appsrc, guint size, App * app) +{ + GstBuffer *buffer; + guint len; + GstFlowReturn ret; + + buffer = gst_buffer_new (); + + if (app->offset >= app->length) { + /* we are EOS, send end-of-stream */ + g_signal_emit_by_name (app->appsrc, "end-of-stream", &ret); + return; + } + + /* read any amount of data, we are allowed to return less if we are EOS */ + len = CHUNK_SIZE; + if (app->offset + len > app->length) + len = app->length - app->offset; + + GST_BUFFER_DATA (buffer) = app->data + app->offset; + GST_BUFFER_SIZE (buffer) = len; + + GST_DEBUG ("feed buffer %p, offset %" G_GUINT64_FORMAT "-%u", buffer, + app->offset, len); + g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret); + gst_buffer_unref (buffer); + + app->offset += len; + + return; +} + +/* called when appsrc wants us to return data from a new position with the next + * call to push-buffer. */ +static gboolean +seek_data (GstElement * appsrc, guint64 position, App * app) +{ + GST_DEBUG ("seek to offset %" G_GUINT64_FORMAT, position); + app->offset = position; + + return TRUE; +} + +/* this callback is called when playbin2 has constructed a source object to read + * from. Since we provided the appsrc:// uri to playbin2, this will be the + * appsrc that we must handle. We set up some signals to push data into appsrc + * and one to perform a seek. */ +static void +found_source (GObject * object, GObject * orig, GParamSpec * pspec, App * app) +{ + /* get a handle to the appsrc */ + g_object_get (orig, pspec->name, &app->appsrc, NULL); + + GST_DEBUG ("got appsrc %p", app->appsrc); + + /* we can set the length in appsrc. This allows some elements to estimate the + * total duration of the stream. It's a good idea to set the property when you + * can but it's not required. */ + g_object_set (app->appsrc, "size", app->length, NULL); + /* we are seekable in push mode, this means that the element usually pushes + * out buffers of an undefined size and that seeks happen only occasionally + * and only by request of the user. */ + g_object_set (app->appsrc, "stream-type", 1, NULL); + + /* configure the appsrc, we will push a buffer to appsrc when it needs more + * data */ + g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data), app); + g_signal_connect (app->appsrc, "seek-data", G_CALLBACK (seek_data), app); +} + +static gboolean +bus_message (GstBus * bus, GstMessage * message, App * app) +{ + GST_DEBUG ("got message %s", + gst_message_type_get_name (GST_MESSAGE_TYPE (message))); + + switch (GST_MESSAGE_TYPE (message)) { + case GST_MESSAGE_ERROR: + g_error ("received error"); + g_main_loop_quit (app->loop); + break; + case GST_MESSAGE_EOS: + g_main_loop_quit (app->loop); + break; + default: + break; + } + return TRUE; +} + +int +main (int argc, char *argv[]) +{ + App *app = &s_app; + GError *error = NULL; + GstBus *bus; + + gst_init (&argc, &argv); + + GST_DEBUG_CATEGORY_INIT (appsrc_playbin_debug, "appsrc-playbin", 0, + "appsrc playbin example"); + + if (argc < 2) { + g_print ("usage: %s <filename>\n", argv[0]); + return -1; + } + + /* try to open the file as an mmapped file */ + app->file = g_mapped_file_new (argv[1], FALSE, &error); + if (error) { + g_print ("failed to open file: %s\n", error->message); + g_error_free (error); + return -2; + } + /* get some vitals, this will be used to read data from the mmapped file and + * feed it to appsrc. */ + app->length = g_mapped_file_get_length (app->file); + app->data = (guint8 *) g_mapped_file_get_contents (app->file); + app->offset = 0; + + /* create a mainloop to get messages */ + app->loop = g_main_loop_new (NULL, TRUE); + + app->playbin = gst_element_factory_make ("playbin2", NULL); + g_assert (app->playbin); + + bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin)); + + /* add watch for messages */ + gst_bus_add_watch (bus, (GstBusFunc) bus_message, app); + + /* set to read from appsrc */ + g_object_set (app->playbin, "uri", "appsrc://", NULL); + + /* get notification when the source is created so that we get a handle to it + * and can configure it */ + g_signal_connect (app->playbin, "deep-notify::source", + (GCallback) found_source, app); + + /* go to playing and wait in a mainloop. */ + gst_element_set_state (app->playbin, GST_STATE_PLAYING); + + /* this mainloop is stopped when we receive an error or EOS */ + g_main_loop_run (app->loop); + + GST_DEBUG ("stopping"); + + gst_element_set_state (app->playbin, GST_STATE_NULL); + + /* free the file */ + g_mapped_file_free (app->file); + + gst_object_unref (bus); + g_main_loop_unref (app->loop); + + return 0; +} diff --git a/tests/examples/app/appsrc-stream.c b/tests/examples/app/appsrc-stream.c new file mode 100644 index 00000000..870d707c --- /dev/null +++ b/tests/examples/app/appsrc-stream.c @@ -0,0 +1,249 @@ +/* GStreamer + * + * appsrc-stream.c: example for using appsrc in streaming mode. + * + * Copyright (C) 2008 Wim Taymans <wim.taymans@gmail.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., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gst/gst.h> + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +GST_DEBUG_CATEGORY (appsrc_playbin_debug); +#define GST_CAT_DEFAULT appsrc_playbin_debug + +/* + * an example application of using appsrc in streaming push mode. We simply push + * buffers into appsrc. The size of the buffers we push can be any size we + * choose. + * + * This example is very close to how one would deal with a streaming webserver + * that does not support range requests or does not report the total file size. + * + * Some optimisations are done so that we don't push too much data. We connect + * to the need-data and enough-data signals to start/stop sending buffers. + * + * Appsrc in streaming mode (the default) does not support seeking so we don't + * have to handle any seek callbacks. + * + * Some formats are able to estimate the duration of the media file based on the + * file length (mp3, mpeg,..), others report an unknown length (ogg,..). + */ +typedef struct _App App; + +struct _App +{ + GstElement *playbin; + GstElement *appsrc; + + GMainLoop *loop; + guint sourceid; + + GMappedFile *file; + guint8 *data; + gsize length; + guint64 offset; +}; + +App s_app; + +#define CHUNK_SIZE 4096 + +/* This method is called by the idle GSource in the mainloop. We 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 +read_data (App * app) +{ + GstBuffer *buffer; + guint len; + GstFlowReturn ret; + + buffer = gst_buffer_new (); + + if (app->offset >= app->length) { + /* we are EOS, send end-of-stream and remove the source */ + g_signal_emit_by_name (app->appsrc, "end-of-stream", &ret); + return FALSE; + } + + /* read the next chunk */ + len = CHUNK_SIZE; + if (app->offset + len > app->length) + len = app->length - app->offset; + + GST_BUFFER_DATA (buffer) = app->data + app->offset; + GST_BUFFER_SIZE (buffer) = len; + + GST_DEBUG ("feed buffer %p, offset %" G_GUINT64_FORMAT "-%u", buffer, + app->offset, len); + g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret); + gst_buffer_unref (buffer); + if (ret != GST_FLOW_OK) { + /* some error, stop sending data */ + return FALSE; + } + + app->offset += len; + + return TRUE; +} + +/* This signal callback is called when appsrc needs data, we add an idle handler + * to the mainloop to start pushing data into the appsrc */ +static void +start_feed (GstElement * playbin, guint size, App * app) +{ + if (app->sourceid == 0) { + GST_DEBUG ("start feeding"); + app->sourceid = g_idle_add ((GSourceFunc) read_data, app); + } +} + +/* This callback is called when appsrc has enough data and we can stop sending. + * We remove the idle handler from the mainloop */ +static void +stop_feed (GstElement * playbin, App * app) +{ + if (app->sourceid != 0) { + GST_DEBUG ("stop feeding"); + g_source_remove (app->sourceid); + app->sourceid = 0; + } +} + +/* this callback is called when playbin2 has constructed a source object to read + * from. Since we provided the appsrc:// uri to playbin2, this will be the + * appsrc that we must handle. We set up some signals to start and stop pushing + * data into appsrc */ +static void +found_source (GObject * object, GObject * orig, GParamSpec * pspec, App * app) +{ + /* get a handle to the appsrc */ + g_object_get (orig, pspec->name, &app->appsrc, NULL); + + GST_DEBUG ("got appsrc %p", app->appsrc); + + /* we can set the length in appsrc. This allows some elements to estimate the + * total duration of the stream. It's a good idea to set the property when you + * can but it's not required. */ + g_object_set (app->appsrc, "size", app->length, NULL); + + /* configure the appsrc, we will push data into the appsrc from the + * mainloop. */ + g_signal_connect (app->appsrc, "need-data", G_CALLBACK (start_feed), app); + g_signal_connect (app->appsrc, "enough-data", G_CALLBACK (stop_feed), app); +} + +static gboolean +bus_message (GstBus * bus, GstMessage * message, App * app) +{ + GST_DEBUG ("got message %s", + gst_message_type_get_name (GST_MESSAGE_TYPE (message))); + + switch (GST_MESSAGE_TYPE (message)) { + case GST_MESSAGE_ERROR: + g_error ("received error"); + g_main_loop_quit (app->loop); + break; + case GST_MESSAGE_EOS: + g_main_loop_quit (app->loop); + break; + default: + break; + } + return TRUE; +} + +int +main (int argc, char *argv[]) +{ + App *app = &s_app; + GError *error = NULL; + GstBus *bus; + + gst_init (&argc, &argv); + + GST_DEBUG_CATEGORY_INIT (appsrc_playbin_debug, "appsrc-playbin", 0, + "appsrc playbin example"); + + if (argc < 2) { + g_print ("usage: %s <filename>\n", argv[0]); + return -1; + } + + /* try to open the file as an mmapped file */ + app->file = g_mapped_file_new (argv[1], FALSE, &error); + if (error) { + g_print ("failed to open file: %s\n", error->message); + g_error_free (error); + return -2; + } + /* get some vitals, this will be used to read data from the mmapped file and + * feed it to appsrc. */ + app->length = g_mapped_file_get_length (app->file); + app->data = (guint8 *) g_mapped_file_get_contents (app->file); + app->offset = 0; + + /* create a mainloop to get messages and to handle the idle handler that will + * feed data to appsrc. */ + app->loop = g_main_loop_new (NULL, TRUE); + + app->playbin = gst_element_factory_make ("playbin2", NULL); + g_assert (app->playbin); + + bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin)); + + /* add watch for messages */ + gst_bus_add_watch (bus, (GstBusFunc) bus_message, app); + + /* set to read from appsrc */ + g_object_set (app->playbin, "uri", "appsrc://", NULL); + + /* get notification when the source is created so that we get a handle to it + * and can configure it */ + g_signal_connect (app->playbin, "deep-notify::source", + (GCallback) found_source, app); + + /* go to playing and wait in a mainloop. */ + gst_element_set_state (app->playbin, GST_STATE_PLAYING); + + /* this mainloop is stopped when we receive an error or EOS */ + g_main_loop_run (app->loop); + + GST_DEBUG ("stopping"); + + gst_element_set_state (app->playbin, GST_STATE_NULL); + + /* free the file */ + g_mapped_file_free (app->file); + + gst_object_unref (bus); + g_main_loop_unref (app->loop); + + return 0; +} diff --git a/tests/examples/app/appsrc-stream2.c b/tests/examples/app/appsrc-stream2.c new file mode 100644 index 00000000..866b0504 --- /dev/null +++ b/tests/examples/app/appsrc-stream2.c @@ -0,0 +1,219 @@ +/* GStreamer + * + * appsrc-stream2.c: example for using appsrc in streaming mode. + * + * Copyright (C) 2008 Wim Taymans <wim.taymans@gmail.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., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gst/gst.h> + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +GST_DEBUG_CATEGORY (appsrc_playbin_debug); +#define GST_CAT_DEFAULT appsrc_playbin_debug + +/* + * an example application of using appsrc in streaming pull mode. When the + * appsrc request data with the need-data signal, we retrieve a buffer of an + * arbitrary size and push it to appsrc. + * + * This example keeps the internal buffer queue of appsrc to a minimal size, + * only feeding data to appsrc when needed. + * + * This is a good example how one would deal with a live resource, such as a udp + * socket where one would feed the most recently acquired buffer to appsrc. + * + * Usually one would timestamp the buffers with the running_time of the + * pipeline or configure the appsrc to do timestamping by setting the + * do-timestamp property to TRUE. + * + * Appsrc in streaming mode (the default) does not support seeking so we don't + * have to handle any seek callbacks. + * + * Some formats are able to estimate the duration of the media file based on the + * file length (mp3, mpeg,..), others report an unknown length (ogg,..). + */ +typedef struct _App App; + +struct _App +{ + GstElement *playbin; + GstElement *appsrc; + + GMainLoop *loop; + + GMappedFile *file; + guint8 *data; + gsize length; + guint64 offset; +}; + +App s_app; + +#define CHUNK_SIZE 4096 + +/* This method is called by the need-data signal callback, we feed data into the + * appsrc. + */ +static void +feed_data (GstElement * appsrc, guint size, App * app) +{ + GstBuffer *buffer; + guint len; + GstFlowReturn ret; + + buffer = gst_buffer_new (); + + if (app->offset >= app->length) { + /* we are EOS, send end-of-stream */ + g_signal_emit_by_name (app->appsrc, "end-of-stream", &ret); + return; + } + + /* read the next chunk */ + len = CHUNK_SIZE; + if (app->offset + len > app->length) + len = app->length - app->offset; + + GST_BUFFER_DATA (buffer) = app->data + app->offset; + GST_BUFFER_SIZE (buffer) = len; + + GST_DEBUG ("feed buffer %p, offset %" G_GUINT64_FORMAT "-%u", buffer, + app->offset, len); + g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret); + gst_buffer_unref (buffer); + + app->offset += len; + + return; +} + +/* this callback is called when playbin2 has constructed a source object to read + * from. Since we provided the appsrc:// uri to playbin2, this will be the + * appsrc that we must handle. We set up a signals to push data into appsrc. */ +static void +found_source (GObject * object, GObject * orig, GParamSpec * pspec, App * app) +{ + /* get a handle to the appsrc */ + g_object_get (orig, pspec->name, &app->appsrc, NULL); + + GST_DEBUG ("got appsrc %p", app->appsrc); + + /* we can set the length in appsrc. This allows some elements to estimate the + * total duration of the stream. It's a good idea to set the property when you + * can but it's not required. */ + g_object_set (app->appsrc, "size", app->length, NULL); + + /* configure the appsrc, we will push a buffer to appsrc when it needs more + * data */ + g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data), app); +} + +static gboolean +bus_message (GstBus * bus, GstMessage * message, App * app) +{ + GST_DEBUG ("got message %s", + gst_message_type_get_name (GST_MESSAGE_TYPE (message))); + + switch (GST_MESSAGE_TYPE (message)) { + case GST_MESSAGE_ERROR: + g_error ("received error"); + g_main_loop_quit (app->loop); + break; + case GST_MESSAGE_EOS: + g_main_loop_quit (app->loop); + break; + default: + break; + } + return TRUE; +} + +int +main (int argc, char *argv[]) +{ + App *app = &s_app; + GError *error = NULL; + GstBus *bus; + + gst_init (&argc, &argv); + + GST_DEBUG_CATEGORY_INIT (appsrc_playbin_debug, "appsrc-playbin", 0, + "appsrc playbin example"); + + if (argc < 2) { + g_print ("usage: %s <filename>\n", argv[0]); + return -1; + } + + /* try to open the file as an mmapped file */ + app->file = g_mapped_file_new (argv[1], FALSE, &error); + if (error) { + g_print ("failed to open file: %s\n", error->message); + g_error_free (error); + return -2; + } + /* get some vitals, this will be used to read data from the mmapped file and + * feed it to appsrc. */ + app->length = g_mapped_file_get_length (app->file); + app->data = (guint8 *) g_mapped_file_get_contents (app->file); + app->offset = 0; + + /* create a mainloop to get messages */ + app->loop = g_main_loop_new (NULL, TRUE); + + app->playbin = gst_element_factory_make ("playbin2", NULL); + g_assert (app->playbin); + + bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin)); + + /* add watch for messages */ + gst_bus_add_watch (bus, (GstBusFunc) bus_message, app); + + /* set to read from appsrc */ + g_object_set (app->playbin, "uri", "appsrc://", NULL); + + /* get notification when the source is created so that we get a handle to it + * and can configure it */ + g_signal_connect (app->playbin, "deep-notify::source", + (GCallback) found_source, app); + + /* go to playing and wait in a mainloop. */ + gst_element_set_state (app->playbin, GST_STATE_PLAYING); + + /* this mainloop is stopped when we receive an error or EOS */ + g_main_loop_run (app->loop); + + GST_DEBUG ("stopping"); + + gst_element_set_state (app->playbin, GST_STATE_NULL); + + /* free the file */ + g_mapped_file_free (app->file); + + gst_object_unref (bus); + g_main_loop_unref (app->loop); + + return 0; +} diff --git a/tests/examples/app/appsrc_ex.c b/tests/examples/app/appsrc_ex.c new file mode 100644 index 00000000..bc629fb0 --- /dev/null +++ b/tests/examples/app/appsrc_ex.c @@ -0,0 +1,89 @@ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gst/gst.h> +#include <gst/app/gstappsrc.h> +#include <gst/app/gstappbuffer.h> +#include <gst/app/gstappsink.h> + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + + +typedef struct _App App; +struct _App +{ + GstElement *pipe; + GstElement *src; + GstElement *id; + GstElement *sink; +}; + +App s_app; + +static void dont_eat_my_chicken_wings (void *priv); + +int +main (int argc, char *argv[]) +{ + App *app = &s_app; + int i; + + gst_init (&argc, &argv); + + app->pipe = gst_pipeline_new (NULL); + g_assert (app->pipe); + + app->src = gst_element_factory_make ("appsrc", NULL); + g_assert (app->src); + gst_bin_add (GST_BIN (app->pipe), app->src); + + app->id = gst_element_factory_make ("identity", NULL); + g_assert (app->id); + gst_bin_add (GST_BIN (app->pipe), app->id); + + app->sink = gst_element_factory_make ("appsink", NULL); + g_assert (app->sink); + gst_bin_add (GST_BIN (app->pipe), app->sink); + + gst_element_link (app->src, app->id); + gst_element_link (app->id, app->sink); + + gst_element_set_state (app->pipe, GST_STATE_PLAYING); + + for (i = 0; i < 10; i++) { + GstBuffer *buf; + void *data; + + data = malloc (100); + memset (data, i, 100); + + buf = gst_app_buffer_new (data, 100, dont_eat_my_chicken_wings, data); + printf ("%d: creating buffer for pointer %p, %p\n", i, data, buf); + gst_app_src_push_buffer (GST_APP_SRC (app->src), buf); + } + + gst_app_src_end_of_stream (GST_APP_SRC (app->src)); + + while (!gst_app_sink_is_eos (GST_APP_SINK (app->sink))) { + GstBuffer *buf; + + buf = gst_app_sink_pull_buffer (GST_APP_SINK (app->sink)); + printf ("retrieved buffer %p\n", buf); + gst_buffer_unref (buf); + } + gst_element_set_state (app->pipe, GST_STATE_NULL); + + return 0; +} + +static void +dont_eat_my_chicken_wings (void *priv) +{ + printf ("freeing buffer for pointer %p\n", priv); + free (priv); +} |