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 "gstmultidisksrc.h"
40 GST_DEBUG_CATEGORY_STATIC (gst_multidisksrc_debug);
41 #define GST_CAT_DEFAULT gst_multidisksrc_debug
43 GstElementDetails gst_multidisksrc_details = GST_ELEMENT_DETAILS (
46 "Read from multiple files in order",
47 "Dominic Ludlam <dom@openfx.org>"
50 /* DiskSrc signals and args */
61 #define _do_init(bla) \
62 GST_DEBUG_CATEGORY_INIT (gst_multidisksrc_debug, "multidisksrc", 0, "multidisksrc element");
64 GST_BOILERPLATE_FULL (GstMultiDiskSrc, gst_multidisksrc, GstElement, GST_TYPE_ELEMENT, _do_init);
66 static void gst_multidisksrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
67 static void gst_multidisksrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
69 static GstData * gst_multidisksrc_get (GstPad *pad);
70 /*static GstBuffer * gst_multidisksrc_get_region (GstPad *pad,GstRegionType type,guint64 offset,guint64 len);*/
72 static GstElementStateReturn gst_multidisksrc_change_state (GstElement *element);
74 static gboolean gst_multidisksrc_open_file (GstMultiDiskSrc *src, GstPad *srcpad);
75 static void gst_multidisksrc_close_file (GstMultiDiskSrc *src);
77 static guint gst_multidisksrc_signals[LAST_SIGNAL] = { 0 };
80 gst_multidisksrc_base_init (gpointer g_class)
82 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
84 gst_element_class_set_details (gstelement_class, &gst_multidisksrc_details);
87 gst_multidisksrc_class_init (GstMultiDiskSrcClass *klass)
89 GObjectClass *gobject_class;
90 GstElementClass *gstelement_class;
92 gobject_class = (GObjectClass*)klass;
93 gstelement_class = (GstElementClass*)klass;
96 gst_multidisksrc_signals[NEW_FILE] =
97 g_signal_new ("new_file", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
98 G_STRUCT_OFFSET (GstMultiDiskSrcClass, new_file), NULL, NULL,
99 g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
102 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LOCATIONS,
103 g_param_spec_pointer("locations","locations","locations",
104 G_PARAM_READWRITE)); /* CHECKME */
106 gobject_class->set_property = gst_multidisksrc_set_property;
107 gobject_class->get_property = gst_multidisksrc_get_property;
109 gstelement_class->change_state = gst_multidisksrc_change_state;
113 gst_multidisksrc_init (GstMultiDiskSrc *multidisksrc)
115 /* GST_FLAG_SET (disksrc, GST_SRC_); */
117 multidisksrc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
118 gst_pad_set_get_function (multidisksrc->srcpad,gst_multidisksrc_get);
119 /* gst_pad_set_getregion_function (multidisksrc->srcpad,gst_multidisksrc_get_region); */
120 gst_element_add_pad (GST_ELEMENT (multidisksrc), multidisksrc->srcpad);
122 multidisksrc->listptr = NULL;
123 multidisksrc->currentfilename = NULL;
124 multidisksrc->fd = 0;
125 multidisksrc->size = 0;
126 multidisksrc->map = NULL;
127 multidisksrc->new_seek = FALSE;
131 gst_multidisksrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
133 GstMultiDiskSrc *src;
135 /* it's not null if we got it, but it might not be ours */
136 g_return_if_fail (GST_IS_MULTIDISKSRC (object));
138 src = GST_MULTIDISKSRC (object);
142 /* the element must be stopped in order to do this */
143 g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
145 /* clear the filename if we get a NULL */
146 if (g_value_get_pointer (value) == NULL) {
147 gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
149 /* otherwise set the new filenames */
151 src->listptr = g_value_get_pointer (value);
160 gst_multidisksrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
162 GstMultiDiskSrc *src;
164 /* it's not null if we got it, but it might not be ours */
165 g_return_if_fail (GST_IS_MULTIDISKSRC (object));
167 src = GST_MULTIDISKSRC (object);
171 g_value_set_pointer (value, src->listptr);
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 * @pad: #GstPad to push a buffer from
183 * Push a new buffer from the disksrc at the current offset.
186 gst_multidisksrc_get (GstPad *pad)
188 GstMultiDiskSrc *src;
192 g_return_val_if_fail (pad != NULL, NULL);
193 src = GST_MULTIDISKSRC (gst_pad_get_parent (pad));
195 if (GST_FLAG_IS_SET (src, GST_MULTIDISKSRC_OPEN))
196 gst_multidisksrc_close_file(src);
199 return GST_DATA (gst_event_new (GST_EVENT_EOS));
203 src->currentfilename = (gchar *) list->data;
204 src->listptr = src->listptr->next;
206 if (!gst_multidisksrc_open_file(src, pad))
209 /* emitted after the open, as the user may free the list and string from here*/
210 g_signal_emit(G_OBJECT(src), gst_multidisksrc_signals[NEW_FILE], 0, list);
212 /* create the buffer */
213 /* FIXME: should eventually use a bufferpool for this */
214 buf = gst_buffer_new ();
216 g_return_val_if_fail (buf != NULL, NULL);
218 /* simply set the buffer to point to the correct region of the file */
219 GST_BUFFER_DATA (buf) = src->map;
220 GST_BUFFER_SIZE (buf) = src->size;
221 GST_BUFFER_OFFSET (buf) = 0;
222 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_DONTFREE);
225 /* fixme, do something here */
226 src->new_seek = FALSE;
229 /* we're done, return the buffer */
230 return GST_DATA (buf);
233 /* open the file and mmap it, necessary to go to READY state */
235 gboolean gst_multidisksrc_open_file (GstMultiDiskSrc *src, GstPad *srcpad)
237 g_return_val_if_fail (!GST_FLAG_IS_SET (src, GST_MULTIDISKSRC_OPEN), FALSE);
240 src->fd = open ((const char *) src->currentfilename, O_RDONLY);
243 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
244 (_("Could not open file \"%s\" for reading"), src->currentfilename),
249 /* find the file length */
250 src->size = lseek (src->fd, 0, SEEK_END);
251 lseek (src->fd, 0, SEEK_SET);
252 /* map the file into memory */
253 src->map = mmap (NULL, src->size, PROT_READ, MAP_SHARED, src->fd, 0);
254 madvise (src->map,src->size, 2);
255 /* collapse state if that failed */
256 if (src->map == NULL) {
258 GST_ELEMENT_ERROR (src, RESOURCE, TOO_LAZY,
260 ("mmap call failed"));
263 GST_FLAG_SET (src, GST_MULTIDISKSRC_OPEN);
264 src->new_seek = TRUE;
269 /* unmap and close the file */
271 gst_multidisksrc_close_file (GstMultiDiskSrc *src)
273 g_return_if_fail (GST_FLAG_IS_SET (src, GST_MULTIDISKSRC_OPEN));
275 /* unmap the file from memory and close the file */
276 munmap (src->map, src->size);
279 /* zero out a lot of our state */
283 src->new_seek = FALSE;
285 GST_FLAG_UNSET (src, GST_MULTIDISKSRC_OPEN);
288 static GstElementStateReturn
289 gst_multidisksrc_change_state (GstElement *element)
291 g_return_val_if_fail (GST_IS_MULTIDISKSRC (element), GST_STATE_FAILURE);
293 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
294 if (GST_FLAG_IS_SET (element, GST_MULTIDISKSRC_OPEN))
295 gst_multidisksrc_close_file (GST_MULTIDISKSRC (element));
298 if (GST_ELEMENT_CLASS (parent_class)->change_state)
299 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
301 return GST_STATE_SUCCESS;