7649e0ab22890df8d9a4df681276c788d2b10be0
[platform/upstream/gst-editing-services.git] / tests / check / ges / markerlist.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 #include "test-utils.h"
22 #include <ges/ges.h>
23 #include <gst/check/gstcheck.h>
24
25 GST_START_TEST (test_add)
26 {
27   GESMarkerList *markerlist;
28   GESMarker *marker;
29   ges_init ();
30
31   markerlist = ges_marker_list_new ();
32   marker = ges_marker_list_add (markerlist, 42);
33
34   ASSERT_OBJECT_REFCOUNT (marker, "marker list", 1);
35
36   g_object_ref (marker);
37
38   ASSERT_OBJECT_REFCOUNT (marker, "marker list + local ref", 2);
39
40   g_object_unref (markerlist);
41
42   ASSERT_OBJECT_REFCOUNT (marker, "local ref", 1);
43
44   g_object_unref (marker);
45
46   ges_deinit ();
47 }
48
49 GST_END_TEST;
50
51 GST_START_TEST (test_remove)
52 {
53   GESMarkerList *markerlist;
54   GESMarker *marker;
55   ges_init ();
56
57   markerlist = ges_marker_list_new ();
58   marker = ges_marker_list_add (markerlist, 42);
59
60   g_object_ref (marker);
61
62   fail_unless_equals_int (ges_marker_list_size (markerlist), 1);
63
64   fail_unless (ges_marker_list_remove (markerlist, marker));
65   fail_unless_equals_int (ges_marker_list_size (markerlist), 0);
66
67   ASSERT_OBJECT_REFCOUNT (marker, "local ref", 1);
68
69   fail_if (ges_marker_list_remove (markerlist, marker));
70
71   g_object_unref (marker);
72
73   g_object_unref (markerlist);
74
75   ges_deinit ();
76 }
77
78 GST_END_TEST;
79
80
81 static void
82 marker_added_cb (GESMarkerList * mlist, GstClockTime position,
83     GESMarker * marker, gboolean * called)
84 {
85   fail_unless_equals_int (position, 42);
86
87   ASSERT_OBJECT_REFCOUNT (marker, "local ref + signal", 2);
88   *called = TRUE;
89 }
90
91 GST_START_TEST (test_signal_marker_added)
92 {
93   GESMarkerList *mlist;
94   GESMarker *marker;
95   gboolean called = FALSE;
96
97   ges_init ();
98
99   mlist = ges_marker_list_new ();
100   g_signal_connect (mlist, "marker-added", G_CALLBACK (marker_added_cb),
101       &called);
102   marker = ges_marker_list_add (mlist, 42);
103   ASSERT_OBJECT_REFCOUNT (marker, "local ref", 1);
104   fail_unless (called == TRUE);
105   g_signal_handlers_disconnect_by_func (mlist, marker_added_cb, &called);
106
107   g_object_unref (mlist);
108
109   ges_deinit ();
110 }
111
112 GST_END_TEST;
113
114
115 static void
116 marker_removed_cb (GESMarkerList * mlist, GESMarker * marker, gboolean * called)
117 {
118   ASSERT_OBJECT_REFCOUNT (marker, "local ref + signal", 2);
119   *called = TRUE;
120 }
121
122 GST_START_TEST (test_signal_marker_removed)
123 {
124   GESMarkerList *mlist;
125   GESMarker *marker;
126   gboolean called = FALSE;
127
128   ges_init ();
129
130   mlist = ges_marker_list_new ();
131   marker = ges_marker_list_add (mlist, 42);
132
133   ASSERT_OBJECT_REFCOUNT (marker, "local ref", 1);
134
135   g_signal_connect (mlist, "marker-removed", G_CALLBACK (marker_removed_cb),
136       &called);
137
138   fail_unless (ges_marker_list_remove (mlist, marker));
139
140   fail_unless (called == TRUE);
141
142   g_signal_handlers_disconnect_by_func (mlist, marker_removed_cb, &called);
143
144   g_object_unref (mlist);
145
146   ges_deinit ();
147 }
148
149 GST_END_TEST;
150
151
152 static void
153 marker_moved_cb (GESMarkerList * mlist, GstClockTime prev_position,
154     GstClockTime position, GESMarker * marker, gboolean * called)
155 {
156   fail_unless_equals_int (prev_position, 10);
157   fail_unless_equals_int (position, 42);
158
159   ASSERT_OBJECT_REFCOUNT (marker, "local ref + signal", 2);
160   *called = TRUE;
161 }
162
163 GST_START_TEST (test_signal_marker_moved)
164 {
165   GESMarkerList *mlist;
166   GESMarker *marker;
167   gboolean called = FALSE;
168
169   ges_init ();
170
171   mlist = ges_marker_list_new ();
172   g_signal_connect (mlist, "marker-moved", G_CALLBACK (marker_moved_cb),
173       &called);
174
175   marker = ges_marker_list_add (mlist, 10);
176   ASSERT_OBJECT_REFCOUNT (marker, "local ref", 1);
177
178   fail_unless (ges_marker_list_move (mlist, marker, 42));
179
180   fail_unless (called == TRUE);
181
182   g_signal_handlers_disconnect_by_func (mlist, marker_moved_cb, &called);
183
184   g_object_unref (mlist);
185
186   ges_deinit ();
187 }
188
189 GST_END_TEST;
190
191
192 GST_START_TEST (test_get_markers)
193 {
194   GESMarkerList *markerlist;
195   GESMarker *marker1, *marker2, *marker3, *marker4;
196   GList *markers;
197
198   ges_init ();
199
200   markerlist = ges_marker_list_new ();
201   marker1 = ges_marker_list_add (markerlist, 0);
202   marker2 = ges_marker_list_add (markerlist, 10);
203   marker3 = ges_marker_list_add (markerlist, 20);
204   marker4 = ges_marker_list_add (markerlist, 30);
205
206   markers = ges_marker_list_get_markers (markerlist);
207
208   ASSERT_OBJECT_REFCOUNT (marker1, "local ref + markers", 2);
209   ASSERT_OBJECT_REFCOUNT (marker2, "local ref + markers", 2);
210   ASSERT_OBJECT_REFCOUNT (marker3, "local ref + markers", 2);
211   ASSERT_OBJECT_REFCOUNT (marker4, "local ref + markers", 2);
212
213   fail_unless (g_list_index (markers, marker1) == 0);
214   fail_unless (g_list_index (markers, marker2) == 1);
215   fail_unless (g_list_index (markers, marker3) == 2);
216   fail_unless (g_list_index (markers, marker4) == 3);
217
218   g_list_foreach (markers, (GFunc) gst_object_unref, NULL);
219   g_list_free (markers);
220
221   g_object_unref (markerlist);
222
223   ges_deinit ();
224 }
225
226 GST_END_TEST;
227
228
229 GST_START_TEST (test_move_marker)
230 {
231   GESMarkerList *markerlist;
232   GESMarker *marker_a, *marker_b;
233   GstClockTime position;
234   GList *range;
235
236   ges_init ();
237
238   markerlist = ges_marker_list_new ();
239
240   marker_a = ges_marker_list_add (markerlist, 10);
241   marker_b = ges_marker_list_add (markerlist, 30);
242
243   fail_unless (ges_marker_list_move (markerlist, marker_a, 20));
244
245   g_object_get (marker_a, "position", &position, NULL);
246   fail_unless_equals_int (position, 20);
247
248   range = ges_marker_list_get_markers (markerlist);
249
250   fail_unless (g_list_index (range, marker_a) == 0);
251   fail_unless (g_list_index (range, marker_b) == 1);
252
253   g_list_foreach (range, (GFunc) gst_object_unref, NULL);
254   g_list_free (range);
255
256   fail_unless (ges_marker_list_move (markerlist, marker_a, 35));
257
258   range = ges_marker_list_get_markers (markerlist);
259
260   fail_unless (g_list_index (range, marker_b) == 0);
261   fail_unless (g_list_index (range, marker_a) == 1);
262
263   g_list_foreach (range, (GFunc) gst_object_unref, NULL);
264   g_list_free (range);
265
266   fail_unless (ges_marker_list_move (markerlist, marker_a, 30));
267
268   g_object_get (marker_a, "position", &position, NULL);
269   fail_unless_equals_int (position, 30);
270
271   g_object_get (marker_b, "position", &position, NULL);
272   fail_unless_equals_int (position, 30);
273
274   fail_unless_equals_int (ges_marker_list_size (markerlist), 2);
275
276   ges_marker_list_remove (markerlist, marker_a);
277
278   fail_unless (!ges_marker_list_move (markerlist, marker_a, 20));
279
280   g_object_unref (markerlist);
281
282   ges_deinit ();
283 }
284
285 GST_END_TEST;
286
287 GST_START_TEST (test_serialize_deserialize_in_timeline)
288 {
289   GESMarkerList *markerlist1, *markerlist2;
290   gchar *metas1, *metas2;
291   GESTimeline *timeline1, *timeline2;
292
293   ges_init ();
294
295   timeline1 = ges_timeline_new_audio_video ();
296
297   markerlist1 = ges_marker_list_new ();
298   ges_marker_list_add (markerlist1, 0);
299   ges_marker_list_add (markerlist1, 10);
300
301   ASSERT_OBJECT_REFCOUNT (markerlist1, "local ref", 1);
302
303   fail_unless (ges_meta_container_set_marker_list (GES_META_CONTAINER
304           (timeline1), "ges-test", markerlist1));
305
306   ASSERT_OBJECT_REFCOUNT (markerlist1, "GstStructure + local ref", 2);
307
308   markerlist2 =
309       ges_meta_container_get_marker_list (GES_META_CONTAINER (timeline1),
310       "ges-test");
311
312   fail_unless (markerlist1 == markerlist2);
313
314   ASSERT_OBJECT_REFCOUNT (markerlist1, "GstStructure + getter + local ref", 3);
315
316   g_object_unref (markerlist2);
317
318   ASSERT_OBJECT_REFCOUNT (markerlist1, "GstStructure + local ref", 2);
319
320   timeline2 = ges_timeline_new_audio_video ();
321
322   metas1 = ges_meta_container_metas_to_string (GES_META_CONTAINER (timeline1));
323   ges_meta_container_add_metas_from_string (GES_META_CONTAINER (timeline2),
324       metas1);
325   metas2 = ges_meta_container_metas_to_string (GES_META_CONTAINER (timeline2));
326
327   fail_unless_equals_string (metas1, metas2);
328
329   g_free (metas1);
330   g_free (metas2);
331
332   fail_unless (ges_meta_container_set_marker_list (GES_META_CONTAINER
333           (timeline1), "ges-test", NULL));
334
335   ASSERT_OBJECT_REFCOUNT (markerlist1, "local ref", 1);
336
337   g_object_unref (markerlist1);
338   g_object_unref (timeline1);
339   g_object_unref (timeline2);
340
341   ges_deinit ();
342 }
343
344 GST_END_TEST;
345
346 GST_START_TEST (test_serialize_deserialize_in_value)
347 {
348   GESMarkerList *markerlist1, *markerlist2;
349   GESMarker *marker;
350   gchar *serialized, *cmp;
351   const gchar *str_val;
352   guint uint_val;
353   const gchar *test_string = "test \" string";
354   GList *markers;
355   guint64 position;
356   GValue val1 = G_VALUE_INIT, val2 = G_VALUE_INIT;
357
358   ges_init ();
359
360   g_value_init (&val1, GES_TYPE_MARKER_LIST);
361   g_value_init (&val2, GES_TYPE_MARKER_LIST);
362
363   markerlist1 = ges_marker_list_new ();
364   marker = ges_marker_list_add (markerlist1, 0);
365   fail_unless (ges_meta_container_set_string (GES_META_CONTAINER (marker),
366           "str-val", test_string));
367   marker = ges_marker_list_add (markerlist1, 10);
368   fail_unless (ges_meta_container_set_string (GES_META_CONTAINER (marker),
369           "first", test_string));
370   fail_unless (ges_meta_container_set_uint (GES_META_CONTAINER (marker),
371           "second", 43));
372
373   ASSERT_OBJECT_REFCOUNT (markerlist1, "local ref", 1);
374
375   g_value_set_instance (&val1, markerlist1);
376   ASSERT_OBJECT_REFCOUNT (markerlist1, "GValue + local ref", 2);
377
378   serialized = gst_value_serialize (&val1);
379   fail_unless (serialized != NULL);
380   GST_DEBUG ("serialized to %s", serialized);
381   fail_unless (gst_value_deserialize (&val2, serialized));
382   cmp = gst_value_serialize (&val2);
383   fail_unless_equals_string (cmp, serialized);
384
385   markerlist2 = GES_MARKER_LIST (g_value_get_object (&val2));
386   ASSERT_OBJECT_REFCOUNT (markerlist2, "GValue", 1);
387
388   fail_unless_equals_int (ges_marker_list_size (markerlist2), 2);
389   markers = ges_marker_list_get_markers (markerlist2);
390   marker = GES_MARKER (markers->data);
391   fail_unless (marker != NULL);
392
393   g_object_get (marker, "position", &position, NULL);
394   fail_unless_equals_uint64 (position, 0);
395   str_val =
396       ges_meta_container_get_string (GES_META_CONTAINER (marker), "str-val");
397   fail_unless_equals_string (str_val, test_string);
398
399   marker = GES_MARKER (markers->next->data);
400   fail_unless (marker != NULL);
401   fail_unless (markers->next->next == NULL);
402
403   g_object_get (marker, "position", &position, NULL);
404   fail_unless_equals_uint64 (position, 10);
405   str_val =
406       ges_meta_container_get_string (GES_META_CONTAINER (marker), "first");
407   fail_unless_equals_string (str_val, test_string);
408   fail_unless (ges_meta_container_get_uint (GES_META_CONTAINER (marker),
409           "second", &uint_val));
410   fail_unless_equals_int (uint_val, 43);
411
412   g_list_free_full (markers, g_object_unref);
413   g_value_unset (&val1);
414   g_value_unset (&val2);
415   ASSERT_OBJECT_REFCOUNT (markerlist1, "local ref", 1);
416   g_object_unref (markerlist1);
417   g_free (serialized);
418   g_free (cmp);
419
420   ges_deinit ();
421 }
422
423 GST_END_TEST;
424
425 GST_START_TEST (test_marker_color)
426 {
427   GESMarkerList *mlist;
428   GESMarker *marker;
429   const guint yellow_rgb = 16776960;
430   guint color;
431
432   ges_init ();
433
434   mlist = ges_marker_list_new ();
435   marker = ges_marker_list_add (mlist, 0);
436   /* getting the color should fail since no value should be set yet */
437   fail_unless (ges_meta_container_get_meta (GES_META_CONTAINER (marker),
438           GES_META_MARKER_COLOR) == NULL);
439   /* trying to set the color field to something other than a uint should
440    * fail */
441   fail_unless (ges_meta_container_set_float (GES_META_CONTAINER (marker),
442           GES_META_MARKER_COLOR, 0.0) == FALSE);
443   fail_unless (ges_meta_container_set_uint (GES_META_CONTAINER (marker),
444           GES_META_MARKER_COLOR, yellow_rgb));
445   fail_unless (ges_meta_container_get_uint (GES_META_CONTAINER (marker),
446           GES_META_MARKER_COLOR, &color));
447   fail_unless_equals_int (color, yellow_rgb);
448
449   g_object_unref (mlist);
450
451   ges_deinit ();
452 }
453
454 GST_END_TEST;
455
456
457 static Suite *
458 ges_suite (void)
459 {
460   Suite *s = suite_create ("ges-marker-list");
461   TCase *tc_chain = tcase_create ("markerlist");
462
463   suite_add_tcase (s, tc_chain);
464
465   tcase_add_test (tc_chain, test_add);
466   tcase_add_test (tc_chain, test_remove);
467   tcase_add_test (tc_chain, test_signal_marker_added);
468   tcase_add_test (tc_chain, test_signal_marker_removed);
469   tcase_add_test (tc_chain, test_signal_marker_moved);
470   tcase_add_test (tc_chain, test_get_markers);
471   tcase_add_test (tc_chain, test_move_marker);
472   tcase_add_test (tc_chain, test_serialize_deserialize_in_timeline);
473   tcase_add_test (tc_chain, test_serialize_deserialize_in_value);
474   tcase_add_test (tc_chain, test_marker_color);
475
476   return s;
477 }
478
479 GST_CHECK_MAIN (ges);