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 /* Define our own iovec structure here, so that we can use it unconditionally
88 * in the code below and use almost the same code path for systems where
89 * writev() is supported and those were it's not supported */
90 #ifndef HAVE_SYS_UIO_H
98 /* completely arbitrary thresholds */
99 #define FDSINK_MAX_ALLOCA_SIZE (64 * 1024) /* 64k */
100 #define FDSINK_MAX_MALLOC_SIZE ( 8 * 1024 * 1024) /* 8M */
102 /* UIO_MAXIOV is documented in writev(2), but <sys/uio.h> only
103 * declares it on osx/ios if defined(KERNEL) */
105 #define UIO_MAXIOV 512
109 gst_writev (gint fd, const struct iovec *iov, gint iovcnt, gsize total_bytes)
113 #ifdef HAVE_SYS_UIO_H
114 if (iovcnt <= UIO_MAXIOV) {
116 written = writev (fd, iov, iovcnt);
117 } while (written < 0 && errno == EINTR);
123 /* We merge the memories here because technically write()/writev() is
124 * supposed to be atomic, which it's not if we do multiple separate
125 * write() calls. It's very doubtful anyone cares though in our use
126 * cases, and it's not clear how that can be reconciled with the
127 * possibility of short writes, so in any case we might want to
128 * simplify this later or just remove it. */
129 if (total_bytes <= FDSINK_MAX_MALLOC_SIZE) {
132 if (total_bytes <= FDSINK_MAX_ALLOCA_SIZE)
133 mem = g_alloca (total_bytes);
135 mem = g_malloc (total_bytes);
138 for (i = 0; i < iovcnt; ++i) {
139 memcpy (p, iov[i].iov_base, iov[i].iov_len);
144 written = write (fd, mem, total_bytes);
145 } while (written < 0 && errno == EINTR);
147 if (total_bytes > FDSINK_MAX_ALLOCA_SIZE)
153 for (i = 0; i < iovcnt; ++i) {
155 ret = write (fd, iov[i].iov_base, iov[i].iov_len);
156 } while (ret < 0 && errno == EINTR);
159 if (ret != iov[i].iov_len)
169 fill_vectors (struct iovec *vecs, GstMapInfo * maps, guint n, GstBuffer * buf)
175 g_assert (gst_buffer_n_memory (buf) == n);
177 for (i = 0; i < n; ++i) {
178 mem = gst_buffer_peek_memory (buf, i);
179 if (gst_memory_map (mem, &maps[i], GST_MAP_READ)) {
180 vecs[i].iov_base = maps[i].data;
181 vecs[i].iov_len = maps[i].size;
183 GST_WARNING ("Failed to map memory %p for reading", mem);
184 vecs[i].iov_base = (void *) "";
187 size += vecs[i].iov_len;
194 gst_writev_buffers (GstObject * sink, gint fd, GstPoll * fdset,
195 GstBuffer ** buffers, guint num_buffers, guint8 * mem_nums,
196 guint total_mem_num, guint64 * total_written, guint64 * cur_pos)
199 GstMapInfo *map_infos;
200 GstFlowReturn flow_ret;
204 GST_LOG_OBJECT (sink, "%u buffers, %u memories", num_buffers, total_mem_num);
206 vecs = g_newa (struct iovec, total_mem_num);
207 map_infos = g_newa (GstMapInfo, total_mem_num);
209 /* populate output vectors */
210 for (i = 0, j = 0; i < num_buffers; ++i) {
211 size += fill_vectors (&vecs[j], &map_infos[j], mem_nums[i], buffers[i]);
215 /* now write it all out! */
218 guint n_vecs = total_mem_num;
225 GST_DEBUG_OBJECT (sink, "going into select, have %" G_GSSIZE_FORMAT
226 " bytes to write", left);
227 ret = gst_poll_wait (fdset, GST_CLOCK_TIME_NONE);
228 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
239 ret = gst_writev (fd, vecs, n_vecs, left);
243 *total_written += ret;
251 if (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
252 /* do nothing, try again */
253 } else if (ret < 0) {
255 } else if (ret < left) {
256 /* skip vectors that have been written in full */
257 while (ret >= vecs[0].iov_len) {
258 ret -= vecs[0].iov_len;
259 left -= vecs[0].iov_len;
263 g_assert (n_vecs > 0);
264 /* skip partially written vector data */
266 vecs[0].iov_len -= ret;
267 vecs[0].iov_base = ((guint8 *) vecs[0].iov_base) + ret;
272 /* do short sleep on windows where we don't use gst_poll(),
273 * to avoid excessive busy looping */
282 flow_ret = GST_FLOW_OK;
286 for (i = 0; i < total_mem_num; ++i)
287 gst_memory_unmap (map_infos[i].memory, &map_infos[i]);
295 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
296 ("select on file descriptor: %s", g_strerror (errno)));
297 GST_DEBUG_OBJECT (sink, "Error during select: %s", g_strerror (errno));
298 flow_ret = GST_FLOW_ERROR;
303 GST_DEBUG_OBJECT (sink, "Select stopped");
304 flow_ret = GST_FLOW_FLUSHING;
312 GST_ELEMENT_ERROR (sink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
315 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
316 ("Error while writing to file descriptor %d: %s",
317 fd, g_strerror (errno)));
320 flow_ret = GST_FLOW_ERROR;