1 /* GStreamer Editing Services
2 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3 * 2009 Nokia Corporation
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.
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.
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.
24 * @short_description: An object for manipulating media files in a GESTimeline
26 * Represents all the output streams from a particular uri. It is assumed that
27 * the URI points to a file of some type.
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"
46 static void ges_extractable_interface_init (GESExtractableInterface * iface);
48 #define parent_class ges_uri_clip_parent_class
50 struct _GESUriClipPrivate
64 PROP_SUPPORTED_FORMATS,
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));
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);
77 uri_clip_set_max_duration (GESTimelineElement * element,
78 GstClockTime maxduration);
81 ges_uri_clip_get_property (GObject * object, guint property_id,
82 GValue * value, GParamSpec * pspec)
84 GESUriClipPrivate *priv = GES_URI_CLIP (object)->priv;
86 switch (property_id) {
88 g_value_set_string (value, priv->uri);
91 g_value_set_boolean (value, priv->mute);
94 g_value_set_boolean (value, priv->is_image);
96 case PROP_SUPPORTED_FORMATS:
97 g_value_set_flags (value,
98 ges_clip_get_supported_formats (GES_CLIP (object)));
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
106 ges_uri_clip_set_property (GObject * object, guint property_id,
107 const GValue * value, GParamSpec * pspec)
109 GESUriClip *uriclip = GES_URI_CLIP (object);
111 switch (property_id) {
113 ges_uri_clip_set_uri (uriclip, g_value_dup_string (value));
116 ges_uri_clip_set_mute (uriclip, g_value_get_boolean (value));
119 ges_uri_clip_set_is_image (uriclip, g_value_get_boolean (value));
121 case PROP_SUPPORTED_FORMATS:
122 ges_clip_set_supported_formats (GES_CLIP (uriclip),
123 g_value_get_flags (value));
126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
131 ges_uri_clip_finalize (GObject * object)
133 GESUriClipPrivate *priv = GES_URI_CLIP (object)->priv;
137 G_OBJECT_CLASS (parent_class)->finalize (object);
141 ges_uri_clip_class_init (GESUriClipClass * klass)
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);
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;
155 * The location of the file/resource to use.
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));
164 * Whether the sound will be played or not.
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));
171 * GESUriClip:is-image:
173 * Whether this uri clip represents a still image or not. This must be set
174 * before create_track_elements is called.
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));
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));
189 element_class->set_max_duration = uri_clip_set_max_duration;
191 clip_class->create_track_elements = ges_uri_clip_create_track_elements;
195 extractable_check_id (GType type, const gchar * id)
197 if (gst_uri_is_valid (id))
198 return g_strdup (id);
203 G_GNUC_BEGIN_IGNORE_DEPRECATIONS; /* Start ignoring GParameter deprecation */
206 extractable_get_parameters_from_id (const gchar * id, guint * n_params)
208 GParameter *params = g_new0 (GParameter, 2);
210 params[0].name = "uri";
211 g_value_init (¶ms[0].value, G_TYPE_STRING);
212 g_value_set_string (¶ms[0].value, id);
219 G_GNUC_END_IGNORE_DEPRECATIONS; /* End ignoring GParameter deprecation */
222 extractable_get_id (GESExtractable * self)
224 return g_strdup (GES_URI_CLIP (self)->priv->uri);
228 get_auto_transitions_around_source (GESTrackElement * child)
230 GList *transitions = NULL;
231 GESTimeline *timeline = GES_TIMELINE_ELEMENT_TIMELINE (child);
233 GESEdge edges[] = { GES_EDGE_START, GES_EDGE_END };
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]);
242 transitions = g_list_prepend (transitions, transition);
249 extractable_set_asset (GESExtractable * self, GESAsset * asset)
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);
265 g_return_val_if_fail (GES_IS_URI_CLIP_ASSET (asset), FALSE);
267 uri_clip_asset = GES_URI_CLIP_ASSET (asset);
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));
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");
290 ges_uri_clip_set_is_image (uriclip,
291 ges_uri_clip_asset_is_image (uri_clip_asset));
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)));
298 prev_asset = element->asset;
299 element->asset = asset;
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.
304 * GESSource * ges_uri_clip_swap_source (
305 * GESClip * clip, GESSource * replace, GList * new_sources,
306 * gpointer user_data)
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.
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).
321 * Then we can perform the replacement, whilst avoiding track-selection
322 * (similar to GESClip's _transfer_child). */
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);
330 ges_timeline_freeze_auto_transitions (timeline, TRUE);
332 children = ges_container_get_children (container, FALSE);
333 for (tmp = children; tmp; tmp = tmp->next) {
334 GESTrackElement *child = tmp->data;
337 /* remove our core children */
338 if (!ges_track_element_is_core (child))
341 track = ges_track_element_get_track (child);
343 g_hash_table_insert (source_by_track, gst_object_ref (track),
344 gst_object_ref (child));
346 transitions = get_auto_transitions_around_source (child);
348 g_hash_table_insert (auto_transitions_on_sources, gst_object_ref (child),
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));
357 g_list_free_full (children, g_object_unref);
359 contains_core = FALSE;
362 gst_object_ref (self);
365 res = ges_layer_remove_clip (layer, clip);
368 /* adding back to the layer will trigger the re-creation of the core
370 res = ges_layer_add_clip (layer, clip);
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);
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;
384 if (!ges_track_element_is_core (child))
387 contains_core = TRUE;
388 orig_source = g_hash_table_lookup (source_by_track,
389 ges_track_element_get_track (child));
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);
400 g_hash_table_lookup (auto_transitions_on_sources, orig_source);
401 for (; transitions; transitions = transitions->next) {
402 GESAutoTransition *transition = transitions->data;
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);
411 GST_ERROR_OBJECT (self, "Failed to remove from the layer. This "
412 "should not happen");
414 gst_object_unref (layer);
416 g_hash_table_unref (source_by_track);
417 g_hash_table_unref (auto_transitions_on_sources);
420 ges_timeline_freeze_auto_transitions (timeline, FALSE);
423 g_free (uriclip->priv->uri);
424 uriclip->priv->uri = g_strdup (ges_asset_get_id (asset));
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");
432 element->asset = prev_asset;
435 /* if re-adding failed, clip may be destroyed */
436 gst_object_unref (self);
442 ges_extractable_interface_init (GESExtractableInterface * iface)
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;
454 ges_uri_clip_init (GESUriClip * self)
456 self->priv = ges_uri_clip_get_instance_private (self);
458 /* Setting the duration to -1 by default. */
459 GES_TIMELINE_ELEMENT (self)->duration = GST_CLOCK_TIME_NONE;
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
467 * Sets whether the audio track of this clip is muted or not.
471 ges_uri_clip_set_mute (GESUriClip * self, gboolean mute)
475 GST_DEBUG ("self:%p, mute:%d", self, mute);
477 self->priv->mute = mute;
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);
484 if (track && track->type == GES_TRACK_TYPE_AUDIO)
485 ges_track_element_set_active (trackelement, !mute);
490 uri_clip_set_max_duration (GESTimelineElement * element,
491 GstClockTime maxduration)
494 GES_TIMELINE_ELEMENT_CLASS (parent_class)->set_max_duration (element,
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);
507 * ges_uri_clip_set_is_image:
508 * @self: the #GESUriClip
509 * @is_image: %TRUE if @self is a still image, %FALSE otherwise
511 * Sets whether the clip is a still image or not.
514 ges_uri_clip_set_is_image (GESUriClip * self, gboolean is_image)
516 self->priv->is_image = is_image;
520 * ges_uri_clip_is_muted:
521 * @self: the #GESUriClip
523 * Lets you know if the audio track of @self is muted or not.
525 * Returns: %TRUE if the audio track of @self is muted, %FALSE otherwise.
528 ges_uri_clip_is_muted (GESUriClip * self)
530 return self->priv->mute;
534 * ges_uri_clip_is_image:
535 * @self: the #GESUriClip
537 * Lets you know if @self is an image or not.
539 * Returns: %TRUE if @self is a still image %FALSE otherwise.
542 ges_uri_clip_is_image (GESUriClip * self)
544 return self->priv->is_image;
548 * ges_uri_clip_get_uri:
549 * @self: the #GESUriClip
551 * Get the location of the resource.
553 * Returns: The location of the resource.
556 ges_uri_clip_get_uri (GESUriClip * self)
558 return self->priv->uri;
562 ges_uri_clip_create_track_elements (GESClip * clip, GESTrackType type)
565 const GList *tmp, *stream_assets;
566 GESAsset *asset = GES_TIMELINE_ELEMENT (clip)->asset;
567 GESUriClipAsset *uri_asset;
568 GstClockTime max_duration;
570 g_return_val_if_fail (asset, NULL);
572 uri_asset = GES_URI_CLIP_ASSET (asset);
574 max_duration = ges_uri_clip_asset_get_max_duration (uri_asset);
575 stream_assets = ges_uri_clip_asset_get_stream_assets (uri_asset);
577 for (tmp = stream_assets; tmp; tmp = tmp->next) {
578 GESTrackElementAsset *element_asset = GES_TRACK_ELEMENT_ASSET (tmp->data);
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),
584 ges_timeline_element_set_max_duration (GES_TIMELINE_ELEMENT (element),
586 res = g_list_prepend (res, element);
595 * @uri: the URI the source should control
597 * Creates a new #GESUriClip for the provided @uri.
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**.
604 * Returns: (transfer floating) (nullable): The newly created #GESUriClip, or
605 * %NULL if there was an error.
608 ges_uri_clip_new (const gchar * uri)
610 GESAsset *asset = GES_ASSET (ges_uri_clip_asset_request_sync (uri, NULL));
611 GESUriClip *res = NULL;
614 res = GES_URI_CLIP (ges_asset_extract (asset, NULL));
615 gst_object_unref (asset);
617 GST_ERROR ("Could not create asset for uri: %s", uri);
623 ges_uri_clip_set_uri (GESUriClip * self, gchar * uri)
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");
633 self->priv->uri = uri;