markerlist: implement GESMarkerList
[platform/upstream/gst-editing-services.git] / ges / ges-marker-list.c
1 /* GStreamer Editing Services
2
3  * Copyright (C) <2019> Mathieu Duponchelle <mathieu@centricular.com>
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: gesmarkerlist
23  * @title: GESMarkerList
24  * @short_description: implements a list of markers with metadata asociated to time positions
25  * @see_also: #GESMarker
26  *
27  * Since: 1.18
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "ges-marker-list.h"
35 #include "ges.h"
36 #include "ges-internal.h"
37 #include "ges-meta-container.h"
38
39 /* GESMarker */
40
41 static void ges_meta_container_interface_init (GESMetaContainerInterface *
42     iface);
43
44 struct _GESMarker
45 {
46   GObject parent;
47   GstClockTime position;
48 };
49
50 G_DEFINE_TYPE_WITH_CODE (GESMarker, ges_marker, G_TYPE_OBJECT,
51     G_IMPLEMENT_INTERFACE (GES_TYPE_META_CONTAINER,
52         ges_meta_container_interface_init));
53
54 enum
55 {
56   PROP_0,
57   PROP_POSITION,
58   PROP_LAST
59 };
60
61 static GParamSpec *properties[PROP_LAST];
62
63 /* GObject Standard vmethods*/
64 static void
65 ges_marker_get_property (GObject * object, guint property_id,
66     GValue * value, GParamSpec * pspec)
67 {
68   GESMarker *marker = GES_MARKER (object);
69
70   switch (property_id) {
71     case PROP_POSITION:
72       g_value_set_uint64 (value, marker->position);
73       break;
74     default:
75       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
76       break;
77   }
78 }
79
80 static void
81 ges_marker_init (GESMarker * self)
82 {
83 }
84
85 static void
86 ges_marker_class_init (GESMarkerClass * klass)
87 {
88   GObjectClass *object_class = G_OBJECT_CLASS (klass);
89
90   object_class->get_property = ges_marker_get_property;
91     /**
92    * GESMarker:position:
93    *
94    * Current position (in nanoseconds) of the #GESMarker
95    */
96   properties[PROP_POSITION] =
97       g_param_spec_uint64 ("position", "Position",
98       "The position of the marker", 0, G_MAXUINT64,
99       GST_CLOCK_TIME_NONE, G_PARAM_READABLE);
100   g_object_class_install_property (object_class, PROP_POSITION,
101       properties[PROP_POSITION]);
102
103 }
104
105 static void
106 ges_meta_container_interface_init (GESMetaContainerInterface * iface)
107 {
108 }
109
110 /* GESMarkerList */
111
112 struct _GESMarkerList
113 {
114   GObject parent;
115
116   GSequence *markers;
117   GHashTable *markers_iters;
118 };
119
120 enum
121 {
122   MARKER_ADDED,
123   MARKER_REMOVED,
124   MARKER_MOVED,
125   LAST_SIGNAL
126 };
127
128 static guint ges_marker_list_signals[LAST_SIGNAL] = { 0 };
129
130 G_DEFINE_TYPE (GESMarkerList, ges_marker_list, G_TYPE_OBJECT);
131
132 static void
133 remove_marker (gpointer data)
134 {
135   GESMarker *marker = (GESMarker *) data;
136
137   g_object_unref (marker);
138 }
139
140 static void
141 ges_marker_list_init (GESMarkerList * self)
142 {
143   self->markers = g_sequence_new (remove_marker);
144   self->markers_iters = g_hash_table_new (g_direct_hash, g_direct_equal);
145 }
146
147 static void
148 ges_marker_list_finalize (GObject * object)
149 {
150   GESMarkerList *self = GES_MARKER_LIST (object);
151
152   g_sequence_free (self->markers);
153   g_hash_table_unref (self->markers_iters);
154
155   G_OBJECT_CLASS (ges_marker_list_parent_class)->finalize (object);
156 }
157
158 static void
159 ges_marker_list_class_init (GESMarkerListClass * klass)
160 {
161   GObjectClass *object_class = G_OBJECT_CLASS (klass);
162
163   object_class->finalize = ges_marker_list_finalize;
164
165   /**
166   * GESMarkerList::marker-added:
167   * @marker-list: the #GESMarkerList
168   * @marker: the #GESMarker that was added.
169   *
170   * Will be emitted after the marker was added to the marker-list.
171   * Since: 1.18
172   */
173   ges_marker_list_signals[MARKER_ADDED] =
174       g_signal_new ("marker-added", G_TYPE_FROM_CLASS (klass),
175       G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_generic,
176       G_TYPE_NONE, 2, G_TYPE_UINT64, GES_TYPE_MARKER);
177
178 /**
179   * GESMarkerList::marker-removed:
180   * @marker-list: the #GESMarkerList
181   * @marker: the #GESMarker that was removed.
182   *
183   * Will be emitted after the marker was removed the marker-list.
184   * Since: 1.18
185   */
186   ges_marker_list_signals[MARKER_REMOVED] =
187       g_signal_new ("marker-removed", G_TYPE_FROM_CLASS (klass),
188       G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_generic,
189       G_TYPE_NONE, 1, GES_TYPE_MARKER);
190
191 /**
192   * GESMarkerList::marker-moved:
193   * @marker-list: the #GESMarkerList
194   * @marker: the #GESMarker that was added.
195   *
196   * Will be emitted after the marker was moved to.
197   * Since: 1.18
198   */
199   ges_marker_list_signals[MARKER_MOVED] =
200       g_signal_new ("marker-moved", G_TYPE_FROM_CLASS (klass),
201       G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_generic,
202       G_TYPE_NONE, 2, G_TYPE_UINT64, GES_TYPE_MARKER);
203 }
204
205 static gint
206 cmp_marker (gconstpointer a, gconstpointer b, G_GNUC_UNUSED gpointer data)
207 {
208   GESMarker *marker_a = (GESMarker *) a;
209   GESMarker *marker_b = (GESMarker *) b;
210
211   if (marker_a->position < marker_b->position)
212     return -1;
213   else if (marker_a->position == marker_b->position)
214     return 0;
215   else
216     return 1;
217 }
218
219 /**
220  * ges_marker_list_new:
221  *
222  * Creates a new #GESMarkerList.
223
224  * Returns: A new #GESMarkerList
225  * Since: 1.18
226  */
227 GESMarkerList *
228 ges_marker_list_new (void)
229 {
230   GESMarkerList *ret;
231
232   ret = (GESMarkerList *) g_object_new (GES_TYPE_MARKER_LIST, NULL);
233
234   return ret;
235 }
236
237 /**
238  * ges_marker_list_add:
239  * @position: The position of the new marker
240  *
241  * Returns: (transfer none): The newly-added marker, the list keeps ownership
242  * of the marker
243  * Since: 1.18
244  */
245 GESMarker *
246 ges_marker_list_add (GESMarkerList * self, GstClockTime position)
247 {
248   GESMarker *ret;
249   GSequenceIter *iter;
250
251   g_return_val_if_fail (GES_IS_MARKER_LIST (self), NULL);
252
253   ret = (GESMarker *) g_object_new (GES_TYPE_MARKER, NULL);
254
255   ret->position = position;
256
257   iter = g_sequence_insert_sorted (self->markers, ret, cmp_marker, NULL);
258
259   g_hash_table_insert (self->markers_iters, ret, iter);
260
261   g_signal_emit (self, ges_marker_list_signals[MARKER_ADDED], 0, position, ret);
262
263   return ret;
264 }
265
266 /**
267  * ges_marker_list_size:
268  *
269  * Returns: The number of markers in @list
270  * Since: 1.18
271  */
272 guint
273 ges_marker_list_size (GESMarkerList * self)
274 {
275   g_return_val_if_fail (GES_IS_MARKER_LIST (self), 0);
276
277   return g_sequence_get_length (self->markers);
278 }
279
280 /**
281  * ges_marker_list_remove:
282  *
283  * Removes @marker from @list, this decreases the refcount of the
284  * marker by 1.
285  *
286  * Returns: %TRUE if the marker could be removed, %FALSE otherwise
287  *   (if the marker was not present in the list for example)
288  * Since: 1.18
289  */
290 gboolean
291 ges_marker_list_remove (GESMarkerList * self, GESMarker * marker)
292 {
293   GSequenceIter *iter;
294   gboolean ret = FALSE;
295
296   g_return_val_if_fail (GES_IS_MARKER_LIST (self), FALSE);
297
298   if (!g_hash_table_lookup_extended (self->markers_iters,
299           marker, NULL, (gpointer *) & iter))
300     goto done;
301   g_assert (iter != NULL);
302   g_hash_table_remove (self->markers_iters, marker);
303
304   g_signal_emit (self, ges_marker_list_signals[MARKER_REMOVED], 0, marker);
305
306   g_sequence_remove (iter);
307
308   ret = TRUE;
309
310 done:
311   return ret;
312 }
313
314
315 /**
316  * ges_marker_list_get_markers:
317  *
318  * Returns: (element-type GESMarker) (transfer full): a #GList 
319  * of the #GESMarker within the GESMarkerList. The user will have
320  * to unref each #GESMarker and free the #GList.
321  *
322  * Since: 1.18
323  */
324 GList *
325 ges_marker_list_get_markers (GESMarkerList * self)
326 {
327   GESMarker *marker;
328   GSequenceIter *iter;
329   GList *ret;
330
331   g_return_val_if_fail (GES_IS_MARKER_LIST (self), NULL);
332   ret = NULL;
333
334   for (iter = g_sequence_get_begin_iter (self->markers);
335       !g_sequence_iter_is_end (iter); iter = g_sequence_iter_next (iter)) {
336     marker = GES_MARKER (g_sequence_get (iter));
337
338     ret = g_list_append (ret, g_object_ref (marker));
339   }
340
341   return ret;
342 }
343
344
345 /**
346  * ges_marker_list_move:
347  * 
348  * Moves a @marker in a @list to a new @position
349  *
350  * Returns: %TRUE if the marker could be moved, %FALSE otherwise
351  *   (if the marker was not present in the list for example)
352  *
353  * Since: 1.18
354  */
355 gboolean
356 ges_marker_list_move (GESMarkerList * self, GESMarker * marker,
357     GstClockTime position)
358 {
359   GSequenceIter *iter;
360   gboolean ret = FALSE;
361
362   g_return_val_if_fail (GES_IS_MARKER_LIST (self), FALSE);
363
364   if (!g_hash_table_lookup_extended (self->markers_iters,
365           marker, NULL, (gpointer *) & iter)) {
366     GST_WARNING ("GESMarkerList doesn't contain GESMarker");
367     goto done;
368   }
369
370   marker->position = position;
371
372   g_signal_emit (self, ges_marker_list_signals[MARKER_MOVED], 0, position,
373       marker);
374
375   g_sequence_sort_changed (iter, cmp_marker, NULL);
376
377   ret = TRUE;
378
379 done:
380   return ret;
381 }
382
383 gboolean
384 ges_marker_list_deserialize (GValue * dest, const gchar * s)
385 {
386   gboolean ret = FALSE;
387   GstCaps *caps = NULL;
388   GESMarkerList *list = ges_marker_list_new ();
389   guint i, l;
390
391   caps = gst_caps_from_string (s);
392
393   l = gst_caps_get_size (caps);
394
395   if (l % 2) {
396     GST_ERROR ("Failed deserializing marker list: expected evenly-sized caps");
397     goto done;
398   }
399
400   for (i = 0; i < l - 1; i += 2) {
401     const GstStructure *pos_s = gst_caps_get_structure (caps, i);
402     const GstStructure *meta_s = gst_caps_get_structure (caps, i + 1);
403     GstClockTime position;
404     GESMarker *marker;
405     gchar *metas;
406
407     if (!gst_structure_has_name (pos_s, "marker-times")) {
408       GST_ERROR_OBJECT (dest,
409           "Failed deserializing marker list: unexpected structure %"
410           GST_PTR_FORMAT, pos_s);
411       goto done;
412     }
413
414     if (!gst_structure_get_uint64 (pos_s, "position", &position)) {
415       GST_ERROR_OBJECT (dest,
416           "Failed deserializing marker list: unexpected structure %"
417           GST_PTR_FORMAT, pos_s);
418       goto done;
419     }
420
421     marker = ges_marker_list_add (list, position);
422
423     metas = gst_structure_to_string (meta_s);
424     ges_meta_container_add_metas_from_string (GES_META_CONTAINER (marker),
425         metas);
426     g_free (metas);
427
428   }
429
430   ret = TRUE;
431
432 done:
433   if (caps)
434     gst_caps_unref (caps);
435
436   if (!ret)
437     g_object_unref (list);
438   else
439     g_value_take_object (dest, list);
440
441   return ret;
442 }
443
444 gchar *
445 ges_marker_list_serialize (const GValue * v)
446 {
447   GESMarkerList *list = GES_MARKER_LIST (g_value_get_object (v));
448   GSequenceIter *iter;
449   GstCaps *caps = gst_caps_new_empty ();
450   gchar *caps_str, *escaped, *res;
451
452   iter = g_sequence_get_begin_iter (list->markers);
453
454   while (!g_sequence_iter_is_end (iter)) {
455     GESMarker *marker = (GESMarker *) g_sequence_get (iter);
456     GstStructure *s;
457     gchar *metas;
458
459     metas = ges_meta_container_metas_to_string (GES_META_CONTAINER (marker));
460
461     s = gst_structure_new ("marker-times", "position", G_TYPE_UINT64,
462         marker->position, NULL);
463     gst_caps_append_structure (caps, s);
464     s = gst_structure_from_string (metas, NULL);
465     gst_caps_append_structure (caps, s);
466
467     g_free (metas);
468
469     iter = g_sequence_iter_next (iter);
470   }
471
472   caps_str = gst_caps_to_string (caps);
473   escaped = g_strescape (caps_str, NULL);
474   g_free (caps_str);
475   res = g_strdup_printf ("\"%s\"", escaped);
476   g_free (escaped);
477   gst_caps_unref (caps);
478
479   return res;
480 }