2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #include <sys/types.h>
29 //#define GST_DEBUG_ENABLED
31 #include "gstasyncdisksrc.h"
34 GstElementDetails gst_asyncdisksrc_details = {
35 "Asynchronous Disk Source",
37 "Read from arbitrary point in a file",
39 "Erik Walthinsen <omega@cse.ogi.edu>",
44 /* AsyncDiskSrc signals and args */
59 static void gst_asyncdisksrc_class_init (GstAsyncDiskSrcClass *klass);
60 static void gst_asyncdisksrc_init (GstAsyncDiskSrc *asyncdisksrc);
62 static void gst_asyncdisksrc_set_arg (GtkObject *object, GtkArg *arg, guint id);
63 static void gst_asyncdisksrc_get_arg (GtkObject *object, GtkArg *arg, guint id);
65 static GstBuffer * gst_asyncdisksrc_get (GstPad *pad);
66 static GstBuffer * gst_asyncdisksrc_get_region (GstPad *pad, gulong offset, gulong size);
68 static GstElementStateReturn gst_asyncdisksrc_change_state (GstElement *element);
71 static GstElementClass *parent_class = NULL;
72 //static guint gst_asyncdisksrc_signals[LAST_SIGNAL] = { 0 };
75 gst_asyncdisksrc_get_type(void)
77 static GtkType asyncdisksrc_type = 0;
79 if (!asyncdisksrc_type) {
80 static const GtkTypeInfo asyncdisksrc_info = {
82 sizeof(GstAsyncDiskSrc),
83 sizeof(GstAsyncDiskSrcClass),
84 (GtkClassInitFunc)gst_asyncdisksrc_class_init,
85 (GtkObjectInitFunc)gst_asyncdisksrc_init,
86 (GtkArgSetFunc)gst_asyncdisksrc_set_arg,
87 (GtkArgGetFunc)gst_asyncdisksrc_get_arg,
88 (GtkClassInitFunc)NULL,
90 asyncdisksrc_type = gtk_type_unique (GST_TYPE_ELEMENT, &asyncdisksrc_info);
92 return asyncdisksrc_type;
96 gst_asyncdisksrc_class_init (GstAsyncDiskSrcClass *klass)
98 GtkObjectClass *gtkobject_class;
99 GstElementClass *gstelement_class;
101 gtkobject_class = (GtkObjectClass*)klass;
102 gstelement_class = (GstElementClass*)klass;
104 parent_class = gtk_type_class (GST_TYPE_ELEMENT);
106 gtk_object_add_arg_type ("GstAsyncDiskSrc::location", GST_TYPE_FILENAME,
107 GTK_ARG_READWRITE, ARG_LOCATION);
108 gtk_object_add_arg_type ("GstAsyncDiskSrc::bytesperread", GTK_TYPE_INT,
109 GTK_ARG_READWRITE, ARG_BYTESPERREAD);
110 gtk_object_add_arg_type ("GstAsyncDiskSrc::offset", GTK_TYPE_LONG,
111 GTK_ARG_READWRITE, ARG_OFFSET);
112 gtk_object_add_arg_type ("GstAsyncDiskSrc::size", GTK_TYPE_LONG,
113 GTK_ARG_READABLE, ARG_SIZE);
115 gtkobject_class->set_arg = gst_asyncdisksrc_set_arg;
116 gtkobject_class->get_arg = gst_asyncdisksrc_get_arg;
118 gstelement_class->change_state = gst_asyncdisksrc_change_state;
122 gst_asyncdisksrc_init (GstAsyncDiskSrc *asyncdisksrc)
124 // GST_FLAG_SET (asyncdisksrc, GST_SRC_ASYNC);
126 asyncdisksrc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
127 gst_pad_set_get_function (asyncdisksrc->srcpad,gst_asyncdisksrc_get);
128 gst_pad_set_getregion_function (asyncdisksrc->srcpad,gst_asyncdisksrc_get_region);
129 gst_element_add_pad (GST_ELEMENT (asyncdisksrc), asyncdisksrc->srcpad);
131 asyncdisksrc->filename = NULL;
132 asyncdisksrc->fd = 0;
133 asyncdisksrc->size = 0;
134 asyncdisksrc->map = NULL;
135 asyncdisksrc->curoffset = 0;
136 asyncdisksrc->bytes_per_read = 4096;
137 asyncdisksrc->seq = 0;
138 asyncdisksrc->new_seek = FALSE;
143 gst_asyncdisksrc_set_arg (GtkObject *object, GtkArg *arg, guint id)
145 GstAsyncDiskSrc *src;
147 /* it's not null if we got it, but it might not be ours */
148 g_return_if_fail (GST_IS_ASYNCDISKSRC (object));
150 src = GST_ASYNCDISKSRC (object);
154 /* the element must be stopped in order to do this */
155 g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
157 if (src->filename) g_free (src->filename);
158 /* clear the filename if we get a NULL (is that possible?) */
159 if (GTK_VALUE_STRING (*arg) == NULL) {
160 gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
161 src->filename = NULL;
162 /* otherwise set the new filename */
164 src->filename = g_strdup (GTK_VALUE_STRING (*arg));
167 case ARG_BYTESPERREAD:
168 src->bytes_per_read = GTK_VALUE_INT (*arg);
171 src->curoffset = GTK_VALUE_LONG (*arg);
172 src->new_seek = TRUE;
180 gst_asyncdisksrc_get_arg (GtkObject *object, GtkArg *arg, guint id)
182 GstAsyncDiskSrc *src;
184 /* it's not null if we got it, but it might not be ours */
185 g_return_if_fail (GST_IS_ASYNCDISKSRC (object));
187 src = GST_ASYNCDISKSRC (object);
191 GTK_VALUE_STRING (*arg) = src->filename;
193 case ARG_BYTESPERREAD:
194 GTK_VALUE_INT (*arg) = src->bytes_per_read;
197 GTK_VALUE_LONG (*arg) = src->curoffset;
200 GTK_VALUE_LONG (*arg) = src->size;
203 arg->type = GTK_TYPE_INVALID;
209 * gst_asyncdisksrc_get:
210 * @pad: #GstPad to push a buffer from
212 * Push a new buffer from the asyncdisksrc at the current offset.
215 gst_asyncdisksrc_get (GstPad *pad)
217 GstAsyncDiskSrc *src;
220 g_return_val_if_fail (pad != NULL, NULL);
221 src = GST_ASYNCDISKSRC (gst_pad_get_parent (pad));
222 g_return_val_if_fail (GST_FLAG_IS_SET (src, GST_ASYNCDISKSRC_OPEN), NULL);
224 /* deal with EOF state */
225 if (src->curoffset >= src->size) {
226 gst_element_signal_eos (GST_ELEMENT (src));
230 /* create the buffer */
231 // FIXME: should eventually use a bufferpool for this
232 buf = gst_buffer_new ();
234 g_return_val_if_fail (buf != NULL, NULL);
236 /* simply set the buffer to point to the correct region of the file */
237 GST_BUFFER_DATA (buf) = src->map + src->curoffset;
238 GST_BUFFER_OFFSET (buf) = src->curoffset;
239 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_DONTFREE);
241 if ((src->curoffset + src->bytes_per_read) > src->size) {
242 GST_BUFFER_SIZE (buf) = src->size - src->curoffset;
243 // FIXME: set the buffer's EOF bit here
245 GST_BUFFER_SIZE (buf) = src->bytes_per_read;
247 GST_DEBUG (0,"map %p, offset %ld, size %d\n", src->map, src->curoffset, GST_BUFFER_SIZE (buf));
249 //gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
251 src->curoffset += GST_BUFFER_SIZE (buf);
254 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLUSH);
255 GST_DEBUG (0,"new seek\n");
256 src->new_seek = FALSE;
259 /* we're done, return the buffer */
264 * gst_asyncdisksrc_get_region:
265 * @src: #GstSrc to push a buffer from
266 * @offset: offset in file
267 * @size: number of bytes
269 * Push a new buffer from the asyncdisksrc of given size at given offset.
272 gst_asyncdisksrc_get_region (GstPad *pad, gulong offset, gulong size)
274 GstAsyncDiskSrc *src;
277 g_return_val_if_fail (pad != NULL, NULL);
279 src = GST_ASYNCDISKSRC (gst_pad_get_parent (pad));
281 g_return_val_if_fail (GST_IS_ASYNCDISKSRC (src), NULL);
282 g_return_val_if_fail (GST_FLAG_IS_SET (src, GST_ASYNCDISKSRC_OPEN), NULL);
284 /* deal with EOF state */
285 if (offset >= src->size) {
286 gst_element_signal_eos (GST_ELEMENT (src));
290 /* create the buffer */
291 // FIXME: should eventually use a bufferpool for this
292 buf = gst_buffer_new ();
293 g_return_val_if_fail (buf != NULL, NULL);
295 /* simply set the buffer to point to the correct region of the file */
296 GST_BUFFER_DATA (buf) = src->map + offset;
297 GST_BUFFER_OFFSET (buf) = offset;
298 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_DONTFREE);
300 if ((offset + size) > src->size) {
301 GST_BUFFER_SIZE (buf) = src->size - offset;
302 // FIXME: set the buffer's EOF bit here
304 GST_BUFFER_SIZE (buf) = size;
306 GST_DEBUG (0,"map %p, offset %ld, size %d\n", src->map, offset, GST_BUFFER_SIZE (buf));
308 /* we're done, return the buffer off now */
313 /* open the file and mmap it, necessary to go to READY state */
315 gboolean gst_asyncdisksrc_open_file (GstAsyncDiskSrc *src)
317 g_return_val_if_fail (!GST_FLAG_IS_SET (src ,GST_ASYNCDISKSRC_OPEN), FALSE);
320 src->fd = open (src->filename, O_RDONLY);
323 gst_element_error (GST_ELEMENT (src), g_strconcat("opening file \"", src->filename, "\"", NULL));
326 /* find the file length */
327 src->size = lseek (src->fd, 0, SEEK_END);
328 lseek (src->fd, 0, SEEK_SET);
329 /* map the file into memory */
330 src->map = mmap (NULL, src->size, PROT_READ, MAP_SHARED, src->fd, 0);
331 madvise (src->map,src->size, 2);
332 /* collapse state if that failed */
333 if (src->map == NULL) {
335 gst_element_error (GST_ELEMENT (src),"mmapping file");
338 GST_FLAG_SET (src, GST_ASYNCDISKSRC_OPEN);
339 src->new_seek = TRUE;
344 /* unmap and close the file */
346 gst_asyncdisksrc_close_file (GstAsyncDiskSrc *src)
348 g_return_if_fail (GST_FLAG_IS_SET (src, GST_ASYNCDISKSRC_OPEN));
350 /* unmap the file from memory */
351 munmap (src->map, src->size);
355 /* zero out a lot of our state */
361 src->new_seek = FALSE;
363 GST_FLAG_UNSET (src, GST_ASYNCDISKSRC_OPEN);
367 static GstElementStateReturn
368 gst_asyncdisksrc_change_state (GstElement *element)
370 g_return_val_if_fail (GST_IS_ASYNCDISKSRC (element), GST_STATE_FAILURE);
372 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
373 if (GST_FLAG_IS_SET (element, GST_ASYNCDISKSRC_OPEN))
374 gst_asyncdisksrc_close_file (GST_ASYNCDISKSRC (element));
376 if (!GST_FLAG_IS_SET (element, GST_ASYNCDISKSRC_OPEN)) {
377 if (!gst_asyncdisksrc_open_file (GST_ASYNCDISKSRC (element)))
378 return GST_STATE_FAILURE;
382 if (GST_ELEMENT_CLASS (parent_class)->change_state)
383 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
385 return GST_STATE_SUCCESS;