tee: Check for the removed pad flag also in the slow pushing path
[platform/upstream/gstreamer.git] / plugins / elements / gstfdsink.c
index d7b9e88..7641cde 100644 (file)
@@ -22,6 +22,7 @@
 
 /**
  * SECTION:element-fdsink
+ * @title: fdsink
  * @see_also: #GstFdSrc
  *
  * Write data to a unix file descriptor.
@@ -58,6 +59,7 @@
 #include <string.h>
 
 #include "gstfdsink.h"
+#include "gstelements_private.h"
 
 #ifdef G_OS_WIN32
 #include <io.h>                 /* lseek, open, close, read */
 #define off_t guint64
 #endif
 
+#if defined(__BIONIC__)         /* Android */
+#if defined(__ANDROID_API__) && __ANDROID_API__ >= 21
+#undef fstat
+#define fstat fstat64
+#endif
+#endif
+
 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
     GST_PAD_SINK,
     GST_PAD_ALWAYS,
@@ -107,6 +116,8 @@ static void gst_fd_sink_dispose (GObject * obj);
 static gboolean gst_fd_sink_query (GstBaseSink * bsink, GstQuery * query);
 static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
     GstBuffer * buffer);
+static GstFlowReturn gst_fd_sink_render_list (GstBaseSink * bsink,
+    GstBufferList * buffer_list);
 static gboolean gst_fd_sink_start (GstBaseSink * basesink);
 static gboolean gst_fd_sink_stop (GstBaseSink * basesink);
 static gboolean gst_fd_sink_unlock (GstBaseSink * basesink);
@@ -134,10 +145,10 @@ gst_fd_sink_class_init (GstFdSinkClass * klass)
       "Filedescriptor Sink",
       "Sink/File",
       "Write data to a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
-  gst_element_class_add_pad_template (gstelement_class,
-      gst_static_pad_template_get (&sinktemplate));
+  gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
 
   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_fd_sink_render);
+  gstbasesink_class->render_list = GST_DEBUG_FUNCPTR (gst_fd_sink_render_list);
   gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_fd_sink_start);
   gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_fd_sink_stop);
   gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock);
@@ -228,107 +239,88 @@ gst_fd_sink_query (GstBaseSink * bsink, GstQuery * query)
 }
 
 static GstFlowReturn
-gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer)
+gst_fd_sink_render_buffers (GstFdSink * sink, GstBuffer ** buffers,
+    guint num_buffers, guint8 * mem_nums, guint total_mems)
 {
-  GstFdSink *fdsink;
-  GstMapInfo info;
-  guint8 *ptr;
-  gsize left;
-  gint written;
+  GstFlowReturn ret;
+  guint64 skip = 0;
 
-#ifndef HAVE_WIN32
-  gint retval;
-#endif
+  for (;;) {
+    guint64 bytes_written = 0;
 
-  fdsink = GST_FD_SINK (sink);
+    ret = gst_writev_buffers (GST_OBJECT_CAST (sink), sink->fd, sink->fdset,
+        buffers, num_buffers, mem_nums, total_mems, &bytes_written, skip);
 
-  g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR);
+    sink->bytes_written += bytes_written;
+    sink->current_pos += bytes_written;
+    skip += bytes_written;
 
-  gst_buffer_map (buffer, &info, GST_MAP_READ);
+    if (!sink->unlock)
+      break;
 
-  ptr = info.data;
-  left = info.size;
+    ret = gst_base_sink_wait_preroll (GST_BASE_SINK (sink));
+    if (ret != GST_FLOW_OK)
+      return ret;
+  }
 
-again:
-#ifndef HAVE_WIN32
-  do {
-    GST_DEBUG_OBJECT (fdsink, "going into select, have %" G_GSIZE_FORMAT
-        " bytes to write", info.size);
-    retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE);
-  } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
+  return ret;
+}
 
-  if (retval == -1) {
-    if (errno == EBUSY)
-      goto stopped;
-    else
-      goto select_error;
+static GstFlowReturn
+gst_fd_sink_render_list (GstBaseSink * bsink, GstBufferList * buffer_list)
+{
+  GstFlowReturn flow;
+  GstBuffer **buffers;
+  GstFdSink *sink;
+  guint8 *mem_nums;
+  guint total_mems;
+  guint i, num_buffers;
+
+  sink = GST_FD_SINK_CAST (bsink);
+
+  num_buffers = gst_buffer_list_length (buffer_list);
+  if (num_buffers == 0)
+    goto no_data;
+
+  /* extract buffers from list and count memories */
+  buffers = g_newa (GstBuffer *, num_buffers);
+  mem_nums = g_newa (guint8, num_buffers);
+  for (i = 0, total_mems = 0; i < num_buffers; ++i) {
+    buffers[i] = gst_buffer_list_get (buffer_list, i);
+    mem_nums[i] = gst_buffer_n_memory (buffers[i]);
+    total_mems += mem_nums[i];
   }
-#endif
 
-  GST_DEBUG_OBJECT (fdsink, "writing %" G_GSIZE_FORMAT " bytes to"
-      " file descriptor %d", info.size, fdsink->fd);
+  flow =
+      gst_fd_sink_render_buffers (sink, buffers, num_buffers, mem_nums,
+      total_mems);
 
-  written = write (fdsink->fd, ptr, left);
+  return flow;
 
-  /* check for errors */
-  if (G_UNLIKELY (written < 0)) {
-    /* try to write again on non-fatal errors */
-    if (errno == EAGAIN || errno == EINTR)
-      goto again;
-
-    /* else go to our error handler */
-    goto write_error;
+no_data:
+  {
+    GST_LOG_OBJECT (sink, "empty buffer list");
+    return GST_FLOW_OK;
   }
+}
 
-  /* all is fine when we get here */
-  left -= written;
-  ptr += written;
-  fdsink->bytes_written += written;
-  fdsink->current_pos += written;
-
-  GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %" G_GSIZE_FORMAT " left", written,
-      left);
-
-  /* short write, select and try to write the remainder */
-  if (G_UNLIKELY (left > 0))
-    goto again;
+static GstFlowReturn
+gst_fd_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
+{
+  GstFlowReturn flow;
+  GstFdSink *sink;
+  guint8 n_mem;
 
-  gst_buffer_unmap (buffer, &info);
+  sink = GST_FD_SINK_CAST (bsink);
 
-  return GST_FLOW_OK;
+  n_mem = gst_buffer_n_memory (buffer);
 
-#ifndef HAVE_WIN32
-select_error:
-  {
-    GST_ELEMENT_ERROR (fdsink, RESOURCE, READ, (NULL),
-        ("select on file descriptor: %s.", g_strerror (errno)));
-    GST_DEBUG_OBJECT (fdsink, "Error during select");
-    gst_buffer_unmap (buffer, &info);
-    return GST_FLOW_ERROR;
-  }
-stopped:
-  {
-    GST_DEBUG_OBJECT (fdsink, "Select stopped");
-    gst_buffer_unmap (buffer, &info);
-    return GST_FLOW_FLUSHING;
-  }
-#endif
+  if (n_mem > 0)
+    flow = gst_fd_sink_render_buffers (sink, &buffer, 1, &n_mem, n_mem);
+  else
+    flow = GST_FLOW_OK;
 
-write_error:
-  {
-    switch (errno) {
-      case ENOSPC:
-        GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
-        break;
-      default:{
-        GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
-            ("Error while writing to file descriptor %d: %s",
-                fdsink->fd, g_strerror (errno)));
-      }
-    }
-    gst_buffer_unmap (buffer, &info);
-    return GST_FLOW_ERROR;
-  }
+  return flow;
 }
 
 static gboolean
@@ -429,6 +421,7 @@ gst_fd_sink_unlock (GstBaseSink * basesink)
 
   GST_LOG_OBJECT (fdsink, "Flushing");
   GST_OBJECT_LOCK (fdsink);
+  fdsink->unlock = TRUE;
   gst_poll_set_flushing (fdsink->fdset, TRUE);
   GST_OBJECT_UNLOCK (fdsink);
 
@@ -442,6 +435,7 @@ gst_fd_sink_unlock_stop (GstBaseSink * basesink)
 
   GST_LOG_OBJECT (fdsink, "No longer flushing");
   GST_OBJECT_LOCK (fdsink);
+  fdsink->unlock = FALSE;
   gst_poll_set_flushing (fdsink->fdset, FALSE);
   GST_OBJECT_UNLOCK (fdsink);