1 /* GStreamer GES plugin
3 * Copyright (C) 2019 Thibault Saunier <tsaunier@igalia.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
25 * @short_description: A GstBin subclasses use to use GESTimeline
26 * as demux inside any GstPipeline.
27 * @see_also: #GESTimeline
29 * The gstdemux is a bin that will simply expose the track source pads
30 * and implements the GstUriHandler interface using a custom ges://0Xpointer
39 #include <glib/gstdio.h>
40 #include <gst/pbutils/pbutils.h>
43 GST_DEBUG_CATEGORY_STATIC (gesdemux);
44 #define GST_CAT_DEFAULT gesdemux
46 static GstStaticPadTemplate video_src_template =
47 GST_STATIC_PAD_TEMPLATE ("video_src",
50 GST_STATIC_CAPS ("video/x-raw(ANY)"));
52 static GstStaticPadTemplate audio_src_template =
53 GST_STATIC_PAD_TEMPLATE ("audio_src",
56 GST_STATIC_CAPS ("audio/x-raw(ANY);"));
58 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
61 GST_STATIC_CAPS ("application/xges"));
63 G_DEFINE_TYPE (GESDemux, ges_demux, GST_TYPE_BIN);
72 static GParamSpec *properties[PROP_LAST];
75 ges_demux_set_timeline (GESDemux * self, GESTimeline * timeline)
78 guint naudiopad = 0, nvideopad = 0;
79 GstBin *sbin = GST_BIN (self);
81 g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
84 GST_ERROR_OBJECT (self, "Implement changing timeline support");
89 GST_INFO_OBJECT (self, "Setting timeline: %" GST_PTR_FORMAT, timeline);
90 self->timeline = gst_object_ref (timeline);
92 if (!gst_bin_add (sbin, GST_ELEMENT (self->timeline))) {
93 GST_ERROR_OBJECT (self, "Could not add timeline to myself!");
97 for (tmp = self->timeline->tracks; tmp; tmp = tmp->next) {
101 GESTrack *track = GES_TRACK (tmp->data);
102 GstPad *tmppad, *pad =
103 ges_timeline_get_pad_for_track (self->timeline, track);
104 GstStaticPadTemplate *template;
107 GST_WARNING_OBJECT (self, "No pad for track: %" GST_PTR_FORMAT, track);
112 if (track->type == GES_TRACK_TYPE_AUDIO) {
113 name = g_strdup_printf ("audio_%u", naudiopad++);
114 template = &audio_src_template;
115 } else if (track->type == GES_TRACK_TYPE_VIDEO) {
116 name = g_strdup_printf ("video_%u", nvideopad++);
117 template = &video_src_template;
119 GST_INFO_OBJECT (self, "Track type not handled: %" GST_PTR_FORMAT, track);
123 queue = gst_element_factory_make ("queue", NULL);
124 /* Add queues the same way as in GESPipeline */
125 g_object_set (G_OBJECT (queue), "max-size-buffers", 0,
126 "max-size-bytes", 0, "max-size-time", (gint64) 2 * GST_SECOND, NULL);
127 gst_bin_add (GST_BIN (self), queue);
128 gst_element_sync_state_with_parent (GST_ELEMENT (queue));
130 tmppad = gst_element_get_static_pad (queue, "sink");
131 if (gst_pad_link (pad, tmppad) != GST_PAD_LINK_OK) {
132 GST_ERROR_OBJECT (self, "Could not link %s:%s and %s:%s",
133 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (tmppad));
135 gst_object_unref (tmppad);
136 gst_object_unref (queue);
140 tmppad = gst_element_get_static_pad (queue, "src");
141 gpad = gst_ghost_pad_new_from_template (name, tmppad,
142 gst_static_pad_template_get (template));
144 gst_pad_set_active (gpad, TRUE);
145 gst_element_add_pad (GST_ELEMENT (self), gpad);
146 GST_DEBUG_OBJECT (self, "Adding pad: %" GST_PTR_FORMAT, gpad);
149 gst_element_sync_state_with_parent (GST_ELEMENT (self->timeline));
155 ges_demux_get_property (GObject * object, guint property_id,
156 GValue * value, GParamSpec * pspec)
158 GESDemux *self = GES_DEMUX (object);
160 switch (property_id) {
162 g_value_set_object (value, self->timeline);
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
170 ges_demux_set_property (GObject * object, guint property_id,
171 const GValue * value, GParamSpec * pspec)
173 switch (property_id) {
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
180 ges_demux_dispose (GObject * object)
182 GESDemux *self = GES_DEMUX (object);
185 gst_clear_object (&self->timeline);
189 ges_demux_class_init (GESDemuxClass * self_class)
191 GObjectClass *gclass = G_OBJECT_CLASS (self_class);
192 GstElementClass *gstelement_klass = GST_ELEMENT_CLASS (self_class);
194 GST_DEBUG_CATEGORY_INIT (gesdemux, "gesdemux", 0, "ges demux element");
196 gclass->get_property = ges_demux_get_property;
197 gclass->set_property = ges_demux_set_property;
198 gclass->dispose = ges_demux_dispose;
203 * Timeline to use in this source.
205 properties[PROP_TIMELINE] = g_param_spec_object ("timeline", "Timeline",
206 "Timeline to use in this source.",
207 GES_TYPE_TIMELINE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
209 g_object_class_install_properties (gclass, PROP_LAST, properties);
211 gst_element_class_set_static_metadata (gstelement_klass,
212 "GStreamer Editing Services based 'demuxer'",
213 "Codec/Demux/Editing",
214 "Demuxer for complex timeline file formats using GES.",
215 "Thibault Saunier <tsaunier@igalia.com");
217 gst_element_class_add_pad_template (gstelement_klass,
218 gst_static_pad_template_get (&sink_template));
219 gst_element_class_add_pad_template (gstelement_klass,
220 gst_static_pad_template_get (&video_src_template));
221 gst_element_class_add_pad_template (gstelement_klass,
222 gst_static_pad_template_get (&audio_src_template));
227 GESTimeline *timeline;
235 } TimelineConstructionData;
238 project_loaded_cb (GESProject * project, GESTimeline * timeline,
239 TimelineConstructionData * data)
241 g_mutex_lock (&data->lock);
242 data->timeline = timeline;
243 g_signal_handler_disconnect (project, data->loaded_sigid);
244 data->loaded_sigid = 0;
245 g_mutex_unlock (&data->lock);
247 g_main_loop_quit (data->ml);
251 error_loading_asset_cb (GESProject * project, GError * error, gchar * id,
252 GType extractable_type, TimelineConstructionData * data)
254 g_mutex_lock (&data->lock);
255 data->error = g_error_copy (error);
256 g_signal_handler_disconnect (project, data->error_sigid);
257 data->error_sigid = 0;
258 g_mutex_unlock (&data->lock);
260 g_main_loop_quit (data->ml);
263 /* TODO: Add a way to run a function in the right GES thread */
265 ges_timeline_new_from_uri_from_main_thread (TimelineConstructionData * data)
267 GESProject *project = ges_project_new (data->uri);
268 GESUriClipAssetClass *klass = g_type_class_peek (GES_TYPE_URI_CLIP_ASSET);
269 GstDiscoverer *previous_discoverer = klass->discoverer;
270 GstClockTime timeout;
271 G_GNUC_UNUSED void *unused;
273 g_object_get (previous_discoverer, "timeout", &timeout, NULL);
275 /* Make sure to use a new discoverer in case we are being discovered,
276 * as discovering is done one by one, and the global discoverer won't
277 * have the chance to discover the project assets */
278 g_mutex_lock (&data->lock);
279 klass->discoverer = gst_discoverer_new (timeout, &data->error);
281 klass->discoverer = previous_discoverer;
282 g_mutex_unlock (&data->lock);
286 g_signal_connect (klass->discoverer, "discovered",
287 G_CALLBACK (klass->discovered), NULL);
288 gst_discoverer_start (klass->discoverer);
290 data->ml = g_main_loop_new (NULL, TRUE);
292 g_signal_connect (project, "loaded", G_CALLBACK (project_loaded_cb),
295 g_signal_connect (project, "error-loading-asset",
296 G_CALLBACK (error_loading_asset_cb), data);
298 unused = GES_TIMELINE (ges_asset_extract (GES_ASSET (project), &data->error));
300 g_mutex_unlock (&data->lock);
304 g_mutex_unlock (&data->lock);
306 g_main_loop_run (data->ml);
307 g_main_loop_unref (data->ml);
311 g_mutex_lock (&data->lock);
313 /* Set previous discoverer back! */
315 if (klass->discoverer)
316 gst_object_unref (klass->discoverer);
317 klass->discoverer = previous_discoverer;
320 ges_timeline_commit (data->timeline);
322 if (data->loaded_sigid)
323 g_signal_handler_disconnect (project, data->loaded_sigid);
325 if (data->error_sigid)
326 g_signal_handler_disconnect (project, data->error_sigid);
328 gst_clear_object (&project);
330 g_cond_broadcast (&data->cond);
331 g_mutex_unlock (&data->lock);
333 return G_SOURCE_REMOVE;
337 ges_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
339 GESDemux *self = GES_DEMUX (parent);
341 switch (event->type) {
344 GstBuffer *xges_buffer;
348 available = gst_adapter_available (self->input_adapter);
349 if (available == 0) {
350 GST_WARNING_OBJECT (self,
351 "Received EOS without any serialized timeline.");
353 return gst_pad_event_default (pad, parent, event);
356 xges_buffer = gst_adapter_take_buffer (self->input_adapter, available);
357 if (gst_buffer_map (xges_buffer, &map, GST_MAP_READ)) {
359 gchar *filename = NULL, *uri = NULL;
360 TimelineConstructionData data = { 0, };
361 gint f = g_file_open_tmp (NULL, &filename, &err);
362 GMainContext *main_context = g_main_context_default ();
364 GST_ERROR ("Loading %s", filename);
366 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_WRITE,
367 ("Could not open temporary file to write timeline description"),
368 ("%s", err->message));
373 g_file_set_contents (filename, (gchar *) map.data, map.size, &err);
375 GST_ELEMENT_ERROR (self, RESOURCE, WRITE,
376 ("Could not write temporary timeline description file"),
377 ("%s", err->message));
382 uri = gst_uri_construct ("file", filename);
385 g_main_context_invoke (main_context,
386 (GSourceFunc) ges_timeline_new_from_uri_from_main_thread, &data);
387 g_mutex_lock (&data.lock);
388 while (!data.error && !data.timeline)
389 g_cond_wait (&data.cond, &data.lock);
390 data.loaded_sigid = 0;
391 data.error_sigid = 0;
392 g_mutex_unlock (&data.lock);
395 GST_ELEMENT_ERROR (self, STREAM, DEMUX,
396 ("Could not create timeline from description"),
397 ("%s", data.error->message));
398 g_clear_error (&data.error);
403 GST_INFO_OBJECT (self, "Timeline properly loaded: %" GST_PTR_FORMAT,
405 ges_demux_set_timeline (self, data.timeline);
415 GST_ELEMENT_ERROR (self, RESOURCE, READ,
416 ("Could not map buffer containing timeline description"),
419 GST_ERROR_OBJECT (xges_buffer, ":YAY");
425 return gst_pad_event_default (pad, parent, event);
429 ges_demux_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
431 GESDemux *self = GES_DEMUX (parent);
433 gst_adapter_push (self->input_adapter, buffer);
435 GST_INFO_OBJECT (self, "Received buffer, total size is %i bytes",
436 (gint) gst_adapter_available (self->input_adapter));
442 ges_demux_init (GESDemux * self)
445 self->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
446 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
448 self->input_adapter = gst_adapter_new ();
450 gst_pad_set_chain_function (self->sinkpad,
451 GST_DEBUG_FUNCPTR (ges_demux_sink_chain));
453 gst_pad_set_event_function (self->sinkpad,
454 GST_DEBUG_FUNCPTR (ges_demux_sink_event));