|
Packit |
971217 |
/* GStreamer
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* appsrc-stream.c: example for using appsrc in streaming mode.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* Copyright (C) 2008 Wim Taymans <wim.taymans@gmail.com>
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* This library is free software; you can redistribute it and/or
|
|
Packit |
971217 |
* modify it under the terms of the GNU Library General Public
|
|
Packit |
971217 |
* License as published by the Free Software Foundation; either
|
|
Packit |
971217 |
* version 2 of the License, or (at your option) any later version.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
971217 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
971217 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
971217 |
* Library General Public License for more details.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* You should have received a copy of the GNU Library General Public
|
|
Packit |
971217 |
* License along with this library; if not, write to the
|
|
Packit |
971217 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
Packit |
971217 |
* Boston, MA 02110-1301, USA.
|
|
Packit |
971217 |
*/
|
|
Packit |
971217 |
|
|
Packit |
971217 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
971217 |
#include "config.h"
|
|
Packit |
971217 |
#endif
|
|
Packit |
971217 |
|
|
Packit |
971217 |
#include <gst/gst.h>
|
|
Packit |
971217 |
|
|
Packit |
971217 |
#include <stdio.h>
|
|
Packit |
971217 |
#include <string.h>
|
|
Packit |
971217 |
#include <stdlib.h>
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_DEBUG_CATEGORY (appsrc_playbin_debug);
|
|
Packit |
971217 |
#define GST_CAT_DEFAULT appsrc_playbin_debug
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/*
|
|
Packit |
971217 |
* an example application of using appsrc in streaming push mode. We simply push
|
|
Packit |
971217 |
* buffers into appsrc. The size of the buffers we push can be any size we
|
|
Packit |
971217 |
* choose.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* This example is very close to how one would deal with a streaming webserver
|
|
Packit |
971217 |
* that does not support range requests or does not report the total file size.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* Some optimisations are done so that we don't push too much data. We connect
|
|
Packit |
971217 |
* to the need-data and enough-data signals to start/stop sending buffers.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* Appsrc in streaming mode (the default) does not support seeking so we don't
|
|
Packit |
971217 |
* have to handle any seek callbacks.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* Some formats are able to estimate the duration of the media file based on the
|
|
Packit |
971217 |
* file length (mp3, mpeg,..), others report an unknown length (ogg,..).
|
|
Packit |
971217 |
*/
|
|
Packit |
971217 |
typedef struct _App App;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
struct _App
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
GstElement *playbin;
|
|
Packit |
971217 |
GstElement *appsrc;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GMainLoop *loop;
|
|
Packit |
971217 |
guint sourceid;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GMappedFile *file;
|
|
Packit |
971217 |
guint8 *data;
|
|
Packit |
971217 |
gsize length;
|
|
Packit |
971217 |
guint64 offset;
|
|
Packit |
971217 |
};
|
|
Packit |
971217 |
|
|
Packit |
971217 |
App s_app;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
#define CHUNK_SIZE 4096
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* This method is called by the idle GSource in the mainloop. We feed CHUNK_SIZE
|
|
Packit |
971217 |
* bytes into appsrc.
|
|
Packit |
971217 |
* The ide handler is added to the mainloop when appsrc requests us to start
|
|
Packit |
971217 |
* sending data (need-data signal) and is removed when appsrc has enough data
|
|
Packit |
971217 |
* (enough-data signal).
|
|
Packit |
971217 |
*/
|
|
Packit |
971217 |
static gboolean
|
|
Packit |
971217 |
read_data (App * app)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
GstBuffer *buffer;
|
|
Packit |
971217 |
guint len;
|
|
Packit |
971217 |
GstFlowReturn ret;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (app->offset >= app->length) {
|
|
Packit |
971217 |
/* we are EOS, send end-of-stream and remove the source */
|
|
Packit |
971217 |
g_signal_emit_by_name (app->appsrc, "end-of-stream", &ret;;
|
|
Packit |
971217 |
return FALSE;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* read the next chunk */
|
|
Packit |
971217 |
buffer = gst_buffer_new ();
|
|
Packit |
971217 |
|
|
Packit |
971217 |
len = CHUNK_SIZE;
|
|
Packit |
971217 |
if (app->offset + len > app->length)
|
|
Packit |
971217 |
len = app->length - app->offset;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
gst_buffer_append_memory (buffer,
|
|
Packit |
971217 |
gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
|
|
Packit |
971217 |
app->data, app->length, app->offset, len, NULL, NULL));
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_DEBUG ("feed buffer %p, offset %" G_GUINT64_FORMAT "-%u", buffer,
|
|
Packit |
971217 |
app->offset, len);
|
|
Packit |
971217 |
g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret;;
|
|
Packit |
971217 |
gst_buffer_unref (buffer);
|
|
Packit |
971217 |
if (ret != GST_FLOW_OK) {
|
|
Packit |
971217 |
/* some error, stop sending data */
|
|
Packit |
971217 |
return FALSE;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
app->offset += len;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
return TRUE;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* This signal callback is called when appsrc needs data, we add an idle handler
|
|
Packit |
971217 |
* to the mainloop to start pushing data into the appsrc */
|
|
Packit |
971217 |
static void
|
|
Packit |
971217 |
start_feed (GstElement * playbin, guint size, App * app)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
if (app->sourceid == 0) {
|
|
Packit |
971217 |
GST_DEBUG ("start feeding");
|
|
Packit |
971217 |
app->sourceid = g_idle_add ((GSourceFunc) read_data, app);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* This callback is called when appsrc has enough data and we can stop sending.
|
|
Packit |
971217 |
* We remove the idle handler from the mainloop */
|
|
Packit |
971217 |
static void
|
|
Packit |
971217 |
stop_feed (GstElement * playbin, App * app)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
if (app->sourceid != 0) {
|
|
Packit |
971217 |
GST_DEBUG ("stop feeding");
|
|
Packit |
971217 |
g_source_remove (app->sourceid);
|
|
Packit |
971217 |
app->sourceid = 0;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* this callback is called when playbin has constructed a source object to read
|
|
Packit |
971217 |
* from. Since we provided the appsrc:// uri to playbin, this will be the
|
|
Packit |
971217 |
* appsrc that we must handle. We set up some signals to start and stop pushing
|
|
Packit |
971217 |
* data into appsrc */
|
|
Packit |
971217 |
static void
|
|
Packit |
971217 |
found_source (GObject * object, GObject * orig, GParamSpec * pspec, App * app)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
/* get a handle to the appsrc */
|
|
Packit |
971217 |
g_object_get (orig, pspec->name, &app->appsrc, NULL);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_DEBUG ("got appsrc %p", app->appsrc);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* we can set the length in appsrc. This allows some elements to estimate the
|
|
Packit |
971217 |
* total duration of the stream. It's a good idea to set the property when you
|
|
Packit |
971217 |
* can but it's not required. */
|
|
Packit |
971217 |
g_object_set (app->appsrc, "size", (gint64) app->length, NULL);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* configure the appsrc, we will push data into the appsrc from the
|
|
Packit |
971217 |
* mainloop. */
|
|
Packit |
971217 |
g_signal_connect (app->appsrc, "need-data", G_CALLBACK (start_feed), app);
|
|
Packit |
971217 |
g_signal_connect (app->appsrc, "enough-data", G_CALLBACK (stop_feed), app);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static gboolean
|
|
Packit |
971217 |
bus_message (GstBus * bus, GstMessage * message, App * app)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
GST_DEBUG ("got message %s",
|
|
Packit |
971217 |
gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
|
|
Packit |
971217 |
|
|
Packit |
971217 |
switch (GST_MESSAGE_TYPE (message)) {
|
|
Packit |
971217 |
case GST_MESSAGE_ERROR:
|
|
Packit |
971217 |
g_error ("received error");
|
|
Packit |
971217 |
g_main_loop_quit (app->loop);
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
case GST_MESSAGE_EOS:
|
|
Packit |
971217 |
g_main_loop_quit (app->loop);
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
default:
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
return TRUE;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
int
|
|
Packit |
971217 |
main (int argc, char *argv[])
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
App *app = &s_app;
|
|
Packit |
971217 |
GError *error = NULL;
|
|
Packit |
971217 |
GstBus *bus;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
gst_init (&argc, &argv);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_DEBUG_CATEGORY_INIT (appsrc_playbin_debug, "appsrc-playbin", 0,
|
|
Packit |
971217 |
"appsrc playbin example");
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (argc < 2) {
|
|
Packit |
971217 |
g_print ("usage: %s <filename>\n", argv[0]);
|
|
Packit |
971217 |
return -1;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* try to open the file as an mmapped file */
|
|
Packit |
971217 |
app->file = g_mapped_file_new (argv[1], FALSE, &error);
|
|
Packit |
971217 |
if (error) {
|
|
Packit |
971217 |
g_print ("failed to open file: %s\n", error->message);
|
|
Packit |
971217 |
g_error_free (error);
|
|
Packit |
971217 |
return -2;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
/* get some vitals, this will be used to read data from the mmapped file and
|
|
Packit |
971217 |
* feed it to appsrc. */
|
|
Packit |
971217 |
app->length = g_mapped_file_get_length (app->file);
|
|
Packit |
971217 |
app->data = (guint8 *) g_mapped_file_get_contents (app->file);
|
|
Packit |
971217 |
app->offset = 0;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* create a mainloop to get messages and to handle the idle handler that will
|
|
Packit |
971217 |
* feed data to appsrc. */
|
|
Packit |
971217 |
app->loop = g_main_loop_new (NULL, TRUE);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
app->playbin = gst_element_factory_make ("playbin", NULL);
|
|
Packit |
971217 |
g_assert (app->playbin);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin));
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* add watch for messages */
|
|
Packit |
971217 |
gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* set to read from appsrc */
|
|
Packit |
971217 |
g_object_set (app->playbin, "uri", "appsrc://", NULL);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* get notification when the source is created so that we get a handle to it
|
|
Packit |
971217 |
* and can configure it */
|
|
Packit |
971217 |
g_signal_connect (app->playbin, "deep-notify::source",
|
|
Packit |
971217 |
(GCallback) found_source, app);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* go to playing and wait in a mainloop. */
|
|
Packit |
971217 |
gst_element_set_state (app->playbin, GST_STATE_PLAYING);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* this mainloop is stopped when we receive an error or EOS */
|
|
Packit |
971217 |
g_main_loop_run (app->loop);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_DEBUG ("stopping");
|
|
Packit |
971217 |
|
|
Packit |
971217 |
gst_element_set_state (app->playbin, GST_STATE_NULL);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* free the file */
|
|
Packit |
971217 |
g_mapped_file_unref (app->file);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
gst_object_unref (bus);
|
|
Packit |
971217 |
g_main_loop_unref (app->loop);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
return 0;
|
|
Packit |
971217 |
}
|