2001-08-22 04:30:27 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2000 Wim Taymans <wtay@chello.be>
|
|
|
|
*
|
|
|
|
* gstfilesrc.c:
|
|
|
|
*
|
|
|
|
* 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 <gst/gst.h>
|
|
|
|
|
2001-12-12 06:08:44 +00:00
|
|
|
#include "gstfilesrc.h"
|
|
|
|
|
2001-12-15 22:59:24 +00:00
|
|
|
#include <stdio.h>
|
2001-08-22 04:30:27 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
2001-10-17 10:21:27 +00:00
|
|
|
#include <errno.h>
|
2001-12-22 21:18:17 +00:00
|
|
|
#include <string.h>
|
2001-08-22 04:30:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* GStreamer Default File Source
|
|
|
|
* Theory of Operation
|
|
|
|
*
|
|
|
|
* This source uses mmap(2) to efficiently load data from a file.
|
|
|
|
* To do this without seriously polluting the applications' memory
|
|
|
|
* space, it must do so in smaller chunks, say 1-4MB at a time.
|
|
|
|
* Buffers are then subdivided from these mmap'd chunks, to directly
|
|
|
|
* make use of the mmap.
|
|
|
|
*
|
|
|
|
* To handle refcounting so that the mmap can be freed at the appropriate
|
|
|
|
* time, a buffer will be created for each mmap'd region, and all new
|
|
|
|
* buffers will be sub-buffers of this top-level buffer. As they are
|
|
|
|
* freed, the refcount goes down on the mmap'd buffer and its free()
|
|
|
|
* function is called, which will call munmap(2) on itself.
|
|
|
|
*
|
|
|
|
* If a buffer happens to cross the boundaries of an mmap'd region, we
|
|
|
|
* have to decide whether it's more efficient to copy the data into a
|
|
|
|
* new buffer, or mmap() just that buffer. There will have to be a
|
|
|
|
* breakpoint size to determine which will be done. The mmap() size
|
|
|
|
* has a lot to do with this as well, because you end up in double-
|
|
|
|
* jeopardy: the larger the outgoing buffer, the more data to copy when
|
|
|
|
* it overlaps, *and* the more frequently you'll have buffers that *do*
|
|
|
|
* overlap.
|
|
|
|
*
|
|
|
|
* Seeking is another tricky aspect to do efficiently. The initial
|
|
|
|
* implementation of this source won't make use of these features, however.
|
|
|
|
* The issue is that if an application seeks backwards in a file, *and*
|
|
|
|
* that region of the file is covered by an mmap that hasn't been fully
|
|
|
|
* deallocated, we really should re-use it. But keeping track of these
|
|
|
|
* regions is tricky because we have to lock the structure that holds
|
|
|
|
* them. We need to settle on a locking primitive (GMutex seems to be
|
|
|
|
* a really good option...), then we can do that.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
GstElementDetails gst_filesrc_details = {
|
|
|
|
"File Source",
|
|
|
|
"Source/File",
|
2002-09-29 17:52:31 +00:00
|
|
|
"LGPL",
|
2001-08-22 04:30:27 +00:00
|
|
|
"Read from arbitrary point in a file",
|
|
|
|
VERSION,
|
|
|
|
"Erik Walthinsen <omega@cse.ogi.edu>",
|
|
|
|
"(C) 1999",
|
|
|
|
};
|
|
|
|
|
2002-06-22 09:23:40 +00:00
|
|
|
/* #define fs_print(format,args...) g_print(format, ## args) */
|
2001-10-17 10:21:27 +00:00
|
|
|
#define fs_print(format,args...)
|
|
|
|
|
2001-08-22 04:30:27 +00:00
|
|
|
/* FileSrc signals and args */
|
|
|
|
enum {
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_0,
|
2002-03-03 00:44:41 +00:00
|
|
|
ARG_OFFSET,
|
2001-08-22 04:30:27 +00:00
|
|
|
ARG_LOCATION,
|
2001-08-22 19:54:08 +00:00
|
|
|
ARG_FILESIZE,
|
|
|
|
ARG_FD,
|
2001-08-22 04:30:27 +00:00
|
|
|
ARG_BLOCKSIZE,
|
2001-08-22 19:54:08 +00:00
|
|
|
ARG_MAPSIZE,
|
2001-10-17 10:21:27 +00:00
|
|
|
ARG_TOUCH,
|
2001-08-22 04:30:27 +00:00
|
|
|
};
|
|
|
|
|
2002-07-30 19:19:15 +00:00
|
|
|
GST_EVENT_MASK_FUNCTION (gst_filesrc_get_event_mask,
|
|
|
|
{ GST_EVENT_SEEK, GST_SEEK_METHOD_CUR |
|
|
|
|
GST_SEEK_METHOD_SET |
|
|
|
|
GST_SEEK_METHOD_END |
|
|
|
|
GST_SEEK_FLAG_FLUSH },
|
|
|
|
{ GST_EVENT_FLUSH, 0 },
|
|
|
|
{ GST_EVENT_SIZE, 0 }
|
|
|
|
)
|
|
|
|
|
|
|
|
GST_PAD_QUERY_TYPE_FUNCTION (gst_filesrc_get_query_types,
|
|
|
|
GST_PAD_QUERY_TOTAL,
|
|
|
|
GST_PAD_QUERY_POSITION
|
|
|
|
)
|
|
|
|
|
|
|
|
GST_FORMATS_FUNCTION (gst_filesrc_get_formats,
|
|
|
|
GST_FORMAT_BYTES
|
|
|
|
)
|
|
|
|
|
2001-10-21 21:58:29 +00:00
|
|
|
static void gst_filesrc_class_init (GstFileSrcClass *klass);
|
|
|
|
static void gst_filesrc_init (GstFileSrc *filesrc);
|
2001-12-28 15:19:38 +00:00
|
|
|
static void gst_filesrc_dispose (GObject *object);
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-10-21 21:58:29 +00:00
|
|
|
static void gst_filesrc_set_property (GObject *object, guint prop_id,
|
|
|
|
const GValue *value, GParamSpec *pspec);
|
|
|
|
static void gst_filesrc_get_property (GObject *object, guint prop_id,
|
|
|
|
GValue *value, GParamSpec *pspec);
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-10-21 21:58:29 +00:00
|
|
|
static GstBuffer * gst_filesrc_get (GstPad *pad);
|
|
|
|
static gboolean gst_filesrc_srcpad_event (GstPad *pad, GstEvent *event);
|
2002-05-26 21:54:27 +00:00
|
|
|
static gboolean gst_filesrc_srcpad_query (GstPad *pad, GstPadQueryType type,
|
2002-07-28 01:06:16 +00:00
|
|
|
GstFormat *format, gint64 *value);
|
2001-08-22 04:30:27 +00:00
|
|
|
|
|
|
|
static GstElementStateReturn gst_filesrc_change_state (GstElement *element);
|
|
|
|
|
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
2001-12-17 18:14:51 +00:00
|
|
|
/*static guint gst_filesrc_signals[LAST_SIGNAL] = { 0 };*/
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-08-22 19:54:08 +00:00
|
|
|
GType
|
2001-08-22 04:30:27 +00:00
|
|
|
gst_filesrc_get_type(void)
|
|
|
|
{
|
2001-08-22 19:54:08 +00:00
|
|
|
static GType filesrc_type = 0;
|
2001-08-22 04:30:27 +00:00
|
|
|
|
|
|
|
if (!filesrc_type) {
|
2001-08-22 19:54:08 +00:00
|
|
|
static const GTypeInfo filesrc_info = {
|
|
|
|
sizeof(GstFileSrcClass), NULL,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc)gst_filesrc_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2001-08-22 04:30:27 +00:00
|
|
|
sizeof(GstFileSrc),
|
2001-08-22 19:54:08 +00:00
|
|
|
0,
|
|
|
|
(GInstanceInitFunc)gst_filesrc_init,
|
2001-08-22 04:30:27 +00:00
|
|
|
};
|
2001-08-22 19:54:08 +00:00
|
|
|
filesrc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFileSrc", &filesrc_info, 0);
|
2001-08-22 04:30:27 +00:00
|
|
|
}
|
|
|
|
return filesrc_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_filesrc_class_init (GstFileSrcClass *klass)
|
|
|
|
{
|
2001-08-22 19:54:08 +00:00
|
|
|
GObjectClass *gobject_class;
|
2001-08-22 04:30:27 +00:00
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
2001-08-22 19:54:08 +00:00
|
|
|
gobject_class = (GObjectClass*)klass;
|
2001-08-22 04:30:27 +00:00
|
|
|
gstelement_class = (GstElementClass*)klass;
|
|
|
|
|
2001-08-22 19:54:08 +00:00
|
|
|
parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
|
|
|
|
|
2002-03-18 04:41:37 +00:00
|
|
|
gst_element_class_install_std_props (
|
2001-12-13 23:37:47 +00:00
|
|
|
GST_ELEMENT_CLASS (klass),
|
|
|
|
"fd", ARG_FD, G_PARAM_READABLE,
|
|
|
|
"offset", ARG_OFFSET, G_PARAM_READWRITE,
|
|
|
|
"filesize", ARG_FILESIZE, G_PARAM_READABLE,
|
|
|
|
"location", ARG_LOCATION, G_PARAM_READWRITE,
|
|
|
|
"blocksize", ARG_BLOCKSIZE, G_PARAM_READWRITE,
|
|
|
|
"mmapsize", ARG_MAPSIZE, G_PARAM_READWRITE,
|
|
|
|
"touch", ARG_TOUCH, G_PARAM_READWRITE,
|
|
|
|
NULL);
|
2001-08-22 19:54:08 +00:00
|
|
|
|
2001-12-28 15:19:38 +00:00
|
|
|
gobject_class->dispose = gst_filesrc_dispose;
|
|
|
|
gobject_class->set_property = gst_filesrc_set_property;
|
|
|
|
gobject_class->get_property = gst_filesrc_get_property;
|
2001-08-22 04:30:27 +00:00
|
|
|
|
|
|
|
gstelement_class->change_state = gst_filesrc_change_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
gst_filesrc_bufcmp (gconstpointer a, gconstpointer b)
|
|
|
|
{
|
2001-12-17 18:14:51 +00:00
|
|
|
/* GstBuffer *bufa = (GstBuffer *)a, *bufb = (GstBuffer *)b;*/
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* sort first by offset, then in reverse by size */
|
2001-08-22 04:30:27 +00:00
|
|
|
if (GST_BUFFER_OFFSET(a) < GST_BUFFER_OFFSET(b)) return -1;
|
|
|
|
else if (GST_BUFFER_OFFSET(a) > GST_BUFFER_OFFSET(b)) return 1;
|
|
|
|
else if (GST_BUFFER_SIZE(a) > GST_BUFFER_SIZE(b)) return -1;
|
|
|
|
else if (GST_BUFFER_SIZE(a) < GST_BUFFER_SIZE(b)) return 1;
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_filesrc_init (GstFileSrc *src)
|
|
|
|
{
|
|
|
|
src->srcpad = gst_pad_new ("src", GST_PAD_SRC);
|
2002-05-26 21:54:27 +00:00
|
|
|
gst_pad_set_get_function (src->srcpad, gst_filesrc_get);
|
|
|
|
gst_pad_set_event_function (src->srcpad, gst_filesrc_srcpad_event);
|
2002-07-24 21:08:43 +00:00
|
|
|
gst_pad_set_event_mask_function (src->srcpad, gst_filesrc_get_event_mask);
|
2002-05-26 21:54:27 +00:00
|
|
|
gst_pad_set_query_function (src->srcpad, gst_filesrc_srcpad_query);
|
2002-07-24 21:08:43 +00:00
|
|
|
gst_pad_set_query_type_function (src->srcpad, gst_filesrc_get_query_types);
|
|
|
|
gst_pad_set_formats_function (src->srcpad, gst_filesrc_get_formats);
|
2001-08-22 04:30:27 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (src), src->srcpad);
|
|
|
|
|
2001-08-22 19:54:08 +00:00
|
|
|
src->pagesize = getpagesize();
|
|
|
|
|
2001-08-22 04:30:27 +00:00
|
|
|
src->filename = NULL;
|
|
|
|
src->fd = 0;
|
|
|
|
src->filelen = 0;
|
|
|
|
|
|
|
|
src->curoffset = 0;
|
2001-08-22 19:54:08 +00:00
|
|
|
src->block_size = 4096;
|
|
|
|
src->touch = TRUE;
|
2001-08-22 04:30:27 +00:00
|
|
|
|
|
|
|
src->mapbuf = NULL;
|
2001-12-17 18:14:51 +00:00
|
|
|
src->mapsize = 4 * 1024 * 1024; /* default is 4MB */
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2002-05-26 21:54:27 +00:00
|
|
|
src->map_regions = g_tree_new (gst_filesrc_bufcmp);
|
2001-08-22 04:30:27 +00:00
|
|
|
src->map_regions_lock = g_mutex_new();
|
2001-10-17 10:21:27 +00:00
|
|
|
|
|
|
|
src->seek_happened = FALSE;
|
2001-08-22 04:30:27 +00:00
|
|
|
}
|
|
|
|
|
2001-12-28 15:19:38 +00:00
|
|
|
static void
|
|
|
|
gst_filesrc_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GstFileSrc *src;
|
|
|
|
|
|
|
|
src = GST_FILESRC (object);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
|
|
|
|
|
|
|
g_tree_destroy (src->map_regions);
|
|
|
|
g_mutex_free (src->map_regions_lock);
|
|
|
|
if (src->filename)
|
|
|
|
g_free (src->filename);
|
|
|
|
}
|
|
|
|
|
2001-08-22 04:30:27 +00:00
|
|
|
|
|
|
|
static void
|
2001-08-22 19:54:08 +00:00
|
|
|
gst_filesrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
|
2001-08-22 04:30:27 +00:00
|
|
|
{
|
|
|
|
GstFileSrc *src;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail (GST_IS_FILESRC (object));
|
|
|
|
|
|
|
|
src = GST_FILESRC (object);
|
|
|
|
|
2001-08-22 19:54:08 +00:00
|
|
|
switch (prop_id) {
|
2001-08-22 04:30:27 +00:00
|
|
|
case ARG_LOCATION:
|
|
|
|
/* the element must be stopped in order to do this */
|
|
|
|
g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
|
|
|
|
|
|
|
|
if (src->filename) g_free (src->filename);
|
|
|
|
/* clear the filename if we get a NULL (is that possible?) */
|
2001-08-22 19:54:08 +00:00
|
|
|
if (g_value_get_string (value) == NULL) {
|
2001-08-22 04:30:27 +00:00
|
|
|
gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
|
|
|
|
src->filename = NULL;
|
|
|
|
/* otherwise set the new filename */
|
|
|
|
} else {
|
2001-08-22 19:54:08 +00:00
|
|
|
src->filename = g_strdup (g_value_get_string (value));
|
2001-08-22 04:30:27 +00:00
|
|
|
}
|
2002-02-05 21:11:43 +00:00
|
|
|
g_object_notify (G_OBJECT (src), "location");
|
2001-08-22 04:30:27 +00:00
|
|
|
break;
|
|
|
|
case ARG_BLOCKSIZE:
|
2001-08-22 19:54:08 +00:00
|
|
|
src->block_size = g_value_get_ulong (value);
|
2002-02-05 21:11:43 +00:00
|
|
|
g_object_notify (G_OBJECT (src), "blocksize");
|
2001-08-22 04:30:27 +00:00
|
|
|
break;
|
|
|
|
case ARG_OFFSET:
|
2001-10-22 19:13:24 +00:00
|
|
|
src->curoffset = g_value_get_int64 (value);
|
2002-02-05 21:11:43 +00:00
|
|
|
g_object_notify (G_OBJECT (src), "offset");
|
2001-08-22 19:54:08 +00:00
|
|
|
break;
|
|
|
|
case ARG_MAPSIZE:
|
2002-05-14 00:40:15 +00:00
|
|
|
if ((src->mapsize % src->pagesize) == 0) {
|
2001-09-10 19:46:01 +00:00
|
|
|
src->mapsize = g_value_get_ulong (value);
|
2002-02-05 21:11:43 +00:00
|
|
|
g_object_notify (G_OBJECT (src), "mmapsize");
|
2002-05-14 00:40:15 +00:00
|
|
|
} else {
|
2001-09-10 19:46:01 +00:00
|
|
|
GST_INFO(0, "invalid mapsize, must a multiple of pagesize, which is %d\n",src->pagesize);
|
2002-05-14 00:40:15 +00:00
|
|
|
}
|
2001-08-22 04:30:27 +00:00
|
|
|
break;
|
2001-10-17 10:21:27 +00:00
|
|
|
case ARG_TOUCH:
|
|
|
|
src->touch = g_value_get_boolean (value);
|
2002-02-05 21:11:43 +00:00
|
|
|
g_object_notify (G_OBJECT (src), "touch");
|
2001-10-17 10:21:27 +00:00
|
|
|
break;
|
2001-08-22 04:30:27 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-08-22 19:54:08 +00:00
|
|
|
gst_filesrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
|
2001-08-22 04:30:27 +00:00
|
|
|
{
|
|
|
|
GstFileSrc *src;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail (GST_IS_FILESRC (object));
|
|
|
|
|
|
|
|
src = GST_FILESRC (object);
|
|
|
|
|
2001-08-22 19:54:08 +00:00
|
|
|
switch (prop_id) {
|
2001-08-22 04:30:27 +00:00
|
|
|
case ARG_LOCATION:
|
2001-08-22 19:54:08 +00:00
|
|
|
g_value_set_string (value, src->filename);
|
|
|
|
break;
|
|
|
|
case ARG_FILESIZE:
|
2001-10-22 19:13:24 +00:00
|
|
|
g_value_set_int64 (value, src->filelen);
|
2001-08-22 19:54:08 +00:00
|
|
|
break;
|
|
|
|
case ARG_FD:
|
|
|
|
g_value_set_int (value, src->fd);
|
2001-08-22 04:30:27 +00:00
|
|
|
break;
|
|
|
|
case ARG_BLOCKSIZE:
|
2001-08-22 19:54:08 +00:00
|
|
|
g_value_set_ulong (value, src->block_size);
|
2001-08-22 04:30:27 +00:00
|
|
|
break;
|
|
|
|
case ARG_OFFSET:
|
2001-10-22 19:13:24 +00:00
|
|
|
g_value_set_int64 (value, src->curoffset);
|
2001-08-22 04:30:27 +00:00
|
|
|
break;
|
2001-08-22 19:54:08 +00:00
|
|
|
case ARG_MAPSIZE:
|
|
|
|
g_value_set_ulong (value, src->mapsize);
|
2001-08-22 04:30:27 +00:00
|
|
|
break;
|
2001-10-17 10:21:27 +00:00
|
|
|
case ARG_TOUCH:
|
|
|
|
g_value_set_boolean (value, src->touch);
|
|
|
|
break;
|
2001-08-22 04:30:27 +00:00
|
|
|
default:
|
2001-08-22 19:54:08 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2001-08-22 04:30:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_filesrc_free_parent_mmap (GstBuffer *buf)
|
|
|
|
{
|
|
|
|
GstFileSrc *src = GST_FILESRC(GST_BUFFER_POOL_PRIVATE(buf));
|
|
|
|
|
2001-10-17 10:21:27 +00:00
|
|
|
fs_print ("freeing mmap()d buffer at %d+%d\n",GST_BUFFER_OFFSET(buf),GST_BUFFER_SIZE(buf));
|
2001-08-22 19:54:08 +00:00
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* remove the buffer from the list of available mmap'd regions */
|
2001-08-22 04:30:27 +00:00
|
|
|
g_mutex_lock(src->map_regions_lock);
|
|
|
|
g_tree_remove(src->map_regions,buf);
|
2001-12-17 18:14:51 +00:00
|
|
|
/* check to see if the tree is empty */
|
2001-08-22 04:30:27 +00:00
|
|
|
if (g_tree_nnodes(src->map_regions) == 0) {
|
2001-12-17 18:14:51 +00:00
|
|
|
/* we have to free the bufferpool we don't have yet */
|
2001-08-22 04:30:27 +00:00
|
|
|
}
|
|
|
|
g_mutex_unlock(src->map_regions_lock);
|
|
|
|
|
2001-10-17 20:57:53 +00:00
|
|
|
#ifdef MADV_DONTNEED
|
2001-12-17 18:14:51 +00:00
|
|
|
/* madvise to tell the kernel what to do with it */
|
2001-10-17 20:57:53 +00:00
|
|
|
madvise(GST_BUFFER_DATA(buf),GST_BUFFER_SIZE(buf),MADV_DONTNEED);
|
|
|
|
#endif
|
2001-12-17 18:14:51 +00:00
|
|
|
/* now unmap the memory */
|
2001-08-22 04:30:27 +00:00
|
|
|
munmap(GST_BUFFER_DATA(buf),GST_BUFFER_MAXSIZE(buf));
|
2002-07-08 19:22:02 +00:00
|
|
|
|
|
|
|
GST_BUFFER_DATA (buf) = NULL;
|
|
|
|
|
2002-07-13 23:12:22 +00:00
|
|
|
gst_buffer_default_free (buf);
|
2001-08-22 04:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstBuffer *
|
2001-10-17 10:21:27 +00:00
|
|
|
gst_filesrc_map_region (GstFileSrc *src, off_t offset, size_t size)
|
2001-08-22 04:30:27 +00:00
|
|
|
{
|
|
|
|
GstBuffer *buf;
|
2001-09-10 19:46:01 +00:00
|
|
|
gint retval;
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-10-17 10:21:27 +00:00
|
|
|
g_return_val_if_fail (offset >= 0, NULL);
|
|
|
|
|
2002-06-22 09:23:40 +00:00
|
|
|
fs_print ("mapping region %08llx+%08x from file into memory\n",offset,size);
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* time to allocate a new mapbuf */
|
2001-08-22 04:30:27 +00:00
|
|
|
buf = gst_buffer_new();
|
2001-12-17 18:14:51 +00:00
|
|
|
/* mmap() the data into this new buffer */
|
2001-08-22 04:30:27 +00:00
|
|
|
GST_BUFFER_DATA(buf) = mmap (NULL, size, PROT_READ, MAP_SHARED, src->fd, offset);
|
|
|
|
if (GST_BUFFER_DATA(buf) == NULL) {
|
2001-12-22 21:18:17 +00:00
|
|
|
gst_element_error (GST_ELEMENT (src), "couldn't map file");
|
2001-10-17 10:21:27 +00:00
|
|
|
} else if (GST_BUFFER_DATA(buf) == MAP_FAILED) {
|
2001-12-22 21:18:17 +00:00
|
|
|
gst_element_error (GST_ELEMENT (src), "mmap (0x%x, %d, 0x%llx) : %s",
|
|
|
|
size, src->fd, offset, strerror (errno));
|
2001-08-22 04:30:27 +00:00
|
|
|
}
|
2001-10-17 20:57:53 +00:00
|
|
|
#ifdef MADV_SEQUENTIAL
|
2001-12-17 18:14:51 +00:00
|
|
|
/* madvise to tell the kernel what to do with it */
|
2001-09-10 19:46:01 +00:00
|
|
|
retval = madvise(GST_BUFFER_DATA(buf),GST_BUFFER_SIZE(buf),MADV_SEQUENTIAL);
|
2001-10-17 20:57:53 +00:00
|
|
|
#endif
|
2001-12-17 18:14:51 +00:00
|
|
|
/* fill in the rest of the fields */
|
2002-04-03 06:27:25 +00:00
|
|
|
GST_BUFFER_FLAG_SET(buf, GST_BUFFER_READONLY);
|
|
|
|
GST_BUFFER_FLAG_SET(buf, GST_BUFFER_ORIGINAL);
|
2001-08-22 04:30:27 +00:00
|
|
|
GST_BUFFER_SIZE(buf) = size;
|
|
|
|
GST_BUFFER_MAXSIZE(buf) = size;
|
|
|
|
GST_BUFFER_OFFSET(buf) = offset;
|
|
|
|
GST_BUFFER_TIMESTAMP(buf) = -1LL;
|
|
|
|
GST_BUFFER_POOL_PRIVATE(buf) = src;
|
2002-07-08 19:22:02 +00:00
|
|
|
GST_BUFFER_FREE_FUNC(buf) = (GstDataFreeFunction) gst_filesrc_free_parent_mmap;
|
2001-08-22 04:30:27 +00:00
|
|
|
|
|
|
|
g_mutex_lock(src->map_regions_lock);
|
|
|
|
g_tree_insert(src->map_regions,buf,buf);
|
|
|
|
g_mutex_unlock(src->map_regions_lock);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstBuffer *
|
2001-10-17 10:21:27 +00:00
|
|
|
gst_filesrc_map_small_region (GstFileSrc *src, off_t offset, size_t size)
|
2001-08-22 04:30:27 +00:00
|
|
|
{
|
2001-10-17 10:21:27 +00:00
|
|
|
size_t mapsize;
|
|
|
|
off_t mod, mapbase;
|
2001-08-22 04:30:27 +00:00
|
|
|
GstBuffer *map;
|
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* printf("attempting to map a small buffer at %d+%d\n",offset,size); */
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* if the offset starts at a non-page boundary, we have to special case */
|
2001-08-22 19:54:08 +00:00
|
|
|
if ((mod = offset % src->pagesize)) {
|
2001-10-17 10:21:27 +00:00
|
|
|
GstBuffer *ret;
|
|
|
|
|
2001-08-22 04:30:27 +00:00
|
|
|
mapbase = offset - mod;
|
2001-08-22 19:54:08 +00:00
|
|
|
mapsize = ((size + mod + src->pagesize - 1) / src->pagesize) * src->pagesize;
|
2001-12-17 18:14:51 +00:00
|
|
|
/* printf("not on page boundaries, resizing map to %d+%d\n",mapbase,mapsize);*/
|
2001-08-22 04:30:27 +00:00
|
|
|
map = gst_filesrc_map_region(src, mapbase, mapsize);
|
2001-10-17 10:21:27 +00:00
|
|
|
ret = gst_buffer_create_sub (map, offset - mapbase, size);
|
|
|
|
|
|
|
|
gst_buffer_unref (map);
|
|
|
|
|
|
|
|
return ret;
|
2001-08-22 04:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return gst_filesrc_map_region(src,offset,size);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
off_t offset;
|
|
|
|
off_t size;
|
|
|
|
} GstFileSrcRegion;
|
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* This allows us to search for a potential mmap region. */
|
2001-08-22 04:30:27 +00:00
|
|
|
static gint
|
|
|
|
gst_filesrc_search_region_match (gpointer a, gpointer b)
|
|
|
|
{
|
|
|
|
GstFileSrcRegion *r = (GstFileSrcRegion *)b;
|
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* trying to walk b down the tree, current node is a */
|
2001-08-22 04:30:27 +00:00
|
|
|
if (r->offset < GST_BUFFER_OFFSET(a)) return -1;
|
|
|
|
else if (r->offset >= (GST_BUFFER_OFFSET(a) + GST_BUFFER_SIZE(a))) return 1;
|
|
|
|
else if ((r->offset + r->size) <= (GST_BUFFER_OFFSET(a) + GST_BUFFER_SIZE(a))) return 0;
|
|
|
|
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_filesrc_get:
|
|
|
|
* @pad: #GstPad to push a buffer from
|
|
|
|
*
|
|
|
|
* Push a new buffer from the filesrc at the current offset.
|
|
|
|
*/
|
|
|
|
static GstBuffer *
|
|
|
|
gst_filesrc_get (GstPad *pad)
|
|
|
|
{
|
|
|
|
GstFileSrc *src;
|
|
|
|
GstBuffer *buf = NULL, *map;
|
2001-10-17 10:21:27 +00:00
|
|
|
size_t readsize;
|
|
|
|
off_t readend,mapstart,mapend;
|
2001-08-22 04:30:27 +00:00
|
|
|
GstFileSrcRegion region;
|
2001-08-22 19:54:08 +00:00
|
|
|
int i;
|
2001-08-22 04:30:27 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (pad != NULL, NULL);
|
|
|
|
src = GST_FILESRC (gst_pad_get_parent (pad));
|
|
|
|
g_return_val_if_fail (GST_FLAG_IS_SET (src, GST_FILESRC_OPEN), NULL);
|
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* check for seek */
|
2001-10-17 10:21:27 +00:00
|
|
|
if (src->seek_happened) {
|
2002-05-26 21:54:27 +00:00
|
|
|
GstEvent *event;
|
|
|
|
|
2001-10-17 10:21:27 +00:00
|
|
|
src->seek_happened = FALSE;
|
2002-05-26 21:54:27 +00:00
|
|
|
GST_DEBUG (GST_CAT_EVENT, "filesrc sending discont\n");
|
|
|
|
event = gst_event_new_discontinuous (FALSE, GST_FORMAT_BYTES, src->curoffset, NULL);
|
2002-06-12 22:26:36 +00:00
|
|
|
GST_EVENT_DISCONT_NEW_MEDIA (event) = FALSE;
|
2002-05-26 21:54:27 +00:00
|
|
|
src->need_flush = FALSE;
|
|
|
|
return GST_BUFFER (event);
|
2002-02-02 13:24:25 +00:00
|
|
|
}
|
|
|
|
/* check for flush */
|
|
|
|
if (src->need_flush) {
|
|
|
|
src->need_flush = FALSE;
|
2002-05-26 21:54:27 +00:00
|
|
|
GST_DEBUG (GST_CAT_EVENT, "filesrc sending flush\n");
|
2002-02-02 13:24:25 +00:00
|
|
|
return GST_BUFFER (gst_event_new_flush ());
|
2001-10-17 10:21:27 +00:00
|
|
|
}
|
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* check for EOF */
|
2001-10-17 10:21:27 +00:00
|
|
|
if (src->curoffset == src->filelen) {
|
2002-05-31 21:36:11 +00:00
|
|
|
GST_DEBUG (0, "filesrc eos %lld %lld\n", src->curoffset, src->filelen);
|
2001-12-28 20:20:26 +00:00
|
|
|
gst_element_set_eos (GST_ELEMENT (src));
|
2002-02-02 13:24:25 +00:00
|
|
|
return GST_BUFFER (gst_event_new (GST_EVENT_EOS));
|
2001-10-17 10:21:27 +00:00
|
|
|
}
|
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* calculate end pointers so we don't have to do so repeatedly later */
|
2001-09-10 19:46:01 +00:00
|
|
|
readsize = src->block_size;
|
2001-12-17 18:14:51 +00:00
|
|
|
readend = src->curoffset + src->block_size; /* note this is the byte *after* the read */
|
2001-08-22 04:30:27 +00:00
|
|
|
mapstart = GST_BUFFER_OFFSET(src->mapbuf);
|
2001-12-17 18:14:51 +00:00
|
|
|
mapend = mapstart + GST_BUFFER_SIZE(src->mapbuf); /* note this is the byte *after* the map */
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* check to see if we're going to overflow the end of the file */
|
2001-09-10 19:46:01 +00:00
|
|
|
if (readend > src->filelen) {
|
|
|
|
readsize = src->filelen - src->curoffset;
|
2002-06-22 09:23:40 +00:00
|
|
|
readend = src->curoffset + readsize;
|
2001-09-10 19:46:01 +00:00
|
|
|
}
|
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* if the start is past the mapstart */
|
2001-08-22 04:30:27 +00:00
|
|
|
if (src->curoffset >= mapstart) {
|
2001-12-17 18:14:51 +00:00
|
|
|
/* if the end is before the mapend, the buffer is in current mmap region... */
|
|
|
|
/* ('cause by definition if readend is in the buffer, so's readstart) */
|
2001-08-22 04:30:27 +00:00
|
|
|
if (readend <= mapend) {
|
2002-06-22 09:23:40 +00:00
|
|
|
fs_print ("read buf %llu+%d lives in current mapbuf %lld+%d, creating subbuffer of mapbuf\n",
|
|
|
|
src->curoffset, readsize, mapstart, GST_BUFFER_SIZE(src->mapbuf));
|
2001-08-22 04:30:27 +00:00
|
|
|
buf = gst_buffer_create_sub (src->mapbuf, src->curoffset - GST_BUFFER_OFFSET(src->mapbuf),
|
2001-09-10 19:46:01 +00:00
|
|
|
readsize);
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* if the start actually is within the current mmap region, map an overlap buffer */
|
2001-08-22 04:30:27 +00:00
|
|
|
} else if (src->curoffset < mapend) {
|
2002-06-22 09:23:40 +00:00
|
|
|
fs_print ("read buf %llu+%d starts in mapbuf %d+%d but ends outside, creating new mmap\n",
|
2001-10-17 10:21:27 +00:00
|
|
|
src->curoffset,readsize,GST_BUFFER_OFFSET(src->mapbuf),GST_BUFFER_SIZE(src->mapbuf));
|
2001-09-10 19:46:01 +00:00
|
|
|
buf = gst_filesrc_map_small_region (src, src->curoffset, readsize);
|
2001-08-22 04:30:27 +00:00
|
|
|
}
|
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* the only other option is that buffer is totally outside, which means we search for it */
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* now we can assume that the start is *before* the current mmap region */
|
|
|
|
/* if the readend is past mapstart, we have two options */
|
2001-08-22 04:30:27 +00:00
|
|
|
} else if (readend >= mapstart) {
|
2001-12-17 18:14:51 +00:00
|
|
|
/* either the read buffer overlaps the start of the mmap region */
|
|
|
|
/* or the read buffer fully contains the current mmap region */
|
|
|
|
/* either way, it's really not relevant, we just create a new region anyway*/
|
2002-06-22 09:23:40 +00:00
|
|
|
fs_print ("read buf %llu+%d starts before mapbuf %d+%d, but overlaps it\n",
|
2001-10-17 10:21:27 +00:00
|
|
|
src->curoffset,readsize,GST_BUFFER_OFFSET(src->mapbuf),GST_BUFFER_SIZE(src->mapbuf));
|
2001-09-10 19:46:01 +00:00
|
|
|
buf = gst_filesrc_map_small_region (src, src->curoffset, readsize);
|
2001-08-22 04:30:27 +00:00
|
|
|
}
|
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* then deal with the case where the read buffer is totally outside */
|
2001-08-22 04:30:27 +00:00
|
|
|
if (buf == NULL) {
|
2001-12-17 18:14:51 +00:00
|
|
|
/* first check to see if there's a map that covers the right region already */
|
2002-06-22 09:23:40 +00:00
|
|
|
fs_print ("searching for mapbuf to cover %llu+%d\n",src->curoffset,readsize);
|
2001-08-22 04:30:27 +00:00
|
|
|
region.offset = src->curoffset;
|
2001-09-10 19:46:01 +00:00
|
|
|
region.size = readsize;
|
2001-10-17 10:21:27 +00:00
|
|
|
map = g_tree_search (src->map_regions,
|
2001-10-22 13:37:50 +00:00
|
|
|
(GCompareFunc) gst_filesrc_search_region_match,
|
2001-12-28 20:20:26 +00:00
|
|
|
(gpointer)®ion);
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* if we found an exact match, subbuffer it */
|
2001-08-22 04:30:27 +00:00
|
|
|
if (map != NULL) {
|
2001-10-17 10:21:27 +00:00
|
|
|
fs_print ("found mapbuf at %d+%d, creating subbuffer\n",GST_BUFFER_OFFSET(map),GST_BUFFER_SIZE(map));
|
2001-09-10 19:46:01 +00:00
|
|
|
buf = gst_buffer_create_sub (map, src->curoffset - GST_BUFFER_OFFSET(map), readsize);
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* otherwise we need to create something out of thin air */
|
2001-08-22 04:30:27 +00:00
|
|
|
} else {
|
2001-12-17 18:14:51 +00:00
|
|
|
/* if the read buffer crosses a mmap region boundary, create a one-off region */
|
2001-09-10 19:46:01 +00:00
|
|
|
if ((src->curoffset / src->mapsize) != (readend / src->mapsize)) {
|
2002-06-22 09:23:40 +00:00
|
|
|
fs_print ("read buf %llu+%d crosses a %d-byte boundary, creating a one-off\n",
|
2001-10-17 10:21:27 +00:00
|
|
|
src->curoffset,readsize,src->mapsize);
|
2001-09-10 19:46:01 +00:00
|
|
|
buf = gst_filesrc_map_small_region (src, src->curoffset, readsize);
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* otherwise we will create a new mmap region and set it to the default */
|
2001-08-22 04:30:27 +00:00
|
|
|
} else {
|
2002-06-22 09:23:40 +00:00
|
|
|
size_t mapsize;
|
|
|
|
|
2001-08-22 04:30:27 +00:00
|
|
|
off_t nextmap = src->curoffset - (src->curoffset % src->mapsize);
|
2002-06-22 09:23:40 +00:00
|
|
|
fs_print ("read buf %llu+%d in new mapbuf at %llu+%d, mapping and subbuffering\n",
|
|
|
|
src->curoffset, readsize, nextmap, src->mapsize);
|
2001-12-17 18:14:51 +00:00
|
|
|
/* first, we're done with the old mapbuf */
|
2001-08-22 04:30:27 +00:00
|
|
|
gst_buffer_unref(src->mapbuf);
|
2002-06-22 09:23:40 +00:00
|
|
|
mapsize = src->mapsize;
|
|
|
|
|
|
|
|
/* double the mapsize as long as the readsize is smaller */
|
|
|
|
while (readsize - (src->curoffset - nextmap) > mapsize) {
|
|
|
|
fs_print ("readsize smaller then mapsize %08x %d\n", readsize, mapsize);
|
|
|
|
mapsize <<=1;
|
|
|
|
}
|
2001-12-17 18:14:51 +00:00
|
|
|
/* create a new one */
|
2002-06-22 09:23:40 +00:00
|
|
|
src->mapbuf = gst_filesrc_map_region (src, nextmap, mapsize);
|
2001-12-17 18:14:51 +00:00
|
|
|
/* subbuffer it */
|
2002-06-22 09:23:40 +00:00
|
|
|
buf = gst_buffer_create_sub (src->mapbuf, src->curoffset - nextmap, readsize);
|
2001-08-22 04:30:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-22 19:54:08 +00:00
|
|
|
/* if we need to touch the buffer (to bring it into memory), do so */
|
|
|
|
if (src->touch) {
|
2001-12-14 12:15:27 +00:00
|
|
|
volatile guchar *p = GST_BUFFER_DATA (buf), c;
|
2001-08-22 19:54:08 +00:00
|
|
|
for (i=0;i<GST_BUFFER_SIZE(buf);i+=src->pagesize)
|
2001-12-14 12:15:27 +00:00
|
|
|
c = p[i];
|
2001-08-22 19:54:08 +00:00
|
|
|
}
|
|
|
|
|
2001-08-22 04:30:27 +00:00
|
|
|
/* we're done, return the buffer */
|
|
|
|
src->curoffset += GST_BUFFER_SIZE(buf);
|
2002-03-30 19:31:14 +00:00
|
|
|
g_object_notify (G_OBJECT (src), "offset");
|
2001-08-22 04:30:27 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open the file and mmap it, necessary to go to READY state */
|
|
|
|
static gboolean
|
|
|
|
gst_filesrc_open_file (GstFileSrc *src)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (!GST_FLAG_IS_SET (src ,GST_FILESRC_OPEN), FALSE);
|
|
|
|
|
2002-03-24 22:07:09 +00:00
|
|
|
GST_DEBUG(0, "opening file %s",src->filename);
|
2001-08-22 04:30:27 +00:00
|
|
|
|
|
|
|
/* open the file */
|
|
|
|
src->fd = open (src->filename, O_RDONLY);
|
|
|
|
if (src->fd < 0) {
|
2001-12-22 21:18:17 +00:00
|
|
|
gst_element_error (GST_ELEMENT (src), "opening file \"%s\" (%s)",
|
|
|
|
src->filename, strerror (errno), NULL);
|
2001-08-22 04:30:27 +00:00
|
|
|
return FALSE;
|
|
|
|
} else {
|
2002-05-08 20:40:48 +00:00
|
|
|
/* check if it is a regular file, otherwise bail out */
|
|
|
|
struct stat stat_results;
|
|
|
|
|
|
|
|
fstat(src->fd, &stat_results);
|
|
|
|
|
|
|
|
if (!S_ISREG(stat_results.st_mode)) {
|
|
|
|
gst_element_error (GST_ELEMENT (src), "opening file \"%s\" failed. it isn't a regular file",
|
|
|
|
src->filename, NULL);
|
|
|
|
close(src->fd);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2002-01-15 22:33:15 +00:00
|
|
|
|
2001-08-22 04:30:27 +00:00
|
|
|
/* find the file length */
|
|
|
|
src->filelen = lseek (src->fd, 0, SEEK_END);
|
|
|
|
lseek (src->fd, 0, SEEK_SET);
|
|
|
|
|
2001-12-17 18:14:51 +00:00
|
|
|
/* allocate the first mmap'd region */
|
2001-08-22 04:30:27 +00:00
|
|
|
src->mapbuf = gst_filesrc_map_region (src, 0, src->mapsize);
|
|
|
|
|
|
|
|
src->curoffset = 0;
|
|
|
|
|
2002-02-05 21:11:43 +00:00
|
|
|
/* now notify of the changes */
|
|
|
|
g_object_freeze_notify (G_OBJECT (src));
|
|
|
|
g_object_notify (G_OBJECT (src), "filesize");
|
2002-03-30 19:31:14 +00:00
|
|
|
g_object_notify (G_OBJECT (src), "offset");
|
2002-02-05 21:11:43 +00:00
|
|
|
g_object_thaw_notify (G_OBJECT (src));
|
|
|
|
|
2001-08-22 04:30:27 +00:00
|
|
|
GST_FLAG_SET (src, GST_FILESRC_OPEN);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unmap and close the file */
|
|
|
|
static void
|
|
|
|
gst_filesrc_close_file (GstFileSrc *src)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GST_FLAG_IS_SET (src, GST_FILESRC_OPEN));
|
|
|
|
|
|
|
|
/* close the file */
|
|
|
|
close (src->fd);
|
|
|
|
|
|
|
|
/* zero out a lot of our state */
|
|
|
|
src->fd = 0;
|
|
|
|
src->filelen = 0;
|
|
|
|
src->curoffset = 0;
|
2002-02-05 21:11:43 +00:00
|
|
|
/* and notify that things changed */
|
|
|
|
g_object_freeze_notify (G_OBJECT (src));
|
|
|
|
g_object_notify (G_OBJECT (src), "filesize");
|
2002-03-30 19:31:14 +00:00
|
|
|
g_object_notify (G_OBJECT (src), "offset");
|
2002-02-05 21:11:43 +00:00
|
|
|
g_object_thaw_notify (G_OBJECT (src));
|
|
|
|
|
2001-12-22 21:18:17 +00:00
|
|
|
if (src->mapbuf)
|
|
|
|
gst_buffer_unref (src->mapbuf);
|
2001-08-22 04:30:27 +00:00
|
|
|
|
|
|
|
GST_FLAG_UNSET (src, GST_FILESRC_OPEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GstElementStateReturn
|
|
|
|
gst_filesrc_change_state (GstElement *element)
|
|
|
|
{
|
2001-12-18 16:54:11 +00:00
|
|
|
GstFileSrc *src = GST_FILESRC(element);
|
2001-08-22 04:30:27 +00:00
|
|
|
|
2001-12-22 21:18:17 +00:00
|
|
|
switch (GST_STATE_TRANSITION (element)) {
|
|
|
|
case GST_STATE_NULL_TO_READY:
|
2002-06-07 19:32:53 +00:00
|
|
|
break;
|
|
|
|
case GST_STATE_READY_TO_NULL:
|
|
|
|
break;
|
|
|
|
case GST_STATE_READY_TO_PAUSED:
|
2001-12-22 21:18:17 +00:00
|
|
|
if (!GST_FLAG_IS_SET (element, GST_FILESRC_OPEN)) {
|
|
|
|
if (!gst_filesrc_open_file (GST_FILESRC (element)))
|
|
|
|
return GST_STATE_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
2002-06-07 19:32:53 +00:00
|
|
|
case GST_STATE_PAUSED_TO_READY:
|
2001-12-22 21:18:17 +00:00
|
|
|
if (GST_FLAG_IS_SET (element, GST_FILESRC_OPEN))
|
|
|
|
gst_filesrc_close_file (GST_FILESRC (element));
|
2002-05-26 21:54:27 +00:00
|
|
|
src->seek_happened = TRUE;
|
2002-06-07 19:32:53 +00:00
|
|
|
break;
|
2001-12-22 21:18:17 +00:00
|
|
|
default:
|
|
|
|
break;
|
2001-08-22 04:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
|
|
|
return GST_ELEMENT_CLASS (parent_class)->change_state (element);
|
|
|
|
|
|
|
|
return GST_STATE_SUCCESS;
|
|
|
|
}
|
2001-09-10 19:46:01 +00:00
|
|
|
|
2002-05-26 21:54:27 +00:00
|
|
|
static gboolean
|
|
|
|
gst_filesrc_srcpad_query (GstPad *pad, GstPadQueryType type,
|
|
|
|
GstFormat *format, gint64 *value)
|
|
|
|
{
|
|
|
|
GstFileSrc *src = GST_FILESRC (GST_PAD_PARENT (pad));
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GST_PAD_QUERY_TOTAL:
|
|
|
|
if (*format != GST_FORMAT_BYTES) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
*value = src->filelen;
|
|
|
|
break;
|
|
|
|
case GST_PAD_QUERY_POSITION:
|
|
|
|
if (*format != GST_FORMAT_BYTES) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
*value = src->curoffset;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2001-09-10 19:46:01 +00:00
|
|
|
static gboolean
|
2001-10-21 21:58:29 +00:00
|
|
|
gst_filesrc_srcpad_event (GstPad *pad, GstEvent *event)
|
2001-09-10 19:46:01 +00:00
|
|
|
{
|
2002-05-26 21:54:27 +00:00
|
|
|
GstFileSrc *src = GST_FILESRC (GST_PAD_PARENT (pad));
|
2001-09-10 19:46:01 +00:00
|
|
|
|
2002-05-31 21:36:11 +00:00
|
|
|
GST_DEBUG(0, "event %d", GST_EVENT_TYPE (event));
|
|
|
|
|
2001-10-21 21:58:29 +00:00
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_SEEK:
|
2002-06-22 09:23:40 +00:00
|
|
|
{
|
|
|
|
guint64 offset;
|
|
|
|
|
2002-05-26 21:54:27 +00:00
|
|
|
if (GST_EVENT_SEEK_FORMAT (event) != GST_FORMAT_BYTES) {
|
2002-07-08 19:22:02 +00:00
|
|
|
goto error;
|
2002-05-26 21:54:27 +00:00
|
|
|
}
|
2002-06-22 09:23:40 +00:00
|
|
|
|
|
|
|
offset = GST_EVENT_SEEK_OFFSET (event);
|
|
|
|
|
2002-05-26 21:54:27 +00:00
|
|
|
switch (GST_EVENT_SEEK_METHOD (event)) {
|
|
|
|
case GST_SEEK_METHOD_SET:
|
2002-06-22 09:23:40 +00:00
|
|
|
if (offset > src->filelen)
|
2002-07-08 19:22:02 +00:00
|
|
|
goto error;
|
2002-06-22 09:23:40 +00:00
|
|
|
src->curoffset = offset;
|
2002-05-31 21:36:11 +00:00
|
|
|
GST_DEBUG(0, "seek set pending to %lld", src->curoffset);
|
2002-01-26 18:28:55 +00:00
|
|
|
break;
|
2002-05-26 21:54:27 +00:00
|
|
|
case GST_SEEK_METHOD_CUR:
|
2002-06-22 09:23:40 +00:00
|
|
|
if (offset + src->curoffset > src->filelen)
|
2002-07-08 19:22:02 +00:00
|
|
|
goto error;
|
2002-06-22 09:23:40 +00:00
|
|
|
src->curoffset += offset;
|
2002-05-31 21:36:11 +00:00
|
|
|
GST_DEBUG(0, "seek cur pending to %lld", src->curoffset);
|
2002-01-26 18:28:55 +00:00
|
|
|
break;
|
2002-05-26 21:54:27 +00:00
|
|
|
case GST_SEEK_METHOD_END:
|
2002-06-22 09:23:40 +00:00
|
|
|
if (ABS (offset) > src->filelen)
|
2002-07-08 19:22:02 +00:00
|
|
|
goto error;
|
2002-06-22 09:23:40 +00:00
|
|
|
src->curoffset = src->filelen - ABS (offset);
|
2002-05-31 21:36:11 +00:00
|
|
|
GST_DEBUG(0, "seek end pending to %lld", src->curoffset);
|
2002-01-26 18:28:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
2002-07-08 19:22:02 +00:00
|
|
|
goto error;
|
2002-01-26 18:28:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-02-05 21:11:43 +00:00
|
|
|
g_object_notify (G_OBJECT (src), "offset");
|
2001-10-21 21:58:29 +00:00
|
|
|
src->seek_happened = TRUE;
|
2002-05-26 21:54:27 +00:00
|
|
|
src->need_flush = GST_EVENT_SEEK_FLAGS(event) & GST_SEEK_FLAG_FLUSH;
|
2002-02-02 13:24:25 +00:00
|
|
|
break;
|
2002-06-22 09:23:40 +00:00
|
|
|
}
|
2002-06-13 22:27:40 +00:00
|
|
|
case GST_EVENT_SIZE:
|
|
|
|
if (GST_EVENT_SIZE_FORMAT (event) != GST_FORMAT_BYTES) {
|
2002-07-08 19:22:02 +00:00
|
|
|
goto error;
|
2002-06-13 22:27:40 +00:00
|
|
|
}
|
|
|
|
src->block_size = GST_EVENT_SIZE_VALUE (event);
|
|
|
|
g_object_notify (G_OBJECT (src), "blocksize");
|
|
|
|
break;
|
2002-02-02 13:24:25 +00:00
|
|
|
case GST_EVENT_FLUSH:
|
|
|
|
src->need_flush = TRUE;
|
|
|
|
break;
|
2001-10-21 21:58:29 +00:00
|
|
|
default:
|
2002-07-08 19:22:02 +00:00
|
|
|
goto error;
|
2001-10-21 21:58:29 +00:00
|
|
|
break;
|
2001-09-10 19:46:01 +00:00
|
|
|
}
|
2002-07-08 19:22:02 +00:00
|
|
|
gst_event_unref (event);
|
2001-10-21 21:58:29 +00:00
|
|
|
return TRUE;
|
2002-07-08 19:22:02 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
gst_event_unref (event);
|
|
|
|
return FALSE;
|
2001-09-10 19:46:01 +00:00
|
|
|
}
|