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 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
45 GST_DEBUG_CATEGORY_STATIC (gst_multifilesrc_debug);
46 #define GST_CAT_DEFAULT gst_multifilesrc_debug
48 GstElementDetails gst_multifilesrc_details =
49 GST_ELEMENT_DETAILS ("Multi File Source",
51 "Read from multiple files in order",
52 "Dominic Ludlam <dom@openfx.org>");
54 /* FileSrc signals and args */
68 #define _do_init(bla) \
69 GST_DEBUG_CATEGORY_INIT (gst_multifilesrc_debug, "multifilesrc", 0, "multifilesrc element");
71 GST_BOILERPLATE_FULL (GstMultiFileSrc, gst_multifilesrc, GstElement,
72 GST_TYPE_ELEMENT, _do_init);
74 static void gst_multifilesrc_set_property (GObject * object, guint prop_id,
75 const GValue * value, GParamSpec * pspec);
76 static void gst_multifilesrc_get_property (GObject * object, guint prop_id,
77 GValue * value, GParamSpec * pspec);
79 static GstData *gst_multifilesrc_get (GstPad * pad);
81 /*static GstBuffer * gst_multifilesrc_get_region (GstPad *pad,GstRegionType type,guint64 offset,guint64 len);*/
83 static GstElementStateReturn gst_multifilesrc_change_state (GstElement *
86 static gboolean gst_multifilesrc_open_file (GstMultiFileSrc * src,
88 static void gst_multifilesrc_close_file (GstMultiFileSrc * src);
90 static guint gst_multifilesrc_signals[LAST_SIGNAL] = { 0 };
93 gst_multifilesrc_base_init (gpointer g_class)
95 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
97 gst_element_class_add_pad_template (gstelement_class,
98 gst_static_pad_template_get (&srctemplate));
99 gst_element_class_set_details (gstelement_class, &gst_multifilesrc_details);
102 gst_multifilesrc_class_init (GstMultiFileSrcClass * klass)
104 GObjectClass *gobject_class;
105 GstElementClass *gstelement_class;
107 gobject_class = (GObjectClass *) klass;
108 gstelement_class = (GstElementClass *) klass;
110 gobject_class->set_property = gst_multifilesrc_set_property;
111 gobject_class->get_property = gst_multifilesrc_get_property;
113 gst_multifilesrc_signals[NEW_FILE] =
114 g_signal_new ("new-file", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
115 G_STRUCT_OFFSET (GstMultiFileSrcClass, new_file), NULL, NULL,
116 g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
119 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATIONS, g_param_spec_pointer ("locations", "locations", "locations", G_PARAM_READWRITE)); /* CHECKME */
120 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HAVENEWMEDIA,
121 g_param_spec_boolean ("newmedia", "newmedia",
122 "generate new media events?", FALSE, G_PARAM_READWRITE));
124 gstelement_class->change_state = gst_multifilesrc_change_state;
128 gst_multifilesrc_init (GstMultiFileSrc * multifilesrc)
130 /* GST_FLAG_SET (filesrc, GST_SRC_); */
132 multifilesrc->srcpad =
133 gst_pad_new_from_template (gst_static_pad_template_get (&srctemplate),
135 gst_pad_set_get_function (multifilesrc->srcpad, gst_multifilesrc_get);
136 /* gst_pad_set_getregion_function (multifilesrc->srcpad,gst_multifilesrc_get_region); */
137 gst_element_add_pad (GST_ELEMENT (multifilesrc), multifilesrc->srcpad);
139 multifilesrc->listptr = NULL;
140 multifilesrc->currentfilename = NULL;
141 multifilesrc->fd = 0;
142 multifilesrc->size = 0;
143 multifilesrc->map = NULL;
144 multifilesrc->new_seek = FALSE;
145 multifilesrc->have_newmedia_events = FALSE;
149 gst_multifilesrc_set_property (GObject * object, guint prop_id,
150 const GValue * value, GParamSpec * pspec)
152 GstMultiFileSrc *src;
154 g_return_if_fail (GST_IS_MULTIFILESRC (object));
156 src = GST_MULTIFILESRC (object);
160 /* the element must be stopped in order to do this */
161 g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
163 /* clear the filename if we get a NULL */
164 if (g_value_get_pointer (value) == NULL) {
165 gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
167 /* otherwise set the new filenames */
169 src->listptr = g_value_get_pointer (value);
172 case ARG_HAVENEWMEDIA:
173 src->have_newmedia_events = g_value_get_boolean (value);
176 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182 gst_multifilesrc_get_property (GObject * object, guint prop_id, GValue * value,
185 GstMultiFileSrc *src;
187 g_return_if_fail (GST_IS_MULTIFILESRC (object));
189 src = GST_MULTIFILESRC (object);
193 g_value_set_pointer (value, src->listptr);
195 case ARG_HAVENEWMEDIA:
196 g_value_set_boolean (value, src->have_newmedia_events);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
206 * @pad: #GstPad to push a buffer from
208 * Push a new buffer from the filesrc at the current offset.
211 gst_multifilesrc_get (GstPad * pad)
213 GstMultiFileSrc *src;
219 g_return_val_if_fail (pad != NULL, NULL);
220 src = GST_MULTIFILESRC (gst_pad_get_parent (pad));
222 GST_DEBUG ("curfileindex = %d newmedia flag = %s", src->curfileindex,
223 GST_FLAG_IS_SET (src, GST_MULTIFILESRC_NEWFILE) ? "true" : "false");
225 switch (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_NEWFILE)) {
227 if (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN))
228 gst_multifilesrc_close_file (src);
231 GST_DEBUG ("sending EOS event");
232 gst_element_set_eos (GST_ELEMENT (src));
233 return GST_DATA (gst_event_new (GST_EVENT_EOS));
237 src->currentfilename = (gchar *) list->data;
238 src->listptr = src->listptr->next;
240 if (!gst_multifilesrc_open_file (src, pad))
243 /* emitted after the open, as the user may free the list and string from here */
244 g_signal_emit (G_OBJECT (src), gst_multifilesrc_signals[NEW_FILE], 0,
246 if (src->have_newmedia_events) {
248 gst_event_new_discontinuous (TRUE, GST_FORMAT_TIME, (gint64) 0,
249 GST_FORMAT_UNDEFINED);
250 GST_FLAG_SET (src, GST_MULTIFILESRC_NEWFILE);
252 GST_DEBUG ("sending new media event");
253 return GST_DATA (newmedia);
256 if (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_NEWFILE))
257 GST_FLAG_UNSET (src, GST_MULTIFILESRC_NEWFILE);
258 /* create the buffer */
259 /* FIXME: should eventually use a bufferpool for this */
260 buf = gst_buffer_new ();
262 g_return_val_if_fail (buf != NULL, NULL);
264 /* simply set the buffer to point to the correct region of the file */
265 GST_BUFFER_DATA (buf) = src->map;
266 GST_BUFFER_SIZE (buf) = src->size;
267 GST_BUFFER_OFFSET (buf) = 0;
268 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_DONTFREE);
271 /* fixme, do something here */
272 src->new_seek = FALSE;
275 /* we're done, return the buffer */
276 GST_DEBUG ("sending buffer");
277 return GST_DATA (buf);
280 /* should not reach here */
281 g_assert_not_reached ();
285 /* open the file and mmap it, necessary to go to READY state */
287 gst_multifilesrc_open_file (GstMultiFileSrc * src, GstPad * srcpad)
289 g_return_val_if_fail (!GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN), FALSE);
291 if (src->currentfilename == NULL || src->currentfilename[0] == '\0') {
292 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
293 (_("No file name specified for reading.")), (NULL));
297 /* open the file. FIXME: do we need to use O_LARGEFILE here? */
298 src->fd = open ((const char *) src->currentfilename, O_RDONLY);
300 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
301 (_("Could not open file \"%s\" for reading."), src->currentfilename),
306 /* find the file length */
307 src->size = lseek (src->fd, 0, SEEK_END);
308 lseek (src->fd, 0, SEEK_SET);
309 /* map the file into memory.
310 * FIXME: don't map the whole file at once, there might
311 * be restrictions set. Get max size via getrlimit
312 * or re-try with smaller size if mmap fails with ENOMEM? */
313 src->map = mmap (NULL, src->size, PROT_READ, MAP_SHARED, src->fd, 0);
314 madvise (src->map, src->size, MADV_SEQUENTIAL);
315 /* collapse state if that failed */
316 if (src->map == NULL) {
318 GST_ELEMENT_ERROR (src, RESOURCE, TOO_LAZY, (NULL),
319 ("mmap call failed."));
322 GST_FLAG_SET (src, GST_MULTIFILESRC_OPEN);
323 src->new_seek = TRUE;
328 /* unmap and close the file */
330 gst_multifilesrc_close_file (GstMultiFileSrc * src)
332 g_return_if_fail (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN));
334 /* unmap the file from memory and close the file */
335 munmap (src->map, src->size);
338 /* zero out a lot of our state */
342 src->new_seek = FALSE;
344 GST_FLAG_UNSET (src, GST_MULTIFILESRC_OPEN);
347 static GstElementStateReturn
348 gst_multifilesrc_change_state (GstElement * element)
350 g_return_val_if_fail (GST_IS_MULTIFILESRC (element), GST_STATE_FAILURE);
352 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
353 if (GST_FLAG_IS_SET (element, GST_MULTIFILESRC_OPEN))
354 gst_multifilesrc_close_file (GST_MULTIFILESRC (element));
357 if (GST_ELEMENT_CLASS (parent_class)->change_state)
358 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
360 return GST_STATE_SUCCESS;