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