2 * Copyright (C) 2011 David Schleef <ds@schleef.org>
3 * Copyright (C) 2011 Tim-Philipp Müller <tim.muller@collabora.co.uk>
4 * Copyright (C) 2014 Tim-Philipp Müller <tim@centricular.com>
5 * Copyright (C) 2014 Vincent Penquerc'h <vincent@collabora.co.uk>
7 * gstelements_private.c: Shared code for core elements
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
35 #include <sys/types.h>
40 #include "gstelements_private.h"
43 # include <io.h> /* lseek, open, close, read */
45 # define lseek _lseeki64
47 # define off_t guint64
48 # define WIN32_LEAN_AND_MEAN /* prevents from including too many things */
50 # undef WIN32_LEAN_AND_MEAN
52 # define EWOULDBLOCK EAGAIN
54 #endif /* G_OS_WIN32 */
56 #define BUFFER_FLAG_SHIFT 4
58 G_STATIC_ASSERT ((1 << BUFFER_FLAG_SHIFT) == GST_MINI_OBJECT_FLAG_LAST);
60 /* Returns a newly allocated string describing the flags on this buffer */
62 gst_buffer_get_flags_string (GstBuffer * buffer)
64 static const char flag_strings[] =
65 "\000\000\000\000live\000decode-only\000discont\000resync\000corrupted\000"
66 "marker\000header\000gap\000droppable\000delta-unit\000tag-memory\000"
68 static const guint8 flag_idx[] = { 0, 1, 2, 3, 4, 9, 21, 29, 36, 46, 53,
74 /* max size is all flag strings plus a space or terminator after each one */
75 max_bytes = sizeof (flag_strings);
76 flag_str = g_malloc (max_bytes);
80 for (i = BUFFER_FLAG_SHIFT; i < G_N_ELEMENTS (flag_idx); i++) {
81 if (GST_MINI_OBJECT_CAST (buffer)->flags & (1 << i)) {
82 strcpy (end, flag_strings + flag_idx[i]);
93 /* Returns a newly-allocated string describing the metas on this buffer, or NULL */
95 gst_buffer_get_meta_string (GstBuffer * buffer)
97 gpointer state = NULL;
101 while ((meta = gst_buffer_iterate_meta (buffer, &state))) {
102 const gchar *desc = g_type_name (meta->info->type);
105 s = g_string_new (NULL);
107 g_string_append (s, ", ");
109 g_string_append (s, desc);
112 return (s != NULL) ? g_string_free (s, FALSE) : NULL;
115 /* Define our own iovec structure here, so that we can use it unconditionally
116 * in the code below and use almost the same code path for systems where
117 * writev() is supported and those were it's not supported */
118 #ifndef HAVE_SYS_UIO_H
126 /* completely arbitrary thresholds */
127 #define FDSINK_MAX_ALLOCA_SIZE (64 * 1024) /* 64k */
128 #define FDSINK_MAX_MALLOC_SIZE ( 8 * 1024 * 1024) /* 8M */
130 /* UIO_MAXIOV is documented in writev(2), but <sys/uio.h> only
131 * declares it on osx/ios if defined(KERNEL) */
133 #define UIO_MAXIOV 512
137 gst_writev (gint fd, const struct iovec *iov, gint iovcnt, gsize total_bytes)
141 #ifdef HAVE_SYS_UIO_H
142 if (iovcnt <= UIO_MAXIOV) {
144 written = writev (fd, iov, iovcnt);
145 } while (written < 0 && errno == EINTR);
151 /* We merge the memories here because technically write()/writev() is
152 * supposed to be atomic, which it's not if we do multiple separate
153 * write() calls. It's very doubtful anyone cares though in our use
154 * cases, and it's not clear how that can be reconciled with the
155 * possibility of short writes, so in any case we might want to
156 * simplify this later or just remove it. */
157 if (total_bytes <= FDSINK_MAX_MALLOC_SIZE) {
160 if (total_bytes <= FDSINK_MAX_ALLOCA_SIZE)
161 mem = g_alloca (total_bytes);
163 mem = g_malloc (total_bytes);
166 for (i = 0; i < iovcnt; ++i) {
167 memcpy (p, iov[i].iov_base, iov[i].iov_len);
172 written = write (fd, mem, total_bytes);
173 } while (written < 0 && errno == EINTR);
175 if (total_bytes > FDSINK_MAX_ALLOCA_SIZE)
181 for (i = 0; i < iovcnt; ++i) {
183 ret = write (fd, iov[i].iov_base, iov[i].iov_len);
184 } while (ret < 0 && errno == EINTR);
187 if (ret != iov[i].iov_len)
197 fill_vectors (struct iovec *vecs, GstMapInfo * maps, guint n, GstBuffer * buf)
203 g_assert (gst_buffer_n_memory (buf) == n);
205 for (i = 0; i < n; ++i) {
206 mem = gst_buffer_peek_memory (buf, i);
207 if (gst_memory_map (mem, &maps[i], GST_MAP_READ)) {
208 vecs[i].iov_base = maps[i].data;
209 vecs[i].iov_len = maps[i].size;
211 GST_WARNING ("Failed to map memory %p for reading", mem);
212 vecs[i].iov_base = (void *) "";
215 size += vecs[i].iov_len;
222 gst_writev_buffers (GstObject * sink, gint fd, GstPoll * fdset,
223 GstBuffer ** buffers, guint num_buffers, guint8 * mem_nums,
224 guint total_mem_num, guint64 * bytes_written, guint64 skip,
225 gint max_transient_error_timeout, guint64 current_position,
229 GstMapInfo *map_infos;
230 GstFlowReturn flow_ret;
233 gint64 start_time = 0;
235 max_transient_error_timeout *= 1000;
236 if (max_transient_error_timeout)
237 start_time = g_get_monotonic_time ();
239 GST_LOG_OBJECT (sink, "%u buffers, %u memories", num_buffers, total_mem_num);
241 vecs = g_newa (struct iovec, total_mem_num);
242 map_infos = g_newa (GstMapInfo, total_mem_num);
244 /* populate output vectors */
245 for (i = 0, j = 0; i < num_buffers; ++i) {
246 size += fill_vectors (&vecs[j], &map_infos[j], mem_nums[i], buffers[i]);
250 /* now write it all out! */
253 guint n_vecs = total_mem_num;
264 if (flushing != NULL && g_atomic_int_get (flushing)) {
265 GST_DEBUG_OBJECT (sink, "Flushing, exiting loop");
266 flow_ret = GST_FLOW_FLUSHING;
272 GST_DEBUG_OBJECT (sink, "going into select, have %" G_GSSIZE_FORMAT
273 " bytes to write", left);
274 ret = gst_poll_wait (fdset, GST_CLOCK_TIME_NONE);
275 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
286 ret = gst_writev (fd, vecs, n_vecs, left);
290 *bytes_written += ret;
298 if (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
299 /* do nothing, try again */
300 if (max_transient_error_timeout)
301 start_time = g_get_monotonic_time ();
302 } else if (ret < 0 && errno == EACCES && max_transient_error_timeout > 0) {
303 /* seek back to where we started writing and try again after sleeping
306 * Some network file systems report EACCES spuriously, presumably
307 * because at the same time another client is reading the file.
308 * It happens at least on Linux and macOS on SMB/CIFS and NFS file
311 * Note that NFS does not check access permissions during open()
312 * but only on write()/read() according to open(2), so we would
313 * loop here in case of NFS.
315 if (g_get_monotonic_time () > start_time + max_transient_error_timeout) {
316 GST_ERROR_OBJECT (sink, "Got EACCES for more than %dms, failing",
317 max_transient_error_timeout);
320 GST_DEBUG_OBJECT (sink, "got EACCES, retry after 10ms sleep");
321 g_assert (current_position != -1);
324 /* Seek back to the current position, sometimes a partial write
325 * happened and we have no idea how much and if what was written
326 * is actually correct (it sometimes isn't)
328 ret = lseek (fd, current_position + *bytes_written, SEEK_SET);
329 if (ret < 0 || ret != current_position + *bytes_written) {
330 GST_ERROR_OBJECT (sink,
331 "failed to seek back to current write position");
334 } else if (ret < 0) {
336 } else { /* if (ret < left) */
337 if (max_transient_error_timeout)
338 start_time = g_get_monotonic_time ();
339 /* skip vectors that have been written in full */
340 while (ret >= vecs[0].iov_len) {
341 ret -= vecs[0].iov_len;
342 left -= vecs[0].iov_len;
346 g_assert (n_vecs > 0);
347 /* skip partially written vector data */
349 vecs[0].iov_len -= ret;
350 vecs[0].iov_base = ((guint8 *) vecs[0].iov_base) + ret;
355 /* do short sleep on windows where we don't use gst_poll(),
356 * to avoid excessive busy looping */
365 flow_ret = GST_FLOW_OK;
369 for (i = 0; i < total_mem_num; ++i)
370 gst_memory_unmap (map_infos[i].memory, &map_infos[i]);
378 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
379 ("select on file descriptor: %s", g_strerror (errno)));
380 GST_DEBUG_OBJECT (sink, "Error during select: %s", g_strerror (errno));
381 flow_ret = GST_FLOW_ERROR;
386 GST_DEBUG_OBJECT (sink, "Select stopped");
387 flow_ret = GST_FLOW_FLUSHING;
395 GST_ELEMENT_ERROR (sink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
398 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
399 ("Error while writing to file descriptor %d: %s",
400 fd, g_strerror (errno)));
403 flow_ret = GST_FLOW_ERROR;