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>
36 #define DEFAULT_BLOCKSIZE 4096
38 GST_DEBUG_CATEGORY_STATIC (gst_fdsrc_debug);
39 #define GST_CAT_DEFAULT gst_fdsrc_debug
41 GstElementDetails gst_fdsrc_details = GST_ELEMENT_DETAILS (
44 "Synchronous read from a file",
45 "Erik Walthinsen <omega@cse.ogi.edu>"
49 /* FdSrc signals and args */
61 #define _do_init(bla) \
62 GST_DEBUG_CATEGORY_INIT (gst_fdsrc_debug, "fdsrc", 0, "fdsrc element");
64 GST_BOILERPLATE_FULL (GstFdSrc, gst_fdsrc, GstElement, GST_TYPE_ELEMENT, _do_init);
66 static void gst_fdsrc_set_property (GObject *object, guint prop_id,
67 const GValue *value, GParamSpec *pspec);
68 static void gst_fdsrc_get_property (GObject *object, guint prop_id,
69 GValue *value, GParamSpec *pspec);
71 static GstData * gst_fdsrc_get (GstPad *pad);
75 gst_fdsrc_base_init (gpointer g_class)
77 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
79 gst_element_class_set_details (gstelement_class, &gst_fdsrc_details);
82 gst_fdsrc_class_init (GstFdSrcClass *klass)
84 GObjectClass *gobject_class;
86 gobject_class = G_OBJECT_CLASS (klass);
89 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FD,
90 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
91 0, G_MAXINT, 0, G_PARAM_READWRITE));
92 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BLOCKSIZE,
93 g_param_spec_ulong ("blocksize", "Block size", "Size in bytes to read per buffer",
94 1, G_MAXULONG, DEFAULT_BLOCKSIZE, G_PARAM_READWRITE));
96 gobject_class->set_property = gst_fdsrc_set_property;
97 gobject_class->get_property = gst_fdsrc_get_property;
100 static void gst_fdsrc_init(GstFdSrc *fdsrc) {
101 fdsrc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
103 gst_pad_set_get_function (fdsrc->srcpad, gst_fdsrc_get);
104 gst_element_add_pad (GST_ELEMENT (fdsrc), fdsrc->srcpad);
107 fdsrc->curoffset = 0;
108 fdsrc->blocksize = DEFAULT_BLOCKSIZE;
114 gst_fdsrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
118 /* it's not null if we got it, but it might not be ours */
119 g_return_if_fail (GST_IS_FDSRC (object));
121 src = GST_FDSRC (object);
125 src->fd = g_value_get_int (value);
128 src->blocksize = g_value_get_ulong (value);
136 gst_fdsrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
140 /* it's not null if we got it, but it might not be ours */
141 g_return_if_fail (GST_IS_FDSRC (object));
143 src = GST_FDSRC (object);
147 g_value_set_ulong (value, src->blocksize);
150 g_value_set_int (value, src->fd);
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159 gst_fdsrc_get(GstPad *pad)
165 src = GST_FDSRC (gst_pad_get_parent (pad));
167 /* create the buffer */
168 buf = gst_buffer_new_and_alloc (src->blocksize);
170 /* read it in from the file */
171 readbytes = read (src->fd, GST_BUFFER_DATA (buf), src->blocksize);
173 /* if nothing was read, we're in eos */
174 if (readbytes == 0) {
175 gst_element_set_eos (GST_ELEMENT (src));
176 return GST_DATA (gst_event_new (GST_EVENT_EOS));
179 if (readbytes == -1) {
180 g_error ("Error reading from file descriptor. Ending stream.\n");
181 gst_element_set_eos (GST_ELEMENT (src));
182 return GST_DATA (gst_event_new (GST_EVENT_EOS));
185 GST_BUFFER_OFFSET (buf) = src->curoffset;
186 GST_BUFFER_SIZE (buf) = readbytes;
187 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
188 src->curoffset += readbytes;
190 /* we're done, return the buffer */
191 return GST_DATA (buf);