629993bed81e9fadbf0392665fbe4dc032726a72
[platform/upstream/gstreamer.git] / plugins / elements / gstmultidisksrc.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  * gstmultidisksrc.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
30 /*#define GST_DEBUG_ENABLED*/
31
32 #include "gstmultidisksrc.h"
33
34 GstElementDetails gst_multidisksrc_details = {
35   "Multi Disk Source",
36   "Source/File",
37   "LGPL",
38   "Read from multiple files in order",
39   VERSION,
40   "Dominic Ludlam <dom@openfx.org>",
41   "(C) 2001",
42 };
43
44 /* DiskSrc signals and args */
45 enum {
46   NEW_FILE,
47   LAST_SIGNAL
48 };
49
50 enum {
51   ARG_0,
52   ARG_LOCATIONS,
53 };
54
55 static void             gst_multidisksrc_class_init     (GstMultiDiskSrcClass *klass);
56 static void             gst_multidisksrc_init           (GstMultiDiskSrc *disksrc);
57
58 static void             gst_multidisksrc_set_property   (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
59 static void             gst_multidisksrc_get_property   (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
60
61 static GstBuffer *      gst_multidisksrc_get            (GstPad *pad);
62 /*static GstBuffer *    gst_multidisksrc_get_region     (GstPad *pad,GstRegionType type,guint64 offset,guint64 len);*/
63
64 static GstElementStateReturn    gst_multidisksrc_change_state   (GstElement *element);
65
66 static gboolean         gst_multidisksrc_open_file      (GstMultiDiskSrc *src, GstPad *srcpad);
67 static void             gst_multidisksrc_close_file     (GstMultiDiskSrc *src);
68
69 static GstElementClass *parent_class = NULL;
70 static guint gst_multidisksrc_signals[LAST_SIGNAL] = { 0 };
71
72 GType
73 gst_multidisksrc_get_type(void)
74 {
75   static GType multidisksrc_type = 0;
76
77   if (!multidisksrc_type) {
78     static const GTypeInfo multidisksrc_info = {
79       sizeof(GstMultiDiskSrcClass),      NULL,
80       NULL,
81       (GClassInitFunc)gst_multidisksrc_class_init,
82       NULL,
83       NULL,
84       sizeof(GstMultiDiskSrc),
85       0,
86       (GInstanceInitFunc)gst_multidisksrc_init,
87     };
88     multidisksrc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstMultiDiskSrc", &multidisksrc_info, 0);
89   }
90   return multidisksrc_type;
91 }
92
93 static void
94 gst_multidisksrc_class_init (GstMultiDiskSrcClass *klass)
95 {
96   GObjectClass *gobject_class;
97   GstElementClass *gstelement_class;
98
99   gobject_class = (GObjectClass*)klass;
100   gstelement_class = (GstElementClass*)klass;
101
102   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
103
104   gst_multidisksrc_signals[NEW_FILE] =
105     g_signal_new ("new_file", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
106                     G_STRUCT_OFFSET (GstMultiDiskSrcClass, new_file), NULL, NULL,
107                     g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
108                     G_TYPE_POINTER);
109
110   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LOCATIONS,
111     g_param_spec_pointer("locations","locations","locations",
112                         G_PARAM_READWRITE)); /* CHECKME */
113
114   gobject_class->set_property = gst_multidisksrc_set_property;
115   gobject_class->get_property = gst_multidisksrc_get_property;
116
117   gstelement_class->change_state = gst_multidisksrc_change_state;
118 }
119
120 static void
121 gst_multidisksrc_init (GstMultiDiskSrc *multidisksrc)
122 {
123 /*  GST_FLAG_SET (disksrc, GST_SRC_); */
124
125   multidisksrc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
126   gst_pad_set_get_function (multidisksrc->srcpad,gst_multidisksrc_get);
127 /*  gst_pad_set_getregion_function (multidisksrc->srcpad,gst_multidisksrc_get_region); */
128   gst_element_add_pad (GST_ELEMENT (multidisksrc), multidisksrc->srcpad);
129
130   multidisksrc->listptr = NULL;
131   multidisksrc->currentfilename = NULL;
132   multidisksrc->fd = 0;
133   multidisksrc->size = 0;
134   multidisksrc->map = NULL;
135   multidisksrc->new_seek = FALSE;
136 }
137
138 static void
139 gst_multidisksrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
140 {
141   GstMultiDiskSrc *src;
142
143   /* it's not null if we got it, but it might not be ours */
144   g_return_if_fail (GST_IS_MULTIDISKSRC (object));
145
146   src = GST_MULTIDISKSRC (object);
147
148   switch (prop_id) {
149     case ARG_LOCATIONS:
150       /* the element must be stopped in order to do this */
151       g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
152
153       /* clear the filename if we get a NULL */
154       if (g_value_get_pointer (value) == NULL) {
155         gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
156         src->listptr = NULL;
157       /* otherwise set the new filenames */
158       } else {
159         src->listptr = g_value_get_pointer (value);
160       }
161       break;
162     default:
163       break;
164   }
165 }
166
167 static void
168 gst_multidisksrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
169 {
170   GstMultiDiskSrc *src;
171
172   /* it's not null if we got it, but it might not be ours */
173   g_return_if_fail (GST_IS_MULTIDISKSRC (object));
174
175   src = GST_MULTIDISKSRC (object);
176
177   switch (prop_id) {
178     case ARG_LOCATIONS:
179       g_value_set_pointer (value, src->listptr);
180       break;
181     default:
182       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183       break;
184   }
185 }
186
187 /**
188  * gst_disksrc_get:
189  * @pad: #GstPad to push a buffer from
190  *
191  * Push a new buffer from the disksrc at the current offset.
192  */
193 static GstBuffer *
194 gst_multidisksrc_get (GstPad *pad)
195 {
196   GstMultiDiskSrc *src;
197   GstBuffer *buf;
198   GSList *list;
199
200   g_return_val_if_fail (pad != NULL, NULL);
201   src = GST_MULTIDISKSRC (gst_pad_get_parent (pad));
202
203   if (GST_FLAG_IS_SET (src, GST_MULTIDISKSRC_OPEN))
204     gst_multidisksrc_close_file(src);
205
206   if (!src->listptr) {
207     return GST_BUFFER(gst_event_new (GST_EVENT_EOS));
208   }
209
210   list = src->listptr;
211   src->currentfilename = (gchar *) list->data;
212   src->listptr = src->listptr->next;
213
214   if (!gst_multidisksrc_open_file(src, pad))
215       return NULL;
216
217   /* emitted after the open, as the user may free the list and string from here*/
218   g_signal_emit(G_OBJECT(src), gst_multidisksrc_signals[NEW_FILE], 0, list);
219
220   /* create the buffer */
221   /* FIXME: should eventually use a bufferpool for this */
222   buf = gst_buffer_new ();
223
224   g_return_val_if_fail (buf != NULL, NULL);
225
226   /* simply set the buffer to point to the correct region of the file */
227   GST_BUFFER_DATA (buf) = src->map;
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 buf;
238 }
239
240 /* open the file and mmap it, necessary to go to READY state */
241 static
242 gboolean gst_multidisksrc_open_file (GstMultiDiskSrc *src, GstPad *srcpad)
243 {
244   g_return_val_if_fail (!GST_FLAG_IS_SET (src, GST_MULTIDISKSRC_OPEN), FALSE);
245
246   /* open the file */
247   src->fd = open ((const char *) src->currentfilename, O_RDONLY);
248
249   if (src->fd < 0) {
250     perror ("open");
251     gst_element_error (GST_ELEMENT (src), g_strconcat("opening file \"", src->currentfilename, "\"", NULL));
252     return FALSE;
253   } else {
254     /* find the file length */
255     src->size = lseek (src->fd, 0, SEEK_END);
256     lseek (src->fd, 0, SEEK_SET);
257     /* map the file into memory */
258     src->map = mmap (NULL, src->size, PROT_READ, MAP_SHARED, src->fd, 0);
259     madvise (src->map,src->size, 2);
260     /* collapse state if that failed */
261     if (src->map == NULL) {
262       close (src->fd);
263       gst_element_error (GST_ELEMENT (src),"mmapping file");
264       return FALSE;
265     }
266     GST_FLAG_SET (src, GST_MULTIDISKSRC_OPEN);
267     src->new_seek = TRUE;
268   }
269   return TRUE;
270 }
271
272 /* unmap and close the file */
273 static void
274 gst_multidisksrc_close_file (GstMultiDiskSrc *src)
275 {
276   g_return_if_fail (GST_FLAG_IS_SET (src, GST_MULTIDISKSRC_OPEN));
277
278   /* unmap the file from memory and close the file */
279   munmap (src->map, src->size);
280   close (src->fd);
281
282   /* zero out a lot of our state */
283   src->fd = 0;
284   src->size = 0;
285   src->map = NULL;
286   src->new_seek = FALSE;
287
288   GST_FLAG_UNSET (src, GST_MULTIDISKSRC_OPEN);
289 }
290
291 static GstElementStateReturn
292 gst_multidisksrc_change_state (GstElement *element)
293 {
294   g_return_val_if_fail (GST_IS_MULTIDISKSRC (element), GST_STATE_FAILURE);
295
296   if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
297     if (GST_FLAG_IS_SET (element, GST_MULTIDISKSRC_OPEN))
298       gst_multidisksrc_close_file (GST_MULTIDISKSRC (element));
299   }
300
301   if (GST_ELEMENT_CLASS (parent_class)->change_state)
302     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
303
304   return GST_STATE_SUCCESS;
305 }