ges: Add a method to retrieve the 'natural' size of VideoSource
[platform/upstream/gst-editing-services.git] / 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-video-uri-source.h"
37 #include "ges-uri-asset.h"
38 #include "ges-extractable.h"
39
40 struct _GESVideoUriSourcePrivate
41 {
42   GstElement *decodebin;        /* Reference owned by parent class */
43 };
44
45 enum
46 {
47   PROP_0,
48   PROP_URI
49 };
50
51 static void
52 ges_video_uri_source_track_set_cb (GESVideoUriSource * self,
53     GParamSpec * arg G_GNUC_UNUSED, gpointer nothing)
54 {
55   GESTrack *track;
56   const GstCaps *caps = NULL;
57
58   if (!self->priv->decodebin)
59     return;
60
61   track = ges_track_element_get_track (GES_TRACK_ELEMENT (self));
62   if (!track)
63     return;
64
65   caps = ges_track_get_caps (track);
66
67   GST_INFO_OBJECT (self, "Setting caps to: %" GST_PTR_FORMAT, caps);
68   g_object_set (self->priv->decodebin, "caps", caps, NULL);
69 }
70
71 /* GESSource VMethod */
72 static GstElement *
73 ges_video_uri_source_create_source (GESTrackElement * trksrc)
74 {
75   GESVideoUriSource *self;
76   GESTrack *track;
77   GstElement *decodebin;
78   const GstCaps *caps = NULL;
79
80   self = (GESVideoUriSource *) trksrc;
81
82   track = ges_track_element_get_track (trksrc);
83   if (track)
84     caps = ges_track_get_caps (track);
85
86   decodebin = self->priv->decodebin = gst_element_factory_make ("uridecodebin",
87       NULL);
88
89   g_object_set (decodebin, "caps", caps,
90       "expose-all-streams", FALSE, "uri", self->uri, NULL);
91
92   return decodebin;
93 }
94
95 static gboolean
96 ges_video_uri_source_needs_converters (GESVideoSource * source)
97 {
98   GESTrack *track = ges_track_element_get_track (GES_TRACK_ELEMENT (source));
99
100   if (!track || ges_track_get_mixing (track)) {
101     GESAsset *asset = ges_asset_request (GES_TYPE_URI_CLIP,
102         GES_VIDEO_URI_SOURCE (source)->uri, NULL);
103     gboolean is_nested = FALSE;
104
105     g_assert (asset);
106
107     g_object_get (asset, "is-nested-timeline", &is_nested, NULL);
108     gst_object_unref (asset);
109
110     return !is_nested;
111   }
112
113
114   return FALSE;
115 }
116
117 gboolean
118 ges_video_uri_source_get_natural_size (GESVideoSource * source, gint * width,
119     gint * height)
120 {
121   const GstTagList *tags = NULL;
122   gchar *rotation_info = NULL;
123   gint videoflip_method, rotate_angle;
124   GstDiscovererStreamInfo *info;
125   GESAsset *asset = ges_extractable_get_asset (GES_EXTRACTABLE (source));
126
127   g_assert (GES_IS_URI_SOURCE_ASSET (asset));
128   info = ges_uri_source_asset_get_stream_info (GES_URI_SOURCE_ASSET (asset));
129
130   if (!GST_IS_DISCOVERER_VIDEO_INFO (info)) {
131     GST_ERROR_OBJECT (source, "Doesn't have a video info (%" GST_PTR_FORMAT
132         ")", info);
133     return FALSE;
134   }
135
136   *width =
137       gst_discoverer_video_info_get_width (GST_DISCOVERER_VIDEO_INFO (info));
138   *height =
139       gst_discoverer_video_info_get_height (GST_DISCOVERER_VIDEO_INFO (info));
140
141   if (!ges_timeline_element_lookup_child (GES_TIMELINE_ELEMENT (source),
142           "GstVideoFlip::video-direction", NULL, NULL))
143     goto done;
144
145   ges_timeline_element_get_child_properties (GES_TIMELINE_ELEMENT (source),
146       "GstVideoFlip::video-direction", &videoflip_method, NULL);
147
148   /* Rotating 90 degrees, either way, rotate */
149   if (videoflip_method == 1 || videoflip_method == 3)
150     goto rotate;
151
152   if (videoflip_method != 8)
153     goto done;
154
155   /* Rotation is automatic, we need to check if the media file is naturally
156      rotated */
157   tags =
158       gst_discoverer_stream_info_get_tags (GST_DISCOVERER_STREAM_INFO (info));
159   if (!tags)
160     goto done;
161
162   if (!gst_tag_list_get_string (tags, GST_TAG_IMAGE_ORIENTATION,
163           &rotation_info))
164     goto done;
165
166   if (sscanf (rotation_info, "rotate-%d", &rotate_angle) == 1) {
167     if (rotate_angle == 90 || rotate_angle == 270)
168       goto rotate;
169   }
170
171 done:
172   g_free (rotation_info);
173   return TRUE;
174
175 rotate:
176   GST_INFO_OBJECT (self, "Stream is rotated, taking that into account");
177   *width =
178       gst_discoverer_video_info_get_height (GST_DISCOVERER_VIDEO_INFO (info));
179   *height =
180       gst_discoverer_video_info_get_width (GST_DISCOVERER_VIDEO_INFO (info));
181
182   goto done;
183 }
184
185 /* Extractable interface implementation */
186
187 static gchar *
188 ges_extractable_check_id (GType type, const gchar * id, GError ** error)
189 {
190   return g_strdup (id);
191 }
192
193 static void
194 extractable_set_asset (GESExtractable * extractable, GESAsset * asset)
195 {
196   /* FIXME That should go into #GESTrackElement, but
197    * some work is needed to make sure it works properly */
198
199   if (ges_track_element_get_track_type (GES_TRACK_ELEMENT (extractable)) ==
200       GES_TRACK_TYPE_UNKNOWN) {
201     ges_track_element_set_track_type (GES_TRACK_ELEMENT (extractable),
202         ges_track_element_asset_get_track_type (GES_TRACK_ELEMENT_ASSET
203             (asset)));
204   }
205 }
206
207 static void
208 ges_extractable_interface_init (GESExtractableInterface * iface)
209 {
210   iface->asset_type = GES_TYPE_URI_SOURCE_ASSET;
211   iface->check_id = ges_extractable_check_id;
212   iface->set_asset = extractable_set_asset;
213 }
214
215 G_DEFINE_TYPE_WITH_CODE (GESVideoUriSource, ges_video_uri_source,
216     GES_TYPE_VIDEO_SOURCE, G_ADD_PRIVATE (GESVideoUriSource)
217     G_IMPLEMENT_INTERFACE (GES_TYPE_EXTRACTABLE,
218         ges_extractable_interface_init));
219
220
221 /* GObject VMethods */
222
223 static void
224 ges_video_uri_source_get_property (GObject * object, guint property_id,
225     GValue * value, GParamSpec * pspec)
226 {
227   GESVideoUriSource *uriclip = GES_VIDEO_URI_SOURCE (object);
228
229   switch (property_id) {
230     case PROP_URI:
231       g_value_set_string (value, uriclip->uri);
232       break;
233     default:
234       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
235   }
236 }
237
238 static void
239 ges_video_uri_source_set_property (GObject * object, guint property_id,
240     const GValue * value, GParamSpec * pspec)
241 {
242   GESVideoUriSource *uriclip = GES_VIDEO_URI_SOURCE (object);
243
244   switch (property_id) {
245     case PROP_URI:
246       if (uriclip->uri) {
247         GST_WARNING_OBJECT (object, "Uri already set to %s", uriclip->uri);
248         return;
249       }
250       uriclip->uri = g_value_dup_string (value);
251       break;
252     default:
253       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
254   }
255 }
256
257 static void
258 ges_video_uri_source_dispose (GObject * object)
259 {
260   GESVideoUriSource *uriclip = GES_VIDEO_URI_SOURCE (object);
261
262   if (uriclip->uri)
263     g_free (uriclip->uri);
264
265   G_OBJECT_CLASS (ges_video_uri_source_parent_class)->dispose (object);
266 }
267
268 static void
269 ges_video_uri_source_class_init (GESVideoUriSourceClass * klass)
270 {
271   GObjectClass *object_class = G_OBJECT_CLASS (klass);
272   GESVideoSourceClass *source_class = GES_VIDEO_SOURCE_CLASS (klass);
273
274   object_class->get_property = ges_video_uri_source_get_property;
275   object_class->set_property = ges_video_uri_source_set_property;
276   object_class->dispose = ges_video_uri_source_dispose;
277
278   /**
279    * GESVideoUriSource:uri:
280    *
281    * The location of the file/resource to use.
282    */
283   g_object_class_install_property (object_class, PROP_URI,
284       g_param_spec_string ("uri", "URI", "uri of the resource",
285           NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
286
287   source_class->create_source = ges_video_uri_source_create_source;
288   source_class->ABI.abi.needs_converters =
289       ges_video_uri_source_needs_converters;
290   source_class->ABI.abi.get_natural_size =
291       ges_video_uri_source_get_natural_size;
292 }
293
294 static void
295 ges_video_uri_source_init (GESVideoUriSource * self)
296 {
297   self->priv = ges_video_uri_source_get_instance_private (self);
298
299   g_signal_connect (self, "notify::track",
300       G_CALLBACK (ges_video_uri_source_track_set_cb), NULL);
301 }
302
303 /**
304  * ges_video_uri_source_new:
305  * @uri: the URI the source should control
306  *
307  * Creates a new #GESVideoUriSource for the provided @uri.
308  *
309  * Returns: (transfer floating) (nullable): The newly created #GESVideoUriSource,
310  * or %NULL if there was an error.
311  */
312 GESVideoUriSource *
313 ges_video_uri_source_new (gchar * uri)
314 {
315   return g_object_new (GES_TYPE_VIDEO_URI_SOURCE, "uri", uri, NULL);
316 }