2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include <sys/types.h>
30 GstElementDetails gst_fdsrc_details = {
33 "Synchronous read from a file",
35 "Erik Walthinsen <omega@cse.ogi.edu>",
40 /* FdSrc signals and args */
54 static void gst_fdsrc_class_init (GstFdSrcClass *klass);
55 static void gst_fdsrc_init (GstFdSrc *fdsrc);
57 static void gst_fdsrc_set_arg (GtkObject *object, GtkArg *arg, guint id);
58 static void gst_fdsrc_get_arg (GtkObject *object, GtkArg *arg, guint id);
60 static void gst_fdsrc_pull (GstPad *pad);
63 static GstSrcClass *parent_class = NULL;
64 //static guint gst_fdsrc_signals[LAST_SIGNAL] = { 0 };
67 gst_fdsrc_get_type (void)
69 static GtkType fdsrc_type = 0;
72 static const GtkTypeInfo fdsrc_info = {
75 sizeof(GstFdSrcClass),
76 (GtkClassInitFunc)gst_fdsrc_class_init,
77 (GtkObjectInitFunc)gst_fdsrc_init,
78 (GtkArgSetFunc)gst_fdsrc_set_arg,
79 (GtkArgGetFunc)gst_fdsrc_get_arg,
80 (GtkClassInitFunc)NULL,
82 fdsrc_type = gtk_type_unique (GST_TYPE_SRC, &fdsrc_info);
88 gst_fdsrc_class_init (GstFdSrcClass *klass)
90 GtkObjectClass *gtkobject_class;
91 GstSrcClass *gstsrc_class;
93 gtkobject_class = (GtkObjectClass*)klass;
94 gstsrc_class = (GstSrcClass*)klass;
96 parent_class = gtk_type_class(GST_TYPE_SRC);
98 gtk_object_add_arg_type ("GstFdSrc::location", GST_TYPE_FILENAME,
99 GTK_ARG_WRITABLE, ARG_LOCATION);
100 gtk_object_add_arg_type ("GstFdSrc::bytesperread", GTK_TYPE_INT,
101 GTK_ARG_READWRITE, ARG_BYTESPERREAD);
102 gtk_object_add_arg_type ("GstFdSrc::offset", GTK_TYPE_INT,
103 GTK_ARG_READABLE, ARG_OFFSET);
105 gtkobject_class->set_arg = gst_fdsrc_set_arg;
106 gtkobject_class->get_arg = gst_fdsrc_get_arg;
109 static void gst_fdsrc_init(GstFdSrc *fdsrc) {
110 fdsrc->srcpad = gst_pad_new("src",GST_PAD_SRC);
111 gst_pad_set_pull_function(fdsrc->srcpad,gst_fdsrc_pull);
112 gst_element_add_pad(GST_ELEMENT(fdsrc),fdsrc->srcpad);
115 fdsrc->curoffset = 0;
116 fdsrc->bytes_per_read = 4096;
122 gst_fdsrc_set_arg (GtkObject *object, GtkArg *arg, guint id)
127 /* it's not null if we got it, but it might not be ours */
128 g_return_if_fail (GST_IS_FDSRC (object));
130 src = GST_FDSRC (object);
134 /* the element must not be playing in order to do this */
135 g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
137 /* if we get a NULL, consider it to be a fd of 0 */
138 if (GTK_VALUE_STRING (*arg) == NULL) {
139 gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
141 /* otherwise set the new filename */
143 if (sscanf (GTK_VALUE_STRING (*arg), "%d", &fd))
147 case ARG_BYTESPERREAD:
148 src->bytes_per_read = GTK_VALUE_INT (*arg);
156 gst_fdsrc_get_arg (GtkObject *object, GtkArg *arg, guint id)
160 /* it's not null if we got it, but it might not be ours */
161 g_return_if_fail (GST_IS_FDSRC (object));
163 src = GST_FDSRC (object);
166 case ARG_BYTESPERREAD:
167 GTK_VALUE_INT (*arg) = src->bytes_per_read;
170 GTK_VALUE_INT (*arg) = src->curoffset;
173 arg->type = GTK_TYPE_INVALID;
178 void gst_fdsrc_pull(GstPad *pad) {
183 g_return_if_fail(pad != NULL);
184 src = GST_FDSRC(gst_pad_get_parent(pad));
186 /* create the buffer */
187 // FIXME: should eventually use a bufferpool for this
188 buf = gst_buffer_new ();
189 g_return_if_fail (buf);
191 /* allocate the space for the buffer data */
192 GST_BUFFER_DATA(buf) = g_malloc(src->bytes_per_read);
193 g_return_if_fail(GST_BUFFER_DATA(buf) != NULL);
195 /* read it in from the file */
196 readbytes = read(src->fd,GST_BUFFER_DATA(buf),src->bytes_per_read);
197 if (readbytes == 0) {
198 gst_src_signal_eos(GST_SRC(src));
202 /* if we didn't get as many bytes as we asked for, we're at EOF */
203 if (readbytes < src->bytes_per_read) {
204 // set the buffer's EOF bit here
205 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_EOS);
207 GST_BUFFER_OFFSET(buf) = src->curoffset;
208 GST_BUFFER_SIZE(buf) = readbytes;
209 src->curoffset += readbytes;
211 /* we're done, push the buffer off now */
212 gst_pad_push(pad,buf);