more of the popular // fixes
[platform/upstream/gstreamer.git] / plugins / 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
33 GstElementDetails gst_fdsrc_details = {
34   "Disk Source",
35   "Source/File",
36   "Synchronous read from a file",
37   VERSION,
38   "Erik Walthinsen <omega@cse.ogi.edu>",
39   "(C) 1999",
40 };
41
42
43 /* FdSrc signals and args */
44 enum {
45   /* FILL ME */
46   LAST_SIGNAL
47 };
48
49 enum {
50   ARG_0,
51   ARG_LOCATION,
52   ARG_BYTESPERREAD,
53   ARG_OFFSET,
54 };
55
56
57 static void             gst_fdsrc_class_init    (GstFdSrcClass *klass);
58 static void             gst_fdsrc_init          (GstFdSrc *fdsrc);
59
60 static void             gst_fdsrc_set_property  (GObject *object, guint prop_id, 
61                                                  const GValue *value, GParamSpec *pspec);
62 static void             gst_fdsrc_get_property  (GObject *object, guint prop_id, 
63                                                  GValue *value, GParamSpec *pspec);
64
65 static GstBuffer *      gst_fdsrc_get           (GstPad *pad);
66
67
68 static GstElementClass *parent_class = NULL;
69 /*static guint gst_fdsrc_signals[LAST_SIGNAL] = { 0 };*/
70
71 GType
72 gst_fdsrc_get_type (void) 
73 {
74   static GType fdsrc_type = 0;
75
76   if (!fdsrc_type) {
77     static const GTypeInfo fdsrc_info = {
78       sizeof(GstFdSrcClass),      NULL,
79       NULL,
80       (GClassInitFunc)gst_fdsrc_class_init,
81       NULL,
82       NULL,
83       sizeof(GstFdSrc),
84       0,
85       (GInstanceInitFunc)gst_fdsrc_init,
86     };
87     fdsrc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFdSrc", &fdsrc_info, 0);
88   }
89   return fdsrc_type;
90 }
91
92 static void
93 gst_fdsrc_class_init (GstFdSrcClass *klass) 
94 {
95   GObjectClass *gobject_class;
96
97   gobject_class = (GObjectClass*)klass;
98
99   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
100
101   gst_element_install_std_props (
102           GST_ELEMENT_CLASS (klass),
103           "location",     ARG_LOCATION,     G_PARAM_WRITABLE,
104           "bytesperread", ARG_BYTESPERREAD, G_PARAM_READWRITE,
105           "offset",       ARG_OFFSET,       G_PARAM_READABLE,
106           NULL);
107
108   gobject_class->set_property = gst_fdsrc_set_property;
109   gobject_class->get_property = gst_fdsrc_get_property;
110 }
111
112 static void gst_fdsrc_init(GstFdSrc *fdsrc) {
113   fdsrc->srcpad = gst_pad_new("src",GST_PAD_SRC);
114   gst_pad_set_get_function(fdsrc->srcpad,gst_fdsrc_get);
115   gst_element_add_pad(GST_ELEMENT(fdsrc),fdsrc->srcpad);
116
117   fdsrc->fd = 0;
118   fdsrc->curoffset = 0;
119   fdsrc->bytes_per_read = 4096;
120   fdsrc->seq = 0;
121 }
122
123
124 static void 
125 gst_fdsrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
126 {
127   GstFdSrc *src;
128   int fd;
129
130   /* it's not null if we got it, but it might not be ours */
131   g_return_if_fail (GST_IS_FDSRC (object));
132   
133   src = GST_FDSRC (object);
134
135   switch (prop_id) {
136     case ARG_LOCATION:
137       /* the element must not be playing in order to do this */
138       g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
139
140       /* if we get a NULL, consider it to be a fd of 0 */
141       if (g_value_get_string (value) == NULL) {
142         gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
143         src->fd = 0;
144       /* otherwise set the new filename */
145       } else {
146         if (sscanf (g_value_get_string (value), "%d", &fd))
147           src->fd = fd;
148       }
149       break;
150     case ARG_BYTESPERREAD:
151       src->bytes_per_read = g_value_get_int (value);
152       break;
153     default:
154       break;
155   }
156 }
157
158 static void 
159 gst_fdsrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) 
160 {
161   GstFdSrc *src;
162
163   /* it's not null if we got it, but it might not be ours */
164   g_return_if_fail (GST_IS_FDSRC (object));
165   
166   src = GST_FDSRC (object);
167
168   switch (prop_id) {
169     case ARG_BYTESPERREAD:
170       g_value_set_int (value, src->bytes_per_read);
171       break;
172     case ARG_OFFSET:
173       g_value_set_int (value, src->curoffset);
174       break;
175     default:
176       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177       break;
178   }
179 }
180
181 static GstBuffer *
182 gst_fdsrc_get(GstPad *pad)
183 {
184   GstFdSrc *src;
185   GstBuffer *buf;
186   glong readbytes;
187
188   g_return_val_if_fail (pad != NULL, NULL);
189   src = GST_FDSRC(gst_pad_get_parent (pad));
190
191   /* create the buffer */
192   /* FIXME: should eventually use a bufferpool for this*/
193   buf = gst_buffer_new ();
194   g_return_val_if_fail (buf, NULL);
195
196   /* allocate the space for the buffer data */
197   GST_BUFFER_DATA(buf) = g_malloc(src->bytes_per_read);
198   g_return_val_if_fail(GST_BUFFER_DATA(buf) != NULL, NULL);
199
200   /* read it in from the file */
201   readbytes = read(src->fd,GST_BUFFER_DATA(buf),src->bytes_per_read);
202   if (readbytes == 0) {
203     return NULL;
204   }
205
206   /* if we didn't get as many bytes as we asked for, we're at EOF */
207   if (readbytes < src->bytes_per_read) {
208     /* set the buffer's EOF bit here */
209     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_EOS);
210   }
211   GST_BUFFER_OFFSET(buf) = src->curoffset;
212   GST_BUFFER_SIZE(buf) = readbytes;
213   src->curoffset += readbytes;
214
215   /* we're done, return the buffer */
216   return buf;
217 }