gst_writev: respect IOV_MAX for the writev iovec array #439
[platform/upstream/gstreamer.git] / plugins / elements / gstfilesrc.c
index b68c279..76ab12c 100644 (file)
  */
 /**
  * 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 <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>
 
@@ -75,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
@@ -112,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
@@ -186,8 +194,7 @@ gst_file_src_class_init (GstFileSrcClass * klass)
       "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);
@@ -227,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;
 
@@ -241,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;
@@ -254,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");
-  /* FIXME 0.11: notify "uri" property once there is one */
+  /* FIXME 2.0: notify "uri" property once there is one */
 
   return TRUE;
 
@@ -263,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;
   }
@@ -280,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);
@@ -330,7 +342,7 @@ gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
 
   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);
@@ -340,7 +352,8 @@ gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
     src->read_position = offset;
   }
 
-  gst_buffer_map (buf, &info, GST_MAP_WRITE);
+  if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
+    goto buffer_write_fail;
   data = info.data;
 
   bytes_read = 0;
@@ -399,6 +412,11 @@ eos:
     gst_buffer_resize (buf, 0, 0);
     return GST_FLOW_EOS;
   }
+buffer_write_fail:
+  {
+    GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL), ("Can't write to buffer"));
+    return GST_FLOW_ERROR;
+  }
 }
 
 static gboolean
@@ -480,9 +498,16 @@ gst_file_src_start (GstBaseSrc * basesrc)
           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
@@ -533,6 +558,13 @@ was_socket:
         (_("File \"%s\" is a socket."), src->filename), (NULL));
     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:
@@ -592,7 +624,7 @@ gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
     /* 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;
   }
 
@@ -614,14 +646,14 @@ gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
 #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)