824d1ce30d826180211ca877fcf890028b6abc46
[platform/upstream/gst-editing-services.git] / plugins / ges / gesdemux.c
1 /* GStreamer GES plugin
2  *
3  * Copyright (C) 2019 Thibault Saunier <tsaunier@igalia.com>
4  *
5  * gesdemux.c
6  *
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.
11  *
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.
16  *
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.
21  *
22
23  **
24  * SECTION:gstdemux
25  * @short_description: A GstBin subclasses use to use GESTimeline
26  * as demux inside any GstPipeline.
27  * @see_also: #GESTimeline
28  *
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
31  * uri scheme.
32  **/
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <gst/gst.h>
39 #include <glib/gstdio.h>
40 #include <gst/pbutils/pbutils.h>
41 #include "gesdemux.h"
42
43 GST_DEBUG_CATEGORY_STATIC (gesdemux);
44 #define GST_CAT_DEFAULT gesdemux
45
46 static GstStaticPadTemplate video_src_template =
47 GST_STATIC_PAD_TEMPLATE ("video_src",
48     GST_PAD_SRC,
49     GST_PAD_SOMETIMES,
50     GST_STATIC_CAPS ("video/x-raw(ANY)"));
51
52 static GstStaticPadTemplate audio_src_template =
53     GST_STATIC_PAD_TEMPLATE ("audio_src",
54     GST_PAD_SRC,
55     GST_PAD_SOMETIMES,
56     GST_STATIC_CAPS ("audio/x-raw(ANY);"));
57
58 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("application/xges"));
62
63 G_DEFINE_TYPE (GESDemux, ges_demux, GST_TYPE_BIN);
64
65 enum
66 {
67   PROP_0,
68   PROP_TIMELINE,
69   PROP_LAST
70 };
71
72 static GParamSpec *properties[PROP_LAST];
73
74 static gboolean
75 ges_demux_set_timeline (GESDemux * self, GESTimeline * timeline)
76 {
77   GList *tmp;
78   guint naudiopad = 0, nvideopad = 0;
79   GstBin *sbin = GST_BIN (self);
80
81   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
82
83   if (self->timeline) {
84     GST_ERROR_OBJECT (self, "Implement changing timeline support");
85
86     return FALSE;
87   }
88
89   GST_INFO_OBJECT (self, "Setting timeline: %" GST_PTR_FORMAT, timeline);
90   self->timeline = gst_object_ref (timeline);
91
92   if (!gst_bin_add (sbin, GST_ELEMENT (self->timeline))) {
93     GST_ERROR_OBJECT (self, "Could not add timeline to myself!");
94
95     return FALSE;
96   }
97   for (tmp = self->timeline->tracks; tmp; tmp = tmp->next) {
98     GstPad *gpad;
99     gchar *name = NULL;
100     GstElement *queue;
101     GESTrack *track = GES_TRACK (tmp->data);
102     GstPad *tmppad, *pad =
103         ges_timeline_get_pad_for_track (self->timeline, track);
104     GstStaticPadTemplate *template;
105
106     if (!pad) {
107       GST_WARNING_OBJECT (self, "No pad for track: %" GST_PTR_FORMAT, track);
108
109       continue;
110     }
111
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;
118     } else {
119       GST_INFO_OBJECT (self, "Track type not handled: %" GST_PTR_FORMAT, track);
120       continue;
121     }
122
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));
129
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));
134
135       gst_object_unref (tmppad);
136       gst_object_unref (queue);
137       continue;
138     }
139
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));
143
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);
147   }
148
149   gst_element_sync_state_with_parent (GST_ELEMENT (self->timeline));
150
151   return TRUE;
152 }
153
154 static void
155 ges_demux_get_property (GObject * object, guint property_id,
156     GValue * value, GParamSpec * pspec)
157 {
158   GESDemux *self = GES_DEMUX (object);
159
160   switch (property_id) {
161     case PROP_TIMELINE:
162       g_value_set_object (value, self->timeline);
163       break;
164     default:
165       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
166   }
167 }
168
169 static void
170 ges_demux_set_property (GObject * object, guint property_id,
171     const GValue * value, GParamSpec * pspec)
172 {
173   switch (property_id) {
174     default:
175       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
176   }
177 }
178
179 static void
180 ges_demux_dispose (GObject * object)
181 {
182   GESDemux *self = GES_DEMUX (object);
183
184   if (self->timeline)
185     gst_clear_object (&self->timeline);
186 }
187
188 static void
189 ges_demux_class_init (GESDemuxClass * self_class)
190 {
191   GObjectClass *gclass = G_OBJECT_CLASS (self_class);
192   GstElementClass *gstelement_klass = GST_ELEMENT_CLASS (self_class);
193
194   GST_DEBUG_CATEGORY_INIT (gesdemux, "gesdemux", 0, "ges demux element");
195
196   gclass->get_property = ges_demux_get_property;
197   gclass->set_property = ges_demux_set_property;
198   gclass->dispose = ges_demux_dispose;
199
200   /**
201    * GESDemux:timeline:
202    *
203    * Timeline to use in this source.
204    */
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);
208
209   g_object_class_install_properties (gclass, PROP_LAST, properties);
210
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");
216
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));
223 }
224
225 typedef struct
226 {
227   GESTimeline *timeline;
228   gchar *uri;
229   GMainLoop *ml;
230   GError *error;
231   GMutex lock;
232   GCond cond;
233   gulong loaded_sigid;
234   gulong error_sigid;
235 } TimelineConstructionData;
236
237 static void
238 project_loaded_cb (GESProject * project, GESTimeline * timeline,
239     TimelineConstructionData * data)
240 {
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);
246
247   g_main_loop_quit (data->ml);
248 }
249
250 static void
251 error_loading_asset_cb (GESProject * project, GError * error, gchar * id,
252     GType extractable_type, TimelineConstructionData * data)
253 {
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);
259
260   g_main_loop_quit (data->ml);
261 }
262
263 /* TODO: Add a way to run a function in the right GES thread */
264 static gboolean
265 ges_timeline_new_from_uri_from_main_thread (TimelineConstructionData * data)
266 {
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;
272
273   g_object_get (previous_discoverer, "timeout", &timeout, NULL);
274
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);
280   if (data->error) {
281     klass->discoverer = previous_discoverer;
282     g_mutex_unlock (&data->lock);
283
284     goto done;
285   }
286   g_signal_connect (klass->discoverer, "discovered",
287       G_CALLBACK (klass->discovered), NULL);
288   gst_discoverer_start (klass->discoverer);
289
290   data->ml = g_main_loop_new (NULL, TRUE);
291   data->loaded_sigid =
292       g_signal_connect (project, "loaded", G_CALLBACK (project_loaded_cb),
293       data);
294   data->error_sigid =
295       g_signal_connect (project, "error-loading-asset",
296       G_CALLBACK (error_loading_asset_cb), data);
297
298   unused = GES_TIMELINE (ges_asset_extract (GES_ASSET (project), &data->error));
299   if (data->error) {
300     g_mutex_unlock (&data->lock);
301
302     goto done;
303   }
304   g_mutex_unlock (&data->lock);
305
306   g_main_loop_run (data->ml);
307   g_main_loop_unref (data->ml);
308
309 done:
310
311   g_mutex_lock (&data->lock);
312
313   /* Set previous discoverer back! */
314
315   if (klass->discoverer)
316     gst_object_unref (klass->discoverer);
317   klass->discoverer = previous_discoverer;
318
319   if (data->timeline)
320     ges_timeline_commit (data->timeline);
321
322   if (data->loaded_sigid)
323     g_signal_handler_disconnect (project, data->loaded_sigid);
324
325   if (data->error_sigid)
326     g_signal_handler_disconnect (project, data->error_sigid);
327
328   gst_clear_object (&project);
329
330   g_cond_broadcast (&data->cond);
331   g_mutex_unlock (&data->lock);
332
333   return G_SOURCE_REMOVE;
334 }
335
336 static gboolean
337 ges_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
338 {
339   GESDemux *self = GES_DEMUX (parent);
340
341   switch (event->type) {
342     case GST_EVENT_EOS:{
343       GstMapInfo map;
344       GstBuffer *xges_buffer;
345       gboolean ret = TRUE;
346       gsize available;
347
348       available = gst_adapter_available (self->input_adapter);
349       if (available == 0) {
350         GST_WARNING_OBJECT (self,
351             "Received EOS without any serialized timeline.");
352
353         return gst_pad_event_default (pad, parent, event);
354       }
355
356       xges_buffer = gst_adapter_take_buffer (self->input_adapter, available);
357       if (gst_buffer_map (xges_buffer, &map, GST_MAP_READ)) {
358         GError *err = NULL;
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 ();
363
364         GST_ERROR ("Loading %s", filename);
365         if (err) {
366           GST_ELEMENT_ERROR (self, RESOURCE, OPEN_WRITE,
367               ("Could not open temporary file to write timeline description"),
368               ("%s", err->message));
369
370           goto error;
371         }
372
373         g_file_set_contents (filename, (gchar *) map.data, map.size, &err);
374         if (err) {
375           GST_ELEMENT_ERROR (self, RESOURCE, WRITE,
376               ("Could not write temporary timeline description file"),
377               ("%s", err->message));
378
379           goto error;
380         }
381
382         uri = gst_uri_construct ("file", filename);
383         data.uri = uri;
384
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);
393
394         if (data.error) {
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);
399
400           goto error;
401         }
402
403         GST_INFO_OBJECT (self, "Timeline properly loaded: %" GST_PTR_FORMAT,
404             data.timeline);
405         ges_demux_set_timeline (self, data.timeline);
406       done:
407         g_free (filename);
408         g_free (uri);
409         g_close (f, NULL);
410         return ret;
411       error:
412         ret = FALSE;
413         goto done;
414       } else {
415         GST_ELEMENT_ERROR (self, RESOURCE, READ,
416             ("Could not map buffer containing timeline description"),
417             ("Not info"));
418       }
419       GST_ERROR_OBJECT (xges_buffer, ":YAY");
420     }
421     default:
422       break;
423   }
424
425   return gst_pad_event_default (pad, parent, event);
426 }
427
428 static GstFlowReturn
429 ges_demux_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
430 {
431   GESDemux *self = GES_DEMUX (parent);
432
433   gst_adapter_push (self->input_adapter, buffer);
434
435   GST_INFO_OBJECT (self, "Received buffer, total size is %i bytes",
436       (gint) gst_adapter_available (self->input_adapter));
437
438   return GST_FLOW_OK;
439 }
440
441 static void
442 ges_demux_init (GESDemux * self)
443 {
444   ges_init ();
445   self->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
446   gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
447
448   self->input_adapter = gst_adapter_new ();
449
450   gst_pad_set_chain_function (self->sinkpad,
451       GST_DEBUG_FUNCPTR (ges_demux_sink_chain));
452
453   gst_pad_set_event_function (self->sinkpad,
454       GST_DEBUG_FUNCPTR (ges_demux_sink_event));
455 }