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