ges-uri-asset.c 23.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* GStreamer Editing Services
 *
 * Copyright (C) 2012 Thibault Saunier <thibault.saunier@collabora.com>
 * 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: gesuriclipasset
23
 * @title: GESUriClipAsset
24
 * @short_description: A GESAsset subclass specialized in GESUriClip extraction
25
 *
26
 * The #GESUriClipAsset is a special #GESAsset that lets you handle
27
28
29
30
 * the media file to use inside the GStreamer Editing Services. It has APIs that
 * let you get information about the medias. Also, the tags found in the media file are
 * set as Metadatas of the Asser.
 */
31
32
33
34
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

35
#include <errno.h>
36
37
38
#include <gst/pbutils/pbutils.h>
#include "ges.h"
#include "ges-internal.h"
39
#include "ges-track-element-asset.h"
40

41
42
#define DEFAULT_DISCOVERY_TIMEOUT (60 * GST_SECOND)

43
static GHashTable *parent_newparent_table = NULL;
44

Guillaume Desmottes's avatar
Guillaume Desmottes committed
45
46
47
static GstDiscoverer *discoverer = NULL;
static GstDiscoverer *sync_discoverer = NULL;

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
static void
initable_iface_init (GInitableIface * initable_iface)
{
  /*  We can not iniate synchronously */
  initable_iface->init = NULL;
}

/* TODO: We should monitor files here, and add some way of reporting changes
 * to user
 */
enum
{
  PROP_0,
  PROP_DURATION,
  PROP_LAST
};
static GParamSpec *properties[PROP_LAST];

static void discoverer_discovered_cb (GstDiscoverer * discoverer,
    GstDiscovererInfo * info, GError * err, gpointer user_data);

69
struct _GESUriClipAssetPrivate
70
71
72
73
{
  GstDiscovererInfo *info;
  GstClockTime duration;
  gboolean is_image;
74
75
76
77

  GList *asset_trackfilesources;
};

78
struct _GESUriSourceAssetPrivate
79
80
{
  GstDiscovererStreamInfo *sinfo;
81
  GESUriClipAsset *parent_asset;
82
83

  const gchar *uri;
84
85
};

86
87
88
G_DEFINE_TYPE_WITH_CODE (GESUriClipAsset, ges_uri_clip_asset,
    GES_TYPE_CLIP_ASSET, G_ADD_PRIVATE (GESUriClipAsset)
    G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init));
89
90

static void
91
ges_uri_clip_asset_get_property (GObject * object, guint property_id,
92
93
    GValue * value, GParamSpec * pspec)
{
94
  GESUriClipAssetPrivate *priv = GES_URI_CLIP_ASSET (object)->priv;
95
96
97
98
99
100
101
102
103
104
105

  switch (property_id) {
    case PROP_DURATION:
      g_value_set_uint64 (value, priv->duration);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
106
ges_uri_clip_asset_set_property (GObject * object, guint property_id,
107
108
    const GValue * value, GParamSpec * pspec)
{
109
  GESUriClipAssetPrivate *priv = GES_URI_CLIP_ASSET (object)->priv;
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

  switch (property_id) {
    case PROP_DURATION:
      priv->duration = g_value_get_uint64 (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static GESAssetLoadingReturn
_start_loading (GESAsset * asset, GError ** error)
{
  gboolean ret;
  const gchar *uri;
125
  GESUriClipAssetClass *class = GES_URI_CLIP_ASSET_GET_CLASS (asset);
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

  GST_DEBUG ("Started loading %p", asset);

  uri = ges_asset_get_id (asset);

  ret = gst_discoverer_discover_uri_async (class->discoverer, uri);
  if (ret)
    return GES_ASSET_LOADING_ASYNC;

  return GES_ASSET_LOADING_ERROR;
}

static gboolean
_request_id_update (GESAsset * self, gchar ** proposed_new_id, GError * error)
{
  if (error->domain == GST_RESOURCE_ERROR &&
142
143
      (error->code == GST_RESOURCE_ERROR_NOT_FOUND ||
          error->code == GST_RESOURCE_ERROR_OPEN_READ)) {
144
145
146
147
148
149
150
151
152
153
154
155
    const gchar *uri = ges_asset_get_id (self);
    GFile *parent, *file = g_file_new_for_uri (uri);

    /* Check if we have the new parent in cache */
    parent = g_file_get_parent (file);
    if (parent) {
      GFile *new_parent = g_hash_table_lookup (parent_newparent_table, parent);

      if (new_parent) {
        gchar *basename = g_file_get_basename (file);
        GFile *new_file = g_file_get_child (new_parent, basename);

156
        /* FIXME Handle the GCancellable */
157
        if (g_file_query_exists (new_file, NULL)) {
158
159
160
161
          *proposed_new_id = g_file_get_uri (new_file);
          GST_DEBUG_OBJECT (self, "Proposing path: %s as proxy",
              *proposed_new_id);
        }
162

163
        gst_object_unref (new_file);
164
165
        g_free (basename);
      }
166
      gst_object_unref (parent);
167
168
    }

169
    gst_object_unref (file);
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

    return TRUE;
  }

  return FALSE;
}

static void
_asset_proxied (GESAsset * self, const gchar * new_uri)
{
  const gchar *uri = ges_asset_get_id (self);
  GFile *parent, *new_parent, *new_file = g_file_new_for_uri (new_uri),
      *file = g_file_new_for_uri (uri);

  parent = g_file_get_parent (file);
  new_parent = g_file_get_parent (new_file);
  g_hash_table_insert (parent_newparent_table, parent, new_parent);
187
188
  gst_object_unref (file);
  gst_object_unref (new_file);
189
190
}

Seungha Yang's avatar
Seungha Yang committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
static void
ges_uri_clip_asset_dispose (GObject * object)
{
  GESUriClipAsset *self = GES_URI_CLIP_ASSET (object);
  GESUriClipAssetPrivate *prif = self->priv;

  if (prif->asset_trackfilesources) {
    g_list_free_full (prif->asset_trackfilesources,
        (GDestroyNotify) gst_object_unref);
    prif->asset_trackfilesources = NULL;
  }

  G_OBJECT_CLASS (ges_uri_clip_asset_parent_class)->dispose (object);
}

206
static void
207
ges_uri_clip_asset_class_init (GESUriClipAssetClass * klass)
208
{
209
  GError *err;
210
211
  GstClockTime timeout;
  const gchar *timeout_str;
212
213
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

214
215
  object_class->get_property = ges_uri_clip_asset_get_property;
  object_class->set_property = ges_uri_clip_asset_set_property;
Seungha Yang's avatar
Seungha Yang committed
216
  object_class->dispose = ges_uri_clip_asset_dispose;
217
218
219
220
221
222
223

  GES_ASSET_CLASS (klass)->start_loading = _start_loading;
  GES_ASSET_CLASS (klass)->request_id_update = _request_id_update;
  GES_ASSET_CLASS (klass)->inform_proxy = _asset_proxied;


  /**
224
   * GESUriClipAsset:duration:
225
226
227
228
229
230
231
232
233
   *
   * The duration (in nanoseconds) of the media file
   */
  properties[PROP_DURATION] =
      g_param_spec_uint64 ("duration", "Duration", "The duration to use", 0,
      G_MAXUINT64, GST_CLOCK_TIME_NONE, G_PARAM_READWRITE);
  g_object_class_install_property (object_class, PROP_DURATION,
      properties[PROP_DURATION]);

234
  timeout = DEFAULT_DISCOVERY_TIMEOUT;
235
236
237
238
239
240
241
242
  errno = 0;
  timeout_str = g_getenv ("GES_DISCOVERY_TIMEOUT");
  if (timeout_str)
    timeout = g_ascii_strtod (timeout_str, NULL) * GST_SECOND;
  else
    errno = 10;

  if (errno)
243
    timeout = DEFAULT_DISCOVERY_TIMEOUT;
244

Guillaume Desmottes's avatar
Guillaume Desmottes committed
245
246
247
248
249
250
251
252
253
254
255
  if (!discoverer) {
    discoverer = gst_discoverer_new (timeout, &err);
    if (!discoverer) {
      GST_ERROR ("Could not create discoverer: %s", err->message);
      g_error_free (err);
      return;
    }
  }

  /* The class structure keeps weak pointers on the discoverers so they
   * can be properly cleaned up in _ges_uri_asset_cleanup(). */
256
  if (!klass->discoverer) {
Guillaume Desmottes's avatar
Guillaume Desmottes committed
257
258
259
260
261
262
263
264
265
266
267
268
269
    klass->discoverer = discoverer;
    g_object_add_weak_pointer (G_OBJECT (discoverer),
        (gpointer *) & klass->discoverer);
  }

  if (!sync_discoverer) {
    sync_discoverer = gst_discoverer_new (timeout, &err);

    if (!sync_discoverer) {
      GST_ERROR ("Could not create discoverer: %s", err->message);
      g_error_free (err);
      return;
    }
270
271
272
  }

  if (!klass->sync_discoverer) {
Guillaume Desmottes's avatar
Guillaume Desmottes committed
273
274
275
    klass->sync_discoverer = sync_discoverer;
    g_object_add_weak_pointer (G_OBJECT (sync_discoverer),
        (gpointer *) & klass->sync_discoverer);
276
277
  }

278
279
280
281
282
283
284
  g_signal_connect (klass->discoverer, "discovered",
      G_CALLBACK (discoverer_discovered_cb), NULL);

  /* We just start the discoverer and let it live */
  gst_discoverer_start (klass->discoverer);
  if (parent_newparent_table == NULL) {
    parent_newparent_table = g_hash_table_new_full (g_file_hash,
285
        (GEqualFunc) g_file_equal, g_object_unref, g_object_unref);
286
287
288
289
  }
}

static void
290
ges_uri_clip_asset_init (GESUriClipAsset * self)
291
{
292
  GESUriClipAssetPrivate *priv;
293

294
  priv = self->priv = ges_uri_clip_asset_get_instance_private (self);
295
296
297
298
299
300

  priv->info = NULL;
  priv->duration = GST_CLOCK_TIME_NONE;
  priv->is_image = FALSE;
}

301
static void
302
_create_uri_source_asset (GESUriClipAsset * asset,
303
304
305
    GstDiscovererStreamInfo * sinfo, GESTrackType type)
{
  GESAsset *tck_filesource_asset;
306
  GESUriSourceAssetPrivate *priv_tckasset;
307
  GESUriClipAssetPrivate *priv = asset->priv;
308
309
310
311
312
313
314
315
316
  gchar *stream_id =
      g_strdup (gst_discoverer_stream_info_get_stream_id (sinfo));

  if (stream_id == NULL) {
    GST_WARNING ("No stream ID found, using the pointer instead");

    stream_id = g_strdup_printf ("%i", GPOINTER_TO_INT (sinfo));
  }

317
318
319
320
321
322
  if (type == GES_TRACK_TYPE_VIDEO)
    tck_filesource_asset = ges_asset_request (GES_TYPE_VIDEO_URI_SOURCE,
        stream_id, NULL);
  else
    tck_filesource_asset = ges_asset_request (GES_TYPE_AUDIO_URI_SOURCE,
        stream_id, NULL);
323
324
  g_free (stream_id);

325
  priv_tckasset = GES_URI_SOURCE_ASSET (tck_filesource_asset)->priv;
326
  priv_tckasset->uri = ges_asset_get_id (GES_ASSET (asset));
327
  priv_tckasset->sinfo = gst_object_ref (sinfo);
328
  priv_tckasset->parent_asset = asset;
329
  ges_track_element_asset_set_track_type (GES_TRACK_ELEMENT_ASSET
330
      (tck_filesource_asset), type);
331
332

  priv->asset_trackfilesources = g_list_append (priv->asset_trackfilesources,
Seungha Yang's avatar
Seungha Yang committed
333
      tck_filesource_asset);
334
335
}

336
static void
337
ges_uri_clip_asset_set_info (GESUriClipAsset * self, GstDiscovererInfo * info)
338
339
340
341
{
  GList *tmp, *stream_list;

  GESTrackType supportedformats = GES_TRACK_TYPE_UNKNOWN;
342
  GESUriClipAssetPrivate *priv = GES_URI_CLIP_ASSET (self)->priv;
343
344
345
346

  /* Extract infos from the GstDiscovererInfo */
  stream_list = gst_discoverer_info_get_stream_list (info);
  for (tmp = stream_list; tmp; tmp = tmp->next) {
347
    GESTrackType type = GES_TRACK_TYPE_UNKNOWN;
348
349
350
351
352
353
354
355
    GstDiscovererStreamInfo *sinf = (GstDiscovererStreamInfo *) tmp->data;

    if (GST_IS_DISCOVERER_AUDIO_INFO (sinf)) {
      if (supportedformats == GES_TRACK_TYPE_UNKNOWN)
        supportedformats = GES_TRACK_TYPE_AUDIO;
      else
        supportedformats |= GES_TRACK_TYPE_AUDIO;

356
      type = GES_TRACK_TYPE_AUDIO;
357
358
359
360
361
362
    } else if (GST_IS_DISCOVERER_VIDEO_INFO (sinf)) {
      if (supportedformats == GES_TRACK_TYPE_UNKNOWN)
        supportedformats = GES_TRACK_TYPE_VIDEO;
      else
        supportedformats |= GES_TRACK_TYPE_VIDEO;
      if (gst_discoverer_video_info_is_image ((GstDiscovererVideoInfo *)
363
              sinf))
364
        priv->is_image = TRUE;
365
366
367
      type = GES_TRACK_TYPE_VIDEO;
    }

368
    GST_DEBUG_OBJECT (self, "Creating GESUriSourceAsset for stream: %s",
369
        gst_discoverer_stream_info_get_stream_id (sinf));
370
    _create_uri_source_asset (self, sinf, type);
371
  }
372
  ges_clip_asset_set_supported_formats (GES_CLIP_ASSET
373
      (self), supportedformats);
374
375
376
377
378
379
380
381

  if (stream_list)
    gst_discoverer_stream_info_list_free (stream_list);

  if (priv->is_image == FALSE)
    priv->duration = gst_discoverer_info_get_duration (info);
  /* else we keep #GST_CLOCK_TIME_NONE */

382
  priv->info = gst_object_ref (info);
383
384
}

385
386
387
388
static void
_set_meta_file_size (const gchar * uri, GESUriClipAsset * asset)
{
  GError *error = NULL;
Edward Hervey's avatar
Edward Hervey committed
389
  GFileInfo *file_info = NULL;
390
  guint64 file_size;
Edward Hervey's avatar
Edward Hervey committed
391
  GFile *gfile = NULL;
392
393
394

  GESMetaContainer *container = GES_META_CONTAINER (asset);

Edward Hervey's avatar
Edward Hervey committed
395
396
  gfile = g_file_new_for_uri (uri);
  file_info = g_file_query_info (gfile, "standard::size",
397
398
399
400
401
402
403
404
      G_FILE_QUERY_INFO_NONE, NULL, &error);
  if (!error) {
    file_size = g_file_info_get_attribute_uint64 (file_info, "standard::size");
    ges_meta_container_register_meta_uint64 (container, GES_META_READ_WRITE,
        "file-size", file_size);
  } else {
    g_error_free (error);
  }
Edward Hervey's avatar
Edward Hervey committed
405
406
407
408
  if (gfile)
    g_object_unref (gfile);
  if (file_info)
    g_object_unref (file_info);
409
410
}

411
412
413
414
415
416
static void
_set_meta_foreach (const GstTagList * tags, const gchar * tag,
    GESMetaContainer * container)
{
  GValue value = { 0 };

417
418
419
420
421
422
  if (gst_tag_list_copy_value (&value, tags, tag)) {
    ges_meta_container_set_meta (container, tag, &value);
    g_value_unset (&value);
  } else {
    GST_INFO ("Could not set metadata: %s", tag);
  }
423
424
425
426
427
428
}

static void
discoverer_discovered_cb (GstDiscoverer * discoverer,
    GstDiscovererInfo * info, GError * err, gpointer user_data)
{
429
  GError *error = NULL;
430
431
432
  const GstTagList *tags;

  const gchar *uri = gst_discoverer_info_get_uri (info);
433
434
  GESUriClipAsset *mfs =
      GES_URI_CLIP_ASSET (ges_asset_cache_lookup (GES_TYPE_URI_CLIP, uri));
435
436
437
438
439

  tags = gst_discoverer_info_get_tags (info);
  if (tags)
    gst_tag_list_foreach (tags, (GstTagForeachFunc) _set_meta_foreach, mfs);

440
441
  _set_meta_file_size (uri, mfs);

442
  if (gst_discoverer_info_get_result (info) == GST_DISCOVERER_OK) {
443
    ges_uri_clip_asset_set_info (mfs, info);
444
445
446
447
448
449
450
451
  } else {
    if (err) {
      error = g_error_copy (err);
    } else {
      error = g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_FAILED,
          "Stream %s discovering failed (error code: %d)",
          uri, gst_discoverer_info_get_result (info));
    }
452
453
  }

454
455
456
457
  ges_asset_cache_set_loaded (GES_TYPE_URI_CLIP, uri, error);

  if (error)
    g_error_free (error);
458
459
460
461
}

/* API implementation */
/**
462
 * ges_uri_clip_asset_get_info:
463
464
465
466
467
468
469
 * @self: Target asset
 *
 * Gets #GstDiscovererInfo about the file
 *
 * Returns: (transfer none): #GstDiscovererInfo of specified asset
 */
GstDiscovererInfo *
470
ges_uri_clip_asset_get_info (const GESUriClipAsset * self)
471
{
472
473
  g_return_val_if_fail (GES_IS_URI_CLIP_ASSET (self), NULL);

474
475
476
477
  return self->priv->info;
}

/**
478
479
 * ges_uri_clip_asset_get_duration:
 * @self: a #GESUriClipAsset
480
481
482
483
484
485
 *
 * Gets duration of the file represented by @self
 *
 * Returns: The duration of @self
 */
GstClockTime
486
ges_uri_clip_asset_get_duration (GESUriClipAsset * self)
487
{
488
  g_return_val_if_fail (GES_IS_URI_CLIP_ASSET (self), GST_CLOCK_TIME_NONE);
489
490
491
492
493

  return self->priv->duration;
}

/**
494
 * ges_uri_clip_asset_is_image:
495
 * @self: a #indent: Standard input:311: Error:Unexpected end of file
496
GESUriClipAsset
497
498
499
500
501
502
 *
 * Gets Whether the file represented by @self is an image or not
 *
 * Returns: Whether the file represented by @self is an image or not
 */
gboolean
503
ges_uri_clip_asset_is_image (GESUriClipAsset * self)
504
{
505
  g_return_val_if_fail (GES_IS_URI_CLIP_ASSET (self), FALSE);
506
507
508
509
510

  return self->priv->is_image;
}

/**
511
512
 * ges_uri_clip_asset_new:
 * @uri: The URI of the file for which to create a #GESUriClipAsset
513
514
515
516
 * @cancellable: optional %GCancellable object, %NULL to ignore.
 * @callback: (scope async): a #GAsyncReadyCallback to call when the initialization is finished
 * @user_data: The user data to pass when @callback is called
 *
517
 * Creates a #GESUriClipAsset for @uri
518
 *
519
 * Example of request of a GESUriClipAsset:
520
521
522
523
524
525
 * |[
 * // The request callback
 * static void
 * filesource_asset_loaded_cb (GESAsset * source, GAsyncResult * res, gpointer user_data)
 * {
 *   GError *error = NULL;
526
 *   GESUriClipAsset *filesource_asset;
527
 *
528
 *   filesource_asset = ges_uri_clip_asset_finish (res, &error);
529
530
531
 *   if (filesource_asset) {
 *    g_print ("The file: %s is usable as a FileSource, it is%s an image and lasts %" GST_TIME_FORMAT,
 *        ges_asset_get_id (GES_ASSET (filesource_asset))
532
533
 *        ges_uri_clip_asset_is_image (filesource_asset) ? "" : " not",
 *        GST_TIME_ARGS (ges_uri_clip_asset_get_duration (filesource_asset));
534
535
536
537
538
 *   } else {
 *    g_print ("The file: %s is *not* usable as a FileSource because: %s",
 *        ges_asset_get_id (source), error->message);
 *   }
 *
539
 *   gst_object_unref (mfs);
540
541
542
 * }
 *
 * // The request:
543
 * ges_uri_clip_asset_new (uri, (GAsyncReadyCallback) filesource_asset_loaded_cb, user_data);
544
545
546
 * ]|
 */
void
547
ges_uri_clip_asset_new (const gchar * uri, GCancellable * cancellable,
548
549
    GAsyncReadyCallback callback, gpointer user_data)
{
550
  ges_asset_request_async (GES_TYPE_URI_CLIP, uri, cancellable,
551
552
553
      callback, user_data);
}

554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578

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

  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);

  asset = ges_asset_request_finish (res, error);
  if (asset != NULL) {
    return GES_URI_CLIP_ASSET (asset);
  }

  return NULL;
}

579
/**
580
 * ges_uri_clip_asset_request_sync:
Lubosz Sarnecki's avatar
Lubosz Sarnecki committed
581
582
 * @uri: The URI of the file for which to create a #GESUriClipAsset.
 * You can also use multi file uris for #GESMultiFileSource.
583
 * @error: An error to be set in case something wrong happens or %NULL
584
585
586
587
 *
 * Creates a #GESUriClipAsset for @uri syncronously. You should avoid
 * to use it in application, and rather create #GESUriClipAsset asynchronously
 *
588
589
 * Returns: (transfer full): A reference to the requested asset or %NULL if
 * an error happened
590
591
592
593
594
595
596
597
 */
GESUriClipAsset *
ges_uri_clip_asset_request_sync (const gchar * uri, GError ** error)
{
  GError *lerror = NULL;
  GstDiscovererInfo *info;
  GstDiscoverer *discoverer;
  GESUriClipAsset *asset;
Lubosz Sarnecki's avatar
Lubosz Sarnecki committed
598
  gchar *first_file, *first_file_uri;
599
600
601
602
603
604
605

  asset = GES_URI_CLIP_ASSET (ges_asset_request (GES_TYPE_URI_CLIP, uri,
          &lerror));

  if (asset)
    return asset;

606
607
  if (lerror && lerror->domain == GES_ERROR &&
      lerror->code == GES_ERROR_ASSET_WRONG_ID) {
608
609
610
611
612
613
614
615
    g_propagate_error (error, lerror);

    return NULL;
  }

  asset = g_object_new (GES_TYPE_URI_CLIP_ASSET, "id", uri,
      "extractable-type", GES_TYPE_URI_CLIP, NULL);
  discoverer = GES_URI_CLIP_ASSET_GET_CLASS (asset)->sync_discoverer;
Lubosz Sarnecki's avatar
Lubosz Sarnecki committed
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631

  if (g_str_has_prefix (uri, GES_MULTI_FILE_URI_PREFIX)) {
    GESMultiFileURI *uri_data;

    uri_data = ges_multi_file_uri_new (uri);
    first_file = g_strdup_printf (uri_data->location, uri_data->start);
    first_file_uri = gst_filename_to_uri (first_file, &lerror);
    info = gst_discoverer_discover_uri (discoverer, first_file_uri, &lerror);
    GST_DEBUG ("Got multifile uri. Discovering first file %s", first_file_uri);
    g_free (uri_data);
    g_free (first_file_uri);
    g_free (first_file);
  } else {
    info = gst_discoverer_discover_uri (discoverer, uri, &lerror);
  }

632
633
634
635
636
637
638
639
640
641
  /* We might get a discoverer info but it might have a non-OK result. We
   * should consider that an error */
  if (!lerror && info
      && gst_discoverer_info_get_result (info) != GST_DISCOVERER_OK) {
    lerror =
        g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_FAILED,
        "Stream %s discovering failed (error code: %d)", uri,
        gst_discoverer_info_get_result (info));
  }

642
643
  _set_meta_file_size (uri, asset);

644
645
646
647
  ges_asset_cache_put (gst_object_ref (asset), NULL);
  ges_uri_clip_asset_set_info (asset, info);
  ges_asset_cache_set_loaded (GES_TYPE_URI_CLIP, uri, lerror);

648
649
  if (info == NULL || lerror != NULL) {
    gst_object_unref (asset);
650
651
    if (info)
      gst_discoverer_info_unref (info);
652
653
654
655
656
657
    if (lerror)
      g_propagate_error (error, lerror);

    return NULL;
  }

658
659
  gst_discoverer_info_unref (info);

660
661
662
  return asset;
}

663
/**
664
 * ges_uri_clip_asset_class_set_timeout:
Lubosz Sarnecki's avatar
Lubosz Sarnecki committed
665
 * @klass: The #GESUriClipAssetClass on which to set the discoverer timeout
666
667
 * @timeout: The timeout to set
 *
668
 * Sets the timeout of #GESUriClipAsset loading
669
670
 */
void
Lubosz Sarnecki's avatar
Lubosz Sarnecki committed
671
ges_uri_clip_asset_class_set_timeout (GESUriClipAssetClass * klass,
672
673
    GstClockTime timeout)
{
Lubosz Sarnecki's avatar
Lubosz Sarnecki committed
674
  g_return_if_fail (GES_IS_URI_CLIP_ASSET_CLASS (klass));
675

Lubosz Sarnecki's avatar
Lubosz Sarnecki committed
676
677
  g_object_set (klass->discoverer, "timeout", timeout, NULL);
  g_object_set (klass->sync_discoverer, "timeout", timeout, NULL);
678
}
679
680

/**
681
682
 * ges_uri_clip_asset_get_stream_assets:
 * @self: A #GESUriClipAsset
683
 *
684
 * Get the GESUriSourceAsset @self containes
685
 *
686
687
 * Returns: (transfer none) (element-type GESUriSourceAsset): a
 * #GList of #GESUriSourceAsset
688
689
 */
const GList *
690
ges_uri_clip_asset_get_stream_assets (GESUriClipAsset * self)
691
{
692
  g_return_val_if_fail (GES_IS_URI_CLIP_ASSET (self), FALSE);
693
694
695
696
697

  return self->priv->asset_trackfilesources;
}

/*****************************************************************
698
 *            GESUriSourceAsset implementation             *
699
700
 *****************************************************************/
/**
701
 * SECTION: gesurisourceasset
702
 * @title: GESUriClipAsset
703
 * @short_description: A GESAsset subclass specialized in GESUriSource extraction
704
705
 *
 * NOTE: You should never request such a #GESAsset as they will be created automatically
706
 * by #GESUriClipAsset-s.
707
708
 */

709
G_DEFINE_TYPE_WITH_PRIVATE (GESUriSourceAsset, ges_uri_source_asset,
710
    GES_TYPE_TRACK_ELEMENT_ASSET);
711
712
713
714

static GESExtractable *
_extract (GESAsset * asset, GError ** error)
{
Justin Kim's avatar
Justin Kim committed
715
  gchar *uri = NULL;
716
  GESTrackElement *trackelement;
717
  GESUriSourceAssetPrivate *priv = GES_URI_SOURCE_ASSET (asset)->priv;
718
719
720
721
722
723
724
725
726
727
728
729
730

  if (GST_IS_DISCOVERER_STREAM_INFO (priv->sinfo) == FALSE) {
    GST_WARNING_OBJECT (asset, "Can not extract as no strean info set");

    return NULL;
  }

  if (priv->uri == NULL) {
    GST_WARNING_OBJECT (asset, "Can not extract as no uri set");

    return NULL;
  }

Justin Kim's avatar
Justin Kim committed
731
732
  uri = g_strdup (priv->uri);

Lubosz Sarnecki's avatar
Lubosz Sarnecki committed
733
  if (g_str_has_prefix (priv->uri, GES_MULTI_FILE_URI_PREFIX)) {
Justin Kim's avatar
Justin Kim committed
734
    trackelement = GES_TRACK_ELEMENT (ges_multi_file_source_new (uri));
Lubosz Sarnecki's avatar
Lubosz Sarnecki committed
735
736
  } else if (GST_IS_DISCOVERER_VIDEO_INFO (priv->sinfo)
      && gst_discoverer_video_info_is_image ((GstDiscovererVideoInfo *)
737
          priv->sinfo))
Justin Kim's avatar
Justin Kim committed
738
    trackelement = GES_TRACK_ELEMENT (ges_image_source_new (uri));
739
  else if (GST_IS_DISCOVERER_VIDEO_INFO (priv->sinfo))
Justin Kim's avatar
Justin Kim committed
740
    trackelement = GES_TRACK_ELEMENT (ges_video_uri_source_new (uri));
741
  else
Justin Kim's avatar
Justin Kim committed
742
    trackelement = GES_TRACK_ELEMENT (ges_audio_uri_source_new (uri));
743

744
745
  ges_track_element_set_track_type (trackelement,
      ges_track_element_asset_get_track_type (GES_TRACK_ELEMENT_ASSET (asset)));
746

Justin Kim's avatar
Justin Kim committed
747
748
  g_free (uri);

749
  return GES_EXTRACTABLE (trackelement);
750
751
752
}

static void
753
ges_uri_source_asset_class_init (GESUriSourceAssetClass * klass)
754
755
756
757
758
{
  GES_ASSET_CLASS (klass)->extract = _extract;
}

static void
759
ges_uri_source_asset_init (GESUriSourceAsset * self)
760
{
761
  GESUriSourceAssetPrivate *priv;
762

763
  priv = self->priv = ges_uri_source_asset_get_instance_private (self);
764
765
766
767
768
769

  priv->sinfo = NULL;
  priv->parent_asset = NULL;
  priv->uri = NULL;
}

Thibault Saunier's avatar
Thibault Saunier committed
770
/**
771
 * ges_uri_source_asset_get_stream_info:
772
 * @asset: A #GESUriClipAsset
Thibault Saunier's avatar
Thibault Saunier committed
773
774
775
 *
 * Get the #GstDiscovererStreamInfo user by @asset
 *
776
 * Returns: (transfer none): a #GESUriClipAsset
Thibault Saunier's avatar
Thibault Saunier committed
777
 */
778
GstDiscovererStreamInfo *
779
ges_uri_source_asset_get_stream_info (GESUriSourceAsset * asset)
780
{
781
  g_return_val_if_fail (GES_IS_URI_SOURCE_ASSET (asset), NULL);
782
783
784
785
786

  return asset->priv->sinfo;
}

const gchar *
787
ges_uri_source_asset_get_stream_uri (GESUriSourceAsset * asset)
788
{
789
  g_return_val_if_fail (GES_IS_URI_SOURCE_ASSET (asset), NULL);
790
791
792
793
794

  return asset->priv->uri;
}

/**
795
 * ges_uri_source_asset_get_filesource_asset:
796
 * @asset: A #GESUriClipAsset
797
 *
798
 * Get the #GESUriClipAsset @self is contained in
799
 *
800
 * Returns: a #GESUriClipAsset
801
 */
802
const GESUriClipAsset *
803
ges_uri_source_asset_get_filesource_asset (GESUriSourceAsset * asset)
804
{
805
  g_return_val_if_fail (GES_IS_URI_SOURCE_ASSET (asset), NULL);
806
807
808

  return asset->priv->parent_asset;
}
Guillaume Desmottes's avatar
Guillaume Desmottes committed
809
810
811
812
813
814

void
_ges_uri_asset_cleanup (void)
{
  g_clear_object (&discoverer);
  g_clear_object (&sync_discoverer);
815
816
817
818
  if (parent_newparent_table) {
    g_hash_table_destroy (parent_newparent_table);
    parent_newparent_table = NULL;
  }
Guillaume Desmottes's avatar
Guillaume Desmottes committed
819
}