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