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>
36 #include "../gst-i18n-lib.h"
38 #include "gstmultifilesrc.h"
40 GST_DEBUG_CATEGORY_STATIC (gst_multifilesrc_debug);
41 #define GST_CAT_DEFAULT gst_multifilesrc_debug
43 GstElementDetails gst_multifilesrc_details =
44 GST_ELEMENT_DETAILS ("Multi File Source",
46 "Read from multiple files in order",
47 "Dominic Ludlam <dom@openfx.org>");
49 /* FileSrc signals and args */
62 #define _do_init(bla) \
63 GST_DEBUG_CATEGORY_INIT (gst_multifilesrc_debug, "multifilesrc", 0, "multifilesrc element");
65 GST_BOILERPLATE_FULL (GstMultiFileSrc, gst_multifilesrc, GstElement,
66 GST_TYPE_ELEMENT, _do_init);
68 static void gst_multifilesrc_set_property (GObject * object, guint prop_id,
69 const GValue * value, GParamSpec * pspec);
70 static void gst_multifilesrc_get_property (GObject * object, guint prop_id,
71 GValue * value, GParamSpec * pspec);
73 static GstData *gst_multifilesrc_get (GstPad * pad);
75 /*static GstBuffer * gst_multifilesrc_get_region (GstPad *pad,GstRegionType type,guint64 offset,guint64 len);*/
77 static GstElementStateReturn gst_multifilesrc_change_state (GstElement *
80 static gboolean gst_multifilesrc_open_file (GstMultiFileSrc * src,
82 static void gst_multifilesrc_close_file (GstMultiFileSrc * src);
84 static guint gst_multifilesrc_signals[LAST_SIGNAL] = { 0 };
87 gst_multifilesrc_base_init (gpointer g_class)
89 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
91 gst_element_class_set_details (gstelement_class, &gst_multifilesrc_details);
94 gst_multifilesrc_class_init (GstMultiFileSrcClass * klass)
96 GObjectClass *gobject_class;
97 GstElementClass *gstelement_class;
99 gobject_class = (GObjectClass *) klass;
100 gstelement_class = (GstElementClass *) klass;
103 gst_multifilesrc_signals[NEW_FILE] =
104 g_signal_new ("new-file", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
105 G_STRUCT_OFFSET (GstMultiFileSrcClass, new_file), NULL, NULL,
106 g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
108 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATIONS, g_param_spec_pointer ("locations", "locations", "locations", G_PARAM_READWRITE)); /* CHECKME */
110 gobject_class->set_property = gst_multifilesrc_set_property;
111 gobject_class->get_property = gst_multifilesrc_get_property;
113 gstelement_class->change_state = gst_multifilesrc_change_state;
117 gst_multifilesrc_init (GstMultiFileSrc * multifilesrc)
119 /* GST_FLAG_SET (filesrc, GST_SRC_); */
121 multifilesrc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
122 gst_pad_set_get_function (multifilesrc->srcpad, gst_multifilesrc_get);
123 /* gst_pad_set_getregion_function (multifilesrc->srcpad,gst_multifilesrc_get_region); */
124 gst_element_add_pad (GST_ELEMENT (multifilesrc), multifilesrc->srcpad);
126 multifilesrc->listptr = NULL;
127 multifilesrc->currentfilename = NULL;
128 multifilesrc->fd = 0;
129 multifilesrc->size = 0;
130 multifilesrc->map = NULL;
131 multifilesrc->new_seek = FALSE;
135 gst_multifilesrc_set_property (GObject * object, guint prop_id,
136 const GValue * value, GParamSpec * pspec)
138 GstMultiFileSrc *src;
140 /* it's not null if we got it, but it might not be ours */
141 g_return_if_fail (GST_IS_MULTIFILESRC (object));
143 src = GST_MULTIFILESRC (object);
147 /* the element must be stopped in order to do this */
148 g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
150 /* clear the filename if we get a NULL */
151 if (g_value_get_pointer (value) == NULL) {
152 gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
154 /* otherwise set the new filenames */
156 src->listptr = g_value_get_pointer (value);
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166 gst_multifilesrc_get_property (GObject * object, guint prop_id, GValue * value,
169 GstMultiFileSrc *src;
171 /* it's not null if we got it, but it might not be ours */
172 g_return_if_fail (GST_IS_MULTIFILESRC (object));
174 src = GST_MULTIFILESRC (object);
178 g_value_set_pointer (value, src->listptr);
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 * @pad: #GstPad to push a buffer from
190 * Push a new buffer from the filesrc at the current offset.
193 gst_multifilesrc_get (GstPad * pad)
195 GstMultiFileSrc *src;
199 g_return_val_if_fail (pad != NULL, NULL);
200 src = GST_MULTIFILESRC (gst_pad_get_parent (pad));
202 if (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN))
203 gst_multifilesrc_close_file (src);
206 return GST_DATA (gst_event_new (GST_EVENT_EOS));
210 src->currentfilename = (gchar *) list->data;
211 src->listptr = src->listptr->next;
213 if (!gst_multifilesrc_open_file (src, pad))
216 /* emitted after the open, as the user may free the list and string from here */
217 g_signal_emit (G_OBJECT (src), gst_multifilesrc_signals[NEW_FILE], 0, list);
219 /* create the buffer */
220 /* FIXME: should eventually use a bufferpool for this */
221 buf = gst_buffer_new ();
223 g_return_val_if_fail (buf != NULL, NULL);
225 /* simply set the buffer to point to the correct region of the file */
226 GST_BUFFER_DATA (buf) = src->map;
227 GST_BUFFER_SIZE (buf) = src->size;
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 */
237 return GST_DATA (buf);
240 /* open the file and mmap it, necessary to go to READY state */
242 gst_multifilesrc_open_file (GstMultiFileSrc * src, GstPad * srcpad)
244 g_return_val_if_fail (!GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN), FALSE);
246 if (src->currentfilename == NULL || src->currentfilename[0] == '\0') {
247 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
248 (_("No file name specified for reading.")), (NULL));
252 /* open the file. FIXME: do we need to use O_LARGEFILE here? */
253 src->fd = open ((const char *) src->currentfilename, O_RDONLY);
255 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
256 (_("Could not open file \"%s\" for reading."), src->currentfilename),
261 /* find the file length */
262 src->size = lseek (src->fd, 0, SEEK_END);
263 lseek (src->fd, 0, SEEK_SET);
264 /* map the file into memory.
265 * FIXME: don't map the whole file at once, there might
266 * be restrictions set. Get max size via getrlimit
267 * or re-try with smaller size if mmap fails with ENOMEM? */
268 src->map = mmap (NULL, src->size, PROT_READ, MAP_SHARED, src->fd, 0);
269 madvise (src->map, src->size, MADV_SEQUENTIAL);
270 /* collapse state if that failed */
271 if (src->map == NULL) {
273 GST_ELEMENT_ERROR (src, RESOURCE, TOO_LAZY, (NULL),
274 ("mmap call failed."));
277 GST_FLAG_SET (src, GST_MULTIFILESRC_OPEN);
278 src->new_seek = TRUE;
283 /* unmap and close the file */
285 gst_multifilesrc_close_file (GstMultiFileSrc * src)
287 g_return_if_fail (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN));
289 /* unmap the file from memory and close the file */
290 munmap (src->map, src->size);
293 /* zero out a lot of our state */
297 src->new_seek = FALSE;
299 GST_FLAG_UNSET (src, GST_MULTIFILESRC_OPEN);
302 static GstElementStateReturn
303 gst_multifilesrc_change_state (GstElement * element)
305 g_return_val_if_fail (GST_IS_MULTIFILESRC (element), GST_STATE_FAILURE);
307 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
308 if (GST_FLAG_IS_SET (element, GST_MULTIFILESRC_OPEN))
309 gst_multifilesrc_close_file (GST_MULTIFILESRC (element));
312 if (GST_ELEMENT_CLASS (parent_class)->change_state)
313 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
315 return GST_STATE_SUCCESS;