2001-03-24 17:22:03 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2000 Wim Taymans <wtay@chello.be>
|
|
|
|
* 2001 Dominic Ludlam <dom@recoil.org>
|
|
|
|
*
|
2004-02-11 18:49:26 +00:00
|
|
|
* gstmultifilesrc.c:
|
2001-03-24 17:22:03 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
2004-01-18 21:36:20 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2003-06-29 14:05:49 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-01-18 21:36:20 +00:00
|
|
|
#include "../gst-i18n-lib.h"
|
|
|
|
|
2004-02-11 18:49:26 +00:00
|
|
|
#include "gstmultifilesrc.h"
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-02-11 18:49:26 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_multifilesrc_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_multifilesrc_debug
|
2003-06-29 14:05:49 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
GstElementDetails gst_multifilesrc_details =
|
|
|
|
GST_ELEMENT_DETAILS ("Multi File Source",
|
|
|
|
"Source/File",
|
|
|
|
"Read from multiple files in order",
|
|
|
|
"Dominic Ludlam <dom@openfx.org>");
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-02-11 18:49:26 +00:00
|
|
|
/* FileSrc signals and args */
|
2004-03-13 15:27:01 +00:00
|
|
|
enum
|
|
|
|
{
|
2001-03-24 17:22:03 +00:00
|
|
|
NEW_FILE,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
enum
|
|
|
|
{
|
2001-03-24 17:22:03 +00:00
|
|
|
ARG_0,
|
2004-01-30 20:48:13 +00:00
|
|
|
ARG_LOCATIONS
|
2001-03-24 17:22:03 +00:00
|
|
|
};
|
|
|
|
|
2004-01-08 04:10:18 +00:00
|
|
|
#define _do_init(bla) \
|
2004-02-11 18:49:26 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_multifilesrc_debug, "multifilesrc", 0, "multifilesrc element");
|
2004-01-08 04:10:18 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_BOILERPLATE_FULL (GstMultiFileSrc, gst_multifilesrc, GstElement,
|
|
|
|
GST_TYPE_ELEMENT, _do_init);
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static void gst_multifilesrc_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_multifilesrc_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
|
|
|
|
static GstData *gst_multifilesrc_get (GstPad * pad);
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-02-11 18:49:26 +00:00
|
|
|
/*static GstBuffer * gst_multifilesrc_get_region (GstPad *pad,GstRegionType type,guint64 offset,guint64 len);*/
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static GstElementStateReturn gst_multifilesrc_change_state (GstElement *
|
|
|
|
element);
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static gboolean gst_multifilesrc_open_file (GstMultiFileSrc * src,
|
|
|
|
GstPad * srcpad);
|
|
|
|
static void gst_multifilesrc_close_file (GstMultiFileSrc * src);
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-02-11 18:49:26 +00:00
|
|
|
static guint gst_multifilesrc_signals[LAST_SIGNAL] = { 0 };
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2003-10-31 19:32:47 +00:00
|
|
|
static void
|
2004-02-11 18:49:26 +00:00
|
|
|
gst_multifilesrc_base_init (gpointer g_class)
|
2003-10-31 19:32:47 +00:00
|
|
|
{
|
|
|
|
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2004-02-11 18:49:26 +00:00
|
|
|
gst_element_class_set_details (gstelement_class, &gst_multifilesrc_details);
|
2003-10-31 19:32:47 +00:00
|
|
|
}
|
2001-03-24 17:22:03 +00:00
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_multifilesrc_class_init (GstMultiFileSrcClass * klass)
|
2001-03-24 17:22:03 +00:00
|
|
|
{
|
2001-06-25 01:20:11 +00:00
|
|
|
GObjectClass *gobject_class;
|
2001-03-24 17:22:03 +00:00
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
2001-03-24 17:22:03 +00:00
|
|
|
|
|
|
|
|
2004-02-11 18:49:26 +00:00
|
|
|
gst_multifilesrc_signals[NEW_FILE] =
|
2004-03-13 15:27:01 +00:00
|
|
|
g_signal_new ("new-file", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GstMultiFileSrcClass, new_file), NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-03-15 19:27:17 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATIONS, g_param_spec_pointer ("locations", "locations", "locations", G_PARAM_READWRITE)); /* CHECKME */
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-02-11 18:49:26 +00:00
|
|
|
gobject_class->set_property = gst_multifilesrc_set_property;
|
|
|
|
gobject_class->get_property = gst_multifilesrc_get_property;
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-02-11 18:49:26 +00:00
|
|
|
gstelement_class->change_state = gst_multifilesrc_change_state;
|
2001-03-24 17:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_multifilesrc_init (GstMultiFileSrc * multifilesrc)
|
2001-03-24 17:22:03 +00:00
|
|
|
{
|
2004-02-11 18:49:26 +00:00
|
|
|
/* GST_FLAG_SET (filesrc, GST_SRC_); */
|
|
|
|
|
|
|
|
multifilesrc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_pad_set_get_function (multifilesrc->srcpad, gst_multifilesrc_get);
|
2004-02-11 18:49:26 +00:00
|
|
|
/* gst_pad_set_getregion_function (multifilesrc->srcpad,gst_multifilesrc_get_region); */
|
|
|
|
gst_element_add_pad (GST_ELEMENT (multifilesrc), multifilesrc->srcpad);
|
|
|
|
|
|
|
|
multifilesrc->listptr = NULL;
|
|
|
|
multifilesrc->currentfilename = NULL;
|
|
|
|
multifilesrc->fd = 0;
|
|
|
|
multifilesrc->size = 0;
|
|
|
|
multifilesrc->map = NULL;
|
|
|
|
multifilesrc->new_seek = FALSE;
|
2001-03-24 17:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_multifilesrc_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
2001-03-24 17:22:03 +00:00
|
|
|
{
|
2004-02-11 18:49:26 +00:00
|
|
|
GstMultiFileSrc *src;
|
2001-03-24 17:22:03 +00:00
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
2004-02-11 18:49:26 +00:00
|
|
|
g_return_if_fail (GST_IS_MULTIFILESRC (object));
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-02-11 18:49:26 +00:00
|
|
|
src = GST_MULTIFILESRC (object);
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2001-06-25 01:20:11 +00:00
|
|
|
switch (prop_id) {
|
2001-03-24 17:22:03 +00:00
|
|
|
case ARG_LOCATIONS:
|
|
|
|
/* the element must be stopped in order to do this */
|
|
|
|
g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
|
|
|
|
|
|
|
|
/* clear the filename if we get a NULL */
|
2001-06-25 01:20:11 +00:00
|
|
|
if (g_value_get_pointer (value) == NULL) {
|
2004-03-15 19:27:17 +00:00
|
|
|
gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
|
|
|
|
src->listptr = NULL;
|
|
|
|
/* otherwise set the new filenames */
|
2001-03-24 17:22:03 +00:00
|
|
|
} else {
|
2004-03-15 19:27:17 +00:00
|
|
|
src->listptr = g_value_get_pointer (value);
|
2001-03-24 17:22:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_multifilesrc_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
2001-03-24 17:22:03 +00:00
|
|
|
{
|
2004-02-11 18:49:26 +00:00
|
|
|
GstMultiFileSrc *src;
|
2001-03-24 17:22:03 +00:00
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
2004-02-11 18:49:26 +00:00
|
|
|
g_return_if_fail (GST_IS_MULTIFILESRC (object));
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-02-11 18:49:26 +00:00
|
|
|
src = GST_MULTIFILESRC (object);
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2001-06-25 01:20:11 +00:00
|
|
|
switch (prop_id) {
|
2001-03-24 17:22:03 +00:00
|
|
|
case ARG_LOCATIONS:
|
2001-06-25 01:20:11 +00:00
|
|
|
g_value_set_pointer (value, src->listptr);
|
2001-03-24 17:22:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
2001-06-25 01:20:11 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2001-03-24 17:22:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-02-11 18:49:26 +00:00
|
|
|
* gst_filesrc_get:
|
2001-03-24 17:22:03 +00:00
|
|
|
* @pad: #GstPad to push a buffer from
|
|
|
|
*
|
2004-02-11 18:49:26 +00:00
|
|
|
* Push a new buffer from the filesrc at the current offset.
|
2001-03-24 17:22:03 +00:00
|
|
|
*/
|
2003-10-08 16:06:02 +00:00
|
|
|
static GstData *
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_multifilesrc_get (GstPad * pad)
|
2001-03-24 17:22:03 +00:00
|
|
|
{
|
2004-02-11 18:49:26 +00:00
|
|
|
GstMultiFileSrc *src;
|
2001-03-24 17:22:03 +00:00
|
|
|
GstBuffer *buf;
|
|
|
|
GSList *list;
|
|
|
|
|
|
|
|
g_return_val_if_fail (pad != NULL, NULL);
|
2004-02-11 18:49:26 +00:00
|
|
|
src = GST_MULTIFILESRC (gst_pad_get_parent (pad));
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-02-11 18:49:26 +00:00
|
|
|
if (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN))
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_multifilesrc_close_file (src);
|
2001-03-24 17:22:03 +00:00
|
|
|
|
|
|
|
if (!src->listptr) {
|
2003-10-08 16:06:02 +00:00
|
|
|
return GST_DATA (gst_event_new (GST_EVENT_EOS));
|
2001-03-24 17:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list = src->listptr;
|
|
|
|
src->currentfilename = (gchar *) list->data;
|
|
|
|
src->listptr = src->listptr->next;
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
if (!gst_multifilesrc_open_file (src, pad))
|
|
|
|
return NULL;
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
/* emitted after the open, as the user may free the list and string from here */
|
|
|
|
g_signal_emit (G_OBJECT (src), gst_multifilesrc_signals[NEW_FILE], 0, list);
|
2001-03-24 17:22:03 +00:00
|
|
|
|
|
|
|
/* create the buffer */
|
2001-12-17 18:14:51 +00:00
|
|
|
/* FIXME: should eventually use a bufferpool for this */
|
2001-03-24 17:22:03 +00:00
|
|
|
buf = gst_buffer_new ();
|
|
|
|
|
|
|
|
g_return_val_if_fail (buf != NULL, NULL);
|
|
|
|
|
|
|
|
/* simply set the buffer to point to the correct region of the file */
|
|
|
|
GST_BUFFER_DATA (buf) = src->map;
|
2003-10-07 21:56:36 +00:00
|
|
|
GST_BUFFER_SIZE (buf) = src->size;
|
2001-03-24 17:22:03 +00:00
|
|
|
GST_BUFFER_OFFSET (buf) = 0;
|
|
|
|
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_DONTFREE);
|
|
|
|
|
|
|
|
if (src->new_seek) {
|
2001-12-28 20:20:26 +00:00
|
|
|
/* fixme, do something here */
|
2001-03-24 17:22:03 +00:00
|
|
|
src->new_seek = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we're done, return the buffer */
|
2003-10-08 16:06:02 +00:00
|
|
|
return GST_DATA (buf);
|
2001-03-24 17:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* open the file and mmap it, necessary to go to READY state */
|
2004-03-13 15:27:01 +00:00
|
|
|
static gboolean
|
|
|
|
gst_multifilesrc_open_file (GstMultiFileSrc * src, GstPad * srcpad)
|
2001-03-24 17:22:03 +00:00
|
|
|
{
|
2004-02-11 18:49:26 +00:00
|
|
|
g_return_val_if_fail (!GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN), FALSE);
|
2001-03-24 17:22:03 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
if (src->currentfilename == NULL || src->currentfilename[0] == '\0') {
|
2004-02-22 14:07:55 +00:00
|
|
|
GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
|
2004-03-15 19:27:17 +00:00
|
|
|
(_("No file name specified for reading.")), (NULL));
|
2004-02-22 14:07:55 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-03-24 17:22:03 +00:00
|
|
|
/* open the file */
|
|
|
|
src->fd = open ((const char *) src->currentfilename, O_RDONLY);
|
|
|
|
if (src->fd < 0) {
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
|
2004-03-15 19:27:17 +00:00
|
|
|
(_("Could not open file \"%s\" for reading."), src->currentfilename),
|
|
|
|
GST_ERROR_SYSTEM);
|
2001-03-24 17:22:03 +00:00
|
|
|
return FALSE;
|
2004-01-18 21:36:20 +00:00
|
|
|
|
2001-03-24 17:22:03 +00:00
|
|
|
} else {
|
|
|
|
/* find the file length */
|
|
|
|
src->size = lseek (src->fd, 0, SEEK_END);
|
|
|
|
lseek (src->fd, 0, SEEK_SET);
|
|
|
|
/* map the file into memory */
|
|
|
|
src->map = mmap (NULL, src->size, PROT_READ, MAP_SHARED, src->fd, 0);
|
2004-03-13 15:27:01 +00:00
|
|
|
madvise (src->map, src->size, 2);
|
2001-03-24 17:22:03 +00:00
|
|
|
/* collapse state if that failed */
|
|
|
|
if (src->map == NULL) {
|
|
|
|
close (src->fd);
|
2004-01-31 19:30:31 +00:00
|
|
|
GST_ELEMENT_ERROR (src, RESOURCE, TOO_LAZY, (NULL),
|
2004-03-15 19:27:17 +00:00
|
|
|
("mmap call failed."));
|
2001-03-24 17:22:03 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2004-02-11 18:49:26 +00:00
|
|
|
GST_FLAG_SET (src, GST_MULTIFILESRC_OPEN);
|
2001-03-24 17:22:03 +00:00
|
|
|
src->new_seek = TRUE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unmap and close the file */
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_multifilesrc_close_file (GstMultiFileSrc * src)
|
2001-03-24 17:22:03 +00:00
|
|
|
{
|
2004-02-11 18:49:26 +00:00
|
|
|
g_return_if_fail (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN));
|
2001-03-24 17:22:03 +00:00
|
|
|
|
|
|
|
/* unmap the file from memory and close the file */
|
|
|
|
munmap (src->map, src->size);
|
|
|
|
close (src->fd);
|
|
|
|
|
|
|
|
/* zero out a lot of our state */
|
|
|
|
src->fd = 0;
|
|
|
|
src->size = 0;
|
|
|
|
src->map = NULL;
|
|
|
|
src->new_seek = FALSE;
|
|
|
|
|
2004-02-11 18:49:26 +00:00
|
|
|
GST_FLAG_UNSET (src, GST_MULTIFILESRC_OPEN);
|
2001-03-24 17:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstElementStateReturn
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_multifilesrc_change_state (GstElement * element)
|
2001-03-24 17:22:03 +00:00
|
|
|
{
|
2004-02-11 18:49:26 +00:00
|
|
|
g_return_val_if_fail (GST_IS_MULTIFILESRC (element), GST_STATE_FAILURE);
|
2001-03-24 17:22:03 +00:00
|
|
|
|
|
|
|
if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
|
2004-02-11 18:49:26 +00:00
|
|
|
if (GST_FLAG_IS_SET (element, GST_MULTIFILESRC_OPEN))
|
|
|
|
gst_multifilesrc_close_file (GST_MULTIFILESRC (element));
|
2001-03-24 17:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
|
|
|
return GST_ELEMENT_CLASS (parent_class)->change_state (element);
|
|
|
|
|
|
|
|
return GST_STATE_SUCCESS;
|
|
|
|
}
|