63ae963ef5ea3fb0698726e49486ffb6bad48439
[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  * ## Children Properties:
27  *
28  * You can use the following children properties through the
29  * #ges_track_element_set_child_property and alike set of methods:
30  * 
31  * - #gdouble `alpha`: The desired alpha for the stream.
32  * - #gint `posx`: The desired x position for the stream.
33  * - #gint `posy`: The desired y position for the stream
34  * - #gint `width`: The desired width for that source.
35  *   Set to 0 if size is not mandatory, will be set to width of the current track.
36  * - #gint `height`: The desired height for that source.
37  *   Set to 0 if size is not mandatory, will be set to height of the current track.
38  * - #GstDeinterlaceModes `deinterlace-mode`: Deinterlace Mode
39  * - #GstDeinterlaceFields `deinterlace-fields`: Fields to use for deinterlacing
40  * - #GstDeinterlaceFieldLayout `deinterlace-tff`: Deinterlace top field first
41  * - #GstVideoOrientationMethod `video-direction`: The desired video rotation and flipping.
42  */
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47 #include <gst/pbutils/missing-plugins.h>
48 #include <gst/video/video.h>
49
50 #include "ges-internal.h"
51 #include "ges/ges-meta-container.h"
52 #include "ges-track-element.h"
53 #include "ges-video-source.h"
54 #include "ges-layer.h"
55 #include "gstframepositioner.h"
56
57 #define parent_class ges_video_source_parent_class
58
59 struct _GESVideoSourcePrivate
60 {
61   GstFramePositioner *positioner;
62   GstElement *capsfilter;
63 };
64
65 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GESVideoSource, ges_video_source,
66     GES_TYPE_SOURCE);
67
68 /* TrackElement VMethods */
69
70 static gboolean
71 _set_priority (GESTimelineElement * element, guint32 priority)
72 {
73   gboolean res;
74   GESVideoSource *self = GES_VIDEO_SOURCE (element);
75
76   res = GES_TIMELINE_ELEMENT_CLASS (parent_class)->set_priority (element,
77       priority);
78
79   if (res && self->priv->positioner)
80     g_object_set (self->priv->positioner, "zorder", G_MAXUINT - priority, NULL);
81
82   return res;
83 }
84
85 static void
86 post_missing_element_message (GstElement * element, const gchar * name)
87 {
88   GstMessage *msg;
89
90   msg = gst_missing_element_message_new (element, name);
91   gst_element_post_message (element, msg);
92 }
93
94 static GstElement *
95 ges_video_source_create_element (GESTrackElement * trksrc)
96 {
97   GstElement *topbin;
98   GstElement *sub_element;
99   GESVideoSourceClass *source_class = GES_VIDEO_SOURCE_GET_CLASS (trksrc);
100   GESVideoSource *self;
101   GstElement *positioner, *videoflip, *capsfilter, *deinterlace;
102   const gchar *positioner_props[] =
103       { "alpha", "posx", "posy", "width", "height", NULL };
104   const gchar *deinterlace_props[] = { "mode", "fields", "tff", NULL };
105   const gchar *videoflip_props[] = { "video-direction", NULL };
106   gboolean needs_converters = TRUE;
107   GPtrArray *elements;
108
109   if (!source_class->create_source)
110     return NULL;
111
112   sub_element = source_class->create_source (trksrc);
113
114   self = (GESVideoSource *) trksrc;
115   if (source_class->ABI.abi.needs_converters)
116     needs_converters = source_class->ABI.abi.needs_converters (self);
117
118   elements = g_ptr_array_new ();
119   g_ptr_array_add (elements, gst_element_factory_make ("queue", NULL));
120
121   /* That positioner will add metadata to buffers according to its
122      properties, acting like a proxy for our smart-mixer dynamic pads. */
123   positioner = gst_element_factory_make ("framepositioner", "frame_tagger");
124   g_object_set (positioner, "zorder",
125       G_MAXUINT - GES_TIMELINE_ELEMENT_PRIORITY (self), NULL);
126   g_ptr_array_add (elements, positioner);
127
128   /* If there's image-orientation tag, make sure the image is correctly oriented
129    * before we scale it. */
130   videoflip = gst_element_factory_make ("videoflip", "track-element-videoflip");
131   g_object_set (videoflip, "video-direction", GST_VIDEO_ORIENTATION_AUTO, NULL);
132   g_ptr_array_add (elements, videoflip);
133
134   if (needs_converters) {
135     g_ptr_array_add (elements, gst_element_factory_make ("videoscale",
136             "track-element-videoscale"));
137     g_ptr_array_add (elements, gst_element_factory_make ("videoconvert",
138             "track-element-videoconvert"));
139   }
140   g_ptr_array_add (elements, gst_element_factory_make ("videorate",
141           "track-element-videorate"));
142   capsfilter =
143       gst_element_factory_make ("capsfilter", "track-element-capsfilter");
144   g_ptr_array_add (elements, capsfilter);
145
146   ges_frame_positioner_set_source_and_filter (GST_FRAME_POSITIONNER
147       (positioner), trksrc, capsfilter);
148
149   ges_track_element_add_children_props (trksrc, positioner, NULL, NULL,
150       positioner_props);
151   ges_track_element_add_children_props (trksrc, videoflip, NULL, NULL,
152       videoflip_props);
153
154   deinterlace = gst_element_factory_make ("deinterlace", "deinterlace");
155   if (deinterlace == NULL) {
156     post_missing_element_message (sub_element, "deinterlace");
157
158     GST_ELEMENT_WARNING (sub_element, CORE, MISSING_PLUGIN,
159         ("Missing element '%s' - check your GStreamer installation.",
160             "deinterlace"), ("deinterlacing won't work"));
161   } else {
162     g_ptr_array_add (elements, deinterlace);
163     ges_track_element_add_children_props (trksrc, deinterlace, NULL, NULL,
164         deinterlace_props);
165   }
166   topbin = ges_source_create_topbin ("videosrcbin", sub_element, elements);
167   g_ptr_array_free (elements, TRUE);
168
169   self->priv->positioner = GST_FRAME_POSITIONNER (positioner);
170   self->priv->positioner->scale_in_compositor =
171       !GES_VIDEO_SOURCE_GET_CLASS (self)->ABI.abi.disable_scale_in_compositor;
172   self->priv->capsfilter = capsfilter;
173
174   return topbin;
175 }
176
177 static gboolean
178 _lookup_child (GESTimelineElement * object,
179     const gchar * prop_name, GObject ** element, GParamSpec ** pspec)
180 {
181   gboolean res;
182
183   gchar *clean_name;
184
185   if (!g_strcmp0 (prop_name, "deinterlace-fields"))
186     clean_name = g_strdup ("GstDeinterlace::fields");
187   else if (!g_strcmp0 (prop_name, "deinterlace-mode"))
188     clean_name = g_strdup ("GstDeinterlace::mode");
189   else if (!g_strcmp0 (prop_name, "deinterlace-tff"))
190     clean_name = g_strdup ("GstDeinterlace::tff");
191   else if (!g_strcmp0 (prop_name, "tff") ||
192       !g_strcmp0 (prop_name, "fields") || !g_strcmp0 (prop_name, "mode")) {
193     GST_DEBUG_OBJECT (object, "Not allowed to use GstDeinterlace %s"
194         " property without prefixing its name", prop_name);
195     return FALSE;
196   } else
197     clean_name = g_strdup (prop_name);
198
199   res =
200       GES_TIMELINE_ELEMENT_CLASS (ges_video_source_parent_class)->lookup_child
201       (object, clean_name, element, pspec);
202
203   g_free (clean_name);
204
205   return res;
206 }
207
208 static void
209 ges_video_source_class_init (GESVideoSourceClass * klass)
210 {
211   GESTrackElementClass *track_element_class = GES_TRACK_ELEMENT_CLASS (klass);
212   GESTimelineElementClass *element_class = GES_TIMELINE_ELEMENT_CLASS (klass);
213   GESVideoSourceClass *video_source_class = GES_VIDEO_SOURCE_CLASS (klass);
214
215   element_class->set_priority = _set_priority;
216   element_class->lookup_child = _lookup_child;
217
218   track_element_class->nleobject_factorytype = "nlesource";
219   track_element_class->create_element = ges_video_source_create_element;
220   video_source_class->create_source = NULL;
221 }
222
223 static void
224 ges_video_source_init (GESVideoSource * self)
225 {
226   self->priv = ges_video_source_get_instance_private (self);
227   self->priv->positioner = NULL;
228   self->priv->capsfilter = NULL;
229 }