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 * bsink, GstBuffer * buffer)
288 sink = GST_FD_SINK_CAST (bsink);
290 n_mem = gst_buffer_n_memory (buffer);
293 flow = gst_fd_sink_render_buffers (sink, &buffer, 1, &n_mem, n_mem);
301 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd, GError ** error)
303 struct stat stat_results;
306 /* see that it is a valid file descriptor */
307 if (fstat (fd, &stat_results) < 0)
310 if (!S_ISREG (stat_results.st_mode))
313 /* see if it is a seekable stream */
314 result = lseek (fd, 0, SEEK_CUR);
325 GST_DEBUG_OBJECT (fdsink, "File descriptor %d is seekable", fd);
331 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
332 ("File descriptor %d is not valid: %s", fd, g_strerror (errno)));
333 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
334 "File descriptor %d is not valid: %s", fd, g_strerror (errno));
339 GST_DEBUG_OBJECT (fdsink, "File descriptor %d is a pipe", fd);
345 gst_fd_sink_start (GstBaseSink * basesink)
348 GstPollFD fd = GST_POLL_FD_INIT;
350 fdsink = GST_FD_SINK (basesink);
351 if (!gst_fd_sink_check_fd (fdsink, fdsink->fd, NULL))
354 if ((fdsink->fdset = gst_poll_new (TRUE)) == NULL)
358 gst_poll_add_fd (fdsink->fdset, &fd);
359 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
361 fdsink->bytes_written = 0;
362 fdsink->current_pos = 0;
364 fdsink->seekable = gst_fd_sink_do_seek (fdsink, 0);
365 GST_INFO_OBJECT (fdsink, "seeking supported: %d", fdsink->seekable);
372 GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
379 gst_fd_sink_stop (GstBaseSink * basesink)
381 GstFdSink *fdsink = GST_FD_SINK (basesink);
384 gst_poll_free (fdsink->fdset);
385 fdsink->fdset = NULL;
392 gst_fd_sink_unlock (GstBaseSink * basesink)
394 GstFdSink *fdsink = GST_FD_SINK (basesink);
396 GST_LOG_OBJECT (fdsink, "Flushing");
397 GST_OBJECT_LOCK (fdsink);
398 gst_poll_set_flushing (fdsink->fdset, TRUE);
399 GST_OBJECT_UNLOCK (fdsink);
405 gst_fd_sink_unlock_stop (GstBaseSink * basesink)
407 GstFdSink *fdsink = GST_FD_SINK (basesink);
409 GST_LOG_OBJECT (fdsink, "No longer flushing");
410 GST_OBJECT_LOCK (fdsink);
411 gst_poll_set_flushing (fdsink->fdset, FALSE);
412 GST_OBJECT_UNLOCK (fdsink);
418 gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd, GError ** error)
421 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
422 "File descriptor %d is not valid", new_fd);
426 if (!gst_fd_sink_check_fd (fdsink, new_fd, error))
430 GST_OBJECT_LOCK (fdsink);
432 GstPollFD fd = GST_POLL_FD_INIT;
435 gst_poll_remove_fd (fdsink->fdset, &fd);
438 gst_poll_add_fd (fdsink->fdset, &fd);
439 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
442 g_free (fdsink->uri);
443 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
445 GST_OBJECT_UNLOCK (fdsink);
456 gst_fd_sink_set_property (GObject * object, guint prop_id,
457 const GValue * value, GParamSpec * pspec)
461 fdsink = GST_FD_SINK (object);
467 fd = g_value_get_int (value);
468 gst_fd_sink_update_fd (fdsink, fd, NULL);
472 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
478 gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
483 fdsink = GST_FD_SINK (object);
487 g_value_set_int (value, fdsink->fd);
490 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
496 gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset)
500 result = lseek (fdsink->fd, new_offset, SEEK_SET);
505 fdsink->current_pos = new_offset;
507 GST_DEBUG_OBJECT (fdsink, "File descriptor %d to seek to position "
508 "%" G_GUINT64_FORMAT, fdsink->fd, fdsink->current_pos);
515 GST_DEBUG_OBJECT (fdsink, "File descriptor %d failed to seek to position "
516 "%" G_GUINT64_FORMAT, fdsink->fd, new_offset);
522 gst_fd_sink_event (GstBaseSink * sink, GstEvent * event)
527 fdsink = GST_FD_SINK (sink);
529 type = GST_EVENT_TYPE (event);
532 case GST_EVENT_SEGMENT:
534 const GstSegment *segment;
536 gst_event_parse_segment (event, &segment);
538 if (segment->format == GST_FORMAT_BYTES) {
539 /* only try to seek and fail when we are going to a different
541 if (fdsink->current_pos != segment->start) {
542 /* FIXME, the seek should be performed on the pos field, start/stop are
543 * just boundaries for valid bytes offsets. We should also fill the file
544 * with zeroes if the new position extends the current EOF (sparse streams
545 * and segment accumulation). */
546 if (!gst_fd_sink_do_seek (fdsink, (guint64) segment->start))
550 GST_DEBUG_OBJECT (fdsink,
551 "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
552 gst_format_get_name (segment->format));
560 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
564 GST_ELEMENT_ERROR (fdsink, RESOURCE, SEEK, (NULL),
565 ("Error while seeking on file descriptor %d: %s",
566 fdsink->fd, g_strerror (errno)));
567 gst_event_unref (event);
573 /*** GSTURIHANDLER INTERFACE *************************************************/
576 gst_fd_sink_uri_get_type (GType type)
581 static const gchar *const *
582 gst_fd_sink_uri_get_protocols (GType type)
584 static const gchar *protocols[] = { "fd", NULL };
590 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
592 GstFdSink *sink = GST_FD_SINK (handler);
594 /* FIXME: make thread-safe */
595 return g_strdup (sink->uri);
599 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
602 GstFdSink *sink = GST_FD_SINK (handler);
605 if (sscanf (uri, "fd://%d", &fd) != 1) {
606 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
607 "File descriptor URI could not be parsed");
611 return gst_fd_sink_update_fd (sink, fd, error);
615 gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
617 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
619 iface->get_type = gst_fd_sink_uri_get_type;
620 iface->get_protocols = gst_fd_sink_uri_get_protocols;
621 iface->get_uri = gst_fd_sink_uri_get_uri;
622 iface->set_uri = gst_fd_sink_uri_set_uri;