ges-asset.c 40.1 KB
Newer Older
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1
2
/* GStreamer Editing Services
 *
3
 * Copyright (C) 2012-2015 Thibault Saunier <thibault.saunier@collabora.com>
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * Copyright (C) 2012 Volodymyr Rudyi <vladimir.rudoy@gmail.com>
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
/**
22
 * SECTION: gesasset
23
 * @title: GESAsset
24
 * @short_description: Represents usable resources inside the GStreamer Editing Services
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
25
 *
26
 * The Assets in the GStreamer Editing Services represent the resources
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
27
 * that can be used. You can create assets for any type that implements the #GESExtractable
28
 * interface, for example #GESClips, #GESFormatter, and #GESTrackElement do implement it.
29
 * This means that assets will represent for example a #GESUriClips, #GESBaseEffect etc,
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
30
31
32
33
34
 * and then you can extract objects of those types with the appropriate parameters from the asset
 * using the #ges_asset_extract method:
 *
 * |[
 * GESAsset *effect_asset;
35
 * GESEffect *effect;
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
36
37
 *
 * // You create an asset for an effect
38
 * effect_asset = ges_asset_request (GES_TYPE_EFFECT, "agingtv", NULL);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
39
 *
40
41
 * // And now you can extract an instance of GESEffect from that asset
 * effect = GES_EFFECT (ges_asset_extract (effect_asset));
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
42
43
44
45
46
47
48
49
 *
 * ]|
 *
 * In that example, the advantages of having a #GESAsset are that you can know what effects
 * you are working with and let your user know about the avalaible ones, you can add metadata
 * to the #GESAsset through the #GESMetaContainer interface and you have a model for your
 * custom effects. Note that #GESAsset management is making easier thanks to the #GESProject class.
 *
50
 * Each asset is represented by a pair of @extractable_type and @id (string). Actually the @extractable_type
51
 * is the type that implements the #GESExtractable interface, that means that for example for a #GESUriClip,
52
 * the type that implements the #GESExtractable interface is #GESClip.
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
53
54
55
 * The identifier represents different things depending on the @extractable_type and you should check
 * the documentation of each type to know what the ID of #GESAsset actually represents for that type. By default,
 * we only have one #GESAsset per type, and the @id is the name of the type, but this behaviour is overriden
56
57
 * to be more useful. For example, for GESTransitionClips, the ID is the vtype of the transition
 * you will extract from it (ie crossfade, box-wipe-rc etc..) For #GESEffect the ID is the
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
58
59
60
 * @bin-description property of the extracted objects (ie the gst-launch style description of the bin that
 * will be used).
 *
61
 * Each and every #GESAsset is cached into GES, and you can query those with the #ges_list_assets function.
62
 * Also the system will automatically register #GESAssets for #GESFormatters and #GESTransitionClips
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 * and standard effects (actually not implemented yet) and you can simply query those calling:
 * |[
 *    GList *formatter_assets, *tmp;
 *
 *    //  List all  the transitions
 *    formatter_assets = ges_list_assets (GES_TYPE_FORMATTER);
 *
 *    // Print some infos about the formatter GESAsset
 *    for (tmp = formatter_assets; tmp; tmp = tmp->next) {
 *      g_print ("Name of the formatter: %s, file extension it produces: %s",
 *        ges_meta_container_get_string (GES_META_CONTAINER (tmp->data), GES_META_FORMATTER_NAME),
 *        ges_meta_container_get_string (GES_META_CONTAINER (tmp->data), GES_META_FORMATTER_EXTENSION));
 *    }
 *
 *    g_list_free (transition_assets);
 *
 * ]|
 *
Mathieu Duponchelle's avatar
Mathieu Duponchelle committed
81
82
 * You can request the creation of #GESAssets using either ges_asset_request() or
 * ges_asset_request_async(). All the #GESAssets are cached and thus any asset that has already
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
83
84
 * been created can be requested again without overhead.
 */
85
86
87
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
88
89
90
91

#include "ges.h"
#include "ges-internal.h"

92
93
#define GLIB_DISABLE_DEPRECATION_WARNINGS

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
94
95
#include <gst/gst.h>

96
97
98
99
GST_DEBUG_CATEGORY_STATIC (ges_asset_debug);
#undef GST_CAT_DEFAULT
#define GST_CAT_DEFAULT ges_asset_debug

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
100
101
102
103
104
enum
{
  PROP_0,
  PROP_TYPE,
  PROP_ID,
105
106
  PROP_PROXY,
  PROP_PROXY_TARGET,
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
107
108
109
110
111
112
113
114
  PROP_LAST
};

typedef enum
{
  ASSET_NOT_INITIALIZED,
  ASSET_INITIALIZING, ASSET_INITIALIZED_WITH_ERROR,
  ASSET_PROXIED,
115
  ASSET_NEEDS_RELOAD,
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
116
117
118
119
120
121
122
123
124
125
126
  ASSET_INITIALIZED
} GESAssetState;

static GParamSpec *_properties[PROP_LAST];

struct _GESAssetPrivate
{
  gchar *id;
  GESAssetState state;
  GType extractable_type;

127
  /* When an asset is proxied, instantiating it will
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
128
129
130
   * return the asset it points to */
  char *proxied_asset_id;

131
132
133
  GList *proxies;
  GESAsset *proxy_target;

Thibault Saunier's avatar
Thibault Saunier committed
134
  /* The error that occurred when an asset has been initialized with error */
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
135
136
137
138
139
140
141
142
143
144
145
146
147
  GError *error;
};

/* Internal structure to help avoid full loading
 * of one asset several times
 */
typedef struct
{
  GList *results;
  GESAsset *asset;
} GESAssetCacheEntry;

/* Also protect all the entries in the cache */
Seungha Yang's avatar
Seungha Yang committed
148
G_LOCK_DEFINE_STATIC (asset_cache_lock);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* We are mapping entries by types and ID, such as:
 *
 * {
 *   first_extractable_type_name1 :
 *      {
 *        "some ID": GESAssetCacheEntry,
 *        "some other ID": GESAssetCacheEntry 2
 *      },
 *   second_extractable_type_name :
 *      {
 *        "some ID": GESAssetCacheEntry,
 *        "some other ID": GESAssetCacheEntry 2
 *      }
 * }
 *
 * (The first extractable type is the type of the class that implemented
165
 *  the GESExtractable interface ie: GESClip, GESTimeline,
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
166
167
168
169
170
171
 *  GESFomatter, etc... but not subclasses)
 *
 * This is in order to be able to have 2 Asset with the same ID but
 * different extractable types.
 **/
static GHashTable *type_entries_table = NULL;
Seungha Yang's avatar
Seungha Yang committed
172
173
#define LOCK_CACHE   (G_LOCK (asset_cache_lock))
#define UNLOCK_CACHE (G_UNLOCK (asset_cache_lock))
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

static gchar *
_check_and_update_parameters (GType * extractable_type, const gchar * id,
    GError ** error)
{
  gchar *real_id;
  GType old_type = *extractable_type;

  *extractable_type =
      ges_extractable_get_real_extractable_type_for_id (*extractable_type, id);

  if (*extractable_type == G_TYPE_NONE) {
    GST_WARNING ("No way to create a Asset for ID: %s, type: %s", id,
        g_type_name (old_type));

    if (error && *error == NULL)
190
      g_set_error (error, GES_ERROR, GES_ERROR_ASSET_WRONG_ID,
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
191
192
193
194
195
196
197
198
199
200
          "Wrong ID, can not find any extractable_type");
    return NULL;
  }

  real_id = ges_extractable_type_check_id (*extractable_type, id, error);
  if (real_id == NULL) {
    GST_WARNING ("Wrong ID %s, can not create asset", id);

    g_free (real_id);
    if (error && *error == NULL)
201
      g_set_error (error, GES_ERROR, GES_ERROR_ASSET_WRONG_ID, "Wrong ID");
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
202
203
204
205
206
207
208
209

    return NULL;
  }

  return real_id;
}

static gboolean
210
start_loading (GESAsset * asset)
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
211
212
{
  ges_asset_cache_put (gst_object_ref (asset), NULL);
213
  return ges_asset_cache_set_loaded (asset->priv->extractable_type,
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
214
      asset->priv->id, NULL);
215
}
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
216

217
218
219
220
static gboolean
initable_init (GInitable * initable, GCancellable * cancellable,
    GError ** error)
{
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
221
222
  g_clear_error (error);

223
  return start_loading (GES_ASSET (initable));
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
224
225
226
227
228
229
230
}

static void
async_initable_init_async (GAsyncInitable * initable, gint io_priority,
    GCancellable * cancellable, GAsyncReadyCallback callback,
    gpointer user_data)
{
231
  GTask *task;
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
232
233
234
235

  GError *error = NULL;
  GESAsset *asset = GES_ASSET (initable);

236
  task = g_task_new (asset, cancellable, callback, user_data);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
237

238
  ges_asset_cache_put (g_object_ref (asset), task);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
239
240
241
242
  switch (GES_ASSET_GET_CLASS (asset)->start_loading (asset, &error)) {
    case GES_ASSET_LOADING_ERROR:
    {
      if (error == NULL)
243
        g_set_error (&error, GES_ERROR, GES_ERROR_ASSET_LOADING,
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
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
            "Could not start loading asset");

      /* FIXME Define error code */
      ges_asset_cache_set_loaded (asset->priv->extractable_type,
          asset->priv->id, error);
      g_error_free (error);
      return;
    }
    case GES_ASSET_LOADING_OK:
    {
      ges_asset_cache_set_loaded (asset->priv->extractable_type,
          asset->priv->id, error);
      return;
    }
    case GES_ASSET_LOADING_ASYNC:
      /* If Async....  let it go */
      break;
  }
}

static void
async_initable_iface_init (GAsyncInitableIface * async_initable_iface)
{
  async_initable_iface->init_async = async_initable_init_async;
}

static void
initable_iface_init (GInitableIface * initable_iface)
{
  initable_iface->init = initable_init;
}

G_DEFINE_TYPE_WITH_CODE (GESAsset, ges_asset, G_TYPE_OBJECT,
277
    G_ADD_PRIVATE (GESAsset)
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
    G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
    G_IMPLEMENT_INTERFACE (GES_TYPE_META_CONTAINER, NULL));

/* GESAsset virtual methods default implementation */
static GESAssetLoadingReturn
ges_asset_start_loading_default (GESAsset * asset, GError ** error)
{
  return GES_ASSET_LOADING_OK;
}

static GESExtractable *
ges_asset_extract_default (GESAsset * asset, GError ** error)
{
  guint n_params;
  GParameter *params;
  GESAssetPrivate *priv = asset->priv;
295
  GESExtractable *n_extractable;
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
296

297

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
298
299
300
  params = ges_extractable_type_get_parameters_from_id (priv->extractable_type,
      priv->id, &n_params);

301
302
303
304
305
306
307
#if GLIB_CHECK_VERSION(2, 53, 1)
  {
    gint i;
    GValue *values;
    const gchar **names;

    values = g_malloc0 (sizeof (GValue) * n_params);
Edward Hervey's avatar
Edward Hervey committed
308
    names = g_malloc0 (sizeof (gchar *) * n_params);
309
310
311
312
313
314
315
316
317
318
319
320
321

    for (i = 0; i < n_params; i++) {
      values[i] = params[i].value;
      names[i] = params[i].name;
    }

    n_extractable =
        GES_EXTRACTABLE (g_object_new_with_properties (priv->extractable_type,
            n_params, names, values));
    g_free (names);
    g_free (values);
  }
#else
322
  n_extractable = g_object_newv (priv->extractable_type, n_params, params);
323
#endif
324
325
326

  while (n_params--)
    g_value_unset (&params[n_params].value);
327
328

  g_free (params);
329
330

  return n_extractable;
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
}

static gboolean
ges_asset_request_id_update_default (GESAsset * self, gchar ** proposed_new_id,
    GError * error)
{
  return FALSE;
}

/* GObject virtual methods implementation */
static void
ges_asset_get_property (GObject * object, guint property_id,
    GValue * value, GParamSpec * pspec)
{
  GESAsset *asset = GES_ASSET (object);

  switch (property_id) {
    case PROP_TYPE:
      g_value_set_gtype (value, asset->priv->extractable_type);
      break;
    case PROP_ID:
      g_value_set_string (value, asset->priv->id);
      break;
354
355
356
357
358
359
    case PROP_PROXY:
      g_value_set_object (value, ges_asset_get_proxy (asset));
      break;
    case PROP_PROXY_TARGET:
      g_value_set_object (value, ges_asset_get_proxy_target (asset));
      break;
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
ges_asset_set_property (GObject * object, guint property_id,
    const GValue * value, GParamSpec * pspec)
{
  GESAsset *asset = GES_ASSET (object);

  switch (property_id) {
    case PROP_TYPE:
      asset->priv->extractable_type = g_value_get_gtype (value);
      ges_extractable_register_metas (asset->priv->extractable_type, asset);
      break;
    case PROP_ID:
      asset->priv->id = g_value_dup_string (value);
      break;
379
380
381
382
383
384
    case PROP_PROXY:
      ges_asset_set_proxy (asset, g_value_get_object (value));
      break;
    case PROP_PROXY_TARGET:
      ges_asset_set_proxy (g_value_get_object (value), asset);
      break;
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
385
386
387
388
389
390
391
392
393
394
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
ges_asset_finalize (GObject * object)
{
  GESAssetPrivate *priv = GES_ASSET (object)->priv;

395
396
  GST_DEBUG_OBJECT (object, "finalizing");

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
397
398
399
400
401
402
  if (priv->id)
    g_free (priv->id);

  if (priv->proxied_asset_id)
    g_free (priv->proxied_asset_id);

Thibault Saunier's avatar
Thibault Saunier committed
403
404
405
  if (priv->error)
    g_error_free (priv->error);

Seungha Yang's avatar
Seungha Yang committed
406
407
408
  if (priv->proxies)
    g_list_free (priv->proxies);

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
  G_OBJECT_CLASS (ges_asset_parent_class)->finalize (object);
}

void
ges_asset_class_init (GESAssetClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = ges_asset_get_property;
  object_class->set_property = ges_asset_set_property;
  object_class->finalize = ges_asset_finalize;

  _properties[PROP_TYPE] =
      g_param_spec_gtype ("extractable-type", "Extractable type",
      "The type of the Object that can be extracted out of the asset",
      G_TYPE_OBJECT, G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);

  _properties[PROP_ID] =
      g_param_spec_string ("id", "Identifier",
      "The unic identifier of the asset", NULL,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);

431
432
433
434
435
436
437
438
  _properties[PROP_PROXY] =
      g_param_spec_object ("proxy", "Proxy",
      "The asset default proxy.", GES_TYPE_ASSET, G_PARAM_READWRITE);

  _properties[PROP_PROXY_TARGET] =
      g_param_spec_object ("proxy-target", "Proxy target",
      "The target of a proxy asset.", GES_TYPE_ASSET, G_PARAM_READWRITE);

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
439
440
441
442
443
444
  g_object_class_install_properties (object_class, PROP_LAST, _properties);

  klass->start_loading = ges_asset_start_loading_default;
  klass->extract = ges_asset_extract_default;
  klass->request_id_update = ges_asset_request_id_update_default;
  klass->inform_proxy = NULL;
445
446
447

  GST_DEBUG_CATEGORY_INIT (ges_asset_debug, "ges-asset",
      GST_DEBUG_FG_BLUE | GST_DEBUG_BOLD, "GES Asset");
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
448
449
450
451
452
}

void
ges_asset_init (GESAsset * self)
{
453
  self->priv = ges_asset_get_instance_private (self);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488

  self->priv->state = ASSET_INITIALIZING;
  self->priv->proxied_asset_id = NULL;
}

/* Internal methods */

/* Find the type that implemented the GESExtractable interface */
static inline const gchar *
_extractable_type_name (GType type)
{
  while (1) {
    if (g_type_is_a (g_type_parent (type), GES_TYPE_EXTRACTABLE))
      type = g_type_parent (type);
    else
      return g_type_name (type);
  }
}

static inline GESAssetCacheEntry *
_lookup_entry (GType extractable_type, const gchar * id)
{
  GHashTable *entries_table;

  entries_table = g_hash_table_lookup (type_entries_table,
      _extractable_type_name (extractable_type));
  if (entries_table)
    return g_hash_table_lookup (entries_table, id);

  return NULL;
}

static void
_free_entries (gpointer entry)
{
Seungha Yang's avatar
Seungha Yang committed
489
490
491
  GESAssetCacheEntry *data = (GESAssetCacheEntry *) entry;
  if (data->asset)
    gst_object_unref (data->asset);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
492
493
494
  g_slice_free (GESAssetCacheEntry, entry);
}

495
496
497
498
499
static void
_gtask_return_error (GTask * task, GError * error)
{
  g_task_return_error (task, g_error_copy (error));
}
500
501

static void
502
_gtask_return_true (GTask * task, gpointer udata)
503
504
505
506
{
  g_task_return_boolean (task, TRUE);
}

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
507
508
509
510
511
512
513
/**
 * ges_asset_cache_lookup:
 *
 * @id String identifier of asset
 *
 * Looks for asset with specified id in cache and it's completely loaded.
 *
514
 * Returns: (transfer none) (nullable): The #GESAsset found or %NULL
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
 */
GESAsset *
ges_asset_cache_lookup (GType extractable_type, const gchar * id)
{
  GESAsset *asset = NULL;
  GESAssetCacheEntry *entry = NULL;

  g_return_val_if_fail (id, NULL);

  LOCK_CACHE;
  entry = _lookup_entry (extractable_type, id);
  if (entry)
    asset = entry->asset;
  UNLOCK_CACHE;

  return asset;
}

static void
534
535
ges_asset_cache_append_task (GType extractable_type,
    const gchar * id, GTask * task)
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
536
537
538
539
540
{
  GESAssetCacheEntry *entry = NULL;

  LOCK_CACHE;
  if ((entry = _lookup_entry (extractable_type, id)))
541
    entry->results = g_list_append (entry->results, task);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
542
543
544
545
546
547
548
549
550
  UNLOCK_CACHE;
}

gboolean
ges_asset_cache_set_loaded (GType extractable_type, const gchar * id,
    GError * error)
{
  GESAsset *asset;
  GESAssetCacheEntry *entry = NULL;
551
552
553
554
  GList *results = NULL;
  GFunc user_func = NULL;
  gpointer user_data = NULL;

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
555
556
557
558
559
560
561
562
563
564
565
566
567
568
  LOCK_CACHE;
  if ((entry = _lookup_entry (extractable_type, id)) == NULL) {
    UNLOCK_CACHE;
    GST_ERROR ("Calling but type %s ID: %s not in cached, "
        "something massively screwed", g_type_name (extractable_type), id);

    return FALSE;
  }

  asset = entry->asset;
  GST_DEBUG_OBJECT (entry->asset, ": (extractable type: %s) loaded, calling %i "
      "callback (Error: %s)", g_type_name (asset->priv->extractable_type),
      g_list_length (entry->results), error ? error->message : "");

569
570
  results = entry->results;
  entry->results = NULL;
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
571

572
  if (error) {
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
573
    asset->priv->state = ASSET_INITIALIZED_WITH_ERROR;
Thibault Saunier's avatar
Thibault Saunier committed
574
575
    if (asset->priv->error)
      g_error_free (asset->priv->error);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
576
577
578
579
    asset->priv->error = g_error_copy (error);

    /* In case of error we do not want to emit in idle as we need to recover
     * if possible */
580
581
582
    user_func = (GFunc) _gtask_return_error;
    user_data = error;
    GST_DEBUG_OBJECT (asset, "initialized with error");
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
583
  } else {
584
    asset->priv->state = ASSET_INITIALIZED;
585
586
    user_func = (GFunc) _gtask_return_true;
    GST_DEBUG_OBJECT (asset, "initialized");
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
587
  }
588
589
590
591
  UNLOCK_CACHE;

  g_list_foreach (results, user_func, user_data);
  g_list_free_full (results, g_object_unref);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
592
593
594
595
596

  return TRUE;
}

void
597
ges_asset_cache_put (GESAsset * asset, GTask * task)
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
{
  GType extractable_type;
  const gchar *asset_id;
  GESAssetCacheEntry *entry;

  /* Needing to work with the cache, taking the lock */
  asset_id = ges_asset_get_id (asset);
  extractable_type = asset->priv->extractable_type;

  LOCK_CACHE;
  if (!(entry = _lookup_entry (extractable_type, asset_id))) {
    GHashTable *entries_table;

    entries_table = g_hash_table_lookup (type_entries_table,
        _extractable_type_name (extractable_type));
    if (entries_table == NULL) {
      entries_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
          _free_entries);

      g_hash_table_insert (type_entries_table,
          g_strdup (_extractable_type_name (extractable_type)), entries_table);
    }

    entry = g_slice_new0 (GESAssetCacheEntry);

    entry->asset = asset;
624
625
    if (task)
      entry->results = g_list_prepend (entry->results, task);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
626
627
628
    g_hash_table_insert (entries_table, (gpointer) g_strdup (asset_id),
        (gpointer) entry);
  } else {
629
630
631
    if (task) {
      GST_DEBUG ("%s already in cache, adding result %p", asset_id, task);
      entry->results = g_list_prepend (entry->results, task);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
632
633
634
635
636
637
638
639
640
    }
  }
  UNLOCK_CACHE;
}

void
ges_asset_cache_init (void)
{
  type_entries_table = g_hash_table_new_full (g_str_hash, g_str_equal,
Seungha Yang's avatar
Seungha Yang committed
641
      g_free, (GDestroyNotify) g_hash_table_unref);
642
643

  _init_formatter_assets ();
644
  _init_standard_transition_assets ();
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
645
646
}

647
648
649
650
651
652
653
void
ges_asset_cache_deinit (void)
{
  g_hash_table_destroy (type_entries_table);
  type_entries_table = NULL;
}

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
654
655
656
657
gboolean
ges_asset_request_id_update (GESAsset * asset, gchar ** proposed_id,
    GError * error)
{
658
659
  g_return_val_if_fail (GES_IS_ASSET (asset), FALSE);

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
660
661
662
663
664
  return GES_ASSET_GET_CLASS (asset)->request_id_update (asset, proposed_id,
      error);
}

gboolean
665
ges_asset_try_proxy (GESAsset * asset, const gchar * new_id)
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
666
667
{
  GESAssetClass *class;
668

669
670
  g_return_val_if_fail (GES_IS_ASSET (asset), FALSE);

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
671
672
673
674
  if (g_strcmp0 (asset->priv->id, new_id) == 0) {
    GST_WARNING_OBJECT (asset, "Trying to proxy to itself (%s),"
        " NOT possible", new_id);

675
676
    return FALSE;
  } else if (g_strcmp0 (asset->priv->proxied_asset_id, new_id) == 0) {
677
678
679
    GST_WARNING_OBJECT (asset,
        "Trying to proxy to same currently set proxy: %s -- %s",
        asset->priv->proxied_asset_id, new_id);
680

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
681
682
683
    return FALSE;
  }

684
  g_free (asset->priv->proxied_asset_id);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
685
686
687
688
689
690
691
  asset->priv->state = ASSET_PROXIED;
  asset->priv->proxied_asset_id = g_strdup (new_id);

  class = GES_ASSET_GET_CLASS (asset);
  if (class->inform_proxy)
    GES_ASSET_GET_CLASS (asset)->inform_proxy (asset, new_id);

692
693
  GST_DEBUG_OBJECT (asset, "Trying to proxy to %s", new_id);

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
694
695
696
  return TRUE;
}

697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
static gboolean
_lookup_proxied_asset (const gchar * id, GESAssetCacheEntry * entry,
    const gchar * asset_id)
{
  return !g_strcmp0 (asset_id, entry->asset->priv->proxied_asset_id);
}

/**
 * ges_asset_set_proxy:
 * @asset: The #GESAsset to set proxy on
 * @proxy: (allow-none): The #GESAsset that should be used as default proxy for @asset or
 * %NULL if you want to use the currently set proxy. Note that an asset can proxy one and only
 * one other asset.
 *
 * A proxying asset is an asset that can substitue the real @asset. For example if you
 * have a full HD #GESUriClipAsset you might want to set a lower resolution (HD version
 * of the same file) as proxy. Note that when an asset is proxied, calling
 * #ges_asset_request will actually return the proxy asset.
 *
716
 * Returns: %TRUE if @proxy has been set on @asset, %FALSE otherwise.
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
 */
gboolean
ges_asset_set_proxy (GESAsset * asset, GESAsset * proxy)
{
  g_return_val_if_fail (asset == NULL || GES_IS_ASSET (asset), FALSE);
  g_return_val_if_fail (proxy == NULL || GES_IS_ASSET (proxy), FALSE);
  g_return_val_if_fail (asset != proxy, FALSE);

  if (!proxy) {
    if (asset->priv->error) {
      GST_ERROR_OBJECT (asset,
          "Proxy was loaded with error (%s), it should not be 'unproxied'",
          asset->priv->error->message);

      return FALSE;
    }

    if (asset->priv->proxies) {
      GESAsset *old_proxy = GES_ASSET (asset->priv->proxies->data);

737
      old_proxy->priv->proxy_target = NULL;
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
      g_object_notify_by_pspec (G_OBJECT (old_proxy),
          _properties[PROP_PROXY_TARGET]);
    }

    GST_DEBUG_OBJECT (asset, "%s not proxied anymore", asset->priv->id);
    asset->priv->state = ASSET_INITIALIZED;
    g_object_notify_by_pspec (G_OBJECT (asset), _properties[PROP_PROXY]);

    return TRUE;
  }

  if (asset == NULL) {
    GHashTable *entries_table;
    GESAssetCacheEntry *entry;

    entries_table = g_hash_table_lookup (type_entries_table,
        _extractable_type_name (proxy->priv->extractable_type));
    entry = g_hash_table_find (entries_table, (GHRFunc) _lookup_proxied_asset,
        (gpointer) ges_asset_get_id (proxy));

    if (!entry) {
      GST_DEBUG_OBJECT (asset, "Not proxying any asset");
      return FALSE;
    }

    asset = entry->asset;
    while (asset->priv->proxies)
      asset = asset->priv->proxies->data;
  }

  if (proxy->priv->proxy_target) {
    GST_ERROR_OBJECT (asset,
        "Trying to use %s as a proxy, but it is already proxying %s",
        proxy->priv->id, proxy->priv->proxy_target->priv->id);
772
773

    return FALSE;
774
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
  }

  if (g_list_find (proxy->priv->proxies, asset)) {
    GST_ERROR_OBJECT (asset, "Trying to setup a circular proxying dependency!");

    return FALSE;
  }

  if (g_list_find (asset->priv->proxies, proxy)) {
    GST_INFO_OBJECT (asset,
        "%" GST_PTR_FORMAT " already marked as proxy, moving to first", proxy);
    GES_ASSET (asset->priv->proxies->data)->priv->proxy_target = NULL;
    asset->priv->proxies = g_list_remove (asset->priv->proxies, proxy);
  }

  GST_INFO ("%s is now proxied by %s", asset->priv->id, proxy->priv->id);
  asset->priv->proxies = g_list_prepend (asset->priv->proxies, proxy);
  proxy->priv->proxy_target = asset;
  g_object_notify_by_pspec (G_OBJECT (proxy), _properties[PROP_PROXY_TARGET]);

  asset->priv->state = ASSET_PROXIED;
  g_object_notify_by_pspec (G_OBJECT (asset), _properties[PROP_PROXY]);

  return TRUE;
}

800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/**
 * ges_asset_unproxy:
 * @asset: The #GESAsset to stop proxying with @proxy
 * @proxy: The #GESAsset to stop considering as a proxy for @asset
 *
 * Removes @proxy from the list of known proxies for @asset.
 * If @proxy was the current proxy for @asset, stop using it.
 *
 * Returns: %TRUE if @proxy was a known proxy for @asset, %FALSE otherwise.
 */
gboolean
ges_asset_unproxy (GESAsset * asset, GESAsset * proxy)
{
  g_return_val_if_fail (GES_IS_ASSET (asset), FALSE);
  g_return_val_if_fail (GES_IS_ASSET (proxy), FALSE);
  g_return_val_if_fail (asset != proxy, FALSE);

  if (!g_list_find (asset->priv->proxies, proxy)) {
    GST_INFO_OBJECT (asset, "%s is not a proxy.", proxy->priv->id);

    return FALSE;
  }

  if (asset->priv->proxies->data == proxy)
    ges_asset_set_proxy (asset, NULL);

  asset->priv->proxies = g_list_remove (asset->priv->proxies, proxy);

  return TRUE;
}

831
832
833
834
/**
 * ges_asset_list_proxies:
 * @asset: The #GESAsset to get proxies from
 *
835
 * Returns: (element-type GESAsset) (transfer none): The list of proxies @asset has. Note that the default asset to be
836
837
838
839
840
841
842
843
844
845
846
847
848
849
 * used is always the first in that list.
 */
GList *
ges_asset_list_proxies (GESAsset * asset)
{
  g_return_val_if_fail (GES_IS_ASSET (asset), NULL);

  return asset->priv->proxies;
}

/**
 * ges_asset_get_proxy:
 * @asset: The #GESAsset to get currenlty used proxy
 *
850
 * Returns: (transfer none) (nullable): The proxy in use for @asset
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
 */
GESAsset *
ges_asset_get_proxy (GESAsset * asset)
{
  g_return_val_if_fail (GES_IS_ASSET (asset), NULL);

  if (asset->priv->state == ASSET_PROXIED && asset->priv->proxies) {
    return asset->priv->proxies->data;
  }

  return NULL;
}

/**
 * ges_asset_get_proxy_target:
 * @proxy: The #GESAsset from which to get the the asset it proxies.
 *
868
 * Returns: (transfer none) (nullable): The #GESAsset that is proxied by @proxy
869
870
871
872
873
874
875
876
877
 */
GESAsset *
ges_asset_get_proxy_target (GESAsset * proxy)
{
  g_return_val_if_fail (GES_IS_ASSET (proxy), NULL);

  return proxy->priv->proxy_target;
}

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
878
879
880
881
882
883
884
885
886
887
888
/* Caution, this method should be used in rare cases (ie: for the project
 * as we can change its ID from a useless one to a proper URI). In most
 * cases you want to update the ID creating a proxy
 */
void
ges_asset_set_id (GESAsset * asset, const gchar * id)
{
  GHashTable *entries;

  gpointer orig_id = NULL;
  GESAssetCacheEntry *entry = NULL;
889
890
891
892
893
  GESAssetPrivate *priv = NULL;

  g_return_if_fail (GES_IS_ASSET (asset));

  priv = asset->priv;
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910

  if (priv->state != ASSET_INITIALIZED) {
    GST_WARNING_OBJECT (asset, "Trying to rest ID on an object that is"
        " not properly loaded");
    return;
  }

  if (g_strcmp0 (id, priv->id) == 0) {
    GST_DEBUG_OBJECT (asset, "ID is already %s", id);

    return;
  }

  LOCK_CACHE;
  entries = g_hash_table_lookup (type_entries_table,
      _extractable_type_name (asset->priv->extractable_type));

911
912
  g_return_if_fail (g_hash_table_lookup_extended (entries, priv->id, &orig_id,
          (gpointer *) & entry));
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959

  g_hash_table_steal (entries, priv->id);
  g_hash_table_insert (entries, g_strdup (id), entry);

  GST_DEBUG_OBJECT (asset, "Changing id from %s to %s", priv->id, id);
  g_free (priv->id);
  g_free (orig_id);
  priv->id = g_strdup (id);
  UNLOCK_CACHE;
}

static GESAsset *
_unsure_material_for_wrong_id (const gchar * wrong_id, GType extractable_type,
    GError * error)
{
  GESAsset *asset;

  if ((asset = ges_asset_cache_lookup (extractable_type, wrong_id)))
    return asset;

  /* It is a dummy GESAsset, we just bruteforce its creation */
  asset = g_object_new (GES_TYPE_ASSET, "id", wrong_id,
      "extractable-type", extractable_type, NULL);

  ges_asset_cache_put (asset, NULL);
  ges_asset_cache_set_loaded (extractable_type, wrong_id, error);

  return asset;
}

/**********************************
 *                                *
 *      API implementation        *
 *                                *
 **********************************/

/**
 * ges_asset_get_extractable_type:
 * @self: The #GESAsset
 *
 * Gets the type of object that can be extracted from @self
 *
 * Returns: the type of object that can be extracted from @self
 */
GType
ges_asset_get_extractable_type (GESAsset * self)
{
960
961
  g_return_val_if_fail (GES_IS_ASSET (self), G_TYPE_INVALID);

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
962
963
964
965
966
967
968
969
970
971
972
973
  return self->priv->extractable_type;
}

/**
 * ges_asset_request:
 * @extractable_type: The #GType of the object that can be extracted from the new asset.
 * @id: (allow-none): The Identifier or %NULL
 * @error: (allow-none): An error to be set in case something wrong happens or %NULL
 *
 * Create a #GESAsset in the most simple cases, you should look at the @extractable_type
 * documentation to see if that constructor can be called for this particular type
 *
974
975
976
 * As it is recommanded not to instanciate assets for GESUriClip synchronously,
 * it will not work with this method, but you can instead use the specific
 * #ges_uri_clip_asset_request_sync method if you really want to.
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
 *
 * Returns: (transfer full) (allow-none): A reference to the wanted #GESAsset or %NULL
 */
GESAsset *
ges_asset_request (GType extractable_type, const gchar * id, GError ** error)
{
  gchar *real_id;

  GError *lerr = NULL;
  GESAsset *asset = NULL;

  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
  g_return_val_if_fail (g_type_is_a (extractable_type, G_TYPE_OBJECT), NULL);
  g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
      NULL);

  real_id = _check_and_update_parameters (&extractable_type, id, &lerr);
  if (real_id == NULL) {
    /* We create an asset for that wrong ID so we have a reference that the
     * user requested it */
    _unsure_material_for_wrong_id (id, extractable_type, lerr);
    real_id = g_strdup (id);
  }
Edward Hervey's avatar
Edward Hervey committed
1000
1001
  if (lerr)
    g_error_free (lerr);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013

  asset = ges_asset_cache_lookup (extractable_type, real_id);
  if (asset) {
    while (TRUE) {
      switch (asset->priv->state) {
        case ASSET_INITIALIZED:
          gst_object_ref (asset);
          goto done;
        case ASSET_INITIALIZING:
          asset = NULL;
          goto done;
        case ASSET_PROXIED:
1014
1015
          if (asset->priv->proxies == NULL) {
            GST_ERROR ("Proxied against an asset we do not"
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1016
1017
1018
1019
                " have in cache, something massively screwed");

            goto done;
          }
1020
1021
1022
1023
1024

          asset = asset->priv->proxies->data;
          while (ges_asset_get_proxy (asset))
            asset = ges_asset_get_proxy (asset);

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1025
          break;
1026
1027
1028
1029
1030
        case ASSET_NEEDS_RELOAD:
          GST_DEBUG_OBJECT (asset, "Asset in cache and needs reload");
          start_loading (asset);

          goto done;
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1031
1032
        case ASSET_INITIALIZED_WITH_ERROR:
          GST_WARNING_OBJECT (asset, "Initialized with error, not returning");
1033
          if (asset->priv->error && error)
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
            *error = g_error_copy (asset->priv->error);
          asset = NULL;
          goto done;
        default:
          GST_WARNING ("Case %i not handle, returning", asset->priv->state);
          goto done;
      }
    }
  } else {
    GObjectClass *klass;
    GInitableIface *iface;
    GType asset_type = ges_extractable_type_get_asset_type (extractable_type);

    klass = g_type_class_ref (asset_type);
    iface = g_type_interface_peek (klass, G_TYPE_INITABLE);

    if (iface->init) {
      asset = g_initable_new (asset_type,
          NULL, NULL, "id", real_id, "extractable-type",
          extractable_type, NULL);
    } else {
      GST_WARNING ("Tried to create an Asset for type %s but no ->init method",
          g_type_name (extractable_type));
    }
    g_type_class_unref (klass);
  }

done:
  if (real_id)
    g_free (real_id);

  GST_DEBUG ("New asset created synchronously: %p", asset);
  return asset;
}

/**
 * ges_asset_request_async:
 * @extractable_type: The #GType of the object that can be extracted from the
 *    new asset. The class must implement the #GESExtractable interface.
 * @id: The Identifier of the asset we want to create. This identifier depends of the extractable,
 * type you want. By default it is the name of the class itself (or %NULL), but for example for a
1075
 * GESEffect, it will be the pipeline description, for a GESUriClip it
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
 * will be the name of the file, etc... You should refer to the documentation of the #GESExtractable
 * type you want to create a #GESAsset for.
 * @cancellable: (allow-none): optional %GCancellable object, %NULL to ignore.
 * @callback: a #GAsyncReadyCallback to call when the initialization is finished,
 * Note that the @source of the callback will be the #GESAsset, but you need to
 * make sure that the asset is properly loaded using the #ges_asset_request_finish
 * method. This asset can not be used as is.
 * @user_data: The user data to pass when @callback is called
 *
 * Request a new #GESAsset asyncronously, @callback will be called when the materail is
 * ready to be used or if an error occured.
 *
 * Example of request of a GESAsset async:
 * |[
 * // The request callback
 * static void
 * asset_loaded_cb (GESAsset * source, GAsyncResult * res, gpointer user_data)
 * {
 *   GESAsset *asset;
 *   GError *error = NULL;
 *
 *   asset = ges_asset_request_finish (res, &error);
 *   if (asset) {
 *    g_print ("The file: %s is usable as a FileSource",
 *        ges_asset_get_id (asset));
 *   } else {
 *    g_print ("The file: %s is *not* usable as a FileSource because: %s",
 *        ges_asset_get_id (source), error->message);
 *   }
 *
1106
 *   gst_object_unref (mfs);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1107
1108
1109
 * }
 *
 * // The request:
1110
 * ges_asset_request_async (GES_TYPE_URI_CLIP, some_uri, NULL,
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
 *    (GAsyncReadyCallback) asset_loaded_cb, user_data);
 * ]|
 */
void
ges_asset_request_async (GType extractable_type,
    const gchar * id, GCancellable * cancellable, GAsyncReadyCallback callback,
    gpointer user_data)
{
  gchar *real_id;
  GESAsset *asset;
  GError *error = NULL;

  g_return_if_fail (g_type_is_a (extractable_type, G_TYPE_OBJECT));
  g_return_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE));
  g_return_if_fail (callback);

  GST_DEBUG ("Creating asset with extractable type %s and ID=%s",
      g_type_name (extractable_type), id);

  real_id = _check_and_update_parameters (&extractable_type, id, &error);
  if (error) {
    _unsure_material_for_wrong_id (id, extractable_type, error);
    real_id = g_strdup (id);
  }

1136
  /* Check if we already have an asset for this ID */
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1137
1138
  asset = ges_asset_cache_lookup (extractable_type, real_id);
  if (asset) {
1139
    GTask *task = g_task_new (asset, NULL, callback, user_data);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151

    /* In the case of proxied asset, we will loop until we find the
     * last asset of the chain of proxied asset */
    while (TRUE) {
      switch (asset->priv->state) {
        case ASSET_INITIALIZED:
          gst_object_ref (asset);

          GST_DEBUG_OBJECT (asset, "Asset in cache and initialized, "
              "using it");

          /* Takes its own references to @asset */
1152
          g_task_return_boolean (task, TRUE);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1153
1154
1155
1156
1157

          goto done;
        case ASSET_INITIALIZING:
          GST_DEBUG_OBJECT (asset, "Asset in cache and but not "
              "initialized, setting a new callback");
1158
          ges_asset_cache_append_task (extractable_type, real_id, task);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1159
1160

          goto done;
1161
1162
        case ASSET_PROXIED:{
          GESAsset *target = ges_asset_get_proxy (asset);
1163

1164
          if (target == NULL) {
1165
1166
1167
            GST_ERROR ("Asset %s proxied against an asset (%s) we do not"
                " have in cache, something massively screwed",
                asset->priv->id, asset->priv->proxied_asset_id);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1168
1169
1170

            goto done;
          }
1171
          asset = target;
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1172
          break;
1173
        }
1174
1175
1176
1177
1178
1179
        case ASSET_NEEDS_RELOAD:
          GST_DEBUG_OBJECT (asset, "Asset in cache and needs reload");
          ges_asset_cache_append_task (extractable_type, real_id, task);
          GES_ASSET_GET_CLASS (asset)->start_loading (asset, &error);

          goto done;
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1180
        case ASSET_INITIALIZED_WITH_ERROR:
1181
1182
          g_task_return_error (task,
              error ? error : g_error_copy (asset->priv->error));
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199

          goto done;
        default:
          GST_WARNING ("Case %i not handle, returning", asset->priv->state);
          return;
      }
    }
  }

  g_async_initable_new_async (ges_extractable_type_get_asset_type
      (extractable_type), G_PRIORITY_DEFAULT, cancellable, callback, user_data,
      "id", real_id, "extractable-type", extractable_type, NULL);
done:
  if (real_id)
    g_free (real_id);
}

1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
/**
 * ges_asset_needs_reload
 * @extractable_type: The #GType of the object that can be extracted from the
 *  asset to be reloaded.
 * @id: The identifier of the asset to mark as needing reload
 *
 * Sets an asset from the internal cache as needing reload. An asset needs reload
 * in the case where, for example, we were missing a GstPlugin to use it and that
 * plugin has been installed, or, that particular asset content as changed
 * meanwhile (in the case of the usage of proxies).
 *
 * Once an asset has been set as "needs reload", requesting that asset again
 * will lead to it being re discovered, and reloaded as if it was not in the
 * cache before.
 *
 * Returns: %TRUE if the asset was in the cache and could be set as needing reload,
 * %FALSE otherwise.
 */
gboolean
ges_asset_needs_reload (GType extractable_type, const gchar * id)
{
  gchar *real_id;
  GESAsset *asset;
  GError *error = NULL;

  real_id = _check_and_update_parameters (&extractable_type, id, &error);
  if (error) {
    _unsure_material_for_wrong_id (id, extractable_type, error);
    real_id = g_strdup (id);
  }

  asset = ges_asset_cache_lookup (extractable_type, real_id);

  if (real_id) {
    g_free (real_id);
  }

  if (asset) {
    GST_DEBUG_OBJECT (asset,
        "Asset with id %s switch state to ASSET_NEEDS_RELOAD",
        ges_asset_get_id (asset));
    asset->priv->state = ASSET_NEEDS_RELOAD;
1242
    g_clear_error (&asset->priv->error);
1243
1244
1245
1246
1247
1248
1249
    return TRUE;
  }

  GST_DEBUG ("Asset with id %s not found in cache", id);
  return FALSE;
}

Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
/**
 * ges_asset_get_id:
 * @self: The #GESAsset to get ID from
 *
 * Gets the ID of a #GESAsset
 *
 * Returns: The ID of @self
 */
const gchar *
ges_asset_get_id (GESAsset * self)
{
  g_return_val_if_fail (GES_IS_ASSET (self), NULL);

  return self->priv->id;
}

/**
 * ges_asset_extract:
 * @self: The #GESAsset to get extract an object from
 * @error: (allow-none): An error to be set in case something wrong happens or %NULL
 *
 * Extracts a new #GObject from @asset. The type of the object is
 * defined by the extractable-type of @asset, you can check what
 * type will be extracted from @asset using
 * #ges_asset_get_extractable_type
 *
 * Returns: (transfer floating) (allow-none): A newly created #GESExtractable
 */
GESExtractable *
ges_asset_extract (GESAsset * self, GError ** error)
{
  GESExtractable *extractable;

  g_return_val_if_fail (GES_IS_ASSET (self), NULL);
  g_return_val_if_fail (GES_ASSET_GET_CLASS (self)->extract, NULL);

  GST_DEBUG_OBJECT (self, "Extracting asset of type %s",
      g_type_name (self->priv->extractable_type));
  extractable = GES_ASSET_GET_CLASS (self)->extract (self, error);

  if (extractable == NULL)
    return NULL;

1293
1294
  if (ges_extractable_get_asset (extractable) == NULL)
    ges_extractable_set_asset (extractable, self);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322

  return extractable;
}

/**
 * ges_asset_request_finish:
 * @res: The #GAsyncResult from which to get the newly created #GESAsset
 * @error: (out) (allow-none) (transfer full): An error to be set in case
 * something wrong happens or %NULL
 *
 * Finalize the request of an async #GESAsset
 *
 * Returns: (transfer full)(allow-none): The #GESAsset previously requested
 */
GESAsset *
ges_asset_request_finish (GAsyncResult * res, GError ** error)
{
  GObject *object;
  GObject *source_object;

  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);

  source_object = g_async_result_get_source_object (res);
  g_assert (source_object != NULL);

  object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
      res, error);

1323
  gst_object_unref (source_object);
Volodymyr Rudyi's avatar
Volodymyr Rudyi committed
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366

  return GES_ASSET (object);
}

/**
 * ges_list_assets:
 * @filter: Type of assets to list, #GES_TYPE_EXTRACTABLE  will list
 * all assets
 *
 * List all @asset filtering per filter as defined by @filter.
 * It copies the asset and thus will not be updated in time.
 *
 * Returns: (transfer container) (element-type GESAsset): The list of
 * #GESAsset the object contains
 */
GList *
ges_list_assets (GType filter)
{
  GList *ret = NULL;
  GESAsset *asset;
  GHashTableIter iter, types_iter;
  gpointer key, value, typename, assets;

  g_return_val_if_fail (g_type_is_a (filter, GES_TYPE_EXTRACTABLE), NULL);

  LOCK_CACHE;
  g_hash_table_iter_init (&types_iter, type_entries_table);
  while (g_hash_table_iter_next (&types_iter, &typename, &assets)) {
    if (g_type_is_a (filter, g_type_from_name ((gchar *) typename)) == FALSE)
      continue;

    g_hash_table_iter_init (&iter, (GHashTable *) assets);
    while (g_hash_table_iter_next (&iter, &key, &value)) {
      asset = ((GESAssetCacheEntry *) value)->asset;

      if (g_type_is_a (asset->priv->extractable_type, filter))
        ret = g_list_append (ret, asset);
    }
  }
  UNLOCK_CACHE;

  return ret;
}
1367
1368
1369
1370
1371

/**
 * ges_asset_get_error:
 * @self: The asset to retrieve the error from
 *
1372
1373
 * Returns: (transfer none) (nullable): The #GError of the asset or %NULL if
 * the asset was loaded without issue
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
 *
 * Since: 1.8
 */
GError *
ges_asset_get_error (GESAsset * self)
{
  g_return_val_if_fail (GES_IS_ASSET (self), NULL);

  return self->priv->error;
}