ges: Move GESVideo/AudioSource::create_source to GESSource
[platform/upstream/gst-editing-services.git] / ges / ges-video-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:gesvideosource
23  * @title: GESVideoSource
24  * @short_description: Base Class for video sources
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <gst/pbutils/missing-plugins.h>
31 #include <gst/video/video.h>
32
33 #include "ges-internal.h"
34 #include "ges/ges-meta-container.h"
35 #include "ges-track-element.h"
36 #include "ges-video-source.h"
37 #include "ges-layer.h"
38 #include "gstframepositioner.h"
39 #include "ges-extractable.h"
40
41 #define parent_class ges_video_source_parent_class
42 static GESExtractableInterface *parent_extractable_iface = NULL;
43
44 struct _GESVideoSourcePrivate
45 {
46   GstFramePositioner *positioner;
47   GstElement *capsfilter;
48 };
49
50 static void
51 ges_video_source_set_asset (GESExtractable * extractable, GESAsset * asset)
52 {
53   GESVideoSource *self = GES_VIDEO_SOURCE (extractable);
54
55   parent_extractable_iface->set_asset (extractable, asset);
56
57   ges_video_source_get_natural_size (self,
58       &self->priv->positioner->natural_width,
59       &self->priv->positioner->natural_height);
60 }
61
62 static void
63 ges_extractable_interface_init (GESExtractableInterface * iface)
64 {
65   iface->set_asset = ges_video_source_set_asset;
66
67   parent_extractable_iface = g_type_interface_peek_parent (iface);
68 }
69
70 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GESVideoSource, ges_video_source,
71     GES_TYPE_SOURCE, G_ADD_PRIVATE (GESVideoSource)
72     G_IMPLEMENT_INTERFACE (GES_TYPE_EXTRACTABLE,
73         ges_extractable_interface_init));
74
75 /* TrackElement VMethods */
76
77 static gboolean
78 _set_priority (GESTimelineElement * element, guint32 priority)
79 {
80   gboolean res;
81   GESVideoSource *self = GES_VIDEO_SOURCE (element);
82
83   res = GES_TIMELINE_ELEMENT_CLASS (parent_class)->set_priority (element,
84       priority);
85
86   if (res && self->priv->positioner)
87     g_object_set (self->priv->positioner, "zorder", G_MAXUINT - priority, NULL);
88
89   return res;
90 }
91
92
93 static gboolean
94 _set_parent (GESTimelineElement * element, GESTimelineElement * parent)
95 {
96   GESVideoSource *self = GES_VIDEO_SOURCE (element);
97
98   if (!parent)
99     return TRUE;
100
101   /* Some subclass might have different access to its natural size only
102    * once it knows its parent */
103   ges_video_source_get_natural_size (GES_VIDEO_SOURCE (self),
104       &self->priv->positioner->natural_width,
105       &self->priv->positioner->natural_height);
106
107   return TRUE;
108 }
109
110
111 static gboolean
112 ges_video_source_create_filters (GESVideoSource * self, GPtrArray * elements,
113     gboolean needs_converters)
114 {
115   GESTrackElement *trksrc = GES_TRACK_ELEMENT (self);
116   GstElement *positioner, *videoflip, *capsfilter;
117   const gchar *positioner_props[]
118   = { "alpha", "posx", "posy", "width", "height", "operator", NULL };
119   const gchar *videoflip_props[] = { "video-direction", NULL };
120
121   g_ptr_array_add (elements, gst_element_factory_make ("queue", NULL));
122
123   /* That positioner will add metadata to buffers according to its
124      properties, acting like a proxy for our smart-mixer dynamic pads. */
125   positioner = gst_element_factory_make ("framepositioner", NULL);
126   g_object_set (positioner, "zorder",
127       G_MAXUINT - GES_TIMELINE_ELEMENT_PRIORITY (self), NULL);
128   g_ptr_array_add (elements, positioner);
129
130   if (needs_converters)
131     g_ptr_array_add (elements, gst_element_factory_make ("videoconvert", NULL));
132
133   /* If there's image-orientation tag, make sure the image is correctly oriented
134    * before we scale it. */
135   videoflip = gst_element_factory_make ("videoflip", "track-element-videoflip");
136   g_object_set (videoflip, "video-direction", GST_VIDEO_ORIENTATION_AUTO, NULL);
137   g_ptr_array_add (elements, videoflip);
138
139   if (needs_converters) {
140     g_ptr_array_add (elements, gst_element_factory_make ("videoscale",
141             "track-element-videoscale"));
142     g_ptr_array_add (elements, gst_element_factory_make ("videoconvert",
143             "track-element-videoconvert"));
144   }
145   g_ptr_array_add (elements, gst_element_factory_make ("videorate",
146           "track-element-videorate"));
147
148   capsfilter =
149       gst_element_factory_make ("capsfilter", "track-element-capsfilter");
150   g_ptr_array_add (elements, capsfilter);
151
152   ges_frame_positioner_set_source_and_filter (GST_FRAME_POSITIONNER
153       (positioner), trksrc, capsfilter);
154
155   ges_track_element_add_children_props (trksrc, positioner, NULL, NULL,
156       positioner_props);
157   ges_track_element_add_children_props (trksrc, videoflip, NULL, NULL,
158       videoflip_props);
159
160   self->priv->positioner = GST_FRAME_POSITIONNER (positioner);
161   self->priv->positioner->scale_in_compositor =
162       !GES_VIDEO_SOURCE_GET_CLASS (self)->ABI.abi.disable_scale_in_compositor;
163   ges_video_source_get_natural_size (self,
164       &self->priv->positioner->natural_width,
165       &self->priv->positioner->natural_height);
166
167   self->priv->capsfilter = capsfilter;
168
169   return TRUE;
170 }
171
172 static GstElement *
173 ges_video_source_create_element (GESTrackElement * trksrc)
174 {
175   GstElement *topbin;
176   GstElement *sub_element;
177   GESVideoSourceClass *vsource_class = GES_VIDEO_SOURCE_GET_CLASS (trksrc);
178   GESSourceClass *source_class = GES_SOURCE_GET_CLASS (trksrc);
179   GESVideoSource *self;
180   gboolean needs_converters = TRUE;
181   GPtrArray *elements;
182
183   if (!source_class->create_source)
184     return NULL;
185
186   sub_element = source_class->create_source (GES_SOURCE (trksrc));
187
188   self = (GESVideoSource *) trksrc;
189   if (vsource_class->ABI.abi.needs_converters)
190     needs_converters = vsource_class->ABI.abi.needs_converters (self);
191
192   elements = g_ptr_array_new ();
193   g_assert (vsource_class->ABI.abi.create_filters);
194   if (!vsource_class->ABI.abi.create_filters (self, elements, needs_converters)) {
195     g_ptr_array_free (elements, TRUE);
196
197     return NULL;
198   }
199
200   topbin = ges_source_create_topbin (GES_SOURCE (trksrc), "videosrcbin",
201       sub_element, elements);
202
203   return topbin;
204 }
205
206 static gboolean
207 _lookup_child (GESTimelineElement * object,
208     const gchar * prop_name, GObject ** element, GParamSpec ** pspec)
209 {
210   gboolean res;
211
212   gchar *clean_name;
213
214   if (!g_strcmp0 (prop_name, "deinterlace-fields"))
215     clean_name = g_strdup ("GstDeinterlace::fields");
216   else if (!g_strcmp0 (prop_name, "deinterlace-mode"))
217     clean_name = g_strdup ("GstDeinterlace::mode");
218   else if (!g_strcmp0 (prop_name, "deinterlace-tff"))
219     clean_name = g_strdup ("GstDeinterlace::tff");
220   else if (!g_strcmp0 (prop_name, "tff") ||
221       !g_strcmp0 (prop_name, "fields") || !g_strcmp0 (prop_name, "mode")) {
222     GST_DEBUG_OBJECT (object, "Not allowed to use GstDeinterlace %s"
223         " property without prefixing its name", prop_name);
224     return FALSE;
225   } else
226     clean_name = g_strdup (prop_name);
227
228   res =
229       GES_TIMELINE_ELEMENT_CLASS (ges_video_source_parent_class)->lookup_child
230       (object, clean_name, element, pspec);
231
232   g_free (clean_name);
233
234   return res;
235 }
236
237 static void
238 ges_video_source_class_init (GESVideoSourceClass * klass)
239 {
240   GESTrackElementClass *track_element_class = GES_TRACK_ELEMENT_CLASS (klass);
241   GESTimelineElementClass *element_class = GES_TIMELINE_ELEMENT_CLASS (klass);
242   GESVideoSourceClass *video_source_class = GES_VIDEO_SOURCE_CLASS (klass);
243
244   element_class->set_priority = _set_priority;
245   element_class->lookup_child = _lookup_child;
246   element_class->set_parent = _set_parent;
247
248   track_element_class->nleobject_factorytype = "nlesource";
249   track_element_class->create_element = ges_video_source_create_element;
250   track_element_class->ABI.abi.default_track_type = GES_TRACK_TYPE_VIDEO;
251
252   video_source_class->ABI.abi.create_filters = ges_video_source_create_filters;
253 }
254
255 static void
256 ges_video_source_init (GESVideoSource * self)
257 {
258   self->priv = ges_video_source_get_instance_private (self);
259   self->priv->positioner = NULL;
260   self->priv->capsfilter = NULL;
261 }
262
263 /**
264  * ges_video_source_get_natural_size:
265  * @self: A #GESVideoSource
266  * @width: (out): The natural width of the underlying source
267  * @height: (out): The natural height of the underlying source
268  *
269  * Retrieves the natural size of the video stream. The natural size, is
270  * the size at which it will be displayed if no scaling is being applied.
271  *
272  * NOTE: The sources take into account the potential video rotation applied
273  * by the #videoflip element that is inside the source, effects applied on
274  * the clip which potentially also rotate the element are not taken into
275  * account.
276  *
277  * Returns: %TRUE if the object has a natural size, %FALSE otherwise.
278  *
279  * Since: 1.18
280  */
281 gboolean
282 ges_video_source_get_natural_size (GESVideoSource * self, gint * width,
283     gint * height)
284 {
285   GESVideoSourceClass *klass = GES_VIDEO_SOURCE_GET_CLASS (self);
286
287   if (!klass->ABI.abi.get_natural_size)
288     return FALSE;
289
290   return klass->ABI.abi.get_natural_size (self, width, height);
291 }