More porting to markdown
[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   GstElement *queue = gst_element_factory_make ("queue", NULL);
100   GESVideoSourceClass *source_class = GES_VIDEO_SOURCE_GET_CLASS (trksrc);
101   GESVideoSource *self;
102   GstElement *positioner, *videoflip, *videoscale, *videorate, *capsfilter,
103       *videoconvert, *deinterlace;
104   const gchar *positioner_props[] =
105       { "alpha", "posx", "posy", "width", "height", NULL };
106   const gchar *deinterlace_props[] = { "mode", "fields", "tff", NULL };
107   const gchar *videoflip_props[] = { "video-direction", NULL };
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
116   /* That positioner will add metadata to buffers according to its
117      properties, acting like a proxy for our smart-mixer dynamic pads. */
118   positioner = gst_element_factory_make ("framepositioner", "frame_tagger");
119   g_object_set (positioner, "zorder",
120       G_MAXUINT - GES_TIMELINE_ELEMENT_PRIORITY (self), NULL);
121
122   /* If there's image-orientation tag, make sure the image is correctly oriented
123    * before we scale it. */
124   videoflip = gst_element_factory_make ("videoflip", "track-element-videoflip");
125   g_object_set (videoflip, "video-direction", GST_VIDEO_ORIENTATION_AUTO, NULL);
126
127   videoscale =
128       gst_element_factory_make ("videoscale", "track-element-videoscale");
129   videoconvert =
130       gst_element_factory_make ("videoconvert", "track-element-videoconvert");
131   videorate = gst_element_factory_make ("videorate", "track-element-videorate");
132   deinterlace = gst_element_factory_make ("deinterlace", "deinterlace");
133   capsfilter =
134       gst_element_factory_make ("capsfilter", "track-element-capsfilter");
135
136   ges_frame_positioner_set_source_and_filter (GST_FRAME_POSITIONNER
137       (positioner), trksrc, capsfilter);
138
139   ges_track_element_add_children_props (trksrc, positioner, NULL, NULL,
140       positioner_props);
141   ges_track_element_add_children_props (trksrc, videoflip, NULL, NULL,
142       videoflip_props);
143
144   if (deinterlace == NULL) {
145     post_missing_element_message (sub_element, "deinterlace");
146
147     GST_ELEMENT_WARNING (sub_element, CORE, MISSING_PLUGIN,
148         ("Missing element '%s' - check your GStreamer installation.",
149             "deinterlace"), ("deinterlacing won't work"));
150     topbin =
151         ges_source_create_topbin ("videosrcbin", sub_element, queue,
152         videoconvert, positioner, videoflip, videoscale, videorate, capsfilter,
153         NULL);
154   } else {
155     ges_track_element_add_children_props (trksrc, deinterlace, NULL, NULL,
156         deinterlace_props);
157     topbin =
158         ges_source_create_topbin ("videosrcbin", sub_element, queue,
159         videoconvert, deinterlace, positioner, videoflip, videoscale, videorate,
160         capsfilter, NULL);
161   }
162
163   self->priv->positioner = GST_FRAME_POSITIONNER (positioner);
164   self->priv->positioner->scale_in_compositor =
165       !GES_VIDEO_SOURCE_GET_CLASS (self)->ABI.abi.disable_scale_in_compositor;
166   self->priv->capsfilter = capsfilter;
167
168   return topbin;
169 }
170
171 static gboolean
172 _lookup_child (GESTimelineElement * object,
173     const gchar * prop_name, GObject ** element, GParamSpec ** pspec)
174 {
175   gboolean res;
176
177   gchar *clean_name;
178
179   if (!g_strcmp0 (prop_name, "deinterlace-fields"))
180     clean_name = g_strdup ("GstDeinterlace::fields");
181   else if (!g_strcmp0 (prop_name, "deinterlace-mode"))
182     clean_name = g_strdup ("GstDeinterlace::mode");
183   else if (!g_strcmp0 (prop_name, "deinterlace-tff"))
184     clean_name = g_strdup ("GstDeinterlace::tff");
185   else if (!g_strcmp0 (prop_name, "tff") ||
186       !g_strcmp0 (prop_name, "fields") || !g_strcmp0 (prop_name, "mode")) {
187     GST_DEBUG_OBJECT (object, "Not allowed to use GstDeinterlace %s"
188         " property without prefixing its name", prop_name);
189     return FALSE;
190   } else
191     clean_name = g_strdup (prop_name);
192
193   res =
194       GES_TIMELINE_ELEMENT_CLASS (ges_video_source_parent_class)->lookup_child
195       (object, clean_name, element, pspec);
196
197   g_free (clean_name);
198
199   return res;
200 }
201
202 static void
203 ges_video_source_class_init (GESVideoSourceClass * klass)
204 {
205   GESTrackElementClass *track_element_class = GES_TRACK_ELEMENT_CLASS (klass);
206   GESTimelineElementClass *element_class = GES_TIMELINE_ELEMENT_CLASS (klass);
207   GESVideoSourceClass *video_source_class = GES_VIDEO_SOURCE_CLASS (klass);
208
209   element_class->set_priority = _set_priority;
210   element_class->lookup_child = _lookup_child;
211
212   track_element_class->nleobject_factorytype = "nlesource";
213   track_element_class->create_element = ges_video_source_create_element;
214   video_source_class->create_source = NULL;
215 }
216
217 static void
218 ges_video_source_init (GESVideoSource * self)
219 {
220   self->priv = ges_video_source_get_instance_private (self);
221   self->priv->positioner = NULL;
222   self->priv->capsfilter = NULL;
223 }