plugins/elements/: Remove short_description. Add basic docs for gsttypefindelement.
[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
134 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
135
136 static void
137 gst_fd_src_base_init (gpointer g_class)
138 {
139   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
140
141   gst_element_class_set_details_simple (gstelement_class,
142       "Filedescriptor Source",
143       "Source/File",
144       "Read from a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
145   gst_element_class_add_pad_template (gstelement_class,
146       gst_static_pad_template_get (&srctemplate));
147 }
148
149 static void
150 gst_fd_src_class_init (GstFdSrcClass * klass)
151 {
152   GObjectClass *gobject_class;
153   GstBaseSrcClass *gstbasesrc_class;
154   GstElementClass *gstelement_class;
155   GstPushSrcClass *gstpush_src_class;
156
157   gobject_class = G_OBJECT_CLASS (klass);
158   gstelement_class = GST_ELEMENT_CLASS (klass);
159   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
160   gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
161
162   parent_class = g_type_class_peek_parent (klass);
163
164   gobject_class->set_property = gst_fd_src_set_property;
165   gobject_class->get_property = gst_fd_src_get_property;
166   gobject_class->dispose = gst_fd_src_dispose;
167
168   g_object_class_install_property (gobject_class, PROP_FD,
169       g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
170           0, G_MAXINT, DEFAULT_FD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171   /**
172    * GstFdSrc:timeout
173    *
174    * Post a message after timeout microseconds
175    *
176    * Since: 0.10.21
177    */
178   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
179       g_param_spec_uint64 ("timeout", "Timeout",
180           "Post a message after timeout microseconds (0 = disabled)", 0,
181           G_MAXUINT64, DEFAULT_TIMEOUT,
182           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183
184   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
185   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
186   gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
187   gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
188   gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
189   gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
190   gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
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_new_and_alloc (blocksize);
436
437   do {
438     readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
439     GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
440   } while (readbytes == -1 && errno == EINTR);  /* retry if interrupted */
441
442   if (readbytes < 0)
443     goto read_error;
444
445   if (readbytes == 0)
446     goto eos;
447
448   GST_BUFFER_OFFSET (buf) = src->curoffset;
449   GST_BUFFER_SIZE (buf) = readbytes;
450   GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
451   src->curoffset += readbytes;
452
453   GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
454
455   /* we're done, return the buffer */
456   *outbuf = buf;
457
458   return GST_FLOW_OK;
459
460   /* ERRORS */
461 #ifndef HAVE_WIN32
462 poll_error:
463   {
464     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
465         ("poll on file descriptor: %s.", g_strerror (errno)));
466     GST_DEBUG_OBJECT (psrc, "Error during poll");
467     return GST_FLOW_ERROR;
468   }
469 stopped:
470   {
471     GST_DEBUG_OBJECT (psrc, "Poll stopped");
472     return GST_FLOW_WRONG_STATE;
473   }
474 #endif
475 eos:
476   {
477     GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
478     gst_buffer_unref (buf);
479     return GST_FLOW_UNEXPECTED;
480   }
481 read_error:
482   {
483     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
484         ("read on file descriptor: %s.", g_strerror (errno)));
485     GST_DEBUG_OBJECT (psrc, "Error reading from fd");
486     gst_buffer_unref (buf);
487     return GST_FLOW_ERROR;
488   }
489 }
490
491 static gboolean
492 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
493 {
494   GstFdSrc *src = GST_FD_SRC (bsrc);
495
496   return src->seekable_fd;
497 }
498
499 static gboolean
500 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
501 {
502   GstFdSrc *src = GST_FD_SRC (bsrc);
503   struct stat stat_results;
504
505   if (!src->seekable_fd) {
506     /* If it isn't seekable, we won't know the length (but fstat will still
507      * succeed, and wrongly say our length is zero. */
508     return FALSE;
509   }
510
511   if (fstat (src->fd, &stat_results) < 0)
512     goto could_not_stat;
513
514   *size = stat_results.st_size;
515
516   return TRUE;
517
518   /* ERROR */
519 could_not_stat:
520   {
521     return FALSE;
522   }
523 }
524
525 static gboolean
526 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
527 {
528   gint res;
529   gint64 offset;
530   GstFdSrc *src = GST_FD_SRC (bsrc);
531
532   offset = segment->start;
533
534   /* No need to seek to the current position */
535   if (offset == src->curoffset)
536     return TRUE;
537
538   res = lseek (src->fd, offset, SEEK_SET);
539   if (G_UNLIKELY (res < 0 || res != offset))
540     goto seek_failed;
541
542   segment->last_stop = segment->start;
543   segment->time = segment->start;
544
545   return TRUE;
546
547 seek_failed:
548   GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
549   return FALSE;
550 }
551
552 /*** GSTURIHANDLER INTERFACE *************************************************/
553
554 static GstURIType
555 gst_fd_src_uri_get_type (void)
556 {
557   return GST_URI_SRC;
558 }
559 static gchar **
560 gst_fd_src_uri_get_protocols (void)
561 {
562   static gchar *protocols[] = { "fd", NULL };
563
564   return protocols;
565 }
566 static const gchar *
567 gst_fd_src_uri_get_uri (GstURIHandler * handler)
568 {
569   GstFdSrc *src = GST_FD_SRC (handler);
570
571   return src->uri;
572 }
573
574 static gboolean
575 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
576 {
577   gchar *protocol;
578   GstFdSrc *src = GST_FD_SRC (handler);
579   gint fd;
580
581   protocol = gst_uri_get_protocol (uri);
582   if (strcmp (protocol, "fd") != 0) {
583     g_free (protocol);
584     return FALSE;
585   }
586   g_free (protocol);
587
588   if (sscanf (uri, "fd://%d", &fd) != 1 || fd < 0)
589     return FALSE;
590
591   src->new_fd = fd;
592
593   GST_OBJECT_LOCK (src);
594   if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
595     gst_fd_src_update_fd (src);
596   }
597   GST_OBJECT_UNLOCK (src);
598
599   return TRUE;
600 }
601
602 static void
603 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
604 {
605   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
606
607   iface->get_type = gst_fd_src_uri_get_type;
608   iface->get_protocols = gst_fd_src_uri_get_protocols;
609   iface->get_uri = gst_fd_src_uri_get_uri;
610   iface->set_uri = gst_fd_src_uri_set_uri;
611 }