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