Debian Codegraph @

tests/check/ges/markerlist.c

raw
1 /* GStreamer Editing Services
2 *
3 * Copyright (C) <2019> Mathieu Duponchelle <[email protected]>
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 GESMarkerFlags flags;
358
359 ges_init ();
360
361 g_value_init (&val1, GES_TYPE_MARKER_LIST);
362 g_value_init (&val2, GES_TYPE_MARKER_LIST);
363
364 markerlist1 = ges_marker_list_new ();
365 g_object_set (markerlist1, "flags", GES_MARKER_FLAG_SNAPPABLE, NULL);
366 marker = ges_marker_list_add (markerlist1, 0);
367 fail_unless (ges_meta_container_set_string (GES_META_CONTAINER (marker),
368 "str-val", test_string));
369 marker = ges_marker_list_add (markerlist1, 10);
370 fail_unless (ges_meta_container_set_string (GES_META_CONTAINER (marker),
371 "first", test_string));
372 fail_unless (ges_meta_container_set_uint (GES_META_CONTAINER (marker),
373 "second", 43));
374
375 ASSERT_OBJECT_REFCOUNT (markerlist1, "local ref", 1);
376
377 g_value_set_instance (&val1, markerlist1);
378 ASSERT_OBJECT_REFCOUNT (markerlist1, "GValue + local ref", 2);
379
380 serialized = gst_value_serialize (&val1);
381 fail_unless (serialized != NULL);
382 GST_DEBUG ("serialized to %s", serialized);
383 fail_unless (gst_value_deserialize (&val2, serialized));
384 cmp = gst_value_serialize (&val2);
385 fail_unless_equals_string (cmp, serialized);
386
387 markerlist2 = GES_MARKER_LIST (g_value_get_object (&val2));
388 ASSERT_OBJECT_REFCOUNT (markerlist2, "GValue", 1);
389
390 g_object_get (markerlist2, "flags", &flags, NULL);
391 fail_unless (flags == GES_MARKER_FLAG_SNAPPABLE);
392
393 fail_unless_equals_int (ges_marker_list_size (markerlist2), 2);
394 markers = ges_marker_list_get_markers (markerlist2);
395 marker = GES_MARKER (markers->data);
396 fail_unless (marker != NULL);
397
398 g_object_get (marker, "position", &position, NULL);
399 fail_unless_equals_uint64 (position, 0);
400 str_val =
401 ges_meta_container_get_string (GES_META_CONTAINER (marker), "str-val");
402 fail_unless_equals_string (str_val, test_string);
403
404 marker = GES_MARKER (markers->next->data);
405 fail_unless (marker != NULL);
406 fail_unless (markers->next->next == NULL);
407
408 g_object_get (marker, "position", &position, NULL);
409 fail_unless_equals_uint64 (position, 10);
410 str_val =
411 ges_meta_container_get_string (GES_META_CONTAINER (marker), "first");
412 fail_unless_equals_string (str_val, test_string);
413 fail_unless (ges_meta_container_get_uint (GES_META_CONTAINER (marker),
414 "second", &uint_val));
415 fail_unless_equals_int (uint_val, 43);
416
417 g_list_free_full (markers, g_object_unref);
418 g_value_unset (&val1);
419 g_value_unset (&val2);
420 ASSERT_OBJECT_REFCOUNT (markerlist1, "local ref", 1);
421 g_object_unref (markerlist1);
422 g_free (serialized);
423 g_free (cmp);
424
425 ges_deinit ();
426 }
427
428 GST_END_TEST;
429
430 GST_START_TEST (test_marker_color)
431 {
432 GESMarkerList *mlist;
433 GESMarker *marker;
434 const guint yellow_rgb = 16776960;
435 guint color;
436
437 ges_init ();
438
439 mlist = ges_marker_list_new ();
440 marker = ges_marker_list_add (mlist, 0);
441 /* getting the color should fail since no value should be set yet */
442 fail_unless (ges_meta_container_get_meta (GES_META_CONTAINER (marker),
443 GES_META_MARKER_COLOR) == NULL);
444 /* trying to set the color field to something other than a uint should
445 * fail */
446 fail_unless (ges_meta_container_set_float (GES_META_CONTAINER (marker),
447 GES_META_MARKER_COLOR, 0.0) == FALSE);
448 fail_unless (ges_meta_container_set_uint (GES_META_CONTAINER (marker),
449 GES_META_MARKER_COLOR, yellow_rgb));
450 fail_unless (ges_meta_container_get_uint (GES_META_CONTAINER (marker),
451 GES_META_MARKER_COLOR, &color));
452 fail_unless_equals_int (color, yellow_rgb);
453
454 g_object_unref (mlist);
455
456 ges_deinit ();
457 }
458
459 GST_END_TEST;
460
461 static Suite *
462 ges_suite (void)
463 {
464 Suite *s = suite_create ("ges-marker-list");
465 TCase *tc_chain = tcase_create ("markerlist");
466
467 suite_add_tcase (s, tc_chain);
468
469 tcase_add_test (tc_chain, test_add);
470 tcase_add_test (tc_chain, test_remove);
471 tcase_add_test (tc_chain, test_signal_marker_added);
472 tcase_add_test (tc_chain, test_signal_marker_removed);
473 tcase_add_test (tc_chain, test_signal_marker_moved);
474 tcase_add_test (tc_chain, test_get_markers);
475 tcase_add_test (tc_chain, test_move_marker);
476 tcase_add_test (tc_chain, test_serialize_deserialize_in_timeline);
477 tcase_add_test (tc_chain, test_serialize_deserialize_in_value);
478 tcase_add_test (tc_chain, test_marker_color);
479
480 return s;
481 }
482
483 GST_CHECK_MAIN (ges);