Blame tests/examples/app/appsrc-seekable.c

Packit 971217
/* GStreamer
Packit 971217
 *
Packit 971217
 * appsrc-seekable.c: example for using appsrc in seekable 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 seekable mode. When the
Packit 971217
 * appsrc requests data with the need-data signal, we retrieve a buffer and
Packit 971217
 * push it to appsrc. We can also use the method as demonstrated in
Packit 971217
 * appsrc-stream.c, ie. pushing buffers when we can.
Packit 971217
 *
Packit 971217
 * This is a good example how one would deal with a remote http server that
Packit 971217
 * supports range requests.
Packit 971217
 *
Packit 971217
 * Appsrc in seekable mode needs seeking support and we must thus connect
Packit 971217
 * to the seek signal to perform any seeks when requested.
Packit 971217
 *
Packit 971217
 * In seekable mode we should set the size of the source material.
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
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 need-data signal callback, we feed data into the
Packit 971217
 * appsrc with an arbitrary size.
Packit 971217
 */
Packit 971217
static void
Packit 971217
feed_data (GstElement * appsrc, guint size, 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 */
Packit 971217
    g_signal_emit_by_name (app->appsrc, "end-of-stream", &ret;;
Packit 971217
    return;
Packit 971217
  }
Packit 971217
Packit 971217
  /* read any amount of data, we are allowed to return less if we are EOS */
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
Packit 971217
  app->offset += len;
Packit 971217
Packit 971217
  return;
Packit 971217
}
Packit 971217
Packit 971217
/* called when appsrc wants us to return data from a new position with the next
Packit 971217
 * call to push-buffer. */
Packit 971217
static gboolean
Packit 971217
seek_data (GstElement * appsrc, guint64 position, App * app)
Packit 971217
{
Packit 971217
  GST_DEBUG ("seek to offset %" G_GUINT64_FORMAT, position);
Packit 971217
  app->offset = position;
Packit 971217
Packit 971217
  return TRUE;
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 push data into appsrc
Packit 971217
 * and one to perform a seek. */
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
  /* we are seekable in push mode, this means that the element usually pushes
Packit 971217
   * out buffers of an undefined size and that seeks happen only occasionally
Packit 971217
   * and only by request of the user. */
Packit 971217
  gst_util_set_object_arg (G_OBJECT (app->appsrc), "stream-type", "seekable");
Packit 971217
Packit 971217
  /* configure the appsrc, we will push a buffer to appsrc when it needs more
Packit 971217
   * data */
Packit 971217
  g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data), app);
Packit 971217
  g_signal_connect (app->appsrc, "seek-data", G_CALLBACK (seek_data), 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 */
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
}