a5dfa0c06e9f6df8bba149c5c32a52c7dbfd86a2
[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_arg       (GtkObject *object, GtkArg *arg, guint id);
61 static void             gst_fdsrc_get_arg       (GtkObject *object, GtkArg *arg, guint id);
62
63 static GstBuffer *      gst_fdsrc_get           (GstPad *pad);
64
65
66 static GstElementClass *parent_class = NULL;
67 //static guint gst_fdsrc_signals[LAST_SIGNAL] = { 0 };
68
69 GtkType
70 gst_fdsrc_get_type (void) 
71 {
72   static GtkType fdsrc_type = 0;
73
74   if (!fdsrc_type) {
75     static const GtkTypeInfo fdsrc_info = {
76       "GstFdSrc",
77       sizeof(GstFdSrc),
78       sizeof(GstFdSrcClass),
79       (GtkClassInitFunc)gst_fdsrc_class_init,
80       (GtkObjectInitFunc)gst_fdsrc_init,
81       (GtkArgSetFunc)gst_fdsrc_set_arg,
82       (GtkArgGetFunc)gst_fdsrc_get_arg,
83       (GtkClassInitFunc)NULL,
84     };
85     fdsrc_type = gtk_type_unique (GST_TYPE_ELEMENT, &fdsrc_info);
86   }
87   return fdsrc_type;
88 }
89
90 static void
91 gst_fdsrc_class_init (GstFdSrcClass *klass) 
92 {
93   GtkObjectClass *gtkobject_class;
94
95   gtkobject_class = (GtkObjectClass*)klass;
96
97   parent_class = gtk_type_class(GST_TYPE_ELEMENT);
98
99   gtk_object_add_arg_type ("GstFdSrc::location", GST_TYPE_FILENAME,
100                            GTK_ARG_WRITABLE, ARG_LOCATION);
101   gtk_object_add_arg_type ("GstFdSrc::bytesperread", GTK_TYPE_INT,
102                            GTK_ARG_READWRITE, ARG_BYTESPERREAD);
103   gtk_object_add_arg_type ("GstFdSrc::offset", GTK_TYPE_INT,
104                            GTK_ARG_READABLE, ARG_OFFSET);
105
106   gtkobject_class->set_arg = gst_fdsrc_set_arg;
107   gtkobject_class->get_arg = gst_fdsrc_get_arg;
108 }
109
110 static void gst_fdsrc_init(GstFdSrc *fdsrc) {
111   fdsrc->srcpad = gst_pad_new("src",GST_PAD_SRC);
112   gst_pad_set_get_function(fdsrc->srcpad,gst_fdsrc_get);
113   gst_element_add_pad(GST_ELEMENT(fdsrc),fdsrc->srcpad);
114
115   fdsrc->fd = 0;
116   fdsrc->curoffset = 0;
117   fdsrc->bytes_per_read = 4096;
118   fdsrc->seq = 0;
119 }
120
121
122 static void 
123 gst_fdsrc_set_arg (GtkObject *object, GtkArg *arg, guint id) 
124 {
125   GstFdSrc *src;
126   int fd;
127
128   /* it's not null if we got it, but it might not be ours */
129   g_return_if_fail (GST_IS_FDSRC (object));
130   
131   src = GST_FDSRC (object);
132
133   switch(id) {
134     case ARG_LOCATION:
135       /* the element must not be playing in order to do this */
136       g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
137
138       /* if we get a NULL, consider it to be a fd of 0 */
139       if (GTK_VALUE_STRING (*arg) == NULL) {
140         gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
141         src->fd = 0;
142       /* otherwise set the new filename */
143       } else {
144         if (sscanf (GTK_VALUE_STRING (*arg), "%d", &fd))
145           src->fd = fd;
146       }
147       break;
148     case ARG_BYTESPERREAD:
149       src->bytes_per_read = GTK_VALUE_INT (*arg);
150       break;
151     default:
152       break;
153   }
154 }
155
156 static void 
157 gst_fdsrc_get_arg (GtkObject *object, GtkArg *arg, guint id) 
158 {
159   GstFdSrc *src;
160
161   /* it's not null if we got it, but it might not be ours */
162   g_return_if_fail (GST_IS_FDSRC (object));
163   
164   src = GST_FDSRC (object);
165
166   switch (id) {
167     case ARG_BYTESPERREAD:
168       GTK_VALUE_INT (*arg) = src->bytes_per_read;
169       break;
170     case ARG_OFFSET:
171       GTK_VALUE_INT (*arg) = src->curoffset;
172       break;
173     default:
174       arg->type = GTK_TYPE_INVALID;
175       break;
176   }
177 }
178
179 static GstBuffer *
180 gst_fdsrc_get(GstPad *pad)
181 {
182   GstFdSrc *src;
183   GstBuffer *buf;
184   glong readbytes;
185
186   g_return_val_if_fail (pad != NULL, NULL);
187   src = GST_FDSRC(gst_pad_get_parent(pad));
188
189   /* create the buffer */
190   // FIXME: should eventually use a bufferpool for this
191   buf = gst_buffer_new ();
192   g_return_val_if_fail (buf, NULL);
193
194   /* allocate the space for the buffer data */
195   GST_BUFFER_DATA(buf) = g_malloc(src->bytes_per_read);
196   g_return_val_if_fail(GST_BUFFER_DATA(buf) != NULL, NULL);
197
198   /* read it in from the file */
199   readbytes = read(src->fd,GST_BUFFER_DATA(buf),src->bytes_per_read);
200   if (readbytes == 0) {
201     gst_element_signal_eos(GST_ELEMENT(src));
202     return NULL;
203   }
204
205   /* if we didn't get as many bytes as we asked for, we're at EOF */
206   if (readbytes < src->bytes_per_read) {
207     // set the buffer's EOF bit here
208     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_EOS);
209   }
210   GST_BUFFER_OFFSET(buf) = src->curoffset;
211   GST_BUFFER_SIZE(buf) = readbytes;
212   src->curoffset += readbytes;
213
214   /* we're done, return the buffer */
215   return buf;
216 }