dataurisrc: Fix crash when semicolon is aprt of data
[platform/upstream/gstreamer.git] / plugins / elements / gstelements_private.c
1 /* GStreamer
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>
6  *
7  * gstelements_private.c: Shared code for core elements
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #ifdef HAVE_SYS_UIO_H
33 #include <sys/uio.h>
34 #endif
35 #include <sys/types.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <string.h>
39 #include "gst/gst.h"
40 #include "gstelements_private.h"
41
42 #ifdef G_OS_WIN32
43 #  include <io.h>               /* lseek, open, close, read */
44 #  undef lseek
45 #  define lseek _lseeki64
46 #  undef off_t
47 #  define off_t guint64
48 #  define WIN32_LEAN_AND_MEAN   /* prevents from including too many things */
49 #  include <windows.h>
50 #  undef WIN32_LEAN_AND_MEAN
51 #  ifndef EWOULDBLOCK
52 #  define EWOULDBLOCK EAGAIN
53 #  endif
54 #endif /* G_OS_WIN32 */
55
56 #define BUFFER_FLAG_SHIFT 4
57
58 G_STATIC_ASSERT ((1 << BUFFER_FLAG_SHIFT) == GST_MINI_OBJECT_FLAG_LAST);
59
60 /* Returns a newly allocated string describing the flags on this buffer */
61 gchar *
62 gst_buffer_get_flags_string (GstBuffer * buffer)
63 {
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"
67       "FIXME";
68   static const guint8 flag_idx[] = { 0, 1, 2, 3, 4, 9, 21, 29, 36, 46, 53,
69     60, 64, 74, 85, 96
70   };
71   int i, max_bytes;
72   char *flag_str, *end;
73
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);
77
78   end = flag_str;
79   end[0] = '\0';
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]);
83       end += strlen (end);
84       end[0] = ' ';
85       end[1] = '\0';
86       end++;
87     }
88   }
89
90   return flag_str;
91 }
92
93 /* Returns a newly-allocated string describing the metas on this buffer, or NULL */
94 gchar *
95 gst_buffer_get_meta_string (GstBuffer * buffer)
96 {
97   gpointer state = NULL;
98   GstMeta *meta;
99   GString *s = NULL;
100
101   while ((meta = gst_buffer_iterate_meta (buffer, &state))) {
102     const gchar *desc = g_type_name (meta->info->type);
103
104     if (s == NULL)
105       s = g_string_new (NULL);
106     else
107       g_string_append (s, ", ");
108
109     g_string_append (s, desc);
110   }
111
112   return (s != NULL) ? g_string_free (s, FALSE) : NULL;
113 }
114
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
119 struct iovec
120 {
121   gpointer iov_base;
122   gsize iov_len;
123 };
124 #endif
125
126 /* completely arbitrary thresholds */
127 #define FDSINK_MAX_ALLOCA_SIZE (64 * 1024)      /* 64k */
128 #define FDSINK_MAX_MALLOC_SIZE ( 8 * 1024 * 1024)       /*  8M */
129
130 /* UIO_MAXIOV is documented in writev(2), but <sys/uio.h> only
131  * declares it on osx/ios if defined(KERNEL) */
132 #ifndef UIO_MAXIOV
133 #define UIO_MAXIOV 512
134 #endif
135
136 static gssize
137 gst_writev (gint fd, const struct iovec *iov, gint iovcnt, gsize total_bytes)
138 {
139   gssize written;
140
141 #ifdef HAVE_SYS_UIO_H
142   if (iovcnt <= UIO_MAXIOV) {
143     do {
144       written = writev (fd, iov, iovcnt);
145     } while (written < 0 && errno == EINTR);
146   } else
147 #endif
148   {
149     gint i;
150
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) {
158       gchar *mem, *p;
159
160       if (total_bytes <= FDSINK_MAX_ALLOCA_SIZE)
161         mem = g_alloca (total_bytes);
162       else
163         mem = g_malloc (total_bytes);
164
165       p = mem;
166       for (i = 0; i < iovcnt; ++i) {
167         memcpy (p, iov[i].iov_base, iov[i].iov_len);
168         p += iov[i].iov_len;
169       }
170
171       do {
172         written = write (fd, mem, total_bytes);
173       } while (written < 0 && errno == EINTR);
174
175       if (total_bytes > FDSINK_MAX_ALLOCA_SIZE)
176         g_free (mem);
177     } else {
178       gssize ret;
179
180       written = 0;
181       for (i = 0; i < iovcnt; ++i) {
182         do {
183           ret = write (fd, iov[i].iov_base, iov[i].iov_len);
184         } while (ret < 0 && errno == EINTR);
185         if (ret > 0)
186           written += ret;
187         if (ret != iov[i].iov_len)
188           break;
189       }
190     }
191   }
192
193   return written;
194 }
195
196 static gsize
197 fill_vectors (struct iovec *vecs, GstMapInfo * maps, guint n, GstBuffer * buf)
198 {
199   GstMemory *mem;
200   gsize size = 0;
201   guint i;
202
203   g_assert (gst_buffer_n_memory (buf) == n);
204
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;
210     } else {
211       GST_WARNING ("Failed to map memory %p for reading", mem);
212       vecs[i].iov_base = (void *) "";
213       vecs[i].iov_len = 0;
214     }
215     size += vecs[i].iov_len;
216   }
217
218   return size;
219 }
220
221 GstFlowReturn
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,
226     gboolean * flushing)
227 {
228   struct iovec *vecs;
229   GstMapInfo *map_infos;
230   GstFlowReturn flow_ret;
231   gsize size = 0;
232   guint i, j;
233   gint64 start_time = 0;
234
235   max_transient_error_timeout *= 1000;
236   if (max_transient_error_timeout)
237     start_time = g_get_monotonic_time ();
238
239   GST_LOG_OBJECT (sink, "%u buffers, %u memories", num_buffers, total_mem_num);
240
241   vecs = g_newa (struct iovec, total_mem_num);
242   map_infos = g_newa (GstMapInfo, total_mem_num);
243
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]);
247     j += mem_nums[i];
248   }
249
250   /* now write it all out! */
251   {
252     gssize ret, left;
253     guint n_vecs = total_mem_num;
254
255     left = size;
256
257     if (skip) {
258       ret = skip;
259       errno = 0;
260       goto skip_first;
261     }
262
263     do {
264       if (flushing != NULL && g_atomic_int_get (flushing)) {
265         GST_DEBUG_OBJECT (sink, "Flushing, exiting loop");
266         flow_ret = GST_FLOW_FLUSHING;
267         goto out;
268       }
269 #ifndef HAVE_WIN32
270       if (fdset != NULL) {
271         do {
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));
276
277         if (ret == -1) {
278           if (errno == EBUSY)
279             goto stopped;
280           else
281             goto select_error;
282         }
283       }
284 #endif
285
286       ret = gst_writev (fd, vecs, n_vecs, left);
287
288       if (ret > 0) {
289         if (bytes_written)
290           *bytes_written += ret;
291       }
292
293     skip_first:
294
295       if (ret == left)
296         break;
297
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
304          * for 10ms.
305          *
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
309          * systems.
310          *
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.
314          */
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);
318           goto write_error;
319         }
320         GST_DEBUG_OBJECT (sink, "got EACCES, retry after 10ms sleep");
321         g_assert (current_position != -1);
322         g_usleep (10000);
323
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)
327          */
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");
332           goto write_error;
333         }
334       } else if (ret < 0) {
335         goto write_error;
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;
343           ++vecs;
344           --n_vecs;
345         }
346         g_assert (n_vecs > 0);
347         /* skip partially written vector data */
348         if (ret > 0) {
349           vecs[0].iov_len -= ret;
350           vecs[0].iov_base = ((guint8 *) vecs[0].iov_base) + ret;
351           left -= ret;
352         }
353       }
354 #ifdef HAVE_WIN32
355       /* do short sleep on windows where we don't use gst_poll(),
356        * to avoid excessive busy looping */
357       if (fdset != NULL)
358         g_usleep (1000);
359 #endif
360
361     }
362     while (left > 0);
363   }
364
365   flow_ret = GST_FLOW_OK;
366
367 out:
368
369   for (i = 0; i < total_mem_num; ++i)
370     gst_memory_unmap (map_infos[i].memory, &map_infos[i]);
371
372   return flow_ret;
373
374 /* ERRORS */
375 #ifndef HAVE_WIN32
376 select_error:
377   {
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;
382     goto out;
383   }
384 stopped:
385   {
386     GST_DEBUG_OBJECT (sink, "Select stopped");
387     flow_ret = GST_FLOW_FLUSHING;
388     goto out;
389   }
390 #endif
391 write_error:
392   {
393     switch (errno) {
394       case ENOSPC:
395         GST_ELEMENT_ERROR (sink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
396         break;
397       default:{
398         GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
399             ("Error while writing to file descriptor %d: %s",
400                 fd, g_strerror (errno)));
401       }
402     }
403     flow_ret = GST_FLOW_ERROR;
404     goto out;
405   }
406 }