typefind: Only push a CAPS event downstream if the sinkpad is not in PULL mode
[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 <errno.h>
36 #include <string.h>
37 #include <string.h>
38 #include "gst/gst.h"
39 #include "gstelements_private.h"
40
41 #ifdef G_OS_WIN32
42 #  define WIN32_LEAN_AND_MEAN   /* prevents from including too many things */
43 #  include <windows.h>
44 #  undef WIN32_LEAN_AND_MEAN
45 #  ifndef EWOULDBLOCK
46 #  define EWOULDBLOCK EAGAIN
47 #  endif
48 #endif /* G_OS_WIN32 */
49
50 #define BUFFER_FLAG_SHIFT 4
51
52 G_STATIC_ASSERT ((1 << BUFFER_FLAG_SHIFT) == GST_MINI_OBJECT_FLAG_LAST);
53
54 /* Returns a newly allocated string describing the flags on this buffer */
55 char *
56 gst_buffer_get_flags_string (GstBuffer * buffer)
57 {
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"
61       "FIXME";
62   static const guint8 flag_idx[] = { 0, 1, 2, 3, 4, 9, 21, 29, 36, 46, 53,
63     60, 64, 74, 85, 96
64   };
65   int i, max_bytes;
66   char *flag_str, *end;
67
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);
71
72   end = flag_str;
73   end[0] = '\0';
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]);
77       end += strlen (end);
78       end[0] = ' ';
79       end[1] = '\0';
80       end++;
81     }
82   }
83
84   return flag_str;
85 }
86
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
91 struct iovec
92 {
93   gpointer iov_base;
94   gsize iov_len;
95 };
96 #endif
97
98 /* completely arbitrary thresholds */
99 #define FDSINK_MAX_ALLOCA_SIZE (64 * 1024)      /* 64k */
100 #define FDSINK_MAX_MALLOC_SIZE ( 8 * 1024 * 1024)       /*  8M */
101
102 /* UIO_MAXIOV is documented in writev(2), but <sys/uio.h> only
103  * declares it on osx/ios if defined(KERNEL) */
104 #ifndef UIO_MAXIOV
105 #define UIO_MAXIOV 512
106 #endif
107
108 static gssize
109 gst_writev (gint fd, const struct iovec *iov, gint iovcnt, gsize total_bytes)
110 {
111   gssize written;
112
113 #ifdef HAVE_SYS_UIO_H
114   if (iovcnt <= UIO_MAXIOV) {
115     do {
116       written = writev (fd, iov, iovcnt);
117     } while (written < 0 && errno == EINTR);
118   } else
119 #endif
120   {
121     gint i;
122
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) {
130       gchar *mem, *p;
131
132       if (total_bytes <= FDSINK_MAX_ALLOCA_SIZE)
133         mem = g_alloca (total_bytes);
134       else
135         mem = g_malloc (total_bytes);
136
137       p = mem;
138       for (i = 0; i < iovcnt; ++i) {
139         memcpy (p, iov[i].iov_base, iov[i].iov_len);
140         p += iov[i].iov_len;
141       }
142
143       do {
144         written = write (fd, mem, total_bytes);
145       } while (written < 0 && errno == EINTR);
146
147       if (total_bytes > FDSINK_MAX_ALLOCA_SIZE)
148         g_free (mem);
149     } else {
150       gssize ret;
151
152       written = 0;
153       for (i = 0; i < iovcnt; ++i) {
154         do {
155           ret = write (fd, iov[i].iov_base, iov[i].iov_len);
156         } while (ret < 0 && errno == EINTR);
157         if (ret > 0)
158           written += ret;
159         if (ret != iov[i].iov_len)
160           break;
161       }
162     }
163   }
164
165   return written;
166 }
167
168 static gsize
169 fill_vectors (struct iovec *vecs, GstMapInfo * maps, guint n, GstBuffer * buf)
170 {
171   GstMemory *mem;
172   gsize size = 0;
173   guint i;
174
175   g_assert (gst_buffer_n_memory (buf) == n);
176
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;
182     } else {
183       GST_WARNING ("Failed to map memory %p for reading", mem);
184       vecs[i].iov_base = (void *) "";
185       vecs[i].iov_len = 0;
186     }
187     size += vecs[i].iov_len;
188   }
189
190   return size;
191 }
192
193 GstFlowReturn
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)
197 {
198   struct iovec *vecs;
199   GstMapInfo *map_infos;
200   GstFlowReturn flow_ret;
201   gsize size = 0;
202   guint i, j;
203
204   GST_LOG_OBJECT (sink, "%u buffers, %u memories", num_buffers, total_mem_num);
205
206   vecs = g_newa (struct iovec, total_mem_num);
207   map_infos = g_newa (GstMapInfo, total_mem_num);
208
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]);
212     j += mem_nums[i];
213   }
214
215   /* now write it all out! */
216   {
217     gssize ret, left;
218     guint n_vecs = total_mem_num;
219
220     left = size;
221     do {
222 #ifndef HAVE_WIN32
223       if (fdset != NULL) {
224         do {
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));
229
230         if (ret == -1) {
231           if (errno == EBUSY)
232             goto stopped;
233           else
234             goto select_error;
235         }
236       }
237 #endif
238
239       ret = gst_writev (fd, vecs, n_vecs, left);
240
241       if (ret > 0) {
242         if (total_written)
243           *total_written += ret;
244         if (cur_pos)
245           *cur_pos += ret;
246       }
247
248       if (ret == left)
249         break;
250
251       if (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
252         /* do nothing, try again */
253       } else if (ret < 0) {
254         goto write_error;
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;
260           ++vecs;
261           --n_vecs;
262         }
263         g_assert (n_vecs > 0);
264         /* skip partially written vector data */
265         if (ret > 0) {
266           vecs[0].iov_len -= ret;
267           vecs[0].iov_base = ((guint8 *) vecs[0].iov_base) + ret;
268           left -= ret;
269         }
270       }
271 #ifdef HAVE_WIN32
272       /* do short sleep on windows where we don't use gst_poll(),
273        * to avoid excessive busy looping */
274       if (fdset != NULL)
275         g_usleep (1000);
276 #endif
277
278     }
279     while (left > 0);
280   }
281
282   flow_ret = GST_FLOW_OK;
283
284 out:
285
286   for (i = 0; i < total_mem_num; ++i)
287     gst_memory_unmap (map_infos[i].memory, &map_infos[i]);
288
289   return flow_ret;
290
291 /* ERRORS */
292 #ifndef HAVE_WIN32
293 select_error:
294   {
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;
299     goto out;
300   }
301 stopped:
302   {
303     GST_DEBUG_OBJECT (sink, "Select stopped");
304     flow_ret = GST_FLOW_FLUSHING;
305     goto out;
306   }
307 #endif
308 write_error:
309   {
310     switch (errno) {
311       case ENOSPC:
312         GST_ELEMENT_ERROR (sink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
313         break;
314       default:{
315         GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
316             ("Error while writing to file descriptor %d: %s",
317                 fd, g_strerror (errno)));
318       }
319     }
320     flow_ret = GST_FLOW_ERROR;
321     goto out;
322   }
323 }