gst/gstminiobject.c: update docs
[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  * SECTION:element-fdsrc
25  * @short_description: read from a unix file descriptor
26  * @see_also: #GstFdSink
27  *
28  * Read data from a unix file descriptor.
29  */
30
31
32 #ifdef HAVE_CONFIG_H
33 #  include "config.h"
34 #endif
35 #include "gst/gst_private.h"
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/socket.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45 #ifdef _MSC_VER
46 #include <io.h>
47 #endif
48 #include <stdlib.h>
49 #include <errno.h>
50
51 #include "gstfdsrc.h"
52
53 /* the select call is also performed on the control sockets, that way
54  * we can send special commands to unblock the select call */
55 #define CONTROL_STOP            'S'     /* stop the select call */
56 #define CONTROL_SOCKETS(src)   src->control_sock
57 #define WRITE_SOCKET(src)      src->control_sock[1]
58 #define READ_SOCKET(src)       src->control_sock[0]
59
60 #define SEND_COMMAND(src, command)          \
61 G_STMT_START {                              \
62   unsigned char c; c = command;             \
63   write (WRITE_SOCKET(src), &c, 1);         \
64 } G_STMT_END
65
66 #define READ_COMMAND(src, command, res)        \
67 G_STMT_START {                                 \
68   res = read(READ_SOCKET(src), &command, 1);   \
69 } G_STMT_END
70
71 #define DEFAULT_BLOCKSIZE       4096
72
73 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
74     GST_PAD_SRC,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS_ANY);
77
78 GST_DEBUG_CATEGORY_STATIC (gst_fd_src_debug);
79 #define GST_CAT_DEFAULT gst_fd_src_debug
80
81 static const GstElementDetails gst_fd_src_details =
82 GST_ELEMENT_DETAILS ("Disk Source",
83     "Source/File",
84     "Synchronous read from a file",
85     "Erik Walthinsen <omega@cse.ogi.edu>");
86
87 enum
88 {
89   PROP_0,
90   PROP_FD,
91 };
92
93 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
94
95 static void
96 _do_init (GType fd_src_type)
97 {
98   static const GInterfaceInfo urihandler_info = {
99     gst_fd_src_uri_handler_init,
100     NULL,
101     NULL
102   };
103
104   g_type_add_interface_static (fd_src_type, GST_TYPE_URI_HANDLER,
105       &urihandler_info);
106
107   GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
108 }
109
110 GST_BOILERPLATE_FULL (GstFdSrc, gst_fd_src, GstElement, GST_TYPE_PUSH_SRC,
111     _do_init);
112
113 static void gst_fd_src_set_property (GObject * object, guint prop_id,
114     const GValue * value, GParamSpec * pspec);
115 static void gst_fd_src_get_property (GObject * object, guint prop_id,
116     GValue * value, GParamSpec * pspec);
117 static void gst_fd_src_dispose (GObject * obj);
118
119 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
120 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
121 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
122 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
123 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
124
125 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
126
127 static void
128 gst_fd_src_base_init (gpointer g_class)
129 {
130   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
131
132   gst_element_class_add_pad_template (gstelement_class,
133       gst_static_pad_template_get (&srctemplate));
134   gst_element_class_set_details (gstelement_class, &gst_fd_src_details);
135 }
136
137 static void
138 gst_fd_src_class_init (GstFdSrcClass * klass)
139 {
140   GObjectClass *gobject_class;
141   GstBaseSrcClass *gstbasesrc_class;
142   GstElementClass *gstelement_class;
143   GstPushSrcClass *gstpush_src_class;
144
145   gobject_class = G_OBJECT_CLASS (klass);
146   gstelement_class = GST_ELEMENT_CLASS (klass);
147   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
148   gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
149
150   parent_class = g_type_class_peek_parent (klass);
151
152   gobject_class->set_property = gst_fd_src_set_property;
153   gobject_class->get_property = gst_fd_src_get_property;
154   gobject_class->dispose = gst_fd_src_dispose;
155
156   g_object_class_install_property (gobject_class, PROP_FD,
157       g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
158           0, G_MAXINT, 0, G_PARAM_READWRITE));
159
160   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
161   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
162   gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
163   gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
164   gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
165
166   gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
167 }
168
169 static void
170 gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
171 {
172   fdsrc->fd = 0;
173   fdsrc->new_fd = 0;
174   fdsrc->seekable_fd = FALSE;
175   fdsrc->uri = g_strdup_printf ("fd://%d", fdsrc->fd);
176   fdsrc->curoffset = 0;
177 }
178
179 static void
180 gst_fd_src_dispose (GObject * obj)
181 {
182   GstFdSrc *src = GST_FD_SRC (obj);
183
184   g_free (src->uri);
185   src->uri = NULL;
186
187   G_OBJECT_CLASS (parent_class)->dispose (obj);
188 }
189
190 static void
191 gst_fd_src_update_fd (GstFdSrc * src)
192 {
193   struct stat stat_results;
194
195   src->fd = src->new_fd;
196   g_free (src->uri);
197   src->uri = g_strdup_printf ("fd://%d", src->fd);
198
199   if (fstat (src->fd, &stat_results) < 0)
200     goto not_seekable;
201
202   if (!S_ISREG (stat_results.st_mode))
203     goto not_seekable;
204
205   /* Try a seek of 0 bytes offset to check for seekability */
206   if (lseek (src->fd, SEEK_CUR, 0) < 0)
207     goto not_seekable;
208
209   src->seekable_fd = TRUE;
210   return;
211
212 not_seekable:
213   src->seekable_fd = FALSE;
214 }
215
216 static gboolean
217 gst_fd_src_start (GstBaseSrc * bsrc)
218 {
219   GstFdSrc *src = GST_FD_SRC (bsrc);
220   gint control_sock[2];
221
222   src->curoffset = 0;
223
224   gst_fd_src_update_fd (src);
225
226   if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
227     goto socket_pair;
228
229   READ_SOCKET (src) = control_sock[0];
230   WRITE_SOCKET (src) = control_sock[1];
231
232   fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
233   fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
234
235   return TRUE;
236
237   /* ERRORS */
238 socket_pair:
239   {
240     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
241         GST_ERROR_SYSTEM);
242     return FALSE;
243   }
244 }
245
246 static gboolean
247 gst_fd_src_stop (GstBaseSrc * bsrc)
248 {
249   GstFdSrc *src = GST_FD_SRC (bsrc);
250
251   close (READ_SOCKET (src));
252   close (WRITE_SOCKET (src));
253
254   return TRUE;
255 }
256
257 static gboolean
258 gst_fd_src_unlock (GstBaseSrc * bsrc)
259 {
260   GstFdSrc *src = GST_FD_SRC (bsrc);
261
262   SEND_COMMAND (src, CONTROL_STOP);
263
264   return TRUE;
265 }
266
267 static void
268 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
269     GParamSpec * pspec)
270 {
271   GstFdSrc *src = GST_FD_SRC (object);
272
273   switch (prop_id) {
274     case PROP_FD:
275       src->new_fd = g_value_get_int (value);
276
277       /* If state is ready or below, update the current fd immediately
278        * so it is reflected in get_properties and uri */
279       GST_OBJECT_LOCK (object);
280       if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
281         GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
282         gst_fd_src_update_fd (src);
283       } else {
284         GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
285       }
286       GST_OBJECT_UNLOCK (object);
287       break;
288     default:
289       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290       break;
291   }
292 }
293
294 static void
295 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
296     GParamSpec * pspec)
297 {
298   GstFdSrc *src = GST_FD_SRC (object);
299
300   switch (prop_id) {
301     case PROP_FD:
302       g_value_set_int (value, src->fd);
303       break;
304     default:
305       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306       break;
307   }
308 }
309
310 static GstFlowReturn
311 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
312 {
313   GstFdSrc *src;
314   GstBuffer *buf;
315   glong readbytes;
316   guint blocksize;
317
318 #ifndef HAVE_WIN32
319   fd_set readfds;
320   gint retval;
321 #endif
322
323   src = GST_FD_SRC (psrc);
324
325 #ifndef HAVE_WIN32
326   FD_ZERO (&readfds);
327   FD_SET (src->fd, &readfds);
328   FD_SET (READ_SOCKET (src), &readfds);
329
330   do {
331     retval = select (FD_SETSIZE, &readfds, NULL, NULL, NULL);
332   } while ((retval == -1 && errno == EINTR));
333
334   if (retval == -1)
335     goto select_error;
336
337   if (FD_ISSET (READ_SOCKET (src), &readfds)) {
338     /* read all stop commands */
339     while (TRUE) {
340       gchar command;
341       int res;
342
343       READ_COMMAND (src, command, res);
344       if (res < 0) {
345         GST_LOG_OBJECT (src, "no more commands");
346         /* no more commands */
347         break;
348       }
349     }
350     goto stopped;
351   }
352 #endif
353
354   blocksize = GST_BASE_SRC (src)->blocksize;
355
356   /* create the buffer */
357   buf = gst_buffer_new_and_alloc (blocksize);
358
359   do {
360     readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
361   } while (readbytes == -1 && errno == EINTR);  /* retry if interrupted */
362
363   if (readbytes < 0)
364     goto read_error;
365
366   if (readbytes == 0)
367     goto eos;
368
369   GST_BUFFER_OFFSET (buf) = src->curoffset;
370   GST_BUFFER_SIZE (buf) = readbytes;
371   GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
372   src->curoffset += readbytes;
373
374   GST_LOG_OBJECT (psrc, "Read buffer of size %u.", readbytes);
375
376   /* we're done, return the buffer */
377   *outbuf = buf;
378
379   return GST_FLOW_OK;
380
381   /* ERRORS */
382 #ifndef HAVE_WIN32
383 select_error:
384   {
385     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
386         ("select on file descriptor: %s.", g_strerror (errno)));
387     GST_DEBUG_OBJECT (psrc, "Error during select");
388     return GST_FLOW_ERROR;
389   }
390 stopped:
391   {
392     GST_DEBUG_OBJECT (psrc, "Select stopped");
393     return GST_FLOW_WRONG_STATE;
394   }
395 #endif
396 eos:
397   {
398     GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
399     gst_buffer_unref (buf);
400     return GST_FLOW_UNEXPECTED;
401   }
402 read_error:
403   {
404     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
405         ("read on file descriptor: %s.", g_strerror (errno)));
406     GST_DEBUG_OBJECT (psrc, "Error reading from fd");
407     gst_buffer_unref (buf);
408     return GST_FLOW_ERROR;
409   }
410 }
411
412 gboolean
413 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
414 {
415   GstFdSrc *src = GST_FD_SRC (bsrc);
416
417   return src->seekable_fd;
418 }
419
420 gboolean
421 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
422 {
423   GstFdSrc *src = GST_FD_SRC (bsrc);
424   struct stat stat_results;
425
426   if (!src->seekable_fd) {
427     /* If it isn't seekable, we won't know the length (but fstat will still
428      * succeed, and wrongly say our length is zero. */
429     return FALSE;
430   }
431
432   if (fstat (src->fd, &stat_results) < 0)
433     goto could_not_stat;
434
435   *size = stat_results.st_size;
436
437   return TRUE;
438
439   /* ERROR */
440 could_not_stat:
441   {
442     return FALSE;
443   }
444
445 }
446
447 /*** GSTURIHANDLER INTERFACE *************************************************/
448
449 static guint
450 gst_fd_src_uri_get_type (void)
451 {
452   return GST_URI_SRC;
453 }
454 static gchar **
455 gst_fd_src_uri_get_protocols (void)
456 {
457   static gchar *protocols[] = { "fd", NULL };
458
459   return protocols;
460 }
461 static const gchar *
462 gst_fd_src_uri_get_uri (GstURIHandler * handler)
463 {
464   GstFdSrc *src = GST_FD_SRC (handler);
465
466   return src->uri;
467 }
468
469 static gboolean
470 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
471 {
472   gchar *protocol;
473   GstFdSrc *src = GST_FD_SRC (handler);
474   gint fd;
475
476   protocol = gst_uri_get_protocol (uri);
477   if (strcmp (protocol, "fd") != 0) {
478     g_free (protocol);
479     return FALSE;
480   }
481   g_free (protocol);
482
483   if (sscanf (uri, "fd://%d", &fd) != 1)
484     return FALSE;
485
486   src->new_fd = fd;
487
488   GST_OBJECT_LOCK (src);
489   if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
490     gst_fd_src_update_fd (src);
491   }
492   GST_OBJECT_UNLOCK (src);
493
494   return TRUE;
495 }
496
497 static void
498 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
499 {
500   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
501
502   iface->get_type = gst_fd_src_uri_get_type;
503   iface->get_protocols = gst_fd_src_uri_get_protocols;
504   iface->get_uri = gst_fd_src_uri_get_uri;
505   iface->set_uri = gst_fd_src_uri_set_uri;
506 }