making _set_property warn on unknown properties use GST_TIME_FORMAT for timestamp...
[platform/upstream/gstreamer.git] / gst / 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       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161       break;
162   }
163 }
164
165 static void
166 gst_multifilesrc_get_property (GObject * object, guint prop_id, GValue * value,
167     GParamSpec * pspec)
168 {
169   GstMultiFileSrc *src;
170
171   /* it's not null if we got it, but it might not be ours */
172   g_return_if_fail (GST_IS_MULTIFILESRC (object));
173
174   src = GST_MULTIFILESRC (object);
175
176   switch (prop_id) {
177     case ARG_LOCATIONS:
178       g_value_set_pointer (value, src->listptr);
179       break;
180     default:
181       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182       break;
183   }
184 }
185
186 /**
187  * gst_filesrc_get:
188  * @pad: #GstPad to push a buffer from
189  *
190  * Push a new buffer from the filesrc at the current offset.
191  */
192 static GstData *
193 gst_multifilesrc_get (GstPad * pad)
194 {
195   GstMultiFileSrc *src;
196   GstBuffer *buf;
197   GSList *list;
198
199   g_return_val_if_fail (pad != NULL, NULL);
200   src = GST_MULTIFILESRC (gst_pad_get_parent (pad));
201
202   if (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN))
203     gst_multifilesrc_close_file (src);
204
205   if (!src->listptr) {
206     return GST_DATA (gst_event_new (GST_EVENT_EOS));
207   }
208
209   list = src->listptr;
210   src->currentfilename = (gchar *) list->data;
211   src->listptr = src->listptr->next;
212
213   if (!gst_multifilesrc_open_file (src, pad))
214     return NULL;
215
216   /* emitted after the open, as the user may free the list and string from here */
217   g_signal_emit (G_OBJECT (src), gst_multifilesrc_signals[NEW_FILE], 0, list);
218
219   /* create the buffer */
220   /* FIXME: should eventually use a bufferpool for this */
221   buf = gst_buffer_new ();
222
223   g_return_val_if_fail (buf != NULL, NULL);
224
225   /* simply set the buffer to point to the correct region of the file */
226   GST_BUFFER_DATA (buf) = src->map;
227   GST_BUFFER_SIZE (buf) = src->size;
228   GST_BUFFER_OFFSET (buf) = 0;
229   GST_BUFFER_FLAG_SET (buf, GST_BUFFER_DONTFREE);
230
231   if (src->new_seek) {
232     /* fixme, do something here */
233     src->new_seek = FALSE;
234   }
235
236   /* we're done, return the buffer */
237   return GST_DATA (buf);
238 }
239
240 /* open the file and mmap it, necessary to go to READY state */
241 static gboolean
242 gst_multifilesrc_open_file (GstMultiFileSrc * src, GstPad * srcpad)
243 {
244   g_return_val_if_fail (!GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN), FALSE);
245
246   if (src->currentfilename == NULL || src->currentfilename[0] == '\0') {
247     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
248         (_("No file name specified for reading.")), (NULL));
249     return FALSE;
250   }
251
252   /* open the file. FIXME: do we need to use O_LARGEFILE here? */
253   src->fd = open ((const char *) src->currentfilename, O_RDONLY);
254   if (src->fd < 0) {
255     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
256         (_("Could not open file \"%s\" for reading."), src->currentfilename),
257         GST_ERROR_SYSTEM);
258     return FALSE;
259
260   } else {
261     /* find the file length */
262     src->size = lseek (src->fd, 0, SEEK_END);
263     lseek (src->fd, 0, SEEK_SET);
264     /* map the file into memory. 
265      * FIXME: don't map the whole file at once, there might
266      *        be restrictions set. Get max size via getrlimit
267      *        or re-try with smaller size if mmap fails with ENOMEM? */
268     src->map = mmap (NULL, src->size, PROT_READ, MAP_SHARED, src->fd, 0);
269     madvise (src->map, src->size, MADV_SEQUENTIAL);
270     /* collapse state if that failed */
271     if (src->map == NULL) {
272       close (src->fd);
273       GST_ELEMENT_ERROR (src, RESOURCE, TOO_LAZY, (NULL),
274           ("mmap call failed."));
275       return FALSE;
276     }
277     GST_FLAG_SET (src, GST_MULTIFILESRC_OPEN);
278     src->new_seek = TRUE;
279   }
280   return TRUE;
281 }
282
283 /* unmap and close the file */
284 static void
285 gst_multifilesrc_close_file (GstMultiFileSrc * src)
286 {
287   g_return_if_fail (GST_FLAG_IS_SET (src, GST_MULTIFILESRC_OPEN));
288
289   /* unmap the file from memory and close the file */
290   munmap (src->map, src->size);
291   close (src->fd);
292
293   /* zero out a lot of our state */
294   src->fd = 0;
295   src->size = 0;
296   src->map = NULL;
297   src->new_seek = FALSE;
298
299   GST_FLAG_UNSET (src, GST_MULTIFILESRC_OPEN);
300 }
301
302 static GstElementStateReturn
303 gst_multifilesrc_change_state (GstElement * element)
304 {
305   g_return_val_if_fail (GST_IS_MULTIFILESRC (element), GST_STATE_FAILURE);
306
307   if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
308     if (GST_FLAG_IS_SET (element, GST_MULTIFILESRC_OPEN))
309       gst_multifilesrc_close_file (GST_MULTIFILESRC (element));
310   }
311
312   if (GST_ELEMENT_CLASS (parent_class)->change_state)
313     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
314
315   return GST_STATE_SUCCESS;
316 }