gst-indent run on core
[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 =
44 GST_ELEMENT_DETAILS ("Multi File Source",
45     "Source/File",
46     "Read from multiple files in order",
47     "Dominic Ludlam <dom@openfx.org>");
48
49 /* FileSrc signals and args */
50 enum
51 {
52   NEW_FILE,
53   LAST_SIGNAL
54 };
55
56 enum
57 {
58   ARG_0,
59   ARG_LOCATIONS
60 };
61
62 #define _do_init(bla) \
63     GST_DEBUG_CATEGORY_INIT (gst_multifilesrc_debug, "multifilesrc", 0, "multifilesrc element");
64
65 GST_BOILERPLATE_FULL (GstMultiFileSrc, gst_multifilesrc, GstElement,
66     GST_TYPE_ELEMENT, _do_init);
67
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);
72
73 static GstData *gst_multifilesrc_get (GstPad * pad);
74
75 /*static GstBuffer *    gst_multifilesrc_get_region     (GstPad *pad,GstRegionType type,guint64 offset,guint64 len);*/
76
77 static GstElementStateReturn gst_multifilesrc_change_state (GstElement *
78     element);
79
80 static gboolean gst_multifilesrc_open_file (GstMultiFileSrc * src,
81     GstPad * srcpad);
82 static void gst_multifilesrc_close_file (GstMultiFileSrc * src);
83
84 static guint gst_multifilesrc_signals[LAST_SIGNAL] = { 0 };
85
86 static void
87 gst_multifilesrc_base_init (gpointer g_class)
88 {
89   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
90
91   gst_element_class_set_details (gstelement_class, &gst_multifilesrc_details);
92 }
93 static void
94 gst_multifilesrc_class_init (GstMultiFileSrcClass * klass)
95 {
96   GObjectClass *gobject_class;
97   GstElementClass *gstelement_class;
98
99   gobject_class = (GObjectClass *) klass;
100   gstelement_class = (GstElementClass *) klass;
101
102
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);
107
108   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATIONS, g_param_spec_pointer ("locations", "locations", "locations", G_PARAM_READWRITE));     /* CHECKME */
109
110   gobject_class->set_property = gst_multifilesrc_set_property;
111   gobject_class->get_property = gst_multifilesrc_get_property;
112
113   gstelement_class->change_state = gst_multifilesrc_change_state;
114 }
115
116 static void
117 gst_multifilesrc_init (GstMultiFileSrc * multifilesrc)
118 {
119 /*  GST_FLAG_SET (filesrc, GST_SRC_); */
120
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);
125
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;
132 }
133
134 static void
135 gst_multifilesrc_set_property (GObject * object, guint prop_id,
136     const GValue * value, GParamSpec * pspec)
137 {
138   GstMultiFileSrc *src;
139
140   /* it's not null if we got it, but it might not be ours */
141   g_return_if_fail (GST_IS_MULTIFILESRC (object));
142
143   src = GST_MULTIFILESRC (object);
144
145   switch (prop_id) {
146     case ARG_LOCATIONS:
147       /* the element must be stopped in order to do this */
148       g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
149
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);
153         src->listptr = NULL;
154         /* otherwise set the new filenames */
155       } else {
156         src->listptr = g_value_get_pointer (value);
157       }
158       break;
159     default:
160       break;
161   }
162 }
163
164 static void
165 gst_multifilesrc_get_property (GObject * object, guint prop_id, GValue * value,
166     GParamSpec * pspec)
167 {
168   GstMultiFileSrc *src;
169
170   /* it's not null if we got it, but it might not be ours */
171   g_return_if_fail (GST_IS_MULTIFILESRC (object));
172
173   src = GST_MULTIFILESRC (object);
174
175   switch (prop_id) {
176     case ARG_LOCATIONS:
177       g_value_set_pointer (value, src->listptr);
178       break;
179     default:
180       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181       break;
182   }
183 }
184
185 /**
186  * gst_filesrc_get:
187  * @pad: #GstPad to push a buffer from
188  *
189  * Push a new buffer from the filesrc at the current offset.
190  */
191 static GstData *
192 gst_multifilesrc_get (GstPad * pad)
193 {
194   GstMultiFileSrc *src;
195   GstBuffer *buf;
196   GSList *list;
197
198   g_return_val_if_fail (pad != NULL, NULL);
199   src = GST_MULTIFILESRC (gst_pad_get_parent (pad));
200
201   if (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN))
202     gst_multifilesrc_close_file (src);
203
204   if (!src->listptr) {
205     return GST_DATA (gst_event_new (GST_EVENT_EOS));
206   }
207
208   list = src->listptr;
209   src->currentfilename = (gchar *) list->data;
210   src->listptr = src->listptr->next;
211
212   if (!gst_multifilesrc_open_file (src, pad))
213     return NULL;
214
215   /* emitted after the open, as the user may free the list and string from here */
216   g_signal_emit (G_OBJECT (src), gst_multifilesrc_signals[NEW_FILE], 0, list);
217
218   /* create the buffer */
219   /* FIXME: should eventually use a bufferpool for this */
220   buf = gst_buffer_new ();
221
222   g_return_val_if_fail (buf != NULL, NULL);
223
224   /* simply set the buffer to point to the correct region of the file */
225   GST_BUFFER_DATA (buf) = src->map;
226   GST_BUFFER_SIZE (buf) = src->size;
227   GST_BUFFER_OFFSET (buf) = 0;
228   GST_BUFFER_FLAG_SET (buf, GST_BUFFER_DONTFREE);
229
230   if (src->new_seek) {
231     /* fixme, do something here */
232     src->new_seek = FALSE;
233   }
234
235   /* we're done, return the buffer */
236   return GST_DATA (buf);
237 }
238
239 /* open the file and mmap it, necessary to go to READY state */
240 static gboolean
241 gst_multifilesrc_open_file (GstMultiFileSrc * src, GstPad * srcpad)
242 {
243   g_return_val_if_fail (!GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN), FALSE);
244
245   if (src->currentfilename == NULL || src->currentfilename[0] == '\0') {
246     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
247         (_("No file name specified for reading.")), (NULL));
248     return FALSE;
249   }
250
251   /* open the file */
252   src->fd = open ((const char *) src->currentfilename, O_RDONLY);
253   if (src->fd < 0) {
254     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
255         (_("Could not open file \"%s\" for reading."), src->currentfilename),
256         GST_ERROR_SYSTEM);
257     return FALSE;
258
259   } else {
260     /* find the file length */
261     src->size = lseek (src->fd, 0, SEEK_END);
262     lseek (src->fd, 0, SEEK_SET);
263     /* map the file into memory */
264     src->map = mmap (NULL, src->size, PROT_READ, MAP_SHARED, src->fd, 0);
265     madvise (src->map, src->size, 2);
266     /* collapse state if that failed */
267     if (src->map == NULL) {
268       close (src->fd);
269       GST_ELEMENT_ERROR (src, RESOURCE, TOO_LAZY, (NULL),
270           ("mmap call failed."));
271       return FALSE;
272     }
273     GST_FLAG_SET (src, GST_MULTIFILESRC_OPEN);
274     src->new_seek = TRUE;
275   }
276   return TRUE;
277 }
278
279 /* unmap and close the file */
280 static void
281 gst_multifilesrc_close_file (GstMultiFileSrc * src)
282 {
283   g_return_if_fail (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN));
284
285   /* unmap the file from memory and close the file */
286   munmap (src->map, src->size);
287   close (src->fd);
288
289   /* zero out a lot of our state */
290   src->fd = 0;
291   src->size = 0;
292   src->map = NULL;
293   src->new_seek = FALSE;
294
295   GST_FLAG_UNSET (src, GST_MULTIFILESRC_OPEN);
296 }
297
298 static GstElementStateReturn
299 gst_multifilesrc_change_state (GstElement * element)
300 {
301   g_return_val_if_fail (GST_IS_MULTIFILESRC (element), GST_STATE_FAILURE);
302
303   if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
304     if (GST_FLAG_IS_SET (element, GST_MULTIFILESRC_OPEN))
305       gst_multifilesrc_close_file (GST_MULTIFILESRC (element));
306   }
307
308   if (GST_ELEMENT_CLASS (parent_class)->change_state)
309     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
310
311   return GST_STATE_SUCCESS;
312 }