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