gstplugin.c 22.7 KB
Newer Older
1 2 3 4 5
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
 * gstplugin.c: Plugin subsystem for loading elements, types, and libs
Erik Walthinsen's avatar
Erik Walthinsen committed
6 7 8 9 10
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
11
 *
Erik Walthinsen's avatar
Erik Walthinsen committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * 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.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>

28
#include "gst_private.h"
29
#include "gstplugin.h"
30
#include "gstversion.h"
31
#include "gstregistry.h"
32
#include "config.h"
Erik Walthinsen's avatar
Erik Walthinsen committed
33

34 35 36
static GModule *main_module;

GList *_gst_plugin_static = NULL;
Erik Walthinsen's avatar
Erik Walthinsen committed
37 38

/* global list of plugins and its sequence number */
39 40
GList *_gst_plugins = NULL;
gint _gst_plugins_seqno = 0;
Erik Walthinsen's avatar
Erik Walthinsen committed
41
/* list of paths to check for plugins */
42
GList *_gst_plugin_paths = NULL;
Erik Walthinsen's avatar
Erik Walthinsen committed
43

44 45
GList *_gst_libraries = NULL;
gint _gst_libraries_seqno = 0;
46

Erik Walthinsen's avatar
Erik Walthinsen committed
47
/* whether or not to spew library load issues */
48
gboolean _gst_plugin_spew = FALSE;
Erik Walthinsen's avatar
Erik Walthinsen committed
49

50
/* whether or not to warn if registry needs rebuild (gst-register sets
51 52
 * this to false.) */
gboolean _gst_warn_old_registry = TRUE;
53 54
/* whether or not the main app will be writing to the registry */
gboolean _gst_init_registry_write = FALSE;
55

56 57 58 59 60 61 62
#ifndef GST_DISABLE_REGISTRY
static gboolean 	plugin_times_older_than		(time_t regtime);
static time_t 		get_time			(const char * path);
#endif
static void 		gst_plugin_register_statics 	(GModule *module);
static GstPlugin* 	gst_plugin_register_func 	(GstPluginDesc *desc, GstPlugin *plugin, 
							 GModule *module);
63 64
void
_gst_plugin_initialize (void)
65
{
66
  GList *gst_plugin_default_paths = NULL;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
67
  struct stat stat_buf;
68
#ifndef GST_DISABLE_REGISTRY
69 70
  GstRegistryRead *gst_reg;
  gchar *gst_registry;
71
  xmlDocPtr doc = NULL;
72 73
#endif

74 75
  main_module =  g_module_open (NULL, G_MODULE_BIND_LAZY);
  gst_plugin_register_statics (main_module);
Erik Walthinsen's avatar
Erik Walthinsen committed
76

77
#ifdef PLUGINS_USE_BUILDDIR
Erik Walthinsen's avatar
Erik Walthinsen committed
78
  /* location libgstelements.so */
79 80 81 82 83 84 85 86 87 88 89
  gst_plugin_default_paths = g_list_prepend (gst_plugin_default_paths,
                                             PLUGINS_BUILDDIR "/gst/elements");
  gst_plugin_default_paths = g_list_prepend (gst_plugin_default_paths,
                                             PLUGINS_BUILDDIR "/gst/types");
  gst_plugin_default_paths = g_list_prepend (gst_plugin_default_paths,
                                             PLUGINS_BUILDDIR "/gst/autoplug");
  gst_plugin_default_paths = g_list_prepend (gst_plugin_default_paths,
                                             PLUGINS_BUILDDIR "/gst/schedulers");
#else
  /* add the main (installed) library path */
  gst_plugin_default_paths = g_list_prepend (gst_plugin_default_paths, PLUGINS_DIR);
90
#endif /* PLUGINS_USE_BUILDDIR */
91

92
#ifndef GST_DISABLE_REGISTRY
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  /* FIXME:
   * we want to check both the global and the local registry here
   * at first, we check if there is a local one, and if there is only use
   * that one.
   * Later, we would like to read the global one first, then have each
   * plugin also in the local one override the global one.
   */
  
  gst_reg = gst_registry_read_get ();
  if (gst_reg->local_reg)
    gst_registry = gst_reg->local_reg;
  else
    gst_registry = gst_reg->global_reg;
  
  if (_gst_init_registry_write)
  {
    /* delete it before writing */
    unlink (gst_registry);
  }
  if (stat (gst_registry, &stat_buf) == 0)
    doc = xmlParseFile (gst_registry);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
114 115
  else
    doc = NULL;
116

117 118 119 120
  if (!doc || 
      !doc->xmlRootNode ||
      doc->xmlRootNode->name == 0 ||
      strcmp (doc->xmlRootNode->name, "GST-PluginRegistry") ||
121
      !plugin_times_older_than(get_time(gst_registry))) 
122
  {
123 124
    if (_gst_warn_old_registry &&
	!plugin_times_older_than(get_time(gst_registry))) 
125
	g_warning ("gstplugin: registry needs rebuild: run gst-register\n");
126
    _gst_plugin_paths = g_list_concat (_gst_plugin_paths, gst_plugin_default_paths);
127 128 129 130
#ifdef PLUGINS_USE_BUILDDIR
    /* we prepend the lib dir first for performance reasons */
    _gst_plugin_paths = g_list_prepend (_gst_plugin_paths, PLUGINS_BUILDDIR "/libs/gst");
#endif
131
    gst_plugin_load_all ();
132
    /* gst_plugin_unload_all (); */
133 134
    return;
  }
135
  /* this will pull in the plugin paths for us */
Richard Boulton's avatar
Richard Boulton committed
136
  gst_plugin_load_thyself (doc->xmlRootNode);
Erik Walthinsen's avatar
Erik Walthinsen committed
137 138

  xmlFreeDoc (doc);
139 140
#else
  _gst_plugin_paths = g_list_concat (_gst_plugin_paths, gst_plugin_default_paths);
141
#endif /* GST_DISABLE_REGISTRY */
Erik Walthinsen's avatar
Erik Walthinsen committed
142 143
}

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
void
_gst_plugin_register_static (GstPluginDesc *desc)
{
  _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
}

static void
gst_plugin_register_statics (GModule *module)
{
  GList *walk = _gst_plugin_static;

  while (walk) {
    GstPluginDesc *desc = (GstPluginDesc *) walk->data;
    GstPlugin *plugin;

    plugin = g_new0 (GstPlugin, 1);
    plugin->filename = NULL;
    plugin->module = NULL;
    plugin = gst_plugin_register_func (desc, plugin, module);

    if (plugin) {
      _gst_plugins = g_list_prepend (_gst_plugins, plugin);
      _gst_plugins_seqno++;
      plugin->module = module;
    }
    
    walk = g_list_next (walk);
  }
}

174 175 176 177 178 179
/**
 * gst_plugin_add_path:
 * @path: the directory to add to the search path
 *
 * Add a directory to the path searched for plugins.
 */
180 181 182 183 184 185
void
gst_plugin_add_path (const gchar *path)
{
  _gst_plugin_paths = g_list_prepend (_gst_plugin_paths,g_strdup(path));
}

186
#ifndef GST_DISABLE_REGISTRY
187 188 189 190 191 192 193 194 195 196 197 198 199 200
static time_t
get_time(const char * path)
{
  struct stat statbuf;
  if (stat(path, &statbuf)) return 0;
  if (statbuf.st_mtime > statbuf.st_ctime) return statbuf.st_mtime;
  return statbuf.st_ctime;
}

static gboolean
plugin_times_older_than_recurse(gchar *path, time_t regtime)
{
  DIR *dir;
  struct dirent *dirent;
Erik Walthinsen's avatar
Erik Walthinsen committed
201
  gchar *pluginname;
202 203 204 205 206 207 208 209 210

  time_t pathtime = get_time(path);

  if (pathtime > regtime) {
    GST_INFO (GST_CAT_PLUGIN_LOADING,
	       "time for %s was %ld; more recent than registry time of %ld\n",
	       path, (long)pathtime, (long)regtime);
    return FALSE;
  }
211

212 213 214 215 216
  dir = opendir(path);
  if (dir) {
    while ((dirent = readdir(dir))) {
      /* don't want to recurse in place or backwards */
      if (strcmp(dirent->d_name,".") && strcmp(dirent->d_name,"..")) {
Erik Walthinsen's avatar
Erik Walthinsen committed
217 218 219 220 221 222 223
	pluginname = g_strjoin("/",path,dirent->d_name,NULL);
	if (!plugin_times_older_than_recurse(pluginname , regtime)) {
          g_free (pluginname);
          closedir(dir);
          return FALSE;
        }
        g_free (pluginname);
224 225 226 227 228 229 230 231 232 233
      }
    }
    closedir(dir);
  }
  return TRUE;
}

static gboolean
plugin_times_older_than(time_t regtime)
{
234 235 236 237
  /* return true iff regtime is more recent than the times of all the files
   * in the plugin dirs.
   */

238 239 240 241 242 243 244 245 246 247 248 249
  GList *path;
  path = _gst_plugin_paths;
  while (path != NULL) {
    GST_DEBUG (GST_CAT_PLUGIN_LOADING,
	       "comparing plugin times from %s with %ld\n",
	       (gchar *)path->data, (long) regtime);
    if(!plugin_times_older_than_recurse(path->data, regtime))
	return FALSE;
    path = g_list_next(path);
  }
  return TRUE;
}
250
#endif
251

252 253
static gboolean
gst_plugin_load_recurse (gchar *directory, gchar *name)
254
{
Erik Walthinsen's avatar
Erik Walthinsen committed
255 256 257
  DIR *dir;
  struct dirent *dirent;
  gboolean loaded = FALSE;
Erik Walthinsen's avatar
Erik Walthinsen committed
258
  gchar *dirname;
Erik Walthinsen's avatar
Erik Walthinsen committed
259

260
  /* g_print("recursive load of '%s' in '%s'\n", name, directory); */
Erik Walthinsen's avatar
Erik Walthinsen committed
261 262
  dir = opendir(directory);
  if (dir) {
263
    while ((dirent = readdir(dir))) {
Erik Walthinsen's avatar
Erik Walthinsen committed
264 265
      /* don't want to recurse in place or backwards */
      if (strcmp(dirent->d_name,".") && strcmp(dirent->d_name,"..")) {
Erik Walthinsen's avatar
Erik Walthinsen committed
266 267 268 269 270 271 272
        dirname = g_strjoin("/",directory,dirent->d_name,NULL);
        loaded = gst_plugin_load_recurse(dirname,name);
        g_free(dirname);
	if (loaded && name) {
          closedir(dir);
          return TRUE;
        }
Erik Walthinsen's avatar
Erik Walthinsen committed
273 274 275 276 277 278 279
      }
    }
    closedir(dir);
  } else {
    if (strstr(directory,".so")) {
      gchar *temp;
      if (name) {
280
        if ((temp = strstr(directory,name)) &&
Erik Walthinsen's avatar
Erik Walthinsen committed
281
            (!strcmp(temp,name))) {
282
          loaded = gst_plugin_load_absolute(directory);
Erik Walthinsen's avatar
Erik Walthinsen committed
283 284 285
        }
      } else if ((temp = strstr(directory,".so")) &&
                 (!strcmp(temp,".so"))) {
286
        loaded = gst_plugin_load_absolute(directory);
Erik Walthinsen's avatar
Erik Walthinsen committed
287 288 289 290 291 292 293 294 295
      }
    }
  }
  return loaded;
}

/**
 * gst_plugin_load_all:
 *
296
 * Load all plugins in the path (in the global GList* _gst_plugin_paths).
Erik Walthinsen's avatar
Erik Walthinsen committed
297
 */
298
void
299
gst_plugin_load_all (void)
300
{
Erik Walthinsen's avatar
Erik Walthinsen committed
301 302 303
  GList *path;

  path = _gst_plugin_paths;
304
  if (path == NULL) { g_warning ("gst_plugin_load_all: path is NULL !"); }
Erik Walthinsen's avatar
Erik Walthinsen committed
305
  while (path != NULL) {
306
    GST_DEBUG (GST_CAT_PLUGIN_LOADING,"loading plugins from %s",(gchar *)path->data);
Erik Walthinsen's avatar
Erik Walthinsen committed
307 308 309
    gst_plugin_load_recurse(path->data,NULL);
    path = g_list_next(path);
  }
310 311 312
  GST_INFO (GST_CAT_PLUGIN_LOADING,"loaded %d plugins", _gst_plugins_seqno);
}

313 314 315 316 317
/**
 * gst_plugin_unload_all:
 *
 * Unload all plugins in memory.
 */
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
void
gst_plugin_unload_all (void)
{
  GList *walk = _gst_plugins;

  while (walk) {
    GstPlugin *plugin = (GstPlugin *) walk->data;

    GST_INFO (GST_CAT_PLUGIN_LOADING, "unloading plugin %s", plugin->name);
    if (plugin->module) {
      GList *features = gst_plugin_get_feature_list (plugin);

      while (features) {
        GstPluginFeature *feature = GST_PLUGIN_FEATURE (features->data);

	GST_INFO (GST_CAT_PLUGIN_LOADING, "unloading feature %s", GST_OBJECT_NAME (feature));
	gst_plugin_feature_unload_thyself (feature);

	features = g_list_next (features);
      }
      if (g_module_close (plugin->module)) {
        plugin->module = NULL;
      }
      else {
	g_warning ("error closing module");
      }
    }
    
    walk = g_list_next (walk);
  }
Erik Walthinsen's avatar
Erik Walthinsen committed
348 349
}

350 351
/**
 * gst_library_load:
352
 * @name: name of library to load
353
 *
354 355
 * Load the named library.  Name should be given as
 * &quot;liblibrary.so&quot;.
356
 *
357
 * Returns: whether the library was loaded or not
358
 */
359 360
gboolean
gst_library_load (const gchar *name)
361
{
362 363 364 365 366 367 368 369
  gboolean res;
  GList *libraries = _gst_libraries;

  while (libraries) {
    if (!strcmp((gchar *)libraries->data, name)) return TRUE;

    libraries = g_list_next(libraries);
  }
370

371
  /* for now this is the same */
372 373 374
  res = gst_plugin_load(name);

  if (res) {
Wim Taymans's avatar
Wim Taymans committed
375
    _gst_libraries = g_list_prepend(_gst_libraries, g_strdup (name));
376 377 378
  }

  return res;
379
}
380

Erik Walthinsen's avatar
Erik Walthinsen committed
381 382 383 384 385 386 387 388 389
/**
 * gst_plugin_load:
 * @name: name of plugin to load
 *
 * Load the named plugin.  Name should be given as
 * &quot;libplugin.so&quot;.
 *
 * Returns: whether the plugin was loaded or not
 */
390 391
gboolean
gst_plugin_load (const gchar *name)
392
{
Erik Walthinsen's avatar
Erik Walthinsen committed
393 394
  GList *path;
  gchar *libspath;
Erik Walthinsen's avatar
Erik Walthinsen committed
395
  gchar *pluginname;
396 397 398
  GstPlugin *plugin;
  
  g_return_val_if_fail (name != NULL, FALSE);
Erik Walthinsen's avatar
Erik Walthinsen committed
399

400
  plugin = gst_plugin_find (name);
401 402 403
  
  if (plugin && plugin->module) 
    return TRUE;
404

Erik Walthinsen's avatar
Erik Walthinsen committed
405 406
  path = _gst_plugin_paths;
  while (path != NULL) {
Erik Walthinsen's avatar
Erik Walthinsen committed
407 408 409
    pluginname = g_module_build_path(path->data,name);
    if (gst_plugin_load_absolute(pluginname)) {
      g_free(pluginname);
Erik Walthinsen's avatar
Erik Walthinsen committed
410
      return TRUE;
Erik Walthinsen's avatar
Erik Walthinsen committed
411 412
    }
    g_free(pluginname);
Erik Walthinsen's avatar
Erik Walthinsen committed
413
    libspath = g_strconcat(path->data,"/.libs",NULL);
414
    /* g_print("trying to load '%s'\n",g_module_build_path(libspath,name)); */
Erik Walthinsen's avatar
Erik Walthinsen committed
415 416 417 418
    pluginname = g_module_build_path(libspath,name);
    g_free(libspath);
    if (gst_plugin_load_absolute(pluginname)) {
      g_free(pluginname);
Erik Walthinsen's avatar
Erik Walthinsen committed
419 420
      return TRUE;
    }
Erik Walthinsen's avatar
Erik Walthinsen committed
421
    g_free(pluginname);
422
    /* g_print("trying to load '%s' from '%s'\n",name,path->data); */
Erik Walthinsen's avatar
Erik Walthinsen committed
423 424 425
    pluginname = g_module_build_path("",name);
    if (gst_plugin_load_recurse(path->data,pluginname)) {
      g_free(pluginname);
Erik Walthinsen's avatar
Erik Walthinsen committed
426 427
      return TRUE;
    }
Erik Walthinsen's avatar
Erik Walthinsen committed
428
    g_free(pluginname);
Erik Walthinsen's avatar
Erik Walthinsen committed
429 430 431 432 433
    path = g_list_next(path);
  }
  return FALSE;
}

434
/**
435
 * gst_plugin_load_absolute:
436 437 438
 * @name: name of plugin to load
 *
 * Load the named plugin.  Name should be given as
439
 * &quot;/path/to/plugin/libplugin.so&quot;.
440 441 442 443 444 445 446 447 448 449 450
 *
 * Returns: whether the plugin was loaded or not
 */
gboolean
gst_plugin_load_absolute (const gchar *filename)
{
  GstPlugin *plugin = NULL;
  GList *plugins = _gst_plugins;

  g_return_val_if_fail (filename != NULL, FALSE);

451
  GST_DEBUG (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" absolute loading", filename);
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476

  while (plugins) {
    GstPlugin *testplugin = (GstPlugin *)plugins->data;

    if (testplugin->filename) {
      if (!strcmp (testplugin->filename, filename)) {
	plugin = testplugin;
	break;
      }
    }
    plugins = g_list_next (plugins);
  }
  if (!plugin) {
    plugin = g_new0 (GstPlugin, 1);
    plugin->filename = g_strdup (filename);
    _gst_plugins = g_list_prepend (_gst_plugins, plugin);
    _gst_plugins_seqno++;
  }

  return gst_plugin_load_plugin (plugin);
}

static gboolean
gst_plugin_check_version (gint major, gint minor)
{
477 478
  /* return NULL if the major and minor version numbers are not compatible */
  /* with ours. */
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR) 
    return FALSE;

  return TRUE;
}

static GstPlugin*
gst_plugin_register_func (GstPluginDesc *desc, GstPlugin *plugin, GModule *module)
{
  if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
    GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" has incompatible version, not loading",
       plugin->filename);
    g_free(plugin);
    return NULL;
  }

  plugin->name = g_strdup(desc->name);

  if (!((desc->plugin_init) (module, plugin))) {
    GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" failed to initialise",
       plugin->filename);
    g_free(plugin);
    return NULL;
  }
  
  return plugin;
}

Erik Walthinsen's avatar
Erik Walthinsen committed
507
/**
508 509 510 511
 * gst_plugin_load_plugin:
 * @plugin: The plugin to load
 *
 * Load the given plugin.
Erik Walthinsen's avatar
Erik Walthinsen committed
512 513 514
 *
 * Returns: whether or not the plugin loaded
 */
515
gboolean
516
gst_plugin_load_plugin (GstPlugin *plugin)
517
{
Erik Walthinsen's avatar
Erik Walthinsen committed
518
  GModule *module;
519
  GstPluginDesc *desc;
520
  struct stat file_status;
521
  gchar *filename;
Erik Walthinsen's avatar
Erik Walthinsen committed
522

523
  g_return_val_if_fail (plugin != NULL, FALSE);
Wim Taymans's avatar
Wim Taymans committed
524

525 526 527 528 529
  if (plugin->module) 
    return TRUE;

  filename = plugin->filename;

530
  GST_DEBUG (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" loading", filename);
531 532

  if (g_module_supported () == FALSE) {
533
    g_warning ("gstplugin: wow, you built this on a platform without dynamic loading???\n");
534
    return FALSE;
Erik Walthinsen's avatar
Erik Walthinsen committed
535 536
  }

537
  if (stat (filename, &file_status)) {
538
    /* g_print("problem opening file %s\n",filename); */
539 540 541
    return FALSE;
  }

542 543
  module = g_module_open (filename, G_MODULE_BIND_LAZY);

Erik Walthinsen's avatar
Erik Walthinsen committed
544
  if (module != NULL) {
545
    if (g_module_symbol (module, "plugin_desc", (gpointer *)&desc)) {
546
      GST_DEBUG (GST_CAT_PLUGIN_LOADING,"loading plugin \"%s\"...", filename);
547 548 549

      plugin->filename = g_strdup (filename);
      plugin = gst_plugin_register_func (desc, plugin, module);
550 551

      if (plugin != NULL) {
552 553 554
        GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" loaded",
             plugin->filename);
        plugin->module = module;
Erik Walthinsen's avatar
Erik Walthinsen committed
555 556 557
        return TRUE;
      }
    }
558
    return TRUE;
Erik Walthinsen's avatar
Erik Walthinsen committed
559
  } else if (_gst_plugin_spew) {
560
    /* FIXME this should be some standard gst mechanism!!! */
561
    g_printerr ("error loading plugin %s, reason: %s\n", filename, g_module_error());
Erik Walthinsen's avatar
Erik Walthinsen committed
562
  }
Wim Taymans's avatar
Wim Taymans committed
563
  else {
564
    GST_INFO (GST_CAT_PLUGIN_LOADING, "error loading plugin %s, reason: %s\n", filename, g_module_error());
Wim Taymans's avatar
Wim Taymans committed
565
  }
Erik Walthinsen's avatar
Erik Walthinsen committed
566 567 568 569 570

  return FALSE;
}


Wim Taymans's avatar
Wim Taymans committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
/**
 * gst_plugin_get_name:
 * @plugin: plugin to get the name of
 *
 * Get the short name of the plugin
 *
 * Returns: the name of the plugin
 */
const gchar*
gst_plugin_get_name (GstPlugin *plugin)
{
  g_return_val_if_fail (plugin != NULL, NULL);

  return plugin->name;
}

/**
 * gst_plugin_set_name:
 * @plugin: plugin to set name of
 * @name: new name
 *
 * Sets the name (should be short) of the plugin.
 */
void
gst_plugin_set_name (GstPlugin *plugin, const gchar *name)
{
  g_return_if_fail (plugin != NULL);

  if (plugin->name)
    g_free (plugin->name);

  plugin->name = g_strdup (name);
}

Erik Walthinsen's avatar
Erik Walthinsen committed
605 606 607 608 609 610 611
/**
 * gst_plugin_set_longname:
 * @plugin: plugin to set long name of
 * @longname: new long name
 *
 * Sets the long name (should be descriptive) of the plugin.
 */
612 613
void
gst_plugin_set_longname (GstPlugin *plugin, const gchar *longname)
614
{
Erik Walthinsen's avatar
Erik Walthinsen committed
615 616
  g_return_if_fail(plugin != NULL);

617
  if (plugin->longname)
Wim Taymans's avatar
Wim Taymans committed
618 619
    g_free(plugin->longname);

Erik Walthinsen's avatar
Erik Walthinsen committed
620 621 622
  plugin->longname = g_strdup(longname);
}

Wim Taymans's avatar
Wim Taymans committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
/**
 * gst_plugin_get_longname:
 * @plugin: plugin to get long name of
 *
 * Get the long descriptive name of the plugin
 *
 * Returns: the long name of the plugin
 */
const gchar*
gst_plugin_get_longname (GstPlugin *plugin)
{
  g_return_val_if_fail (plugin != NULL, NULL);

  return plugin->longname;
}

/**
 * gst_plugin_get_filename:
 * @plugin: plugin to get the filename of
 *
 * get the filename of the plugin
 *
 * Returns: the filename of the plugin
 */
const gchar*
gst_plugin_get_filename (GstPlugin *plugin)
{
  g_return_val_if_fail (plugin != NULL, NULL);

  return plugin->filename;
}

/**
 * gst_plugin_is_loaded:
 * @plugin: plugin to query
 *
 * queries if the plugin is loaded into memory
 *
 * Returns: TRUE is loaded, FALSE otherwise
 */
gboolean
gst_plugin_is_loaded (GstPlugin *plugin)
{
  g_return_val_if_fail (plugin != NULL, FALSE);

668
  return (plugin->module != NULL);
Wim Taymans's avatar
Wim Taymans committed
669 670 671
}


Erik Walthinsen's avatar
Erik Walthinsen committed
672 673 674 675 676 677 678 679
/**
 * gst_plugin_find:
 * @name: name of plugin to find
 *
 * Search the list of registered plugins for one of the given name
 *
 * Returns: pointer to the #GstPlugin if found, NULL otherwise
 */
680
GstPlugin*
681
gst_plugin_find (const gchar *name)
682
{
Erik Walthinsen's avatar
Erik Walthinsen committed
683 684
  GList *plugins = _gst_plugins;

685
  g_return_val_if_fail (name != NULL, NULL);
Erik Walthinsen's avatar
Erik Walthinsen committed
686 687 688

  while (plugins) {
    GstPlugin *plugin = (GstPlugin *)plugins->data;
689

Erik Walthinsen's avatar
Erik Walthinsen committed
690
    if (plugin->name) {
691
      if (!strcmp (plugin->name, name)) {
Erik Walthinsen's avatar
Erik Walthinsen committed
692
        return plugin;
693
      }
Erik Walthinsen's avatar
Erik Walthinsen committed
694
    }
695
    plugins = g_list_next (plugins);
Erik Walthinsen's avatar
Erik Walthinsen committed
696 697 698 699
  }
  return NULL;
}

700 701
static GstPluginFeature*
gst_plugin_find_feature_func (GstPlugin *plugin, const gchar *name, GType type)
702
{
703
  GList *features = plugin->features;
704

705
  g_return_val_if_fail (name != NULL, NULL);
706

707 708
  while (features) {
    GstPluginFeature *feature = GST_PLUGIN_FEATURE (features->data);
709 710


711 712
    if (!strcmp(GST_OBJECT_NAME (feature), name) && G_OBJECT_TYPE (feature) == type)
      return  GST_PLUGIN_FEATURE (feature);
713

714
    features = g_list_next (features);
Wim Taymans's avatar
Wim Taymans committed
715 716 717 718 719
  }

  return NULL;
}

720
G_GNUC_UNUSED static GstPluginFeature*
721
gst_plugin_find_feature (const gchar *name, GType type)
722
{
723
  GList *plugins;
724

725
  plugins = _gst_plugins;
726
  while (plugins) {
727 728
    GstPlugin *plugin = (GstPlugin *)plugins->data;
    GstPluginFeature *feature;
Wim Taymans's avatar
Wim Taymans committed
729

730 731 732 733
    feature = gst_plugin_find_feature_func (plugin, name, type);
    if (feature)
      return feature;
    
734 735 736
    plugins = g_list_next(plugins);
  }

737
  return NULL;
738 739
}

Erik Walthinsen's avatar
Erik Walthinsen committed
740
/**
741 742 743
 * gst_plugin_add_feature:
 * @plugin: plugin to add feature to
 * @feature: feature to add
Erik Walthinsen's avatar
Erik Walthinsen committed
744
 *
745
 * Add feature to the list of those provided by the plugin.
746 747
 * There is a separate namespace for each plugin feature type.
 * See #gst_plugin_get_feature_list
Erik Walthinsen's avatar
Erik Walthinsen committed
748
 */
749
void
750
gst_plugin_add_feature (GstPlugin *plugin, GstPluginFeature *feature)
751
{
752
  GstPluginFeature *oldfeature;
Erik Walthinsen's avatar
Erik Walthinsen committed
753

754
  g_return_if_fail (plugin != NULL);
755 756
  g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
  g_return_if_fail (feature != NULL);
757

758
  oldfeature = gst_plugin_find_feature_func (plugin, GST_OBJECT_NAME (feature), G_OBJECT_TYPE (feature));
759

760 761 762 763 764
  if (!oldfeature) {
    feature->manager = plugin;
    plugin->features = g_list_prepend (plugin->features, feature);
    plugin->numfeatures++;
  }
Wim Taymans's avatar
Wim Taymans committed
765 766
}

767 768 769 770 771 772 773
/**
 * gst_plugin_get_list:
 *
 * get the currently loaded plugins
 *
 * Returns; a GList of GstPlugin elements
 */
774
GList*
Wim Taymans's avatar
Wim Taymans committed
775
gst_plugin_get_list (void)
776
{
777
  return g_list_copy (_gst_plugins);
Erik Walthinsen's avatar
Erik Walthinsen committed
778
}
779

780
#ifndef GST_DISABLE_REGISTRY
Wim Taymans's avatar
Wim Taymans committed
781 782 783 784 785 786 787 788
/**
 * gst_plugin_save_thyself:
 * @parent: the parent node to save the plugin to
 *
 * saves the plugin into an XML representation
 *
 * Returns: the new XML node
 */
789 790
xmlNodePtr
gst_plugin_save_thyself (xmlNodePtr parent)
791
{
792
  xmlNodePtr tree, subtree;
793
  GList *l = NULL, *plugins = NULL;
794

795
  plugins = _gst_plugins;
796 797
  while (plugins) {
    GstPlugin *plugin = (GstPlugin *)plugins->data;
798
    GList *features;
Wim Taymans's avatar
Wim Taymans committed
799

800 801 802 803 804
    plugins = g_list_next (plugins);

    if (!plugin->name) 
      continue;
    
Wim Taymans's avatar
Wim Taymans committed
805 806 807 808 809
    tree = xmlNewChild (parent, NULL, "plugin", NULL);
    xmlNewChild (tree, NULL, "name", plugin->name);
    xmlNewChild (tree, NULL, "longname", plugin->longname);
    xmlNewChild (tree, NULL, "filename", plugin->filename);

810 811 812
    features = plugin->features;
    while (features) {
      GstPluginFeature *feature = GST_PLUGIN_FEATURE (features->data);
813

814 815
      subtree = xmlNewChild(tree, NULL, "feature", NULL);
      xmlNewProp (subtree, "typename", g_type_name (G_OBJECT_TYPE (feature)));
816

817
      gst_object_save_thyself (GST_OBJECT (feature), subtree);
818

819
      features = g_list_next (features);
Wim Taymans's avatar
Wim Taymans committed
820
    }
821
  }
822 823 824 825 826 827 828 829
  
  /* save the plugin search path in reverse order (because they are cons'd back on) */
  l=g_list_last(_gst_plugin_paths);
  while(l) {
    xmlNewChild (parent, NULL, "plugin-path", l->data);
    l = l->prev;
  }
    
830 831 832
  return parent;
}

Wim Taymans's avatar
Wim Taymans committed
833 834 835 836 837 838
/**
 * gst_plugin_load_thyself:
 * @parent: the parent node to load the plugin from
 *
 * load the plugin from an XML representation
 */
839 840
void
gst_plugin_load_thyself (xmlNodePtr parent)
841
{
842
  xmlNodePtr kinderen;
843
  gint featurecount = 0;
Erik Walthinsen's avatar
Erik Walthinsen committed
844
  gchar *pluginname;
845

846
  kinderen = parent->xmlChildrenNode; /* Dutch invasion :-) */
847
  while (kinderen) {
Wim Taymans's avatar
Wim Taymans committed
848
    if (!strcmp (kinderen->name, "plugin")) {
Richard Boulton's avatar
Richard Boulton committed
849
      xmlNodePtr field = kinderen->xmlChildrenNode;
850
      GstPlugin *plugin = g_new0 (GstPlugin, 1);
851
      
852 853 854
      plugin->numfeatures = 0;
      plugin->features = NULL;
      plugin->module = NULL;
855
      
856
      while (field) {
Wim Taymans's avatar
Wim Taymans committed
857 858 859 860 861
	if (!strcmp (field->name, "name")) {
          pluginname = xmlNodeGetContent (field);
	  if (gst_plugin_find (pluginname)) {
            g_free (pluginname);
            g_free (plugin);
862 863
	    plugin = NULL;
            break;
Erik Walthinsen's avatar
Erik Walthinsen committed
864 865
	  } else {
	    plugin->name = pluginname;
866 867
	  }
	}
Wim Taymans's avatar
Wim Taymans committed
868 869
	else if (!strcmp (field->name, "longname")) {
	  plugin->longname = xmlNodeGetContent (field);
870
	}
Wim Taymans's avatar
Wim Taymans committed
871 872
	else if (!strcmp (field->name, "filename")) {
	  plugin->filename = xmlNodeGetContent (field);
873
	}
874 875 876 877 878 879 880 881 882 883 884 885
	else if (!strcmp (field->name, "feature")) {
	  GstPluginFeature *feature;
	  gchar *prop;
	  
	  prop = xmlGetProp (field, "typename");
	  feature = GST_PLUGIN_FEATURE (g_object_new (g_type_from_name (prop), NULL));
	  
	  if (feature) {
	    gst_object_restore_thyself (GST_OBJECT (feature), field);
	    gst_plugin_add_feature (plugin, feature);
	    featurecount++;
	  }
886 887 888 889 890 891
	}

	field = field->next;
      }

      if (plugin) {
Wim Taymans's avatar
Wim Taymans committed
892
        _gst_plugins = g_list_prepend (_gst_plugins, plugin);
893
        _gst_plugins_seqno++;
894
      }
895 896 897
    } else if (!strcmp (kinderen->name, "plugin-path")) {
      _gst_plugin_paths = g_list_prepend (_gst_plugin_paths,
                                          xmlNodeGetContent (kinderen));
898
    }
899
    
900 901
    kinderen = kinderen->next;
  }
902
  GST_INFO (GST_CAT_PLUGIN_LOADING, " added %d features ", featurecount);
903
}
904
#endif /* GST_DISABLE_REGISTRY */
905

906

Wim Taymans's avatar
Wim Taymans committed
907
/**
908 909
 * gst_plugin_get_feature_list:
 * @plugin: the plugin to get the features from
Wim Taymans's avatar
Wim Taymans committed
910
 *
911
 * get a list of all the features that this plugin provides
Wim Taymans's avatar
Wim Taymans committed
912
 *
913
 * Returns: a GList of features
Wim Taymans's avatar
Wim Taymans committed
914 915
 */
GList*
916
gst_plugin_get_feature_list (GstPlugin *plugin)
Wim Taymans's avatar
Wim Taymans committed
917 918 919
{
  g_return_val_if_fail (plugin != NULL, NULL);

920
  return plugin->features;
Wim Taymans's avatar
Wim Taymans committed
921
}