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 #ifdef __BIONIC__ /* Android */
80 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
85 GST_DEBUG_CATEGORY_STATIC (gst_fd_sink__debug);
86 #define GST_CAT_DEFAULT gst_fd_sink__debug
89 /* FdSink signals and args */
102 static void gst_fd_sink_uri_handler_init (gpointer g_iface,
103 gpointer iface_data);
106 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_fd_sink_uri_handler_init); \
107 GST_DEBUG_CATEGORY_INIT (gst_fd_sink__debug, "fdsink", 0, "fdsink element");
108 #define gst_fd_sink_parent_class parent_class
109 G_DEFINE_TYPE_WITH_CODE (GstFdSink, gst_fd_sink, GST_TYPE_BASE_SINK, _do_init);
111 static void gst_fd_sink_set_property (GObject * object, guint prop_id,
112 const GValue * value, GParamSpec * pspec);
113 static void gst_fd_sink_get_property (GObject * object, guint prop_id,
114 GValue * value, GParamSpec * pspec);
115 static void gst_fd_sink_dispose (GObject * obj);
117 static gboolean gst_fd_sink_query (GstBaseSink * bsink, GstQuery * query);
118 static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
120 static GstFlowReturn gst_fd_sink_render_list (GstBaseSink * bsink,
121 GstBufferList * buffer_list);
122 static gboolean gst_fd_sink_start (GstBaseSink * basesink);
123 static gboolean gst_fd_sink_stop (GstBaseSink * basesink);
124 static gboolean gst_fd_sink_unlock (GstBaseSink * basesink);
125 static gboolean gst_fd_sink_unlock_stop (GstBaseSink * basesink);
126 static gboolean gst_fd_sink_event (GstBaseSink * sink, GstEvent * event);
128 static gboolean gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset);
131 gst_fd_sink_class_init (GstFdSinkClass * klass)
133 GObjectClass *gobject_class;
134 GstElementClass *gstelement_class;
135 GstBaseSinkClass *gstbasesink_class;
137 gobject_class = G_OBJECT_CLASS (klass);
138 gstelement_class = GST_ELEMENT_CLASS (klass);
139 gstbasesink_class = GST_BASE_SINK_CLASS (klass);
141 gobject_class->set_property = gst_fd_sink_set_property;
142 gobject_class->get_property = gst_fd_sink_get_property;
143 gobject_class->dispose = gst_fd_sink_dispose;
145 gst_element_class_set_static_metadata (gstelement_class,
146 "Filedescriptor Sink",
148 "Write data to a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
149 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
151 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_fd_sink_render);
152 gstbasesink_class->render_list = GST_DEBUG_FUNCPTR (gst_fd_sink_render_list);
153 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_fd_sink_start);
154 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_fd_sink_stop);
155 gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock);
156 gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock_stop);
157 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_fd_sink_event);
158 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_fd_sink_query);
160 g_object_class_install_property (gobject_class, ARG_FD,
161 g_param_spec_int ("fd", "fd", "An open file descriptor to write to",
162 0, G_MAXINT, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
166 gst_fd_sink_init (GstFdSink * fdsink)
169 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
170 fdsink->bytes_written = 0;
171 fdsink->current_pos = 0;
173 gst_base_sink_set_sync (GST_BASE_SINK (fdsink), FALSE);
177 gst_fd_sink_dispose (GObject * obj)
179 GstFdSink *fdsink = GST_FD_SINK (obj);
181 g_free (fdsink->uri);
184 G_OBJECT_CLASS (parent_class)->dispose (obj);
188 gst_fd_sink_query (GstBaseSink * bsink, GstQuery * query)
190 gboolean res = FALSE;
193 fdsink = GST_FD_SINK (bsink);
195 switch (GST_QUERY_TYPE (query)) {
196 case GST_QUERY_POSITION:
200 gst_query_parse_position (query, &format, NULL);
203 case GST_FORMAT_DEFAULT:
204 case GST_FORMAT_BYTES:
205 gst_query_set_position (query, GST_FORMAT_BYTES, fdsink->current_pos);
213 case GST_QUERY_FORMATS:
214 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
218 gst_query_set_uri (query, fdsink->uri);
221 case GST_QUERY_SEEKING:{
224 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
225 if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
226 gst_query_set_seeking (query, GST_FORMAT_BYTES, fdsink->seekable, 0,
229 gst_query_set_seeking (query, format, FALSE, 0, -1);
235 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
243 gst_fd_sink_render_buffers (GstFdSink * sink, GstBuffer ** buffers,
244 guint num_buffers, guint8 * mem_nums, guint total_mems)
246 return gst_writev_buffers (GST_OBJECT_CAST (sink), sink->fd, sink->fdset,
247 buffers, num_buffers, mem_nums, total_mems, &sink->bytes_written,
252 gst_fd_sink_render_list (GstBaseSink * bsink, GstBufferList * buffer_list)
259 guint i, num_buffers;
261 sink = GST_FD_SINK_CAST (bsink);
263 num_buffers = gst_buffer_list_length (buffer_list);
264 if (num_buffers == 0)
267 /* extract buffers from list and count memories */
268 buffers = g_newa (GstBuffer *, num_buffers);
269 mem_nums = g_newa (guint8, num_buffers);
270 for (i = 0, total_mems = 0; i < num_buffers; ++i) {
271 buffers[i] = gst_buffer_list_get (buffer_list, i);
272 mem_nums[i] = gst_buffer_n_memory (buffers[i]);
273 total_mems += mem_nums[i];
277 gst_fd_sink_render_buffers (sink, buffers, num_buffers, mem_nums,
284 GST_LOG_OBJECT (sink, "empty buffer list");
290 gst_fd_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
296 sink = GST_FD_SINK_CAST (bsink);
298 n_mem = gst_buffer_n_memory (buffer);
301 flow = gst_fd_sink_render_buffers (sink, &buffer, 1, &n_mem, n_mem);
309 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd, GError ** error)
311 struct stat stat_results;
314 /* see that it is a valid file descriptor */
315 if (fstat (fd, &stat_results) < 0)
318 if (!S_ISREG (stat_results.st_mode))
321 /* see if it is a seekable stream */
322 result = lseek (fd, 0, SEEK_CUR);
333 GST_DEBUG_OBJECT (fdsink, "File descriptor %d is seekable", fd);
339 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
340 ("File descriptor %d is not valid: %s", fd, g_strerror (errno)));
341 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
342 "File descriptor %d is not valid: %s", fd, g_strerror (errno));
347 GST_DEBUG_OBJECT (fdsink, "File descriptor %d is a pipe", fd);
353 gst_fd_sink_start (GstBaseSink * basesink)
356 GstPollFD fd = GST_POLL_FD_INIT;
358 fdsink = GST_FD_SINK (basesink);
359 if (!gst_fd_sink_check_fd (fdsink, fdsink->fd, NULL))
362 if ((fdsink->fdset = gst_poll_new (TRUE)) == NULL)
366 gst_poll_add_fd (fdsink->fdset, &fd);
367 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
369 fdsink->bytes_written = 0;
370 fdsink->current_pos = 0;
372 fdsink->seekable = gst_fd_sink_do_seek (fdsink, 0);
373 GST_INFO_OBJECT (fdsink, "seeking supported: %d", fdsink->seekable);
380 GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
387 gst_fd_sink_stop (GstBaseSink * basesink)
389 GstFdSink *fdsink = GST_FD_SINK (basesink);
392 gst_poll_free (fdsink->fdset);
393 fdsink->fdset = NULL;
400 gst_fd_sink_unlock (GstBaseSink * basesink)
402 GstFdSink *fdsink = GST_FD_SINK (basesink);
404 GST_LOG_OBJECT (fdsink, "Flushing");
405 GST_OBJECT_LOCK (fdsink);
406 gst_poll_set_flushing (fdsink->fdset, TRUE);
407 GST_OBJECT_UNLOCK (fdsink);
413 gst_fd_sink_unlock_stop (GstBaseSink * basesink)
415 GstFdSink *fdsink = GST_FD_SINK (basesink);
417 GST_LOG_OBJECT (fdsink, "No longer flushing");
418 GST_OBJECT_LOCK (fdsink);
419 gst_poll_set_flushing (fdsink->fdset, FALSE);
420 GST_OBJECT_UNLOCK (fdsink);
426 gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd, GError ** error)
429 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
430 "File descriptor %d is not valid", new_fd);
434 if (!gst_fd_sink_check_fd (fdsink, new_fd, error))
438 GST_OBJECT_LOCK (fdsink);
440 GstPollFD fd = GST_POLL_FD_INIT;
443 gst_poll_remove_fd (fdsink->fdset, &fd);
446 gst_poll_add_fd (fdsink->fdset, &fd);
447 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
450 g_free (fdsink->uri);
451 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
453 GST_OBJECT_UNLOCK (fdsink);
464 gst_fd_sink_set_property (GObject * object, guint prop_id,
465 const GValue * value, GParamSpec * pspec)
469 fdsink = GST_FD_SINK (object);
475 fd = g_value_get_int (value);
476 gst_fd_sink_update_fd (fdsink, fd, NULL);
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
486 gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
491 fdsink = GST_FD_SINK (object);
495 g_value_set_int (value, fdsink->fd);
498 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
504 gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset)
508 result = lseek (fdsink->fd, new_offset, SEEK_SET);
513 fdsink->current_pos = new_offset;
515 GST_DEBUG_OBJECT (fdsink, "File descriptor %d to seek to position "
516 "%" G_GUINT64_FORMAT, fdsink->fd, fdsink->current_pos);
523 GST_DEBUG_OBJECT (fdsink, "File descriptor %d failed to seek to position "
524 "%" G_GUINT64_FORMAT, fdsink->fd, new_offset);
530 gst_fd_sink_event (GstBaseSink * sink, GstEvent * event)
535 fdsink = GST_FD_SINK (sink);
537 type = GST_EVENT_TYPE (event);
540 case GST_EVENT_SEGMENT:
542 const GstSegment *segment;
544 gst_event_parse_segment (event, &segment);
546 if (segment->format == GST_FORMAT_BYTES) {
547 /* only try to seek and fail when we are going to a different
549 if (fdsink->current_pos != segment->start) {
550 /* FIXME, the seek should be performed on the pos field, start/stop are
551 * just boundaries for valid bytes offsets. We should also fill the file
552 * with zeroes if the new position extends the current EOF (sparse streams
553 * and segment accumulation). */
554 if (!gst_fd_sink_do_seek (fdsink, (guint64) segment->start))
558 GST_DEBUG_OBJECT (fdsink,
559 "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
560 gst_format_get_name (segment->format));
568 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
572 GST_ELEMENT_ERROR (fdsink, RESOURCE, SEEK, (NULL),
573 ("Error while seeking on file descriptor %d: %s",
574 fdsink->fd, g_strerror (errno)));
575 gst_event_unref (event);
581 /*** GSTURIHANDLER INTERFACE *************************************************/
584 gst_fd_sink_uri_get_type (GType type)
589 static const gchar *const *
590 gst_fd_sink_uri_get_protocols (GType type)
592 static const gchar *protocols[] = { "fd", NULL };
598 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
600 GstFdSink *sink = GST_FD_SINK (handler);
602 /* FIXME: make thread-safe */
603 return g_strdup (sink->uri);
607 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
610 GstFdSink *sink = GST_FD_SINK (handler);
613 if (sscanf (uri, "fd://%d", &fd) != 1) {
614 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
615 "File descriptor URI could not be parsed");
619 return gst_fd_sink_update_fd (sink, fd, error);
623 gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
625 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
627 iface->get_type = gst_fd_sink_uri_get_type;
628 iface->get_protocols = gst_fd_sink_uri_get_protocols;
629 iface->get_uri = gst_fd_sink_uri_get_uri;
630 iface->set_uri = gst_fd_sink_uri_set_uri;