gst/elements/gstfdsrc.c: Don't ignore sscanf results
[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  *                    2005 Philippe Khalaf <burger@speedy.org>
5  *
6  * gstfdsrc.c:
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27 #include "gst/gst_private.h"
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/socket.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #ifdef _MSC_VER
38 #include <io.h>
39 #endif
40 #include <stdlib.h>
41 #include <errno.h>
42
43 #include "gstfdsrc.h"
44
45 /* the select call is also performed on the control sockets, that way
46  * we can send special commands to unblock the select call */
47 #define CONTROL_STOP            'S'     /* stop the select call */
48 #define CONTROL_SOCKETS(src)   src->control_sock
49 #define WRITE_SOCKET(src)      src->control_sock[1]
50 #define READ_SOCKET(src)       src->control_sock[0]
51
52 #define SEND_COMMAND(src, command)          \
53 G_STMT_START {                              \
54   unsigned char c; c = command;             \
55   write (WRITE_SOCKET(src), &c, 1);         \
56 } G_STMT_END
57
58 #define READ_COMMAND(src, command, res)        \
59 G_STMT_START {                                 \
60   res = read(READ_SOCKET(src), &command, 1);   \
61 } G_STMT_END
62
63 #define DEFAULT_BLOCKSIZE       4096
64
65 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
66     GST_PAD_SRC,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS_ANY);
69
70 GST_DEBUG_CATEGORY_STATIC (gst_fdsrc_debug);
71 #define GST_CAT_DEFAULT gst_fdsrc_debug
72
73 static GstElementDetails gst_fdsrc_details = GST_ELEMENT_DETAILS ("Disk Source",
74     "Source/File",
75     "Synchronous read from a file",
76     "Erik Walthinsen <omega@cse.ogi.edu>");
77
78 enum
79 {
80   PROP_0,
81   PROP_FD,
82 };
83
84 static void gst_fdsrc_uri_handler_init (gpointer g_iface, gpointer iface_data);
85
86 static void
87 _do_init (GType fdsrc_type)
88 {
89   static const GInterfaceInfo urihandler_info = {
90     gst_fdsrc_uri_handler_init,
91     NULL,
92     NULL
93   };
94
95   g_type_add_interface_static (fdsrc_type, GST_TYPE_URI_HANDLER,
96       &urihandler_info);
97
98   GST_DEBUG_CATEGORY_INIT (gst_fdsrc_debug, "fdsrc", 0, "fdsrc element");
99 }
100
101 GST_BOILERPLATE_FULL (GstFdSrc, gst_fdsrc, GstElement, GST_TYPE_PUSH_SRC,
102     _do_init);
103
104 static void gst_fdsrc_set_property (GObject * object, guint prop_id,
105     const GValue * value, GParamSpec * pspec);
106 static void gst_fdsrc_get_property (GObject * object, guint prop_id,
107     GValue * value, GParamSpec * pspec);
108 static void gst_fdsrc_dispose (GObject * obj);
109
110 static gboolean gst_fdsrc_start (GstBaseSrc * bsrc);
111 static gboolean gst_fdsrc_stop (GstBaseSrc * bsrc);
112 static gboolean gst_fdsrc_unlock (GstBaseSrc * bsrc);
113
114 static GstFlowReturn gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf);
115
116 static void
117 gst_fdsrc_base_init (gpointer g_class)
118 {
119   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
120
121   gst_element_class_add_pad_template (gstelement_class,
122       gst_static_pad_template_get (&srctemplate));
123   gst_element_class_set_details (gstelement_class, &gst_fdsrc_details);
124 }
125
126 static void
127 gst_fdsrc_class_init (GstFdSrcClass * klass)
128 {
129   GObjectClass *gobject_class;
130   GstBaseSrcClass *gstbasesrc_class;
131   GstElementClass *gstelement_class;
132   GstPushSrcClass *gstpush_src_class;
133
134   gobject_class = G_OBJECT_CLASS (klass);
135   gstelement_class = GST_ELEMENT_CLASS (klass);
136   gstbasesrc_class = (GstBaseSrcClass *) klass;
137   gstpush_src_class = (GstPushSrcClass *) klass;
138
139   parent_class = g_type_class_ref (GST_TYPE_PUSH_SRC);
140
141   gobject_class->set_property = gst_fdsrc_set_property;
142   gobject_class->get_property = gst_fdsrc_get_property;
143   gobject_class->dispose = gst_fdsrc_dispose;
144
145   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FD,
146       g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
147           0, G_MAXINT, 0, G_PARAM_READWRITE));
148
149   gstbasesrc_class->start = gst_fdsrc_start;
150   gstbasesrc_class->stop = gst_fdsrc_stop;
151   gstbasesrc_class->unlock = gst_fdsrc_unlock;
152
153   gstpush_src_class->create = gst_fdsrc_create;
154 }
155
156 static void
157 gst_fdsrc_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
158 {
159   /* TODO set live only if it's actually a live source (check
160    * for seekable fd) */
161   gst_base_src_set_live (GST_BASE_SRC (fdsrc), TRUE);
162
163   fdsrc->fd = 0;
164   fdsrc->uri = g_strdup_printf ("fd://%d", fdsrc->fd);
165   fdsrc->curoffset = 0;
166 }
167
168 static void
169 gst_fdsrc_dispose (GObject * obj)
170 {
171   GstFdSrc *src = GST_FDSRC (obj);
172
173   g_free (src->uri);
174   src->uri = NULL;
175
176   G_OBJECT_CLASS (parent_class)->dispose (obj);
177 }
178
179 static gboolean
180 gst_fdsrc_start (GstBaseSrc * bsrc)
181 {
182   GstFdSrc *src = GST_FDSRC (bsrc);
183   gint control_sock[2];
184
185   src->curoffset = 0;
186
187   if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
188     goto socket_pair;
189
190   READ_SOCKET (src) = control_sock[0];
191   WRITE_SOCKET (src) = control_sock[1];
192
193   fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
194   fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
195
196   return TRUE;
197
198   /* ERRORS */
199 socket_pair:
200   {
201     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
202         GST_ERROR_SYSTEM);
203     return FALSE;
204   }
205 }
206
207 static gboolean
208 gst_fdsrc_stop (GstBaseSrc * bsrc)
209 {
210   GstFdSrc *src = GST_FDSRC (bsrc);
211
212   close (READ_SOCKET (src));
213   close (WRITE_SOCKET (src));
214
215   return TRUE;
216 }
217
218 static gboolean
219 gst_fdsrc_unlock (GstBaseSrc * bsrc)
220 {
221   GstFdSrc *src = GST_FDSRC (bsrc);
222
223   SEND_COMMAND (src, CONTROL_STOP);
224
225   return TRUE;
226 }
227
228 static void
229 gst_fdsrc_set_property (GObject * object, guint prop_id, const GValue * value,
230     GParamSpec * pspec)
231 {
232   GstFdSrc *src = GST_FDSRC (object);
233
234   switch (prop_id) {
235     case PROP_FD:
236       src->fd = g_value_get_int (value);
237       g_free (src->uri);
238       src->uri = g_strdup_printf ("fd://%d", src->fd);
239       break;
240     default:
241       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242       break;
243   }
244 }
245
246 static void
247 gst_fdsrc_get_property (GObject * object, guint prop_id, GValue * value,
248     GParamSpec * pspec)
249 {
250   GstFdSrc *src = GST_FDSRC (object);
251
252   switch (prop_id) {
253     case PROP_FD:
254       g_value_set_int (value, src->fd);
255       break;
256     default:
257       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258       break;
259   }
260 }
261
262 static GstFlowReturn
263 gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf)
264 {
265   GstFdSrc *src;
266   GstBuffer *buf;
267   glong readbytes;
268   guint blocksize;
269
270 #ifndef HAVE_WIN32
271   fd_set readfds;
272   gint retval;
273 #endif
274
275   src = GST_FDSRC (psrc);
276
277 #ifndef HAVE_WIN32
278   FD_ZERO (&readfds);
279   FD_SET (src->fd, &readfds);
280   FD_SET (READ_SOCKET (src), &readfds);
281
282   do {
283     retval = select (FD_SETSIZE, &readfds, NULL, NULL, NULL);
284   } while ((retval == -1 && errno == EINTR));
285
286   if (retval == -1)
287     goto select_error;
288
289   if (FD_ISSET (READ_SOCKET (src), &readfds)) {
290     /* read all stop commands */
291     while (TRUE) {
292       gchar command;
293       int res;
294
295       READ_COMMAND (src, command, res);
296       if (res < 0) {
297         GST_LOG_OBJECT (src, "no more commands");
298         /* no more commands */
299         break;
300       }
301     }
302     goto stopped;
303   }
304 #endif
305
306   blocksize = GST_BASE_SRC (src)->blocksize;
307
308   /* create the buffer */
309   buf = gst_buffer_new_and_alloc (blocksize);
310
311   do {
312     readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
313   } while (readbytes == -1 && errno == EINTR);  /* retry if interrupted */
314
315   if (readbytes < 0)
316     goto read_error;
317
318   if (readbytes == 0)
319     goto eos;
320
321   GST_BUFFER_OFFSET (buf) = src->curoffset;
322   GST_BUFFER_SIZE (buf) = readbytes;
323   GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
324   src->curoffset += readbytes;
325
326   GST_DEBUG_OBJECT (psrc, "Read buffer of size %u.", readbytes);
327
328   /* we're done, return the buffer */
329   *outbuf = buf;
330
331   return GST_FLOW_OK;
332
333   /* ERRORS */
334 #ifndef HAVE_WIN32
335 select_error:
336   {
337     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
338         ("select on file descriptor: %s.", g_strerror (errno)));
339     GST_DEBUG_OBJECT (psrc, "Error during select");
340     return GST_FLOW_ERROR;
341   }
342 stopped:
343   {
344     GST_DEBUG_OBJECT (psrc, "Select stopped");
345     return GST_FLOW_WRONG_STATE;
346   }
347 #endif
348 eos:
349   {
350     GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
351     gst_buffer_unref (buf);
352     return GST_FLOW_UNEXPECTED;
353   }
354 read_error:
355   {
356     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
357         ("read on file descriptor: %s.", g_strerror (errno)));
358     GST_DEBUG_OBJECT (psrc, "Error reading from fd");
359     gst_buffer_unref (buf);
360     return GST_FLOW_ERROR;
361   }
362 }
363
364 /*** GSTURIHANDLER INTERFACE *************************************************/
365
366 static guint
367 gst_fdsrc_uri_get_type (void)
368 {
369   return GST_URI_SRC;
370 }
371 static gchar **
372 gst_fdsrc_uri_get_protocols (void)
373 {
374   static gchar *protocols[] = { "fd", NULL };
375
376   return protocols;
377 }
378 static const gchar *
379 gst_fdsrc_uri_get_uri (GstURIHandler * handler)
380 {
381   GstFdSrc *src = GST_FDSRC (handler);
382
383   return src->uri;
384 }
385
386 static gboolean
387 gst_fdsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri)
388 {
389   gchar *protocol;
390   GstFdSrc *src = GST_FDSRC (handler);
391   gint fd = src->fd;
392
393   protocol = gst_uri_get_protocol (uri);
394   if (strcmp (protocol, "fd") != 0) {
395     g_free (protocol);
396     return FALSE;
397   }
398   g_free (protocol);
399
400   if (sscanf (uri, "fd://%d", &fd) != 1)
401     return FALSE;
402
403   src->fd = fd;
404   g_free (src->uri);
405   src->uri = g_strdup (uri);
406
407   return TRUE;
408 }
409
410 static void
411 gst_fdsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
412 {
413   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
414
415   iface->get_type = gst_fdsrc_uri_get_type;
416   iface->get_protocols = gst_fdsrc_uri_get_protocols;
417   iface->get_uri = gst_fdsrc_uri_get_uri;
418   iface->set_uri = gst_fdsrc_uri_set_uri;
419 }