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