2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2001 Dominic Ludlam <dom@recoil.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 #include <sys/types.h>
30 /*#define GST_DEBUG_ENABLED*/
32 #include "gstmultidisksrc.h"
34 GstElementDetails gst_multidisksrc_details = {
38 "Read from multiple files in order",
40 "Dominic Ludlam <dom@openfx.org>",
44 /* DiskSrc signals and args */
55 static void gst_multidisksrc_class_init (GstMultiDiskSrcClass *klass);
56 static void gst_multidisksrc_init (GstMultiDiskSrc *disksrc);
58 static void gst_multidisksrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
59 static void gst_multidisksrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
61 static GstBuffer * gst_multidisksrc_get (GstPad *pad);
62 /*static GstBuffer * gst_multidisksrc_get_region (GstPad *pad,GstRegionType type,guint64 offset,guint64 len);*/
64 static GstElementStateReturn gst_multidisksrc_change_state (GstElement *element);
66 static gboolean gst_multidisksrc_open_file (GstMultiDiskSrc *src, GstPad *srcpad);
67 static void gst_multidisksrc_close_file (GstMultiDiskSrc *src);
69 static GstElementClass *parent_class = NULL;
70 static guint gst_multidisksrc_signals[LAST_SIGNAL] = { 0 };
73 gst_multidisksrc_get_type(void)
75 static GType multidisksrc_type = 0;
77 if (!multidisksrc_type) {
78 static const GTypeInfo multidisksrc_info = {
79 sizeof(GstMultiDiskSrcClass), NULL,
81 (GClassInitFunc)gst_multidisksrc_class_init,
84 sizeof(GstMultiDiskSrc),
86 (GInstanceInitFunc)gst_multidisksrc_init,
88 multidisksrc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstMultiDiskSrc", &multidisksrc_info, 0);
90 return multidisksrc_type;
94 gst_multidisksrc_class_init (GstMultiDiskSrcClass *klass)
96 GObjectClass *gobject_class;
97 GstElementClass *gstelement_class;
99 gobject_class = (GObjectClass*)klass;
100 gstelement_class = (GstElementClass*)klass;
102 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
104 gst_multidisksrc_signals[NEW_FILE] =
105 g_signal_new ("new_file", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
106 G_STRUCT_OFFSET (GstMultiDiskSrcClass, new_file), NULL, NULL,
107 g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
110 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LOCATIONS,
111 g_param_spec_pointer("locations","locations","locations",
112 G_PARAM_READWRITE)); /* CHECKME */
114 gobject_class->set_property = gst_multidisksrc_set_property;
115 gobject_class->get_property = gst_multidisksrc_get_property;
117 gstelement_class->change_state = gst_multidisksrc_change_state;
121 gst_multidisksrc_init (GstMultiDiskSrc *multidisksrc)
123 /* GST_FLAG_SET (disksrc, GST_SRC_); */
125 multidisksrc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
126 gst_pad_set_get_function (multidisksrc->srcpad,gst_multidisksrc_get);
127 /* gst_pad_set_getregion_function (multidisksrc->srcpad,gst_multidisksrc_get_region); */
128 gst_element_add_pad (GST_ELEMENT (multidisksrc), multidisksrc->srcpad);
130 multidisksrc->listptr = NULL;
131 multidisksrc->currentfilename = NULL;
132 multidisksrc->fd = 0;
133 multidisksrc->size = 0;
134 multidisksrc->map = NULL;
135 multidisksrc->new_seek = FALSE;
139 gst_multidisksrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
141 GstMultiDiskSrc *src;
143 /* it's not null if we got it, but it might not be ours */
144 g_return_if_fail (GST_IS_MULTIDISKSRC (object));
146 src = GST_MULTIDISKSRC (object);
150 /* the element must be stopped in order to do this */
151 g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
153 /* clear the filename if we get a NULL */
154 if (g_value_get_pointer (value) == NULL) {
155 gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
157 /* otherwise set the new filenames */
159 src->listptr = g_value_get_pointer (value);
168 gst_multidisksrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
170 GstMultiDiskSrc *src;
172 /* it's not null if we got it, but it might not be ours */
173 g_return_if_fail (GST_IS_MULTIDISKSRC (object));
175 src = GST_MULTIDISKSRC (object);
179 g_value_set_pointer (value, src->listptr);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 * @pad: #GstPad to push a buffer from
191 * Push a new buffer from the disksrc at the current offset.
194 gst_multidisksrc_get (GstPad *pad)
196 GstMultiDiskSrc *src;
200 g_return_val_if_fail (pad != NULL, NULL);
201 src = GST_MULTIDISKSRC (gst_pad_get_parent (pad));
203 if (GST_FLAG_IS_SET (src, GST_MULTIDISKSRC_OPEN))
204 gst_multidisksrc_close_file(src);
207 return GST_BUFFER(gst_event_new (GST_EVENT_EOS));
211 src->currentfilename = (gchar *) list->data;
212 src->listptr = src->listptr->next;
214 if (!gst_multidisksrc_open_file(src, pad))
217 /* emitted after the open, as the user may free the list and string from here*/
218 g_signal_emit(G_OBJECT(src), gst_multidisksrc_signals[NEW_FILE], 0, list);
220 /* create the buffer */
221 /* FIXME: should eventually use a bufferpool for this */
222 buf = gst_buffer_new ();
224 g_return_val_if_fail (buf != NULL, NULL);
226 /* simply set the buffer to point to the correct region of the file */
227 GST_BUFFER_DATA (buf) = src->map;
228 GST_BUFFER_OFFSET (buf) = 0;
229 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_DONTFREE);
232 /* fixme, do something here */
233 src->new_seek = FALSE;
236 /* we're done, return the buffer */
240 /* open the file and mmap it, necessary to go to READY state */
242 gboolean gst_multidisksrc_open_file (GstMultiDiskSrc *src, GstPad *srcpad)
244 g_return_val_if_fail (!GST_FLAG_IS_SET (src, GST_MULTIDISKSRC_OPEN), FALSE);
247 src->fd = open ((const char *) src->currentfilename, O_RDONLY);
251 gst_element_error (GST_ELEMENT (src), g_strconcat("opening file \"", src->currentfilename, "\"", NULL));
254 /* find the file length */
255 src->size = lseek (src->fd, 0, SEEK_END);
256 lseek (src->fd, 0, SEEK_SET);
257 /* map the file into memory */
258 src->map = mmap (NULL, src->size, PROT_READ, MAP_SHARED, src->fd, 0);
259 madvise (src->map,src->size, 2);
260 /* collapse state if that failed */
261 if (src->map == NULL) {
263 gst_element_error (GST_ELEMENT (src),"mmapping file");
266 GST_FLAG_SET (src, GST_MULTIDISKSRC_OPEN);
267 src->new_seek = TRUE;
272 /* unmap and close the file */
274 gst_multidisksrc_close_file (GstMultiDiskSrc *src)
276 g_return_if_fail (GST_FLAG_IS_SET (src, GST_MULTIDISKSRC_OPEN));
278 /* unmap the file from memory and close the file */
279 munmap (src->map, src->size);
282 /* zero out a lot of our state */
286 src->new_seek = FALSE;
288 GST_FLAG_UNSET (src, GST_MULTIDISKSRC_OPEN);
291 static GstElementStateReturn
292 gst_multidisksrc_change_state (GstElement *element)
294 g_return_val_if_fail (GST_IS_MULTIDISKSRC (element), GST_STATE_FAILURE);
296 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
297 if (GST_FLAG_IS_SET (element, GST_MULTIDISKSRC_OPEN))
298 gst_multidisksrc_close_file (GST_MULTIDISKSRC (element));
301 if (GST_ELEMENT_CLASS (parent_class)->change_state)
302 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
304 return GST_STATE_SUCCESS;