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.
39 #include "gstelements_private.h"
42 # define WIN32_LEAN_AND_MEAN /* prevents from including too many things */
44 # undef WIN32_LEAN_AND_MEAN
46 # define EWOULDBLOCK EAGAIN
48 #endif /* G_OS_WIN32 */
50 #define BUFFER_FLAG_SHIFT 4
52 G_STATIC_ASSERT ((1 << BUFFER_FLAG_SHIFT) == GST_MINI_OBJECT_FLAG_LAST);
54 /* Returns a newly allocated string describing the flags on this buffer */
56 gst_buffer_get_flags_string (GstBuffer * buffer)
58 static const char flag_strings[] =
59 "\000\000\000\000live\000decode-only\000discont\000resync\000corrupted\000"
60 "marker\000header\000gap\000droppable\000delta-unit\000tag-memory\000"
62 static const guint8 flag_idx[] = { 0, 1, 2, 3, 4, 9, 21, 29, 36, 46, 53,
68 /* max size is all flag strings plus a space or terminator after each one */
69 max_bytes = sizeof (flag_strings);
70 flag_str = g_malloc (max_bytes);
74 for (i = BUFFER_FLAG_SHIFT; i < G_N_ELEMENTS (flag_idx); i++) {
75 if (GST_MINI_OBJECT_CAST (buffer)->flags & (1 << i)) {
76 strcpy (end, flag_strings + flag_idx[i]);
87 /* Returns a newly-allocated string describing the metas on this buffer, or NULL */
89 gst_buffer_get_meta_string (GstBuffer * buffer)
91 gpointer state = NULL;
95 while ((meta = gst_buffer_iterate_meta (buffer, &state))) {
96 const gchar *desc = g_type_name (meta->info->type);
99 s = g_string_new (NULL);
101 g_string_append (s, ", ");
103 g_string_append (s, desc);
106 return (s != NULL) ? g_string_free (s, FALSE) : NULL;
109 /* Define our own iovec structure here, so that we can use it unconditionally
110 * in the code below and use almost the same code path for systems where
111 * writev() is supported and those were it's not supported */
112 #ifndef HAVE_SYS_UIO_H
120 /* completely arbitrary thresholds */
121 #define FDSINK_MAX_ALLOCA_SIZE (64 * 1024) /* 64k */
122 #define FDSINK_MAX_MALLOC_SIZE ( 8 * 1024 * 1024) /* 8M */
124 /* UIO_MAXIOV is documented in writev(2), but <sys/uio.h> only
125 * declares it on osx/ios if defined(KERNEL) */
127 #define UIO_MAXIOV 512
131 gst_writev (gint fd, const struct iovec *iov, gint iovcnt, gsize total_bytes)
135 #ifdef HAVE_SYS_UIO_H
136 if (iovcnt <= UIO_MAXIOV) {
138 written = writev (fd, iov, iovcnt);
139 } while (written < 0 && errno == EINTR);
145 /* We merge the memories here because technically write()/writev() is
146 * supposed to be atomic, which it's not if we do multiple separate
147 * write() calls. It's very doubtful anyone cares though in our use
148 * cases, and it's not clear how that can be reconciled with the
149 * possibility of short writes, so in any case we might want to
150 * simplify this later or just remove it. */
151 if (total_bytes <= FDSINK_MAX_MALLOC_SIZE) {
154 if (total_bytes <= FDSINK_MAX_ALLOCA_SIZE)
155 mem = g_alloca (total_bytes);
157 mem = g_malloc (total_bytes);
160 for (i = 0; i < iovcnt; ++i) {
161 memcpy (p, iov[i].iov_base, iov[i].iov_len);
166 written = write (fd, mem, total_bytes);
167 } while (written < 0 && errno == EINTR);
169 if (total_bytes > FDSINK_MAX_ALLOCA_SIZE)
175 for (i = 0; i < iovcnt; ++i) {
177 ret = write (fd, iov[i].iov_base, iov[i].iov_len);
178 } while (ret < 0 && errno == EINTR);
181 if (ret != iov[i].iov_len)
191 fill_vectors (struct iovec *vecs, GstMapInfo * maps, guint n, GstBuffer * buf)
197 g_assert (gst_buffer_n_memory (buf) == n);
199 for (i = 0; i < n; ++i) {
200 mem = gst_buffer_peek_memory (buf, i);
201 if (gst_memory_map (mem, &maps[i], GST_MAP_READ)) {
202 vecs[i].iov_base = maps[i].data;
203 vecs[i].iov_len = maps[i].size;
205 GST_WARNING ("Failed to map memory %p for reading", mem);
206 vecs[i].iov_base = (void *) "";
209 size += vecs[i].iov_len;
216 gst_writev_buffers (GstObject * sink, gint fd, GstPoll * fdset,
217 GstBuffer ** buffers, guint num_buffers, guint8 * mem_nums,
218 guint total_mem_num, guint64 * bytes_written, guint64 skip)
221 GstMapInfo *map_infos;
222 GstFlowReturn flow_ret;
226 GST_LOG_OBJECT (sink, "%u buffers, %u memories", num_buffers, total_mem_num);
228 vecs = g_newa (struct iovec, total_mem_num);
229 map_infos = g_newa (GstMapInfo, total_mem_num);
231 /* populate output vectors */
232 for (i = 0, j = 0; i < num_buffers; ++i) {
233 size += fill_vectors (&vecs[j], &map_infos[j], mem_nums[i], buffers[i]);
237 /* now write it all out! */
240 guint n_vecs = total_mem_num;
254 GST_DEBUG_OBJECT (sink, "going into select, have %" G_GSSIZE_FORMAT
255 " bytes to write", left);
256 ret = gst_poll_wait (fdset, GST_CLOCK_TIME_NONE);
257 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
268 ret = gst_writev (fd, vecs, n_vecs, left);
272 *bytes_written += ret;
280 if (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
281 /* do nothing, try again */
282 } else if (ret < 0) {
284 } else if (ret < left) {
285 /* skip vectors that have been written in full */
286 while (ret >= vecs[0].iov_len) {
287 ret -= vecs[0].iov_len;
288 left -= vecs[0].iov_len;
292 g_assert (n_vecs > 0);
293 /* skip partially written vector data */
295 vecs[0].iov_len -= ret;
296 vecs[0].iov_base = ((guint8 *) vecs[0].iov_base) + ret;
301 /* do short sleep on windows where we don't use gst_poll(),
302 * to avoid excessive busy looping */
311 flow_ret = GST_FLOW_OK;
315 for (i = 0; i < total_mem_num; ++i)
316 gst_memory_unmap (map_infos[i].memory, &map_infos[i]);
324 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
325 ("select on file descriptor: %s", g_strerror (errno)));
326 GST_DEBUG_OBJECT (sink, "Error during select: %s", g_strerror (errno));
327 flow_ret = GST_FLOW_ERROR;
332 GST_DEBUG_OBJECT (sink, "Select stopped");
333 flow_ret = GST_FLOW_FLUSHING;
341 GST_ELEMENT_ERROR (sink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
344 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
345 ("Error while writing to file descriptor %d: %s",
346 fd, g_strerror (errno)));
349 flow_ret = GST_FLOW_ERROR;