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 GESExtractableInterface *parent_extractable_iface;
52 struct _GESUriClipPrivate
66 PROP_SUPPORTED_FORMATS,
69 G_DEFINE_TYPE_WITH_CODE (GESUriClip, ges_uri_clip,
70 GES_TYPE_SOURCE_CLIP, G_ADD_PRIVATE (GESUriClip)
71 G_IMPLEMENT_INTERFACE (GES_TYPE_EXTRACTABLE,
72 ges_extractable_interface_init));
74 static GList *ges_uri_clip_create_track_elements (GESClip *
75 clip, GESTrackType type);
76 static GESTrackElement
77 * ges_uri_clip_create_track_element (GESClip * clip, GESTrackType type);
78 static void ges_uri_clip_set_uri (GESUriClip * self, gchar * uri);
81 uri_clip_set_max_duration (GESTimelineElement * element,
82 GstClockTime maxduration);
85 ges_uri_clip_get_property (GObject * object, guint property_id,
86 GValue * value, GParamSpec * pspec)
88 GESUriClipPrivate *priv = GES_URI_CLIP (object)->priv;
90 switch (property_id) {
92 g_value_set_string (value, priv->uri);
95 g_value_set_boolean (value, priv->mute);
98 g_value_set_boolean (value, priv->is_image);
100 case PROP_SUPPORTED_FORMATS:
101 g_value_set_flags (value,
102 ges_clip_get_supported_formats (GES_CLIP (object)));
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
110 ges_uri_clip_set_property (GObject * object, guint property_id,
111 const GValue * value, GParamSpec * pspec)
113 GESUriClip *uriclip = GES_URI_CLIP (object);
115 switch (property_id) {
117 ges_uri_clip_set_uri (uriclip, g_value_dup_string (value));
120 ges_uri_clip_set_mute (uriclip, g_value_get_boolean (value));
123 ges_uri_clip_set_is_image (uriclip, g_value_get_boolean (value));
125 case PROP_SUPPORTED_FORMATS:
126 ges_clip_set_supported_formats (GES_CLIP (uriclip),
127 g_value_get_flags (value));
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
135 ges_uri_clip_finalize (GObject * object)
137 GESUriClipPrivate *priv = GES_URI_CLIP (object)->priv;
141 G_OBJECT_CLASS (parent_class)->finalize (object);
145 ges_uri_clip_class_init (GESUriClipClass * klass)
147 GObjectClass *object_class = G_OBJECT_CLASS (klass);
148 GESClipClass *timobj_class = GES_CLIP_CLASS (klass);
149 GESTimelineElementClass *element_class = GES_TIMELINE_ELEMENT_CLASS (klass);
151 object_class->get_property = ges_uri_clip_get_property;
152 object_class->set_property = ges_uri_clip_set_property;
153 object_class->finalize = ges_uri_clip_finalize;
159 * The location of the file/resource to use.
161 g_object_class_install_property (object_class, PROP_URI,
162 g_param_spec_string ("uri", "URI", "uri of the resource", NULL,
163 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
168 * Whether the sound will be played or not.
170 g_object_class_install_property (object_class, PROP_MUTE,
171 g_param_spec_boolean ("mute", "Mute", "Mute audio track",
172 FALSE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
175 * GESUriClip:is-image:
177 * Whether this uri clip represents a still image or not. This must be set
178 * before create_track_elements is called.
180 g_object_class_install_property (object_class, PROP_IS_IMAGE,
181 g_param_spec_boolean ("is-image", "Is still image",
182 "Whether the clip represents a still image or not",
183 FALSE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
185 /* Redefine the supported formats property so the default value is UNKNOWN
186 * and not AUDIO | VIDEO */
187 g_object_class_install_property (object_class, PROP_SUPPORTED_FORMATS,
188 g_param_spec_flags ("supported-formats",
189 "Supported formats", "Formats supported by the file",
190 GES_TYPE_TRACK_TYPE, GES_TRACK_TYPE_UNKNOWN,
191 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
193 element_class->set_max_duration = uri_clip_set_max_duration;
195 timobj_class->create_track_elements = ges_uri_clip_create_track_elements;
196 timobj_class->create_track_element = ges_uri_clip_create_track_element;
200 extractable_check_id (GType type, const gchar * id)
202 const gchar *testing_directory;
204 testing_directory = g_getenv ("GES_TESTING_ASSETS_DIRECTORY");
206 /* Testing purposes, user can specify a directory to look up for script */
207 if (testing_directory != NULL) {
209 gchar *location = NULL;
212 GST_DEBUG ("Checking if the testing directory contains needed media");
214 tokens = g_strsplit (id, "media", 2);
215 for (i = 0; tokens[i]; i++)
217 location = tokens[1];
219 if (location == NULL)
220 GST_WARNING ("The provided id doesn't have a media subdirectory");
223 g_strconcat ("file://", testing_directory, "/media/", location, NULL);
225 if (gst_uri_is_valid (actual_id)) {
226 GST_DEBUG ("Returning new id %s instead of id %s", actual_id, id);
230 GST_WARNING ("The constructed id %s was not valid, trying %s anyway",
239 if (gst_uri_is_valid (id))
240 return g_strdup (id);
246 extractable_get_parameters_from_id (const gchar * id, guint * n_params)
248 GParameter *params = g_new0 (GParameter, 2);
250 params[0].name = "uri";
251 g_value_init (¶ms[0].value, G_TYPE_STRING);
252 g_value_set_string (¶ms[0].value, id);
260 extractable_get_id (GESExtractable * self)
262 return g_strdup (GES_URI_CLIP (self)->priv->uri);
266 extractable_set_asset (GESExtractable * self, GESAsset * asset)
269 GESUriClip *uriclip = GES_URI_CLIP (self);
270 GESUriClipAsset *uri_clip_asset;
271 GESClip *clip = GES_CLIP (self);
272 GESLayer *layer = ges_clip_get_layer (clip);
274 GESTimelineElement *audio_source = NULL, *video_source = NULL;
276 g_return_val_if_fail (GES_IS_URI_CLIP_ASSET (asset), FALSE);
278 uri_clip_asset = GES_URI_CLIP_ASSET (asset);
279 if (GST_CLOCK_TIME_IS_VALID (GES_TIMELINE_ELEMENT_DURATION (self)) &&
280 ges_uri_clip_asset_get_duration (uri_clip_asset) <
281 GES_TIMELINE_ELEMENT_INPOINT (self) +
282 GES_TIMELINE_ELEMENT_DURATION (self)) {
283 GST_INFO_OBJECT (self,
284 "Can not set asset to %p as its duration is %" GST_TIME_FORMAT
285 " < to inpoint %" GST_TIME_FORMAT " + %" GST_TIME_FORMAT " = %"
286 GST_TIME_FORMAT, asset,
287 GST_TIME_ARGS (ges_uri_clip_asset_get_duration (uri_clip_asset)),
288 GST_TIME_ARGS (GES_TIMELINE_ELEMENT_INPOINT (self)),
289 GST_TIME_ARGS (GES_TIMELINE_ELEMENT_DURATION (self)),
290 GST_TIME_ARGS (GES_TIMELINE_ELEMENT_INPOINT (self) +
291 GES_TIMELINE_ELEMENT_DURATION (self)));
297 if (GST_CLOCK_TIME_IS_VALID (GES_TIMELINE_ELEMENT_DURATION (clip)) == FALSE)
298 _set_duration0 (GES_TIMELINE_ELEMENT (uriclip),
299 ges_uri_clip_asset_get_duration (uri_clip_asset));
301 ges_timeline_element_set_max_duration (GES_TIMELINE_ELEMENT (uriclip),
302 ges_uri_clip_asset_get_duration (uri_clip_asset));
303 ges_uri_clip_set_is_image (uriclip,
304 ges_uri_clip_asset_is_image (uri_clip_asset));
306 if (ges_clip_get_supported_formats (clip) == GES_TRACK_TYPE_UNKNOWN) {
307 ges_clip_set_supported_formats (clip,
308 ges_clip_asset_get_supported_formats (GES_CLIP_ASSET (uri_clip_asset)));
311 GES_TIMELINE_ELEMENT (uriclip)->asset = asset;
314 GList *children = ges_container_get_children (GES_CONTAINER (self), TRUE);
316 for (tmp = children; tmp; tmp = tmp->next) {
317 if (GES_IS_SOURCE (tmp->data)) {
318 GESTrack *track = ges_track_element_get_track (tmp->data);
320 if (track->type == GES_TRACK_TYPE_AUDIO)
321 audio_source = gst_object_ref (tmp->data);
322 else if (track->type == GES_TRACK_TYPE_VIDEO)
323 video_source = gst_object_ref (tmp->data);
325 ges_track_remove_element (track, tmp->data);
326 ges_container_remove (GES_CONTAINER (self), tmp->data);
329 g_list_free_full (children, g_object_unref);
331 gst_object_ref (clip);
333 ges_layer_remove_clip (layer, clip);
334 res = ges_layer_add_clip (layer, clip);
336 for (tmp = GES_CONTAINER_CHILDREN (self); tmp; tmp = tmp->next) {
337 if (GES_IS_SOURCE (tmp->data)) {
338 GESTrack *track = ges_track_element_get_track (tmp->data);
340 if (track->type == GES_TRACK_TYPE_AUDIO && audio_source) {
341 ges_track_element_copy_properties (audio_source, tmp->data);
342 ges_track_element_copy_bindings (GES_TRACK_ELEMENT (audio_source),
343 tmp->data, GST_CLOCK_TIME_NONE);
344 } else if (track->type == GES_TRACK_TYPE_VIDEO && video_source) {
345 ges_track_element_copy_properties (video_source, tmp->data);
346 ges_track_element_copy_bindings (GES_TRACK_ELEMENT (video_source),
347 tmp->data, GST_CLOCK_TIME_NONE);
352 g_clear_object (&audio_source);
353 g_clear_object (&video_source);
354 gst_object_unref (clip);
355 gst_object_unref (layer);
359 g_free (uriclip->priv->uri);
360 uriclip->priv->uri = g_strdup (ges_asset_get_id (asset));
367 ges_extractable_interface_init (GESExtractableInterface * iface)
369 iface->asset_type = GES_TYPE_URI_CLIP_ASSET;
370 iface->check_id = (GESExtractableCheckId) extractable_check_id;
371 iface->get_parameters_from_id = extractable_get_parameters_from_id;
372 iface->get_id = extractable_get_id;
373 iface->get_id = extractable_get_id;
374 iface->can_update_asset = TRUE;
375 iface->set_asset_full = extractable_set_asset;
379 ges_uri_clip_init (GESUriClip * self)
381 self->priv = ges_uri_clip_get_instance_private (self);
383 /* Setting the duration to -1 by default. */
384 GES_TIMELINE_ELEMENT (self)->duration = GST_CLOCK_TIME_NONE;
388 * ges_uri_clip_set_mute:
389 * @self: the #GESUriClip on which to mute or unmute the audio track
390 * @mute: %TRUE to mute @self audio track, %FALSE to unmute it
392 * Sets whether the audio track of this clip is muted or not.
396 ges_uri_clip_set_mute (GESUriClip * self, gboolean mute)
400 GST_DEBUG ("self:%p, mute:%d", self, mute);
402 self->priv->mute = mute;
404 /* Go over tracked objects, and update 'active' status on all audio objects */
405 for (tmp = GES_CONTAINER_CHILDREN (self); tmp; tmp = g_list_next (tmp)) {
406 GESTrackElement *trackelement = (GESTrackElement *) tmp->data;
408 if (ges_track_element_get_track (trackelement)->type ==
409 GES_TRACK_TYPE_AUDIO)
410 ges_track_element_set_active (trackelement, !mute);
415 uri_clip_set_max_duration (GESTimelineElement * element,
416 GstClockTime maxduration)
418 if (_DURATION (element) == GST_CLOCK_TIME_NONE || _DURATION (element) == 0)
419 /* If we don't have a valid duration, use the max duration */
420 _set_duration0 (element, maxduration - _INPOINT (element));
423 GES_TIMELINE_ELEMENT_CLASS (parent_class)->set_max_duration (element,
428 * ges_uri_clip_set_is_image:
429 * @self: the #GESUriClip
430 * @is_image: %TRUE if @self is a still image, %FALSE otherwise
432 * Sets whether the clip is a still image or not.
435 ges_uri_clip_set_is_image (GESUriClip * self, gboolean is_image)
437 self->priv->is_image = is_image;
441 * ges_uri_clip_is_muted:
442 * @self: the #GESUriClip
444 * Lets you know if the audio track of @self is muted or not.
446 * Returns: %TRUE if the audio track of @self is muted, %FALSE otherwise.
449 ges_uri_clip_is_muted (GESUriClip * self)
451 return self->priv->mute;
455 * ges_uri_clip_is_image:
456 * @self: the #GESUriClip
458 * Lets you know if @self is an image or not.
460 * Returns: %TRUE if @self is a still image %FALSE otherwise.
463 ges_uri_clip_is_image (GESUriClip * self)
465 return self->priv->is_image;
469 * ges_uri_clip_get_uri:
470 * @self: the #GESUriClip
472 * Get the location of the resource.
474 * Returns: The location of the resource.
477 ges_uri_clip_get_uri (GESUriClip * self)
479 return self->priv->uri;
483 ges_uri_clip_create_track_elements (GESClip * clip, GESTrackType type)
486 const GList *tmp, *stream_assets;
488 g_return_val_if_fail (GES_TIMELINE_ELEMENT (clip)->asset, NULL);
491 ges_uri_clip_asset_get_stream_assets (GES_URI_CLIP_ASSET
492 (GES_TIMELINE_ELEMENT (clip)->asset));
493 for (tmp = stream_assets; tmp; tmp = tmp->next) {
494 GESTrackElementAsset *asset = GES_TRACK_ELEMENT_ASSET (tmp->data);
496 if (ges_track_element_asset_get_track_type (asset) == type)
497 res = g_list_prepend (res, ges_asset_extract (GES_ASSET (asset), NULL));
503 static GESTrackElement *
504 ges_uri_clip_create_track_element (GESClip * clip, GESTrackType type)
506 GESUriClipPrivate *priv = GES_URI_CLIP (clip)->priv;
507 GESTrackElement *res = NULL;
509 if (g_str_has_prefix (priv->uri, GES_MULTI_FILE_URI_PREFIX)) {
510 GST_DEBUG ("Creating a GESMultiFileSource for %s", priv->uri);
511 res = (GESTrackElement *) ges_multi_file_source_new (priv->uri);
512 } else if (priv->is_image) {
513 if (type != GES_TRACK_TYPE_VIDEO) {
514 GST_DEBUG ("Object is still image, not adding any audio source");
517 GST_DEBUG ("Creating a GESImageSource");
518 res = (GESTrackElement *) ges_image_source_new (priv->uri);
522 GST_DEBUG ("Creating a GESUriSource");
524 /* FIXME : Implement properly ! */
525 if (type == GES_TRACK_TYPE_VIDEO)
526 res = (GESTrackElement *) ges_video_uri_source_new (priv->uri);
527 else if (type == GES_TRACK_TYPE_AUDIO)
528 res = (GESTrackElement *) ges_audio_uri_source_new (priv->uri);
530 /* If mute and track is audio, deactivate the track element */
531 if (type == GES_TRACK_TYPE_AUDIO && priv->mute)
532 ges_track_element_set_active (res, FALSE);
536 ges_track_element_set_track_type (res, type);
543 * @uri: the URI the source should control
545 * Creates a new #GESUriClip for the provided @uri.
547 * Returns: (transfer floating) (nullable): The newly created #GESUriClip, or
548 * %NULL if there was an error.
551 ges_uri_clip_new (const gchar * uri)
553 GESAsset *asset = GES_ASSET (ges_uri_clip_asset_request_sync (uri, NULL));
554 GESUriClip *res = NULL;
557 res = GES_URI_CLIP (ges_asset_extract (asset, NULL));
558 gst_object_unref (asset);
560 GST_ERROR ("Could not create asset for uri: %s", uri);
566 ges_uri_clip_set_uri (GESUriClip * self, gchar * uri)
568 if (GES_CONTAINER_CHILDREN (self)) {
569 /* FIXME handle this case properly */
570 GST_WARNING_OBJECT (self, "Can not change uri when already"
571 "containing TrackElements");
576 self->priv->uri = uri;