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>
33 GstElementDetails gst_fdsrc_details = {
36 "Synchronous read from a file",
38 "Erik Walthinsen <omega@cse.ogi.edu>",
43 /* FdSrc signals and args */
57 static void gst_fdsrc_class_init (GstFdSrcClass *klass);
58 static void gst_fdsrc_init (GstFdSrc *fdsrc);
60 static void gst_fdsrc_set_arg (GtkObject *object, GtkArg *arg, guint id);
61 static void gst_fdsrc_get_arg (GtkObject *object, GtkArg *arg, guint id);
63 static GstBuffer * gst_fdsrc_get (GstPad *pad);
66 static GstElementClass *parent_class = NULL;
67 //static guint gst_fdsrc_signals[LAST_SIGNAL] = { 0 };
70 gst_fdsrc_get_type (void)
72 static GtkType fdsrc_type = 0;
75 static const GtkTypeInfo fdsrc_info = {
78 sizeof(GstFdSrcClass),
79 (GtkClassInitFunc)gst_fdsrc_class_init,
80 (GtkObjectInitFunc)gst_fdsrc_init,
81 (GtkArgSetFunc)gst_fdsrc_set_arg,
82 (GtkArgGetFunc)gst_fdsrc_get_arg,
83 (GtkClassInitFunc)NULL,
85 fdsrc_type = gtk_type_unique (GST_TYPE_ELEMENT, &fdsrc_info);
91 gst_fdsrc_class_init (GstFdSrcClass *klass)
93 GtkObjectClass *gtkobject_class;
95 gtkobject_class = (GtkObjectClass*)klass;
97 parent_class = gtk_type_class(GST_TYPE_ELEMENT);
99 gtk_object_add_arg_type ("GstFdSrc::location", GST_TYPE_FILENAME,
100 GTK_ARG_WRITABLE, ARG_LOCATION);
101 gtk_object_add_arg_type ("GstFdSrc::bytesperread", GTK_TYPE_INT,
102 GTK_ARG_READWRITE, ARG_BYTESPERREAD);
103 gtk_object_add_arg_type ("GstFdSrc::offset", GTK_TYPE_INT,
104 GTK_ARG_READABLE, ARG_OFFSET);
106 gtkobject_class->set_arg = gst_fdsrc_set_arg;
107 gtkobject_class->get_arg = gst_fdsrc_get_arg;
110 static void gst_fdsrc_init(GstFdSrc *fdsrc) {
111 fdsrc->srcpad = gst_pad_new("src",GST_PAD_SRC);
112 gst_pad_set_get_function(fdsrc->srcpad,gst_fdsrc_get);
113 gst_element_add_pad(GST_ELEMENT(fdsrc),fdsrc->srcpad);
116 fdsrc->curoffset = 0;
117 fdsrc->bytes_per_read = 4096;
123 gst_fdsrc_set_arg (GtkObject *object, GtkArg *arg, guint id)
128 /* it's not null if we got it, but it might not be ours */
129 g_return_if_fail (GST_IS_FDSRC (object));
131 src = GST_FDSRC (object);
135 /* the element must not be playing in order to do this */
136 g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
138 /* if we get a NULL, consider it to be a fd of 0 */
139 if (GTK_VALUE_STRING (*arg) == NULL) {
140 gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
142 /* otherwise set the new filename */
144 if (sscanf (GTK_VALUE_STRING (*arg), "%d", &fd))
148 case ARG_BYTESPERREAD:
149 src->bytes_per_read = GTK_VALUE_INT (*arg);
157 gst_fdsrc_get_arg (GtkObject *object, GtkArg *arg, guint id)
161 /* it's not null if we got it, but it might not be ours */
162 g_return_if_fail (GST_IS_FDSRC (object));
164 src = GST_FDSRC (object);
167 case ARG_BYTESPERREAD:
168 GTK_VALUE_INT (*arg) = src->bytes_per_read;
171 GTK_VALUE_INT (*arg) = src->curoffset;
174 arg->type = GTK_TYPE_INVALID;
180 gst_fdsrc_get(GstPad *pad)
186 g_return_val_if_fail (pad != NULL, NULL);
187 src = GST_FDSRC(gst_pad_get_parent(pad));
189 /* create the buffer */
190 // FIXME: should eventually use a bufferpool for this
191 buf = gst_buffer_new ();
192 g_return_val_if_fail (buf, NULL);
194 /* allocate the space for the buffer data */
195 GST_BUFFER_DATA(buf) = g_malloc(src->bytes_per_read);
196 g_return_val_if_fail(GST_BUFFER_DATA(buf) != NULL, NULL);
198 /* read it in from the file */
199 readbytes = read(src->fd,GST_BUFFER_DATA(buf),src->bytes_per_read);
200 if (readbytes == 0) {
201 gst_element_signal_eos(GST_ELEMENT(src));
205 /* if we didn't get as many bytes as we asked for, we're at EOF */
206 if (readbytes < src->bytes_per_read) {
207 // set the buffer's EOF bit here
208 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_EOS);
210 GST_BUFFER_OFFSET(buf) = src->curoffset;
211 GST_BUFFER_SIZE(buf) = readbytes;
212 src->curoffset += readbytes;
214 /* we're done, return the buffer */