ges: Add a method to retrieve the 'natural' size of VideoSource
[platform/upstream/gst-editing-services.git] / ges / ges-image-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:gesimagesource
23  * @title: GESImageSource
24  * @short_description: outputs the video stream from a media file as a still
25  * image.
26  *
27  * Outputs the video stream from a given file as a still frame. The frame
28  * chosen will be determined by the in-point property on the track element. For
29  * image files, do not set the in-point property.
30  */
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include "ges-internal.h"
36 #include "ges-track-element.h"
37 #include "ges-image-source.h"
38
39
40 struct _GESImageSourcePrivate
41 {
42   /*  Dummy variable */
43   void *nothing;
44 };
45
46 enum
47 {
48   PROP_0,
49   PROP_URI
50 };
51
52 G_DEFINE_TYPE_WITH_PRIVATE (GESImageSource, ges_image_source,
53     GES_TYPE_VIDEO_SOURCE);
54 static void
55 ges_image_source_get_property (GObject * object, guint property_id,
56     GValue * value, GParamSpec * pspec)
57 {
58   GESImageSource *uriclip = GES_IMAGE_SOURCE (object);
59
60   switch (property_id) {
61     case PROP_URI:
62       g_value_set_string (value, uriclip->uri);
63       break;
64     default:
65       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
66   }
67 }
68
69 static void
70 ges_image_source_set_property (GObject * object, guint property_id,
71     const GValue * value, GParamSpec * pspec)
72 {
73   GESImageSource *uriclip = GES_IMAGE_SOURCE (object);
74
75   switch (property_id) {
76     case PROP_URI:
77       uriclip->uri = g_value_dup_string (value);
78       break;
79     default:
80       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
81   }
82 }
83
84 static void
85 ges_image_source_dispose (GObject * object)
86 {
87   GESImageSource *uriclip = GES_IMAGE_SOURCE (object);
88
89   if (uriclip->uri)
90     g_free (uriclip->uri);
91
92   G_OBJECT_CLASS (ges_image_source_parent_class)->dispose (object);
93 }
94
95 static void
96 pad_added_cb (GstElement * timeline, GstPad * pad, GstElement * scale)
97 {
98   GstPad *sinkpad;
99   GstPadLinkReturn ret;
100
101   sinkpad = gst_element_get_static_pad (scale, "sink");
102   if (sinkpad) {
103     GST_DEBUG ("got sink pad, trying to link");
104
105     ret = gst_pad_link (pad, sinkpad);
106     gst_object_unref (sinkpad);
107     if (GST_PAD_LINK_SUCCESSFUL (ret)) {
108       GST_DEBUG ("linked ok, returning");
109       return;
110     }
111   }
112
113   GST_DEBUG ("pad failed to link properly");
114 }
115
116 static GstElement *
117 ges_image_source_create_source (GESTrackElement * track_element)
118 {
119   GstElement *bin, *source, *scale, *freeze, *iconv;
120   GstPad *src, *target;
121
122   bin = GST_ELEMENT (gst_bin_new ("still-image-bin"));
123   source = gst_element_factory_make ("uridecodebin", NULL);
124   scale = gst_element_factory_make ("videoscale", NULL);
125   freeze = gst_element_factory_make ("imagefreeze", NULL);
126   iconv = gst_element_factory_make ("videoconvert", NULL);
127
128   g_object_set (scale, "add-borders", TRUE, NULL);
129
130   gst_bin_add_many (GST_BIN (bin), source, scale, freeze, iconv, NULL);
131
132   gst_element_link_pads_full (scale, "src", iconv, "sink",
133       GST_PAD_LINK_CHECK_NOTHING);
134   gst_element_link_pads_full (iconv, "src", freeze, "sink",
135       GST_PAD_LINK_CHECK_NOTHING);
136
137   /* FIXME: add capsfilter here with sink caps (see 626518) */
138
139   target = gst_element_get_static_pad (freeze, "src");
140
141   src = gst_ghost_pad_new ("src", target);
142   gst_element_add_pad (bin, src);
143   gst_object_unref (target);
144
145   g_object_set (source, "uri", ((GESImageSource *) track_element)->uri, NULL);
146
147   g_signal_connect (G_OBJECT (source), "pad-added",
148       G_CALLBACK (pad_added_cb), scale);
149
150   return bin;
151 }
152
153 static void
154 ges_image_source_class_init (GESImageSourceClass * klass)
155 {
156   GObjectClass *object_class = G_OBJECT_CLASS (klass);
157   GESVideoSourceClass *source_class = GES_VIDEO_SOURCE_CLASS (klass);
158
159   object_class->get_property = ges_image_source_get_property;
160   object_class->set_property = ges_image_source_set_property;
161   object_class->dispose = ges_image_source_dispose;
162
163   /**
164    * GESImageSource:uri:
165    *
166    * The location of the file/resource to use.
167    */
168   g_object_class_install_property (object_class, PROP_URI,
169       g_param_spec_string ("uri", "URI", "uri of the resource",
170           NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
171
172   GES_TIMELINE_ELEMENT_CLASS (klass)->set_inpoint = NULL;
173   source_class->create_source = ges_image_source_create_source;
174   source_class->ABI.abi.get_natural_size =
175       ges_video_uri_source_get_natural_size;
176 }
177
178 static void
179 ges_image_source_init (GESImageSource * self)
180 {
181   self->priv = ges_image_source_get_instance_private (self);
182 }
183
184 /**
185  * ges_image_source_new:
186  * @uri: the URI the source should control
187  *
188  * Creates a new #GESImageSource for the provided @uri.
189  *
190  * Returns: (transfer floating): A new #GESImageSource.
191  */
192 GESImageSource *
193 ges_image_source_new (gchar * uri)
194 {
195   return g_object_new (GES_TYPE_IMAGE_SOURCE, "uri", uri, "track-type",
196       GES_TRACK_TYPE_VIDEO, NULL);
197 }