marker-list: Add flags (de)serialization
[platform/upstream/gstreamer.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  * A #GESMarker can be colored by setting the #GES_META_MARKER_COLOR meta.
28  *
29  * Since: 1.18
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include "ges-marker-list.h"
37 #include "ges.h"
38 #include "ges-internal.h"
39 #include "ges-meta-container.h"
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_MARKER_0,
57   PROP_MARKER_POSITION,
58   PROP_MARKER_LAST
59 };
60
61 static GParamSpec *marker_properties[PROP_MARKER_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_MARKER_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   ges_meta_container_register_static_meta (GES_META_CONTAINER (self),
84       GES_META_READ_WRITE, GES_META_MARKER_COLOR, G_TYPE_UINT);
85 }
86
87 static void
88 ges_marker_class_init (GESMarkerClass * klass)
89 {
90   GObjectClass *object_class = G_OBJECT_CLASS (klass);
91
92   object_class->get_property = ges_marker_get_property;
93   /**
94    * GESMarker:position:
95    *
96    * Current position (in nanoseconds) of the #GESMarker
97    *
98    * Since: 1.18
99    */
100   marker_properties[PROP_MARKER_POSITION] =
101       g_param_spec_uint64 ("position", "Position",
102       "The position of the marker", 0, G_MAXUINT64,
103       GST_CLOCK_TIME_NONE, G_PARAM_READABLE);
104   g_object_class_install_property (object_class, PROP_MARKER_POSITION,
105       marker_properties[PROP_MARKER_POSITION]);
106
107 }
108
109 static void
110 ges_meta_container_interface_init (GESMetaContainerInterface * iface)
111 {
112 }
113
114 /* GESMarkerList */
115
116 struct _GESMarkerList
117 {
118   GObject parent;
119
120   GSequence *markers;
121   GHashTable *markers_iters;
122   GESMarkerFlags flags;
123 };
124
125 enum
126 {
127   PROP_MARKER_LIST_0,
128   PROP_MARKER_LIST_FLAGS,
129   PROP_MARKER_LIST_LAST
130 };
131
132 static GParamSpec *list_properties[PROP_MARKER_LIST_LAST];
133
134 enum
135 {
136   MARKER_ADDED,
137   MARKER_REMOVED,
138   MARKER_MOVED,
139   LAST_SIGNAL
140 };
141
142 static guint ges_marker_list_signals[LAST_SIGNAL] = { 0 };
143
144 G_DEFINE_TYPE (GESMarkerList, ges_marker_list, G_TYPE_OBJECT);
145
146 static void
147 ges_marker_list_get_property (GObject * object, guint property_id,
148     GValue * value, GParamSpec * pspec)
149 {
150   GESMarkerList *self = GES_MARKER_LIST (object);
151
152   switch (property_id) {
153     case PROP_MARKER_LIST_FLAGS:
154       g_value_set_flags (value, self->flags);
155       break;
156     default:
157       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
158       break;
159   }
160 }
161
162 static void
163 ges_marker_list_set_property (GObject * object,
164     guint property_id, const GValue * value, GParamSpec * pspec)
165 {
166   GESMarkerList *self = GES_MARKER_LIST (object);
167
168   switch (property_id) {
169     case PROP_MARKER_LIST_FLAGS:
170       self->flags = g_value_get_flags (value);
171       break;
172     default:
173       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
174   }
175 }
176
177 static void
178 remove_marker (gpointer data)
179 {
180   GESMarker *marker = (GESMarker *) data;
181
182   g_object_unref (marker);
183 }
184
185 static void
186 ges_marker_list_init (GESMarkerList * self)
187 {
188   self->markers = g_sequence_new (remove_marker);
189   self->markers_iters = g_hash_table_new (g_direct_hash, g_direct_equal);
190 }
191
192 static void
193 ges_marker_list_finalize (GObject * object)
194 {
195   GESMarkerList *self = GES_MARKER_LIST (object);
196
197   g_sequence_free (self->markers);
198   g_hash_table_unref (self->markers_iters);
199
200   G_OBJECT_CLASS (ges_marker_list_parent_class)->finalize (object);
201 }
202
203 static void
204 ges_marker_list_class_init (GESMarkerListClass * klass)
205 {
206   GObjectClass *object_class = G_OBJECT_CLASS (klass);
207
208   object_class->finalize = ges_marker_list_finalize;
209   object_class->get_property = ges_marker_list_get_property;
210   object_class->set_property = ges_marker_list_set_property;
211
212 /**
213   * GESMarkerList:flags:
214   *
215   * Flags indicating how markers on the list should be treated.
216   *
217   * Since: 1.20
218   */
219   list_properties[PROP_MARKER_LIST_FLAGS] =
220       g_param_spec_flags ("flags", "Flags",
221       "Functionalities the marker list should be used for",
222       GES_TYPE_MARKER_FLAGS, GES_MARKER_FLAG_NONE,
223       G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
224   g_object_class_install_property (object_class, PROP_MARKER_LIST_FLAGS,
225       list_properties[PROP_MARKER_LIST_FLAGS]);
226
227 /**
228   * GESMarkerList::marker-added:
229   * @marker-list: the #GESMarkerList
230   * @position: the position of the added marker
231   * @marker: the #GESMarker that was added.
232   *
233   * Will be emitted after the marker was added to the marker-list.
234   * Since: 1.18
235   */
236   ges_marker_list_signals[MARKER_ADDED] =
237       g_signal_new ("marker-added", G_TYPE_FROM_CLASS (klass),
238       G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL,
239       G_TYPE_NONE, 2, G_TYPE_UINT64, GES_TYPE_MARKER);
240
241 /**
242   * GESMarkerList::marker-removed:
243   * @marker_list: the #GESMarkerList
244   * @marker: the #GESMarker that was removed.
245   *
246   * Will be emitted after the marker was removed the marker-list.
247   * Since: 1.18
248   */
249   ges_marker_list_signals[MARKER_REMOVED] =
250       g_signal_new ("marker-removed", G_TYPE_FROM_CLASS (klass),
251       G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, GES_TYPE_MARKER);
252
253 /**
254   * GESMarkerList::marker-moved:
255   * @marker_list: the #GESMarkerList
256   * @previous_position: the previous position of the marker
257   * @new_position: the new position of the marker
258   * @marker: the #GESMarker that was moved.
259   *
260   * Will be emitted after the marker was moved to.
261   * Since: 1.18
262   */
263   ges_marker_list_signals[MARKER_MOVED] =
264       g_signal_new ("marker-moved", G_TYPE_FROM_CLASS (klass),
265       G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL,
266       G_TYPE_NONE, 3, G_TYPE_UINT64, G_TYPE_UINT64, GES_TYPE_MARKER);
267 }
268
269 static gint
270 cmp_marker (gconstpointer a, gconstpointer b, G_GNUC_UNUSED gpointer data)
271 {
272   GESMarker *marker_a = (GESMarker *) a;
273   GESMarker *marker_b = (GESMarker *) b;
274
275   if (marker_a->position < marker_b->position)
276     return -1;
277   else if (marker_a->position == marker_b->position)
278     return 0;
279   else
280     return 1;
281 }
282
283 /**
284  * ges_marker_list_new:
285  *
286  * Creates a new #GESMarkerList.
287
288  * Returns: A new #GESMarkerList
289  * Since: 1.18
290  */
291 GESMarkerList *
292 ges_marker_list_new (void)
293 {
294   GESMarkerList *ret;
295
296   ret = (GESMarkerList *) g_object_new (GES_TYPE_MARKER_LIST, NULL);
297
298   return ret;
299 }
300
301 /**
302  * ges_marker_list_add:
303  * @position: The position of the new marker
304  *
305  * Returns: (transfer none): The newly-added marker, the list keeps ownership
306  * of the marker
307  * Since: 1.18
308  */
309 GESMarker *
310 ges_marker_list_add (GESMarkerList * self, GstClockTime position)
311 {
312   GESMarker *ret;
313   GSequenceIter *iter;
314
315   g_return_val_if_fail (GES_IS_MARKER_LIST (self), NULL);
316
317   ret = (GESMarker *) g_object_new (GES_TYPE_MARKER, NULL);
318
319   ret->position = position;
320
321   iter = g_sequence_insert_sorted (self->markers, ret, cmp_marker, NULL);
322
323   g_hash_table_insert (self->markers_iters, ret, iter);
324
325   g_signal_emit (self, ges_marker_list_signals[MARKER_ADDED], 0, position, ret);
326
327   return ret;
328 }
329
330 /**
331  * ges_marker_list_size:
332  *
333  * Returns: The number of markers in @list
334  * Since: 1.18
335  */
336 guint
337 ges_marker_list_size (GESMarkerList * self)
338 {
339   g_return_val_if_fail (GES_IS_MARKER_LIST (self), 0);
340
341   return g_sequence_get_length (self->markers);
342 }
343
344 /**
345  * ges_marker_list_remove:
346  *
347  * Removes @marker from @list, this decreases the refcount of the
348  * marker by 1.
349  *
350  * Returns: %TRUE if the marker could be removed, %FALSE otherwise
351  *   (if the marker was not present in the list for example)
352  * Since: 1.18
353  */
354 gboolean
355 ges_marker_list_remove (GESMarkerList * self, GESMarker * marker)
356 {
357   GSequenceIter *iter;
358   gboolean ret = FALSE;
359
360   g_return_val_if_fail (GES_IS_MARKER_LIST (self), FALSE);
361
362   if (!g_hash_table_lookup_extended (self->markers_iters,
363           marker, NULL, (gpointer *) & iter))
364     goto done;
365   g_assert (iter != NULL);
366   g_hash_table_remove (self->markers_iters, marker);
367
368   g_signal_emit (self, ges_marker_list_signals[MARKER_REMOVED], 0, marker);
369
370   g_sequence_remove (iter);
371
372   ret = TRUE;
373
374 done:
375   return ret;
376 }
377
378 /**
379  * ges_marker_list_get_markers:
380  *
381  * Returns: (element-type GESMarker) (transfer full): a #GList
382  * of the #GESMarker within the GESMarkerList. The user will have
383  * to unref each #GESMarker and free the #GList.
384  *
385  * Since: 1.18
386  */
387 GList *
388 ges_marker_list_get_markers (GESMarkerList * self)
389 {
390   GESMarker *marker;
391   GSequenceIter *iter;
392   GList *ret;
393
394   g_return_val_if_fail (GES_IS_MARKER_LIST (self), NULL);
395   ret = NULL;
396
397   for (iter = g_sequence_get_begin_iter (self->markers);
398       !g_sequence_iter_is_end (iter); iter = g_sequence_iter_next (iter)) {
399     marker = GES_MARKER (g_sequence_get (iter));
400
401     ret = g_list_append (ret, g_object_ref (marker));
402   }
403
404   return ret;
405 }
406
407 /*
408  * ges_marker_list_get_closest:
409  * @position: The position which we want to find the closest marker to
410  *
411  * Returns: (transfer full): The marker found to be the closest
412  * to the given position. If two markers are at equal distance from position,
413  * the "earlier" one will be returned.
414  */
415 GESMarker *
416 ges_marker_list_get_closest (GESMarkerList * self, GstClockTime position)
417 {
418   GESMarker *new_marker, *ret = NULL;
419   GstClockTime distance_next, distance_prev;
420   GSequenceIter *iter;
421
422   if (g_sequence_is_empty (self->markers))
423     goto done;
424
425   new_marker = (GESMarker *) g_object_new (GES_TYPE_MARKER, NULL);
426   new_marker->position = position;
427   iter = g_sequence_search (self->markers, new_marker, cmp_marker, NULL);
428   g_object_unref (new_marker);
429
430   if (g_sequence_iter_is_begin (iter)) {
431     /* We know the sequence isn't empty, this is safe */
432     ret = g_sequence_get (iter);
433   } else if (g_sequence_iter_is_end (iter)) {
434     /* We know the sequence isn't empty, this is safe */
435     ret = g_sequence_get (g_sequence_iter_prev (iter));
436   } else {
437     GESMarker *next_marker, *prev_marker;
438
439     prev_marker = g_sequence_get (g_sequence_iter_prev (iter));
440     next_marker = g_sequence_get (iter);
441
442     distance_next = next_marker->position - position;
443     distance_prev = position - prev_marker->position;
444
445     ret = distance_prev <= distance_next ? prev_marker : next_marker;
446   }
447
448 done:
449   if (ret)
450     return g_object_ref (ret);
451   return NULL;
452 }
453
454 /**
455  * ges_marker_list_move:
456  *
457  * Moves a @marker in a @list to a new @position
458  *
459  * Returns: %TRUE if the marker could be moved, %FALSE otherwise
460  *   (if the marker was not present in the list for example)
461  *
462  * Since: 1.18
463  */
464 gboolean
465 ges_marker_list_move (GESMarkerList * self, GESMarker * marker,
466     GstClockTime position)
467 {
468   GSequenceIter *iter;
469   gboolean ret = FALSE;
470   GstClockTime previous_position;
471
472   g_return_val_if_fail (GES_IS_MARKER_LIST (self), FALSE);
473
474   if (!g_hash_table_lookup_extended (self->markers_iters,
475           marker, NULL, (gpointer *) & iter)) {
476     GST_WARNING ("GESMarkerList doesn't contain GESMarker");
477     goto done;
478   }
479
480   previous_position = marker->position;
481   marker->position = position;
482
483   g_signal_emit (self, ges_marker_list_signals[MARKER_MOVED], 0,
484       previous_position, position, marker);
485
486   g_sequence_sort_changed (iter, cmp_marker, NULL);
487
488   ret = TRUE;
489
490 done:
491   return ret;
492 }
493
494 gboolean
495 ges_marker_list_deserialize (GValue * dest, const gchar * s)
496 {
497   gboolean ret = FALSE;
498   GstCaps *caps = NULL;
499   GESMarkerList *list = ges_marker_list_new ();
500   guint caps_len, i = 0;
501   gsize string_len;
502   gchar *escaped, *caps_str;
503   GstStructure *data_s;
504   gint flags;
505
506   string_len = strlen (s);
507   if (G_UNLIKELY (*s != '"' || string_len < 2 || s[string_len - 1] != '"')) {
508     /* "\"" is not an accepted string, so len must be at least 2 */
509     GST_ERROR ("Failed deserializing marker list: expected string to start "
510         "and end with '\"'");
511     goto done;
512   }
513   escaped = g_strdup (s + 1);
514   escaped[string_len - 2] = '\0';
515   /* removed trailing '"' */
516   caps_str = g_strcompress (escaped);
517   g_free (escaped);
518
519   caps = gst_caps_from_string (caps_str);
520   g_free (caps_str);
521   if (G_UNLIKELY (caps == NULL)) {
522     GST_ERROR ("Failed deserializing marker list: could not extract caps");
523     goto done;
524   }
525
526   caps_len = gst_caps_get_size (caps);
527   if (G_UNLIKELY (caps_len == 0)) {
528     GST_DEBUG ("Got empty caps: %s", s);
529     goto done;
530   }
531
532   data_s = gst_caps_get_structure (caps, i);
533   if (gst_structure_has_name (data_s, "marker-list-flags")) {
534     if (!gst_structure_get_int (data_s, "flags", &flags)) {
535       GST_ERROR_OBJECT (dest,
536           "Failed deserializing marker list: unexpected structure %"
537           GST_PTR_FORMAT, data_s);
538       goto done;
539     }
540
541     list->flags = flags;
542     i += 1;
543   }
544
545   if (G_UNLIKELY ((caps_len - i) % 2)) {
546     GST_ERROR ("Failed deserializing marker list: incomplete marker caps");
547   }
548
549   for (; i < caps_len - 1; i += 2) {
550     const GstStructure *pos_s = gst_caps_get_structure (caps, i);
551     const GstStructure *meta_s = gst_caps_get_structure (caps, i + 1);
552     GstClockTime position;
553     GESMarker *marker;
554     gchar *metas;
555
556     if (!gst_structure_has_name (pos_s, "marker-times")) {
557       GST_ERROR_OBJECT (dest,
558           "Failed deserializing marker list: unexpected structure %"
559           GST_PTR_FORMAT, pos_s);
560       goto done;
561     }
562
563     if (!gst_structure_get_uint64 (pos_s, "position", &position)) {
564       GST_ERROR_OBJECT (dest,
565           "Failed deserializing marker list: unexpected structure %"
566           GST_PTR_FORMAT, pos_s);
567       goto done;
568     }
569
570     marker = ges_marker_list_add (list, position);
571
572     metas = gst_structure_to_string (meta_s);
573     ges_meta_container_add_metas_from_string (GES_META_CONTAINER (marker),
574         metas);
575     g_free (metas);
576   }
577
578   ret = TRUE;
579
580 done:
581   if (caps)
582     gst_caps_unref (caps);
583
584   if (!ret)
585     g_object_unref (list);
586   else
587     g_value_take_object (dest, list);
588
589   return ret;
590 }
591
592 gchar *
593 ges_marker_list_serialize (const GValue * v)
594 {
595   GESMarkerList *list = GES_MARKER_LIST (g_value_get_object (v));
596   GSequenceIter *iter;
597   GstCaps *caps = gst_caps_new_empty ();
598   gchar *caps_str, *escaped, *res;
599   GstStructure *s;
600
601   s = gst_structure_new ("marker-list-flags", "flags", G_TYPE_INT,
602       list->flags, NULL);
603   gst_caps_append_structure (caps, s);
604
605   iter = g_sequence_get_begin_iter (list->markers);
606
607   while (!g_sequence_iter_is_end (iter)) {
608     GESMarker *marker = (GESMarker *) g_sequence_get (iter);
609     gchar *metas;
610
611     metas = ges_meta_container_metas_to_string (GES_META_CONTAINER (marker));
612
613     s = gst_structure_new ("marker-times", "position", G_TYPE_UINT64,
614         marker->position, NULL);
615     gst_caps_append_structure (caps, s);
616     s = gst_structure_from_string (metas, NULL);
617     gst_caps_append_structure (caps, s);
618
619     g_free (metas);
620
621     iter = g_sequence_iter_next (iter);
622   }
623
624   caps_str = gst_caps_to_string (caps);
625   escaped = g_strescape (caps_str, NULL);
626   g_free (caps_str);
627   res = g_strdup_printf ("\"%s\"", escaped);
628   g_free (escaped);
629   gst_caps_unref (caps);
630
631   return res;
632 }