gst.c 42.6 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>
 *
 * gst.c: Initialization and non-pipeline operations
Erik Walthinsen's avatar
Erik Walthinsen committed
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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.
 *
 * 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
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
19 20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Erik Walthinsen's avatar
Erik Walthinsen committed
21
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
22

23 24
/**
 * SECTION:gst
25
 * @title: GStreamer
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
26 27
 * @short_description: Media library supporting arbitrary formats and filter
 *                     graphs.
28 29
 *
 * GStreamer is a framework for constructing graphs of various filters
Olivier Crête's avatar
Olivier Crête committed
30
 * (termed elements here) that will handle streaming media.  Any discrete
31 32 33 34 35
 * (packetizable) media type is supported, with provisions for automatically
 * determining source type.  Formatting/framing information is provided with
 * a powerful negotiation framework.  Plugins are heavily used to provide for
 * all elements, allowing one to construct plugins outside of the GST
 * library, even released binary-only if license require (please don't).
36 37
 * GStreamer covers a wide range of use cases including: playback, recording,
 * editing, serving streams, voice over ip and video calls.
38 39 40 41 42 43
 *
 * The <application>GStreamer</application> library should be initialized with
 * gst_init() before it can be used. You should pass pointers to the main argc
 * and argv variables so that GStreamer can process its own command line
 * options, as shown in the following example.
 *
44 45 46
 * ## Initializing the gstreamer library
 *
 * |[ <!-- language="C" -->
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
47
 * int
48 49 50 51 52 53
 * main (int argc, char *argv[])
 * {
 *   // initialize the GStreamer library
 *   gst_init (&amp;argc, &amp;argv);
 *   ...
 * }
54
 * ]|
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
55
 *
56
 * It's allowed to pass two %NULL pointers to gst_init() in case you don't want
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
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 62 63
 *
 * ## Initializing own parameters when initializing gstreamer
 * |[ <!-- language="C" -->
64 65
 * static gboolean stats = FALSE;
 * ...
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
66
 * int
67 68
 * main (int argc, char *argv[])
 * {
69 70 71 72 73
 *  GOptionEntry options[] = {
 *   {"tags", 't', 0, G_OPTION_ARG_NONE, &amp;tags,
 *       N_("Output tags (also known as metadata)"), NULL},
 *   {NULL}
 *  };
74
 *  ctx = g_option_context_new ("[ADDITIONAL ARGUMENTS]");
75 76 77
 *  g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
 *  g_option_context_add_group (ctx, gst_init_get_option_group ());
 *  if (!g_option_context_parse (ctx, &amp;argc, &amp;argv, &amp;err)) {
78
 *    g_print ("Error initializing: &percnt;s\n", GST_STR_NULL (err->message));
79 80 81 82
 *    exit (1);
 *  }
 *  g_option_context_free (ctx);
 * ...
83
 * }
84
 * ]|
85
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
86
 * Use gst_version() to query the library version at runtime or use the
Wim Taymans's avatar
Wim Taymans committed
87 88
 * GST_VERSION_* macros to find the version at compile time. Optionally
 * gst_version_string() returns a printable string.
89
 *
Wim Taymans's avatar
Wim Taymans committed
90
 * The gst_deinit() call is used to clean up all internal resources used
91
 * by GStreamer. It is mostly used in unit tests to check for leaks.
92
 */
Erik Walthinsen's avatar
Erik Walthinsen committed
93

94
#include "gst_private.h"
95
#include "gstconfig.h"
Wim Taymans's avatar
Wim Taymans committed
96
#include <stdlib.h>
97
#include <stdio.h>
98
#include <sys/types.h>
99 100 101
#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif
102
#ifdef HAVE_UNISTD_H
103
#include <unistd.h>
104
#endif
105 106 107 108
#ifdef G_OS_WIN32
#define WIN32_LEAN_AND_MEAN     /* prevents from including too many things */
#include <windows.h>            /* GetStdHandle, windows console */
#endif
Wim Taymans's avatar
Wim Taymans committed
109

110
#include "gst-i18n-lib.h"
111
#include <locale.h>             /* for LC_ALL */
112

113
#include "gst.h"
Erik Walthinsen's avatar
Erik Walthinsen committed
114

115 116
#define GST_CAT_DEFAULT GST_CAT_GST_INIT

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
117
#define MAX_PATH_SPLIT  16
118
#define GST_PLUGIN_SEPARATOR ","
119

120
static gboolean gst_initialized = FALSE;
121
static gboolean gst_deinitialized = FALSE;
122

123 124
GstClockTime _priv_gst_start_time;

125
#ifdef G_OS_WIN32
Jan Schmidt's avatar
Jan Schmidt committed
126
HMODULE _priv_gst_dll_handle = NULL;
127 128
#endif

129
#ifndef GST_DISABLE_REGISTRY
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
130
GList *_priv_gst_plugin_paths = NULL;   /* for delayed processing in init_post */
Jan Schmidt's avatar
Jan Schmidt committed
131

132
extern gboolean _priv_gst_disable_registry;
Jan Schmidt's avatar
Jan Schmidt committed
133
extern gboolean _priv_gst_disable_registry_update;
134
#endif
135

136
#ifndef GST_DISABLE_GST_DEBUG
137
const gchar *priv_gst_dump_dot_dir;
138 139
#endif

Wim Taymans's avatar
Wim Taymans committed
140 141
/* defaults */

142 143
/* set to TRUE when segfaults need to be left as is */
static gboolean _gst_disable_segtrap = FALSE;
144

145 146 147 148
static gboolean init_pre (GOptionContext * context, GOptionGroup * group,
    gpointer data, GError ** error);
static gboolean init_post (GOptionContext * context, GOptionGroup * group,
    gpointer data, GError ** error);
149
#ifndef GST_DISABLE_OPTION_PARSING
150 151
static gboolean parse_goption_arg (const gchar * s_opt,
    const gchar * arg, gpointer data, GError ** err);
152
#endif
153

Jan Schmidt's avatar
Jan Schmidt committed
154
GSList *_priv_gst_preload_plugins = NULL;
155

156
const gchar g_log_domain_gstreamer[] = "GStreamer";
157 158

static void
159 160
debug_log_handler (const gchar * log_domain,
    GLogLevelFlags log_level, const gchar * message, gpointer user_data)
161
{
162
  g_log_default_handler (log_domain, log_level, message, user_data);
163 164 165
  /* FIXME: do we still need this ? fatal errors these days are all
   * other than core errors */
  /* g_on_error_query (NULL); */
166 167
}

168 169 170
enum
{
  ARG_VERSION = 1,
171
  ARG_FATAL_WARNINGS,
172
#ifndef GST_DISABLE_GST_DEBUG
173 174 175 176
  ARG_DEBUG_LEVEL,
  ARG_DEBUG,
  ARG_DEBUG_DISABLE,
  ARG_DEBUG_NO_COLOR,
LRN's avatar
LRN committed
177
  ARG_DEBUG_COLOR_MODE,
178
  ARG_DEBUG_HELP,
Benjamin Otte's avatar
Benjamin Otte committed
179
#endif
180 181 182
  ARG_PLUGIN_SPEW,
  ARG_PLUGIN_PATH,
  ARG_PLUGIN_LOAD,
Wim Taymans's avatar
Wim Taymans committed
183
  ARG_SEGTRAP_DISABLE,
184
  ARG_REGISTRY_UPDATE_DISABLE,
Wim Taymans's avatar
Wim Taymans committed
185
  ARG_REGISTRY_FORK_DISABLE
186 187
};

188 189 190 191 192 193
/* debug-spec ::= category-spec [, category-spec]*
 * category-spec ::= category:val | val
 * category ::= [^:]+
 * val ::= [0-5]
 */

194
#ifdef G_OS_WIN32
195 196 197 198 199 200 201 202 203 204 205
/* Note: DllMain is only called when DLLs are loaded or unloaded, so this will
 * never be called if libgstreamer-1.0 is linked statically. Do not add any code
 * here to, say, initialize variables or set things up since that will only
 * happen for dynamically-built GStreamer.
 *
 * Also, ideally this should not be defined when GStreamer is built statically.
 * i.e., it should be conditional on #ifdef DLL_EXPORT. It will be ignored, but
 * if other libraries make the same mistake of defining it when building
 * statically, there will be a symbol collision during linking. Fixing this
 * requires one to build two object files: one for static linking and another
 * for dynamic linking. */
206
BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
207 208
BOOL WINAPI
DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
209 210
{
  if (fdwReason == DLL_PROCESS_ATTACH)
Jan Schmidt's avatar
Jan Schmidt committed
211
    _priv_gst_dll_handle = (HMODULE) hinstDLL;
212 213 214 215 216
  return TRUE;
}

#endif

217
/**
218
 * gst_init_get_option_group: (skip)
219
 *
220 221 222 223
 * Returns a #GOptionGroup with GStreamer's argument specifications. The
 * group is set up to use standard GOption callbacks, so when using this
 * group in combination with GOption parsing methods, all argument parsing
 * and initialization is automated.
224
 *
225
 * This function is useful if you want to integrate GStreamer with other
226
 * libraries that use GOption (see g_option_context_add_group() ).
227
 *
228 229 230 231
 * If you use this function, you should make sure you initialise the GLib
 * threading system as one of the very first things in your program
 * (see the example at the beginning of this section).
 *
232
 * Returns: (transfer full): a pointer to GStreamer's option group.
233
 */
234

235 236 237
GOptionGroup *
gst_init_get_option_group (void)
{
238
#ifndef GST_DISABLE_OPTION_PARSING
239
  GOptionGroup *group;
240
  static const GOptionEntry gst_args[] = {
241
    {"gst-version", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
242
        (gpointer) parse_goption_arg, N_("Print the GStreamer version"), NULL},
243
    {"gst-fatal-warnings", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
244
        (gpointer) parse_goption_arg, N_("Make all warnings fatal"), NULL},
245
#ifndef GST_DISABLE_GST_DEBUG
246
    {"gst-debug-help", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
247 248
          (gpointer) parse_goption_arg,
          N_("Print available debug categories and exit"),
249
        NULL},
250 251
    {"gst-debug-level", 0, 0, G_OPTION_ARG_CALLBACK,
          (gpointer) parse_goption_arg,
252
          N_("Default debug level from 1 (only error) to 9 (anything) or "
253 254
              "0 for no output"),
        N_("LEVEL")},
255
    {"gst-debug", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) parse_goption_arg,
256 257 258 259
          N_("Comma-separated list of category_name:level pairs to set "
              "specific levels for the individual categories. Example: "
              "GST_AUTOPLUG:5,GST_ELEMENT_*:3"),
        N_("LIST")},
260
    {"gst-debug-no-color", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
261 262
          (gpointer) parse_goption_arg, N_("Disable colored debugging output"),
        NULL},
LRN's avatar
LRN committed
263 264 265 266 267
    {"gst-debug-color-mode", 0, 0, G_OPTION_ARG_CALLBACK,
          (gpointer) parse_goption_arg,
          N_("Changes coloring mode of the debug log. "
              "Possible modes: off, on, disable, auto, unix"),
        NULL},
268
    {"gst-debug-disable", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
269
        (gpointer) parse_goption_arg, N_("Disable debugging"), NULL},
270
#endif
271
    {"gst-plugin-spew", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
272 273
          (gpointer) parse_goption_arg,
          N_("Enable verbose plugin loading diagnostics"),
274
        NULL},
275 276
    {"gst-plugin-path", 0, 0, G_OPTION_ARG_CALLBACK,
          (gpointer) parse_goption_arg,
277
        N_("Colon-separated paths containing plugins"), N_("PATHS")},
278 279
    {"gst-plugin-load", 0, 0, G_OPTION_ARG_CALLBACK,
          (gpointer) parse_goption_arg,
280
          N_("Comma-separated list of plugins to preload in addition to the "
281
              "list stored in environment variable GST_PLUGIN_PATH"),
282
        N_("PLUGINS")},
283
    {"gst-disable-segtrap", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
284
          (gpointer) parse_goption_arg,
285 286
          N_("Disable trapping of segmentation faults during plugin loading"),
        NULL},
287 288 289 290 291
    {"gst-disable-registry-update", 0, G_OPTION_FLAG_NO_ARG,
          G_OPTION_ARG_CALLBACK,
          (gpointer) parse_goption_arg,
          N_("Disable updating the registry"),
        NULL},
Wim Taymans's avatar
Wim Taymans committed
292 293
    {"gst-disable-registry-fork", 0, G_OPTION_FLAG_NO_ARG,
          G_OPTION_ARG_CALLBACK,
294
          (gpointer) parse_goption_arg,
Jan Schmidt's avatar
Jan Schmidt committed
295
          N_("Disable spawning a helper process while scanning the registry"),
Wim Taymans's avatar
Wim Taymans committed
296
        NULL},
297
    {NULL}
298 299
  };

300 301 302 303 304 305 306
  group = g_option_group_new ("gst", _("GStreamer Options"),
      _("Show GStreamer Options"), NULL, NULL);
  g_option_group_set_parse_hooks (group, (GOptionParseFunc) init_pre,
      (GOptionParseFunc) init_post);

  g_option_group_add_entries (group, gst_args);
  g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
307

308
  return group;
309 310 311
#else
  return NULL;
#endif
312 313
}

314 315
/**
 * gst_init_check:
316
 * @argc: (inout) (allow-none): pointer to application's argc
317
 * @argv: (inout) (array length=argc) (allow-none): pointer to application's argv
318
 * @err: pointer to a #GError to which a message will be posted on error
319 320 321 322 323 324 325
 *
 * Initializes the GStreamer library, setting up internal path lists,
 * registering built-in elements, and loading standard plugins.
 *
 * This function will return %FALSE if GStreamer could not be initialized
 * for some reason.  If you want your program to fail fatally,
 * use gst_init() instead.
Wim Taymans's avatar
Wim Taymans committed
326
 *
327
 * Returns: %TRUE if GStreamer could be initialized.
328 329
 */
gboolean
330
gst_init_check (int *argc, char **argv[], GError ** err)
331
{
332
  static GMutex init_lock;
333
#ifndef GST_DISABLE_OPTION_PARSING
334 335
  GOptionGroup *group;
  GOptionContext *ctx;
336
#endif
337 338
  gboolean res;

339 340
  g_mutex_lock (&init_lock);

341 342
  if (gst_initialized) {
    GST_DEBUG ("already initialized gst");
343
    g_mutex_unlock (&init_lock);
344 345
    return TRUE;
  }
346
#ifndef GST_DISABLE_OPTION_PARSING
347
  ctx = g_option_context_new ("- GStreamer initialization");
348
  g_option_context_set_ignore_unknown_options (ctx, TRUE);
349
  g_option_context_set_help_enabled (ctx, FALSE);
350 351 352 353
  group = gst_init_get_option_group ();
  g_option_context_add_group (ctx, group);
  res = g_option_context_parse (ctx, argc, argv, err);
  g_option_context_free (ctx);
354 355 356 357 358
#else
  init_pre (NULL, NULL, NULL, NULL);
  init_post (NULL, NULL, NULL, NULL);
  res = TRUE;
#endif
359

360 361
  gst_initialized = res;

362 363
  g_mutex_unlock (&init_lock);

364
  return res;
365 366
}

Erik Walthinsen's avatar
Erik Walthinsen committed
367 368
/**
 * gst_init:
369
 * @argc: (inout) (allow-none): pointer to application's argc
370
 * @argv: (inout) (array length=argc) (allow-none): pointer to application's argv
Erik Walthinsen's avatar
Erik Walthinsen committed
371
 *
372 373
 * Initializes the GStreamer library, setting up internal path lists,
 * registering built-in elements, and loading standard plugins.
374
 *
375 376 377 378 379 380 381
 * Unless the plugin registry is disabled at compile time, the registry will be
 * loaded. By default this will also check if the registry cache needs to be
 * updated and rescan all plugins if needed. See gst_update_registry() for
 * details and section
 * <link linkend="gst-running">Running GStreamer Applications</link>
 * for how to disable automatic registry updates.
 *
382 383 384
 * > This function will terminate your program if it was unable to initialize
 * > GStreamer for some reason.  If you want your program to fall back,
 * > use gst_init_check() instead.
385 386
 *
 * WARNING: This function does not work in the same way as corresponding
387
 * functions in other glib-style libraries, such as gtk_init\(\). In
388 389
 * particular, unknown command line options cause this function to
 * abort program execution.
Erik Walthinsen's avatar
Erik Walthinsen committed
390
 */
391
void
Benjamin Otte's avatar
Benjamin Otte committed
392 393
gst_init (int *argc, char **argv[])
{
394
  GError *err = NULL;
395

396
  if (!gst_init_check (argc, argv, &err)) {
397
    g_print ("Could not initialize GStreamer: %s\n",
398 399 400
        err ? err->message : "unknown error occurred");
    if (err) {
      g_error_free (err);
401
    }
402
    exit (1);
403 404 405
  }
}

406 407 408 409 410 411
/**
 * gst_is_initialized:
 *
 * Use this function to check if GStreamer has been initialized with gst_init()
 * or gst_init_check().
 *
412
 * Returns: %TRUE if initialization has been done, %FALSE otherwise.
413 414 415 416 417 418 419
 */
gboolean
gst_is_initialized (void)
{
  return gst_initialized;
}

Wim Taymans's avatar
Wim Taymans committed
420
#ifndef GST_DISABLE_REGISTRY
421
static void
422 423
add_path_func (gpointer data, gpointer user_data)
{
424
  GST_INFO ("Adding plugin path: \"%s\", will scan later", (gchar *) data);
Jan Schmidt's avatar
Jan Schmidt committed
425 426
  _priv_gst_plugin_paths =
      g_list_append (_priv_gst_plugin_paths, g_strdup (data));
427
}
Wim Taymans's avatar
Wim Taymans committed
428
#endif
429

430
#ifndef GST_DISABLE_OPTION_PARSING
431
static void
432 433
prepare_for_load_plugin_func (gpointer data, gpointer user_data)
{
Jan Schmidt's avatar
Jan Schmidt committed
434 435
  _priv_gst_preload_plugins =
      g_slist_prepend (_priv_gst_preload_plugins, g_strdup (data));
436
}
437
#endif
438

439
#ifndef GST_DISABLE_OPTION_PARSING
440
static void
Benjamin Otte's avatar
Benjamin Otte committed
441
split_and_iterate (const gchar * stringlist, const gchar * separator,
442
    GFunc iterator, gpointer user_data)
443 444 445 446
{
  gchar **strings;
  gint j = 0;
  gchar *lastlist = g_strdup (stringlist);
Erik Walthinsen's avatar
Erik Walthinsen committed
447

448 449 450 451
  while (lastlist) {
    strings = g_strsplit (lastlist, separator, MAX_PATH_SPLIT);
    g_free (lastlist);
    lastlist = NULL;
452

453
    while (strings[j]) {
454
      iterator (strings[j], user_data);
455
      if (++j == MAX_PATH_SPLIT) {
456 457 458
        lastlist = g_strdup (strings[j]);
        j = 0;
        break;
459 460
      }
    }
461
    g_strfreev (strings);
Wim Taymans's avatar
Wim Taymans committed
462
  }
463
}
464
#endif
Wim Taymans's avatar
Wim Taymans committed
465

466 467
/* we have no fail cases yet, but maybe in the future */
static gboolean
468 469
init_pre (GOptionContext * context, GOptionGroup * group, gpointer data,
    GError ** error)
470
{
471
  gchar *libdir;
472 473 474 475
  if (gst_initialized) {
    GST_DEBUG ("already initialized");
    return TRUE;
  }
476 477 478

  _priv_gst_start_time = gst_util_get_timestamp ();

479 480
#ifndef GST_DISABLE_GST_DEBUG
  _priv_gst_debug_init ();
481
  priv_gst_dump_dot_dir = g_getenv ("GST_DEBUG_DUMP_DOT_DIR");
482
#endif
483 484

#ifdef ENABLE_NLS
485
  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
486
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
487
#endif /* ENABLE_NLS */
488

489 490
  /* This is the earliest we can make stuff show up in the logs.
   * So give some useful info about GStreamer here */
491 492
#ifdef G_OS_WIN32
  {
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
493 494 495
    gchar *basedir =
        g_win32_get_package_installation_directory_of_module
        (_priv_gst_dll_handle);
496 497 498

    libdir = g_build_filename (basedir,
#ifdef _DEBUG
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
499
        "debug"
500
#endif
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
501
        "lib", NULL);
502 503 504 505 506
    g_free (basedir);
  }
#else
  libdir = g_strdup (LIBDIR);
#endif
507
  GST_INFO ("Initializing GStreamer Core Library version %s", VERSION);
508 509
  GST_INFO ("Using library installed in %s", libdir);
  g_free (libdir);
510

511 512 513 514 515 516 517 518 519
#ifndef GST_DISABLE_REGISTRY
  {
    const gchar *disable_registry;
    if ((disable_registry = g_getenv ("GST_REGISTRY_DISABLE"))) {
      _priv_gst_disable_registry = (strcmp (disable_registry, "yes") == 0);
    }
  }
#endif

520 521 522 523 524 525 526 527 528 529 530 531 532
  /* Print some basic system details if possible (OS/architecture) */
#ifdef HAVE_SYS_UTSNAME_H
  {
    struct utsname sys_details;

    if (uname (&sys_details) == 0) {
      GST_INFO ("%s %s %s %s %s", sys_details.sysname,
          sys_details.nodename, sys_details.release, sys_details.version,
          sys_details.machine);
    }
  }
#endif

533 534 535 536 537
#ifndef G_ATOMIC_LOCK_FREE
  GST_CAT_WARNING (GST_CAT_PERFORMANCE, "GLib atomic operations are NOT "
      "implemented using real hardware atomic operations!");
#endif

538
  return TRUE;
539 540 541
}

static gboolean
542
gst_register_core_elements (GstPlugin * plugin)
543 544
{
  /* register some standard builtin types */
545 546 547
  if (!gst_element_register (plugin, "bin", GST_RANK_PRIMARY,
          GST_TYPE_BIN) ||
      !gst_element_register (plugin, "pipeline", GST_RANK_PRIMARY,
548
          GST_TYPE_PIPELINE)
Wim Taymans's avatar
Wim Taymans committed
549
      )
550
    g_assert_not_reached ();
551 552

  return TRUE;
553 554
}

555 556 557 558 559 560 561
/*
 * this bit handles:
 * - initalization of threads if we use them
 * - log handler
 * - initial output
 * - initializes gst_format
 * - registers a bunch of types for gst_objects
562 563 564
 *
 * - we don't have cases yet where this fails, but in the future
 *   we might and then it's nice to be able to return that
565
 */
566
static gboolean
567 568
init_post (GOptionContext * context, GOptionGroup * group, gpointer data,
    GError ** error)
569 570
{
  GLogLevelFlags llf;
571

572 573 574 575 576
  if (gst_initialized) {
    GST_DEBUG ("already initialized");
    return TRUE;
  }

577
  llf = G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR | G_LOG_FLAG_FATAL;
578
  g_log_set_handler (g_log_domain_gstreamer, llf, debug_log_handler, NULL);
579

580
  _priv_gst_mini_object_initialize ();
581
  _priv_gst_quarks_initialize ();
582
  _priv_gst_allocator_initialize ();
Wim Taymans's avatar
Wim Taymans committed
583
  _priv_gst_memory_initialize ();
584 585 586 587
  _priv_gst_format_initialize ();
  _priv_gst_query_initialize ();
  _priv_gst_structure_initialize ();
  _priv_gst_caps_initialize ();
588
  _priv_gst_caps_features_initialize ();
Wim Taymans's avatar
Wim Taymans committed
589
  _priv_gst_meta_initialize ();
590
  _priv_gst_message_initialize ();
Wim Taymans's avatar
Wim Taymans committed
591

592 593 594 595
  g_type_class_ref (gst_object_get_type ());
  g_type_class_ref (gst_pad_get_type ());
  g_type_class_ref (gst_element_factory_get_type ());
  g_type_class_ref (gst_element_get_type ());
596
  g_type_class_ref (gst_tracer_factory_get_type ());
597 598
  g_type_class_ref (gst_type_find_factory_get_type ());
  g_type_class_ref (gst_bin_get_type ());
599 600
  g_type_class_ref (gst_bus_get_type ());
  g_type_class_ref (gst_task_get_type ());
601
  g_type_class_ref (gst_clock_get_type ());
602
  g_type_class_ref (gst_debug_color_mode_get_type ());
603

604
  gst_uri_handler_get_type ();
605

606 607
  g_type_class_ref (gst_object_flags_get_type ());
  g_type_class_ref (gst_bin_flags_get_type ());
608
  g_type_class_ref (gst_buffer_flags_get_type ());
609 610 611 612 613 614 615
  g_type_class_ref (gst_buffer_copy_flags_get_type ());
  g_type_class_ref (gst_bus_flags_get_type ());
  g_type_class_ref (gst_bus_sync_reply_get_type ());
  g_type_class_ref (gst_caps_flags_get_type ());
  g_type_class_ref (gst_clock_return_get_type ());
  g_type_class_ref (gst_clock_entry_type_get_type ());
  g_type_class_ref (gst_clock_flags_get_type ());
616
  g_type_class_ref (gst_clock_type_get_type ());
617 618 619 620 621
  g_type_class_ref (gst_debug_graph_details_get_type ());
  g_type_class_ref (gst_state_get_type ());
  g_type_class_ref (gst_state_change_return_get_type ());
  g_type_class_ref (gst_state_change_get_type ());
  g_type_class_ref (gst_element_flags_get_type ());
622 623
  g_type_class_ref (gst_tracer_value_scope_get_type ());
  g_type_class_ref (gst_tracer_value_flags_get_type ());
624 625 626 627 628 629 630 631
  g_type_class_ref (gst_core_error_get_type ());
  g_type_class_ref (gst_library_error_get_type ());
  g_type_class_ref (gst_resource_error_get_type ());
  g_type_class_ref (gst_stream_error_get_type ());
  g_type_class_ref (gst_event_type_flags_get_type ());
  g_type_class_ref (gst_event_type_get_type ());
  g_type_class_ref (gst_seek_type_get_type ());
  g_type_class_ref (gst_seek_flags_get_type ());
Wim Taymans's avatar
Wim Taymans committed
632
  g_type_class_ref (gst_qos_type_get_type ());
633 634 635 636 637 638 639 640
  g_type_class_ref (gst_format_get_type ());
  g_type_class_ref (gst_debug_level_get_type ());
  g_type_class_ref (gst_debug_color_flags_get_type ());
  g_type_class_ref (gst_iterator_result_get_type ());
  g_type_class_ref (gst_iterator_item_get_type ());
  g_type_class_ref (gst_message_type_get_type ());
  g_type_class_ref (gst_mini_object_flags_get_type ());
  g_type_class_ref (gst_pad_link_return_get_type ());
641
  g_type_class_ref (gst_pad_link_check_get_type ());
642
  g_type_class_ref (gst_flow_return_get_type ());
Wim Taymans's avatar
Wim Taymans committed
643
  g_type_class_ref (gst_pad_mode_get_type ());
644 645 646 647 648 649 650
  g_type_class_ref (gst_pad_direction_get_type ());
  g_type_class_ref (gst_pad_flags_get_type ());
  g_type_class_ref (gst_pad_presence_get_type ());
  g_type_class_ref (gst_pad_template_flags_get_type ());
  g_type_class_ref (gst_pipeline_flags_get_type ());
  g_type_class_ref (gst_plugin_error_get_type ());
  g_type_class_ref (gst_plugin_flags_get_type ());
651
  g_type_class_ref (gst_plugin_dependency_flags_get_type ());
652
  g_type_class_ref (gst_rank_get_type ());
653
  g_type_class_ref (gst_query_type_flags_get_type ());
654 655
  g_type_class_ref (gst_query_type_get_type ());
  g_type_class_ref (gst_buffering_mode_get_type ());
656 657
  g_type_class_ref (gst_stream_status_type_get_type ());
  g_type_class_ref (gst_structure_change_type_get_type ());
658 659
  g_type_class_ref (gst_tag_merge_mode_get_type ());
  g_type_class_ref (gst_tag_flag_get_type ());
660
  g_type_class_ref (gst_tag_scope_get_type ());
661
  g_type_class_ref (gst_task_pool_get_type ());
662
  g_type_class_ref (gst_task_state_get_type ());
663
  g_type_class_ref (gst_toc_entry_type_get_type ());
664
  g_type_class_ref (gst_type_find_probability_get_type ());
665
  g_type_class_ref (gst_uri_error_get_type ());
666 667
  g_type_class_ref (gst_uri_type_get_type ());
  g_type_class_ref (gst_parse_error_get_type ());
668
  g_type_class_ref (gst_parse_flags_get_type ());
669
  g_type_class_ref (gst_search_mode_get_type ());
Wim Taymans's avatar
Wim Taymans committed
670
  g_type_class_ref (gst_progress_type_get_type ());
671
  g_type_class_ref (gst_buffer_pool_acquire_flags_get_type ());
Wim Taymans's avatar
Wim Taymans committed
672 673
  g_type_class_ref (gst_memory_flags_get_type ());
  g_type_class_ref (gst_map_flags_get_type ());
674
  g_type_class_ref (gst_caps_intersect_mode_get_type ());
675 676
  g_type_class_ref (gst_pad_probe_type_get_type ());
  g_type_class_ref (gst_pad_probe_return_get_type ());
Wim Taymans's avatar
Wim Taymans committed
677
  g_type_class_ref (gst_segment_flags_get_type ());
Wim Taymans's avatar
Wim Taymans committed
678
  g_type_class_ref (gst_scheduling_flags_get_type ());
679
  g_type_class_ref (gst_meta_flags_get_type ());
Wim Taymans's avatar
Wim Taymans committed
680
  g_type_class_ref (gst_toc_entry_type_get_type ());
681
  g_type_class_ref (gst_toc_scope_get_type ());
682
  g_type_class_ref (gst_toc_loop_type_get_type ());
683 684
  g_type_class_ref (gst_control_binding_get_type ());
  g_type_class_ref (gst_control_source_get_type ());
Wim Taymans's avatar
Wim Taymans committed
685
  g_type_class_ref (gst_lock_flags_get_type ());
Wim Taymans's avatar
Wim Taymans committed
686
  g_type_class_ref (gst_allocator_flags_get_type ());
Wim Taymans's avatar
Wim Taymans committed
687
  g_type_class_ref (gst_stream_flags_get_type ());
688
  g_type_class_ref (gst_stream_type_get_type ());
689
  g_type_class_ref (gst_stack_trace_flags_get_type ());
690

691 692 693
  _priv_gst_event_initialize ();
  _priv_gst_buffer_initialize ();
  _priv_gst_buffer_list_initialize ();
694
  _priv_gst_sample_initialize ();
695
  _priv_gst_context_initialize ();
696
  _priv_gst_date_time_initialize ();
697
  _priv_gst_value_initialize ();
698
  _priv_gst_tag_initialize ();
699
  _priv_gst_toc_initialize ();
700

Wim Taymans's avatar
Wim Taymans committed
701
  g_type_class_ref (gst_param_spec_fraction_get_type ());
702
  gst_parse_context_get_type ();
Erik Walthinsen's avatar
Erik Walthinsen committed
703

704
  _priv_gst_plugin_initialize ();
705

706
  /* register core plugins */
707 708 709 710
  gst_plugin_register_static (GST_VERSION_MAJOR, GST_VERSION_MINOR,
      "staticelements", "core elements linked into the GStreamer library",
      gst_register_core_elements, VERSION, GST_LICENSE, PACKAGE,
      GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
711

712 713 714 715 716
  /*
   * Any errors happening below this point are non-fatal, we therefore mark
   * gstreamer as being initialized, since it is the case from a plugin point of
   * view.
   *
717
   * If anything fails, it will be put back to %FALSE in gst_init_check().
718 719 720 721 722
   * This allows some special plugins that would call gst_init() to not cause a
   * looping effect (i.e. initializing GStreamer twice).
   */
  gst_initialized = TRUE;

Jan Schmidt's avatar
Jan Schmidt committed
723
  if (!gst_update_registry ())
724
    return FALSE;
725

726
  GST_INFO ("GLib runtime version: %d.%d.%d", glib_major_version,
727
      glib_minor_version, glib_micro_version);
728
  GST_INFO ("GLib headers version: %d.%d.%d", GLIB_MAJOR_VERSION,
729
      GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
730
  GST_INFO ("initialized GStreamer successfully");
731

732
#ifndef GST_DISABLE_GST_DEBUG
733
  _priv_gst_tracing_init ();
734 735
#endif

736
  return TRUE;
737
}
738

Benjamin Otte's avatar
Benjamin Otte committed
739
#ifndef GST_DISABLE_GST_DEBUG
David Schleef's avatar
remove  
David Schleef committed
740 741 742 743 744 745
static gboolean
select_all (GstPlugin * plugin, gpointer user_data)
{
  return TRUE;
}

Benjamin Otte's avatar
Benjamin Otte committed
746 747 748 749
static gint
sort_by_category_name (gconstpointer a, gconstpointer b)
{
  return strcmp (gst_debug_category_get_name ((GstDebugCategory *) a),
750
      gst_debug_category_get_name ((GstDebugCategory *) b));
Benjamin Otte's avatar
Benjamin Otte committed
751
}
752

753
static void
754
gst_debug_help (void)
755
{
Benjamin Otte's avatar
Benjamin Otte committed
756
  GSList *list, *walk;
David Schleef's avatar
remove  
David Schleef committed
757
  GList *list2, *g;
758

759 760
  /* Need to ensure the registry is loaded to get debug categories */
  if (!init_post (NULL, NULL, NULL, NULL))
Benjamin Otte's avatar
Benjamin Otte committed
761
    exit (1);
762

763
  list2 = gst_registry_plugin_filter (gst_registry_get (),
David Schleef's avatar
remove  
David Schleef committed
764
      select_all, FALSE, NULL);
765

David Schleef's avatar
remove  
David Schleef committed
766 767
  /* FIXME this is gross.  why don't debug have categories PluginFeatures? */
  for (g = list2; g; g = g_list_next (g)) {
768
    GstPlugin *plugin = GST_PLUGIN_CAST (g->data);
769 770 771 772
    GList *features, *orig_features;

    if (GST_OBJECT_FLAG_IS_SET (plugin, GST_PLUGIN_FLAG_BLACKLISTED))
      continue;
773

David Schleef's avatar
remove  
David Schleef committed
774
    gst_plugin_load (plugin);
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
    /* Now create one of each feature so the class_init functions
     * are called, as that's where most debug categories are
     * registered. FIXME: If debug categories were a plugin feature,
     * this would be unneeded */
    orig_features = features =
        gst_registry_get_feature_list_by_plugin (gst_registry_get (),
        gst_plugin_get_name (plugin));
    while (features) {
      GstPluginFeature *feature;

      if (G_UNLIKELY (features->data == NULL))
        goto next;

      feature = GST_PLUGIN_FEATURE (features->data);
      if (GST_IS_ELEMENT_FACTORY (feature)) {
        GstElementFactory *factory;
        GstElement *e;

        factory = GST_ELEMENT_FACTORY (feature);
        e = gst_element_factory_create (factory, NULL);
        if (e)
          gst_object_unref (e);
      }

    next:
      features = g_list_next (features);
    }

    gst_plugin_feature_list_free (orig_features);
Benjamin Otte's avatar
Benjamin Otte committed
804 805
  }
  g_list_free (list2);
806

Benjamin Otte's avatar
Benjamin Otte committed
807 808
  list = gst_debug_get_all_categories ();
  walk = list = g_slist_sort (list, sort_by_category_name);
809

810 811 812 813 814
  g_print ("\n");
  g_print ("name                  level    description\n");
  g_print ("---------------------+--------+--------------------------------\n");

  while (walk) {
LRN's avatar
LRN committed
815
    gboolean on_unix;
Benjamin Otte's avatar
Benjamin Otte committed
816
    GstDebugCategory *cat = (GstDebugCategory *) walk->data;
LRN's avatar
LRN committed
817 818 819 820 821 822 823 824 825 826
    GstDebugColorMode coloring = gst_debug_get_color_mode ();
#ifdef G_OS_UNIX
    on_unix = TRUE;
#else
    on_unix = FALSE;
#endif

    if (GST_DEBUG_COLOR_MODE_UNIX == coloring
        || (on_unix && GST_DEBUG_COLOR_MODE_ON == coloring)) {
      gchar *color = gst_debug_construct_term_color (cat->color);
827

LRN's avatar
LRN committed
828 829 830 831 832 833 834 835
      g_print ("%s%-20s\033[00m  %1d %s  %s%s\033[00m\n",
          color,
          gst_debug_category_get_name (cat),
          gst_debug_category_get_threshold (cat),
          gst_debug_level_get_name (gst_debug_category_get_threshold (cat)),
          color, gst_debug_category_get_description (cat));
      g_free (color);
    } else if (GST_DEBUG_COLOR_MODE_ON == coloring && !on_unix) {
836 837 838 839 840 841 842 843 844 845 846 847 848 849
#ifdef G_OS_WIN32
      gint color = gst_debug_construct_win_color (cat->color);
      const gint clear = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;

      SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), color);
      g_print ("%-20s", gst_debug_category_get_name (cat));
      SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), clear);
      g_print (" %1d %s ", gst_debug_category_get_threshold (cat),
          gst_debug_level_get_name (gst_debug_category_get_threshold (cat)));
      SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), color);
      g_print ("%s", gst_debug_category_get_description (cat));
      SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), clear);
      g_print ("\n");
#endif /* G_OS_WIN32 */
850
    } else {
851
      g_print ("%-20s  %1d %s  %s\n", gst_debug_category_get_name (cat),
852 853 854
          gst_debug_category_get_threshold (cat),
          gst_debug_level_get_name (gst_debug_category_get_threshold (cat)),
          gst_debug_category_get_description (cat));
Wim Taymans's avatar
Wim Taymans committed
855
    }
856
    walk = g_slist_next (walk);
857
  }
Benjamin Otte's avatar
Benjamin Otte committed
858
  g_slist_free (list);
859
  g_print ("\n");