gst_writev: respect IOV_MAX for the writev iovec array #439
[platform/upstream/gstreamer.git] / plugins / elements / gstfilesrc.c
index 86900b0..76ab12c 100644 (file)
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 /**
  * SECTION:element-filesrc
+ * @title: filesrc
  * @see_also: #GstFileSrc
  *
  * Read data from a file in the local file system.
  *
- * <refsect2>
- * <title>Example launch line</title>
+ * ## Example launch line
  * |[
- * gst-launch filesrc location=song.ogg ! decodebin2 ! autoaudiosink
- * ]| Play a song.ogg from local dir.
- * </refsect2>
+ * gst-launch-1.0 filesrc location=song.ogg ! decodebin ! audioconvert ! audioresample ! autoaudiosink
+ * ]| Play song.ogg audio file which must be in the current working directory.
+ *
  */
 
 #ifdef HAVE_CONFIG_H
 
 #include <stdio.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #ifdef G_OS_WIN32
 #include <io.h>                 /* lseek, open, close, read */
 /* On win32, stat* default to 32 bit; we need the 64-bit
  * variants, so explicitly define it that way. */
+#undef stat
 #define stat __stat64
+#undef fstat
 #define fstat _fstat64
 #undef lseek
 #define lseek _lseeki64
  * _stat*, since we're explicitly overriding that */
 #undef _INC_STAT_INL
 #endif
-#include <sys/stat.h>
 #include <fcntl.h>
 
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
 #endif
 
+#ifdef __BIONIC__               /* Android */
+#if defined(__ANDROID_API__) && __ANDROID_API__ >= 21
+#undef fstat
+#define fstat fstat64
+#endif
+#endif
+
 #include <errno.h>
 #include <string.h>
 
@@ -73,7 +82,6 @@ static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
     GST_PAD_ALWAYS,
     GST_STATIC_CAPS_ANY);
 
-/* FIXME we should be using glib for this */
 #ifndef S_ISREG
 #define S_ISREG(mode) ((mode)&_S_IFREG)
 #endif
@@ -110,6 +118,8 @@ gst_open (const gchar * filename, int flags, int mode)
 
   errno = save_errno;
   return retval;
+#elif defined (__BIONIC__)
+  return open (filename, flags | O_LARGEFILE, mode);
 #else
   return open (filename, flags, mode);
 #endif
@@ -130,8 +140,7 @@ enum
 enum
 {
   PROP_0,
-  PROP_LOCATION,
-  PROP_FD
+  PROP_LOCATION
 };
 
 static void gst_file_src_finalize (GObject * object);
@@ -148,7 +157,6 @@ static gboolean gst_file_src_is_seekable (GstBaseSrc * src);
 static gboolean gst_file_src_get_size (GstBaseSrc * src, guint64 * size);
 static GstFlowReturn gst_file_src_fill (GstBaseSrc * src, guint64 offset,
     guint length, GstBuffer * buf);
-static gboolean gst_file_src_query (GstBaseSrc * src, GstQuery * query);
 
 static void gst_file_src_uri_handler_init (gpointer g_iface,
     gpointer iface_data);
@@ -173,10 +181,6 @@ gst_file_src_class_init (GstFileSrcClass * klass)
   gobject_class->set_property = gst_file_src_set_property;
   gobject_class->get_property = gst_file_src_get_property;
 
-  g_object_class_install_property (gobject_class, PROP_FD,
-      g_param_spec_int ("fd", "File-descriptor",
-          "File-descriptor for the file being mmap()d", 0, G_MAXINT, 0,
-          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (gobject_class, PROP_LOCATION,
       g_param_spec_string ("location", "File Location",
           "Location of the file to read", NULL,
@@ -185,20 +189,18 @@ gst_file_src_class_init (GstFileSrcClass * klass)
 
   gobject_class->finalize = gst_file_src_finalize;
 
-  gst_element_class_set_details_simple (gstelement_class,
+  gst_element_class_set_static_metadata (gstelement_class,
       "File Source",
       "Source/File",
       "Read from arbitrary point in a file",
       "Erik Walthinsen <omega@cse.ogi.edu>");
-  gst_element_class_add_pad_template (gstelement_class,
-      gst_static_pad_template_get (&srctemplate));
+  gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
 
   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_file_src_start);
   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_file_src_stop);
   gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_file_src_is_seekable);
   gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_file_src_get_size);
   gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_file_src_fill);
-  gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_file_src_query);
 
   if (sizeof (off_t) < 8) {
     GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
@@ -214,6 +216,8 @@ gst_file_src_init (GstFileSrc * src)
   src->uri = NULL;
 
   src->is_regular = FALSE;
+
+  gst_base_src_set_blocksize (GST_BASE_SRC (src), DEFAULT_BLOCKSIZE);
 }
 
 static void
@@ -230,7 +234,8 @@ gst_file_src_finalize (GObject * object)
 }
 
 static gboolean
-gst_file_src_set_location (GstFileSrc * src, const gchar * location)
+gst_file_src_set_location (GstFileSrc * src, const gchar * location,
+    GError ** err)
 {
   GstState state;
 
@@ -244,7 +249,7 @@ gst_file_src_set_location (GstFileSrc * src, const gchar * location)
   g_free (src->filename);
   g_free (src->uri);
 
-  /* clear the filename if we get a NULL (is that possible?) */
+  /* clear the filename if we get a NULL */
   if (location == NULL) {
     src->filename = NULL;
     src->uri = NULL;
@@ -257,7 +262,7 @@ gst_file_src_set_location (GstFileSrc * src, const gchar * location)
     GST_INFO ("uri      : %s", src->uri);
   }
   g_object_notify (G_OBJECT (src), "location");
-  gst_uri_handler_new_uri (GST_URI_HANDLER (src), src->uri);
+  /* FIXME 2.0: notify "uri" property once there is one */
 
   return TRUE;
 
@@ -266,6 +271,10 @@ wrong_state:
   {
     g_warning ("Changing the `location' property on filesrc when a file is "
         "open is not supported.");
+    if (err)
+      g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
+          "Changing the `location' property on filesrc when a file is "
+          "open is not supported.");
     GST_OBJECT_UNLOCK (src);
     return FALSE;
   }
@@ -283,7 +292,7 @@ gst_file_src_set_property (GObject * object, guint prop_id,
 
   switch (prop_id) {
     case PROP_LOCATION:
-      gst_file_src_set_location (src, g_value_get_string (value));
+      gst_file_src_set_location (src, g_value_get_string (value), NULL);
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -305,9 +314,6 @@ gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
     case PROP_LOCATION:
       g_value_set_string (value, src->filename);
       break;
-    case PROP_FD:
-      g_value_set_int (value, src->fd);
-      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -329,12 +335,14 @@ gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
     GstBuffer * buf)
 {
   GstFileSrc *src;
+  guint to_read, bytes_read;
   int ret;
+  GstMapInfo info;
   guint8 *data;
 
   src = GST_FILE_SRC_CAST (basesrc);
 
-  if (G_UNLIKELY (src->read_position != offset)) {
+  if (G_UNLIKELY (offset != -1 && src->read_position != offset)) {
     off_t res;
 
     res = lseek (src->fd, offset, SEEK_SET);
@@ -344,31 +352,43 @@ gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
     src->read_position = offset;
   }
 
-  data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
-
-  GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
-      length, offset);
-
-  ret = read (src->fd, data, length);
-  if (G_UNLIKELY (ret < 0))
-    goto could_not_read;
+  if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
+    goto buffer_write_fail;
+  data = info.data;
+
+  bytes_read = 0;
+  to_read = length;
+  while (to_read > 0) {
+    GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
+        to_read, offset + bytes_read);
+    errno = 0;
+    ret = read (src->fd, data + bytes_read, to_read);
+    if (G_UNLIKELY (ret < 0)) {
+      if (errno == EAGAIN || errno == EINTR)
+        continue;
+      goto could_not_read;
+    }
 
-  /* seekable regular files should have given us what we expected */
-  if (G_UNLIKELY ((guint) ret < length && src->seekable))
-    goto unexpected_eos;
+    /* files should eos if they read 0 and more was requested */
+    if (G_UNLIKELY (ret == 0)) {
+      /* .. but first we should return any remaining data */
+      if (bytes_read > 0)
+        break;
+      goto eos;
+    }
 
-  /* other files should eos if they read 0 and more was requested */
-  if (G_UNLIKELY (ret == 0))
-    goto eos;
+    to_read -= ret;
+    bytes_read += ret;
 
-  length = ret;
+    src->read_position += ret;
+  }
 
-  gst_buffer_unmap (buf, data, length);
+  gst_buffer_unmap (buf, &info);
+  if (bytes_read != length)
+    gst_buffer_resize (buf, 0, bytes_read);
 
   GST_BUFFER_OFFSET (buf) = offset;
-  GST_BUFFER_OFFSET_END (buf) = offset + length;
-
-  src->read_position += length;
+  GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
 
   return GST_FLOW_OK;
 
@@ -381,44 +401,22 @@ seek_failed:
 could_not_read:
   {
     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
-    gst_buffer_unmap (buf, data, 0);
-    return GST_FLOW_ERROR;
-  }
-unexpected_eos:
-  {
-    GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
-        ("unexpected end of file."));
-    gst_buffer_unmap (buf, data, 0);
+    gst_buffer_unmap (buf, &info);
+    gst_buffer_resize (buf, 0, 0);
     return GST_FLOW_ERROR;
   }
 eos:
   {
-    GST_DEBUG ("non-regular file hits EOS");
-    gst_buffer_unmap (buf, data, 0);
+    GST_DEBUG ("EOS");
+    gst_buffer_unmap (buf, &info);
+    gst_buffer_resize (buf, 0, 0);
     return GST_FLOW_EOS;
   }
-}
-
-static gboolean
-gst_file_src_query (GstBaseSrc * basesrc, GstQuery * query)
-{
-  gboolean ret = FALSE;
-  GstFileSrc *src = GST_FILE_SRC (basesrc);
-
-  switch (GST_QUERY_TYPE (query)) {
-    case GST_QUERY_URI:
-      gst_query_set_uri (query, src->uri);
-      ret = TRUE;
-      break;
-    default:
-      ret = FALSE;
-      break;
+buffer_write_fail:
+  {
+    GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL), ("Can't write to buffer"));
+    return GST_FLOW_ERROR;
   }
-
-  if (!ret)
-    ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
-
-  return ret;
 }
 
 static gboolean
@@ -457,7 +455,7 @@ could_not_stat:
   }
 }
 
-/* open the file and mmap it, necessary to go to READY state */
+/* open the file, necessary to go to READY state */
 static gboolean
 gst_file_src_start (GstBaseSrc * basesrc)
 {
@@ -496,13 +494,20 @@ gst_file_src_start (GstBaseSrc * basesrc)
     off_t res = lseek (src->fd, 0, SEEK_END);
 
     if (res < 0) {
-      GST_LOG_OBJECT (src, "disabling seeking, not in mmap mode and lseek "
-          "failed: %s", g_strerror (errno));
+      GST_LOG_OBJECT (src, "disabling seeking, lseek failed: %s",
+          g_strerror (errno));
       src->seekable = FALSE;
     } else {
+      res = lseek (src->fd, 0, SEEK_SET);
+
+      if (res < 0) {
+        /* We really don't like not being able to go back to 0 */
+        src->seekable = FALSE;
+        goto lseek_wonky;
+      }
+
       src->seekable = TRUE;
     }
-    lseek (src->fd, 0, SEEK_SET);
   }
 
   /* We can only really do seeking on regular files - for other file types, we
@@ -518,7 +523,7 @@ no_filename:
   {
     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
         (_("No file name specified for reading.")), (NULL));
-    return FALSE;
+    goto error_exit;
   }
 open_failed:
   {
@@ -533,29 +538,37 @@ open_failed:
             GST_ERROR_SYSTEM);
         break;
     }
-    return FALSE;
+    goto error_exit;
   }
 no_stat:
   {
     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
         (_("Could not get info on \"%s\"."), src->filename), (NULL));
-    close (src->fd);
-    return FALSE;
+    goto error_close;
   }
 was_directory:
   {
     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
         (_("\"%s\" is a directory."), src->filename), (NULL));
-    close (src->fd);
-    return FALSE;
+    goto error_close;
   }
 was_socket:
   {
     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
         (_("File \"%s\" is a socket."), src->filename), (NULL));
-    close (src->fd);
-    return FALSE;
+    goto error_close;
   }
+lseek_wonky:
+  {
+    GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
+        ("Could not seek back to zero after seek test in file \"%s\"",
+            src->filename));
+    goto error_close;
+  }
+error_close:
+  close (src->fd);
+error_exit:
+  return FALSE;
 }
 
 /* unmap and close the file */
@@ -582,67 +595,65 @@ gst_file_src_uri_get_type (GType type)
   return GST_URI_SRC;
 }
 
-static gchar **
+static const gchar *const *
 gst_file_src_uri_get_protocols (GType type)
 {
-  static gchar *protocols[] = { (char *) "file", NULL };
+  static const gchar *protocols[] = { "file", NULL };
 
   return protocols;
 }
 
-static const gchar *
+static gchar *
 gst_file_src_uri_get_uri (GstURIHandler * handler)
 {
   GstFileSrc *src = GST_FILE_SRC (handler);
 
-  return src->uri;
+  /* FIXME: make thread-safe */
+  return g_strdup (src->uri);
 }
 
 static gboolean
-gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
+gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
+    GError ** err)
 {
   gchar *location, *hostname = NULL;
   gboolean ret = FALSE;
   GstFileSrc *src = GST_FILE_SRC (handler);
-  GError *error = NULL;
 
   if (strcmp (uri, "file://") == 0) {
     /* Special case for "file://" as this is used by some applications
      *  to test with gst_element_make_from_uri if there's an element
      *  that supports the URI protocol. */
-    gst_file_src_set_location (src, NULL);
+    gst_file_src_set_location (src, NULL, NULL);
     return TRUE;
   }
 
-  location = g_filename_from_uri (uri, &hostname, &error);
+  location = g_filename_from_uri (uri, &hostname, err);
 
-  if (!location || error) {
-    if (error) {
-      GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
-          error->message);
-      g_error_free (error);
-    } else {
-      GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc", uri);
-    }
+  if (!location || (err != NULL && *err != NULL)) {
+    GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
+        (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
     goto beach;
   }
 
   if ((hostname) && (strcmp (hostname, "localhost"))) {
     /* Only 'localhost' is permitted */
     GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
+    g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
+        "File URI with invalid hostname '%s'", hostname);
     goto beach;
   }
 #ifdef G_OS_WIN32
   /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
    * correctly on windows, it leaves them with an extra backslash
-   * at the start if they're of the mozilla-style file://///host/path/file 
+   * at the start if they're of the mozilla-style file://///host/path/file
    * form. Correct this.
    */
   if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
-    g_memmove (location, location + 1, strlen (location + 1) + 1);
+    memmove (location, location + 1, strlen (location + 1) + 1);
 #endif
 
-  ret = gst_file_src_set_location (src, location);
+  ret = gst_file_src_set_location (src, location, err);
 
 beach:
   if (location)