Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / gstpluginloader.c
index b4b3d60..6ac9b60 100644 (file)
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
+#else
+#define fsync(fd) _commit(fd)
+#include <io.h>
 #endif
+
 #include <errno.h>
 
 #include <gst/gstconfig.h>
@@ -57,12 +61,14 @@ static gboolean plugin_loader_free (GstPluginLoader * loader);
 static gboolean plugin_loader_load (GstPluginLoader * loader,
     const gchar * filename, off_t file_size, time_t file_mtime);
 
+/* functions used in GstRegistry scanning */
 const GstPluginLoaderFuncs _priv_gst_plugin_loader_funcs = {
   plugin_loader_new, plugin_loader_free, plugin_loader_load
 };
 
 typedef struct _PendingPluginEntry
 {
+  /* sequence number */
   guint32 tag;
   gchar *filename;
   off_t file_size;
@@ -88,6 +94,7 @@ struct _GstPluginLoader
   guint tx_buf_write;
   guint tx_buf_read;
 
+  /* next sequence number (for PendingPluginEntry) */
   guint32 next_tag;
 
   guint8 *rx_buf;
@@ -131,7 +138,7 @@ static gboolean plugin_loader_sync_with_child (GstPluginLoader * l);
 static GstPluginLoader *
 plugin_loader_new (GstRegistry * registry)
 {
-  GstPluginLoader *l = g_new0 (GstPluginLoader, 1);
+  GstPluginLoader *l = g_slice_new0 (GstPluginLoader);
 
   if (registry)
     l->registry = gst_object_ref (registry);
@@ -192,12 +199,12 @@ plugin_loader_free (GstPluginLoader * loader)
   while (cur) {
     PendingPluginEntry *entry = (PendingPluginEntry *) (cur->data);
     g_free (entry->filename);
-    g_free (entry);
+    g_slice_free (PendingPluginEntry, entry);
 
     cur = g_list_delete_link (cur, cur);
   }
 
-  g_free (loader);
+  g_slice_free (GstPluginLoader, loader);
 
   return got_plugin_details;
 }
@@ -216,7 +223,7 @@ plugin_loader_load (GstPluginLoader * loader, const gchar * filename,
   GST_LOG_OBJECT (loader->registry,
       "Sending file %s to child. tag %u", filename, loader->next_tag);
 
-  entry = g_new (PendingPluginEntry, 1);
+  entry = g_slice_new (PendingPluginEntry);
   entry->tag = loader->next_tag++;
   entry->filename = g_strdup (filename);
   entry->file_size = file_size;
@@ -263,6 +270,8 @@ restart:
       l->got_plugin_details = TRUE;
       /* Now remove this crashy plugin from the head of the list */
       l->pending_plugins = g_list_delete_link (cur, cur);
+      g_free (entry->filename);
+      g_slice_free (PendingPluginEntry, entry);
       if (l->pending_plugins == NULL)
         l->pending_plugins_tail = NULL;
       if (!gst_plugin_loader_spawn (l))
@@ -330,9 +339,9 @@ plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
 
   plugin->basename = g_path_get_basename (plugin->filename);
   plugin->desc.name = g_intern_string (plugin->basename);
-  plugin->desc.description = g_strdup_printf ("Plugin for blacklisted file");
-  plugin->desc.version = g_intern_string ("0.0.0");
-  plugin->desc.license = g_intern_string ("BLACKLIST");
+  plugin->desc.description = "Plugin for blacklisted file";
+  plugin->desc.version = "0.0.0";
+  plugin->desc.license = "BLACKLIST";
   plugin->desc.source = plugin->desc.license;
   plugin->desc.package = plugin->desc.license;
   plugin->desc.origin = plugin->desc.license;
@@ -344,9 +353,36 @@ plugin_loader_create_blacklist_plugin (GstPluginLoader * l,
 static gboolean
 gst_plugin_loader_try_helper (GstPluginLoader * loader, gchar * location)
 {
-  char *argv[] = { location, "-l", NULL };
+#ifdef __APPLE__
+#if defined(__x86_64__)
+  char *argv[] = { (char *) "/usr/bin/arch", (char *) "-x86_64",
+    location, (char *) "-l", NULL
+  };
+#elif defined(__i386__)
+  char *argv[] = { (char *) "/usr/bin/arch", (char *) "-i386",
+    location, (char *) "-l", NULL
+  };
+#elif defined(__ppc__)
+  char *argv[] = { (char *) "/usr/bin/arch", (char *) "-ppc",
+    location, (char *) "-l", NULL
+  };
+#elif defined(__ppc64__)
+  char *argv[] = { (char *) "/usr/bin/arch", (char *) "-ppc64",
+    location, (char *) "-l", NULL
+  };
+#endif
+#else /* ! __APPLE__ */
+  char *argv[] = { location, (char *) "-l", NULL };
+#endif
+
 
+#ifdef __APPLE__
+  GST_LOG ("Trying to spawn gst-plugin-scanner helper at %s with arch %s",
+      location, argv[1]);
+#else
   GST_LOG ("Trying to spawn gst-plugin-scanner helper at %s", location);
+#endif
+
   if (!g_spawn_async_with_pipes (NULL, argv, NULL,
           G_SPAWN_DO_NOT_REAP_CHILD /* | G_SPAWN_STDERR_TO_DEV_NULL */ ,
           NULL, NULL, &loader->child_pid, &loader->fd_w.fd, &loader->fd_r.fd,
@@ -372,29 +408,32 @@ gst_plugin_loader_try_helper (GstPluginLoader * loader, gchar * location)
 static gboolean
 gst_plugin_loader_spawn (GstPluginLoader * loader)
 {
+  const gchar *env;
   char *helper_bin;
-  gboolean res;
+  gboolean res = FALSE;
 
   if (loader->child_running)
     return TRUE;
 
-  /* Find the gst-plugin-scanner, first try installed then by env-var */
-  helper_bin = g_strdup (GST_PLUGIN_SCANNER_INSTALLED);
-  res = gst_plugin_loader_try_helper (loader, helper_bin);
-  g_free (helper_bin);
+  /* Find the gst-plugin-scanner: first try the env-var if it is set,
+   * otherwise use the installed version */
+  env = g_getenv ("GST_PLUGIN_SCANNER");
+
+  if (env != NULL && *env != '\0') {
+    GST_LOG ("Trying GST_PLUGIN_SCANNER env var: %s", env);
+    helper_bin = g_strdup (env);
+    res = gst_plugin_loader_try_helper (loader, helper_bin);
+    g_free (helper_bin);
+  }
 
   if (!res) {
-    /* Try the GST_PLUGIN_SCANNER env var */
-    const gchar *env = g_getenv ("GST_PLUGIN_SCANNER");
-    if (env != NULL) {
-      GST_LOG ("Installed plugin scanner failed. "
-          "Trying GST_PLUGIN_SCANNER env var: %s", env);
-      helper_bin = g_strdup (env);
-      res = gst_plugin_loader_try_helper (loader, helper_bin);
-      g_free (helper_bin);
-    } else {
-      GST_LOG ("Installed plugin scanner failed and GST_PLUGIN_SCANNER "
-          " env var not set. No gst-plugin-scanner available");
+    GST_LOG ("Trying installed plugin scanner");
+    helper_bin = g_strdup (GST_PLUGIN_SCANNER_INSTALLED);
+    res = gst_plugin_loader_try_helper (loader, helper_bin);
+    g_free (helper_bin);
+
+    if (!res) {
+      GST_INFO ("No gst-plugin-scanner available, or not working");
     }
   }
 
@@ -425,10 +464,9 @@ plugin_loader_cleanup_child (GstPluginLoader * l)
 }
 
 gboolean
-_gst_plugin_loader_client_run ()
+_gst_plugin_loader_client_run (void)
 {
   GstPluginLoader *l;
-  int dup_fd;
 
   l = plugin_loader_new (NULL);
   if (l == NULL)
@@ -438,25 +476,29 @@ _gst_plugin_loader_client_run ()
    * Dup those somewhere better so that plugins printing things
    * won't interfere with anything */
 #ifndef G_OS_WIN32
-  dup_fd = dup (0);             /* STDIN */
-  if (dup_fd == -1) {
-    GST_ERROR ("Failed to start. Could no dup STDIN, errno %d", errno);
-    return FALSE;
-  }
-  l->fd_r.fd = dup_fd;
-  close (0);
+  {
+    int dup_fd;
 
-  dup_fd = dup (1);             /* STDOUT */
-  if (dup_fd == -1) {
-    GST_ERROR ("Failed to start. Could no dup STDOUT, errno %d", errno);
-    return FALSE;
-  }
-  l->fd_w.fd = dup_fd;
-  close (1);
+    dup_fd = dup (0);           /* STDIN */
+    if (dup_fd == -1) {
+      GST_ERROR ("Failed to start. Could no dup STDIN, errno %d", errno);
+      return FALSE;
+    }
+    l->fd_r.fd = dup_fd;
+    close (0);
 
-  /* Dup stderr down to stdout so things that plugins print are visible,
-   * but don't care if it fails */
-  dup2 (2, 1);
+    dup_fd = dup (1);           /* STDOUT */
+    if (dup_fd == -1) {
+      GST_ERROR ("Failed to start. Could no dup STDOUT, errno %d", errno);
+      return FALSE;
+    }
+    l->fd_w.fd = dup_fd;
+    close (1);
+
+    /* Dup stderr down to stdout so things that plugins print are visible,
+     * but don't care if it fails */
+    dup2 (2, 1);
+  }
 #else
   /* FIXME: Use DuplicateHandle and friends on win32 */
   l->fd_w.fd = 1;               /* STDOUT */
@@ -603,7 +645,7 @@ do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
   GstPlugin *newplugin;
   GList *chunks = NULL;
 
-  GST_DEBUG ("Plugin scanner loading file %s. tag %u\n", filename, tag);
+  GST_DEBUG ("Plugin scanner loading file %s. tag %u", filename, tag);
 
 #if 0                           /* Test code - crash based on filename */
   if (strstr (filename, "coreelements") == NULL) {
@@ -634,9 +676,7 @@ do_plugin_load (GstPluginLoader * l, const gchar * filename, guint tag)
         GstRegistryChunk *cur = walk->data;
         put_chunk (l, cur, &offset);
 
-        if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
-          g_free (cur->data);
-        g_free (cur);
+        _priv_gst_registry_chunk_free (cur);
       }
 
       g_list_free (chunks);
@@ -667,9 +707,7 @@ fail:
     for (walk = chunks; walk; walk = g_list_next (walk)) {
       GstRegistryChunk *cur = walk->data;
 
-      if (!(cur->flags & GST_REGISTRY_CHUNK_FLAG_CONST))
-        g_free (cur->data);
-      g_free (cur);
+      _priv_gst_registry_chunk_free (cur);
     }
 
     g_list_free (chunks);
@@ -714,11 +752,10 @@ handle_rx_packet (GstPluginLoader * l,
     case PACKET_EXIT:
       gst_poll_fd_ctl_read (l->fdset, &l->fd_r, FALSE);
       if (l->is_child) {
-        /* Respond, then we keep looping until the parent closes the fd */
+        /* Respond */
         put_packet (l, PACKET_EXIT, 0, NULL, 0);
-      } else {
-        l->rx_done = TRUE;      /* All done reading from child */
       }
+      l->rx_done = TRUE;
       return TRUE;
     case PACKET_LOAD_PLUGIN:{
       if (!l->is_child)
@@ -754,7 +791,7 @@ handle_rx_packet (GstPluginLoader * l,
         } else {
           cur = g_list_delete_link (cur, cur);
           g_free (e->filename);
-          g_free (e);
+          g_slice_free (PendingPluginEntry, e);
         }
       }
 
@@ -788,7 +825,7 @@ handle_rx_packet (GstPluginLoader * l,
 
       if (entry != NULL) {
         g_free (entry->filename);
-        g_free (entry);
+        g_slice_free (PendingPluginEntry, entry);
       }
 
       /* Remove the plugin entry we just loaded */