video-source: Add a videorate in video-source.
[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:ges-video-source
23  * @short_description: Base Class for video sources
24  */
25
26 #include "ges-internal.h"
27 #include "ges/ges-meta-container.h"
28 #include "ges-track-element.h"
29 #include "ges-video-source.h"
30 #include "ges-layer.h"
31 #include "gstframepositionner.h"
32
33 G_DEFINE_ABSTRACT_TYPE (GESVideoSource, ges_video_source, GES_TYPE_SOURCE);
34
35 struct _GESVideoSourcePrivate
36 {
37   GstFramePositionner *positionner;
38   GstElement *capsfilter;
39   GESLayer *layer;
40 };
41
42 /* TrackElement VMethods */
43 static void
44 layer_priority_changed_cb (GESLayer * layer, GParamSpec * arg G_GNUC_UNUSED,
45     GESVideoSource * self)
46 {
47   g_object_set (self->priv->positionner, "zorder",
48       10000 - ges_layer_get_priority (layer), NULL);
49 }
50
51 static void
52 layer_changed_cb (GESClip * clip, GParamSpec * arg G_GNUC_UNUSED,
53     GESVideoSource * self)
54 {
55   GESVideoSourcePrivate *priv = self->priv;
56
57   if (priv->layer) {
58     g_signal_handlers_disconnect_by_func (priv->layer,
59         layer_priority_changed_cb, self);
60   }
61
62   priv->layer = ges_clip_get_layer (clip);
63   if (priv->layer == NULL)
64     return;
65
66   /* We do not need any ref ourself as our parent owns one and we are connected
67    * to it */
68   g_object_unref (priv->layer);
69   /* 10000 is the max value of zorder on videomixerpad, hardcoded */
70   g_signal_connect (self->priv->layer, "notify::priority",
71       G_CALLBACK (layer_priority_changed_cb), self);
72
73   g_object_set (self->priv->positionner, "zorder",
74       10000 - ges_layer_get_priority (self->priv->layer), NULL);
75 }
76
77 static GstElement *
78 ges_video_source_create_element (GESTrackElement * trksrc)
79 {
80   GstElement *topbin;
81   GstElement *sub_element;
82   GESVideoSourceClass *source_class = GES_VIDEO_SOURCE_GET_CLASS (trksrc);
83   GESVideoSource *self;
84   GstElement *positionner, *videoscale, *videorate, *capsfilter;
85   const gchar *props[] = { "alpha", "posx", "posy", "width", "height", NULL };
86   GESTimelineElement *parent;
87
88   if (!source_class->create_source)
89     return NULL;
90
91   sub_element = source_class->create_source (trksrc);
92
93   self = (GESVideoSource *) trksrc;
94
95   /* That positionner will add metadata to buffers according to its
96      properties, acting like a proxy for our smart-mixer dynamic pads. */
97   positionner = gst_element_factory_make ("framepositionner", "frame_tagger");
98
99   videoscale =
100       gst_element_factory_make ("videoscale", "track-element-videoscale");
101   videorate = gst_element_factory_make ("videorate", "track-element-videorate");
102   capsfilter =
103       gst_element_factory_make ("capsfilter", "track-element-capsfilter");
104
105   ges_frame_positionner_set_source_and_filter (GST_FRAME_POSITIONNER
106       (positionner), trksrc, capsfilter);
107
108   ges_track_element_add_children_props (trksrc, positionner, NULL, NULL, props);
109   topbin =
110       ges_source_create_topbin ("videosrcbin", sub_element, positionner,
111       videoscale, videorate, capsfilter, NULL);
112   parent = ges_timeline_element_get_parent (GES_TIMELINE_ELEMENT (trksrc));
113   if (parent) {
114     self->priv->positionner = GST_FRAME_POSITIONNER (positionner);
115     g_signal_connect (parent, "notify::layer",
116         (GCallback) layer_changed_cb, trksrc);
117     layer_changed_cb (GES_CLIP (parent), NULL, self);
118     gst_object_unref (parent);
119   } else {
120     GST_ERROR ("No parent timeline element, SHOULD NOT HAPPEN");
121   }
122
123   self->priv->capsfilter = capsfilter;
124
125   return topbin;
126 }
127
128 static gboolean
129 _set_parent (GESTimelineElement * self, GESTimelineElement * parent)
130 {
131   GESVideoSourcePrivate *priv = GES_VIDEO_SOURCE (self)->priv;
132
133   if (self->parent) {
134     if (priv->layer) {
135       g_signal_handlers_disconnect_by_func (priv->layer,
136           layer_priority_changed_cb, self);
137       priv->layer = NULL;
138     }
139
140     g_signal_handlers_disconnect_by_func (self->parent, layer_changed_cb, self);
141   }
142
143   if (parent && priv->positionner)
144     layer_changed_cb (GES_CLIP (parent), NULL, GES_VIDEO_SOURCE (self));
145
146
147   return TRUE;
148 }
149
150 static void
151 ges_video_source_class_init (GESVideoSourceClass * klass)
152 {
153   GESTrackElementClass *track_class = GES_TRACK_ELEMENT_CLASS (klass);
154   GESTimelineElementClass *element_class = GES_TIMELINE_ELEMENT_CLASS (klass);
155   GESVideoSourceClass *video_source_class = GES_VIDEO_SOURCE_CLASS (klass);
156
157   g_type_class_add_private (klass, sizeof (GESVideoSourcePrivate));
158
159   element_class->set_parent = _set_parent;
160   track_class->gnlobject_factorytype = "gnlsource";
161   track_class->create_element = ges_video_source_create_element;
162   video_source_class->create_source = NULL;
163 }
164
165 static void
166 ges_video_source_init (GESVideoSource * self)
167 {
168   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
169       GES_TYPE_VIDEO_SOURCE, GESVideoSourcePrivate);
170   self->priv->positionner = NULL;
171   self->priv->capsfilter = NULL;
172 }