Merge branch 'master' into 0.11
[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  * @see_also: #GstFdSink
26  *
27  * Read data from a unix file descriptor.
28  * 
29  * To generate data, enter some data on the console folowed by enter.
30  * The above mentioned pipeline should dump data packets to the console.
31  * 
32  * If the #GstFdSrc:timeout property is set to a value bigger than 0, fdsrc will
33  * generate an element message named
34  * <classname>&quot;GstFdSrcTimeout&quot;</classname>
35  * if no data was recieved in the given timeout.
36  * The message's structure contains one field:
37  * <itemizedlist>
38  * <listitem>
39  *   <para>
40  *   #guint64
41  *   <classname>&quot;timeout&quot;</classname>: the timeout in microseconds that
42  *   expired when waiting for data.
43  *   </para>
44  * </listitem>
45  * </itemizedlist>
46  * 
47  * <refsect2>
48  * <title>Example launch line</title>
49  * |[
50  * echo "Hello GStreamer" | gst-launch -v fdsrc ! fakesink dump=true
51  * ]| A simple pipeline to read from the standard input and dump the data
52  * with a fakesink as hex ascii block.
53  * </refsect2>
54  * 
55  * Last reviewed on 2008-06-20 (0.10.21)
56  */
57
58 #ifdef HAVE_CONFIG_H
59 #  include "config.h"
60 #endif
61 #include "gst/gst_private.h"
62
63 #include <sys/types.h>
64
65 #ifdef G_OS_WIN32
66 #include <io.h>                 /* lseek, open, close, read */
67 #undef lseek
68 #define lseek _lseeki64
69 #undef off_t
70 #define off_t guint64
71 #endif
72
73 #include <sys/stat.h>
74 #ifdef HAVE_SYS_SOCKET_H
75 #include <sys/socket.h>
76 #endif
77 #include <fcntl.h>
78 #include <stdio.h>
79 #ifdef HAVE_UNISTD_H
80 #include <unistd.h>
81 #endif
82 #ifdef _MSC_VER
83 #undef stat
84 #define stat _stat
85 #define fstat _fstat
86 #define S_ISREG(m)      (((m)&S_IFREG)==S_IFREG)
87 #endif
88 #include <stdlib.h>
89 #include <errno.h>
90
91 #include "gstfdsrc.h"
92
93 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
94     GST_PAD_SRC,
95     GST_PAD_ALWAYS,
96     GST_STATIC_CAPS_ANY);
97
98 GST_DEBUG_CATEGORY_STATIC (gst_fd_src_debug);
99 #define GST_CAT_DEFAULT gst_fd_src_debug
100
101 #define DEFAULT_FD              0
102 #define DEFAULT_TIMEOUT         0
103
104 enum
105 {
106   PROP_0,
107
108   PROP_FD,
109   PROP_TIMEOUT,
110
111   PROP_LAST
112 };
113
114 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
115
116 static void
117 _do_init (GType fd_src_type)
118 {
119   static const GInterfaceInfo urihandler_info = {
120     gst_fd_src_uri_handler_init,
121     NULL,
122     NULL
123   };
124
125   g_type_add_interface_static (fd_src_type, GST_TYPE_URI_HANDLER,
126       &urihandler_info);
127
128   GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
129 }
130
131 GST_BOILERPLATE_FULL (GstFdSrc, gst_fd_src, GstPushSrc, GST_TYPE_PUSH_SRC,
132     _do_init);
133
134 static void gst_fd_src_set_property (GObject * object, guint prop_id,
135     const GValue * value, GParamSpec * pspec);
136 static void gst_fd_src_get_property (GObject * object, guint prop_id,
137     GValue * value, GParamSpec * pspec);
138 static void gst_fd_src_dispose (GObject * obj);
139
140 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
141 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
142 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
143 static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
144 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
145 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
146 static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
147 static gboolean gst_fd_src_query (GstBaseSrc * src, GstQuery * query);
148
149 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
150
151 static void
152 gst_fd_src_base_init (gpointer g_class)
153 {
154   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
155
156   gst_element_class_set_details_simple (gstelement_class,
157       "Filedescriptor Source",
158       "Source/File",
159       "Read from a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
160   gst_element_class_add_pad_template (gstelement_class,
161       gst_static_pad_template_get (&srctemplate));
162 }
163
164 static void
165 gst_fd_src_class_init (GstFdSrcClass * klass)
166 {
167   GObjectClass *gobject_class;
168   GstBaseSrcClass *gstbasesrc_class;
169   GstPushSrcClass *gstpush_src_class;
170
171   gobject_class = G_OBJECT_CLASS (klass);
172   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
173   gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
174
175   gobject_class->set_property = gst_fd_src_set_property;
176   gobject_class->get_property = gst_fd_src_get_property;
177   gobject_class->dispose = gst_fd_src_dispose;
178
179   g_object_class_install_property (gobject_class, PROP_FD,
180       g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
181           0, G_MAXINT, DEFAULT_FD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182   /**
183    * GstFdSrc:timeout
184    *
185    * Post a message after timeout microseconds
186    *
187    * Since: 0.10.21
188    */
189   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
190       g_param_spec_uint64 ("timeout", "Timeout",
191           "Post a message after timeout microseconds (0 = disabled)", 0,
192           G_MAXUINT64, DEFAULT_TIMEOUT,
193           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194
195   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
196   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
197   gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
198   gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
199   gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
200   gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
201   gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
202   gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_fd_src_query);
203
204   gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
205 }
206
207 static void
208 gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
209 {
210   fdsrc->new_fd = DEFAULT_FD;
211   fdsrc->seekable_fd = FALSE;
212   fdsrc->fd = -1;
213   fdsrc->size = -1;
214   fdsrc->timeout = DEFAULT_TIMEOUT;
215   fdsrc->uri = g_strdup_printf ("fd://0");
216   fdsrc->curoffset = 0;
217 }
218
219 static void
220 gst_fd_src_dispose (GObject * obj)
221 {
222   GstFdSrc *src = GST_FD_SRC (obj);
223
224   g_free (src->uri);
225   src->uri = NULL;
226
227   G_OBJECT_CLASS (parent_class)->dispose (obj);
228 }
229
230 static void
231 gst_fd_src_update_fd (GstFdSrc * src, guint64 size)
232 {
233   struct stat stat_results;
234
235   GST_DEBUG_OBJECT (src, "fdset %p, old_fd %d, new_fd %d", src->fdset, src->fd,
236       src->new_fd);
237
238   /* we need to always update the fdset since it may not have existed when
239    * gst_fd_src_update_fd () was called earlier */
240   if (src->fdset != NULL) {
241     GstPollFD fd = GST_POLL_FD_INIT;
242
243     if (src->fd >= 0) {
244       fd.fd = src->fd;
245       /* this will log a harmless warning, if it was never added */
246       gst_poll_remove_fd (src->fdset, &fd);
247     }
248
249     fd.fd = src->new_fd;
250     gst_poll_add_fd (src->fdset, &fd);
251     gst_poll_fd_ctl_read (src->fdset, &fd, TRUE);
252   }
253
254
255   if (src->fd != src->new_fd) {
256     GST_INFO_OBJECT (src, "Updating to fd %d", src->new_fd);
257
258     src->fd = src->new_fd;
259
260     GST_INFO_OBJECT (src, "Setting size to fd %" G_GUINT64_FORMAT, size);
261     src->size = size;
262
263     g_free (src->uri);
264     src->uri = g_strdup_printf ("fd://%d", src->fd);
265
266     if (fstat (src->fd, &stat_results) < 0)
267       goto not_seekable;
268
269     if (!S_ISREG (stat_results.st_mode))
270       goto not_seekable;
271
272     /* Try a seek of 0 bytes offset to check for seekability */
273     if (lseek (src->fd, 0, SEEK_CUR) < 0)
274       goto not_seekable;
275
276     GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
277     src->seekable_fd = TRUE;
278   }
279   return;
280
281 not_seekable:
282   {
283     GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
284     src->seekable_fd = FALSE;
285   }
286 }
287
288 static gboolean
289 gst_fd_src_start (GstBaseSrc * bsrc)
290 {
291   GstFdSrc *src = GST_FD_SRC (bsrc);
292
293   src->curoffset = 0;
294
295   if ((src->fdset = gst_poll_new (TRUE)) == NULL)
296     goto socket_pair;
297
298   gst_fd_src_update_fd (src, -1);
299
300   return TRUE;
301
302   /* ERRORS */
303 socket_pair:
304   {
305     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
306         GST_ERROR_SYSTEM);
307     return FALSE;
308   }
309 }
310
311 static gboolean
312 gst_fd_src_stop (GstBaseSrc * bsrc)
313 {
314   GstFdSrc *src = GST_FD_SRC (bsrc);
315
316   if (src->fdset) {
317     gst_poll_free (src->fdset);
318     src->fdset = NULL;
319   }
320
321   return TRUE;
322 }
323
324 static gboolean
325 gst_fd_src_unlock (GstBaseSrc * bsrc)
326 {
327   GstFdSrc *src = GST_FD_SRC (bsrc);
328
329   GST_LOG_OBJECT (src, "Flushing");
330   GST_OBJECT_LOCK (src);
331   gst_poll_set_flushing (src->fdset, TRUE);
332   GST_OBJECT_UNLOCK (src);
333
334   return TRUE;
335 }
336
337 static gboolean
338 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
339 {
340   GstFdSrc *src = GST_FD_SRC (bsrc);
341
342   GST_LOG_OBJECT (src, "No longer flushing");
343   GST_OBJECT_LOCK (src);
344   gst_poll_set_flushing (src->fdset, FALSE);
345   GST_OBJECT_UNLOCK (src);
346
347   return TRUE;
348 }
349
350 static void
351 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
352     GParamSpec * pspec)
353 {
354   GstFdSrc *src = GST_FD_SRC (object);
355
356   switch (prop_id) {
357     case PROP_FD:
358       src->new_fd = g_value_get_int (value);
359
360       /* If state is ready or below, update the current fd immediately
361        * so it is reflected in get_properties and uri */
362       GST_OBJECT_LOCK (object);
363       if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
364         GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
365         gst_fd_src_update_fd (src, -1);
366       } else {
367         GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
368       }
369       GST_OBJECT_UNLOCK (object);
370       break;
371     case PROP_TIMEOUT:
372       src->timeout = g_value_get_uint64 (value);
373       GST_DEBUG_OBJECT (src, "poll timeout set to %" GST_TIME_FORMAT,
374           GST_TIME_ARGS (src->timeout));
375       break;
376     default:
377       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
378       break;
379   }
380 }
381
382 static void
383 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
384     GParamSpec * pspec)
385 {
386   GstFdSrc *src = GST_FD_SRC (object);
387
388   switch (prop_id) {
389     case PROP_FD:
390       g_value_set_int (value, src->fd);
391       break;
392     case PROP_TIMEOUT:
393       g_value_set_uint64 (value, src->timeout);
394       break;
395     default:
396       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
397       break;
398   }
399 }
400
401 static GstFlowReturn
402 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
403 {
404   GstFdSrc *src;
405   GstBuffer *buf;
406   gssize readbytes;
407   guint blocksize;
408   GstClockTime timeout;
409   guint8 *data;
410   gsize maxsize;
411
412 #ifndef HAVE_WIN32
413   gboolean try_again;
414   gint retval;
415 #endif
416
417   src = GST_FD_SRC (psrc);
418
419   if (src->timeout > 0) {
420     timeout = src->timeout * GST_USECOND;
421   } else {
422     timeout = GST_CLOCK_TIME_NONE;
423   }
424
425 #ifndef HAVE_WIN32
426   do {
427     try_again = FALSE;
428
429     GST_LOG_OBJECT (src, "doing poll, timeout %" GST_TIME_FORMAT,
430         GST_TIME_ARGS (src->timeout));
431
432     retval = gst_poll_wait (src->fdset, timeout);
433     GST_LOG_OBJECT (src, "poll returned %d", retval);
434
435     if (G_UNLIKELY (retval == -1)) {
436       if (errno == EINTR || errno == EAGAIN) {
437         /* retry if interrupted */
438         try_again = TRUE;
439       } else if (errno == EBUSY) {
440         goto stopped;
441       } else {
442         goto poll_error;
443       }
444     } else if (G_UNLIKELY (retval == 0)) {
445       try_again = TRUE;
446       /* timeout, post element message */
447       gst_element_post_message (GST_ELEMENT_CAST (src),
448           gst_message_new_element (GST_OBJECT_CAST (src),
449               gst_structure_new ("GstFdSrcTimeout",
450                   "timeout", G_TYPE_UINT64, src->timeout, NULL)));
451     }
452   } while (G_UNLIKELY (try_again));     /* retry if interrupted or timeout */
453 #endif
454
455   blocksize = GST_BASE_SRC (src)->blocksize;
456
457   /* create the buffer */
458   buf = gst_buffer_new_and_alloc (blocksize);
459   if (G_UNLIKELY (buf == NULL))
460     goto alloc_failed;
461
462   data = gst_buffer_map (buf, NULL, &maxsize, GST_MAP_WRITE);
463
464   do {
465     readbytes = read (src->fd, data, blocksize);
466     GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
467   } while (readbytes == -1 && errno == EINTR);  /* retry if interrupted */
468
469   if (readbytes < 0)
470     goto read_error;
471
472   gst_buffer_unmap (buf, data, readbytes);
473
474   if (readbytes == 0)
475     goto eos;
476
477   GST_BUFFER_OFFSET (buf) = src->curoffset;
478   GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
479   src->curoffset += readbytes;
480
481   GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
482
483   /* we're done, return the buffer */
484   *outbuf = buf;
485
486   return GST_FLOW_OK;
487
488   /* ERRORS */
489 #ifndef HAVE_WIN32
490 poll_error:
491   {
492     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
493         ("poll on file descriptor: %s.", g_strerror (errno)));
494     GST_DEBUG_OBJECT (psrc, "Error during poll");
495     return GST_FLOW_ERROR;
496   }
497 stopped:
498   {
499     GST_DEBUG_OBJECT (psrc, "Poll stopped");
500     return GST_FLOW_WRONG_STATE;
501   }
502 #endif
503 alloc_failed:
504   {
505     GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", blocksize);
506     return GST_FLOW_ERROR;
507   }
508 eos:
509   {
510     GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
511     gst_buffer_unref (buf);
512     return GST_FLOW_UNEXPECTED;
513   }
514 read_error:
515   {
516     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
517         ("read on file descriptor: %s.", g_strerror (errno)));
518     GST_DEBUG_OBJECT (psrc, "Error reading from fd");
519     gst_buffer_unmap (buf, data, 0);
520     gst_buffer_unref (buf);
521     return GST_FLOW_ERROR;
522   }
523 }
524
525 static gboolean
526 gst_fd_src_query (GstBaseSrc * basesrc, GstQuery * query)
527 {
528   gboolean ret = FALSE;
529   GstFdSrc *src = GST_FD_SRC (basesrc);
530
531   switch (GST_QUERY_TYPE (query)) {
532     case GST_QUERY_URI:
533       gst_query_set_uri (query, src->uri);
534       ret = TRUE;
535       break;
536     default:
537       ret = FALSE;
538       break;
539   }
540
541   if (!ret)
542     ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
543
544   return ret;
545 }
546
547 static gboolean
548 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
549 {
550   GstFdSrc *src = GST_FD_SRC (bsrc);
551
552   return src->seekable_fd;
553 }
554
555 static gboolean
556 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
557 {
558   GstFdSrc *src = GST_FD_SRC (bsrc);
559   struct stat stat_results;
560
561   if (src->size != -1) {
562     *size = src->size;
563     return TRUE;
564   }
565
566   if (!src->seekable_fd) {
567     /* If it isn't seekable, we won't know the length (but fstat will still
568      * succeed, and wrongly say our length is zero. */
569     return FALSE;
570   }
571
572   if (fstat (src->fd, &stat_results) < 0)
573     goto could_not_stat;
574
575   *size = stat_results.st_size;
576
577   return TRUE;
578
579   /* ERROR */
580 could_not_stat:
581   {
582     return FALSE;
583   }
584 }
585
586 static gboolean
587 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
588 {
589   gint res;
590   gint64 offset;
591   GstFdSrc *src = GST_FD_SRC (bsrc);
592
593   offset = segment->start;
594
595   /* No need to seek to the current position */
596   if (offset == src->curoffset)
597     return TRUE;
598
599   res = lseek (src->fd, offset, SEEK_SET);
600   if (G_UNLIKELY (res < 0 || res != offset))
601     goto seek_failed;
602
603   segment->last_stop = segment->start;
604   segment->time = segment->start;
605
606   return TRUE;
607
608 seek_failed:
609   GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
610   return FALSE;
611 }
612
613 /*** GSTURIHANDLER INTERFACE *************************************************/
614
615 static GstURIType
616 gst_fd_src_uri_get_type (void)
617 {
618   return GST_URI_SRC;
619 }
620
621 static gchar **
622 gst_fd_src_uri_get_protocols (void)
623 {
624   static gchar *protocols[] = { (char *) "fd", NULL };
625
626   return protocols;
627 }
628
629 static const gchar *
630 gst_fd_src_uri_get_uri (GstURIHandler * handler)
631 {
632   GstFdSrc *src = GST_FD_SRC (handler);
633
634   return src->uri;
635 }
636
637 static gboolean
638 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
639 {
640   gchar *protocol, *q;
641   GstFdSrc *src = GST_FD_SRC (handler);
642   gint fd;
643   guint64 size = -1;
644
645   GST_INFO_OBJECT (src, "checking uri %s", uri);
646
647   protocol = gst_uri_get_protocol (uri);
648   if (strcmp (protocol, "fd") != 0) {
649     g_free (protocol);
650     return FALSE;
651   }
652   g_free (protocol);
653
654   if (sscanf (uri, "fd://%d", &fd) != 1 || fd < 0)
655     return FALSE;
656
657   if ((q = g_strstr_len (uri, -1, "?"))) {
658     gchar *sp;
659
660     GST_INFO_OBJECT (src, "found ?");
661
662     if ((sp = g_strstr_len (q, -1, "size="))) {
663       if (sscanf (sp, "size=%" G_GUINT64_FORMAT, &size) != 1) {
664         GST_INFO_OBJECT (src, "parsing size failed");
665         size = -1;
666       } else {
667         GST_INFO_OBJECT (src, "found size %" G_GUINT64_FORMAT, size);
668       }
669     }
670   }
671
672   src->new_fd = fd;
673
674   GST_OBJECT_LOCK (src);
675   if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
676     gst_fd_src_update_fd (src, size);
677   }
678   GST_OBJECT_UNLOCK (src);
679
680   return TRUE;
681 }
682
683 static void
684 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
685 {
686   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
687
688   iface->get_type = gst_fd_src_uri_get_type;
689   iface->get_protocols = gst_fd_src_uri_get_protocols;
690   iface->get_uri = gst_fd_src_uri_get_uri;
691   iface->set_uri = gst_fd_src_uri_set_uri;
692 }