mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-11-27 12:11:13 +00:00
examples/app/: Added 3 more example application for using appsrc in random-access mode, pull-mode streaming and pull ...
Original commit message from CVS: * examples/app/Makefile.am: * examples/app/appsrc-ra.c: (feed_data), (seek_data), (found_source), (bus_message), (main): * examples/app/appsrc-seekable.c: (feed_data), (seek_data), (found_source), (bus_message), (main): * examples/app/appsrc-stream2.c: (feed_data), (found_source), (bus_message), (main): Added 3 more example application for using appsrc in random-access mode, pull-mode streaming and pull mode seekable. * gst-libs/gst/app/gstappsrc.c: (gst_app_src_class_init), (gst_app_src_start), (gst_app_src_do_get_size), (gst_app_src_create): * gst-libs/gst/app/gstappsrc.h: Make stream-type property writable. Unset flushing when starting so that we reuse appsrc. Inform basesrc about the configured size. Emit seek-data signal when we are going to a different offset in random-access mode.
This commit is contained in:
parent
23692b06a0
commit
1ec82dec72
7 changed files with 782 additions and 16 deletions
22
ChangeLog
22
ChangeLog
|
@ -1,3 +1,25 @@
|
|||
2008-06-06 Wim Taymans <wim.taymans@collabora.co.uk>
|
||||
|
||||
* examples/app/Makefile.am:
|
||||
* examples/app/appsrc-ra.c: (feed_data), (seek_data),
|
||||
(found_source), (bus_message), (main):
|
||||
* examples/app/appsrc-seekable.c: (feed_data), (seek_data),
|
||||
(found_source), (bus_message), (main):
|
||||
* examples/app/appsrc-stream2.c: (feed_data), (found_source),
|
||||
(bus_message), (main):
|
||||
Added 3 more example application for using appsrc in random-access mode,
|
||||
pull-mode streaming and pull mode seekable.
|
||||
|
||||
* gst-libs/gst/app/gstappsrc.c: (gst_app_src_class_init),
|
||||
(gst_app_src_start), (gst_app_src_do_get_size),
|
||||
(gst_app_src_create):
|
||||
* gst-libs/gst/app/gstappsrc.h:
|
||||
Make stream-type property writable.
|
||||
Unset flushing when starting so that we reuse appsrc.
|
||||
Inform basesrc about the configured size.
|
||||
Emit seek-data signal when we are going to a different offset in
|
||||
random-access mode.
|
||||
|
||||
2008-06-06 Wim Taymans <wim.taymans@collabora.co.uk>
|
||||
|
||||
* examples/app/appsrc-stream.c: (found_source), (main):
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
noinst_PROGRAMS = appsrc_ex appsrc-stream
|
||||
noinst_PROGRAMS = appsrc_ex appsrc-stream appsrc-stream2 appsrc-ra \
|
||||
appsrc-seekable
|
||||
|
||||
appsrc_ex_SOURCES = appsrc_ex.c
|
||||
appsrc_ex_CFLAGS = $(GST_CFLAGS) $(GCONF_CFLAGS)
|
||||
|
@ -10,3 +11,15 @@ appsrc_ex_LDFLAGS = \
|
|||
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)
|
||||
|
|
222
examples/app/appsrc-ra.c
Normal file
222
examples/app/appsrc-ra.c
Normal file
|
@ -0,0 +1,222 @@
|
|||
/* 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);
|
||||
|
||||
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;
|
||||
}
|
228
examples/app/appsrc-seekable.c
Normal file
228
examples/app/appsrc-seekable.c
Normal file
|
@ -0,0 +1,228 @@
|
|||
/* 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);
|
||||
|
||||
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;
|
||||
}
|
218
examples/app/appsrc-stream2.c
Normal file
218
examples/app/appsrc-stream2.c
Normal file
|
@ -0,0 +1,218 @@
|
|||
/* 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);
|
||||
|
||||
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;
|
||||
}
|
|
@ -115,6 +115,7 @@ static gboolean gst_app_src_unlock_stop (GstBaseSrc * bsrc);
|
|||
static gboolean gst_app_src_do_seek (GstBaseSrc * src, GstSegment * segment);
|
||||
static gboolean gst_app_src_is_seekable (GstBaseSrc * src);
|
||||
static gboolean gst_app_src_check_get_range (GstBaseSrc * src);
|
||||
static gboolean gst_app_src_do_get_size (GstBaseSrc * src, guint64 * size);
|
||||
|
||||
static guint gst_app_src_signals[LAST_SIGNAL] = { 0 };
|
||||
|
||||
|
@ -177,7 +178,8 @@ gst_app_src_class_init (GstAppSrcClass * klass)
|
|||
g_object_class_install_property (gobject_class, PROP_STREAM_TYPE,
|
||||
g_param_spec_enum ("stream-type", "Stream Type",
|
||||
"the type of the stream", GST_TYPE_APP_STREAM_TYPE,
|
||||
DEFAULT_PROP_STREAM_TYPE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
|
||||
DEFAULT_PROP_STREAM_TYPE,
|
||||
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class, PROP_MAX_BYTES,
|
||||
g_param_spec_uint64 ("max-bytes", "Max bytes",
|
||||
|
@ -268,6 +270,7 @@ gst_app_src_class_init (GstAppSrcClass * klass)
|
|||
basesrc_class->do_seek = gst_app_src_do_seek;
|
||||
basesrc_class->is_seekable = gst_app_src_is_seekable;
|
||||
basesrc_class->check_get_range = gst_app_src_check_get_range;
|
||||
basesrc_class->get_size = gst_app_src_do_get_size;
|
||||
|
||||
klass->push_buffer = gst_app_src_push_buffer;
|
||||
klass->end_of_stream = gst_app_src_end_of_stream;
|
||||
|
@ -421,6 +424,10 @@ gst_app_src_start (GstBaseSrc * bsrc)
|
|||
g_mutex_lock (appsrc->mutex);
|
||||
GST_DEBUG_OBJECT (appsrc, "starting");
|
||||
appsrc->started = TRUE;
|
||||
/* set the offset to -1 so that we always do a first seek. This is only used
|
||||
* in random-access mode. */
|
||||
appsrc->offset = -1;
|
||||
appsrc->flushing = FALSE;
|
||||
g_mutex_unlock (appsrc->mutex);
|
||||
|
||||
gst_base_src_set_format (bsrc, appsrc->format);
|
||||
|
@ -478,6 +485,16 @@ gst_app_src_check_get_range (GstBaseSrc * src)
|
|||
return res;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_app_src_do_get_size (GstBaseSrc * src, guint64 * size)
|
||||
{
|
||||
GstAppSrc *appsrc = GST_APP_SRC (src);
|
||||
|
||||
*size = gst_app_src_get_size (appsrc);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* will be called in push mode */
|
||||
static gboolean
|
||||
gst_app_src_do_seek (GstBaseSrc * src, GstSegment * segment)
|
||||
|
@ -516,45 +533,84 @@ gst_app_src_create (GstBaseSrc * bsrc, guint64 offset, guint size,
|
|||
GstFlowReturn ret;
|
||||
|
||||
g_mutex_lock (appsrc->mutex);
|
||||
while (TRUE) {
|
||||
/* check flushing first */
|
||||
if (appsrc->flushing)
|
||||
goto flushing;
|
||||
/* check flushing first */
|
||||
if (G_UNLIKELY (appsrc->flushing))
|
||||
goto flushing;
|
||||
|
||||
if (appsrc->stream_type == GST_APP_STREAM_TYPE_RANDOM_ACCESS) {
|
||||
/* if we are dealing with a random-access stream, issue a seek if the offset
|
||||
* changed. */
|
||||
if (G_UNLIKELY (appsrc->offset != offset)) {
|
||||
gboolean res;
|
||||
|
||||
g_mutex_unlock (appsrc->mutex);
|
||||
|
||||
GST_DEBUG_OBJECT (appsrc,
|
||||
"we are at %" G_GINT64_FORMAT ", seek to %" G_GINT64_FORMAT,
|
||||
appsrc->offset, offset);
|
||||
|
||||
g_signal_emit (appsrc, gst_app_src_signals[SIGNAL_SEEK_DATA], 0,
|
||||
offset, &res);
|
||||
|
||||
if (G_UNLIKELY (!res))
|
||||
/* failing to seek is fatal */
|
||||
goto seek_error;
|
||||
|
||||
g_mutex_lock (appsrc->mutex);
|
||||
|
||||
appsrc->offset = offset;
|
||||
}
|
||||
}
|
||||
|
||||
while (TRUE) {
|
||||
/* return data as long as we have some */
|
||||
if (!g_queue_is_empty (appsrc->queue)) {
|
||||
again:
|
||||
*buf = g_queue_pop_head (appsrc->queue);
|
||||
guint buf_size;
|
||||
|
||||
appsrc->queued_bytes -= GST_BUFFER_SIZE (*buf);
|
||||
*buf = g_queue_pop_head (appsrc->queue);
|
||||
buf_size = GST_BUFFER_SIZE (*buf);
|
||||
|
||||
GST_DEBUG_OBJECT (appsrc, "we have buffer %p of size %u", *buf, buf_size);
|
||||
|
||||
appsrc->queued_bytes -= buf_size;
|
||||
|
||||
/* only update the offset when in random_access mode */
|
||||
if (appsrc->stream_type == GST_APP_STREAM_TYPE_RANDOM_ACCESS) {
|
||||
appsrc->offset += buf_size;
|
||||
}
|
||||
|
||||
gst_buffer_set_caps (*buf, appsrc->caps);
|
||||
|
||||
GST_DEBUG_OBJECT (appsrc, "we have buffer %p", *buf);
|
||||
ret = GST_FLOW_OK;
|
||||
break;
|
||||
} else {
|
||||
/* we have no data, we need some. We fire the signal with the size hint. */
|
||||
g_mutex_unlock (appsrc->mutex);
|
||||
|
||||
/* we have no data, we need some. We fire the signal with the size hint. */
|
||||
g_signal_emit (appsrc, gst_app_src_signals[SIGNAL_NEED_DATA], 0, size,
|
||||
NULL);
|
||||
|
||||
g_mutex_lock (appsrc->mutex);
|
||||
/* we can be flushing now because we released the lock */
|
||||
if (appsrc->flushing)
|
||||
if (G_UNLIKELY (appsrc->flushing))
|
||||
goto flushing;
|
||||
|
||||
/* if we have a buffer now, retry to return it */
|
||||
/* if we have a buffer now, continue the loop and try to return it. In
|
||||
* random-access mode (where a buffer is normally pushed in the above
|
||||
* signal) we can still be empty because the pushed buffer got flushed or
|
||||
* when the application pushes the requested buffer later, we support both
|
||||
* possiblities. */
|
||||
if (!g_queue_is_empty (appsrc->queue))
|
||||
goto again;
|
||||
continue;
|
||||
|
||||
/* no buffer yet, maybe we are EOS, if not, block for more data. */
|
||||
}
|
||||
|
||||
/* check EOS */
|
||||
if (appsrc->is_eos)
|
||||
if (G_UNLIKELY (appsrc->is_eos))
|
||||
goto eos;
|
||||
|
||||
/* nothing to return, wait a while for new data or flushing */
|
||||
/* nothing to return, wait a while for new data or flushing. */
|
||||
g_cond_wait (appsrc->cond, appsrc->mutex);
|
||||
}
|
||||
g_mutex_unlock (appsrc->mutex);
|
||||
|
@ -574,6 +630,12 @@ eos:
|
|||
g_mutex_unlock (appsrc->mutex);
|
||||
return GST_FLOW_UNEXPECTED;
|
||||
}
|
||||
seek_error:
|
||||
{
|
||||
GST_ELEMENT_ERROR (appsrc, RESOURCE, READ, ("failed to seek"),
|
||||
GST_ERROR_SYSTEM);
|
||||
return GST_FLOW_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/* external API */
|
||||
|
|
|
@ -76,6 +76,7 @@ struct _GstAppSrc
|
|||
gboolean started;
|
||||
gboolean is_eos;
|
||||
guint64 queued_bytes;
|
||||
guint64 offset;
|
||||
GstAppStreamType current_type;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue