plugins/elements/: Use init macros and functions.
[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 #define DEFAULT_BLOCKSIZE       4096
54
55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
56     GST_PAD_SRC,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS_ANY);
59
60 GST_DEBUG_CATEGORY_STATIC (gst_fd_src_debug);
61 #define GST_CAT_DEFAULT gst_fd_src_debug
62
63 enum
64 {
65   PROP_0,
66   PROP_FD,
67 };
68
69 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
70
71 static void
72 _do_init (GType fd_src_type)
73 {
74   static const GInterfaceInfo urihandler_info = {
75     gst_fd_src_uri_handler_init,
76     NULL,
77     NULL
78   };
79
80   g_type_add_interface_static (fd_src_type, GST_TYPE_URI_HANDLER,
81       &urihandler_info);
82
83   GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
84 }
85
86 GST_BOILERPLATE_FULL (GstFdSrc, gst_fd_src, GstPushSrc, GST_TYPE_PUSH_SRC,
87     _do_init);
88
89 static void gst_fd_src_set_property (GObject * object, guint prop_id,
90     const GValue * value, GParamSpec * pspec);
91 static void gst_fd_src_get_property (GObject * object, guint prop_id,
92     GValue * value, GParamSpec * pspec);
93 static void gst_fd_src_dispose (GObject * obj);
94
95 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
96 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
97 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
98 static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
99 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
100 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
101 static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
102
103 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
104
105 static void
106 gst_fd_src_base_init (gpointer g_class)
107 {
108   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
109
110   gst_element_class_set_details_simple (gstelement_class,
111       "Filedescriptor Source",
112       "Source/File",
113       "Read from a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
114   gst_element_class_add_pad_template (gstelement_class,
115       gst_static_pad_template_get (&srctemplate));
116 }
117
118 static void
119 gst_fd_src_class_init (GstFdSrcClass * klass)
120 {
121   GObjectClass *gobject_class;
122   GstBaseSrcClass *gstbasesrc_class;
123   GstElementClass *gstelement_class;
124   GstPushSrcClass *gstpush_src_class;
125
126   gobject_class = G_OBJECT_CLASS (klass);
127   gstelement_class = GST_ELEMENT_CLASS (klass);
128   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
129   gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
130
131   parent_class = g_type_class_peek_parent (klass);
132
133   gobject_class->set_property = gst_fd_src_set_property;
134   gobject_class->get_property = gst_fd_src_get_property;
135   gobject_class->dispose = gst_fd_src_dispose;
136
137   g_object_class_install_property (gobject_class, PROP_FD,
138       g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
139           0, G_MAXINT, 0, G_PARAM_READWRITE));
140
141   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
142   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
143   gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
144   gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
145   gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
146   gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
147   gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
148
149   gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
150 }
151
152 static void
153 gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
154 {
155   fdsrc->fd = -1;
156   fdsrc->new_fd = 0;
157   fdsrc->seekable_fd = FALSE;
158   fdsrc->uri = g_strdup_printf ("fd://0");
159   fdsrc->curoffset = 0;
160 }
161
162 static void
163 gst_fd_src_dispose (GObject * obj)
164 {
165   GstFdSrc *src = GST_FD_SRC (obj);
166
167   g_free (src->uri);
168   src->uri = NULL;
169
170   G_OBJECT_CLASS (parent_class)->dispose (obj);
171 }
172
173 static void
174 gst_fd_src_update_fd (GstFdSrc * src)
175 {
176   struct stat stat_results;
177
178   /* we need to always update the fdset since it may not have existed when
179    * gst_fd_src_update_fd() was called earlier */
180   if (src->fdset != NULL) {
181     GstPollFD fd = GST_POLL_FD_INIT;
182
183     if (src->fd >= 0) {
184       fd.fd = src->fd;
185       gst_poll_remove_fd (src->fdset, &fd);
186     }
187
188     /* Reset the GstPollFD */
189     gst_poll_fd_init (&fd);
190
191     fd.fd = src->new_fd;
192     gst_poll_add_fd (src->fdset, &fd);
193     gst_poll_fd_ctl_read (src->fdset, &fd, TRUE);
194   }
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
231   src->curoffset = 0;
232
233   if ((src->fdset = gst_poll_new (GST_POLL_MODE_AUTO, TRUE)) == NULL)
234     goto socket_pair;
235
236   gst_fd_src_update_fd (src);
237
238   return TRUE;
239
240   /* ERRORS */
241 socket_pair:
242   {
243     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
244         GST_ERROR_SYSTEM);
245     return FALSE;
246   }
247 }
248
249 static gboolean
250 gst_fd_src_stop (GstBaseSrc * bsrc)
251 {
252   GstFdSrc *src = GST_FD_SRC (bsrc);
253
254   if (src->fdset) {
255     gst_poll_free (src->fdset);
256     src->fdset = NULL;
257   }
258
259   return TRUE;
260 }
261
262 static gboolean
263 gst_fd_src_unlock (GstBaseSrc * bsrc)
264 {
265   GstFdSrc *src = GST_FD_SRC (bsrc);
266
267   GST_LOG_OBJECT (src, "Flushing");
268   GST_OBJECT_LOCK (src);
269   gst_poll_set_flushing (src->fdset, TRUE);
270   GST_OBJECT_UNLOCK (src);
271
272   return TRUE;
273 }
274
275 static gboolean
276 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
277 {
278   GstFdSrc *src = GST_FD_SRC (bsrc);
279
280   GST_LOG_OBJECT (src, "No longer flushing");
281   GST_OBJECT_LOCK (src);
282   gst_poll_set_flushing (src->fdset, FALSE);
283   GST_OBJECT_UNLOCK (src);
284
285   return TRUE;
286 }
287
288 static void
289 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
290     GParamSpec * pspec)
291 {
292   GstFdSrc *src = GST_FD_SRC (object);
293
294   switch (prop_id) {
295     case PROP_FD:
296       src->new_fd = g_value_get_int (value);
297
298       /* If state is ready or below, update the current fd immediately
299        * so it is reflected in get_properties and uri */
300       GST_OBJECT_LOCK (object);
301       if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
302         GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
303         gst_fd_src_update_fd (src);
304       } else {
305         GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
306       }
307       GST_OBJECT_UNLOCK (object);
308       break;
309     default:
310       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
311       break;
312   }
313 }
314
315 static void
316 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
317     GParamSpec * pspec)
318 {
319   GstFdSrc *src = GST_FD_SRC (object);
320
321   switch (prop_id) {
322     case PROP_FD:
323       g_value_set_int (value, src->fd);
324       break;
325     default:
326       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327       break;
328   }
329 }
330
331 static GstFlowReturn
332 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
333 {
334   GstFdSrc *src;
335   GstBuffer *buf;
336   gssize readbytes;
337   guint blocksize;
338
339 #ifndef HAVE_WIN32
340   gint retval;
341 #endif
342
343   src = GST_FD_SRC (psrc);
344
345 #ifndef HAVE_WIN32
346   do {
347     retval = gst_poll_wait (src->fdset, GST_CLOCK_TIME_NONE);
348   } while (retval == -1 && errno == EINTR);
349
350   if (retval == -1) {
351     if (errno == EBUSY)
352       goto stopped;
353     else
354       goto select_error;
355   }
356 #endif
357
358   blocksize = GST_BASE_SRC (src)->blocksize;
359
360   /* create the buffer */
361   buf = gst_buffer_new_and_alloc (blocksize);
362
363   do {
364     readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
365     GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
366   } while (readbytes == -1 && errno == EINTR);  /* retry if interrupted */
367
368   if (readbytes < 0)
369     goto read_error;
370
371   if (readbytes == 0)
372     goto eos;
373
374   GST_BUFFER_OFFSET (buf) = src->curoffset;
375   GST_BUFFER_SIZE (buf) = readbytes;
376   GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
377   src->curoffset += readbytes;
378
379   GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
380
381   /* we're done, return the buffer */
382   *outbuf = buf;
383
384   return GST_FLOW_OK;
385
386   /* ERRORS */
387 #ifndef HAVE_WIN32
388 select_error:
389   {
390     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
391         ("select on file descriptor: %s.", g_strerror (errno)));
392     GST_DEBUG_OBJECT (psrc, "Error during select");
393     return GST_FLOW_ERROR;
394   }
395 stopped:
396   {
397     GST_DEBUG_OBJECT (psrc, "Select stopped");
398     return GST_FLOW_WRONG_STATE;
399   }
400 #endif
401 eos:
402   {
403     GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
404     gst_buffer_unref (buf);
405     return GST_FLOW_UNEXPECTED;
406   }
407 read_error:
408   {
409     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
410         ("read on file descriptor: %s.", g_strerror (errno)));
411     GST_DEBUG_OBJECT (psrc, "Error reading from fd");
412     gst_buffer_unref (buf);
413     return GST_FLOW_ERROR;
414   }
415 }
416
417 gboolean
418 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
419 {
420   GstFdSrc *src = GST_FD_SRC (bsrc);
421
422   return src->seekable_fd;
423 }
424
425 gboolean
426 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
427 {
428   GstFdSrc *src = GST_FD_SRC (bsrc);
429   struct stat stat_results;
430
431   if (!src->seekable_fd) {
432     /* If it isn't seekable, we won't know the length (but fstat will still
433      * succeed, and wrongly say our length is zero. */
434     return FALSE;
435   }
436
437   if (fstat (src->fd, &stat_results) < 0)
438     goto could_not_stat;
439
440   *size = stat_results.st_size;
441
442   return TRUE;
443
444   /* ERROR */
445 could_not_stat:
446   {
447     return FALSE;
448   }
449 }
450
451 gboolean
452 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
453 {
454   gint res;
455   gint64 offset;
456   GstFdSrc *src = GST_FD_SRC (bsrc);
457
458   offset = segment->start;
459
460   /* No need to seek to the current position */
461   if (offset == src->curoffset)
462     return TRUE;
463
464   res = lseek (src->fd, offset, SEEK_SET);
465   if (G_UNLIKELY (res < 0 || res != offset))
466     goto seek_failed;
467
468   segment->last_stop = segment->start;
469   segment->time = segment->start;
470
471   return TRUE;
472
473 seek_failed:
474   GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
475   return FALSE;
476 }
477
478 /*** GSTURIHANDLER INTERFACE *************************************************/
479
480 static GstURIType
481 gst_fd_src_uri_get_type (void)
482 {
483   return GST_URI_SRC;
484 }
485 static gchar **
486 gst_fd_src_uri_get_protocols (void)
487 {
488   static gchar *protocols[] = { "fd", NULL };
489
490   return protocols;
491 }
492 static const gchar *
493 gst_fd_src_uri_get_uri (GstURIHandler * handler)
494 {
495   GstFdSrc *src = GST_FD_SRC (handler);
496
497   return src->uri;
498 }
499
500 static gboolean
501 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
502 {
503   gchar *protocol;
504   GstFdSrc *src = GST_FD_SRC (handler);
505   gint fd;
506
507   protocol = gst_uri_get_protocol (uri);
508   if (strcmp (protocol, "fd") != 0) {
509     g_free (protocol);
510     return FALSE;
511   }
512   g_free (protocol);
513
514   if (sscanf (uri, "fd://%d", &fd) != 1 || fd < 0)
515     return FALSE;
516
517   src->new_fd = fd;
518
519   GST_OBJECT_LOCK (src);
520   if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
521     gst_fd_src_update_fd (src);
522   }
523   GST_OBJECT_UNLOCK (src);
524
525   return TRUE;
526 }
527
528 static void
529 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
530 {
531   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
532
533   iface->get_type = gst_fd_src_uri_get_type;
534   iface->get_protocols = gst_fd_src_uri_get_protocols;
535   iface->get_uri = gst_fd_src_uri_get_uri;
536   iface->set_uri = gst_fd_src_uri_set_uri;
537 }