1 /* GStreamer Editing Services
3 * Copyright (C) <2019> Mathieu Duponchelle <mathieu@centricular.com>
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.
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.
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.
22 * SECTION: gesmarkerlist
23 * @title: GESMarkerList
24 * @short_description: implements a list of markers with metadata asociated to time positions
25 * @see_also: #GESMarker
27 * A #GESMarker can be colored by setting the #GES_META_MARKER_COLOR meta.
36 #include "ges-marker-list.h"
38 #include "ges-internal.h"
39 #include "ges-meta-container.h"
41 static void ges_meta_container_interface_init (GESMetaContainerInterface *
47 GstClockTime position;
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));
61 static GParamSpec *marker_properties[PROP_MARKER_LAST];
63 /* GObject Standard vmethods*/
65 ges_marker_get_property (GObject * object, guint property_id,
66 GValue * value, GParamSpec * pspec)
68 GESMarker *marker = GES_MARKER (object);
70 switch (property_id) {
71 case PROP_MARKER_POSITION:
72 g_value_set_uint64 (value, marker->position);
75 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
81 ges_marker_init (GESMarker * self)
83 ges_meta_container_register_static_meta (GES_META_CONTAINER (self),
84 GES_META_READ_WRITE, GES_META_MARKER_COLOR, G_TYPE_UINT);
88 ges_marker_class_init (GESMarkerClass * klass)
90 GObjectClass *object_class = G_OBJECT_CLASS (klass);
92 object_class->get_property = ges_marker_get_property;
96 * Current position (in nanoseconds) of the #GESMarker
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]);
110 ges_meta_container_interface_init (GESMetaContainerInterface * iface)
116 struct _GESMarkerList
121 GHashTable *markers_iters;
122 GESMarkerFlags flags;
128 PROP_MARKER_LIST_FLAGS,
129 PROP_MARKER_LIST_LAST
132 static GParamSpec *list_properties[PROP_MARKER_LIST_LAST];
142 static guint ges_marker_list_signals[LAST_SIGNAL] = { 0 };
144 G_DEFINE_TYPE (GESMarkerList, ges_marker_list, G_TYPE_OBJECT);
147 ges_marker_list_get_property (GObject * object, guint property_id,
148 GValue * value, GParamSpec * pspec)
150 GESMarkerList *self = GES_MARKER_LIST (object);
152 switch (property_id) {
153 case PROP_MARKER_LIST_FLAGS:
154 g_value_set_flags (value, self->flags);
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
163 ges_marker_list_set_property (GObject * object,
164 guint property_id, const GValue * value, GParamSpec * pspec)
166 GESMarkerList *self = GES_MARKER_LIST (object);
168 switch (property_id) {
169 case PROP_MARKER_LIST_FLAGS:
170 self->flags = g_value_get_flags (value);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
178 remove_marker (gpointer data)
180 GESMarker *marker = (GESMarker *) data;
182 g_object_unref (marker);
186 ges_marker_list_init (GESMarkerList * self)
188 self->markers = g_sequence_new (remove_marker);
189 self->markers_iters = g_hash_table_new (g_direct_hash, g_direct_equal);
193 ges_marker_list_finalize (GObject * object)
195 GESMarkerList *self = GES_MARKER_LIST (object);
197 g_sequence_free (self->markers);
198 g_hash_table_unref (self->markers_iters);
200 G_OBJECT_CLASS (ges_marker_list_parent_class)->finalize (object);
204 ges_marker_list_class_init (GESMarkerListClass * klass)
206 GObjectClass *object_class = G_OBJECT_CLASS (klass);
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;
213 * GESMarkerList:flags:
215 * Flags indicating how markers on the list should be treated.
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]);
228 * GESMarkerList::marker-added:
229 * @marker-list: the #GESMarkerList
230 * @position: the position of the added marker
231 * @marker: the #GESMarker that was added.
233 * Will be emitted after the marker was added to the marker-list.
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);
242 * GESMarkerList::marker-removed:
243 * @marker_list: the #GESMarkerList
244 * @marker: the #GESMarker that was removed.
246 * Will be emitted after the marker was removed the marker-list.
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);
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.
260 * Will be emitted after the marker was moved to.
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);
270 cmp_marker (gconstpointer a, gconstpointer b, G_GNUC_UNUSED gpointer data)
272 GESMarker *marker_a = (GESMarker *) a;
273 GESMarker *marker_b = (GESMarker *) b;
275 if (marker_a->position < marker_b->position)
277 else if (marker_a->position == marker_b->position)
284 * ges_marker_list_new:
286 * Creates a new #GESMarkerList.
288 * Returns: A new #GESMarkerList
292 ges_marker_list_new (void)
296 ret = (GESMarkerList *) g_object_new (GES_TYPE_MARKER_LIST, NULL);
302 * ges_marker_list_add:
303 * @position: The position of the new marker
305 * Returns: (transfer none): The newly-added marker, the list keeps ownership
310 ges_marker_list_add (GESMarkerList * self, GstClockTime position)
315 g_return_val_if_fail (GES_IS_MARKER_LIST (self), NULL);
317 ret = (GESMarker *) g_object_new (GES_TYPE_MARKER, NULL);
319 ret->position = position;
321 iter = g_sequence_insert_sorted (self->markers, ret, cmp_marker, NULL);
323 g_hash_table_insert (self->markers_iters, ret, iter);
325 g_signal_emit (self, ges_marker_list_signals[MARKER_ADDED], 0, position, ret);
331 * ges_marker_list_size:
333 * Returns: The number of markers in @list
337 ges_marker_list_size (GESMarkerList * self)
339 g_return_val_if_fail (GES_IS_MARKER_LIST (self), 0);
341 return g_sequence_get_length (self->markers);
345 * ges_marker_list_remove:
347 * Removes @marker from @list, this decreases the refcount of the
350 * Returns: %TRUE if the marker could be removed, %FALSE otherwise
351 * (if the marker was not present in the list for example)
355 ges_marker_list_remove (GESMarkerList * self, GESMarker * marker)
358 gboolean ret = FALSE;
360 g_return_val_if_fail (GES_IS_MARKER_LIST (self), FALSE);
362 if (!g_hash_table_lookup_extended (self->markers_iters,
363 marker, NULL, (gpointer *) & iter))
365 g_assert (iter != NULL);
366 g_hash_table_remove (self->markers_iters, marker);
368 g_signal_emit (self, ges_marker_list_signals[MARKER_REMOVED], 0, marker);
370 g_sequence_remove (iter);
379 * ges_marker_list_get_markers:
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.
388 ges_marker_list_get_markers (GESMarkerList * self)
394 g_return_val_if_fail (GES_IS_MARKER_LIST (self), NULL);
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));
401 ret = g_list_append (ret, g_object_ref (marker));
408 * ges_marker_list_get_closest:
409 * @position: The position which we want to find the closest marker to
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.
416 ges_marker_list_get_closest (GESMarkerList * self, GstClockTime position)
418 GESMarker *new_marker, *ret = NULL;
419 GstClockTime distance_next, distance_prev;
422 if (g_sequence_is_empty (self->markers))
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);
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));
437 GESMarker *next_marker, *prev_marker;
439 prev_marker = g_sequence_get (g_sequence_iter_prev (iter));
440 next_marker = g_sequence_get (iter);
442 distance_next = next_marker->position - position;
443 distance_prev = position - prev_marker->position;
445 ret = distance_prev <= distance_next ? prev_marker : next_marker;
450 return g_object_ref (ret);
455 * ges_marker_list_move:
457 * Moves a @marker in a @list to a new @position
459 * Returns: %TRUE if the marker could be moved, %FALSE otherwise
460 * (if the marker was not present in the list for example)
465 ges_marker_list_move (GESMarkerList * self, GESMarker * marker,
466 GstClockTime position)
469 gboolean ret = FALSE;
470 GstClockTime previous_position;
472 g_return_val_if_fail (GES_IS_MARKER_LIST (self), FALSE);
474 if (!g_hash_table_lookup_extended (self->markers_iters,
475 marker, NULL, (gpointer *) & iter)) {
476 GST_WARNING ("GESMarkerList doesn't contain GESMarker");
480 previous_position = marker->position;
481 marker->position = position;
483 g_signal_emit (self, ges_marker_list_signals[MARKER_MOVED], 0,
484 previous_position, position, marker);
486 g_sequence_sort_changed (iter, cmp_marker, NULL);
495 ges_marker_list_deserialize (GValue * dest, const gchar * s)
497 gboolean ret = FALSE;
498 GstCaps *caps = NULL;
499 GESMarkerList *list = ges_marker_list_new ();
500 guint caps_len, i = 0;
502 gchar *escaped, *caps_str;
503 GstStructure *data_s;
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 '\"'");
513 escaped = g_strdup (s + 1);
514 escaped[string_len - 2] = '\0';
515 /* removed trailing '"' */
516 caps_str = g_strcompress (escaped);
519 caps = gst_caps_from_string (caps_str);
521 if (G_UNLIKELY (caps == NULL)) {
522 GST_ERROR ("Failed deserializing marker list: could not extract caps");
526 caps_len = gst_caps_get_size (caps);
527 if (G_UNLIKELY (caps_len == 0)) {
528 GST_DEBUG ("Got empty caps: %s", s);
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);
545 if (G_UNLIKELY ((caps_len - i) % 2)) {
546 GST_ERROR ("Failed deserializing marker list: incomplete marker caps");
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;
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);
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);
570 marker = ges_marker_list_add (list, position);
572 metas = gst_structure_to_string (meta_s);
573 ges_meta_container_add_metas_from_string (GES_META_CONTAINER (marker),
582 gst_caps_unref (caps);
585 g_object_unref (list);
587 g_value_take_object (dest, list);
593 ges_marker_list_serialize (const GValue * v)
595 GESMarkerList *list = GES_MARKER_LIST (g_value_get_object (v));
597 GstCaps *caps = gst_caps_new_empty ();
598 gchar *caps_str, *escaped, *res;
601 s = gst_structure_new ("marker-list-flags", "flags", G_TYPE_INT,
603 gst_caps_append_structure (caps, s);
605 iter = g_sequence_get_begin_iter (list->markers);
607 while (!g_sequence_iter_is_end (iter)) {
608 GESMarker *marker = (GESMarker *) g_sequence_get (iter);
611 metas = ges_meta_container_metas_to_string (GES_META_CONTAINER (marker));
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);
621 iter = g_sequence_iter_next (iter);
624 caps_str = gst_caps_to_string (caps);
625 escaped = g_strescape (caps_str, NULL);
627 res = g_strdup_printf ("\"%s\"", escaped);
629 gst_caps_unref (caps);