Way, way, way too many files: Remove crack comment from the 2000 era.
[platform/upstream/gst-plugins-good.git] / gst / oldcore / 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 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
41     GST_PAD_SRC,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS_ANY);
44
45 GST_DEBUG_CATEGORY_STATIC (gst_multifilesrc_debug);
46 #define GST_CAT_DEFAULT gst_multifilesrc_debug
47
48 GstElementDetails gst_multifilesrc_details =
49 GST_ELEMENT_DETAILS ("Multi File Source",
50     "Source/File",
51     "Read from multiple files in order",
52     "Dominic Ludlam <dom@openfx.org>");
53
54 /* FileSrc signals and args */
55 enum
56 {
57   NEW_FILE,
58   LAST_SIGNAL
59 };
60
61 enum
62 {
63   ARG_0,
64   ARG_LOCATIONS,
65   ARG_HAVENEWMEDIA
66 };
67
68 #define _do_init(bla) \
69     GST_DEBUG_CATEGORY_INIT (gst_multifilesrc_debug, "multifilesrc", 0, "multifilesrc element");
70
71 GST_BOILERPLATE_FULL (GstMultiFileSrc, gst_multifilesrc, GstElement,
72     GST_TYPE_ELEMENT, _do_init);
73
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);
78
79 static GstData *gst_multifilesrc_get (GstPad * pad);
80
81 /*static GstBuffer *    gst_multifilesrc_get_region     (GstPad *pad,GstRegionType type,guint64 offset,guint64 len);*/
82
83 static GstElementStateReturn gst_multifilesrc_change_state (GstElement *
84     element);
85
86 static gboolean gst_multifilesrc_open_file (GstMultiFileSrc * src,
87     GstPad * srcpad);
88 static void gst_multifilesrc_close_file (GstMultiFileSrc * src);
89
90 static guint gst_multifilesrc_signals[LAST_SIGNAL] = { 0 };
91
92 static void
93 gst_multifilesrc_base_init (gpointer g_class)
94 {
95   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
96
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);
100 }
101 static void
102 gst_multifilesrc_class_init (GstMultiFileSrcClass * klass)
103 {
104   GObjectClass *gobject_class;
105   GstElementClass *gstelement_class;
106
107   gobject_class = (GObjectClass *) klass;
108   gstelement_class = (GstElementClass *) klass;
109
110   gobject_class->set_property = gst_multifilesrc_set_property;
111   gobject_class->get_property = gst_multifilesrc_get_property;
112
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);
117
118
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));
123
124   gstelement_class->change_state = gst_multifilesrc_change_state;
125 }
126
127 static void
128 gst_multifilesrc_init (GstMultiFileSrc * multifilesrc)
129 {
130 /*  GST_FLAG_SET (filesrc, GST_SRC_); */
131
132   multifilesrc->srcpad =
133       gst_pad_new_from_template (gst_static_pad_template_get (&srctemplate),
134       "src");
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);
138
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;
146 }
147
148 static void
149 gst_multifilesrc_set_property (GObject * object, guint prop_id,
150     const GValue * value, GParamSpec * pspec)
151 {
152   GstMultiFileSrc *src;
153
154   g_return_if_fail (GST_IS_MULTIFILESRC (object));
155
156   src = GST_MULTIFILESRC (object);
157
158   switch (prop_id) {
159     case ARG_LOCATIONS:
160       /* the element must be stopped in order to do this */
161       g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
162
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);
166         src->listptr = NULL;
167         /* otherwise set the new filenames */
168       } else {
169         src->listptr = g_value_get_pointer (value);
170       }
171       break;
172     case ARG_HAVENEWMEDIA:
173       src->have_newmedia_events = g_value_get_boolean (value);
174       break;
175     default:
176       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177       break;
178   }
179 }
180
181 static void
182 gst_multifilesrc_get_property (GObject * object, guint prop_id, GValue * value,
183     GParamSpec * pspec)
184 {
185   GstMultiFileSrc *src;
186
187   g_return_if_fail (GST_IS_MULTIFILESRC (object));
188
189   src = GST_MULTIFILESRC (object);
190
191   switch (prop_id) {
192     case ARG_LOCATIONS:
193       g_value_set_pointer (value, src->listptr);
194       break;
195     case ARG_HAVENEWMEDIA:
196       g_value_set_boolean (value, src->have_newmedia_events);
197       break;
198     default:
199       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200       break;
201   }
202 }
203
204 /**
205  * gst_filesrc_get:
206  * @pad: #GstPad to push a buffer from
207  *
208  * Push a new buffer from the filesrc at the current offset.
209  */
210 static GstData *
211 gst_multifilesrc_get (GstPad * pad)
212 {
213   GstMultiFileSrc *src;
214   GstBuffer *buf;
215   GstEvent *newmedia;
216   GSList *list;
217
218
219   g_return_val_if_fail (pad != NULL, NULL);
220   src = GST_MULTIFILESRC (gst_pad_get_parent (pad));
221
222   GST_DEBUG ("curfileindex = %d newmedia flag = %s", src->curfileindex,
223       GST_FLAG_IS_SET (src, GST_MULTIFILESRC_NEWFILE) ? "true" : "false");
224
225   switch (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_NEWFILE)) {
226     case FALSE:
227       if (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN))
228         gst_multifilesrc_close_file (src);
229
230       if (!src->listptr) {
231         GST_DEBUG ("sending EOS event");
232         gst_element_set_eos (GST_ELEMENT (src));
233         return GST_DATA (gst_event_new (GST_EVENT_EOS));
234       }
235
236       list = src->listptr;
237       src->currentfilename = (gchar *) list->data;
238       src->listptr = src->listptr->next;
239
240       if (!gst_multifilesrc_open_file (src, pad))
241         return NULL;
242       src->curfileindex++;
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,
245           list);
246       if (src->have_newmedia_events) {
247         newmedia =
248             gst_event_new_discontinuous (TRUE, GST_FORMAT_TIME, (gint64) 0,
249             GST_FORMAT_UNDEFINED);
250         GST_FLAG_SET (src, GST_MULTIFILESRC_NEWFILE);
251
252         GST_DEBUG ("sending new media event");
253         return GST_DATA (newmedia);
254       }
255     default:
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 ();
261
262       g_return_val_if_fail (buf != NULL, NULL);
263
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);
269
270       if (src->new_seek) {
271         /* fixme, do something here */
272         src->new_seek = FALSE;
273       }
274
275       /* we're done, return the buffer */
276       GST_DEBUG ("sending buffer");
277       return GST_DATA (buf);
278   }
279
280   /* should not reach here */
281   g_assert_not_reached ();
282   return NULL;
283 }
284
285 /* open the file and mmap it, necessary to go to READY state */
286 static gboolean
287 gst_multifilesrc_open_file (GstMultiFileSrc * src, GstPad * srcpad)
288 {
289   g_return_val_if_fail (!GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN), FALSE);
290
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));
294     return FALSE;
295   }
296
297   /* open the file. FIXME: do we need to use O_LARGEFILE here? */
298   src->fd = open ((const char *) src->currentfilename, O_RDONLY);
299   if (src->fd < 0) {
300     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
301         (_("Could not open file \"%s\" for reading."), src->currentfilename),
302         GST_ERROR_SYSTEM);
303     return FALSE;
304
305   } else {
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) {
317       close (src->fd);
318       GST_ELEMENT_ERROR (src, RESOURCE, TOO_LAZY, (NULL),
319           ("mmap call failed."));
320       return FALSE;
321     }
322     GST_FLAG_SET (src, GST_MULTIFILESRC_OPEN);
323     src->new_seek = TRUE;
324   }
325   return TRUE;
326 }
327
328 /* unmap and close the file */
329 static void
330 gst_multifilesrc_close_file (GstMultiFileSrc * src)
331 {
332   g_return_if_fail (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN));
333
334   /* unmap the file from memory and close the file */
335   munmap (src->map, src->size);
336   close (src->fd);
337
338   /* zero out a lot of our state */
339   src->fd = 0;
340   src->size = 0;
341   src->map = NULL;
342   src->new_seek = FALSE;
343
344   GST_FLAG_UNSET (src, GST_MULTIFILESRC_OPEN);
345 }
346
347 static GstElementStateReturn
348 gst_multifilesrc_change_state (GstElement * element)
349 {
350   g_return_val_if_fail (GST_IS_MULTIFILESRC (element), GST_STATE_FAILURE);
351
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));
355   }
356
357   if (GST_ELEMENT_CLASS (parent_class)->change_state)
358     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
359
360   return GST_STATE_SUCCESS;
361 }