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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, 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.
38 #include "../../gst/gst-i18n-lib.h"
40 #include <sys/types.h>
43 #ifdef HAVE_SYS_SOCKET_H
44 #include <sys/socket.h>
55 #define S_ISREG(m) (((m)&S_IFREG)==S_IFREG)
60 #include "gstfdsink.h"
61 #include "gstelements_private.h"
64 #include <io.h> /* lseek, open, close, read */
66 #define lseek _lseeki64
71 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
76 GST_DEBUG_CATEGORY_STATIC (gst_fd_sink__debug);
77 #define GST_CAT_DEFAULT gst_fd_sink__debug
80 /* FdSink signals and args */
93 static void gst_fd_sink_uri_handler_init (gpointer g_iface,
97 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_fd_sink_uri_handler_init); \
98 GST_DEBUG_CATEGORY_INIT (gst_fd_sink__debug, "fdsink", 0, "fdsink element");
99 #define gst_fd_sink_parent_class parent_class
100 G_DEFINE_TYPE_WITH_CODE (GstFdSink, gst_fd_sink, GST_TYPE_BASE_SINK, _do_init);
102 static void gst_fd_sink_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec);
104 static void gst_fd_sink_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * pspec);
106 static void gst_fd_sink_dispose (GObject * obj);
108 static gboolean gst_fd_sink_query (GstBaseSink * bsink, GstQuery * query);
109 static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
111 static GstFlowReturn gst_fd_sink_render_list (GstBaseSink * bsink,
112 GstBufferList * buffer_list);
113 static gboolean gst_fd_sink_start (GstBaseSink * basesink);
114 static gboolean gst_fd_sink_stop (GstBaseSink * basesink);
115 static gboolean gst_fd_sink_unlock (GstBaseSink * basesink);
116 static gboolean gst_fd_sink_unlock_stop (GstBaseSink * basesink);
117 static gboolean gst_fd_sink_event (GstBaseSink * sink, GstEvent * event);
119 static gboolean gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset);
122 gst_fd_sink_class_init (GstFdSinkClass * klass)
124 GObjectClass *gobject_class;
125 GstElementClass *gstelement_class;
126 GstBaseSinkClass *gstbasesink_class;
128 gobject_class = G_OBJECT_CLASS (klass);
129 gstelement_class = GST_ELEMENT_CLASS (klass);
130 gstbasesink_class = GST_BASE_SINK_CLASS (klass);
132 gobject_class->set_property = gst_fd_sink_set_property;
133 gobject_class->get_property = gst_fd_sink_get_property;
134 gobject_class->dispose = gst_fd_sink_dispose;
136 gst_element_class_set_static_metadata (gstelement_class,
137 "Filedescriptor Sink",
139 "Write data to a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
140 gst_element_class_add_pad_template (gstelement_class,
141 gst_static_pad_template_get (&sinktemplate));
143 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_fd_sink_render);
144 gstbasesink_class->render_list = GST_DEBUG_FUNCPTR (gst_fd_sink_render_list);
145 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_fd_sink_start);
146 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_fd_sink_stop);
147 gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock);
148 gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock_stop);
149 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_fd_sink_event);
150 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_fd_sink_query);
152 g_object_class_install_property (gobject_class, ARG_FD,
153 g_param_spec_int ("fd", "fd", "An open file descriptor to write to",
154 0, G_MAXINT, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158 gst_fd_sink_init (GstFdSink * fdsink)
161 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
162 fdsink->bytes_written = 0;
163 fdsink->current_pos = 0;
165 gst_base_sink_set_sync (GST_BASE_SINK (fdsink), FALSE);
169 gst_fd_sink_dispose (GObject * obj)
171 GstFdSink *fdsink = GST_FD_SINK (obj);
173 g_free (fdsink->uri);
176 G_OBJECT_CLASS (parent_class)->dispose (obj);
180 gst_fd_sink_query (GstBaseSink * bsink, GstQuery * query)
182 gboolean res = FALSE;
185 fdsink = GST_FD_SINK (bsink);
187 switch (GST_QUERY_TYPE (query)) {
188 case GST_QUERY_POSITION:
192 gst_query_parse_position (query, &format, NULL);
195 case GST_FORMAT_DEFAULT:
196 case GST_FORMAT_BYTES:
197 gst_query_set_position (query, GST_FORMAT_BYTES, fdsink->current_pos);
205 case GST_QUERY_FORMATS:
206 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
210 gst_query_set_uri (query, fdsink->uri);
213 case GST_QUERY_SEEKING:{
216 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
217 if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
218 gst_query_set_seeking (query, GST_FORMAT_BYTES, fdsink->seekable, 0,
221 gst_query_set_seeking (query, format, FALSE, 0, -1);
227 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
235 gst_fd_sink_render_buffers (GstFdSink * sink, GstBuffer ** buffers,
236 guint num_buffers, guint8 * mem_nums, guint total_mems)
238 return gst_writev_buffers (GST_OBJECT_CAST (sink), sink->fd, sink->fdset,
239 buffers, num_buffers, mem_nums, total_mems, &sink->bytes_written,
244 gst_fd_sink_render_list (GstBaseSink * bsink, GstBufferList * buffer_list)
251 guint i, num_buffers;
253 sink = GST_FD_SINK_CAST (bsink);
255 num_buffers = gst_buffer_list_length (buffer_list);
256 if (num_buffers == 0)
259 /* extract buffers from list and count memories */
260 buffers = g_newa (GstBuffer *, num_buffers);
261 mem_nums = g_newa (guint8, num_buffers);
262 for (i = 0, total_mems = 0; i < num_buffers; ++i) {
263 buffers[i] = gst_buffer_list_get (buffer_list, i);
264 mem_nums[i] = gst_buffer_n_memory (buffers[i]);
265 total_mems += mem_nums[i];
269 gst_fd_sink_render_buffers (sink, buffers, num_buffers, mem_nums,
276 GST_LOG_OBJECT (sink, "empty buffer list");
282 gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer)
294 fdsink = GST_FD_SINK (sink);
296 g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR);
298 gst_buffer_map (buffer, &info, GST_MAP_READ);
306 GST_DEBUG_OBJECT (fdsink, "going into select, have %" G_GSIZE_FORMAT
307 " bytes to write", info.size);
308 retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE);
309 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
319 GST_DEBUG_OBJECT (fdsink, "writing %" G_GSIZE_FORMAT " bytes to"
320 " file descriptor %d", info.size, fdsink->fd);
322 written = write (fdsink->fd, ptr, left);
324 /* check for errors */
325 if (G_UNLIKELY (written < 0)) {
326 /* try to write again on non-fatal errors */
327 if (errno == EAGAIN || errno == EINTR)
330 /* else go to our error handler */
334 /* all is fine when we get here */
337 fdsink->bytes_written += written;
338 fdsink->current_pos += written;
340 GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %" G_GSIZE_FORMAT " left", written,
343 /* short write, select and try to write the remainder */
344 if (G_UNLIKELY (left > 0))
347 gst_buffer_unmap (buffer, &info);
354 GST_ELEMENT_ERROR (fdsink, RESOURCE, READ, (NULL),
355 ("select on file descriptor: %s.", g_strerror (errno)));
356 GST_DEBUG_OBJECT (fdsink, "Error during select");
357 gst_buffer_unmap (buffer, &info);
358 return GST_FLOW_ERROR;
362 GST_DEBUG_OBJECT (fdsink, "Select stopped");
363 gst_buffer_unmap (buffer, &info);
364 return GST_FLOW_FLUSHING;
372 GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
375 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
376 ("Error while writing to file descriptor %d: %s",
377 fdsink->fd, g_strerror (errno)));
380 gst_buffer_unmap (buffer, &info);
381 return GST_FLOW_ERROR;
386 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd, GError ** error)
388 struct stat stat_results;
391 /* see that it is a valid file descriptor */
392 if (fstat (fd, &stat_results) < 0)
395 if (!S_ISREG (stat_results.st_mode))
398 /* see if it is a seekable stream */
399 result = lseek (fd, 0, SEEK_CUR);
410 GST_DEBUG_OBJECT (fdsink, "File descriptor %d is seekable", fd);
416 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
417 ("File descriptor %d is not valid: %s", fd, g_strerror (errno)));
418 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
419 "File descriptor %d is not valid: %s", fd, g_strerror (errno));
424 GST_DEBUG_OBJECT (fdsink, "File descriptor %d is a pipe", fd);
430 gst_fd_sink_start (GstBaseSink * basesink)
433 GstPollFD fd = GST_POLL_FD_INIT;
435 fdsink = GST_FD_SINK (basesink);
436 if (!gst_fd_sink_check_fd (fdsink, fdsink->fd, NULL))
439 if ((fdsink->fdset = gst_poll_new (TRUE)) == NULL)
443 gst_poll_add_fd (fdsink->fdset, &fd);
444 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
446 fdsink->bytes_written = 0;
447 fdsink->current_pos = 0;
449 fdsink->seekable = gst_fd_sink_do_seek (fdsink, 0);
450 GST_INFO_OBJECT (fdsink, "seeking supported: %d", fdsink->seekable);
457 GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
464 gst_fd_sink_stop (GstBaseSink * basesink)
466 GstFdSink *fdsink = GST_FD_SINK (basesink);
469 gst_poll_free (fdsink->fdset);
470 fdsink->fdset = NULL;
477 gst_fd_sink_unlock (GstBaseSink * basesink)
479 GstFdSink *fdsink = GST_FD_SINK (basesink);
481 GST_LOG_OBJECT (fdsink, "Flushing");
482 GST_OBJECT_LOCK (fdsink);
483 gst_poll_set_flushing (fdsink->fdset, TRUE);
484 GST_OBJECT_UNLOCK (fdsink);
490 gst_fd_sink_unlock_stop (GstBaseSink * basesink)
492 GstFdSink *fdsink = GST_FD_SINK (basesink);
494 GST_LOG_OBJECT (fdsink, "No longer flushing");
495 GST_OBJECT_LOCK (fdsink);
496 gst_poll_set_flushing (fdsink->fdset, FALSE);
497 GST_OBJECT_UNLOCK (fdsink);
503 gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd, GError ** error)
506 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
507 "File descriptor %d is not valid", new_fd);
511 if (!gst_fd_sink_check_fd (fdsink, new_fd, error))
515 GST_OBJECT_LOCK (fdsink);
517 GstPollFD fd = GST_POLL_FD_INIT;
520 gst_poll_remove_fd (fdsink->fdset, &fd);
523 gst_poll_add_fd (fdsink->fdset, &fd);
524 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
527 g_free (fdsink->uri);
528 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
530 GST_OBJECT_UNLOCK (fdsink);
541 gst_fd_sink_set_property (GObject * object, guint prop_id,
542 const GValue * value, GParamSpec * pspec)
546 fdsink = GST_FD_SINK (object);
552 fd = g_value_get_int (value);
553 gst_fd_sink_update_fd (fdsink, fd, NULL);
557 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
563 gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
568 fdsink = GST_FD_SINK (object);
572 g_value_set_int (value, fdsink->fd);
575 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
581 gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset)
585 result = lseek (fdsink->fd, new_offset, SEEK_SET);
590 fdsink->current_pos = new_offset;
592 GST_DEBUG_OBJECT (fdsink, "File descriptor %d to seek to position "
593 "%" G_GUINT64_FORMAT, fdsink->fd, fdsink->current_pos);
600 GST_DEBUG_OBJECT (fdsink, "File descriptor %d failed to seek to position "
601 "%" G_GUINT64_FORMAT, fdsink->fd, new_offset);
607 gst_fd_sink_event (GstBaseSink * sink, GstEvent * event)
612 fdsink = GST_FD_SINK (sink);
614 type = GST_EVENT_TYPE (event);
617 case GST_EVENT_SEGMENT:
619 const GstSegment *segment;
621 gst_event_parse_segment (event, &segment);
623 if (segment->format == GST_FORMAT_BYTES) {
624 /* only try to seek and fail when we are going to a different
626 if (fdsink->current_pos != segment->start) {
627 /* FIXME, the seek should be performed on the pos field, start/stop are
628 * just boundaries for valid bytes offsets. We should also fill the file
629 * with zeroes if the new position extends the current EOF (sparse streams
630 * and segment accumulation). */
631 if (!gst_fd_sink_do_seek (fdsink, (guint64) segment->start))
635 GST_DEBUG_OBJECT (fdsink,
636 "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
637 gst_format_get_name (segment->format));
645 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
649 GST_ELEMENT_ERROR (fdsink, RESOURCE, SEEK, (NULL),
650 ("Error while seeking on file descriptor %d: %s",
651 fdsink->fd, g_strerror (errno)));
652 gst_event_unref (event);
658 /*** GSTURIHANDLER INTERFACE *************************************************/
661 gst_fd_sink_uri_get_type (GType type)
666 static const gchar *const *
667 gst_fd_sink_uri_get_protocols (GType type)
669 static const gchar *protocols[] = { "fd", NULL };
675 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
677 GstFdSink *sink = GST_FD_SINK (handler);
679 /* FIXME: make thread-safe */
680 return g_strdup (sink->uri);
684 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
687 GstFdSink *sink = GST_FD_SINK (handler);
690 if (sscanf (uri, "fd://%d", &fd) != 1) {
691 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
692 "File descriptor URI could not be parsed");
696 return gst_fd_sink_update_fd (sink, fd, error);
700 gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
702 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
704 iface->get_type = gst_fd_sink_uri_get_type;
705 iface->get_protocols = gst_fd_sink_uri_get_protocols;
706 iface->get_uri = gst_fd_sink_uri_get_uri;
707 iface->set_uri = gst_fd_sink_uri_set_uri;