Changes to handle compilers that don't have variadic macro support. In particular...
[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 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26 #include "gst/gst_private.h"
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #include <stdlib.h>
36 #include <errno.h>
37
38 #include "gstfdsrc.h"
39
40 #define DEFAULT_BLOCKSIZE       4096
41
42 GST_DEBUG_CATEGORY_STATIC (gst_fdsrc_debug);
43 #define GST_CAT_DEFAULT gst_fdsrc_debug
44
45 GstElementDetails gst_fdsrc_details = GST_ELEMENT_DETAILS ("Disk Source",
46     "Source/File",
47     "Synchronous read from a file",
48     "Erik Walthinsen <omega@cse.ogi.edu>");
49
50
51 /* FdSrc signals and args */
52 enum
53 {
54   SIGNAL_TIMEOUT,
55   LAST_SIGNAL
56 };
57
58 enum
59 {
60   ARG_0,
61   ARG_FD,
62   ARG_BLOCKSIZE,
63   ARG_TIMEOUT
64 };
65
66 static guint gst_fdsrc_signals[LAST_SIGNAL] = { 0 };
67
68 #define _do_init(bla) \
69     GST_DEBUG_CATEGORY_INIT (gst_fdsrc_debug, "fdsrc", 0, "fdsrc element");
70
71 GST_BOILERPLATE_FULL (GstFdSrc, gst_fdsrc, GstElement, GST_TYPE_ELEMENT,
72     _do_init);
73
74 static void gst_fdsrc_set_property (GObject * object, guint prop_id,
75     const GValue * value, GParamSpec * pspec);
76 static void gst_fdsrc_get_property (GObject * object, guint prop_id,
77     GValue * value, GParamSpec * pspec);
78
79 static GstData *gst_fdsrc_get (GstPad * pad);
80
81
82 static void
83 gst_fdsrc_base_init (gpointer g_class)
84 {
85   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
86
87   gst_element_class_set_details (gstelement_class, &gst_fdsrc_details);
88 }
89 static void
90 gst_fdsrc_class_init (GstFdSrcClass * klass)
91 {
92   GObjectClass *gobject_class;
93
94   gobject_class = G_OBJECT_CLASS (klass);
95
96   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FD,
97       g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
98           0, G_MAXINT, 0, G_PARAM_READWRITE));
99   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BLOCKSIZE,
100       g_param_spec_ulong ("blocksize", "Block size",
101           "Size in bytes to read per buffer", 1, G_MAXULONG, DEFAULT_BLOCKSIZE,
102           G_PARAM_READWRITE));
103   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TIMEOUT,
104       g_param_spec_uint64 ("timeout", "Timeout", "Read timeout in nanoseconds",
105           0, G_MAXUINT64, 0, G_PARAM_READWRITE));
106
107   gst_fdsrc_signals[SIGNAL_TIMEOUT] =
108       g_signal_new ("timeout", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
109       G_STRUCT_OFFSET (GstFdSrcClass, timeout), NULL, NULL,
110       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
111
112   gobject_class->set_property = gst_fdsrc_set_property;
113   gobject_class->get_property = gst_fdsrc_get_property;
114 }
115
116 static void
117 gst_fdsrc_init (GstFdSrc * fdsrc)
118 {
119   fdsrc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
120
121   gst_pad_set_get_function (fdsrc->srcpad, gst_fdsrc_get);
122   gst_element_add_pad (GST_ELEMENT (fdsrc), fdsrc->srcpad);
123
124   fdsrc->fd = 0;
125   fdsrc->curoffset = 0;
126   fdsrc->blocksize = DEFAULT_BLOCKSIZE;
127   fdsrc->timeout = 0;
128   fdsrc->seq = 0;
129 }
130
131
132 static void
133 gst_fdsrc_set_property (GObject * object, guint prop_id, const GValue * value,
134     GParamSpec * pspec)
135 {
136   GstFdSrc *src;
137
138   /* it's not null if we got it, but it might not be ours */
139   g_return_if_fail (GST_IS_FDSRC (object));
140
141   src = GST_FDSRC (object);
142
143   switch (prop_id) {
144     case ARG_FD:
145       src->fd = g_value_get_int (value);
146       break;
147     case ARG_BLOCKSIZE:
148       src->blocksize = g_value_get_ulong (value);
149       break;
150     case ARG_TIMEOUT:
151       src->timeout = g_value_get_uint64 (value);
152       break;
153     default:
154       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
155       break;
156   }
157 }
158
159 static void
160 gst_fdsrc_get_property (GObject * object, guint prop_id, GValue * value,
161     GParamSpec * pspec)
162 {
163   GstFdSrc *src;
164
165   /* it's not null if we got it, but it might not be ours */
166   g_return_if_fail (GST_IS_FDSRC (object));
167
168   src = GST_FDSRC (object);
169
170   switch (prop_id) {
171     case ARG_BLOCKSIZE:
172       g_value_set_ulong (value, src->blocksize);
173       break;
174     case ARG_FD:
175       g_value_set_int (value, src->fd);
176       break;
177     case ARG_TIMEOUT:
178       g_value_set_uint64 (value, src->timeout);
179       break;
180     default:
181       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182       break;
183   }
184 }
185
186 static GstData *
187 gst_fdsrc_get (GstPad * pad)
188 {
189   GstFdSrc *src;
190   GstBuffer *buf;
191   glong readbytes;
192
193 #ifndef HAVE_WIN32
194   fd_set readfds;
195   struct timeval t, *tp = &t;
196   gint retval;
197 #endif
198
199   src = GST_FDSRC (gst_pad_get_parent (pad));
200
201   /* create the buffer */
202   buf = gst_buffer_new_and_alloc (src->blocksize);
203
204 #ifndef HAVE_WIN32
205   FD_ZERO (&readfds);
206   FD_SET (src->fd, &readfds);
207
208   if (src->timeout != 0) {
209     GST_TIME_TO_TIMEVAL (src->timeout, t);
210   } else
211     tp = NULL;
212
213   do {
214     retval = select (src->fd + 1, &readfds, NULL, NULL, tp);
215   } while (retval == -1 && errno == EINTR);     /* retry if interrupted */
216
217   if (retval == -1) {
218     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
219         ("select on file descriptor: %s.", g_strerror (errno)));
220     gst_element_set_eos (GST_ELEMENT (src));
221     return GST_DATA (gst_event_new (GST_EVENT_EOS));
222   } else if (retval == 0) {
223     g_signal_emit (G_OBJECT (src), gst_fdsrc_signals[SIGNAL_TIMEOUT], 0);
224     gst_element_set_eos (GST_ELEMENT (src));
225     return GST_DATA (gst_event_new (GST_EVENT_EOS));
226   }
227 #endif
228
229   do {
230     readbytes = read (src->fd, GST_BUFFER_DATA (buf), src->blocksize);
231   } while (readbytes == -1 && errno == EINTR);  /* retry if interrupted */
232
233   if (readbytes > 0) {
234     GST_BUFFER_OFFSET (buf) = src->curoffset;
235     GST_BUFFER_SIZE (buf) = readbytes;
236     GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
237     src->curoffset += readbytes;
238
239     /* we're done, return the buffer */
240     return GST_DATA (buf);
241   } else if (readbytes == 0) {
242     gst_element_set_eos (GST_ELEMENT (src));
243     return GST_DATA (gst_event_new (GST_EVENT_EOS));
244   } else {
245     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
246         ("read on file descriptor: %s.", g_strerror (errno)));
247     gst_element_set_eos (GST_ELEMENT (src));
248     return GST_DATA (gst_event_new (GST_EVENT_EOS));
249   }
250 }