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 _do_init (GType gst_fd_sink_type)
100 static const GInterfaceInfo urihandler_info = {
101 gst_fd_sink_uri_handler_init,
106 g_type_add_interface_static (gst_fd_sink_type, GST_TYPE_URI_HANDLER,
109 GST_DEBUG_CATEGORY_INIT (gst_fd_sink__debug, "fdsink", 0, "fdsink element");
112 GST_BOILERPLATE_FULL (GstFdSink, gst_fd_sink, GstBaseSink, GST_TYPE_BASE_SINK,
115 static void gst_fd_sink_set_property (GObject * object, guint prop_id,
116 const GValue * value, GParamSpec * pspec);
117 static void gst_fd_sink_get_property (GObject * object, guint prop_id,
118 GValue * value, GParamSpec * pspec);
119 static void gst_fd_sink_dispose (GObject * obj);
121 static gboolean gst_fd_sink_query (GstPad * pad, GstQuery * query);
122 static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
124 static gboolean gst_fd_sink_start (GstBaseSink * basesink);
125 static gboolean gst_fd_sink_stop (GstBaseSink * basesink);
126 static gboolean gst_fd_sink_unlock (GstBaseSink * basesink);
127 static gboolean gst_fd_sink_unlock_stop (GstBaseSink * basesink);
128 static gboolean gst_fd_sink_event (GstBaseSink * sink, GstEvent * event);
130 static gboolean gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset);
133 gst_fd_sink_base_init (gpointer g_class)
135 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
137 gst_element_class_set_details_simple (gstelement_class,
138 "Filedescriptor Sink",
140 "Write data to a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
141 gst_element_class_add_pad_template (gstelement_class,
142 gst_static_pad_template_get (&sinktemplate));
146 gst_fd_sink_class_init (GstFdSinkClass * klass)
148 GObjectClass *gobject_class;
149 GstBaseSinkClass *gstbasesink_class;
151 gobject_class = G_OBJECT_CLASS (klass);
152 gstbasesink_class = GST_BASE_SINK_CLASS (klass);
154 gobject_class->set_property = gst_fd_sink_set_property;
155 gobject_class->get_property = gst_fd_sink_get_property;
156 gobject_class->dispose = gst_fd_sink_dispose;
158 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_fd_sink_render);
159 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_fd_sink_start);
160 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_fd_sink_stop);
161 gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock);
162 gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock_stop);
163 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_fd_sink_event);
165 g_object_class_install_property (gobject_class, ARG_FD,
166 g_param_spec_int ("fd", "fd", "An open file descriptor to write to",
167 0, G_MAXINT, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171 gst_fd_sink_init (GstFdSink * fdsink, GstFdSinkClass * klass)
175 pad = GST_BASE_SINK_PAD (fdsink);
176 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_fd_sink_query));
179 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
180 fdsink->bytes_written = 0;
181 fdsink->current_pos = 0;
183 gst_base_sink_set_sync (GST_BASE_SINK (fdsink), FALSE);
187 gst_fd_sink_dispose (GObject * obj)
189 GstFdSink *fdsink = GST_FD_SINK (obj);
191 g_free (fdsink->uri);
194 G_OBJECT_CLASS (parent_class)->dispose (obj);
198 gst_fd_sink_query (GstPad * pad, GstQuery * query)
203 fdsink = GST_FD_SINK (GST_PAD_PARENT (pad));
205 switch (GST_QUERY_TYPE (query)) {
206 case GST_QUERY_POSITION:
207 gst_query_parse_position (query, &format, NULL);
209 case GST_FORMAT_DEFAULT:
210 case GST_FORMAT_BYTES:
211 gst_query_set_position (query, GST_FORMAT_BYTES, fdsink->current_pos);
217 case GST_QUERY_FORMATS:
218 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
222 gst_query_set_uri (query, fdsink->uri);
225 case GST_QUERY_SEEKING:
226 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
227 if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
228 gst_query_set_seeking (query, GST_FORMAT_BYTES, fdsink->seekable, 0,
231 gst_query_set_seeking (query, format, FALSE, 0, -1);
236 return gst_pad_query_default (pad, query);
241 gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer)
252 fdsink = GST_FD_SINK (sink);
254 g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR);
256 data = GST_BUFFER_DATA (buffer);
257 size = GST_BUFFER_SIZE (buffer);
262 GST_DEBUG_OBJECT (fdsink, "going into select, have %d bytes to write",
264 retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE);
265 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
275 GST_DEBUG_OBJECT (fdsink, "writing %d bytes to file descriptor %d", size,
278 written = write (fdsink->fd, data, size);
280 /* check for errors */
281 if (G_UNLIKELY (written < 0)) {
282 /* try to write again on non-fatal errors */
283 if (errno == EAGAIN || errno == EINTR)
286 /* else go to our error handler */
290 /* all is fine when we get here */
293 fdsink->bytes_written += written;
294 fdsink->current_pos += written;
296 GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %d left", written, size);
298 /* short write, select and try to write the remainder */
299 if (G_UNLIKELY (size > 0))
307 GST_ELEMENT_ERROR (fdsink, RESOURCE, READ, (NULL),
308 ("select on file descriptor: %s.", g_strerror (errno)));
309 GST_DEBUG_OBJECT (fdsink, "Error during select");
310 return GST_FLOW_ERROR;
314 GST_DEBUG_OBJECT (fdsink, "Select stopped");
315 return GST_FLOW_WRONG_STATE;
323 GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
326 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
327 ("Error while writing to file descriptor %d: %s",
328 fdsink->fd, g_strerror (errno)));
331 return GST_FLOW_ERROR;
336 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd)
338 struct stat stat_results;
341 /* see that it is a valid file descriptor */
342 if (fstat (fd, &stat_results) < 0)
345 if (!S_ISREG (stat_results.st_mode))
348 /* see if it is a seekable stream */
349 result = lseek (fd, 0, SEEK_CUR);
360 GST_DEBUG_OBJECT (fdsink, "File descriptor %d is seekable", fd);
366 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
367 ("File descriptor %d is not valid: %s", fd, g_strerror (errno)));
372 GST_DEBUG_OBJECT (fdsink, "File descriptor %d is a pipe", fd);
378 gst_fd_sink_start (GstBaseSink * basesink)
381 GstPollFD fd = GST_POLL_FD_INIT;
383 fdsink = GST_FD_SINK (basesink);
384 if (!gst_fd_sink_check_fd (fdsink, fdsink->fd))
387 if ((fdsink->fdset = gst_poll_new (TRUE)) == NULL)
391 gst_poll_add_fd (fdsink->fdset, &fd);
392 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
394 fdsink->bytes_written = 0;
395 fdsink->current_pos = 0;
397 fdsink->seekable = gst_fd_sink_do_seek (fdsink, 0);
398 GST_INFO_OBJECT (fdsink, "seeking supported: %d", fdsink->seekable);
405 GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
412 gst_fd_sink_stop (GstBaseSink * basesink)
414 GstFdSink *fdsink = GST_FD_SINK (basesink);
417 gst_poll_free (fdsink->fdset);
418 fdsink->fdset = NULL;
425 gst_fd_sink_unlock (GstBaseSink * basesink)
427 GstFdSink *fdsink = GST_FD_SINK (basesink);
429 GST_LOG_OBJECT (fdsink, "Flushing");
430 GST_OBJECT_LOCK (fdsink);
431 gst_poll_set_flushing (fdsink->fdset, TRUE);
432 GST_OBJECT_UNLOCK (fdsink);
438 gst_fd_sink_unlock_stop (GstBaseSink * basesink)
440 GstFdSink *fdsink = GST_FD_SINK (basesink);
442 GST_LOG_OBJECT (fdsink, "No longer flushing");
443 GST_OBJECT_LOCK (fdsink);
444 gst_poll_set_flushing (fdsink->fdset, FALSE);
445 GST_OBJECT_UNLOCK (fdsink);
451 gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd)
456 if (!gst_fd_sink_check_fd (fdsink, new_fd))
460 GST_OBJECT_LOCK (fdsink);
462 GstPollFD fd = GST_POLL_FD_INIT;
465 gst_poll_remove_fd (fdsink->fdset, &fd);
468 gst_poll_add_fd (fdsink->fdset, &fd);
469 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
472 g_free (fdsink->uri);
473 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
475 GST_OBJECT_UNLOCK (fdsink);
486 gst_fd_sink_set_property (GObject * object, guint prop_id,
487 const GValue * value, GParamSpec * pspec)
491 fdsink = GST_FD_SINK (object);
497 fd = g_value_get_int (value);
498 gst_fd_sink_update_fd (fdsink, fd);
502 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
508 gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
513 fdsink = GST_FD_SINK (object);
517 g_value_set_int (value, fdsink->fd);
520 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
526 gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset)
530 result = lseek (fdsink->fd, new_offset, SEEK_SET);
535 fdsink->current_pos = new_offset;
537 GST_DEBUG_OBJECT (fdsink, "File descriptor %d to seek to position "
538 "%" G_GUINT64_FORMAT, fdsink->fd, fdsink->current_pos);
545 GST_DEBUG_OBJECT (fdsink, "File descriptor %d failed to seek to position "
546 "%" G_GUINT64_FORMAT, fdsink->fd, new_offset);
552 gst_fd_sink_event (GstBaseSink * sink, GstEvent * event)
557 fdsink = GST_FD_SINK (sink);
559 type = GST_EVENT_TYPE (event);
562 case GST_EVENT_NEWSEGMENT:
564 gint64 start, stop, pos;
566 gst_event_parse_new_segment (event, NULL, NULL, &format, &start,
569 if (format == GST_FORMAT_BYTES) {
570 /* only try to seek and fail when we are going to a different
572 if (fdsink->current_pos != start) {
573 /* FIXME, the seek should be performed on the pos field, start/stop are
574 * just boundaries for valid bytes offsets. We should also fill the file
575 * with zeroes if the new position extends the current EOF (sparse streams
576 * and segment accumulation). */
577 if (!gst_fd_sink_do_seek (fdsink, (guint64) start))
581 GST_DEBUG_OBJECT (fdsink,
582 "Ignored NEWSEGMENT event of format %u (%s)", (guint) format,
583 gst_format_get_name (format));
595 GST_ELEMENT_ERROR (fdsink, RESOURCE, SEEK, (NULL),
596 ("Error while seeking on file descriptor %d: %s",
597 fdsink->fd, g_strerror (errno)));
603 /*** GSTURIHANDLER INTERFACE *************************************************/
606 gst_fd_sink_uri_get_type (void)
612 gst_fd_sink_uri_get_protocols (void)
614 static gchar *protocols[] = { (char *) "fd", NULL };
620 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
622 GstFdSink *sink = GST_FD_SINK (handler);
628 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
631 GstFdSink *sink = GST_FD_SINK (handler);
634 protocol = gst_uri_get_protocol (uri);
635 if (strcmp (protocol, "fd") != 0) {
641 if (sscanf (uri, "fd://%d", &fd) != 1)
644 return gst_fd_sink_update_fd (sink, fd);
648 gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
650 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
652 iface->get_type = gst_fd_sink_uri_get_type;
653 iface->get_protocols = gst_fd_sink_uri_get_protocols;
654 iface->get_uri = gst_fd_sink_uri_get_uri;
655 iface->set_uri = gst_fd_sink_uri_set_uri;