docs/faq/developing.xml: Add a question about how to submit new translations.
[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  * Read data from a unix file descriptor.
29  */
30
31
32 #ifdef HAVE_CONFIG_H
33 #  include "config.h"
34 #endif
35 #include "gst/gst_private.h"
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/socket.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45 #ifdef _MSC_VER
46 #include <io.h>
47 #endif
48 #include <stdlib.h>
49 #include <errno.h>
50
51 #include "gstfdsrc.h"
52
53 /* the select call is also performed on the control sockets, that way
54  * we can send special commands to unblock the select call */
55 #define CONTROL_STOP            'S'     /* stop the select call */
56 #define CONTROL_SOCKETS(src)   src->control_sock
57 #define WRITE_SOCKET(src)      src->control_sock[1]
58 #define READ_SOCKET(src)       src->control_sock[0]
59
60 #define SEND_COMMAND(src, command)          \
61 G_STMT_START {                              \
62   unsigned char c; c = command;             \
63   write (WRITE_SOCKET(src), &c, 1);         \
64 } G_STMT_END
65
66 #define READ_COMMAND(src, command, res)        \
67 G_STMT_START {                                 \
68   res = read(READ_SOCKET(src), &command, 1);   \
69 } G_STMT_END
70
71 #define DEFAULT_BLOCKSIZE       4096
72
73 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
74     GST_PAD_SRC,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS_ANY);
77
78 GST_DEBUG_CATEGORY_STATIC (gst_fd_src_debug);
79 #define GST_CAT_DEFAULT gst_fd_src_debug
80
81 enum
82 {
83   PROP_0,
84   PROP_FD,
85 };
86
87 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
88
89 static void
90 _do_init (GType fd_src_type)
91 {
92   static const GInterfaceInfo urihandler_info = {
93     gst_fd_src_uri_handler_init,
94     NULL,
95     NULL
96   };
97
98   g_type_add_interface_static (fd_src_type, GST_TYPE_URI_HANDLER,
99       &urihandler_info);
100
101   GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
102 }
103
104 GST_BOILERPLATE_FULL (GstFdSrc, gst_fd_src, GstPushSrc, GST_TYPE_PUSH_SRC,
105     _do_init);
106
107 static void gst_fd_src_set_property (GObject * object, guint prop_id,
108     const GValue * value, GParamSpec * pspec);
109 static void gst_fd_src_get_property (GObject * object, guint prop_id,
110     GValue * value, GParamSpec * pspec);
111 static void gst_fd_src_dispose (GObject * obj);
112
113 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
114 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
115 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
116 static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
117 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
118 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
119 static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
120
121 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
122
123 static void
124 gst_fd_src_base_init (gpointer g_class)
125 {
126   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
127
128   gst_element_class_set_details_simple (gstelement_class,
129       "Filedescriptor Source",
130       "Source/File",
131       "Read from a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
132   gst_element_class_add_pad_template (gstelement_class,
133       gst_static_pad_template_get (&srctemplate));
134 }
135
136 static void
137 gst_fd_src_class_init (GstFdSrcClass * klass)
138 {
139   GObjectClass *gobject_class;
140   GstBaseSrcClass *gstbasesrc_class;
141   GstElementClass *gstelement_class;
142   GstPushSrcClass *gstpush_src_class;
143
144   gobject_class = G_OBJECT_CLASS (klass);
145   gstelement_class = GST_ELEMENT_CLASS (klass);
146   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
147   gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
148
149   parent_class = g_type_class_peek_parent (klass);
150
151   gobject_class->set_property = gst_fd_src_set_property;
152   gobject_class->get_property = gst_fd_src_get_property;
153   gobject_class->dispose = gst_fd_src_dispose;
154
155   g_object_class_install_property (gobject_class, PROP_FD,
156       g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
157           0, G_MAXINT, 0, G_PARAM_READWRITE));
158
159   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
160   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
161   gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
162   gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
163   gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
164   gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
165   gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
166
167   gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
168 }
169
170 static void
171 gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
172 {
173   fdsrc->fd = -1;
174   fdsrc->new_fd = 0;
175   fdsrc->seekable_fd = FALSE;
176   fdsrc->uri = g_strdup_printf ("fd://0");
177   fdsrc->curoffset = 0;
178 }
179
180 static void
181 gst_fd_src_dispose (GObject * obj)
182 {
183   GstFdSrc *src = GST_FD_SRC (obj);
184
185   g_free (src->uri);
186   src->uri = NULL;
187
188   G_OBJECT_CLASS (parent_class)->dispose (obj);
189 }
190
191 static void
192 gst_fd_src_update_fd (GstFdSrc * src)
193 {
194   struct stat stat_results;
195
196   if (src->fd != src->new_fd) {
197     GST_INFO_OBJECT (src, "Updating to fd %d", src->new_fd);
198
199     src->fd = src->new_fd;
200
201     g_free (src->uri);
202     src->uri = g_strdup_printf ("fd://%d", src->fd);
203
204     if (fstat (src->fd, &stat_results) < 0)
205       goto not_seekable;
206
207     if (!S_ISREG (stat_results.st_mode))
208       goto not_seekable;
209
210     /* Try a seek of 0 bytes offset to check for seekability */
211     if (lseek (src->fd, 0, SEEK_CUR) < 0)
212       goto not_seekable;
213
214     GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
215     src->seekable_fd = TRUE;
216   }
217   return;
218
219 not_seekable:
220   {
221     GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
222     src->seekable_fd = FALSE;
223   }
224 }
225
226 static gboolean
227 gst_fd_src_start (GstBaseSrc * bsrc)
228 {
229   GstFdSrc *src = GST_FD_SRC (bsrc);
230   gint control_sock[2];
231
232   src->curoffset = 0;
233
234   gst_fd_src_update_fd (src);
235
236   if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
237     goto socket_pair;
238
239   READ_SOCKET (src) = control_sock[0];
240   WRITE_SOCKET (src) = control_sock[1];
241
242   fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
243   fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
244
245   return TRUE;
246
247   /* ERRORS */
248 socket_pair:
249   {
250     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
251         GST_ERROR_SYSTEM);
252     return FALSE;
253   }
254 }
255
256 static gboolean
257 gst_fd_src_stop (GstBaseSrc * bsrc)
258 {
259   GstFdSrc *src = GST_FD_SRC (bsrc);
260
261   close (READ_SOCKET (src));
262   close (WRITE_SOCKET (src));
263
264   return TRUE;
265 }
266
267 static gboolean
268 gst_fd_src_unlock (GstBaseSrc * bsrc)
269 {
270   GstFdSrc *src = GST_FD_SRC (bsrc);
271
272   GST_LOG_OBJECT (src, "sending unlock command");
273   SEND_COMMAND (src, CONTROL_STOP);
274
275   return TRUE;
276 }
277
278 static gboolean
279 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
280 {
281   GstFdSrc *src = GST_FD_SRC (bsrc);
282
283   GST_LOG_OBJECT (src, "clearing unlock command queue");
284
285   /* read all stop commands */
286   while (TRUE) {
287     gchar command;
288     int res;
289
290     GST_LOG_OBJECT (src, "reading command");
291
292     READ_COMMAND (src, command, res);
293     if (res < 0) {
294       GST_LOG_OBJECT (src, "no more commands");
295       /* no more commands */
296       break;
297     }
298   }
299
300   return TRUE;
301 }
302
303 static void
304 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
305     GParamSpec * pspec)
306 {
307   GstFdSrc *src = GST_FD_SRC (object);
308
309   switch (prop_id) {
310     case PROP_FD:
311       src->new_fd = g_value_get_int (value);
312
313       /* If state is ready or below, update the current fd immediately
314        * so it is reflected in get_properties and uri */
315       GST_OBJECT_LOCK (object);
316       if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
317         GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
318         gst_fd_src_update_fd (src);
319       } else {
320         GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
321       }
322       GST_OBJECT_UNLOCK (object);
323       break;
324     default:
325       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
326       break;
327   }
328 }
329
330 static void
331 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
332     GParamSpec * pspec)
333 {
334   GstFdSrc *src = GST_FD_SRC (object);
335
336   switch (prop_id) {
337     case PROP_FD:
338       g_value_set_int (value, src->fd);
339       break;
340     default:
341       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342       break;
343   }
344 }
345
346 static GstFlowReturn
347 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
348 {
349   GstFdSrc *src;
350   GstBuffer *buf;
351   gssize readbytes;
352   guint blocksize;
353
354 #ifndef HAVE_WIN32
355   fd_set readfds;
356   gint retval;
357 #endif
358
359   src = GST_FD_SRC (psrc);
360
361 #ifndef HAVE_WIN32
362   FD_ZERO (&readfds);
363   FD_SET (src->fd, &readfds);
364   FD_SET (READ_SOCKET (src), &readfds);
365
366   do {
367     retval = select (FD_SETSIZE, &readfds, NULL, NULL, NULL);
368   } while ((retval == -1 && errno == EINTR));
369
370   if (retval == -1)
371     goto select_error;
372
373   if (FD_ISSET (READ_SOCKET (src), &readfds))
374     goto stopped;
375 #endif
376
377   blocksize = GST_BASE_SRC (src)->blocksize;
378
379   /* create the buffer */
380   buf = gst_buffer_new_and_alloc (blocksize);
381
382   do {
383     readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
384     GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
385   } while (readbytes == -1 && errno == EINTR);  /* retry if interrupted */
386
387   if (readbytes < 0)
388     goto read_error;
389
390   if (readbytes == 0)
391     goto eos;
392
393   GST_BUFFER_OFFSET (buf) = src->curoffset;
394   GST_BUFFER_SIZE (buf) = readbytes;
395   GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
396   src->curoffset += readbytes;
397
398   GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
399
400   /* we're done, return the buffer */
401   *outbuf = buf;
402
403   return GST_FLOW_OK;
404
405   /* ERRORS */
406 #ifndef HAVE_WIN32
407 select_error:
408   {
409     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
410         ("select on file descriptor: %s.", g_strerror (errno)));
411     GST_DEBUG_OBJECT (psrc, "Error during select");
412     return GST_FLOW_ERROR;
413   }
414 stopped:
415   {
416     GST_DEBUG_OBJECT (psrc, "Select stopped");
417     return GST_FLOW_WRONG_STATE;
418   }
419 #endif
420 eos:
421   {
422     GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
423     gst_buffer_unref (buf);
424     return GST_FLOW_UNEXPECTED;
425   }
426 read_error:
427   {
428     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
429         ("read on file descriptor: %s.", g_strerror (errno)));
430     GST_DEBUG_OBJECT (psrc, "Error reading from fd");
431     gst_buffer_unref (buf);
432     return GST_FLOW_ERROR;
433   }
434 }
435
436 gboolean
437 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
438 {
439   GstFdSrc *src = GST_FD_SRC (bsrc);
440
441   return src->seekable_fd;
442 }
443
444 gboolean
445 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
446 {
447   GstFdSrc *src = GST_FD_SRC (bsrc);
448   struct stat stat_results;
449
450   if (!src->seekable_fd) {
451     /* If it isn't seekable, we won't know the length (but fstat will still
452      * succeed, and wrongly say our length is zero. */
453     return FALSE;
454   }
455
456   if (fstat (src->fd, &stat_results) < 0)
457     goto could_not_stat;
458
459   *size = stat_results.st_size;
460
461   return TRUE;
462
463   /* ERROR */
464 could_not_stat:
465   {
466     return FALSE;
467   }
468 }
469
470 gboolean
471 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
472 {
473   gint res;
474   gint64 offset;
475   GstFdSrc *src = GST_FD_SRC (bsrc);
476
477   offset = segment->start;
478
479   /* No need to seek to the current position */
480   if (offset == src->curoffset)
481     return TRUE;
482
483   res = lseek (src->fd, offset, SEEK_SET);
484   if (G_UNLIKELY (res < 0 || res != offset))
485     goto seek_failed;
486
487   segment->last_stop = segment->start;
488   segment->time = segment->start;
489
490   return TRUE;
491
492 seek_failed:
493   GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
494   return FALSE;
495 }
496
497 /*** GSTURIHANDLER INTERFACE *************************************************/
498
499 static GstURIType
500 gst_fd_src_uri_get_type (void)
501 {
502   return GST_URI_SRC;
503 }
504 static gchar **
505 gst_fd_src_uri_get_protocols (void)
506 {
507   static gchar *protocols[] = { "fd", NULL };
508
509   return protocols;
510 }
511 static const gchar *
512 gst_fd_src_uri_get_uri (GstURIHandler * handler)
513 {
514   GstFdSrc *src = GST_FD_SRC (handler);
515
516   return src->uri;
517 }
518
519 static gboolean
520 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
521 {
522   gchar *protocol;
523   GstFdSrc *src = GST_FD_SRC (handler);
524   gint fd;
525
526   protocol = gst_uri_get_protocol (uri);
527   if (strcmp (protocol, "fd") != 0) {
528     g_free (protocol);
529     return FALSE;
530   }
531   g_free (protocol);
532
533   if (sscanf (uri, "fd://%d", &fd) != 1)
534     return FALSE;
535
536   src->new_fd = fd;
537
538   GST_OBJECT_LOCK (src);
539   if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
540     gst_fd_src_update_fd (src);
541   }
542   GST_OBJECT_UNLOCK (src);
543
544   return TRUE;
545 }
546
547 static void
548 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
549 {
550   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
551
552   iface->get_type = gst_fd_src_uri_get_type;
553   iface->get_protocols = gst_fd_src_uri_get_protocols;
554   iface->get_uri = gst_fd_src_uri_get_uri;
555   iface->set_uri = gst_fd_src_uri_set_uri;
556 }