rename multidisksrc to multifilesrc
[platform/upstream/gstreamer.git] / plugins / elements / gstmultifilesrc.c
1 /* GStreamer
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>
5  *
6  * gstmultifilesrc.c:
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <sys/mman.h>
29 #include <errno.h>
30 #include <string.h>
31
32 #ifdef HAVE_CONFIG_H
33 #  include "config.h"
34 #endif
35
36 #include "../gst-i18n-lib.h"
37
38 #include "gstmultifilesrc.h"
39
40 GST_DEBUG_CATEGORY_STATIC (gst_multifilesrc_debug);
41 #define GST_CAT_DEFAULT gst_multifilesrc_debug
42
43 GstElementDetails gst_multifilesrc_details = GST_ELEMENT_DETAILS (
44   "Multi File Source",
45   "Source/File",
46   "Read from multiple files in order",
47   "Dominic Ludlam <dom@openfx.org>"
48 );
49
50 /* FileSrc signals and args */
51 enum {
52   NEW_FILE,
53   LAST_SIGNAL
54 };
55
56 enum {
57   ARG_0,
58   ARG_LOCATIONS
59 };
60
61 #define _do_init(bla) \
62     GST_DEBUG_CATEGORY_INIT (gst_multifilesrc_debug, "multifilesrc", 0, "multifilesrc element");
63
64 GST_BOILERPLATE_FULL (GstMultiFileSrc, gst_multifilesrc, GstElement, GST_TYPE_ELEMENT, _do_init);
65
66 static void             gst_multifilesrc_set_property   (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
67 static void             gst_multifilesrc_get_property   (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
68
69 static GstData *        gst_multifilesrc_get            (GstPad *pad);
70 /*static GstBuffer *    gst_multifilesrc_get_region     (GstPad *pad,GstRegionType type,guint64 offset,guint64 len);*/
71
72 static GstElementStateReturn    gst_multifilesrc_change_state   (GstElement *element);
73
74 static gboolean         gst_multifilesrc_open_file      (GstMultiFileSrc *src, GstPad *srcpad);
75 static void             gst_multifilesrc_close_file     (GstMultiFileSrc *src);
76
77 static guint gst_multifilesrc_signals[LAST_SIGNAL] = { 0 };
78
79 static void
80 gst_multifilesrc_base_init (gpointer g_class)
81 {
82   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
83   
84   gst_element_class_set_details (gstelement_class, &gst_multifilesrc_details);
85 }
86 static void
87 gst_multifilesrc_class_init (GstMultiFileSrcClass *klass)
88 {
89   GObjectClass *gobject_class;
90   GstElementClass *gstelement_class;
91
92   gobject_class = (GObjectClass*)klass;
93   gstelement_class = (GstElementClass*)klass;
94
95
96   gst_multifilesrc_signals[NEW_FILE] =
97     g_signal_new ("new_file", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
98                     G_STRUCT_OFFSET (GstMultiFileSrcClass, new_file), NULL, NULL,
99                     g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
100                     G_TYPE_POINTER);
101
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 */
105
106   gobject_class->set_property = gst_multifilesrc_set_property;
107   gobject_class->get_property = gst_multifilesrc_get_property;
108
109   gstelement_class->change_state = gst_multifilesrc_change_state;
110 }
111
112 static void
113 gst_multifilesrc_init (GstMultiFileSrc *multifilesrc)
114 {
115 /*  GST_FLAG_SET (filesrc, GST_SRC_); */
116
117   multifilesrc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
118   gst_pad_set_get_function (multifilesrc->srcpad,gst_multifilesrc_get);
119 /*  gst_pad_set_getregion_function (multifilesrc->srcpad,gst_multifilesrc_get_region); */
120   gst_element_add_pad (GST_ELEMENT (multifilesrc), multifilesrc->srcpad);
121
122   multifilesrc->listptr = NULL;
123   multifilesrc->currentfilename = NULL;
124   multifilesrc->fd = 0;
125   multifilesrc->size = 0;
126   multifilesrc->map = NULL;
127   multifilesrc->new_seek = FALSE;
128 }
129
130 static void
131 gst_multifilesrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
132 {
133   GstMultiFileSrc *src;
134
135   /* it's not null if we got it, but it might not be ours */
136   g_return_if_fail (GST_IS_MULTIFILESRC (object));
137
138   src = GST_MULTIFILESRC (object);
139
140   switch (prop_id) {
141     case ARG_LOCATIONS:
142       /* the element must be stopped in order to do this */
143       g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
144
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);
148         src->listptr = NULL;
149       /* otherwise set the new filenames */
150       } else {
151         src->listptr = g_value_get_pointer (value);
152       }
153       break;
154     default:
155       break;
156   }
157 }
158
159 static void
160 gst_multifilesrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
161 {
162   GstMultiFileSrc *src;
163
164   /* it's not null if we got it, but it might not be ours */
165   g_return_if_fail (GST_IS_MULTIFILESRC (object));
166
167   src = GST_MULTIFILESRC (object);
168
169   switch (prop_id) {
170     case ARG_LOCATIONS:
171       g_value_set_pointer (value, src->listptr);
172       break;
173     default:
174       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175       break;
176   }
177 }
178
179 /**
180  * gst_filesrc_get:
181  * @pad: #GstPad to push a buffer from
182  *
183  * Push a new buffer from the filesrc at the current offset.
184  */
185 static GstData *
186 gst_multifilesrc_get (GstPad *pad)
187 {
188   GstMultiFileSrc *src;
189   GstBuffer *buf;
190   GSList *list;
191
192   g_return_val_if_fail (pad != NULL, NULL);
193   src = GST_MULTIFILESRC (gst_pad_get_parent (pad));
194
195   if (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN))
196     gst_multifilesrc_close_file(src);
197
198   if (!src->listptr) {
199     return GST_DATA (gst_event_new (GST_EVENT_EOS));
200   }
201
202   list = src->listptr;
203   src->currentfilename = (gchar *) list->data;
204   src->listptr = src->listptr->next;
205
206   if (!gst_multifilesrc_open_file(src, pad))
207       return NULL;
208
209   /* emitted after the open, as the user may free the list and string from here*/
210   g_signal_emit(G_OBJECT(src), gst_multifilesrc_signals[NEW_FILE], 0, list);
211
212   /* create the buffer */
213   /* FIXME: should eventually use a bufferpool for this */
214   buf = gst_buffer_new ();
215
216   g_return_val_if_fail (buf != NULL, NULL);
217
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);
223
224   if (src->new_seek) {
225     /* fixme, do something here */
226     src->new_seek = FALSE;
227   }
228
229   /* we're done, return the buffer */
230   return GST_DATA (buf);
231 }
232
233 /* open the file and mmap it, necessary to go to READY state */
234 static
235 gboolean gst_multifilesrc_open_file (GstMultiFileSrc *src, GstPad *srcpad)
236 {
237   g_return_val_if_fail (!GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN), FALSE);
238
239   /* open the file */
240   src->fd = open ((const char *) src->currentfilename, O_RDONLY);
241
242   if (src->fd < 0) {
243       GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
244                          (_("Could not open file \"%s\" for reading."), src->currentfilename),
245                          GST_ERROR_SYSTEM);
246     return FALSE;
247
248   } else {
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) {
257       close (src->fd);
258       GST_ELEMENT_ERROR (src, RESOURCE, TOO_LAZY, (NULL),
259                          ("mmap call failed."));
260       return FALSE;
261     }
262     GST_FLAG_SET (src, GST_MULTIFILESRC_OPEN);
263     src->new_seek = TRUE;
264   }
265   return TRUE;
266 }
267
268 /* unmap and close the file */
269 static void
270 gst_multifilesrc_close_file (GstMultiFileSrc *src)
271 {
272   g_return_if_fail (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN));
273
274   /* unmap the file from memory and close the file */
275   munmap (src->map, src->size);
276   close (src->fd);
277
278   /* zero out a lot of our state */
279   src->fd = 0;
280   src->size = 0;
281   src->map = NULL;
282   src->new_seek = FALSE;
283
284   GST_FLAG_UNSET (src, GST_MULTIFILESRC_OPEN);
285 }
286
287 static GstElementStateReturn
288 gst_multifilesrc_change_state (GstElement *element)
289 {
290   g_return_val_if_fail (GST_IS_MULTIFILESRC (element), GST_STATE_FAILURE);
291
292   if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
293     if (GST_FLAG_IS_SET (element, GST_MULTIFILESRC_OPEN))
294       gst_multifilesrc_close_file (GST_MULTIFILESRC (element));
295   }
296
297   if (GST_ELEMENT_CLASS (parent_class)->change_state)
298     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
299
300   return GST_STATE_SUCCESS;
301 }