File: ges.c

package info (click to toggle)
gstreamer-editing-services1.0 1.22.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,944 kB
  • sloc: ansic: 61,405; python: 4,140; lex: 57; makefile: 30
file content (424 lines) | stat: -rw-r--r-- 11,952 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* GStreamer Editing Services
 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
 *               2009 Nokia Corporation
 *
 * 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
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION: ges.h
 * @title: Initialization
 * @short_description: GStreamer editing services initialization functions
 *
 * GES needs to be initialized after GStreamer itself. This section
 * contains the various functions to do so.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <ges/ges.h>
#include "ges/gstframepositioner.h"
#include "ges/ges-smart-adder.h"
#include "ges/ges-smart-video-mixer.h"
#include "ges-internal.h"

#ifndef DISABLE_XPTV
#include <ges/ges-pitivi-formatter.h>
#endif

#define GES_GNONLIN_VERSION_NEEDED_MAJOR 1
#define GES_GNONLIN_VERSION_NEEDED_MINOR 2
#define GES_GNONLIN_VERSION_NEEDED_MICRO 0

GST_DEBUG_CATEGORY (_ges_debug_category);

G_LOCK_DEFINE_STATIC (init_lock);

/* (without holding ref) thread object for thread_self() validation
 * between init/deinit
 */
static GThread *initialized_thread = NULL;

#ifndef GST_DISABLE_GST_DEBUG
static gpointer
init_debug_category (gpointer data)
{
  /* initialize debugging category */
  GST_DEBUG_CATEGORY_INIT (_ges_debug_category, "ges", GST_DEBUG_FG_YELLOW,
      "GStreamer Editing Services");

  return _ges_debug_category;
}

GstDebugCategory *
_ges_debug (void)
{
  static GOnce my_once = G_ONCE_INIT;

  g_once (&my_once, init_debug_category, NULL);

  return my_once.retval;
}
#else
GstDebugCategory *
_ges_debug (void)
{
  return NULL;
}

#endif

static gboolean
ges_init_pre (GOptionContext * context, GOptionGroup * group, gpointer data,
    GError ** error)
{
  if (initialized_thread) {
    GST_DEBUG ("already initialized");
    return TRUE;
  }

  return TRUE;
}

static gboolean
ges_init_post (GOptionContext * context, GOptionGroup * group, gpointer data,
    GError ** error)
{
  GESUriClipAssetClass *uriasset_klass = NULL;
  GstElementFactory *nlecomposition_factory = NULL;
  static GstValueTable gstvtable = {
    G_TYPE_NONE,
    (GstValueCompareFunc) NULL,
    (GstValueSerializeFunc) ges_marker_list_serialize,
    (GstValueDeserializeFunc) ges_marker_list_deserialize
  };
  static gboolean marker_list_registered = FALSE;

  if (initialized_thread) {
    GST_DEBUG ("already initialized ges");
    return TRUE;
  }

  uriasset_klass = g_type_class_ref (GES_TYPE_URI_CLIP_ASSET);

  _init_formatter_assets ();
  if (!_ges_uri_asset_ensure_setup (uriasset_klass)) {
    GST_ERROR ("cannot setup uri asset");
    if (error)
      *error = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
          "Cannot initialize URI asset class.");
    goto failed;
  }

  nlecomposition_factory = gst_element_factory_find ("nlecomposition");
  if (!nlecomposition_factory) {
    GST_ERROR ("The `nlecomposition` object was not found.");
    if (error)
      *error = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
          "The `nle` plugin is missing.");

    goto failed;
  }
  gst_object_unref (nlecomposition_factory);

  /* register clip classes with the system */

  g_type_class_ref (GES_TYPE_TEST_CLIP);
  g_type_class_ref (GES_TYPE_URI_CLIP);
  g_type_class_ref (GES_TYPE_TITLE_CLIP);
  g_type_class_ref (GES_TYPE_TRANSITION_CLIP);
  g_type_class_ref (GES_TYPE_OVERLAY_CLIP);
  g_type_class_ref (GES_TYPE_OVERLAY_TEXT_CLIP);
  g_type_class_ref (GES_TYPE_EFFECT_CLIP);

  g_type_class_ref (GES_TYPE_GROUP);

  /* Register track elements */
  g_type_class_ref (GES_TYPE_EFFECT);

  ges_asset_cache_init ();

  gst_element_register (NULL, "gesaudiomixer", 0, GES_TYPE_SMART_ADDER);
  gst_element_register (NULL, "gescompositor", 0, GES_TYPE_SMART_MIXER);
  gst_element_register (NULL, "framepositioner", 0, GST_TYPE_FRAME_POSITIONNER);
  gst_element_register (NULL, "gespipeline", 0, GES_TYPE_PIPELINE);

  /* TODO: user-defined types? */
  initialized_thread = g_thread_self ();
  g_type_class_unref (uriasset_klass);

  if (!marker_list_registered) {
    gstvtable.type = GES_TYPE_MARKER_LIST;
    gst_value_register (&gstvtable);
    marker_list_registered = TRUE;
  }

  GST_DEBUG ("GStreamer Editing Services initialized");

  return TRUE;

failed:
  if (uriasset_klass)
    g_type_class_unref (uriasset_klass);

  GST_ERROR ("Could not initialize GES.");

  return FALSE;
}

/**
 * ges_init:
 *
 * Initialize the GStreamer Editing Service. Call this before any usage of
 * GES. You should take care of initilizing GStreamer before calling this
 * function.
 *
 * MT safety.
 * GStreamer Editing Services do not guarantee MT safety.
 * An application is required to use GES APIs (including ges_deinit())
 * in the thread where ges_init() was called.
 */

gboolean
ges_init (void)
{
  gboolean ret;

  G_LOCK (init_lock);
  ges_init_pre (NULL, NULL, NULL, NULL);

  ret = ges_init_post (NULL, NULL, NULL, NULL);
  G_UNLOCK (init_lock);

  return ret;
}

/**
 * ges_deinit:
 *
 * Clean up any resources created by GES in ges_init().
 *
 * It is normally not needed to call this function in a normal application as the
 * resources will automatically be freed when the program terminates.
 * This function is therefore mostly used by testsuites and other memory profiling tools.
 * This function should be called from the thread where ges_init() was called.
 *
 * After this call GES should not be used until another ges_init() call.
 */
void
ges_deinit (void)
{
  G_LOCK (init_lock);

  GST_INFO ("deinitializing GES");

  if (!initialized_thread) {
    GST_DEBUG ("nothing to deinitialize");
    G_UNLOCK (init_lock);
    return;
  }

  /* Allow deinit only from a thread where ges_init() was called */
  g_assert (initialized_thread == g_thread_self ());

  _ges_uri_asset_cleanup ();

  g_type_class_unref (g_type_class_peek (GES_TYPE_TEST_CLIP));
  g_type_class_unref (g_type_class_peek (GES_TYPE_URI_CLIP));
  g_type_class_unref (g_type_class_peek (GES_TYPE_TITLE_CLIP));
  g_type_class_unref (g_type_class_peek (GES_TYPE_TRANSITION_CLIP));
  g_type_class_unref (g_type_class_peek (GES_TYPE_OVERLAY_CLIP));
  g_type_class_unref (g_type_class_peek (GES_TYPE_OVERLAY_TEXT_CLIP));
  g_type_class_unref (g_type_class_peek (GES_TYPE_EFFECT_CLIP));

  g_type_class_unref (g_type_class_peek (GES_TYPE_GROUP));
  /* Register track elements */
  g_type_class_unref (g_type_class_peek (GES_TYPE_EFFECT));

  ges_asset_cache_deinit ();
  ges_xml_formatter_deinit ();

  initialized_thread = NULL;
  G_UNLOCK (init_lock);

  GST_INFO ("deinitialized GES");

  return;
}

#ifndef GST_DISABLE_OPTION_PARSING
static gboolean
parse_goption_arg (const gchar * s_opt,
    const gchar * arg, gpointer data, GError ** err)
{
  if (g_strcmp0 (s_opt, "--ges-version") == 0) {
    gst_print ("GStreamer Editing Services version %s\n", PACKAGE_VERSION);
    exit (0);
  } else if (g_strcmp0 (s_opt, "--ges-sample-paths") == 0) {
    ges_add_missing_uri_relocation_uri (arg, FALSE);
  } else if (g_strcmp0 (s_opt, "--ges-sample-path-recurse") == 0) {
    ges_add_missing_uri_relocation_uri (arg, TRUE);
  }

  return TRUE;
}
#endif

/**
 * ges_init_get_option_group: (skip)
 *
 * Returns a #GOptionGroup with GES'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.
 *
 * This function is useful if you want to integrate GES with other
 * libraries that use GOption (see g_option_context_add_group() ).
 *
 * If you use this function, you should make sure you initialise the GStreamer
 * as one of the very first things in your program. That means you need to
 * use gst_init_get_option_group() and add it to the option context before
 * using the ges_init_get_option_group() result.
 *
 * Returns: (transfer full): a pointer to GES's option group.
 */
GOptionGroup *
ges_init_get_option_group (void)
{
#ifndef GST_DISABLE_OPTION_PARSING

  GOptionGroup *group;
  static const GOptionEntry ges_args[] = {
    {"ges-version", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
          (gpointer) parse_goption_arg,
          "Print the GStreamer Editing Services version",
        NULL},
    {"ges-sample-paths", 0, 0, G_OPTION_ARG_CALLBACK,
          (gpointer) parse_goption_arg,
        "List of pathes to look assets in if they were moved"},
    {"ges-sample-path-recurse", 0, 0, G_OPTION_ARG_CALLBACK,
          (gpointer) parse_goption_arg,
        "Same as above, but recursing into the folder"},
    {NULL}
  };

  group = g_option_group_new ("GES", "GStreamer Editing Services Options",
      "Show GES Options", NULL, NULL);
  g_option_group_set_parse_hooks (group, (GOptionParseFunc) ges_init_pre,
      (GOptionParseFunc) ges_init_post);
  g_option_group_add_entries (group, ges_args);

  return group;

#else
  return NULL;
#endif
}

/**
 * ges_version:
 * @major: (out): pointer to a guint to store the major version number
 * @minor: (out): pointer to a guint to store the minor version number
 * @micro: (out): pointer to a guint to store the micro version number
 * @nano:  (out): pointer to a guint to store the nano version number
 *
 * Gets the version number of the GStreamer Editing Services library.
 */
void
ges_version (guint * major, guint * minor, guint * micro, guint * nano)
{
  g_return_if_fail (major);
  g_return_if_fail (minor);
  g_return_if_fail (micro);
  g_return_if_fail (nano);

  *major = GES_VERSION_MAJOR;
  *minor = GES_VERSION_MINOR;
  *micro = GES_VERSION_MICRO;
  *nano = GES_VERSION_NANO;
}

/**
 * ges_init_check:
 * @argc: (inout) (allow-none): pointer to application's argc
 * @argv: (inout) (array length=argc) (allow-none): pointer to application's argv
 * @err: pointer to a #GError to which a message will be posted on error
 *
 * Initializes the GStreamer Editing Services library, setting up internal path lists,
 * and loading evrything needed.
 *
 * This function will return %FALSE if GES could not be initialized
 * for some reason.
 *
 * Returns: %TRUE if GES could be initialized.
 */
gboolean
ges_init_check (int *argc, char **argv[], GError ** err)
{
#ifndef GST_DISABLE_OPTION_PARSING
  GOptionGroup *group;
  GOptionContext *ctx;
#endif
  gboolean res;

  G_LOCK (init_lock);

  if (initialized_thread) {
    GST_DEBUG ("already initialized ges");
    G_UNLOCK (init_lock);
    return TRUE;
  }
#ifndef GST_DISABLE_OPTION_PARSING
  ctx = g_option_context_new ("- GStreamer Editing Services initialization");
  g_option_context_set_ignore_unknown_options (ctx, TRUE);
  g_option_context_set_help_enabled (ctx, FALSE);
  group = ges_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);
#endif

  if (!res) {
    G_UNLOCK (init_lock);
    return res;
  }

  ges_init_pre (NULL, NULL, NULL, NULL);
  res = ges_init_post (NULL, NULL, NULL, NULL);

  G_UNLOCK (init_lock);

  return res;
}

/**
 * ges_is_initialized:
 *
 * Use this function to check if GES has been initialized with ges_init()
 * or ges_init_check().
 *
 * Returns: %TRUE if initialization has been done, %FALSE otherwise.
 *
 * Since: 1.16
 */
gboolean
ges_is_initialized (void)
{
  return initialized_thread ? TRUE : FALSE;
}