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