plugins/elements/gstfdsrc.c: Added Since doc.
[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  * @short_description: read from a unix file descriptor
26  * @see_also: #GstFdSink
27  *
28  * <refsect2>
29  * <para>
30  * Read data from a unix file descriptor.
31  * </para>
32  * <title>Examples</title>
33  * <para>
34  * Here is a simple pipeline to read from the standard input and dump the data
35  * with a fakesink.
36  * <programlisting>
37  * gst-launch -v fdsrc ! fakesink dump=1
38  * </programlisting>
39  * To generate data, enter some data on the console folowed by enter.
40  * The above mentioned pipeline should dump data packets to the console.
41  * </para>
42  * <para>
43  * If the <link linkend="GstFdSrc--timeout">timeout property</link> is set to a
44  * value bigger than 0, fdsrc will generate an element message named
45  * <classname>&quot;GstFdSrcTimeout&quot;</classname>
46  * if no data was recieved in the given timeout.
47  * The message's structure contains one field:
48  * <itemizedlist>
49  * <listitem>
50  *   <para>
51  *   #guint64
52  *   <classname>&quot;timeout&quot;</classname>: the timeout in microseconds that
53  *   expired when waiting for data.
54  *   </para>
55  * </listitem>
56  * </itemizedlist>
57  * </para>
58  * <para>
59  * Last reviewed on 2008-06-20 (0.10.21)
60  * </para>
61  * </refsect2>
62  */
63
64 #ifdef HAVE_CONFIG_H
65 #  include "config.h"
66 #endif
67 #include "gst/gst_private.h"
68
69 #include <sys/types.h>
70 #include <sys/stat.h>
71 #include <sys/socket.h>
72 #include <fcntl.h>
73 #include <stdio.h>
74 #ifdef HAVE_UNISTD_H
75 #include <unistd.h>
76 #endif
77 #ifdef _MSC_VER
78 #include <io.h>
79 #endif
80 #include <stdlib.h>
81 #include <errno.h>
82
83 #include "gstfdsrc.h"
84
85 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
86     GST_PAD_SRC,
87     GST_PAD_ALWAYS,
88     GST_STATIC_CAPS_ANY);
89
90 GST_DEBUG_CATEGORY_STATIC (gst_fd_src_debug);
91 #define GST_CAT_DEFAULT gst_fd_src_debug
92
93 #define DEFAULT_FD              0
94 #define DEFAULT_TIMEOUT         0
95
96 enum
97 {
98   PROP_0,
99
100   PROP_FD,
101   PROP_TIMEOUT,
102
103   PROP_LAST
104 };
105
106 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
107
108 static void
109 _do_init (GType fd_src_type)
110 {
111   static const GInterfaceInfo urihandler_info = {
112     gst_fd_src_uri_handler_init,
113     NULL,
114     NULL
115   };
116
117   g_type_add_interface_static (fd_src_type, GST_TYPE_URI_HANDLER,
118       &urihandler_info);
119
120   GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
121 }
122
123 GST_BOILERPLATE_FULL (GstFdSrc, gst_fd_src, GstPushSrc, GST_TYPE_PUSH_SRC,
124     _do_init);
125
126 static void gst_fd_src_set_property (GObject * object, guint prop_id,
127     const GValue * value, GParamSpec * pspec);
128 static void gst_fd_src_get_property (GObject * object, guint prop_id,
129     GValue * value, GParamSpec * pspec);
130 static void gst_fd_src_dispose (GObject * obj);
131
132 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
133 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
134 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
135 static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
136 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
137 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
138 static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
139
140 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
141
142 static void
143 gst_fd_src_base_init (gpointer g_class)
144 {
145   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
146
147   gst_element_class_set_details_simple (gstelement_class,
148       "Filedescriptor Source",
149       "Source/File",
150       "Read from a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
151   gst_element_class_add_pad_template (gstelement_class,
152       gst_static_pad_template_get (&srctemplate));
153 }
154
155 static void
156 gst_fd_src_class_init (GstFdSrcClass * klass)
157 {
158   GObjectClass *gobject_class;
159   GstBaseSrcClass *gstbasesrc_class;
160   GstElementClass *gstelement_class;
161   GstPushSrcClass *gstpush_src_class;
162
163   gobject_class = G_OBJECT_CLASS (klass);
164   gstelement_class = GST_ELEMENT_CLASS (klass);
165   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
166   gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
167
168   parent_class = g_type_class_peek_parent (klass);
169
170   gobject_class->set_property = gst_fd_src_set_property;
171   gobject_class->get_property = gst_fd_src_get_property;
172   gobject_class->dispose = gst_fd_src_dispose;
173
174   g_object_class_install_property (gobject_class, PROP_FD,
175       g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
176           0, G_MAXINT, DEFAULT_FD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177   /**
178    * GstFdSrc:timeout
179    *
180    * Post a message after timeout microseconds
181    *
182    * Since: 0.10.21
183    */
184   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
185       g_param_spec_uint64 ("timeout", "Timeout",
186           "Post a message after timeout microseconds (0 = disabled)", 0,
187           G_MAXUINT64, DEFAULT_TIMEOUT,
188           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189
190   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
191   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
192   gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
193   gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
194   gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
195   gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
196   gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
197
198   gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
199 }
200
201 static void
202 gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
203 {
204   fdsrc->new_fd = 0;
205   fdsrc->seekable_fd = FALSE;
206   fdsrc->fd = DEFAULT_FD;
207   fdsrc->timeout = DEFAULT_TIMEOUT;
208   fdsrc->uri = g_strdup_printf ("fd://0");
209   fdsrc->curoffset = 0;
210 }
211
212 static void
213 gst_fd_src_dispose (GObject * obj)
214 {
215   GstFdSrc *src = GST_FD_SRC (obj);
216
217   g_free (src->uri);
218   src->uri = NULL;
219
220   G_OBJECT_CLASS (parent_class)->dispose (obj);
221 }
222
223 static void
224 gst_fd_src_update_fd (GstFdSrc * src)
225 {
226   struct stat stat_results;
227
228   /* we need to always update the fdset since it may not have existed when
229    * gst_fd_src_update_fd () was called earlier */
230   if (src->fdset != NULL) {
231     GstPollFD fd = GST_POLL_FD_INIT;
232
233     if (src->fd >= 0) {
234       fd.fd = src->fd;
235       gst_poll_remove_fd (src->fdset, &fd);
236     }
237
238     fd.fd = src->new_fd;
239     gst_poll_add_fd (src->fdset, &fd);
240     gst_poll_fd_ctl_read (src->fdset, &fd, TRUE);
241   }
242
243   if (src->fd != src->new_fd) {
244     GST_INFO_OBJECT (src, "Updating to fd %d", src->new_fd);
245
246     src->fd = src->new_fd;
247
248     g_free (src->uri);
249     src->uri = g_strdup_printf ("fd://%d", src->fd);
250
251     if (fstat (src->fd, &stat_results) < 0)
252       goto not_seekable;
253
254     if (!S_ISREG (stat_results.st_mode))
255       goto not_seekable;
256
257     /* Try a seek of 0 bytes offset to check for seekability */
258     if (lseek (src->fd, 0, SEEK_CUR) < 0)
259       goto not_seekable;
260
261     GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
262     src->seekable_fd = TRUE;
263   }
264   return;
265
266 not_seekable:
267   {
268     GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
269     src->seekable_fd = FALSE;
270   }
271 }
272
273 static gboolean
274 gst_fd_src_start (GstBaseSrc * bsrc)
275 {
276   GstFdSrc *src = GST_FD_SRC (bsrc);
277
278   src->curoffset = 0;
279
280   if ((src->fdset = gst_poll_new (TRUE)) == NULL)
281     goto socket_pair;
282
283   gst_fd_src_update_fd (src);
284
285   return TRUE;
286
287   /* ERRORS */
288 socket_pair:
289   {
290     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
291         GST_ERROR_SYSTEM);
292     return FALSE;
293   }
294 }
295
296 static gboolean
297 gst_fd_src_stop (GstBaseSrc * bsrc)
298 {
299   GstFdSrc *src = GST_FD_SRC (bsrc);
300
301   if (src->fdset) {
302     gst_poll_free (src->fdset);
303     src->fdset = NULL;
304   }
305
306   return TRUE;
307 }
308
309 static gboolean
310 gst_fd_src_unlock (GstBaseSrc * bsrc)
311 {
312   GstFdSrc *src = GST_FD_SRC (bsrc);
313
314   GST_LOG_OBJECT (src, "Flushing");
315   GST_OBJECT_LOCK (src);
316   gst_poll_set_flushing (src->fdset, TRUE);
317   GST_OBJECT_UNLOCK (src);
318
319   return TRUE;
320 }
321
322 static gboolean
323 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
324 {
325   GstFdSrc *src = GST_FD_SRC (bsrc);
326
327   GST_LOG_OBJECT (src, "No longer flushing");
328   GST_OBJECT_LOCK (src);
329   gst_poll_set_flushing (src->fdset, FALSE);
330   GST_OBJECT_UNLOCK (src);
331
332   return TRUE;
333 }
334
335 static void
336 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
337     GParamSpec * pspec)
338 {
339   GstFdSrc *src = GST_FD_SRC (object);
340
341   switch (prop_id) {
342     case PROP_FD:
343       src->new_fd = g_value_get_int (value);
344
345       /* If state is ready or below, update the current fd immediately
346        * so it is reflected in get_properties and uri */
347       GST_OBJECT_LOCK (object);
348       if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
349         GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
350         gst_fd_src_update_fd (src);
351       } else {
352         GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
353       }
354       GST_OBJECT_UNLOCK (object);
355       break;
356     case PROP_TIMEOUT:
357       src->timeout = g_value_get_uint64 (value);
358       GST_DEBUG_OBJECT (src, "poll timeout set to %" GST_TIME_FORMAT,
359           GST_TIME_ARGS (src->timeout));
360       break;
361     default:
362       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363       break;
364   }
365 }
366
367 static void
368 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
369     GParamSpec * pspec)
370 {
371   GstFdSrc *src = GST_FD_SRC (object);
372
373   switch (prop_id) {
374     case PROP_FD:
375       g_value_set_int (value, src->fd);
376       break;
377     case PROP_TIMEOUT:
378       g_value_set_uint64 (value, src->timeout);
379       break;
380     default:
381       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
382       break;
383   }
384 }
385
386 static GstFlowReturn
387 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
388 {
389   GstFdSrc *src;
390   GstBuffer *buf;
391   gssize readbytes;
392   guint blocksize;
393   GstClockTime timeout;
394
395 #ifndef HAVE_WIN32
396   gboolean try_again;
397   gint retval;
398 #endif
399
400   src = GST_FD_SRC (psrc);
401
402   if (src->timeout > 0) {
403     timeout = src->timeout * GST_USECOND;
404   } else {
405     timeout = GST_CLOCK_TIME_NONE;
406   }
407
408 #ifndef HAVE_WIN32
409   do {
410     try_again = FALSE;
411
412     GST_LOG_OBJECT (src, "doing poll, timeout %" GST_TIME_FORMAT,
413         GST_TIME_ARGS (src->timeout));
414
415     retval = gst_poll_wait (src->fdset, timeout);
416     GST_LOG_OBJECT (src, "poll returned %d", retval);
417
418     if (G_UNLIKELY (retval == -1)) {
419       if (errno == EINTR || errno == EAGAIN) {
420         /* retry if interrupted */
421         try_again = TRUE;
422       } else if (errno == EBUSY) {
423         goto stopped;
424       } else {
425         goto poll_error;
426       }
427     } else if (G_UNLIKELY (retval == 0)) {
428       try_again = TRUE;
429       /* timeout, post element message */
430       gst_element_post_message (GST_ELEMENT_CAST (src),
431           gst_message_new_element (GST_OBJECT_CAST (src),
432               gst_structure_new ("GstFdSrcTimeout",
433                   "timeout", G_TYPE_UINT64, src->timeout, NULL)));
434     }
435   } while (G_UNLIKELY (try_again));     /* retry if interrupted or timeout */
436 #endif
437
438   blocksize = GST_BASE_SRC (src)->blocksize;
439
440   /* create the buffer */
441   buf = gst_buffer_new_and_alloc (blocksize);
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_is_seekable (GstBaseSrc * bsrc)
499 {
500   GstFdSrc *src = GST_FD_SRC (bsrc);
501
502   return src->seekable_fd;
503 }
504
505 static gboolean
506 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
507 {
508   GstFdSrc *src = GST_FD_SRC (bsrc);
509   struct stat stat_results;
510
511   if (!src->seekable_fd) {
512     /* If it isn't seekable, we won't know the length (but fstat will still
513      * succeed, and wrongly say our length is zero. */
514     return FALSE;
515   }
516
517   if (fstat (src->fd, &stat_results) < 0)
518     goto could_not_stat;
519
520   *size = stat_results.st_size;
521
522   return TRUE;
523
524   /* ERROR */
525 could_not_stat:
526   {
527     return FALSE;
528   }
529 }
530
531 static gboolean
532 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
533 {
534   gint res;
535   gint64 offset;
536   GstFdSrc *src = GST_FD_SRC (bsrc);
537
538   offset = segment->start;
539
540   /* No need to seek to the current position */
541   if (offset == src->curoffset)
542     return TRUE;
543
544   res = lseek (src->fd, offset, SEEK_SET);
545   if (G_UNLIKELY (res < 0 || res != offset))
546     goto seek_failed;
547
548   segment->last_stop = segment->start;
549   segment->time = segment->start;
550
551   return TRUE;
552
553 seek_failed:
554   GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
555   return FALSE;
556 }
557
558 /*** GSTURIHANDLER INTERFACE *************************************************/
559
560 static GstURIType
561 gst_fd_src_uri_get_type (void)
562 {
563   return GST_URI_SRC;
564 }
565 static gchar **
566 gst_fd_src_uri_get_protocols (void)
567 {
568   static gchar *protocols[] = { "fd", NULL };
569
570   return protocols;
571 }
572 static const gchar *
573 gst_fd_src_uri_get_uri (GstURIHandler * handler)
574 {
575   GstFdSrc *src = GST_FD_SRC (handler);
576
577   return src->uri;
578 }
579
580 static gboolean
581 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
582 {
583   gchar *protocol;
584   GstFdSrc *src = GST_FD_SRC (handler);
585   gint fd;
586
587   protocol = gst_uri_get_protocol (uri);
588   if (strcmp (protocol, "fd") != 0) {
589     g_free (protocol);
590     return FALSE;
591   }
592   g_free (protocol);
593
594   if (sscanf (uri, "fd://%d", &fd) != 1 || fd < 0)
595     return FALSE;
596
597   src->new_fd = fd;
598
599   GST_OBJECT_LOCK (src);
600   if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
601     gst_fd_src_update_fd (src);
602   }
603   GST_OBJECT_UNLOCK (src);
604
605   return TRUE;
606 }
607
608 static void
609 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
610 {
611   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
612
613   iface->get_type = gst_fd_src_uri_get_type;
614   iface->get_protocols = gst_fd_src_uri_get_protocols;
615   iface->get_uri = gst_fd_src_uri_get_uri;
616   iface->set_uri = gst_fd_src_uri_set_uri;
617 }