1 /* GStreamer Editing Services
2  * Copyright (C) 2013 Lubosz Sarnecki <lubosz@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /**
21  * SECTION:gesmultifilesource
22  * @title: GESMultiFileSource
23  * @short_description: outputs the video stream from a sequence of images.
24  *
25  * Outputs the video stream from a given image sequence. The start frame
26  * chosen will be determined by the in-point property on the track element.
27  */
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <stdlib.h>
33 #include <string.h>
34 #include "ges-internal.h"
35 #include "ges-track-element.h"
36 #include "ges-multi-file-source.h"
37 #include "ges-extractable.h"
38 #include "ges-uri-asset.h"
39 #include "ges-internal.h"
40 
41 /* Extractable interface implementation */
42 
43 static gchar *
ges_extractable_check_id(GType type,const gchar * id,GError ** error)44 ges_extractable_check_id (GType type, const gchar * id, GError ** error)
45 {
46   return g_strdup (id);
47 }
48 
49 static void
ges_extractable_interface_init(GESExtractableInterface * iface)50 ges_extractable_interface_init (GESExtractableInterface * iface)
51 {
52   iface->check_id = ges_extractable_check_id;
53 }
54 
55 struct _GESMultiFileSourcePrivate
56 {
57   /*  Dummy variable */
58   void *nothing;
59 };
60 
61 enum
62 {
63   PROP_0,
64   PROP_URI
65 };
66 
67 G_DEFINE_TYPE_WITH_CODE (GESMultiFileSource, ges_multi_file_source,
68     GES_TYPE_VIDEO_SOURCE, G_ADD_PRIVATE (GESMultiFileSource)
69     G_IMPLEMENT_INTERFACE (GES_TYPE_EXTRACTABLE,
70         ges_extractable_interface_init));
71 
72 static void
ges_multi_file_source_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)73 ges_multi_file_source_get_property (GObject * object, guint property_id,
74     GValue * value, GParamSpec * pspec)
75 {
76   GESMultiFileSource *uriclip = GES_MULTI_FILE_SOURCE (object);
77 
78   switch (property_id) {
79     case PROP_URI:
80       g_value_set_string (value, uriclip->uri);
81       break;
82     default:
83       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
84   }
85 }
86 
87 static void
ges_multi_file_source_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)88 ges_multi_file_source_set_property (GObject * object, guint property_id,
89     const GValue * value, GParamSpec * pspec)
90 {
91   GESMultiFileSource *uriclip = GES_MULTI_FILE_SOURCE (object);
92 
93   switch (property_id) {
94     case PROP_URI:
95       uriclip->uri = g_value_dup_string (value);
96       break;
97     default:
98       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
99   }
100 }
101 
102 static void
ges_multi_file_source_dispose(GObject * object)103 ges_multi_file_source_dispose (GObject * object)
104 {
105   GESMultiFileSource *uriclip = GES_MULTI_FILE_SOURCE (object);
106 
107   if (uriclip->uri)
108     g_free (uriclip->uri);
109 
110   G_OBJECT_CLASS (ges_multi_file_source_parent_class)->dispose (object);
111 }
112 
113 static void
pad_added_cb(GstElement * decodebin,GstPad * pad,GstElement * bin)114 pad_added_cb (GstElement * decodebin, GstPad * pad, GstElement * bin)
115 {
116   GstPad *srcpad;
117 
118   srcpad = gst_ghost_pad_new ("src", pad);
119 
120   gst_pad_set_active (srcpad, TRUE);
121   gst_element_add_pad (bin, srcpad);
122 }
123 
124 /**
125   * ges_multi_file_uri_new: (skip)
126   *
127   * Reads start/stop index and location from a multifile uri.
128   *
129   */
130 GESMultiFileURI *
ges_multi_file_uri_new(const gchar * uri)131 ges_multi_file_uri_new (const gchar * uri)
132 {
133   gchar *colon = NULL;
134   gchar *at = NULL;
135   gchar *indices;
136   int charpos;
137   GESMultiFileURI *uri_data;
138   const int prefix_size = strlen (GES_MULTI_FILE_URI_PREFIX);
139 
140   uri_data = malloc (sizeof (GESMultiFileURI));
141 
142   uri_data->start = 0;
143   uri_data->end = -1;
144 
145   at = strchr (uri, '@');
146   if (at != NULL) {
147     charpos = (int) (at - uri);
148     indices = g_strdup_printf ("%.*s", charpos, uri);
149     indices = &indices[prefix_size];
150     colon = strchr (indices, ':');
151     if (colon != NULL) {
152       charpos = (int) (colon - indices);
153       uri_data->end = atoi (colon + 1);
154       uri_data->start = atoi (g_strdup_printf ("%.*s", charpos, indices));
155       GST_DEBUG ("indices start: %d end %d\n", uri_data->start, uri_data->end);
156     } else {
157       GST_ERROR
158           ("Malformated multifile uri. You are using '@' and are missing ':'");
159     }
160     uri_data->location = at + 1;
161   } else {
162     uri_data->location = g_strdup (&uri[prefix_size]);
163   }
164   GST_DEBUG ("location: %s\n", uri_data->location);
165 
166   return uri_data;
167 }
168 
169 static GstElement *
ges_multi_file_source_create_source(GESTrackElement * track_element)170 ges_multi_file_source_create_source (GESTrackElement * track_element)
171 {
172   GESMultiFileSource *self;
173   GstElement *bin, *src, *decodebin;
174   GstCaps *disc_caps;
175   GstDiscovererStreamInfo *stream_info;
176   GValue fps = G_VALUE_INIT;
177   GstCaps *caps;
178   GESUriSourceAsset *asset;
179   GESMultiFileURI *uri_data;
180 
181   self = (GESMultiFileSource *) track_element;
182 
183   asset =
184       GES_URI_SOURCE_ASSET (ges_extractable_get_asset (GES_EXTRACTABLE (self)));
185 
186   if (asset != NULL) {
187     stream_info = ges_uri_source_asset_get_stream_info (asset);
188     g_assert (stream_info);
189     disc_caps = gst_discoverer_stream_info_get_caps (stream_info);
190     caps = gst_caps_copy (disc_caps);
191     GST_DEBUG_OBJECT (disc_caps, "Got some nice caps");
192     gst_object_unref (stream_info);
193     gst_caps_unref (disc_caps);
194   } else {
195     caps = gst_caps_new_empty ();
196     GST_WARNING ("Could not extract asset.");
197   }
198 
199   g_value_init (&fps, GST_TYPE_FRACTION);
200   gst_value_set_fraction (&fps, 25, 1);
201   gst_caps_set_value (caps, "framerate", &fps);
202 
203   bin = GST_ELEMENT (gst_bin_new ("multi-image-bin"));
204   src = gst_element_factory_make ("multifilesrc", NULL);
205 
206   uri_data = ges_multi_file_uri_new (self->uri);
207   g_object_set (src, "start-index", uri_data->start, "stop-index",
208       uri_data->end, "caps", caps, "location", uri_data->location, NULL);
209   g_free (uri_data);
210 
211   decodebin = gst_element_factory_make ("decodebin", NULL);
212 
213   gst_bin_add_many (GST_BIN (bin), src, decodebin, NULL);
214   gst_element_link_pads_full (src, "src", decodebin, "sink",
215       GST_PAD_LINK_CHECK_NOTHING);
216 
217   g_signal_connect (G_OBJECT (decodebin), "pad-added",
218       G_CALLBACK (pad_added_cb), bin);
219 
220   return bin;
221 }
222 
223 static void
ges_multi_file_source_class_init(GESMultiFileSourceClass * klass)224 ges_multi_file_source_class_init (GESMultiFileSourceClass * klass)
225 {
226   GObjectClass *object_class = G_OBJECT_CLASS (klass);
227   GESVideoSourceClass *source_class = GES_VIDEO_SOURCE_CLASS (klass);
228 
229   object_class->get_property = ges_multi_file_source_get_property;
230   object_class->set_property = ges_multi_file_source_set_property;
231   object_class->dispose = ges_multi_file_source_dispose;
232 
233   /**
234    * GESMultiFileSource:uri:
235    *
236    * The uri of the file/resource to use. You can set a start index,
237    * a stop index and a sequence pattern.
238    * The format is &lt;multifile://start:stop\@location-pattern&gt;.
239    * The pattern uses printf string formating.
240    *
241    * Example uris:
242    *
243    * multifile:///home/you/image\%03d.jpg
244    *
245    * multifile://20:50@/home/you/sequence/\%04d.png
246    *
247    */
248   g_object_class_install_property (object_class, PROP_URI,
249       g_param_spec_string ("uri", "URI", "multifile uri",
250           NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
251   source_class->create_source = ges_multi_file_source_create_source;
252 }
253 
254 static void
ges_multi_file_source_init(GESMultiFileSource * self)255 ges_multi_file_source_init (GESMultiFileSource * self)
256 {
257   self->priv = ges_multi_file_source_get_instance_private (self);
258 }
259 
260 /**
261  * ges_multi_file_source_new:
262  * @uri: the URI the source should control
263  *
264  * Creates a new #GESMultiFileSource for the provided @uri.
265  *
266  * Returns: (transfer floating): A new #GESMultiFileSource.
267  */
268 GESMultiFileSource *
ges_multi_file_source_new(gchar * uri)269 ges_multi_file_source_new (gchar * uri)
270 {
271   return g_object_new (GES_TYPE_MULTI_FILE_SOURCE, "uri", uri,
272       "track-type", GES_TRACK_TYPE_VIDEO, NULL);
273 }
274