1 /* GStreamer
2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 *
5 * gstfdsink.c:
6 *
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.
11 *
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.
16 *
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.
21 */
22
23 /**
24 * SECTION:element-fdsink
25 * @title: fdsink
26 * @see_also: #GstFdSrc
27 *
28 * Write data to a unix file descriptor.
29 *
30 * This element will synchronize on the clock before writing the data on the
31 * socket. For file descriptors where this does not make sense (files, ...) the
32 * #GstBaseSink:sync property can be used to disable synchronisation.
33 */
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include "../../gst/gst-i18n-lib.h"
40
41 #include <sys/types.h>
42
43 #include <sys/stat.h>
44 #ifdef HAVE_SYS_SOCKET_H
45 #include <sys/socket.h>
46 #endif
47 #include <fcntl.h>
48 #include <stdio.h>
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52 #ifdef _MSC_VER
53 #undef stat
54 #define stat _stat
55 #define fstat _fstat
56 #define S_ISREG(m) (((m)&S_IFREG)==S_IFREG)
57 #endif
58 #include <errno.h>
59 #include <string.h>
60
61 #include "gstfdsink.h"
62 #include "gstelements_private.h"
63
64 #ifdef G_OS_WIN32
65 #include <io.h> /* lseek, open, close, read */
66 #undef lseek
67 #define lseek _lseeki64
68 #undef off_t
69 #define off_t guint64
70 #endif
71
72 #if defined(__BIONIC__) /* Android */
73 #if defined(__ANDROID_API__) && __ANDROID_API__ >= 21
74 #undef fstat
75 #define fstat fstat64
76 #endif
77 #endif
78
79 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
80 GST_PAD_SINK,
81 GST_PAD_ALWAYS,
82 GST_STATIC_CAPS_ANY);
83
84 GST_DEBUG_CATEGORY_STATIC (gst_fd_sink__debug);
85 #define GST_CAT_DEFAULT gst_fd_sink__debug
86
87
88 /* FdSink signals and args */
89 enum
90 {
91 /* FILL ME */
92 LAST_SIGNAL
93 };
94
95 enum
96 {
97 ARG_0,
98 ARG_FD
99 };
100
101 static void gst_fd_sink_uri_handler_init (gpointer g_iface,
102 gpointer iface_data);
103
104 #define _do_init \
105 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_fd_sink_uri_handler_init); \
106 GST_DEBUG_CATEGORY_INIT (gst_fd_sink__debug, "fdsink", 0, "fdsink element");
107 #define gst_fd_sink_parent_class parent_class
108 G_DEFINE_TYPE_WITH_CODE (GstFdSink, gst_fd_sink, GST_TYPE_BASE_SINK, _do_init);
109
110 static void gst_fd_sink_set_property (GObject * object, guint prop_id,
111 const GValue * value, GParamSpec * pspec);
112 static void gst_fd_sink_get_property (GObject * object, guint prop_id,
113 GValue * value, GParamSpec * pspec);
114 static void gst_fd_sink_dispose (GObject * obj);
115
116 static gboolean gst_fd_sink_query (GstBaseSink * bsink, GstQuery * query);
117 static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
118 GstBuffer * buffer);
119 static GstFlowReturn gst_fd_sink_render_list (GstBaseSink * bsink,
120 GstBufferList * buffer_list);
121 static gboolean gst_fd_sink_start (GstBaseSink * basesink);
122 static gboolean gst_fd_sink_stop (GstBaseSink * basesink);
123 static gboolean gst_fd_sink_unlock (GstBaseSink * basesink);
124 static gboolean gst_fd_sink_unlock_stop (GstBaseSink * basesink);
125 static gboolean gst_fd_sink_event (GstBaseSink * sink, GstEvent * event);
126
127 static gboolean gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset);
128
129 static void
gst_fd_sink_class_init(GstFdSinkClass * klass)130 gst_fd_sink_class_init (GstFdSinkClass * klass)
131 {
132 GObjectClass *gobject_class;
133 GstElementClass *gstelement_class;
134 GstBaseSinkClass *gstbasesink_class;
135
136 gobject_class = G_OBJECT_CLASS (klass);
137 gstelement_class = GST_ELEMENT_CLASS (klass);
138 gstbasesink_class = GST_BASE_SINK_CLASS (klass);
139
140 gobject_class->set_property = gst_fd_sink_set_property;
141 gobject_class->get_property = gst_fd_sink_get_property;
142 gobject_class->dispose = gst_fd_sink_dispose;
143
144 gst_element_class_set_static_metadata (gstelement_class,
145 "Filedescriptor Sink",
146 "Sink/File",
147 "Write data to a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
148 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
149
150 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_fd_sink_render);
151 gstbasesink_class->render_list = GST_DEBUG_FUNCPTR (gst_fd_sink_render_list);
152 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_fd_sink_start);
153 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_fd_sink_stop);
154 gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock);
155 gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock_stop);
156 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_fd_sink_event);
157 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_fd_sink_query);
158
159 g_object_class_install_property (gobject_class, ARG_FD,
160 g_param_spec_int ("fd", "fd", "An open file descriptor to write to",
161 0, G_MAXINT, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162 }
163
164 static void
gst_fd_sink_init(GstFdSink * fdsink)165 gst_fd_sink_init (GstFdSink * fdsink)
166 {
167 fdsink->fd = 1;
168 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
169 fdsink->bytes_written = 0;
170 fdsink->current_pos = 0;
171
172 gst_base_sink_set_sync (GST_BASE_SINK (fdsink), FALSE);
173 }
174
175 static void
gst_fd_sink_dispose(GObject * obj)176 gst_fd_sink_dispose (GObject * obj)
177 {
178 GstFdSink *fdsink = GST_FD_SINK (obj);
179
180 g_free (fdsink->uri);
181 fdsink->uri = NULL;
182
183 G_OBJECT_CLASS (parent_class)->dispose (obj);
184 }
185
186 static gboolean
gst_fd_sink_query(GstBaseSink * bsink,GstQuery * query)187 gst_fd_sink_query (GstBaseSink * bsink, GstQuery * query)
188 {
189 gboolean res = FALSE;
190 GstFdSink *fdsink;
191
192 fdsink = GST_FD_SINK (bsink);
193
194 switch (GST_QUERY_TYPE (query)) {
195 case GST_QUERY_POSITION:
196 {
197 GstFormat format;
198
199 gst_query_parse_position (query, &format, NULL);
200
201 switch (format) {
202 case GST_FORMAT_DEFAULT:
203 case GST_FORMAT_BYTES:
204 gst_query_set_position (query, GST_FORMAT_BYTES, fdsink->current_pos);
205 res = TRUE;
206 break;
207 default:
208 break;
209 }
210 break;
211 }
212 case GST_QUERY_FORMATS:
213 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
214 res = TRUE;
215 break;
216 case GST_QUERY_URI:
217 gst_query_set_uri (query, fdsink->uri);
218 res = TRUE;
219 break;
220 case GST_QUERY_SEEKING:{
221 GstFormat format;
222
223 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
224 if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
225 gst_query_set_seeking (query, GST_FORMAT_BYTES, fdsink->seekable, 0,
226 -1);
227 } else {
228 gst_query_set_seeking (query, format, FALSE, 0, -1);
229 }
230 res = TRUE;
231 break;
232 }
233 default:
234 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
235 break;
236
237 }
238 return res;
239 }
240
241 static GstFlowReturn
gst_fd_sink_render_buffers(GstFdSink * sink,GstBuffer ** buffers,guint num_buffers,guint8 * mem_nums,guint total_mems)242 gst_fd_sink_render_buffers (GstFdSink * sink, GstBuffer ** buffers,
243 guint num_buffers, guint8 * mem_nums, guint total_mems)
244 {
245 GstFlowReturn ret;
246 guint64 skip = 0;
247
248 for (;;) {
249 guint64 bytes_written = 0;
250
251 ret = gst_writev_buffers (GST_OBJECT_CAST (sink), sink->fd, sink->fdset,
252 buffers, num_buffers, mem_nums, total_mems, &bytes_written, skip);
253
254 sink->bytes_written += bytes_written;
255 sink->current_pos += bytes_written;
256 skip += bytes_written;
257
258 if (!sink->unlock)
259 break;
260
261 ret = gst_base_sink_wait_preroll (GST_BASE_SINK (sink));
262 if (ret != GST_FLOW_OK)
263 return ret;
264 }
265
266 return ret;
267 }
268
269 static GstFlowReturn
gst_fd_sink_render_list(GstBaseSink * bsink,GstBufferList * buffer_list)270 gst_fd_sink_render_list (GstBaseSink * bsink, GstBufferList * buffer_list)
271 {
272 GstFlowReturn flow;
273 GstBuffer **buffers;
274 GstFdSink *sink;
275 guint8 *mem_nums;
276 guint total_mems;
277 guint i, num_buffers;
278
279 sink = GST_FD_SINK_CAST (bsink);
280
281 num_buffers = gst_buffer_list_length (buffer_list);
282 if (num_buffers == 0)
283 goto no_data;
284
285 /* extract buffers from list and count memories */
286 buffers = g_newa (GstBuffer *, num_buffers);
287 mem_nums = g_newa (guint8, num_buffers);
288 for (i = 0, total_mems = 0; i < num_buffers; ++i) {
289 buffers[i] = gst_buffer_list_get (buffer_list, i);
290 mem_nums[i] = gst_buffer_n_memory (buffers[i]);
291 total_mems += mem_nums[i];
292 }
293
294 flow =
295 gst_fd_sink_render_buffers (sink, buffers, num_buffers, mem_nums,
296 total_mems);
297
298 return flow;
299
300 no_data:
301 {
302 GST_LOG_OBJECT (sink, "empty buffer list");
303 return GST_FLOW_OK;
304 }
305 }
306
307 static GstFlowReturn
gst_fd_sink_render(GstBaseSink * bsink,GstBuffer * buffer)308 gst_fd_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
309 {
310 GstFlowReturn flow;
311 GstFdSink *sink;
312 guint8 n_mem;
313
314 sink = GST_FD_SINK_CAST (bsink);
315
316 n_mem = gst_buffer_n_memory (buffer);
317
318 if (n_mem > 0)
319 flow = gst_fd_sink_render_buffers (sink, &buffer, 1, &n_mem, n_mem);
320 else
321 flow = GST_FLOW_OK;
322
323 return flow;
324 }
325
326 static gboolean
gst_fd_sink_check_fd(GstFdSink * fdsink,int fd,GError ** error)327 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd, GError ** error)
328 {
329 struct stat stat_results;
330 off_t result;
331
332 /* see that it is a valid file descriptor */
333 if (fstat (fd, &stat_results) < 0)
334 goto invalid;
335
336 if (!S_ISREG (stat_results.st_mode))
337 goto not_seekable;
338
339 /* see if it is a seekable stream */
340 result = lseek (fd, 0, SEEK_CUR);
341 if (result == -1) {
342 switch (errno) {
343 case EINVAL:
344 case EBADF:
345 goto invalid;
346
347 case ESPIPE:
348 goto not_seekable;
349 }
350 } else
351 GST_DEBUG_OBJECT (fdsink, "File descriptor %d is seekable", fd);
352
353 return TRUE;
354
355 invalid:
356 {
357 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
358 ("File descriptor %d is not valid: %s", fd, g_strerror (errno)));
359 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
360 "File descriptor %d is not valid: %s", fd, g_strerror (errno));
361 return FALSE;
362 }
363 not_seekable:
364 {
365 GST_DEBUG_OBJECT (fdsink, "File descriptor %d is a pipe", fd);
366 return TRUE;
367 }
368 }
369
370 static gboolean
gst_fd_sink_start(GstBaseSink * basesink)371 gst_fd_sink_start (GstBaseSink * basesink)
372 {
373 GstFdSink *fdsink;
374 GstPollFD fd = GST_POLL_FD_INIT;
375
376 fdsink = GST_FD_SINK (basesink);
377 if (!gst_fd_sink_check_fd (fdsink, fdsink->fd, NULL))
378 return FALSE;
379
380 if ((fdsink->fdset = gst_poll_new (TRUE)) == NULL)
381 goto socket_pair;
382
383 fd.fd = fdsink->fd;
384 gst_poll_add_fd (fdsink->fdset, &fd);
385 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
386
387 fdsink->bytes_written = 0;
388 fdsink->current_pos = 0;
389
390 fdsink->seekable = gst_fd_sink_do_seek (fdsink, 0);
391 GST_INFO_OBJECT (fdsink, "seeking supported: %d", fdsink->seekable);
392
393 return TRUE;
394
395 /* ERRORS */
396 socket_pair:
397 {
398 GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
399 GST_ERROR_SYSTEM);
400 return FALSE;
401 }
402 }
403
404 static gboolean
gst_fd_sink_stop(GstBaseSink * basesink)405 gst_fd_sink_stop (GstBaseSink * basesink)
406 {
407 GstFdSink *fdsink = GST_FD_SINK (basesink);
408
409 if (fdsink->fdset) {
410 gst_poll_free (fdsink->fdset);
411 fdsink->fdset = NULL;
412 }
413
414 return TRUE;
415 }
416
417 static gboolean
gst_fd_sink_unlock(GstBaseSink * basesink)418 gst_fd_sink_unlock (GstBaseSink * basesink)
419 {
420 GstFdSink *fdsink = GST_FD_SINK (basesink);
421
422 GST_LOG_OBJECT (fdsink, "Flushing");
423 GST_OBJECT_LOCK (fdsink);
424 fdsink->unlock = TRUE;
425 gst_poll_set_flushing (fdsink->fdset, TRUE);
426 GST_OBJECT_UNLOCK (fdsink);
427
428 return TRUE;
429 }
430
431 static gboolean
gst_fd_sink_unlock_stop(GstBaseSink * basesink)432 gst_fd_sink_unlock_stop (GstBaseSink * basesink)
433 {
434 GstFdSink *fdsink = GST_FD_SINK (basesink);
435
436 GST_LOG_OBJECT (fdsink, "No longer flushing");
437 GST_OBJECT_LOCK (fdsink);
438 fdsink->unlock = FALSE;
439 gst_poll_set_flushing (fdsink->fdset, FALSE);
440 GST_OBJECT_UNLOCK (fdsink);
441
442 return TRUE;
443 }
444
445 static gboolean
gst_fd_sink_update_fd(GstFdSink * fdsink,int new_fd,GError ** error)446 gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd, GError ** error)
447 {
448 if (new_fd < 0) {
449 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
450 "File descriptor %d is not valid", new_fd);
451 return FALSE;
452 }
453
454 if (!gst_fd_sink_check_fd (fdsink, new_fd, error))
455 goto invalid;
456
457 /* assign the fd */
458 GST_OBJECT_LOCK (fdsink);
459 if (fdsink->fdset) {
460 GstPollFD fd = GST_POLL_FD_INIT;
461
462 fd.fd = fdsink->fd;
463 gst_poll_remove_fd (fdsink->fdset, &fd);
464
465 fd.fd = new_fd;
466 gst_poll_add_fd (fdsink->fdset, &fd);
467 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
468 }
469 fdsink->fd = new_fd;
470 g_free (fdsink->uri);
471 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
472
473 GST_OBJECT_UNLOCK (fdsink);
474
475 return TRUE;
476
477 invalid:
478 {
479 return FALSE;
480 }
481 }
482
483 static void
gst_fd_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)484 gst_fd_sink_set_property (GObject * object, guint prop_id,
485 const GValue * value, GParamSpec * pspec)
486 {
487 GstFdSink *fdsink;
488
489 fdsink = GST_FD_SINK (object);
490
491 switch (prop_id) {
492 case ARG_FD:{
493 int fd;
494
495 fd = g_value_get_int (value);
496 gst_fd_sink_update_fd (fdsink, fd, NULL);
497 break;
498 }
499 default:
500 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
501 break;
502 }
503 }
504
505 static void
gst_fd_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)506 gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
507 GParamSpec * pspec)
508 {
509 GstFdSink *fdsink;
510
511 fdsink = GST_FD_SINK (object);
512
513 switch (prop_id) {
514 case ARG_FD:
515 g_value_set_int (value, fdsink->fd);
516 break;
517 default:
518 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
519 break;
520 }
521 }
522
523 static gboolean
gst_fd_sink_do_seek(GstFdSink * fdsink,guint64 new_offset)524 gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset)
525 {
526 off_t result;
527
528 result = lseek (fdsink->fd, new_offset, SEEK_SET);
529
530 if (result == -1)
531 goto seek_failed;
532
533 fdsink->current_pos = new_offset;
534
535 GST_DEBUG_OBJECT (fdsink, "File descriptor %d to seek to position "
536 "%" G_GUINT64_FORMAT, fdsink->fd, fdsink->current_pos);
537
538 return TRUE;
539
540 /* ERRORS */
541 seek_failed:
542 {
543 GST_DEBUG_OBJECT (fdsink, "File descriptor %d failed to seek to position "
544 "%" G_GUINT64_FORMAT, fdsink->fd, new_offset);
545 return FALSE;
546 }
547 }
548
549 static gboolean
gst_fd_sink_event(GstBaseSink * sink,GstEvent * event)550 gst_fd_sink_event (GstBaseSink * sink, GstEvent * event)
551 {
552 GstEventType type;
553 GstFdSink *fdsink;
554
555 fdsink = GST_FD_SINK (sink);
556
557 type = GST_EVENT_TYPE (event);
558
559 switch (type) {
560 case GST_EVENT_SEGMENT:
561 {
562 const GstSegment *segment;
563
564 gst_event_parse_segment (event, &segment);
565
566 if (segment->format == GST_FORMAT_BYTES) {
567 /* only try to seek and fail when we are going to a different
568 * position */
569 if (fdsink->current_pos != segment->start) {
570 /* FIXME, the seek should be performed on the pos field, start/stop are
571 * just boundaries for valid bytes offsets. We should also fill the file
572 * with zeroes if the new position extends the current EOF (sparse streams
573 * and segment accumulation). */
574 if (!gst_fd_sink_do_seek (fdsink, (guint64) segment->start))
575 goto seek_failed;
576 }
577 } else {
578 GST_DEBUG_OBJECT (fdsink,
579 "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
580 gst_format_get_name (segment->format));
581 }
582 break;
583 }
584 default:
585 break;
586 }
587
588 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
589
590 seek_failed:
591 {
592 GST_ELEMENT_ERROR (fdsink, RESOURCE, SEEK, (NULL),
593 ("Error while seeking on file descriptor %d: %s",
594 fdsink->fd, g_strerror (errno)));
595 gst_event_unref (event);
596 return FALSE;
597 }
598
599 }
600
601 /*** GSTURIHANDLER INTERFACE *************************************************/
602
603 static GstURIType
gst_fd_sink_uri_get_type(GType type)604 gst_fd_sink_uri_get_type (GType type)
605 {
606 return GST_URI_SINK;
607 }
608
609 static const gchar *const *
gst_fd_sink_uri_get_protocols(GType type)610 gst_fd_sink_uri_get_protocols (GType type)
611 {
612 static const gchar *protocols[] = { "fd", NULL };
613
614 return protocols;
615 }
616
617 static gchar *
gst_fd_sink_uri_get_uri(GstURIHandler * handler)618 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
619 {
620 GstFdSink *sink = GST_FD_SINK (handler);
621
622 /* FIXME: make thread-safe */
623 return g_strdup (sink->uri);
624 }
625
626 static gboolean
gst_fd_sink_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)627 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
628 GError ** error)
629 {
630 GstFdSink *sink = GST_FD_SINK (handler);
631 gint fd;
632
633 if (sscanf (uri, "fd://%d", &fd) != 1) {
634 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
635 "File descriptor URI could not be parsed");
636 return FALSE;
637 }
638
639 return gst_fd_sink_update_fd (sink, fd, error);
640 }
641
642 static void
gst_fd_sink_uri_handler_init(gpointer g_iface,gpointer iface_data)643 gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
644 {
645 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
646
647 iface->get_type = gst_fd_sink_uri_get_type;
648 iface->get_protocols = gst_fd_sink_uri_get_protocols;
649 iface->get_uri = gst_fd_sink_uri_get_uri;
650 iface->set_uri = gst_fd_sink_uri_set_uri;
651 }
652