1 /* GStreamer GstTocSetter interface unit tests
2 * Copyright (C) 2010, 2012 Alexander Saprykin <xelfium@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include <gst/check/gstcheck.h>
24 #define ENTRY_ED1 "/edition1"
25 #define ENTRY_ED2 "/edition2"
26 #define ENTRY_ED3 "test-edition"
28 #define ENTRY_CH1 "/edition1/chapter1"
29 #define ENTRY_CH2 "/edition1/chapter2"
30 #define ENTRY_CH3 "/edition2/chapter3"
31 #define ENTRY_CH4 "/test-chapter"
33 #define ENTRY_SUB1 "/edition2/chapter3/subchapter1"
35 #define ENTRY_TAG "EntryTag"
36 #define TOC_TAG "TocTag"
37 #define INFO_NAME "info"
38 #define INFO_FIELD "info-test"
39 #define INFO_TEXT_EN "info-text-entry"
40 #define INFO_TEXT_TOC "info-text-toc"
42 #define CHECK_TOC_ENTRY(entry_c,type_c,uid_c) \
47 fail_unless_equals_string (entry_c->uid, uid_c); \
48 fail_unless (entry_c->type == type_c); \
49 fail_unless (entry_c->tags != NULL); \
50 fail_unless (entry_c->pads == NULL); \
52 fail_unless (entry_c->info != NULL); \
53 val = gst_structure_get_value (entry_c->info, INFO_FIELD); \
54 fail_unless (val != NULL); \
55 fail_unless_equals_string (g_value_get_string (val), INFO_TEXT_EN); \
57 fail_unless (gst_tag_list_get_string (entry_c->tags, \
58 GST_TAG_TITLE, &tag_c)); \
59 fail_unless_equals_string (tag_c, ENTRY_TAG); \
62 #define CHECK_TOC(toc_t) \
64 GstTocEntry *entry_t, *subentry_t; \
68 fail_unless (g_list_length (toc_t->entries) == 2); \
69 fail_unless (toc_t->tags != NULL); \
70 fail_unless (gst_tag_list_get_string (toc_t->tags, \
71 GST_TAG_TITLE, &tag_t)); \
72 fail_unless_equals_string (tag_t, TOC_TAG); \
74 fail_unless (toc_t->info != NULL); \
75 val = gst_structure_get_value (toc_t->info, INFO_FIELD); \
76 fail_unless (val != NULL); \
77 fail_unless_equals_string (g_value_get_string (val), INFO_TEXT_TOC); \
79 /* check edition1 */ \
80 entry_t = g_list_nth_data (toc_t->entries, 0); \
81 fail_if (entry_t == NULL); \
82 fail_unless (g_list_length (entry_t->subentries) == 2); \
83 CHECK_TOC_ENTRY (entry_t, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1); \
84 /* check chapter1 */ \
85 subentry_t = g_list_nth_data (entry_t->subentries, 0); \
86 fail_if (subentry_t == NULL); \
87 fail_unless (g_list_length (subentry_t->subentries) == 0); \
88 CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1); \
89 /* check chapter2 */ \
90 subentry_t = g_list_nth_data (entry_t->subentries, 1); \
91 fail_if (subentry_t == NULL); \
92 fail_unless (g_list_length (subentry_t->subentries) == 0); \
93 CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2); \
94 /* check edition2 */ \
95 entry_t = g_list_nth_data (toc_t->entries, 1); \
96 fail_if (entry_t == NULL); \
97 fail_unless (g_list_length (entry_t->subentries) == 1); \
98 CHECK_TOC_ENTRY (entry_t, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2); \
99 /* check chapter3 */ \
100 subentry_t = g_list_nth_data (entry_t->subentries, 0); \
101 fail_if (subentry_t == NULL); \
102 fail_unless (g_list_length (subentry_t->subentries) == 1); \
103 CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3); \
104 /* check subchapter1 */ \
105 subentry_t = g_list_nth_data (subentry_t->subentries, 0); \
106 fail_if (subentry_t == NULL); \
107 fail_unless (g_list_length (subentry_t->subentries) == 0); \
108 CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1); \
111 /* some minimal GstTocSetter object */
112 #define GST_TYPE_DUMMY_ENC gst_dummy_enc_get_type()
114 typedef GstElement GstDummyEnc;
115 typedef GstElementClass GstDummyEncClass;
117 GType gst_dummy_enc_get_type (void);
118 G_DEFINE_TYPE_WITH_CODE (GstDummyEnc, gst_dummy_enc,
119 GST_TYPE_ELEMENT, G_IMPLEMENT_INTERFACE (GST_TYPE_TOC_SETTER, NULL));
122 gst_dummy_enc_class_init (GstDummyEncClass * klass)
127 gst_dummy_enc_init (GstDummyEnc * enc)
135 GstTocEntry *ed, *ch, *subch;
137 toc = gst_toc_new ();
138 gst_tag_list_add (toc->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
141 gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_TOC,
144 /* create edition1 */
145 ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);
146 gst_tag_list_add (ed->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
149 gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
152 /* append chapter1 to edition1 */
153 ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1);
154 gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
157 gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
160 ed->subentries = g_list_append (ed->subentries, ch);
162 /* append chapter2 to edition1 */
163 ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2);
164 gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
167 gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
170 ed->subentries = g_list_append (ed->subentries, ch);
172 /* append edition1 to the TOC */
173 toc->entries = g_list_append (toc->entries, ed);
175 /* create edition2 */
176 ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2);
177 gst_tag_list_add (ed->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
180 gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
183 /* create chapter3 */
184 ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3);
185 gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
188 gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
191 /* create subchapter1 */
192 subch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);
193 gst_tag_list_add (subch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
196 gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
199 /* append subchapter1 to chapter3 */
200 ch->subentries = g_list_append (ch->subentries, subch);
202 /* append chapter3 to edition2 */
203 ed->subentries = g_list_append (ed->subentries, ch);
205 /* finally append edition2 to the TOC */
206 toc->entries = g_list_append (toc->entries, ed);
211 GST_START_TEST (test_set)
214 GstTocEntry *entry, *ed;
215 GstTocSetter *setter;
218 enc = g_object_new (GST_TYPE_DUMMY_ENC, NULL);
219 fail_unless (enc != NULL);
221 setter = GST_TOC_SETTER (enc);
224 fail_unless (toc != NULL);
226 gst_toc_setter_set_toc (setter, toc);
229 toc = gst_toc_setter_get_toc_copy (setter);
233 /* test entry adding into the root TOC */
234 entry = g_list_last (toc->entries)->data;
235 toc->entries = g_list_remove (toc->entries, entry);
237 gst_toc_setter_set_toc (setter, toc);
238 gst_toc_setter_add_toc_entry (setter, "0", entry);
241 toc = gst_toc_setter_get_toc_copy (setter);
245 /* test entry adding into the arbitrary entry */
246 entry = gst_toc_find_entry (toc, ENTRY_CH2);
247 fail_if (entry == NULL);
249 ed = toc->entries->data;
250 ed->subentries = g_list_remove (ed->subentries, entry);
252 gst_toc_setter_add_toc_entry (setter, ed->uid, entry);
257 gst_toc_setter_reset_toc (setter);
258 toc = gst_toc_setter_get_toc_copy (setter);
260 fail_unless (toc == NULL);
262 g_object_unref (enc);
265 GST_END_TEST static int spin_and_wait = 1;
266 static int threads_running = 0;
268 #define THREADS_TEST_SECONDS 1.5
271 test_threads_thread_func1 (gpointer data)
274 GstTocSetter *setter = GST_TOC_SETTER (data);
278 timer = g_timer_new ();
280 g_atomic_int_inc (&threads_running);
281 while (g_atomic_int_get (&spin_and_wait))
285 g_timer_start (timer);
287 while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS)
288 gst_toc_setter_set_toc (setter, toc);
291 g_timer_destroy (timer);
298 test_threads_thread_func2 (gpointer data)
301 GstTocSetter *setter = GST_TOC_SETTER (data);
305 timer = g_timer_new ();
307 g_atomic_int_inc (&threads_running);
308 while (g_atomic_int_get (&spin_and_wait))
312 g_timer_start (timer);
314 while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS)
315 gst_toc_setter_set_toc (setter, toc);
318 g_timer_destroy (timer);
325 test_threads_thread_func3 (gpointer data)
327 GstTocSetter *setter = GST_TOC_SETTER (data);
330 timer = g_timer_new ();
332 g_atomic_int_inc (&threads_running);
333 while (g_atomic_int_get (&spin_and_wait))
337 g_timer_start (timer);
339 while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS) {
340 gst_toc_setter_reset_toc (setter);
343 g_timer_destroy (timer);
349 GST_START_TEST (test_threads)
351 GstTocSetter *setter;
354 setter = GST_TOC_SETTER (g_object_new (GST_TYPE_DUMMY_ENC, NULL));
356 spin_and_wait = TRUE;
357 threads[0] = g_thread_create (test_threads_thread_func1, setter, TRUE, NULL);
358 threads[1] = g_thread_create (test_threads_thread_func2, setter, TRUE, NULL);
359 threads[2] = g_thread_create (test_threads_thread_func3, setter, TRUE, NULL);
361 while (g_atomic_int_get (&threads_running) < 3)
364 g_atomic_int_set (&spin_and_wait, FALSE);
366 g_thread_join (threads[0]);
367 g_thread_join (threads[1]);
368 g_thread_join (threads[2]);
370 g_object_unref (G_OBJECT (setter));
373 GST_END_TEST static Suite *
374 gst_toc_setter_suite (void)
376 Suite *s = suite_create ("GstTocSetter");
377 TCase *tc_chain = tcase_create ("general");
379 suite_add_tcase (s, tc_chain);
380 tcase_add_test (tc_chain, test_set);
381 tcase_add_test (tc_chain, test_threads);
386 GST_CHECK_MAIN (gst_toc_setter);