summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorWim Taymans <wim.taymans@gmail.com>2008-06-05 16:38:50 +0000
committerWim Taymans <wim.taymans@gmail.com>2008-06-05 16:38:50 +0000
commit308354073ca3a67261f6cfa096490803bf037ccd (patch)
tree3d22cda2df0c74b2be38607998f394655fa9c109 /examples
parentc6dffa0538819d2995d105e63b445359faa340d0 (diff)
downloadgst-plugins-bad-308354073ca3a67261f6cfa096490803bf037ccd.tar.gz
gst-plugins-bad-308354073ca3a67261f6cfa096490803bf037ccd.tar.bz2
gst-plugins-bad-308354073ca3a67261f6cfa096490803bf037ccd.zip
examples/app/: Added an example on how to use appsrc in playbin in streaming mode from an mmapped file.
Original commit message from CVS: * examples/app/.cvsignore: * examples/app/Makefile.am: * examples/app/appsrc-stream.c: (read_data), (start_feed), (stop_feed), (found_source), (bus_message), (main): Added an example on how to use appsrc in playbin in streaming mode from an mmapped file. * examples/app/appsrc_ex.c: (main): Set pipeline to NULL to free queued buffers. * gst-libs/gst/app/gstapp-marshal.list: * gst-libs/gst/app/gstappsrc.c: (stream_type_get_type), (_do_init), (gst_app_src_class_init), (gst_app_src_init), (gst_app_src_flush_queued), (gst_app_src_dispose), (gst_app_src_set_property), (gst_app_src_get_property), (gst_app_src_unlock), (gst_app_src_unlock_stop), (gst_app_src_start), (gst_app_src_stop), (gst_app_src_is_seekable), (gst_app_src_check_get_range), (gst_app_src_do_seek), (gst_app_src_create), (gst_app_src_set_stream_type), (gst_app_src_get_stream_type), (gst_app_src_set_max_bytes), (gst_app_src_get_max_bytes), (gst_app_src_push_buffer), (gst_app_src_end_of_stream), (gst_app_src_uri_get_type), (gst_app_src_uri_get_protocols), (gst_app_src_uri_get_uri), (gst_app_src_uri_set_uri), (gst_app_src_uri_handler_init): * gst-libs/gst/app/gstappsrc.h: Measure max queue size in bytes instead. Add support for 3 modes of operation, streaming, seekable and random-access, making basesrc handle the scheduling modes for each. Add appsrc:// uri handler so that automatic plugging can be done from playbin2 or uridecodebin, for example. Added support for custom segment formats. Add support for push and pull based operations from the application. Expand the methods so that errors can be detected. Flush the queued buffers on seeks and when shutting down. Add signals to inform the app that a seek must happen.
Diffstat (limited to 'examples')
-rw-r--r--examples/app/.gitignore1
-rw-r--r--examples/app/Makefile.am5
-rw-r--r--examples/app/appsrc-stream.c248
-rw-r--r--examples/app/appsrc_ex.c4
4 files changed, 256 insertions, 2 deletions
diff --git a/examples/app/.gitignore b/examples/app/.gitignore
index f76db7ef..27d8cc2a 100644
--- a/examples/app/.gitignore
+++ b/examples/app/.gitignore
@@ -1 +1,2 @@
appsrc_ex
+appsrc-stream
diff --git a/examples/app/Makefile.am b/examples/app/Makefile.am
index 0b950515..db1393dd 100644
--- a/examples/app/Makefile.am
+++ b/examples/app/Makefile.am
@@ -1,5 +1,5 @@
-noinst_PROGRAMS = appsrc_ex
+noinst_PROGRAMS = appsrc_ex appsrc-stream
appsrc_ex_SOURCES = appsrc_ex.c
appsrc_ex_CFLAGS = $(GST_CFLAGS) $(GCONF_CFLAGS)
@@ -7,3 +7,6 @@ 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)
diff --git a/examples/app/appsrc-stream.c b/examples/app/appsrc-stream.c
new file mode 100644
index 00000000..781adc31
--- /dev/null
+++ b/examples/app/appsrc-stream.c
@@ -0,0 +1,248 @@
+/* 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);
+ 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 * playbin, GParamSpec * pspec, App * app)
+{
+ /* get a handle to the appsrc */
+ g_object_get (playbin, 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, "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/examples/app/appsrc_ex.c b/examples/app/appsrc_ex.c
index c6f8b29e..bc629fb0 100644
--- a/examples/app/appsrc_ex.c
+++ b/examples/app/appsrc_ex.c
@@ -62,8 +62,8 @@ main (int argc, char *argv[])
data = malloc (100);
memset (data, i, 100);
- printf ("%d: creating buffer for pointer %p\n", i, data);
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);
}
@@ -73,8 +73,10 @@ main (int argc, char *argv[])
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;
}