2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gst.c: Initialization and non-pipeline operations
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * @short_description: Media library supporting arbitrary formats and filter graphs.
25 * @see_also: Check out both <ulink url="http://www.cse.ogi.edu/sysl/">OGI's
26 * pipeline</ulink> and Microsoft's DirectShow for some background.
28 * GStreamer is a framework for constructing graphs of various filters
29 * (termed elements here) that will handle streaming media. Any discreet
30 * (packetizable) media type is supported, with provisions for automatically
31 * determining source type. Formatting/framing information is provided with
32 * a powerful negotiation framework. Plugins are heavily used to provide for
33 * all elements, allowing one to construct plugins outside of the GST
34 * library, even released binary-only if license require (please don't).
36 * GStreamer borrows heavily from both the <ulink
37 * url="http://www.cse.ogi.edu/sysl/">OGI media pipeline</ulink> and
38 * Microsoft's DirectShow, hopefully taking the best of both and leaving the
39 * cruft behind. Its interface is still very fluid and thus can be changed
40 * to increase the sanity/noise ratio.
42 * The <application>GStreamer</application> library should be initialized with
43 * gst_init() before it can be used. You should pass pointers to the main argc
44 * and argv variables so that GStreamer can process its own command line
45 * options, as shown in the following example.
48 * <title>Initializing the gstreamer library</title>
49 * <programlisting language="c">
51 * main (int argc, char *argv[])
53 * // initialize the GStreamer library
54 * gst_init (&argc, &argv);
60 * It's allowed to pass two NULL pointers to gst_init() in case you don't want to
61 * pass the command line args to GStreamer.
63 * You can also use a popt table to initialize your own parameters as shown in
64 * the next code fragment:
66 * <title>Initializing own parameters when initializing gstreamer</title>
68 * static gboolean stats = FALSE;
72 * main (int argc, char *argv[])
74 * struct poptOption options[] = {
75 * { "stats", 's', POPT_ARG_NONE|POPT_ARGFLAG_STRIP, &stats, 0,
76 * "Show pad stats", NULL},
80 * // initialize the GStreamer library
81 * gst_init_with_popt_table (&argc, &argv, options);
88 * Use gst_version() to query the library version at runtime or use the GST_VERSION_* macros
89 * to find the version at compile time.
91 * The functions gst_main() and gst_main_quit() enter and exit the main loop.
92 * GStreamer doesn't currently require you to use a mainloop but can intergrate
93 * with it without problems.
99 #include "gst_private.h"
100 #include "gst-i18n-lib.h"
101 #include <locale.h> /* for LC_ALL */
104 #include "gstqueue.h"
106 #define GST_CAT_DEFAULT GST_CAT_GST_INIT
108 #define MAX_PATH_SPLIT 16
109 #define GST_PLUGIN_SEPARATOR ","
111 #ifndef GST_DISABLE_REGISTRY
112 gboolean _gst_registry_auto_load = TRUE;
115 static gboolean gst_initialized = FALSE;
117 extern gint _gst_trace_on;
119 /* set to TRUE when segfaults need to be left as is */
120 gboolean _gst_disable_segtrap = FALSE;
123 static void load_plugin_func (gpointer data, gpointer user_data);
124 static gboolean init_pre (void);
125 static gboolean init_post (void);
126 static gboolean parse_goption_arg (const gchar * s_opt,
127 const gchar * arg, gpointer data, GError ** err);
129 static GSList *preload_plugins = NULL;
131 const gchar *g_log_domain_gstreamer = "GStreamer";
134 debug_log_handler (const gchar * log_domain,
135 GLogLevelFlags log_level, const gchar * message, gpointer user_data)
137 g_log_default_handler (log_domain, log_level, message, user_data);
138 /* FIXME: do we still need this ? fatal errors these days are all
139 * other than core errors */
140 /* g_on_error_query (NULL); */
147 #ifndef GST_DISABLE_GST_DEBUG
160 /* debug-spec ::= category-spec [, category-spec]*
161 * category-spec ::= category:val | val
171 parse_debug_category (gchar * str, const gchar ** category)
188 parse_debug_level (gchar * str, gint * level)
196 if (str[0] != NUL && str[1] == NUL
197 && str[0] >= '0' && str[0] < '0' + GST_LEVEL_COUNT) {
198 *level = str[0] - '0';
206 parse_debug_list (const gchar * list)
211 g_return_if_fail (list != NULL);
213 split = g_strsplit (list, ",", 0);
215 for (walk = split; *walk; walk++) {
216 if (strchr (*walk, ':')) {
217 gchar **values = g_strsplit (*walk, ":", 2);
219 if (values[0] && values[1]) {
221 const gchar *category;
223 if (parse_debug_category (values[0], &category)
224 && parse_debug_level (values[1], &level))
225 gst_debug_set_threshold_for_name (category, level);
232 if (parse_debug_level (*walk, &level))
233 gst_debug_set_default_threshold (level);
240 #ifndef GST_HAVE_GLIB_2_8
241 #define G_OPTION_FLAG_NO_ARG 0
245 * gst_init_get_option_group:
247 * Returns a #GOptionGroup with GStreamer's argument specifications. The
248 * group is set up to use standard GOption callbacks, so when using this
249 * group in combination with GOption parsing methods, all argument parsing
250 * and initialization is automated.
252 * This function is useful if you want to integrate GStreamer with other
253 * libraries that use GOption.
255 * Returns: a pointer to a GStreamer option group. Should be dereferenced
260 gst_init_get_option_group (void)
263 static GOptionEntry gst_args[] = {
264 {"gst-version", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
265 parse_goption_arg, N_("Print the GStreamer version"), NULL},
266 {"gst-fatal-warnings", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
267 parse_goption_arg, N_("Make all warnings fatal"), NULL},
268 #ifndef GST_DISABLE_GST_DEBUG
269 {"gst-debug-help", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
270 parse_goption_arg, N_("Print available debug categories and exit"),
272 {"gst-debug-level", 0, 0, G_OPTION_ARG_CALLBACK, parse_goption_arg,
273 N_("Default debug level from 1 (only error) to 5 (anything) or "
276 {"gst-debug", 0, 0, G_OPTION_ARG_CALLBACK, parse_goption_arg,
277 N_("Comma-separated list of category_name:level pairs to set "
278 "specific levels for the individual categories. Example: "
279 "GST_AUTOPLUG:5,GST_ELEMENT_*:3"),
281 {"gst-debug-no-color", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
282 parse_goption_arg, N_("Disable colored debugging output"), NULL},
283 {"gst-debug-disable", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
284 parse_goption_arg, N_("Disable debugging"), NULL},
286 {"gst-plugin-spew", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
287 parse_goption_arg, N_("Enable verbose plugin loading diagnostics"),
289 {"gst-plugin-path", 0, 0, G_OPTION_ARG_CALLBACK, parse_goption_arg,
290 N_("Colon-separated paths containing plugins"), N_("PATHS")},
291 {"gst-plugin-load", 0, 0, G_OPTION_ARG_CALLBACK, parse_goption_arg,
292 N_("Comma-separated list of plugins to preload in addition to the "
293 "list stored in environment variable GST_PLUGIN_PATH"),
295 {"gst-disable-segtrap", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
297 N_("Disable trapping of segmentation faults during plugin loading"),
302 group = g_option_group_new ("gst", _("GStreamer Options"),
303 _("Show GStreamer Options"), NULL, NULL);
304 g_option_group_set_parse_hooks (group, (GOptionParseFunc) init_pre,
305 (GOptionParseFunc) init_post);
307 g_option_group_add_entries (group, gst_args);
308 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
315 * @argc: pointer to application's argc
316 * @argv: pointer to application's argv
317 * @err: pointer to a #GError to which a message will be posted on error
319 * Initializes the GStreamer library, setting up internal path lists,
320 * registering built-in elements, and loading standard plugins.
322 * This function will return %FALSE if GStreamer could not be initialized
323 * for some reason. If you want your program to fail fatally,
324 * use gst_init() instead.
326 * Returns: %TRUE if GStreamer could be initialized.
329 gst_init_check (int *argc, char **argv[], GError ** err)
335 if (gst_initialized) {
336 GST_DEBUG ("already initialized gst");
340 ctx = g_option_context_new ("- GStreamer initialization");
341 group = gst_init_get_option_group ();
342 g_option_context_add_group (ctx, group);
343 res = g_option_context_parse (ctx, argc, argv, err);
344 g_option_context_free (ctx);
347 gst_initialized = TRUE;
355 * @argc: pointer to application's argc
356 * @argv: pointer to application's argv
358 * Initializes the GStreamer library, setting up internal path lists,
359 * registering built-in elements, and loading standard plugins.
361 * This function will terminate your program if it was unable to initialize
362 * GStreamer for some reason. If you want your program to fall back,
363 * use gst_init_check() instead.
365 * WARNING: This function does not work in the same way as corresponding
366 * functions in other glib-style libraries, such as gtk_init(). In
367 * particular, unknown command line options cause this function to
368 * abort program execution.
371 gst_init (int *argc, char **argv[])
375 if (!gst_init_check (argc, argv, &err)) {
376 g_print ("Could not initialized GStreamer: %s\n",
377 err ? err->message : "unknown error occurred");
385 #ifndef GST_DISABLE_REGISTRY
387 add_path_func (gpointer data, gpointer user_data)
389 GST_INFO ("Adding plugin path: \"%s\"", (gchar *) data);
390 gst_registry_scan_path (gst_registry_get_default (), (gchar *) data);
395 prepare_for_load_plugin_func (gpointer data, gpointer user_data)
397 preload_plugins = g_slist_prepend (preload_plugins, data);
401 load_plugin_func (gpointer data, gpointer user_data)
404 const gchar *filename;
407 filename = (const gchar *) data;
409 plugin = gst_plugin_load_file (filename, &err);
412 GST_INFO ("Loaded plugin: \"%s\"", filename);
414 gst_default_registry_add_plugin (plugin);
417 /* Report error to user, and free error */
418 GST_ERROR ("Failed to load plugin: %s\n", err->message);
421 GST_WARNING ("Failed to load plugin: \"%s\"", filename);
429 split_and_iterate (const gchar * stringlist, gchar * separator, GFunc iterator,
434 gchar *lastlist = g_strdup (stringlist);
437 strings = g_strsplit (lastlist, separator, MAX_PATH_SPLIT);
442 iterator (strings[j], user_data);
443 if (++j == MAX_PATH_SPLIT) {
444 lastlist = g_strdup (strings[j]);
445 g_strfreev (strings);
450 g_strfreev (strings);
454 /* we have no fail cases yet, but maybe in the future */
460 if (g_thread_supported ()) {
461 /* somebody already initialized threading */
463 g_thread_init (NULL);
465 /* we need threading to be enabled right here */
469 setlocale (LC_ALL, "");
470 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
471 #endif /* ENABLE_NLS */
473 #ifndef GST_DISABLE_REGISTRY
475 const gchar *debug_list;
477 if (g_getenv ("GST_DEBUG_NO_COLOR") != NULL)
478 gst_debug_set_colored (FALSE);
480 debug_list = g_getenv ("GST_DEBUG");
482 parse_debug_list (debug_list);
486 /* This is the earliest we can make stuff show up in the logs.
487 * So give some useful info about GStreamer here */
488 GST_INFO ("Initializing GStreamer Core Library version %s", VERSION);
489 GST_INFO ("Using library installed in %s", LIBDIR);
495 gst_register_core_elements (GstPlugin * plugin)
497 /* register some standard builtin types */
498 if (!gst_element_register (plugin, "bin", GST_RANK_PRIMARY,
500 !gst_element_register (plugin, "pipeline", GST_RANK_PRIMARY,
501 GST_TYPE_PIPELINE) ||
502 !gst_element_register (plugin, "queue", GST_RANK_NONE, GST_TYPE_QUEUE)
504 g_assert_not_reached ();
509 static GstPluginDesc plugin_desc = {
513 "core elements of the GStreamer library",
514 gst_register_core_elements,
526 * - initalization of threads if we use them
529 * - initializes gst_format
530 * - registers a bunch of types for gst_objects
532 * - we don't have cases yet where this fails, but in the future
533 * we might and then it's nice to be able to return that
540 #ifndef GST_DISABLE_TRACE
542 #endif /* GST_DISABLE_TRACE */
544 llf = G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR | G_LOG_FLAG_FATAL;
545 g_log_set_handler (g_log_domain_gstreamer, llf, debug_log_handler, NULL);
547 _gst_format_initialize ();
548 _gst_query_initialize ();
549 gst_object_get_type ();
551 gst_element_factory_get_type ();
552 gst_element_get_type ();
553 gst_type_find_factory_get_type ();
556 #ifndef GST_DISABLE_INDEX
557 gst_index_factory_get_type ();
558 #endif /* GST_DISABLE_INDEX */
559 #ifndef GST_DISABLE_URI
560 gst_uri_handler_get_type ();
561 #endif /* GST_DISABLE_URI */
563 /* register core plugins */
564 _gst_plugin_register_static (&plugin_desc);
566 gst_structure_get_type ();
567 _gst_value_initialize ();
568 gst_caps_get_type ();
569 _gst_plugin_initialize ();
570 _gst_event_initialize ();
571 _gst_buffer_initialize ();
572 _gst_message_initialize ();
573 _gst_tag_initialize ();
575 #ifndef GST_DISABLE_REGISTRY
578 const char *plugin_path;
579 GstRegistry *default_registry;
581 default_registry = gst_registry_get_default ();
582 registry_file = g_strdup (g_getenv ("GST_REGISTRY"));
583 if (registry_file == NULL) {
584 registry_file = g_build_filename (g_get_home_dir (),
585 ".gstreamer-0.9", "registry.xml", NULL);
587 GST_DEBUG ("Reading registry cache");
588 gst_registry_xml_read_cache (default_registry, registry_file);
590 /* GST_PLUGIN_PATH specifies a list of directories to scan for
591 * additional plugins. These take precedence over the system plugins */
592 plugin_path = g_getenv ("GST_PLUGIN_PATH");
597 GST_DEBUG ("GST_PLUGIN_PATH set to %s", plugin_path);
598 list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
599 for (i = 0; list[i]; i++) {
600 gst_registry_scan_path (default_registry, list[i]);
604 GST_DEBUG ("GST_PLUGIN_PATH not set");
607 /* GST_PLUGIN_SYSTEM_PATH specifies a list of plugins that are always
608 * loaded by default. If not set, this defaults to the system-installed
609 * path, and the plugins installed in the user's home directory */
610 plugin_path = g_getenv ("GST_PLUGIN_SYSTEM_PATH");
611 if (plugin_path == NULL) {
614 GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH not set");
616 /* plugins in the user's home directory take precedence over
617 * system-installed ones */
618 home_plugins = g_build_filename (g_get_home_dir (),
619 ".gstreamer-0.9", "plugins", NULL);
620 gst_registry_scan_path (default_registry, home_plugins);
621 g_free (home_plugins);
623 /* add the main (installed) library path */
624 gst_registry_scan_path (default_registry, PLUGINS_DIR);
629 GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH set to %s", plugin_path);
630 list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
631 for (i = 0; list[i]; i++) {
632 gst_registry_scan_path (default_registry, list[i]);
637 gst_registry_xml_write_cache (default_registry, registry_file);
639 _gst_registry_remove_cache_plugins (default_registry);
641 g_free (registry_file);
644 #endif /* GST_DISABLE_REGISTRY */
646 /* if we need to preload plugins */
647 if (preload_plugins) {
648 g_slist_foreach (preload_plugins, load_plugin_func, NULL);
649 g_slist_free (preload_plugins);
650 preload_plugins = NULL;
652 #ifndef GST_DISABLE_TRACE
655 gst_trace = gst_trace_new ("gst.trace", 1024);
656 gst_trace_set_default (gst_trace);
658 #endif /* GST_DISABLE_TRACE */
663 #ifndef GST_DISABLE_GST_DEBUG
665 select_all (GstPlugin * plugin, gpointer user_data)
671 sort_by_category_name (gconstpointer a, gconstpointer b)
673 return strcmp (gst_debug_category_get_name ((GstDebugCategory *) a),
674 gst_debug_category_get_name ((GstDebugCategory *) b));
677 gst_debug_help (void)
685 list2 = gst_registry_plugin_filter (gst_registry_get_default (),
686 select_all, FALSE, NULL);
688 /* FIXME this is gross. why don't debug have categories PluginFeatures? */
689 for (g = list2; g; g = g_list_next (g)) {
690 GstPlugin *plugin = GST_PLUGIN (g->data);
692 gst_plugin_load (plugin);
696 list = gst_debug_get_all_categories ();
697 walk = list = g_slist_sort (list, sort_by_category_name);
700 g_print ("name level description\n");
701 g_print ("---------------------+--------+--------------------------------\n");
704 GstDebugCategory *cat = (GstDebugCategory *) walk->data;
706 if (gst_debug_is_colored ()) {
707 gchar *color = gst_debug_construct_term_color (cat->color);
709 g_print ("%s%-20s\033[00m %1d %s %s%s\033[00m\n",
711 gst_debug_category_get_name (cat),
712 gst_debug_category_get_threshold (cat),
713 gst_debug_level_get_name (gst_debug_category_get_threshold (cat)),
714 color, gst_debug_category_get_description (cat));
717 g_print ("%-20s %1d %s %s\n", gst_debug_category_get_name (cat),
718 gst_debug_category_get_threshold (cat),
719 gst_debug_level_get_name (gst_debug_category_get_threshold (cat)),
720 gst_debug_category_get_description (cat));
722 walk = g_slist_next (walk);
730 parse_one_option (gint opt, const gchar * arg, GError ** err)
734 g_print ("GStreamer Core Library version %s\n", GST_VERSION);
736 case ARG_FATAL_WARNINGS:{
737 GLogLevelFlags fatal_mask;
739 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
740 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
741 g_log_set_always_fatal (fatal_mask);
744 #ifndef GST_DISABLE_GST_DEBUG
745 case ARG_DEBUG_LEVEL:{
748 tmp = strtol (arg, NULL, 0);
749 if (tmp >= 0 && tmp < GST_LEVEL_COUNT) {
750 gst_debug_set_default_threshold (tmp);
755 parse_debug_list (arg);
757 case ARG_DEBUG_NO_COLOR:
758 gst_debug_set_colored (FALSE);
760 case ARG_DEBUG_DISABLE:
761 gst_debug_set_active (FALSE);
767 case ARG_PLUGIN_SPEW:
769 case ARG_PLUGIN_PATH:
770 #ifndef GST_DISABLE_REGISTRY
771 split_and_iterate (arg, G_SEARCHPATH_SEPARATOR_S, add_path_func, NULL);
772 #endif /* GST_DISABLE_REGISTRY */
774 case ARG_PLUGIN_LOAD:
775 split_and_iterate (arg, ",", prepare_for_load_plugin_func, NULL);
777 case ARG_SEGTRAP_DISABLE:
778 _gst_disable_segtrap = TRUE;
781 g_set_error (err, G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
782 _("Unknown option"));
790 parse_goption_arg (const gchar * opt,
791 const gchar * arg, gpointer data, GError ** err)
799 "--gst-version", ARG_VERSION}, {
800 "--gst-fatal-warnings", ARG_FATAL_WARNINGS},
801 #ifndef GST_DISABLE_GST_DEBUG
803 "--gst-debug-level", ARG_DEBUG_LEVEL}, {
804 "--gst-debug", ARG_DEBUG}, {
805 "--gst-debug-disable", ARG_DEBUG_DISABLE}, {
806 "--gst-debug-no-color", ARG_DEBUG_NO_COLOR}, {
807 "--gst-debug-help", ARG_DEBUG_HELP},
810 "--gst-plugin-spew", ARG_PLUGIN_SPEW}, {
811 "--gst-plugin-path", ARG_PLUGIN_PATH}, {
812 "--gst-plugin-load", ARG_PLUGIN_LOAD}, {
813 "--gst-disable-segtrap", ARG_SEGTRAP_DISABLE}, {
818 for (n = 0; options[n].opt; n++) {
819 if (!strcmp (opt, options[n].opt)) {
820 val = options[n].val;
825 return parse_one_option (val, arg, err);
832 * Call only once, before exiting.
833 * After this call GStreamer should not be used anymore.
836 extern GstRegistry *_gst_registry_default;
842 GST_INFO ("deinitializing GStreamer");
843 clock = gst_system_clock_obtain ();
844 gst_object_unref (clock);
845 gst_object_unref (clock);
847 _gst_registry_cleanup ();
849 gst_initialized = FALSE;
850 GST_INFO ("deinitialized GStreamer");
855 * @major: pointer to a guint to store the major version number
856 * @minor: pointer to a guint to store the minor version number
857 * @micro: pointer to a guint to store the micro version number
859 * Gets the version number of the GStreamer library.
862 gst_version (guint * major, guint * minor, guint * micro)
864 g_return_if_fail (major);
865 g_return_if_fail (minor);
866 g_return_if_fail (micro);
868 *major = GST_VERSION_MAJOR;
869 *minor = GST_VERSION_MINOR;
870 *micro = GST_VERSION_MICRO;