docs: tell more about what happens in gst_init.
[platform/upstream/gstreamer.git] / gst / gst.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gst.c: Initialization and non-pipeline operations
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /**
24  * SECTION:gst
25  * @short_description: Media library supporting arbitrary formats and filter
26  *                     graphs.
27  *
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).
35  * GStreamer covers a wide range of use cases including: playback, recording,
36  * editing, serving streams, voice over ip and video calls.
37  *
38  * The <application>GStreamer</application> library should be initialized with
39  * gst_init() before it can be used. You should pass pointers to the main argc
40  * and argv variables so that GStreamer can process its own command line
41  * options, as shown in the following example.
42  *
43  * <example>
44  * <title>Initializing the gstreamer library</title>
45  * <programlisting language="c">
46  * int
47  * main (int argc, char *argv[])
48  * {
49  *   // initialize the GStreamer library
50  *   gst_init (&amp;argc, &amp;argv);
51  *   ...
52  * }
53  * </programlisting>
54  * </example>
55  *
56  * It's allowed to pass two NULL pointers to gst_init() in case you don't want
57  * to pass the command line args to GStreamer.
58  *
59  * You can also use GOption to initialize your own parameters as shown in
60  * the next code fragment:
61  * <example>
62  * <title>Initializing own parameters when initializing gstreamer</title>
63  * <programlisting>
64  * static gboolean stats = FALSE;
65  * ...
66  * int
67  * main (int argc, char *argv[])
68  * {
69  *  GOptionEntry options[] = {
70  *   {"tags", 't', 0, G_OPTION_ARG_NONE, &amp;tags,
71  *       N_("Output tags (also known as metadata)"), NULL},
72  *   {NULL}
73  *  };
74  *  // must initialise the threading system before using any other GLib funtion
75  *  if (!g_thread_supported ())
76  *    g_thread_init (NULL);
77  *  ctx = g_option_context_new ("[ADDITIONAL ARGUMENTS]");
78  *  g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
79  *  g_option_context_add_group (ctx, gst_init_get_option_group ());
80  *  if (!g_option_context_parse (ctx, &amp;argc, &amp;argv, &amp;err)) {
81  *    g_print ("Error initializing: &percnt;s\n", GST_STR_NULL (err->message));
82  *    exit (1);
83  *  }
84  *  g_option_context_free (ctx);
85  * ...
86  * }
87  * </programlisting>
88  * </example>
89  *
90  * Use gst_version() to query the library version at runtime or use the
91  * GST_VERSION_* macros to find the version at compile time. Optionally
92  * gst_version_string() returns a printable string.
93  *
94  * The gst_deinit() call is used to clean up all internal resources used
95  * by <application>GStreamer</application>. It is mostly used in unit tests 
96  * to check for leaks.
97  *
98  * Last reviewed on 2006-08-11 (0.10.10)
99  */
100
101 #include "gst_private.h"
102 #include "gstconfig.h"
103 #include <stdlib.h>
104 #include <stdio.h>
105 #include <sys/types.h>
106 #ifdef HAVE_SYS_UTSNAME_H
107 #include <sys/utsname.h>
108 #endif
109 #ifdef HAVE_UNISTD_H
110 #include <unistd.h>
111 #endif
112 #ifdef G_OS_WIN32
113 #define WIN32_LEAN_AND_MEAN     /* prevents from including too many things */
114 #include <windows.h>            /* GetStdHandle, windows console */
115 #endif
116
117 #include "gst-i18n-lib.h"
118 #include <locale.h>             /* for LC_ALL */
119
120 #include "gst.h"
121
122 #define GST_CAT_DEFAULT GST_CAT_GST_INIT
123
124 #define MAX_PATH_SPLIT  16
125 #define GST_PLUGIN_SEPARATOR ","
126
127 static gboolean gst_initialized = FALSE;
128 static gboolean gst_deinitialized = FALSE;
129
130 #ifdef G_OS_WIN32
131 HMODULE _priv_gst_dll_handle = NULL;
132 #endif
133
134 #ifndef GST_DISABLE_REGISTRY
135 GList *_priv_gst_plugin_paths = NULL;   /* for delayed processing in post_init */
136
137 extern gboolean _priv_gst_disable_registry_update;
138 #endif
139
140 #ifndef GST_DISABLE_GST_DEBUG
141 extern const gchar *priv_gst_dump_dot_dir;
142 #endif
143
144 /* defaults */
145
146 /* set to TRUE when segfaults need to be left as is */
147 static gboolean _gst_disable_segtrap = FALSE;
148
149 static gboolean init_pre (GOptionContext * context, GOptionGroup * group,
150     gpointer data, GError ** error);
151 static gboolean init_post (GOptionContext * context, GOptionGroup * group,
152     gpointer data, GError ** error);
153 #ifndef GST_DISABLE_OPTION_PARSING
154 static gboolean parse_goption_arg (const gchar * s_opt,
155     const gchar * arg, gpointer data, GError ** err);
156 #endif
157
158 GSList *_priv_gst_preload_plugins = NULL;
159
160 const gchar g_log_domain_gstreamer[] = "GStreamer";
161
162 static void
163 debug_log_handler (const gchar * log_domain,
164     GLogLevelFlags log_level, const gchar * message, gpointer user_data)
165 {
166   g_log_default_handler (log_domain, log_level, message, user_data);
167   /* FIXME: do we still need this ? fatal errors these days are all
168    * other than core errors */
169   /* g_on_error_query (NULL); */
170 }
171
172 enum
173 {
174   ARG_VERSION = 1,
175   ARG_FATAL_WARNINGS,
176 #ifndef GST_DISABLE_GST_DEBUG
177   ARG_DEBUG_LEVEL,
178   ARG_DEBUG,
179   ARG_DEBUG_DISABLE,
180   ARG_DEBUG_NO_COLOR,
181   ARG_DEBUG_HELP,
182 #endif
183   ARG_PLUGIN_SPEW,
184   ARG_PLUGIN_PATH,
185   ARG_PLUGIN_LOAD,
186   ARG_SEGTRAP_DISABLE,
187   ARG_REGISTRY_UPDATE_DISABLE,
188   ARG_REGISTRY_FORK_DISABLE
189 };
190
191 /* debug-spec ::= category-spec [, category-spec]*
192  * category-spec ::= category:val | val
193  * category ::= [^:]+
194  * val ::= [0-5]
195  */
196
197 #ifndef NUL
198 #define NUL '\0'
199 #endif
200
201 #ifndef GST_DISABLE_GST_DEBUG
202 static gboolean
203 parse_debug_category (gchar * str, const gchar ** category)
204 {
205   if (!str)
206     return FALSE;
207
208   /* works in place */
209   g_strstrip (str);
210
211   if (str[0] != NUL) {
212     *category = str;
213     return TRUE;
214   }
215
216   return FALSE;
217 }
218
219 static gboolean
220 parse_debug_level (gchar * str, gint * level)
221 {
222   if (!str)
223     return FALSE;
224
225   /* works in place */
226   g_strstrip (str);
227
228   if (str[0] != NUL && str[1] == NUL
229       && str[0] >= '0' && str[0] < '0' + GST_LEVEL_COUNT) {
230     *level = str[0] - '0';
231     return TRUE;
232   }
233
234   return FALSE;
235 }
236
237 static void
238 parse_debug_list (const gchar * list)
239 {
240   gchar **split;
241   gchar **walk;
242
243   g_return_if_fail (list != NULL);
244
245   split = g_strsplit (list, ",", 0);
246
247   for (walk = split; *walk; walk++) {
248     if (strchr (*walk, ':')) {
249       gchar **values = g_strsplit (*walk, ":", 2);
250
251       if (values[0] && values[1]) {
252         gint level;
253         const gchar *category;
254
255         if (parse_debug_category (values[0], &category)
256             && parse_debug_level (values[1], &level))
257           gst_debug_set_threshold_for_name (category, level);
258       }
259
260       g_strfreev (values);
261     } else {
262       gint level;
263
264       if (parse_debug_level (*walk, &level))
265         gst_debug_set_default_threshold (level);
266     }
267   }
268
269   g_strfreev (split);
270 }
271 #endif
272
273 #ifdef G_OS_WIN32
274 BOOL WINAPI
275 DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
276 {
277   if (fdwReason == DLL_PROCESS_ATTACH)
278     _priv_gst_dll_handle = (HMODULE) hinstDLL;
279   return TRUE;
280 }
281
282 #endif
283
284 /**
285  * gst_init_get_option_group:
286  *
287  * Returns a #GOptionGroup with GStreamer's argument specifications. The
288  * group is set up to use standard GOption callbacks, so when using this
289  * group in combination with GOption parsing methods, all argument parsing
290  * and initialization is automated.
291  *
292  * This function is useful if you want to integrate GStreamer with other
293  * libraries that use GOption (see g_option_context_add_group() ).
294  *
295  * If you use this function, you should make sure you initialise the GLib
296  * threading system as one of the very first things in your program
297  * (see the example at the beginning of this section).
298  *
299  * Returns: a pointer to GStreamer's option group.
300  */
301
302 GOptionGroup *
303 gst_init_get_option_group (void)
304 {
305 #ifndef GST_DISABLE_OPTION_PARSING
306   GOptionGroup *group;
307   static const GOptionEntry gst_args[] = {
308     {"gst-version", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
309         (gpointer) parse_goption_arg, N_("Print the GStreamer version"), NULL},
310     {"gst-fatal-warnings", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
311         (gpointer) parse_goption_arg, N_("Make all warnings fatal"), NULL},
312 #ifndef GST_DISABLE_GST_DEBUG
313     {"gst-debug-help", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
314           (gpointer) parse_goption_arg,
315           N_("Print available debug categories and exit"),
316         NULL},
317     {"gst-debug-level", 0, 0, G_OPTION_ARG_CALLBACK,
318           (gpointer) parse_goption_arg,
319           N_("Default debug level from 1 (only error) to 5 (anything) or "
320               "0 for no output"),
321         N_("LEVEL")},
322     {"gst-debug", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) parse_goption_arg,
323           N_("Comma-separated list of category_name:level pairs to set "
324               "specific levels for the individual categories. Example: "
325               "GST_AUTOPLUG:5,GST_ELEMENT_*:3"),
326         N_("LIST")},
327     {"gst-debug-no-color", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
328           (gpointer) parse_goption_arg, N_("Disable colored debugging output"),
329         NULL},
330     {"gst-debug-disable", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
331         (gpointer) parse_goption_arg, N_("Disable debugging"), NULL},
332 #endif
333     {"gst-plugin-spew", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
334           (gpointer) parse_goption_arg,
335           N_("Enable verbose plugin loading diagnostics"),
336         NULL},
337     {"gst-plugin-path", 0, 0, G_OPTION_ARG_CALLBACK,
338           (gpointer) parse_goption_arg,
339         N_("Colon-separated paths containing plugins"), N_("PATHS")},
340     {"gst-plugin-load", 0, 0, G_OPTION_ARG_CALLBACK,
341           (gpointer) parse_goption_arg,
342           N_("Comma-separated list of plugins to preload in addition to the "
343               "list stored in environment variable GST_PLUGIN_PATH"),
344         N_("PLUGINS")},
345     {"gst-disable-segtrap", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
346           (gpointer) parse_goption_arg,
347           N_("Disable trapping of segmentation faults during plugin loading"),
348         NULL},
349     {"gst-disable-registry-update", 0, G_OPTION_FLAG_NO_ARG,
350           G_OPTION_ARG_CALLBACK,
351           (gpointer) parse_goption_arg,
352           N_("Disable updating the registry"),
353         NULL},
354     {"gst-disable-registry-fork", 0, G_OPTION_FLAG_NO_ARG,
355           G_OPTION_ARG_CALLBACK,
356           (gpointer) parse_goption_arg,
357           N_("Disable spawning a helper process while scanning the registry"),
358         NULL},
359     {NULL}
360   };
361
362   /* The GLib threading system must be initialised before calling any other
363    * GLib function according to the documentation; if the application hasn't
364    * called gst_init() yet or initialised the threading system otherwise, we
365    * better issue a warning here (since chances are high that the application
366    * has already called other GLib functions such as g_option_context_new() */
367   if (!g_thread_supported ()) {
368     g_warning ("The GStreamer function gst_init_get_option_group() was\n"
369         "\tcalled, but the GLib threading system has not been initialised\n"
370         "\tyet, something that must happen before any other GLib function\n"
371         "\tis called. The application needs to be fixed so that it calls\n"
372         "\t   if (!g_thread_supported ()) g_thread_init(NULL);\n"
373         "\tas very first thing in its main() function. Please file a bug\n"
374         "\tagainst this application.");
375     g_thread_init (NULL);
376   }
377
378   group = g_option_group_new ("gst", _("GStreamer Options"),
379       _("Show GStreamer Options"), NULL, NULL);
380   g_option_group_set_parse_hooks (group, (GOptionParseFunc) init_pre,
381       (GOptionParseFunc) init_post);
382
383   g_option_group_add_entries (group, gst_args);
384   g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
385
386   return group;
387 #else
388   return NULL;
389 #endif
390 }
391
392 /**
393  * gst_init_check:
394  * @argc: pointer to application's argc
395  * @argv: pointer to application's argv
396  * @err: pointer to a #GError to which a message will be posted on error
397  *
398  * Initializes the GStreamer library, setting up internal path lists,
399  * registering built-in elements, and loading standard plugins.
400  *
401  * This function will return %FALSE if GStreamer could not be initialized
402  * for some reason.  If you want your program to fail fatally,
403  * use gst_init() instead.
404  *
405  * This function should be called before calling any other GLib functions. If
406  * this is not an option, your program must initialise the GLib thread system
407  * using g_thread_init() before any other GLib functions are called.
408  *
409  * Returns: %TRUE if GStreamer could be initialized.
410  */
411 gboolean
412 gst_init_check (int *argc, char **argv[], GError ** err)
413 {
414 #ifndef GST_DISABLE_OPTION_PARSING
415   GOptionGroup *group;
416   GOptionContext *ctx;
417 #endif
418   gboolean res;
419
420   if (!g_thread_supported ())
421     g_thread_init (NULL);
422
423   if (gst_initialized) {
424     GST_DEBUG ("already initialized gst");
425     return TRUE;
426   }
427 #ifndef GST_DISABLE_OPTION_PARSING
428   ctx = g_option_context_new ("- GStreamer initialization");
429   g_option_context_set_ignore_unknown_options (ctx, TRUE);
430   group = gst_init_get_option_group ();
431   g_option_context_add_group (ctx, group);
432   res = g_option_context_parse (ctx, argc, argv, err);
433   g_option_context_free (ctx);
434 #else
435   init_pre (NULL, NULL, NULL, NULL);
436   init_post (NULL, NULL, NULL, NULL);
437   res = TRUE;
438 #endif
439
440   gst_initialized = res;
441
442   if (res) {
443     GST_INFO ("initialized GStreamer successfully");
444   } else {
445     GST_INFO ("failed to initialize GStreamer");
446   }
447
448   return res;
449 }
450
451 /**
452  * gst_init:
453  * @argc: (inout): pointer to application's argc
454  * @argv: (inout) (array length=argc) (allow-none): pointer to application's argv
455  *
456  * Initializes the GStreamer library, setting up internal path lists,
457  * registering built-in elements, and loading standard plugins.
458  *
459  * Unless the plugin registry is disabled at compile time, the registry will be
460  * loaded. By default this will also check if the registry cache needs to be
461  * updated and rescan all plugins if needed. See gst_update_registry() for
462  * details and section
463  * <link linkend="gst-running">Running GStreamer Applications</link>
464  * for how to disable automatic registry updates.
465  *
466  * This function should be called before calling any other GLib functions. If
467  * this is not an option, your program must initialise the GLib thread system
468  * using g_thread_init() before any other GLib functions are called.
469  *
470  * <note><para>
471  * This function will terminate your program if it was unable to initialize
472  * GStreamer for some reason.  If you want your program to fall back,
473  * use gst_init_check() instead.
474  * </para></note>
475  *
476  * WARNING: This function does not work in the same way as corresponding
477  * functions in other glib-style libraries, such as gtk_init().  In
478  * particular, unknown command line options cause this function to
479  * abort program execution.
480  */
481 void
482 gst_init (int *argc, char **argv[])
483 {
484   GError *err = NULL;
485
486   if (!gst_init_check (argc, argv, &err)) {
487     g_print ("Could not initialize GStreamer: %s\n",
488         err ? err->message : "unknown error occurred");
489     if (err) {
490       g_error_free (err);
491     }
492     exit (1);
493   }
494 }
495
496 #ifndef GST_DISABLE_REGISTRY
497 static void
498 add_path_func (gpointer data, gpointer user_data)
499 {
500   GST_INFO ("Adding plugin path: \"%s\", will scan later", (gchar *) data);
501   _priv_gst_plugin_paths =
502       g_list_append (_priv_gst_plugin_paths, g_strdup (data));
503 }
504 #endif
505
506 #ifndef GST_DISABLE_OPTION_PARSING
507 static void
508 prepare_for_load_plugin_func (gpointer data, gpointer user_data)
509 {
510   _priv_gst_preload_plugins =
511       g_slist_prepend (_priv_gst_preload_plugins, g_strdup (data));
512 }
513 #endif
514
515 #ifndef GST_DISABLE_OPTION_PARSING
516 static void
517 split_and_iterate (const gchar * stringlist, gchar * separator, GFunc iterator,
518     gpointer user_data)
519 {
520   gchar **strings;
521   gint j = 0;
522   gchar *lastlist = g_strdup (stringlist);
523
524   while (lastlist) {
525     strings = g_strsplit (lastlist, separator, MAX_PATH_SPLIT);
526     g_free (lastlist);
527     lastlist = NULL;
528
529     while (strings[j]) {
530       iterator (strings[j], user_data);
531       if (++j == MAX_PATH_SPLIT) {
532         lastlist = g_strdup (strings[j]);
533         j = 0;
534         break;
535       }
536     }
537     g_strfreev (strings);
538   }
539 }
540 #endif
541
542 /* we have no fail cases yet, but maybe in the future */
543 static gboolean
544 init_pre (GOptionContext * context, GOptionGroup * group, gpointer data,
545     GError ** error)
546 {
547   if (gst_initialized) {
548     GST_DEBUG ("already initialized");
549     return TRUE;
550   }
551
552   /* GStreamer was built against a GLib >= 2.8 and is therefore not doing
553    * the refcount hack. Check that it isn't being run against an older GLib */
554   if (glib_major_version < 2 ||
555       (glib_major_version == 2 && glib_minor_version < 8)) {
556     g_warning ("GStreamer was compiled against GLib %d.%d.%d but is running"
557         " against %d.%d.%d. This will cause reference counting issues",
558         GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION,
559         glib_major_version, glib_minor_version, glib_micro_version);
560   }
561
562   g_type_init ();
563
564   /* we need threading to be enabled right here */
565   g_assert (g_thread_supported ());
566   _gst_debug_init ();
567
568 #ifdef ENABLE_NLS
569   setlocale (LC_ALL, "");
570   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
571   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
572 #endif /* ENABLE_NLS */
573
574 #ifndef GST_DISABLE_GST_DEBUG
575   {
576     const gchar *debug_list;
577
578     if (g_getenv ("GST_DEBUG_NO_COLOR") != NULL)
579       gst_debug_set_colored (FALSE);
580
581     debug_list = g_getenv ("GST_DEBUG");
582     if (debug_list) {
583       parse_debug_list (debug_list);
584     }
585   }
586
587   priv_gst_dump_dot_dir = g_getenv ("GST_DEBUG_DUMP_DOT_DIR");
588 #endif
589   /* This is the earliest we can make stuff show up in the logs.
590    * So give some useful info about GStreamer here */
591   GST_INFO ("Initializing GStreamer Core Library version %s", VERSION);
592   GST_INFO ("Using library installed in %s", LIBDIR);
593
594   /* Print some basic system details if possible (OS/architecture) */
595 #ifdef HAVE_SYS_UTSNAME_H
596   {
597     struct utsname sys_details;
598
599     if (uname (&sys_details) == 0) {
600       GST_INFO ("%s %s %s %s %s", sys_details.sysname,
601           sys_details.nodename, sys_details.release, sys_details.version,
602           sys_details.machine);
603     }
604   }
605 #endif
606
607   return TRUE;
608 }
609
610 static gboolean
611 gst_register_core_elements (GstPlugin * plugin)
612 {
613   /* register some standard builtin types */
614   if (!gst_element_register (plugin, "bin", GST_RANK_PRIMARY,
615           GST_TYPE_BIN) ||
616       !gst_element_register (plugin, "pipeline", GST_RANK_PRIMARY,
617           GST_TYPE_PIPELINE)
618       )
619     g_assert_not_reached ();
620
621   return TRUE;
622 }
623
624 /*
625  * this bit handles:
626  * - initalization of threads if we use them
627  * - log handler
628  * - initial output
629  * - initializes gst_format
630  * - registers a bunch of types for gst_objects
631  *
632  * - we don't have cases yet where this fails, but in the future
633  *   we might and then it's nice to be able to return that
634  */
635 static gboolean
636 init_post (GOptionContext * context, GOptionGroup * group, gpointer data,
637     GError ** error)
638 {
639   GLogLevelFlags llf;
640
641 #ifndef GST_DISABLE_TRACE
642   GstTrace *gst_trace;
643 #endif /* GST_DISABLE_TRACE */
644
645   if (gst_initialized) {
646     GST_DEBUG ("already initialized");
647     return TRUE;
648   }
649
650   llf = G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR | G_LOG_FLAG_FATAL;
651   g_log_set_handler (g_log_domain_gstreamer, llf, debug_log_handler, NULL);
652
653   _priv_gst_quarks_initialize ();
654   _gst_format_initialize ();
655   _gst_query_initialize ();
656   g_type_class_ref (gst_object_get_type ());
657   g_type_class_ref (gst_pad_get_type ());
658   g_type_class_ref (gst_element_factory_get_type ());
659   g_type_class_ref (gst_element_get_type ());
660   g_type_class_ref (gst_type_find_factory_get_type ());
661   g_type_class_ref (gst_bin_get_type ());
662   g_type_class_ref (gst_bus_get_type ());
663   g_type_class_ref (gst_task_get_type ());
664   g_type_class_ref (gst_clock_get_type ());
665
666   g_type_class_ref (gst_index_factory_get_type ());
667   gst_uri_handler_get_type ();
668
669   g_type_class_ref (gst_object_flags_get_type ());
670   g_type_class_ref (gst_bin_flags_get_type ());
671   g_type_class_ref (gst_buffer_flag_get_type ());
672   g_type_class_ref (gst_buffer_copy_flags_get_type ());
673   g_type_class_ref (gst_buffer_list_item_get_type ());
674   g_type_class_ref (gst_bus_flags_get_type ());
675   g_type_class_ref (gst_bus_sync_reply_get_type ());
676   g_type_class_ref (gst_caps_flags_get_type ());
677   g_type_class_ref (gst_clock_return_get_type ());
678   g_type_class_ref (gst_clock_entry_type_get_type ());
679   g_type_class_ref (gst_clock_flags_get_type ());
680   g_type_class_ref (gst_clock_type_get_type ());
681   g_type_class_ref (gst_debug_graph_details_get_type ());
682   g_type_class_ref (gst_state_get_type ());
683   g_type_class_ref (gst_state_change_return_get_type ());
684   g_type_class_ref (gst_state_change_get_type ());
685   g_type_class_ref (gst_element_flags_get_type ());
686   g_type_class_ref (gst_core_error_get_type ());
687   g_type_class_ref (gst_library_error_get_type ());
688   g_type_class_ref (gst_resource_error_get_type ());
689   g_type_class_ref (gst_stream_error_get_type ());
690   g_type_class_ref (gst_event_type_flags_get_type ());
691   g_type_class_ref (gst_event_type_get_type ());
692   g_type_class_ref (gst_seek_type_get_type ());
693   g_type_class_ref (gst_seek_flags_get_type ());
694   g_type_class_ref (gst_format_get_type ());
695   g_type_class_ref (gst_index_certainty_get_type ());
696   g_type_class_ref (gst_index_entry_type_get_type ());
697   g_type_class_ref (gst_index_lookup_method_get_type ());
698   g_type_class_ref (gst_assoc_flags_get_type ());
699   g_type_class_ref (gst_index_resolver_method_get_type ());
700   g_type_class_ref (gst_index_flags_get_type ());
701   g_type_class_ref (gst_debug_level_get_type ());
702   g_type_class_ref (gst_debug_color_flags_get_type ());
703   g_type_class_ref (gst_iterator_result_get_type ());
704   g_type_class_ref (gst_iterator_item_get_type ());
705   g_type_class_ref (gst_message_type_get_type ());
706   g_type_class_ref (gst_mini_object_flags_get_type ());
707   g_type_class_ref (gst_pad_link_return_get_type ());
708   g_type_class_ref (gst_flow_return_get_type ());
709   g_type_class_ref (gst_activate_mode_get_type ());
710   g_type_class_ref (gst_pad_direction_get_type ());
711   g_type_class_ref (gst_pad_flags_get_type ());
712   g_type_class_ref (gst_pad_presence_get_type ());
713   g_type_class_ref (gst_pad_template_flags_get_type ());
714   g_type_class_ref (gst_pipeline_flags_get_type ());
715   g_type_class_ref (gst_plugin_error_get_type ());
716   g_type_class_ref (gst_plugin_flags_get_type ());
717   g_type_class_ref (gst_plugin_dependency_flags_get_type ());
718   g_type_class_ref (gst_rank_get_type ());
719   g_type_class_ref (gst_query_type_get_type ());
720   g_type_class_ref (gst_buffering_mode_get_type ());
721   g_type_class_ref (gst_stream_status_type_get_type ());
722   g_type_class_ref (gst_structure_change_type_get_type ());
723   g_type_class_ref (gst_tag_merge_mode_get_type ());
724   g_type_class_ref (gst_tag_flag_get_type ());
725   g_type_class_ref (gst_task_pool_get_type ());
726   g_type_class_ref (gst_task_state_get_type ());
727   g_type_class_ref (gst_alloc_trace_flags_get_type ());
728   g_type_class_ref (gst_type_find_probability_get_type ());
729   g_type_class_ref (gst_uri_type_get_type ());
730   g_type_class_ref (gst_parse_error_get_type ());
731   g_type_class_ref (gst_parse_flags_get_type ());
732   g_type_class_ref (gst_search_mode_get_type ());
733
734   gst_structure_get_type ();
735   _gst_value_initialize ();
736   g_type_class_ref (gst_param_spec_fraction_get_type ());
737   gst_caps_get_type ();
738   _gst_event_initialize ();
739   _gst_buffer_initialize ();
740   _gst_buffer_list_initialize ();
741   _gst_message_initialize ();
742   _gst_tag_initialize ();
743
744   _gst_plugin_initialize ();
745
746   gst_g_error_get_type ();
747
748   /* register core plugins */
749   gst_plugin_register_static (GST_VERSION_MAJOR, GST_VERSION_MINOR,
750       "staticelements", "core elements linked into the GStreamer library",
751       gst_register_core_elements, VERSION, GST_LICENSE, PACKAGE,
752       GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
753
754   /*
755    * Any errors happening below this point are non-fatal, we therefore mark
756    * gstreamer as being initialized, since it is the case from a plugin point of
757    * view.
758    *
759    * If anything fails, it will be put back to FALSE in gst_init_check().
760    * This allows some special plugins that would call gst_init() to not cause a
761    * looping effect (i.e. initializing GStreamer twice).
762    */
763   gst_initialized = TRUE;
764
765   if (!gst_update_registry ())
766     return FALSE;
767
768 #ifndef GST_DISABLE_TRACE
769   _gst_trace_on = 0;
770   if (_gst_trace_on) {
771     gst_trace = gst_trace_new ("gst.trace", 1024);
772     gst_trace_set_default (gst_trace);
773   }
774 #endif /* GST_DISABLE_TRACE */
775
776   return TRUE;
777 }
778
779 #ifndef GST_DISABLE_GST_DEBUG
780 static gboolean
781 select_all (GstPlugin * plugin, gpointer user_data)
782 {
783   return TRUE;
784 }
785
786 static gint
787 sort_by_category_name (gconstpointer a, gconstpointer b)
788 {
789   return strcmp (gst_debug_category_get_name ((GstDebugCategory *) a),
790       gst_debug_category_get_name ((GstDebugCategory *) b));
791 }
792
793 static void
794 gst_debug_help (void)
795 {
796   GSList *list, *walk;
797   GList *list2, *g;
798
799   /* Need to ensure the registry is loaded to get debug categories */
800   if (!init_post (NULL, NULL, NULL, NULL))
801     exit (1);
802
803   list2 = gst_registry_plugin_filter (gst_registry_get_default (),
804       select_all, FALSE, NULL);
805
806   /* FIXME this is gross.  why don't debug have categories PluginFeatures? */
807   for (g = list2; g; g = g_list_next (g)) {
808     GstPlugin *plugin = GST_PLUGIN_CAST (g->data);
809
810     gst_plugin_load (plugin);
811   }
812   g_list_free (list2);
813
814   list = gst_debug_get_all_categories ();
815   walk = list = g_slist_sort (list, sort_by_category_name);
816
817   g_print ("\n");
818   g_print ("name                  level    description\n");
819   g_print ("---------------------+--------+--------------------------------\n");
820
821   while (walk) {
822     GstDebugCategory *cat = (GstDebugCategory *) walk->data;
823
824     if (gst_debug_is_colored ()) {
825 #ifdef G_OS_WIN32
826       gint color = gst_debug_construct_win_color (cat->color);
827       const gint clear = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
828
829       SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), color);
830       g_print ("%-20s", gst_debug_category_get_name (cat));
831       SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), clear);
832       g_print (" %1d %s ", gst_debug_category_get_threshold (cat),
833           gst_debug_level_get_name (gst_debug_category_get_threshold (cat)));
834       SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), color);
835       g_print ("%s", gst_debug_category_get_description (cat));
836       SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), clear);
837       g_print ("\n");
838 #else /* G_OS_WIN32 */
839       gchar *color = gst_debug_construct_term_color (cat->color);
840
841       g_print ("%s%-20s\033[00m  %1d %s  %s%s\033[00m\n",
842           color,
843           gst_debug_category_get_name (cat),
844           gst_debug_category_get_threshold (cat),
845           gst_debug_level_get_name (gst_debug_category_get_threshold (cat)),
846           color, gst_debug_category_get_description (cat));
847       g_free (color);
848 #endif /* G_OS_WIN32 */
849     } else {
850       g_print ("%-20s  %1d %s  %s\n", gst_debug_category_get_name (cat),
851           gst_debug_category_get_threshold (cat),
852           gst_debug_level_get_name (gst_debug_category_get_threshold (cat)),
853           gst_debug_category_get_description (cat));
854     }
855     walk = g_slist_next (walk);
856   }
857   g_slist_free (list);
858   g_print ("\n");
859 }
860 #endif
861
862 #ifndef GST_DISABLE_OPTION_PARSING
863 static gboolean
864 parse_one_option (gint opt, const gchar * arg, GError ** err)
865 {
866   switch (opt) {
867     case ARG_VERSION:
868       g_print ("GStreamer Core Library version %s\n", PACKAGE_VERSION);
869       exit (0);
870     case ARG_FATAL_WARNINGS:{
871       GLogLevelFlags fatal_mask;
872
873       fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
874       fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
875       g_log_set_always_fatal (fatal_mask);
876       break;
877     }
878 #ifndef GST_DISABLE_GST_DEBUG
879     case ARG_DEBUG_LEVEL:{
880       gint tmp = 0;
881
882       tmp = strtol (arg, NULL, 0);
883       if (tmp >= 0 && tmp < GST_LEVEL_COUNT) {
884         gst_debug_set_default_threshold (tmp);
885       }
886       break;
887     }
888     case ARG_DEBUG:
889       parse_debug_list (arg);
890       break;
891     case ARG_DEBUG_NO_COLOR:
892       gst_debug_set_colored (FALSE);
893       break;
894     case ARG_DEBUG_DISABLE:
895       gst_debug_set_active (FALSE);
896       break;
897     case ARG_DEBUG_HELP:
898       gst_debug_help ();
899       exit (0);
900 #endif
901     case ARG_PLUGIN_SPEW:
902       break;
903     case ARG_PLUGIN_PATH:
904 #ifndef GST_DISABLE_REGISTRY
905       split_and_iterate (arg, G_SEARCHPATH_SEPARATOR_S, add_path_func, NULL);
906 #endif /* GST_DISABLE_REGISTRY */
907       break;
908     case ARG_PLUGIN_LOAD:
909       split_and_iterate (arg, ",", prepare_for_load_plugin_func, NULL);
910       break;
911     case ARG_SEGTRAP_DISABLE:
912       _gst_disable_segtrap = TRUE;
913       break;
914     case ARG_REGISTRY_UPDATE_DISABLE:
915 #ifndef GST_DISABLE_REGISTRY
916       _priv_gst_disable_registry_update = TRUE;
917 #endif
918       break;
919     case ARG_REGISTRY_FORK_DISABLE:
920       gst_registry_fork_set_enabled (FALSE);
921       break;
922     default:
923       g_set_error (err, G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
924           _("Unknown option"));
925       return FALSE;
926   }
927
928   return TRUE;
929 }
930
931 static gboolean
932 parse_goption_arg (const gchar * opt,
933     const gchar * arg, gpointer data, GError ** err)
934 {
935   static const struct
936   {
937     gchar *opt;
938     int val;
939   } options[] = {
940     {
941     "--gst-version", ARG_VERSION}, {
942     "--gst-fatal-warnings", ARG_FATAL_WARNINGS},
943 #ifndef GST_DISABLE_GST_DEBUG
944     {
945     "--gst-debug-level", ARG_DEBUG_LEVEL}, {
946     "--gst-debug", ARG_DEBUG}, {
947     "--gst-debug-disable", ARG_DEBUG_DISABLE}, {
948     "--gst-debug-no-color", ARG_DEBUG_NO_COLOR}, {
949     "--gst-debug-help", ARG_DEBUG_HELP},
950 #endif
951     {
952     "--gst-plugin-spew", ARG_PLUGIN_SPEW}, {
953     "--gst-plugin-path", ARG_PLUGIN_PATH}, {
954     "--gst-plugin-load", ARG_PLUGIN_LOAD}, {
955     "--gst-disable-segtrap", ARG_SEGTRAP_DISABLE}, {
956     "--gst-disable-registry-update", ARG_REGISTRY_UPDATE_DISABLE}, {
957     "--gst-disable-registry-fork", ARG_REGISTRY_FORK_DISABLE}, {
958     NULL}
959   };
960   gint val = 0, n;
961
962   for (n = 0; options[n].opt; n++) {
963     if (!strcmp (opt, options[n].opt)) {
964       val = options[n].val;
965       break;
966     }
967   }
968
969   return parse_one_option (val, arg, err);
970 }
971 #endif
972
973 /**
974  * gst_deinit:
975  *
976  * Clean up any resources created by GStreamer in gst_init().
977  *
978  * It is normally not needed to call this function in a normal application
979  * as the resources will automatically be freed when the program terminates.
980  * This function is therefore mostly used by testsuites and other memory
981  * profiling tools.
982  *
983  * After this call GStreamer (including this method) should not be used anymore. 
984  */
985 void
986 gst_deinit (void)
987 {
988   GstClock *clock;
989
990   GST_INFO ("deinitializing GStreamer");
991
992   if (gst_deinitialized) {
993     GST_DEBUG ("already deinitialized");
994     return;
995   }
996
997   g_slist_foreach (_priv_gst_preload_plugins, (GFunc) g_free, NULL);
998   g_slist_free (_priv_gst_preload_plugins);
999   _priv_gst_preload_plugins = NULL;
1000
1001 #ifndef GST_DISABLE_REGISTRY
1002   g_list_foreach (_priv_gst_plugin_paths, (GFunc) g_free, NULL);
1003   g_list_free (_priv_gst_plugin_paths);
1004   _priv_gst_plugin_paths = NULL;
1005 #endif
1006
1007   clock = gst_system_clock_obtain ();
1008   gst_object_unref (clock);
1009   gst_object_unref (clock);
1010
1011   _priv_gst_registry_cleanup ();
1012
1013   g_type_class_unref (g_type_class_peek (gst_object_get_type ()));
1014   g_type_class_unref (g_type_class_peek (gst_pad_get_type ()));
1015   g_type_class_unref (g_type_class_peek (gst_element_factory_get_type ()));
1016   g_type_class_unref (g_type_class_peek (gst_element_get_type ()));
1017   g_type_class_unref (g_type_class_peek (gst_type_find_factory_get_type ()));
1018   g_type_class_unref (g_type_class_peek (gst_bin_get_type ()));
1019   g_type_class_unref (g_type_class_peek (gst_bus_get_type ()));
1020   g_type_class_unref (g_type_class_peek (gst_task_get_type ()));
1021   g_type_class_unref (g_type_class_peek (gst_index_factory_get_type ()));
1022   g_type_class_unref (g_type_class_peek (gst_object_flags_get_type ()));
1023   g_type_class_unref (g_type_class_peek (gst_bin_flags_get_type ()));
1024   g_type_class_unref (g_type_class_peek (gst_buffer_flag_get_type ()));
1025   g_type_class_unref (g_type_class_peek (gst_buffer_copy_flags_get_type ()));
1026   g_type_class_unref (g_type_class_peek (gst_buffer_list_item_get_type ()));
1027   g_type_class_unref (g_type_class_peek (gst_bus_flags_get_type ()));
1028   g_type_class_unref (g_type_class_peek (gst_bus_sync_reply_get_type ()));
1029   g_type_class_unref (g_type_class_peek (gst_caps_flags_get_type ()));
1030   g_type_class_unref (g_type_class_peek (gst_clock_type_get_type ()));
1031   g_type_class_unref (g_type_class_peek (gst_clock_return_get_type ()));
1032   g_type_class_unref (g_type_class_peek (gst_clock_entry_type_get_type ()));
1033   g_type_class_unref (g_type_class_peek (gst_clock_flags_get_type ()));
1034   g_type_class_unref (g_type_class_peek (gst_debug_graph_details_get_type ()));
1035   g_type_class_unref (g_type_class_peek (gst_state_get_type ()));
1036   g_type_class_unref (g_type_class_peek (gst_state_change_return_get_type ()));
1037   g_type_class_unref (g_type_class_peek (gst_state_change_get_type ()));
1038   g_type_class_unref (g_type_class_peek (gst_element_flags_get_type ()));
1039   g_type_class_unref (g_type_class_peek (gst_core_error_get_type ()));
1040   g_type_class_unref (g_type_class_peek (gst_library_error_get_type ()));
1041   g_type_class_unref (g_type_class_peek (gst_plugin_dependency_flags_get_type
1042           ()));
1043   g_type_class_unref (g_type_class_peek (gst_parse_flags_get_type ()));
1044   g_type_class_unref (g_type_class_peek (gst_resource_error_get_type ()));
1045   g_type_class_unref (g_type_class_peek (gst_search_mode_get_type ()));
1046   g_type_class_unref (g_type_class_peek (gst_stream_error_get_type ()));
1047   g_type_class_unref (g_type_class_peek (gst_stream_status_type_get_type ()));
1048   g_type_class_unref (g_type_class_peek (gst_structure_change_type_get_type
1049           ()));
1050   g_type_class_unref (g_type_class_peek (gst_event_type_flags_get_type ()));
1051   g_type_class_unref (g_type_class_peek (gst_event_type_get_type ()));
1052   g_type_class_unref (g_type_class_peek (gst_seek_type_get_type ()));
1053   g_type_class_unref (g_type_class_peek (gst_seek_flags_get_type ()));
1054   g_type_class_unref (g_type_class_peek (gst_format_get_type ()));
1055   g_type_class_unref (g_type_class_peek (gst_index_certainty_get_type ()));
1056   g_type_class_unref (g_type_class_peek (gst_index_entry_type_get_type ()));
1057   g_type_class_unref (g_type_class_peek (gst_index_lookup_method_get_type ()));
1058   g_type_class_unref (g_type_class_peek (gst_assoc_flags_get_type ()));
1059   g_type_class_unref (g_type_class_peek (gst_index_resolver_method_get_type
1060           ()));
1061   g_type_class_unref (g_type_class_peek (gst_index_flags_get_type ()));
1062   g_type_class_unref (g_type_class_peek (gst_debug_level_get_type ()));
1063   g_type_class_unref (g_type_class_peek (gst_debug_color_flags_get_type ()));
1064   g_type_class_unref (g_type_class_peek (gst_iterator_result_get_type ()));
1065   g_type_class_unref (g_type_class_peek (gst_iterator_item_get_type ()));
1066   g_type_class_unref (g_type_class_peek (gst_message_type_get_type ()));
1067   g_type_class_unref (g_type_class_peek (gst_mini_object_flags_get_type ()));
1068   g_type_class_unref (g_type_class_peek (gst_pad_link_return_get_type ()));
1069   g_type_class_unref (g_type_class_peek (gst_flow_return_get_type ()));
1070   g_type_class_unref (g_type_class_peek (gst_activate_mode_get_type ()));
1071   g_type_class_unref (g_type_class_peek (gst_pad_direction_get_type ()));
1072   g_type_class_unref (g_type_class_peek (gst_pad_flags_get_type ()));
1073   g_type_class_unref (g_type_class_peek (gst_pad_presence_get_type ()));
1074   g_type_class_unref (g_type_class_peek (gst_pad_template_flags_get_type ()));
1075   g_type_class_unref (g_type_class_peek (gst_pipeline_flags_get_type ()));
1076   g_type_class_unref (g_type_class_peek (gst_plugin_error_get_type ()));
1077   g_type_class_unref (g_type_class_peek (gst_plugin_flags_get_type ()));
1078   g_type_class_unref (g_type_class_peek (gst_rank_get_type ()));
1079   g_type_class_unref (g_type_class_peek (gst_query_type_get_type ()));
1080   g_type_class_unref (g_type_class_peek (gst_buffering_mode_get_type ()));
1081   g_type_class_unref (g_type_class_peek (gst_tag_merge_mode_get_type ()));
1082   g_type_class_unref (g_type_class_peek (gst_tag_flag_get_type ()));
1083   g_type_class_unref (g_type_class_peek (gst_task_state_get_type ()));
1084   g_type_class_unref (g_type_class_peek (gst_alloc_trace_flags_get_type ()));
1085   g_type_class_unref (g_type_class_peek (gst_type_find_probability_get_type
1086           ()));
1087   g_type_class_unref (g_type_class_peek (gst_uri_type_get_type ()));
1088   g_type_class_unref (g_type_class_peek (gst_parse_error_get_type ()));
1089   g_type_class_unref (g_type_class_peek (gst_param_spec_fraction_get_type ()));
1090
1091   gst_deinitialized = TRUE;
1092   GST_INFO ("deinitialized GStreamer");
1093 }
1094
1095 /**
1096  * gst_version:
1097  * @major: pointer to a guint to store the major version number
1098  * @minor: pointer to a guint to store the minor version number
1099  * @micro: pointer to a guint to store the micro version number
1100  * @nano:  pointer to a guint to store the nano version number
1101  *
1102  * Gets the version number of the GStreamer library.
1103  */
1104 void
1105 gst_version (guint * major, guint * minor, guint * micro, guint * nano)
1106 {
1107   g_return_if_fail (major);
1108   g_return_if_fail (minor);
1109   g_return_if_fail (micro);
1110   g_return_if_fail (nano);
1111
1112   *major = GST_VERSION_MAJOR;
1113   *minor = GST_VERSION_MINOR;
1114   *micro = GST_VERSION_MICRO;
1115   *nano = GST_VERSION_NANO;
1116 }
1117
1118 /**
1119  * gst_version_string:
1120  *
1121  * This function returns a string that is useful for describing this version
1122  * of GStreamer to the outside world: user agent strings, logging, ...
1123  *
1124  * Returns: a newly allocated string describing this version of GStreamer.
1125  */
1126
1127 gchar *
1128 gst_version_string ()
1129 {
1130   guint major, minor, micro, nano;
1131
1132   gst_version (&major, &minor, &micro, &nano);
1133   if (nano == 0)
1134     return g_strdup_printf ("GStreamer %d.%d.%d", major, minor, micro);
1135   else if (nano == 1)
1136     return g_strdup_printf ("GStreamer %d.%d.%d (GIT)", major, minor, micro);
1137   else
1138     return g_strdup_printf ("GStreamer %d.%d.%d (prerelease)", major, minor,
1139         micro);
1140 }
1141
1142 /**
1143  * gst_segtrap_is_enabled:
1144  *
1145  * Some functions in the GStreamer core might install a custom SIGSEGV handler
1146  * to better catch and report errors to the application. Currently this feature
1147  * is enabled by default when loading plugins.
1148  *
1149  * Applications might want to disable this behaviour with the
1150  * gst_segtrap_set_enabled() function. This is typically done if the application
1151  * wants to install its own handler without GStreamer interfering.
1152  *
1153  * Returns: %TRUE if GStreamer is allowed to install a custom SIGSEGV handler.
1154  *
1155  * Since: 0.10.10
1156  */
1157 gboolean
1158 gst_segtrap_is_enabled (void)
1159 {
1160   /* yeps, it's enabled when it's not disabled */
1161   return !_gst_disable_segtrap;
1162 }
1163
1164 /**
1165  * gst_segtrap_set_enabled:
1166  * @enabled: whether a custom SIGSEGV handler should be installed.
1167  *
1168  * Applications might want to disable/enable the SIGSEGV handling of
1169  * the GStreamer core. See gst_segtrap_is_enabled() for more information.
1170  *
1171  * Since: 0.10.10
1172  */
1173 void
1174 gst_segtrap_set_enabled (gboolean enabled)
1175 {
1176   _gst_disable_segtrap = !enabled;
1177 }