command-line-formatter: Stop uselessly looping over options
[platform/upstream/gstreamer.git] / ges / ges-uri-clip.c
1 /* GStreamer Editing Services
2  * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3  *               2009 Nokia Corporation
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * SECTION: gesuriclip
23  * @title: GESUriClip
24  * @short_description: An object for manipulating media files in a GESTimeline
25  *
26  * Represents all the output streams from a particular uri. It is assumed that
27  * the URI points to a file of some type.
28  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "ges-internal.h"
34 #include "ges-uri-clip.h"
35 #include "ges-source-clip.h"
36 #include "ges-video-uri-source.h"
37 #include "ges-audio-uri-source.h"
38 #include "ges-uri-asset.h"
39 #include "ges-track-element-asset.h"
40 #include "ges-extractable.h"
41 #include "ges-image-source.h"
42 #include "ges-audio-test-source.h"
43 #include "ges-multi-file-source.h"
44 #include "ges-layer.h"
45
46 static void ges_extractable_interface_init (GESExtractableInterface * iface);
47
48 #define parent_class ges_uri_clip_parent_class
49
50 struct _GESUriClipPrivate
51 {
52   gchar *uri;
53
54   gboolean mute;
55   gboolean is_image;
56 };
57
58 enum
59 {
60   PROP_0,
61   PROP_URI,
62   PROP_MUTE,
63   PROP_IS_IMAGE,
64   PROP_SUPPORTED_FORMATS,
65 };
66
67 G_DEFINE_TYPE_WITH_CODE (GESUriClip, ges_uri_clip,
68     GES_TYPE_SOURCE_CLIP, G_ADD_PRIVATE (GESUriClip)
69     G_IMPLEMENT_INTERFACE (GES_TYPE_EXTRACTABLE,
70         ges_extractable_interface_init));
71
72 static GList *ges_uri_clip_create_track_elements (GESClip *
73     clip, GESTrackType type);
74 static void ges_uri_clip_set_uri (GESUriClip * self, gchar * uri);
75
76 gboolean
77 uri_clip_set_max_duration (GESTimelineElement * element,
78     GstClockTime maxduration);
79
80 static void
81 ges_uri_clip_get_property (GObject * object, guint property_id,
82     GValue * value, GParamSpec * pspec)
83 {
84   GESUriClipPrivate *priv = GES_URI_CLIP (object)->priv;
85
86   switch (property_id) {
87     case PROP_URI:
88       g_value_set_string (value, priv->uri);
89       break;
90     case PROP_MUTE:
91       g_value_set_boolean (value, priv->mute);
92       break;
93     case PROP_IS_IMAGE:
94       g_value_set_boolean (value, priv->is_image);
95       break;
96     case PROP_SUPPORTED_FORMATS:
97       g_value_set_flags (value,
98           ges_clip_get_supported_formats (GES_CLIP (object)));
99       break;
100     default:
101       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
102   }
103 }
104
105 static void
106 ges_uri_clip_set_property (GObject * object, guint property_id,
107     const GValue * value, GParamSpec * pspec)
108 {
109   GESUriClip *uriclip = GES_URI_CLIP (object);
110
111   switch (property_id) {
112     case PROP_URI:
113       ges_uri_clip_set_uri (uriclip, g_value_dup_string (value));
114       break;
115     case PROP_MUTE:
116       ges_uri_clip_set_mute (uriclip, g_value_get_boolean (value));
117       break;
118     case PROP_IS_IMAGE:
119       ges_uri_clip_set_is_image (uriclip, g_value_get_boolean (value));
120       break;
121     case PROP_SUPPORTED_FORMATS:
122       ges_clip_set_supported_formats (GES_CLIP (uriclip),
123           g_value_get_flags (value));
124       break;
125     default:
126       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
127   }
128 }
129
130 static void
131 ges_uri_clip_finalize (GObject * object)
132 {
133   GESUriClipPrivate *priv = GES_URI_CLIP (object)->priv;
134
135   if (priv->uri)
136     g_free (priv->uri);
137   G_OBJECT_CLASS (parent_class)->finalize (object);
138 }
139
140 static void
141 ges_uri_clip_class_init (GESUriClipClass * klass)
142 {
143   GObjectClass *object_class = G_OBJECT_CLASS (klass);
144   GESClipClass *clip_class = GES_CLIP_CLASS (klass);
145   GESTimelineElementClass *element_class = GES_TIMELINE_ELEMENT_CLASS (klass);
146
147   object_class->get_property = ges_uri_clip_get_property;
148   object_class->set_property = ges_uri_clip_set_property;
149   object_class->finalize = ges_uri_clip_finalize;
150
151
152   /**
153    * GESUriClip:uri:
154    *
155    * The location of the file/resource to use.
156    */
157   g_object_class_install_property (object_class, PROP_URI,
158       g_param_spec_string ("uri", "URI", "uri of the resource", NULL,
159           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
160
161   /**
162    * GESUriClip:mute:
163    *
164    * Whether the sound will be played or not.
165    */
166   g_object_class_install_property (object_class, PROP_MUTE,
167       g_param_spec_boolean ("mute", "Mute", "Mute audio track",
168           FALSE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
169
170   /**
171    * GESUriClip:is-image:
172    *
173    * Whether this uri clip represents a still image or not. This must be set
174    * before create_track_elements is called.
175    */
176   g_object_class_install_property (object_class, PROP_IS_IMAGE,
177       g_param_spec_boolean ("is-image", "Is still image",
178           "Whether the clip represents a still image or not",
179           FALSE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
180
181   /* Redefine the supported formats property so the default value is UNKNOWN
182    * and not AUDIO | VIDEO */
183   g_object_class_install_property (object_class, PROP_SUPPORTED_FORMATS,
184       g_param_spec_flags ("supported-formats",
185           "Supported formats", "Formats supported by the file",
186           GES_TYPE_TRACK_TYPE, GES_TRACK_TYPE_UNKNOWN,
187           G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
188
189   element_class->set_max_duration = uri_clip_set_max_duration;
190
191   clip_class->create_track_elements = ges_uri_clip_create_track_elements;
192 }
193
194 static gchar *
195 extractable_check_id (GType type, const gchar * id)
196 {
197   if (gst_uri_is_valid (id))
198     return g_strdup (id);
199
200   return NULL;
201 }
202
203 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;       /* Start ignoring GParameter deprecation */
204
205 static GParameter *
206 extractable_get_parameters_from_id (const gchar * id, guint * n_params)
207 {
208   GParameter *params = g_new0 (GParameter, 2);
209
210   params[0].name = "uri";
211   g_value_init (&params[0].value, G_TYPE_STRING);
212   g_value_set_string (&params[0].value, id);
213
214   *n_params = 1;
215
216   return params;
217 }
218
219 G_GNUC_END_IGNORE_DEPRECATIONS; /* End ignoring GParameter deprecation */
220
221 static gchar *
222 extractable_get_id (GESExtractable * self)
223 {
224   return g_strdup (GES_URI_CLIP (self)->priv->uri);
225 }
226
227 static GList *
228 get_auto_transitions_around_source (GESTrackElement * child)
229 {
230   GList *transitions = NULL;
231   GESTimeline *timeline = GES_TIMELINE_ELEMENT_TIMELINE (child);
232   gint i;
233   GESEdge edges[] = { GES_EDGE_START, GES_EDGE_END };
234
235   if (!timeline)
236     return NULL;
237
238   for (i = 0; i < G_N_ELEMENTS (edges); i++) {
239     GESAutoTransition *transition =
240         ges_timeline_get_auto_transition_at_edge (timeline, child, edges[i]);
241     if (transition)
242       transitions = g_list_prepend (transitions, transition);
243   }
244
245   return transitions;
246 }
247
248 static gboolean
249 extractable_set_asset (GESExtractable * self, GESAsset * asset)
250 {
251   gboolean res = TRUE, contains_core;
252   GESUriClip *uriclip = GES_URI_CLIP (self);
253   GESUriClipAsset *uri_clip_asset;
254   GESClip *clip = GES_CLIP (self);
255   GESContainer *container = GES_CONTAINER (clip);
256   GESTimelineElement *element = GES_TIMELINE_ELEMENT (self);
257   GESLayer *layer = ges_clip_get_layer (clip);
258   GList *tmp, *children;
259   GHashTable *source_by_track, *auto_transitions_on_sources;
260   GstClockTime max_duration;
261   GESAsset *prev_asset;
262   GList *transitions = NULL;
263   GESTimeline *timeline = GES_TIMELINE_ELEMENT_TIMELINE (self);
264
265   g_return_val_if_fail (GES_IS_URI_CLIP_ASSET (asset), FALSE);
266
267   uri_clip_asset = GES_URI_CLIP_ASSET (asset);
268
269   /* new sources elements will have their max-duration set to
270    * max_duration. Check that this is possible with the new uri
271    * NOTE: we are assuming that all the new core children will end up
272    * in the same tracks as the previous core children */
273   max_duration = ges_uri_clip_asset_get_max_duration (uri_clip_asset);
274   if (!ges_clip_can_set_max_duration_of_all_core (clip, max_duration, NULL)) {
275     GST_INFO_OBJECT (self, "Can not set asset to %p as its max-duration %"
276         GST_TIME_FORMAT " is too low", asset, GST_TIME_ARGS (max_duration));
277
278     return FALSE;
279   }
280
281   if (!container->children && !GST_CLOCK_TIME_IS_VALID (element->duration)) {
282     if (!ges_timeline_element_set_duration (element,
283             ges_uri_clip_asset_get_duration (uri_clip_asset))) {
284       GST_ERROR_OBJECT (self, "Failed to set the duration using a new "
285           "uri asset when we have no children. This should not happen");
286       return FALSE;
287     }
288   }
289
290   ges_uri_clip_set_is_image (uriclip,
291       ges_uri_clip_asset_is_image (uri_clip_asset));
292
293   if (ges_clip_get_supported_formats (clip) == GES_TRACK_TYPE_UNKNOWN) {
294     ges_clip_set_supported_formats (clip,
295         ges_clip_asset_get_supported_formats (GES_CLIP_ASSET (uri_clip_asset)));
296   }
297
298   prev_asset = element->asset;
299   element->asset = asset;
300
301   /* FIXME: it would be much better if we could have a way to replace
302    * each source one-to-one with a new source in the same track, e.g.
303    * a user supplied
304    * GESSource * ges_uri_clip_swap_source (
305    *   GESClip * clip, GESSource * replace, GList * new_sources,
306    *   gpointer user_data)
307    *
308    * and they select a new source from new_sources to replace @replace, or
309    * %NULL to remove it without a replacement. The default would swap
310    * one video for another video, etc.
311    *
312    * Then we could use this information with
313    * ges_clip_can_update_duration_limit, using the new max-duration and
314    * replacing each source in the same track, to test that the operation
315    * can succeed (basically extending
316    * ges_clip_can_set_max_duration_of_all_core, but with the added
317    * information that sources without a replacement will not contribute
318    * to the duration-limit, and all of the siblings in the same track will
319    * also be removed from the track).
320    *
321    * Then we can perform the replacement, whilst avoiding track-selection
322    * (similar to GESClip's _transfer_child). */
323
324   source_by_track = g_hash_table_new_full (NULL, NULL,
325       gst_object_unref, gst_object_unref);
326   auto_transitions_on_sources = g_hash_table_new_full (NULL, NULL,
327       gst_object_unref, (GDestroyNotify) g_list_free);
328
329   if (timeline)
330     ges_timeline_freeze_auto_transitions (timeline, TRUE);
331
332   children = ges_container_get_children (container, FALSE);
333   for (tmp = children; tmp; tmp = tmp->next) {
334     GESTrackElement *child = tmp->data;
335     GESTrack *track;
336
337     /* remove our core children */
338     if (!ges_track_element_is_core (child))
339       continue;
340
341     track = ges_track_element_get_track (child);
342     if (track)
343       g_hash_table_insert (source_by_track, gst_object_ref (track),
344           gst_object_ref (child));
345
346     transitions = get_auto_transitions_around_source (child);
347     if (transitions)
348       g_hash_table_insert (auto_transitions_on_sources, gst_object_ref (child),
349           transitions);
350
351     /* removing the track element from its clip whilst it is in a
352      * timeline will remove it from its track */
353     /* removing the core element will also empty its non-core siblings
354      * from the same track */
355     ges_container_remove (container, GES_TIMELINE_ELEMENT (child));
356   }
357   g_list_free_full (children, g_object_unref);
358
359   contains_core = FALSE;
360
361   /* keep alive */
362   gst_object_ref (self);
363   if (layer) {
364
365     res = ges_layer_remove_clip (layer, clip);
366
367     if (res) {
368       /* adding back to the layer will trigger the re-creation of the core
369        * children */
370       res = ges_layer_add_clip (layer, clip);
371
372       if (!res)
373         GST_ERROR_OBJECT (self, "Failed to add the uri clip %s back into "
374             "its layer. This is likely caused by track-selection for the "
375             "core sources and effects failing because the core sources "
376             "were not replaced in the same tracks", element->name);
377
378       /* NOTE: assume that core children in the same tracks correspond to
379        * the same source! */
380       for (tmp = container->children; tmp; tmp = tmp->next) {
381         GESTrackElement *child = tmp->data;
382         GESTrackElement *orig_source;
383
384         if (!ges_track_element_is_core (child))
385           continue;
386
387         contains_core = TRUE;
388         orig_source = g_hash_table_lookup (source_by_track,
389             ges_track_element_get_track (child));
390
391         if (!orig_source)
392           continue;
393
394         ges_track_element_copy_properties (GES_TIMELINE_ELEMENT
395             (orig_source), GES_TIMELINE_ELEMENT (child));
396         ges_track_element_copy_bindings (orig_source, child,
397             GST_CLOCK_TIME_NONE);
398
399         transitions =
400             g_hash_table_lookup (auto_transitions_on_sources, orig_source);
401         for (; transitions; transitions = transitions->next) {
402           GESAutoTransition *transition = transitions->data;
403
404           if (transition->previous_source == orig_source)
405             ges_auto_transition_set_source (transition, child, GES_EDGE_START);
406           else if (transition->next_source == orig_source)
407             ges_auto_transition_set_source (transition, child, GES_EDGE_END);
408         }
409       }
410     } else {
411       GST_ERROR_OBJECT (self, "Failed to remove from the layer. This "
412           "should not happen");
413     }
414     gst_object_unref (layer);
415   }
416   g_hash_table_unref (source_by_track);
417   g_hash_table_unref (auto_transitions_on_sources);
418
419   if (timeline)
420     ges_timeline_freeze_auto_transitions (timeline, FALSE);
421
422   if (res) {
423     g_free (uriclip->priv->uri);
424     uriclip->priv->uri = g_strdup (ges_asset_get_id (asset));
425
426     if (!contains_core) {
427       if (!ges_timeline_element_set_max_duration (element, max_duration))
428         GST_ERROR_OBJECT (self, "Failed to set the max-duration on the uri "
429             "clip when it has no children. This should not happen");
430     }
431   } else {
432     element->asset = prev_asset;
433   }
434
435   /* if re-adding failed, clip may be destroyed */
436   gst_object_unref (self);
437
438   return res;
439 }
440
441 static void
442 ges_extractable_interface_init (GESExtractableInterface * iface)
443 {
444   iface->asset_type = GES_TYPE_URI_CLIP_ASSET;
445   iface->check_id = (GESExtractableCheckId) extractable_check_id;
446   iface->get_parameters_from_id = extractable_get_parameters_from_id;
447   iface->get_id = extractable_get_id;
448   iface->get_id = extractable_get_id;
449   iface->can_update_asset = TRUE;
450   iface->set_asset_full = extractable_set_asset;
451 }
452
453 static void
454 ges_uri_clip_init (GESUriClip * self)
455 {
456   self->priv = ges_uri_clip_get_instance_private (self);
457
458   /* Setting the duration to -1 by default. */
459   GES_TIMELINE_ELEMENT (self)->duration = GST_CLOCK_TIME_NONE;
460 }
461
462 /**
463  * ges_uri_clip_set_mute:
464  * @self: the #GESUriClip on which to mute or unmute the audio track
465  * @mute: %TRUE to mute @self audio track, %FALSE to unmute it
466  *
467  * Sets whether the audio track of this clip is muted or not.
468  *
469  */
470 void
471 ges_uri_clip_set_mute (GESUriClip * self, gboolean mute)
472 {
473   GList *tmp;
474
475   GST_DEBUG ("self:%p, mute:%d", self, mute);
476
477   self->priv->mute = mute;
478
479   /* Go over tracked objects, and update 'active' status on all audio objects */
480   for (tmp = GES_CONTAINER_CHILDREN (self); tmp; tmp = g_list_next (tmp)) {
481     GESTrackElement *trackelement = (GESTrackElement *) tmp->data;
482     GESTrack *track = ges_track_element_get_track (trackelement);
483
484     if (track && track->type == GES_TRACK_TYPE_AUDIO)
485       ges_track_element_set_active (trackelement, !mute);
486   }
487 }
488
489 gboolean
490 uri_clip_set_max_duration (GESTimelineElement * element,
491     GstClockTime maxduration)
492 {
493   gboolean ret =
494       GES_TIMELINE_ELEMENT_CLASS (parent_class)->set_max_duration (element,
495       maxduration);
496
497   if (ret) {
498     GstClockTime limit = ges_clip_get_duration_limit (GES_CLIP (element));
499     if (GST_CLOCK_TIME_IS_VALID (limit) && (element->duration == 0))
500       _set_duration0 (element, limit);
501   }
502
503   return ret;
504 }
505
506 /**
507  * ges_uri_clip_set_is_image:
508  * @self: the #GESUriClip
509  * @is_image: %TRUE if @self is a still image, %FALSE otherwise
510  *
511  * Sets whether the clip is a still image or not.
512  */
513 void
514 ges_uri_clip_set_is_image (GESUriClip * self, gboolean is_image)
515 {
516   self->priv->is_image = is_image;
517 }
518
519 /**
520  * ges_uri_clip_is_muted:
521  * @self: the #GESUriClip
522  *
523  * Lets you know if the audio track of @self is muted or not.
524  *
525  * Returns: %TRUE if the audio track of @self is muted, %FALSE otherwise.
526  */
527 gboolean
528 ges_uri_clip_is_muted (GESUriClip * self)
529 {
530   return self->priv->mute;
531 }
532
533 /**
534  * ges_uri_clip_is_image:
535  * @self: the #GESUriClip
536  *
537  * Lets you know if @self is an image or not.
538  *
539  * Returns: %TRUE if @self is a still image %FALSE otherwise.
540  */
541 gboolean
542 ges_uri_clip_is_image (GESUriClip * self)
543 {
544   return self->priv->is_image;
545 }
546
547 /**
548  * ges_uri_clip_get_uri:
549  * @self: the #GESUriClip
550  *
551  * Get the location of the resource.
552  *
553  * Returns: The location of the resource.
554  */
555 const gchar *
556 ges_uri_clip_get_uri (GESUriClip * self)
557 {
558   return self->priv->uri;
559 }
560
561 static GList *
562 ges_uri_clip_create_track_elements (GESClip * clip, GESTrackType type)
563 {
564   GList *res = NULL;
565   const GList *tmp, *stream_assets;
566   GESAsset *asset = GES_TIMELINE_ELEMENT (clip)->asset;
567   GESUriClipAsset *uri_asset;
568   GstClockTime max_duration;
569
570   g_return_val_if_fail (asset, NULL);
571
572   uri_asset = GES_URI_CLIP_ASSET (asset);
573
574   max_duration = ges_uri_clip_asset_get_max_duration (uri_asset);
575   stream_assets = ges_uri_clip_asset_get_stream_assets (uri_asset);
576
577   for (tmp = stream_assets; tmp; tmp = tmp->next) {
578     GESTrackElementAsset *element_asset = GES_TRACK_ELEMENT_ASSET (tmp->data);
579
580     if (ges_track_element_asset_get_track_type (element_asset) == type) {
581       GESTrackElement *element =
582           GES_TRACK_ELEMENT (ges_asset_extract (GES_ASSET (element_asset),
583               NULL));
584       ges_timeline_element_set_max_duration (GES_TIMELINE_ELEMENT (element),
585           max_duration);
586       res = g_list_prepend (res, element);
587     }
588   }
589
590   return res;
591 }
592
593 /**
594  * ges_uri_clip_new:
595  * @uri: the URI the source should control
596  *
597  * Creates a new #GESUriClip for the provided @uri.
598  *
599  * > **WARNING**: This function might 'discover` @uri **synchrounously**, it is
600  * > an IO and processing intensive task that you probably don't want to run in
601  * > an application mainloop. Have a look at #ges_asset_request_async to see how
602  * > to make that operation happen **asynchronously**.
603  *
604  * Returns: (transfer floating) (nullable): The newly created #GESUriClip, or
605  * %NULL if there was an error.
606  */
607 GESUriClip *
608 ges_uri_clip_new (const gchar * uri)
609 {
610   GESAsset *asset = GES_ASSET (ges_uri_clip_asset_request_sync (uri, NULL));
611   GESUriClip *res = NULL;
612
613   if (asset) {
614     res = GES_URI_CLIP (ges_asset_extract (asset, NULL));
615     gst_object_unref (asset);
616   } else
617     GST_ERROR ("Could not create asset for uri: %s", uri);
618
619   return res;
620 }
621
622 void
623 ges_uri_clip_set_uri (GESUriClip * self, gchar * uri)
624 {
625   if (GES_CONTAINER_CHILDREN (self)) {
626     /* FIXME handle this case properly */
627     GST_WARNING_OBJECT (self, "Can not change uri when already"
628         "containing TrackElements");
629
630     return;
631   }
632
633   self->priv->uri = uri;
634 }