3eef67bb0d88ec7c96adfeeff36a1cc752ca7caa
[platform/upstream/gstreamer.git] / subprojects / gst-editing-services / ges / ges-video-uri-source.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:gesvideourisource
23  * @title: GESVideoUriSource
24  * @short_description: outputs a single video stream from a given file
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <stdio.h>
31
32 #include <gst/pbutils/missing-plugins.h>
33 #include "ges-utils.h"
34 #include "ges-internal.h"
35 #include "ges-track-element.h"
36 #include "ges-uri-source.h"
37 #include "ges-video-uri-source.h"
38 #include "ges-uri-asset.h"
39 #include "ges-extractable.h"
40
41 struct _GESVideoUriSourcePrivate
42 {
43   GESUriSource parent;
44 };
45
46 enum
47 {
48   PROP_0,
49   PROP_URI
50 };
51
52 /* GESSource VMethod */
53 static GstElement *
54 ges_video_uri_source_create_source (GESSource * element)
55 {
56   return ges_uri_source_create_source (GES_VIDEO_URI_SOURCE (element)->priv);
57 }
58
59 static gboolean
60 ges_video_uri_source_needs_converters (GESVideoSource * source)
61 {
62   GESTrack *track = ges_track_element_get_track (GES_TRACK_ELEMENT (source));
63
64   if (!track || ges_track_get_mixing (track)) {
65     GESAsset *asset = ges_asset_request (GES_TYPE_URI_CLIP,
66         GES_VIDEO_URI_SOURCE (source)->uri, NULL);
67     gboolean is_nested = FALSE;
68
69     g_assert (asset);
70
71     g_object_get (asset, "is-nested-timeline", &is_nested, NULL);
72     gst_object_unref (asset);
73
74     return !is_nested;
75   }
76
77
78   return FALSE;
79 }
80
81 static GstDiscovererVideoInfo *
82 _get_video_stream_info (GESVideoUriSource * self)
83 {
84   GstDiscovererStreamInfo *info;
85   GESAsset *asset = ges_extractable_get_asset (GES_EXTRACTABLE (self));
86
87   if (!asset) {
88     GST_DEBUG_OBJECT (self, "No asset set yet");
89     return NULL;
90   }
91
92   info = ges_uri_source_asset_get_stream_info (GES_URI_SOURCE_ASSET (asset));
93
94   if (!GST_IS_DISCOVERER_VIDEO_INFO (info)) {
95     GST_ERROR_OBJECT (self, "Doesn't have a video info (%" GST_PTR_FORMAT
96         ")", info);
97     return NULL;
98   }
99
100   return GST_DISCOVERER_VIDEO_INFO (info);
101 }
102
103
104 gboolean
105 ges_video_uri_source_get_natural_size (GESVideoSource * source, gint * width,
106     gint * height)
107 {
108   const GstTagList *tags = NULL;
109   gchar *rotation_info = NULL;
110   gint videoflip_method, rotate_angle;
111   GstDiscovererVideoInfo *info =
112       _get_video_stream_info (GES_VIDEO_URI_SOURCE (source));
113
114   if (!info)
115     return FALSE;
116
117   *width = gst_discoverer_video_info_get_width (info);
118   *height = gst_discoverer_video_info_get_height (info);
119   if (!ges_timeline_element_lookup_child (GES_TIMELINE_ELEMENT (source),
120           "GstVideoFlip::video-direction", NULL, NULL))
121     goto done;
122
123   ges_timeline_element_get_child_properties (GES_TIMELINE_ELEMENT (source),
124       "GstVideoFlip::video-direction", &videoflip_method, NULL);
125
126   /* Rotating 90 degrees, either way, rotate */
127   if (videoflip_method == 1 || videoflip_method == 3)
128     goto rotate;
129
130   if (videoflip_method != 8)
131     goto done;
132
133   /* Rotation is automatic, we need to check if the media file is naturally
134      rotated */
135   tags =
136       gst_discoverer_stream_info_get_tags (GST_DISCOVERER_STREAM_INFO (info));
137   if (!tags)
138     goto done;
139
140   if (!gst_tag_list_get_string (tags, GST_TAG_IMAGE_ORIENTATION,
141           &rotation_info))
142     goto done;
143
144   if (sscanf (rotation_info, "rotate-%d", &rotate_angle) == 1) {
145     if (rotate_angle == 90 || rotate_angle == 270)
146       goto rotate;
147   }
148
149 done:
150   g_free (rotation_info);
151   return TRUE;
152
153 rotate:
154   GST_INFO_OBJECT (source, "Stream is rotated, taking that into account");
155   *width =
156       gst_discoverer_video_info_get_height (GST_DISCOVERER_VIDEO_INFO (info));
157   *height =
158       gst_discoverer_video_info_get_width (GST_DISCOVERER_VIDEO_INFO (info));
159
160   goto done;
161 }
162
163 /* Extractable interface implementation */
164
165 static gchar *
166 ges_extractable_check_id (GType type, const gchar * id, GError ** error)
167 {
168   return g_strdup (id);
169 }
170
171 static void
172 ges_extractable_interface_init (GESExtractableInterface * iface)
173 {
174   iface->asset_type = GES_TYPE_URI_SOURCE_ASSET;
175   iface->check_id = ges_extractable_check_id;
176 }
177
178 G_DEFINE_TYPE_WITH_CODE (GESVideoUriSource, ges_video_uri_source,
179     GES_TYPE_VIDEO_SOURCE, G_ADD_PRIVATE (GESVideoUriSource)
180     G_IMPLEMENT_INTERFACE (GES_TYPE_EXTRACTABLE,
181         ges_extractable_interface_init));
182
183 static gboolean
184 _find_positioner (GstElement * a, GstElement * b)
185 {
186   return !g_strcmp0 (GST_OBJECT_NAME (gst_element_get_factory (a)),
187       "framepositioner");
188 }
189
190 static void
191 post_missing_element_message (GstElement * element, const gchar * name)
192 {
193   GstMessage *msg;
194
195   msg = gst_missing_element_message_new (element, name);
196   gst_element_post_message (element, msg);
197 }
198
199 /* GObject VMethods */
200 static gboolean
201 ges_video_uri_source_create_filters (GESVideoSource * source,
202     GPtrArray * elements, gboolean needs_converters)
203 {
204   GESAsset *asset = ges_extractable_get_asset (GES_EXTRACTABLE (source));
205   GstDiscovererVideoInfo *info =
206       GST_DISCOVERER_VIDEO_INFO (ges_uri_source_asset_get_stream_info
207       (GES_URI_SOURCE_ASSET (asset)));
208
209   g_assert (GES_IS_URI_SOURCE_ASSET (asset));
210   if (!GES_VIDEO_SOURCE_CLASS (ges_video_uri_source_parent_class)
211       ->ABI.abi.create_filters (source, elements, needs_converters))
212     return FALSE;
213
214   if (gst_discoverer_video_info_is_interlaced (info)) {
215     const gchar *deinterlace_props[] = { "mode", "fields", "tff", NULL };
216     GstElement *deinterlace =
217         gst_element_factory_make ("deinterlace", "deinterlace");
218
219     if (deinterlace == NULL) {
220       post_missing_element_message (ges_track_element_get_nleobject
221           (GES_TRACK_ELEMENT (source)), "deinterlace");
222
223       GST_ELEMENT_WARNING (ges_track_element_get_nleobject (GES_TRACK_ELEMENT
224               (source)), CORE, MISSING_PLUGIN,
225           ("Missing element '%s' - check your GStreamer installation.",
226               "deinterlace"), ("deinterlacing won't work"));
227     } else {
228       /* Right after the queue */
229       g_ptr_array_insert (elements, 1, gst_element_factory_make ("videoconvert",
230               NULL));
231       g_ptr_array_insert (elements, 2, deinterlace);
232       ges_track_element_add_children_props (GES_TRACK_ELEMENT (source),
233           deinterlace, NULL, NULL, deinterlace_props);
234     }
235
236   }
237
238   if (ges_uri_source_asset_is_image (GES_URI_SOURCE_ASSET (asset))) {
239     guint i;
240
241     g_ptr_array_find_with_equal_func (elements, NULL,
242         (GEqualFunc) _find_positioner, &i);
243     /* Adding the imagefreeze right before the positionner so positioning can happen
244      * properly */
245     g_ptr_array_insert (elements, i,
246         gst_element_factory_make ("imagefreeze", NULL));
247   }
248
249   return TRUE;
250 }
251
252 static void
253 ges_video_uri_source_get_property (GObject * object, guint property_id,
254     GValue * value, GParamSpec * pspec)
255 {
256   GESVideoUriSource *urisource = GES_VIDEO_URI_SOURCE (object);
257
258   switch (property_id) {
259     case PROP_URI:
260       g_value_set_string (value, urisource->uri);
261       break;
262     default:
263       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
264   }
265 }
266
267 static void
268 ges_video_uri_source_set_property (GObject * object, guint property_id,
269     const GValue * value, GParamSpec * pspec)
270 {
271   GESVideoUriSource *urisource = GES_VIDEO_URI_SOURCE (object);
272
273   switch (property_id) {
274     case PROP_URI:
275       if (urisource->uri) {
276         GST_WARNING_OBJECT (object, "Uri already set to %s", urisource->uri);
277         return;
278       }
279       urisource->priv->uri = urisource->uri = g_value_dup_string (value);
280       break;
281     default:
282       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
283   }
284 }
285
286 static void
287 ges_video_uri_source_finalize (GObject * object)
288 {
289   GESVideoUriSource *urisource = GES_VIDEO_URI_SOURCE (object);
290
291   g_free (urisource->uri);
292
293   G_OBJECT_CLASS (ges_video_uri_source_parent_class)->finalize (object);
294 }
295
296 static void
297 ges_video_uri_source_class_init (GESVideoUriSourceClass * klass)
298 {
299   GObjectClass *object_class = G_OBJECT_CLASS (klass);
300   GESSourceClass *src_class = GES_SOURCE_CLASS (klass);
301   GESVideoSourceClass *video_src_class = GES_VIDEO_SOURCE_CLASS (klass);
302
303   object_class->get_property = ges_video_uri_source_get_property;
304   object_class->set_property = ges_video_uri_source_set_property;
305   object_class->finalize = ges_video_uri_source_finalize;
306
307   /**
308    * GESVideoUriSource:uri:
309    *
310    * The location of the file/resource to use.
311    */
312   g_object_class_install_property (object_class, PROP_URI,
313       g_param_spec_string ("uri", "URI", "uri of the resource",
314           NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
315
316   src_class->select_pad = ges_uri_source_select_pad;
317
318   src_class->create_source = ges_video_uri_source_create_source;
319   video_src_class->ABI.abi.needs_converters =
320       ges_video_uri_source_needs_converters;
321   video_src_class->ABI.abi.get_natural_size =
322       ges_video_uri_source_get_natural_size;
323   video_src_class->ABI.abi.create_filters = ges_video_uri_source_create_filters;
324 }
325
326 static void
327 ges_video_uri_source_init (GESVideoUriSource * self)
328 {
329   self->priv = ges_video_uri_source_get_instance_private (self);
330   ges_uri_source_init (GES_TRACK_ELEMENT (self), self->priv);
331 }
332
333 /**
334  * ges_video_uri_source_new:
335  * @uri: the URI the source should control
336  *
337  * Creates a new #GESVideoUriSource for the provided @uri.
338  *
339  * Returns: (transfer floating) (nullable): The newly created #GESVideoUriSource,
340  * or %NULL if there was an error.
341  */
342 GESVideoUriSource *
343 ges_video_uri_source_new (gchar * uri)
344 {
345   return g_object_new (GES_TYPE_VIDEO_URI_SOURCE, "uri", uri, NULL);
346 }