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 GESExtractableInterface *parent_extractable_iface;
51 
52 struct _GESUriClipPrivate
53 {
54   gchar *uri;
55 
56   gboolean mute;
57   gboolean is_image;
58 };
59 
60 enum
61 {
62   PROP_0,
63   PROP_URI,
64   PROP_MUTE,
65   PROP_IS_IMAGE,
66   PROP_SUPPORTED_FORMATS,
67 };
68 
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));
73 
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);
79 
80 gboolean
81 uri_clip_set_max_duration (GESTimelineElement * element,
82     GstClockTime maxduration);
83 
84 static void
ges_uri_clip_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)85 ges_uri_clip_get_property (GObject * object, guint property_id,
86     GValue * value, GParamSpec * pspec)
87 {
88   GESUriClipPrivate *priv = GES_URI_CLIP (object)->priv;
89 
90   switch (property_id) {
91     case PROP_URI:
92       g_value_set_string (value, priv->uri);
93       break;
94     case PROP_MUTE:
95       g_value_set_boolean (value, priv->mute);
96       break;
97     case PROP_IS_IMAGE:
98       g_value_set_boolean (value, priv->is_image);
99       break;
100     case PROP_SUPPORTED_FORMATS:
101       g_value_set_flags (value,
102           ges_clip_get_supported_formats (GES_CLIP (object)));
103       break;
104     default:
105       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
106   }
107 }
108 
109 static void
ges_uri_clip_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)110 ges_uri_clip_set_property (GObject * object, guint property_id,
111     const GValue * value, GParamSpec * pspec)
112 {
113   GESUriClip *uriclip = GES_URI_CLIP (object);
114 
115   switch (property_id) {
116     case PROP_URI:
117       ges_uri_clip_set_uri (uriclip, g_value_dup_string (value));
118       break;
119     case PROP_MUTE:
120       ges_uri_clip_set_mute (uriclip, g_value_get_boolean (value));
121       break;
122     case PROP_IS_IMAGE:
123       ges_uri_clip_set_is_image (uriclip, g_value_get_boolean (value));
124       break;
125     case PROP_SUPPORTED_FORMATS:
126       ges_clip_set_supported_formats (GES_CLIP (uriclip),
127           g_value_get_flags (value));
128       break;
129     default:
130       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
131   }
132 }
133 
134 static void
ges_uri_clip_finalize(GObject * object)135 ges_uri_clip_finalize (GObject * object)
136 {
137   GESUriClipPrivate *priv = GES_URI_CLIP (object)->priv;
138 
139   if (priv->uri)
140     g_free (priv->uri);
141   G_OBJECT_CLASS (parent_class)->finalize (object);
142 }
143 
144 static void
ges_uri_clip_class_init(GESUriClipClass * klass)145 ges_uri_clip_class_init (GESUriClipClass * klass)
146 {
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);
150 
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;
154 
155 
156   /**
157    * GESUriClip:uri:
158    *
159    * The location of the file/resource to use.
160    */
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));
164 
165   /**
166    * GESUriClip:mute:
167    *
168    * Whether the sound will be played or not.
169    */
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));
173 
174   /**
175    * GESUriClip:is-image:
176    *
177    * Whether this uri clip represents a still image or not. This must be set
178    * before create_track_elements is called.
179    */
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));
184 
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));
192 
193   element_class->set_max_duration = uri_clip_set_max_duration;
194 
195   timobj_class->create_track_elements = ges_uri_clip_create_track_elements;
196   timobj_class->create_track_element = ges_uri_clip_create_track_element;
197 }
198 
199 static gchar *
extractable_check_id(GType type,const gchar * id)200 extractable_check_id (GType type, const gchar * id)
201 {
202   const gchar *testing_directory;
203 
204   testing_directory = g_getenv ("GES_TESTING_ASSETS_DIRECTORY");
205 
206   /* Testing purposes, user can specify a directory to look up for script */
207   if (testing_directory != NULL) {
208     gchar **tokens;
209     gchar *location = NULL;
210     guint i;
211 
212     GST_DEBUG ("Checking if the testing directory contains needed media");
213 
214     tokens = g_strsplit (id, "media", 2);
215     for (i = 0; tokens[i]; i++)
216       if (i == 1)
217         location = tokens[1];
218 
219     if (location == NULL)
220       GST_WARNING ("The provided id doesn't have a media subdirectory");
221     else {
222       gchar *actual_id =
223           g_strconcat ("file://", testing_directory, "/media/", location, NULL);
224 
225       if (gst_uri_is_valid (actual_id)) {
226         GST_DEBUG ("Returning new id %s instead of id %s", actual_id, id);
227         g_strfreev (tokens);
228         return (actual_id);
229       } else
230         GST_WARNING ("The constructed id %s was not valid, trying %s anyway",
231             actual_id, id);
232 
233       g_free (actual_id);
234     }
235 
236     g_strfreev (tokens);
237   }
238 
239   if (gst_uri_is_valid (id))
240     return g_strdup (id);
241 
242   return NULL;
243 }
244 
245 static GParameter *
extractable_get_parameters_from_id(const gchar * id,guint * n_params)246 extractable_get_parameters_from_id (const gchar * id, guint * n_params)
247 {
248   GParameter *params = g_new0 (GParameter, 2);
249 
250   params[0].name = "uri";
251   g_value_init (&params[0].value, G_TYPE_STRING);
252   g_value_set_string (&params[0].value, id);
253 
254   *n_params = 1;
255 
256   return params;
257 }
258 
259 static gchar *
extractable_get_id(GESExtractable * self)260 extractable_get_id (GESExtractable * self)
261 {
262   return g_strdup (GES_URI_CLIP (self)->priv->uri);
263 }
264 
265 static gboolean
extractable_set_asset(GESExtractable * self,GESAsset * asset)266 extractable_set_asset (GESExtractable * self, GESAsset * asset)
267 {
268   gboolean res = TRUE;
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);
273   GList *tmp;
274   GESTimelineElement *audio_source = NULL, *video_source = NULL;
275 
276   g_return_val_if_fail (GES_IS_URI_CLIP_ASSET (asset), FALSE);
277 
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)));
292 
293 
294     return FALSE;
295   }
296 
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));
300 
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));
305 
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)));
309   }
310 
311   GES_TIMELINE_ELEMENT (uriclip)->asset = asset;
312 
313   if (layer) {
314     GList *children = ges_container_get_children (GES_CONTAINER (self), TRUE);
315 
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);
319 
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);
324 
325         ges_track_remove_element (track, tmp->data);
326         ges_container_remove (GES_CONTAINER (self), tmp->data);
327       }
328     }
329     g_list_free_full (children, g_object_unref);
330 
331     gst_object_ref (clip);
332 
333     ges_layer_remove_clip (layer, clip);
334     res = ges_layer_add_clip (layer, clip);
335 
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);
339 
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);
348         }
349       }
350     }
351 
352     g_clear_object (&audio_source);
353     g_clear_object (&video_source);
354     gst_object_unref (clip);
355     gst_object_unref (layer);
356   }
357 
358   if (res) {
359     g_free (uriclip->priv->uri);
360     uriclip->priv->uri = g_strdup (ges_asset_get_id (asset));
361   }
362 
363   return res;
364 }
365 
366 static void
ges_extractable_interface_init(GESExtractableInterface * iface)367 ges_extractable_interface_init (GESExtractableInterface * iface)
368 {
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;
376 }
377 
378 static void
ges_uri_clip_init(GESUriClip * self)379 ges_uri_clip_init (GESUriClip * self)
380 {
381   self->priv = ges_uri_clip_get_instance_private (self);
382 
383   /* Setting the duration to -1 by default. */
384   GES_TIMELINE_ELEMENT (self)->duration = GST_CLOCK_TIME_NONE;
385 }
386 
387 /**
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
391  *
392  * Sets whether the audio track of this clip is muted or not.
393  *
394  */
395 void
ges_uri_clip_set_mute(GESUriClip * self,gboolean mute)396 ges_uri_clip_set_mute (GESUriClip * self, gboolean mute)
397 {
398   GList *tmp;
399 
400   GST_DEBUG ("self:%p, mute:%d", self, mute);
401 
402   self->priv->mute = mute;
403 
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;
407 
408     if (ges_track_element_get_track (trackelement)->type ==
409         GES_TRACK_TYPE_AUDIO)
410       ges_track_element_set_active (trackelement, !mute);
411   }
412 }
413 
414 gboolean
uri_clip_set_max_duration(GESTimelineElement * element,GstClockTime maxduration)415 uri_clip_set_max_duration (GESTimelineElement * element,
416     GstClockTime maxduration)
417 {
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));
421 
422   return
423       GES_TIMELINE_ELEMENT_CLASS (parent_class)->set_max_duration (element,
424       maxduration);
425 }
426 
427 /**
428  * ges_uri_clip_set_is_image:
429  * @self: the #GESUriClip
430  * @is_image: %TRUE if @self is a still image, %FALSE otherwise
431  *
432  * Sets whether the clip is a still image or not.
433  */
434 void
ges_uri_clip_set_is_image(GESUriClip * self,gboolean is_image)435 ges_uri_clip_set_is_image (GESUriClip * self, gboolean is_image)
436 {
437   self->priv->is_image = is_image;
438 }
439 
440 /**
441  * ges_uri_clip_is_muted:
442  * @self: the #GESUriClip
443  *
444  * Lets you know if the audio track of @self is muted or not.
445  *
446  * Returns: %TRUE if the audio track of @self is muted, %FALSE otherwise.
447  */
448 gboolean
ges_uri_clip_is_muted(GESUriClip * self)449 ges_uri_clip_is_muted (GESUriClip * self)
450 {
451   return self->priv->mute;
452 }
453 
454 /**
455  * ges_uri_clip_is_image:
456  * @self: the #GESUriClip
457  *
458  * Lets you know if @self is an image or not.
459  *
460  * Returns: %TRUE if @self is a still image %FALSE otherwise.
461  */
462 gboolean
ges_uri_clip_is_image(GESUriClip * self)463 ges_uri_clip_is_image (GESUriClip * self)
464 {
465   return self->priv->is_image;
466 }
467 
468 /**
469  * ges_uri_clip_get_uri:
470  * @self: the #GESUriClip
471  *
472  * Get the location of the resource.
473  *
474  * Returns: The location of the resource.
475  */
476 const gchar *
ges_uri_clip_get_uri(GESUriClip * self)477 ges_uri_clip_get_uri (GESUriClip * self)
478 {
479   return self->priv->uri;
480 }
481 
482 static GList *
ges_uri_clip_create_track_elements(GESClip * clip,GESTrackType type)483 ges_uri_clip_create_track_elements (GESClip * clip, GESTrackType type)
484 {
485   GList *res = NULL;
486   const GList *tmp, *stream_assets;
487 
488   g_return_val_if_fail (GES_TIMELINE_ELEMENT (clip)->asset, NULL);
489 
490   stream_assets =
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);
495 
496     if (ges_track_element_asset_get_track_type (asset) == type)
497       res = g_list_prepend (res, ges_asset_extract (GES_ASSET (asset), NULL));
498   }
499 
500   return res;
501 }
502 
503 static GESTrackElement *
ges_uri_clip_create_track_element(GESClip * clip,GESTrackType type)504 ges_uri_clip_create_track_element (GESClip * clip, GESTrackType type)
505 {
506   GESUriClipPrivate *priv = GES_URI_CLIP (clip)->priv;
507   GESTrackElement *res = NULL;
508 
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");
515       return NULL;
516     } else {
517       GST_DEBUG ("Creating a GESImageSource");
518       res = (GESTrackElement *) ges_image_source_new (priv->uri);
519     }
520 
521   } else {
522     GST_DEBUG ("Creating a GESUriSource");
523 
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);
529 
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);
533   }
534 
535   if (res)
536     ges_track_element_set_track_type (res, type);
537 
538   return res;
539 }
540 
541 /**
542  * ges_uri_clip_new:
543  * @uri: the URI the source should control
544  *
545  * Creates a new #GESUriClip for the provided @uri.
546  *
547  * Returns: (transfer floating) (nullable): The newly created #GESUriClip, or
548  * %NULL if there was an error.
549  */
550 GESUriClip *
ges_uri_clip_new(const gchar * uri)551 ges_uri_clip_new (const gchar * uri)
552 {
553   GESAsset *asset = GES_ASSET (ges_uri_clip_asset_request_sync (uri, NULL));
554   GESUriClip *res = NULL;
555 
556   if (asset) {
557     res = GES_URI_CLIP (ges_asset_extract (asset, NULL));
558     gst_object_unref (asset);
559   } else
560     GST_ERROR ("Could not create asset for uri: %s", uri);
561 
562   return res;
563 }
564 
565 void
ges_uri_clip_set_uri(GESUriClip * self,gchar * uri)566 ges_uri_clip_set_uri (GESUriClip * self, gchar * uri)
567 {
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");
572 
573     return;
574   }
575 
576   self->priv->uri = uri;
577 }
578