2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * SECTION:element-fdsink
25 * @see_also: #GstFdSrc
27 * Write data to a unix file descriptor.
29 * This element will synchronize on the clock before writing the data on the
30 * socket. For file descriptors where this does not make sense (files, ...) the
31 * #GstBaseSink:sync property can be used to disable synchronisation.
33 * Last reviewed on 2006-04-28 (0.10.6)
40 #include "../../gst/gst-i18n-lib.h"
42 #include <sys/types.h>
45 #include <io.h> /* lseek, open, close, read */
47 #define lseek _lseeki64
53 #ifdef HAVE_SYS_SOCKET_H
54 #include <sys/socket.h>
65 #define S_ISREG(m) (((m)&S_IFREG)==S_IFREG)
70 #include "gstfdsink.h"
72 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
77 GST_DEBUG_CATEGORY_STATIC (gst_fd_sink__debug);
78 #define GST_CAT_DEFAULT gst_fd_sink__debug
81 /* FdSink signals and args */
94 static void gst_fd_sink_uri_handler_init (gpointer g_iface,
98 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_fd_sink_uri_handler_init); \
99 GST_DEBUG_CATEGORY_INIT (gst_fd_sink__debug, "fdsink", 0, "fdsink element");
100 #define gst_fd_sink_parent_class parent_class
101 G_DEFINE_TYPE_WITH_CODE (GstFdSink, gst_fd_sink, GST_TYPE_BASE_SINK, _do_init);
103 static void gst_fd_sink_set_property (GObject * object, guint prop_id,
104 const GValue * value, GParamSpec * pspec);
105 static void gst_fd_sink_get_property (GObject * object, guint prop_id,
106 GValue * value, GParamSpec * pspec);
107 static void gst_fd_sink_dispose (GObject * obj);
109 static gboolean gst_fd_sink_query (GstPad * pad, GstQuery * query);
110 static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
112 static gboolean gst_fd_sink_start (GstBaseSink * basesink);
113 static gboolean gst_fd_sink_stop (GstBaseSink * basesink);
114 static gboolean gst_fd_sink_unlock (GstBaseSink * basesink);
115 static gboolean gst_fd_sink_unlock_stop (GstBaseSink * basesink);
116 static gboolean gst_fd_sink_event (GstBaseSink * sink, GstEvent * event);
118 static gboolean gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset);
121 gst_fd_sink_class_init (GstFdSinkClass * klass)
123 GObjectClass *gobject_class;
124 GstElementClass *gstelement_class;
125 GstBaseSinkClass *gstbasesink_class;
127 gobject_class = G_OBJECT_CLASS (klass);
128 gstelement_class = GST_ELEMENT_CLASS (klass);
129 gstbasesink_class = GST_BASE_SINK_CLASS (klass);
131 gobject_class->set_property = gst_fd_sink_set_property;
132 gobject_class->get_property = gst_fd_sink_get_property;
133 gobject_class->dispose = gst_fd_sink_dispose;
135 gst_element_class_set_details_simple (gstelement_class,
136 "Filedescriptor Sink",
138 "Write data to a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
139 gst_element_class_add_pad_template (gstelement_class,
140 gst_static_pad_template_get (&sinktemplate));
142 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_fd_sink_render);
143 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_fd_sink_start);
144 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_fd_sink_stop);
145 gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock);
146 gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock_stop);
147 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_fd_sink_event);
149 g_object_class_install_property (gobject_class, ARG_FD,
150 g_param_spec_int ("fd", "fd", "An open file descriptor to write to",
151 0, G_MAXINT, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 gst_fd_sink_init (GstFdSink * fdsink)
159 pad = GST_BASE_SINK_PAD (fdsink);
160 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_fd_sink_query));
163 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
164 fdsink->bytes_written = 0;
165 fdsink->current_pos = 0;
167 gst_base_sink_set_sync (GST_BASE_SINK (fdsink), FALSE);
171 gst_fd_sink_dispose (GObject * obj)
173 GstFdSink *fdsink = GST_FD_SINK (obj);
175 g_free (fdsink->uri);
178 G_OBJECT_CLASS (parent_class)->dispose (obj);
182 gst_fd_sink_query (GstPad * pad, GstQuery * query)
187 fdsink = GST_FD_SINK (GST_PAD_PARENT (pad));
189 switch (GST_QUERY_TYPE (query)) {
190 case GST_QUERY_POSITION:
191 gst_query_parse_position (query, &format, NULL);
193 case GST_FORMAT_DEFAULT:
194 case GST_FORMAT_BYTES:
195 gst_query_set_position (query, GST_FORMAT_BYTES, fdsink->current_pos);
201 case GST_QUERY_FORMATS:
202 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
206 gst_query_set_uri (query, fdsink->uri);
210 return gst_pad_query_default (pad, query);
215 gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer)
226 fdsink = GST_FD_SINK (sink);
228 g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR);
230 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
238 GST_DEBUG_OBJECT (fdsink, "going into select, have %d bytes to write",
240 retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE);
241 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
251 GST_DEBUG_OBJECT (fdsink, "writing %d bytes to file descriptor %d", size,
254 written = write (fdsink->fd, ptr, left);
256 /* check for errors */
257 if (G_UNLIKELY (written < 0)) {
258 /* try to write again on non-fatal errors */
259 if (errno == EAGAIN || errno == EINTR)
262 /* else go to our error handler */
266 /* all is fine when we get here */
269 fdsink->bytes_written += written;
270 fdsink->current_pos += written;
272 GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %" G_GSIZE_FORMAT " left", written,
275 /* short write, select and try to write the remainder */
276 if (G_UNLIKELY (left > 0))
279 gst_buffer_unmap (buffer, data, size);
286 GST_ELEMENT_ERROR (fdsink, RESOURCE, READ, (NULL),
287 ("select on file descriptor: %s.", g_strerror (errno)));
288 GST_DEBUG_OBJECT (fdsink, "Error during select");
289 gst_buffer_unmap (buffer, data, size);
290 return GST_FLOW_ERROR;
294 GST_DEBUG_OBJECT (fdsink, "Select stopped");
295 gst_buffer_unmap (buffer, data, size);
296 return GST_FLOW_WRONG_STATE;
304 GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
307 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
308 ("Error while writing to file descriptor %d: %s",
309 fdsink->fd, g_strerror (errno)));
312 gst_buffer_unmap (buffer, data, size);
313 return GST_FLOW_ERROR;
318 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd)
320 struct stat stat_results;
323 /* see that it is a valid file descriptor */
324 if (fstat (fd, &stat_results) < 0)
327 if (!S_ISREG (stat_results.st_mode))
330 /* see if it is a seekable stream */
331 result = lseek (fd, 0, SEEK_CUR);
342 GST_DEBUG_OBJECT (fdsink, "File descriptor %d is seekable", fd);
348 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
349 ("File descriptor %d is not valid: %s", fd, g_strerror (errno)));
354 GST_DEBUG_OBJECT (fdsink, "File descriptor %d is a pipe", fd);
360 gst_fd_sink_start (GstBaseSink * basesink)
363 GstPollFD fd = GST_POLL_FD_INIT;
365 fdsink = GST_FD_SINK (basesink);
366 if (!gst_fd_sink_check_fd (fdsink, fdsink->fd))
369 if ((fdsink->fdset = gst_poll_new (TRUE)) == NULL)
373 gst_poll_add_fd (fdsink->fdset, &fd);
374 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
376 fdsink->bytes_written = 0;
377 fdsink->current_pos = 0;
384 GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
391 gst_fd_sink_stop (GstBaseSink * basesink)
393 GstFdSink *fdsink = GST_FD_SINK (basesink);
396 gst_poll_free (fdsink->fdset);
397 fdsink->fdset = NULL;
404 gst_fd_sink_unlock (GstBaseSink * basesink)
406 GstFdSink *fdsink = GST_FD_SINK (basesink);
408 GST_LOG_OBJECT (fdsink, "Flushing");
409 GST_OBJECT_LOCK (fdsink);
410 gst_poll_set_flushing (fdsink->fdset, TRUE);
411 GST_OBJECT_UNLOCK (fdsink);
417 gst_fd_sink_unlock_stop (GstBaseSink * basesink)
419 GstFdSink *fdsink = GST_FD_SINK (basesink);
421 GST_LOG_OBJECT (fdsink, "No longer flushing");
422 GST_OBJECT_LOCK (fdsink);
423 gst_poll_set_flushing (fdsink->fdset, FALSE);
424 GST_OBJECT_UNLOCK (fdsink);
430 gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd)
435 if (!gst_fd_sink_check_fd (fdsink, new_fd))
439 GST_OBJECT_LOCK (fdsink);
441 GstPollFD fd = GST_POLL_FD_INIT;
444 gst_poll_remove_fd (fdsink->fdset, &fd);
447 gst_poll_add_fd (fdsink->fdset, &fd);
448 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
451 g_free (fdsink->uri);
452 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
454 GST_OBJECT_UNLOCK (fdsink);
465 gst_fd_sink_set_property (GObject * object, guint prop_id,
466 const GValue * value, GParamSpec * pspec)
470 fdsink = GST_FD_SINK (object);
476 fd = g_value_get_int (value);
477 gst_fd_sink_update_fd (fdsink, fd);
481 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
487 gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
492 fdsink = GST_FD_SINK (object);
496 g_value_set_int (value, fdsink->fd);
499 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
505 gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset)
509 result = lseek (fdsink->fd, new_offset, SEEK_SET);
514 fdsink->current_pos = new_offset;
516 GST_DEBUG_OBJECT (fdsink, "File descriptor %d to seek to position "
517 "%" G_GUINT64_FORMAT, fdsink->fd, fdsink->current_pos);
524 GST_DEBUG_OBJECT (fdsink, "File descriptor %d failed to seek to position "
525 "%" G_GUINT64_FORMAT, fdsink->fd, new_offset);
531 gst_fd_sink_event (GstBaseSink * sink, GstEvent * event)
536 fdsink = GST_FD_SINK (sink);
538 type = GST_EVENT_TYPE (event);
541 case GST_EVENT_SEGMENT:
543 const GstSegment *segment;
545 gst_event_parse_segment (event, &segment);
547 if (segment->format == GST_FORMAT_BYTES) {
548 /* only try to seek and fail when we are going to a different
550 if (fdsink->current_pos != segment->start) {
551 /* FIXME, the seek should be performed on the pos field, start/stop are
552 * just boundaries for valid bytes offsets. We should also fill the file
553 * with zeroes if the new position extends the current EOF (sparse streams
554 * and segment accumulation). */
555 if (!gst_fd_sink_do_seek (fdsink, (guint64) segment->start))
559 GST_DEBUG_OBJECT (fdsink,
560 "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
561 gst_format_get_name (segment->format));
573 GST_ELEMENT_ERROR (fdsink, RESOURCE, SEEK, (NULL),
574 ("Error while seeking on file descriptor %d: %s",
575 fdsink->fd, g_strerror (errno)));
581 /*** GSTURIHANDLER INTERFACE *************************************************/
584 gst_fd_sink_uri_get_type (GType type)
590 gst_fd_sink_uri_get_protocols (GType type)
592 static gchar *protocols[] = { (char *) "fd", NULL };
598 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
600 GstFdSink *sink = GST_FD_SINK (handler);
606 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
609 GstFdSink *sink = GST_FD_SINK (handler);
612 protocol = gst_uri_get_protocol (uri);
613 if (strcmp (protocol, "fd") != 0) {
619 if (sscanf (uri, "fd://%d", &fd) != 1)
622 return gst_fd_sink_update_fd (sink, fd);
626 gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
628 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
630 iface->get_type = gst_fd_sink_uri_get_type;
631 iface->get_protocols = gst_fd_sink_uri_get_protocols;
632 iface->get_uri = gst_fd_sink_uri_get_uri;
633 iface->set_uri = gst_fd_sink_uri_set_uri;