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