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