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