commit the fdsrc fix from Pedro Corte-Real
[platform/upstream/gstreamer.git] / gst / elements / gstfdsrc.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstfdsrc.c: 
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29
30 #include <gstfdsrc.h>
31
32 #define DEFAULT_BLOCKSIZE       4096
33
34 GstElementDetails gst_fdsrc_details = 
35 {
36   "Disk Source",
37   "Source/File",
38   "LGPL",
39   "Synchronous read from a file",
40   VERSION,
41   "Erik Walthinsen <omega@cse.ogi.edu>",
42   "(C) 1999",
43 };
44
45
46 /* FdSrc signals and args */
47 enum {
48   /* FILL ME */
49   LAST_SIGNAL
50 };
51
52 enum {
53   ARG_0,
54   ARG_FD,
55   ARG_BLOCKSIZE,
56 };
57
58
59 static void             gst_fdsrc_class_init    (GstFdSrcClass *klass);
60 static void             gst_fdsrc_init          (GstFdSrc *fdsrc);
61
62 static void             gst_fdsrc_set_property  (GObject *object, guint prop_id, 
63                                                  const GValue *value, GParamSpec *pspec);
64 static void             gst_fdsrc_get_property  (GObject *object, guint prop_id, 
65                                                  GValue *value, GParamSpec *pspec);
66
67 static GstBuffer *      gst_fdsrc_get           (GstPad *pad);
68
69
70 static GstElementClass *parent_class = NULL;
71 /*static guint gst_fdsrc_signals[LAST_SIGNAL] = { 0 };*/
72
73 GType
74 gst_fdsrc_get_type (void) 
75 {
76   static GType fdsrc_type = 0;
77
78   if (!fdsrc_type) {
79     static const GTypeInfo fdsrc_info = {
80       sizeof(GstFdSrcClass),      
81       NULL,
82       NULL,
83       (GClassInitFunc)gst_fdsrc_class_init,
84       NULL,
85       NULL,
86       sizeof(GstFdSrc),
87       0,
88       (GInstanceInitFunc)gst_fdsrc_init,
89     };
90     fdsrc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFdSrc", &fdsrc_info, 0);
91   }
92   return fdsrc_type;
93 }
94
95 static void
96 gst_fdsrc_class_init (GstFdSrcClass *klass) 
97 {
98   GObjectClass *gobject_class;
99
100   gobject_class = (GObjectClass*)klass;
101
102   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
103
104   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FD,
105     g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
106                       0, G_MAXINT, 0, G_PARAM_READWRITE));
107   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BLOCKSIZE,
108     g_param_spec_ulong ("blocksize", "Block size", "Size in bytes to read per buffer",
109                         1, G_MAXULONG, DEFAULT_BLOCKSIZE, G_PARAM_READWRITE));
110
111   gobject_class->set_property = gst_fdsrc_set_property;
112   gobject_class->get_property = gst_fdsrc_get_property;
113 }
114
115 static void gst_fdsrc_init(GstFdSrc *fdsrc) {
116   fdsrc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
117   
118   gst_pad_set_get_function (fdsrc->srcpad, gst_fdsrc_get);
119   gst_element_add_pad (GST_ELEMENT (fdsrc), fdsrc->srcpad);
120
121   fdsrc->fd = 0;
122   fdsrc->curoffset = 0;
123   fdsrc->blocksize = DEFAULT_BLOCKSIZE;
124   fdsrc->seq = 0;
125 }
126
127
128 static void 
129 gst_fdsrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
130 {
131   GstFdSrc *src;
132
133   /* it's not null if we got it, but it might not be ours */
134   g_return_if_fail (GST_IS_FDSRC (object));
135   
136   src = GST_FDSRC (object);
137
138   switch (prop_id) {
139     case ARG_FD:
140       src->fd = g_value_get_int (value);
141       break;
142     case ARG_BLOCKSIZE:
143       src->blocksize = g_value_get_ulong (value);
144       break;
145     default:
146       break;
147   }
148 }
149
150 static void 
151 gst_fdsrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) 
152 {
153   GstFdSrc *src;
154
155   /* it's not null if we got it, but it might not be ours */
156   g_return_if_fail (GST_IS_FDSRC (object));
157   
158   src = GST_FDSRC (object);
159
160   switch (prop_id) {
161     case ARG_BLOCKSIZE:
162       g_value_set_ulong (value, src->blocksize);
163       break;
164     case ARG_FD:
165       g_value_set_int (value, src->fd);
166       break;
167     default:
168       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169       break;
170   }
171 }
172
173 static GstBuffer *
174 gst_fdsrc_get(GstPad *pad)
175 {
176   GstFdSrc *src;
177   GstBuffer *buf;
178   glong readbytes;
179
180   src = GST_FDSRC (gst_pad_get_parent (pad));
181
182   /* create the buffer */
183   buf = gst_buffer_new_and_alloc (src->blocksize);
184
185   /* read it in from the file */
186   readbytes = read (src->fd, GST_BUFFER_DATA (buf), src->blocksize);
187
188   /* if nothing was read, we're in eos */
189   if (readbytes == 0) {
190     gst_element_set_eos (GST_ELEMENT (src));
191     return GST_BUFFER (gst_event_new (GST_EVENT_EOS));
192   }
193
194   if (readbytes == -1) {
195           g_error ("Error reading from file descriptor. Ending stream.\n");
196           gst_element_set_eos (GST_ELEMENT (src));
197           return GST_BUFFER (gst_event_new (GST_EVENT_EOS));
198           }
199   
200   GST_BUFFER_OFFSET (buf) = src->curoffset;
201   GST_BUFFER_SIZE (buf) = readbytes;
202   GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
203   src->curoffset += readbytes;
204
205   /* we're done, return the buffer */
206   return buf;
207 }