Merge branch '0.10'
[platform/upstream/gstreamer.git] / tests / check / gst / gsttocsetter.c
1 /* GStreamer GstTocSetter interface unit tests
2  * Copyright (C) 2010, 2012 Alexander Saprykin <xelfium@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <gst/check/gstcheck.h>
21 #include <gst/gst.h>
22 #include <string.h>
23
24 #define ENTRY_ED1       "/edition1"
25 #define ENTRY_ED2       "/edition2"
26 #define ENTRY_ED3       "test-edition"
27
28 #define ENTRY_CH1       "/edition1/chapter1"
29 #define ENTRY_CH2       "/edition1/chapter2"
30 #define ENTRY_CH3       "/edition2/chapter3"
31 #define ENTRY_CH4       "/test-chapter"
32
33 #define ENTRY_SUB1      "/edition2/chapter3/subchapter1"
34
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"
41
42 #define CHECK_TOC_ENTRY(entry_c,type_c,uid_c)                            \
43 {                                                                        \
44   gchar *tag_c;                                                          \
45   const GValue *val;                                                     \
46                                                                          \
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);                                   \
51                                                                          \
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);    \
56                                                                          \
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);                          \
60 }
61
62 #define CHECK_TOC(toc_t)                                                 \
63 {                                                                        \
64   GstTocEntry *entry_t, *subentry_t;                                     \
65   gchar *tag_t;                                                          \
66   const GValue *val;                                                     \
67   /* check TOC */                                                        \
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);                            \
73                                                                          \
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);   \
78                                                                          \
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);  \
109 }
110
111 /* some minimal GstTocSetter object */
112 #define GST_TYPE_DUMMY_ENC gst_dummy_enc_get_type()
113
114 typedef GstElement GstDummyEnc;
115 typedef GstElementClass GstDummyEncClass;
116
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));
120
121 static void
122 gst_dummy_enc_class_init (GstDummyEncClass * klass)
123 {
124 }
125
126 static void
127 gst_dummy_enc_init (GstDummyEnc * enc)
128 {
129 }
130
131 static GstToc *
132 create_toc (void)
133 {
134   GstToc *toc;
135   GstTocEntry *ed, *ch, *subch;
136
137   toc = gst_toc_new ();
138   gst_tag_list_add (toc->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
139       TOC_TAG, NULL);
140   toc->info =
141       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_TOC,
142       NULL);
143
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,
147       ENTRY_TAG, NULL);
148   ed->info =
149       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
150       NULL);
151
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,
155       ENTRY_TAG, NULL);
156   ch->info =
157       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
158       NULL);
159
160   ed->subentries = g_list_append (ed->subentries, ch);
161
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,
165       ENTRY_TAG, NULL);
166   ch->info =
167       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
168       NULL);
169
170   ed->subentries = g_list_append (ed->subentries, ch);
171
172   /* append edition1 to the TOC */
173   toc->entries = g_list_append (toc->entries, ed);
174
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,
178       ENTRY_TAG, NULL);
179   ed->info =
180       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
181       NULL);
182
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,
186       ENTRY_TAG, NULL);
187   ch->info =
188       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
189       NULL);
190
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,
194       ENTRY_TAG, NULL);
195   subch->info =
196       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
197       NULL);
198
199   /* append subchapter1 to chapter3 */
200   ch->subentries = g_list_append (ch->subentries, subch);
201
202   /* append chapter3 to edition2 */
203   ed->subentries = g_list_append (ed->subentries, ch);
204
205   /* finally append edition2 to the TOC */
206   toc->entries = g_list_append (toc->entries, ed);
207
208   return toc;
209 }
210
211 GST_START_TEST (test_set)
212 {
213   GstToc *toc;
214   GstTocEntry *entry, *ed;
215   GstTocSetter *setter;
216   GstElement *enc;
217
218   enc = g_object_new (GST_TYPE_DUMMY_ENC, NULL);
219   fail_unless (enc != NULL);
220
221   setter = GST_TOC_SETTER (enc);
222
223   toc = create_toc ();
224   fail_unless (toc != NULL);
225
226   gst_toc_setter_set_toc (setter, toc);
227
228   gst_toc_free (toc);
229   toc = gst_toc_setter_get_toc_copy (setter);
230
231   CHECK_TOC (toc);
232
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);
236
237   gst_toc_setter_set_toc (setter, toc);
238   gst_toc_setter_add_toc_entry (setter, "0", entry);
239
240   gst_toc_free (toc);
241   toc = gst_toc_setter_get_toc_copy (setter);
242
243   CHECK_TOC (toc);
244
245   /* test entry adding into the arbitrary entry */
246   entry = gst_toc_find_entry (toc, ENTRY_CH2);
247   fail_if (entry == NULL);
248
249   ed = toc->entries->data;
250   ed->subentries = g_list_remove (ed->subentries, entry);
251
252   gst_toc_setter_add_toc_entry (setter, ed->uid, entry);
253
254   CHECK_TOC (toc);
255
256   gst_toc_free (toc);
257   gst_toc_setter_reset_toc (setter);
258   toc = gst_toc_setter_get_toc_copy (setter);
259
260   fail_unless (toc == NULL);
261
262   g_object_unref (enc);
263 }
264
265 GST_END_TEST static int spin_and_wait = 1;
266 static int threads_running = 0;
267
268 #define THREADS_TEST_SECONDS 1.5
269
270 static gpointer
271 test_threads_thread_func1 (gpointer data)
272 {
273   GstToc *toc;
274   GstTocSetter *setter = GST_TOC_SETTER (data);
275   GTimer *timer;
276
277   toc = create_toc ();
278   timer = g_timer_new ();
279
280   g_atomic_int_inc (&threads_running);
281   while (g_atomic_int_get (&spin_and_wait))
282     g_usleep (0);
283
284   GST_INFO ("Go!");
285   g_timer_start (timer);
286
287   while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS)
288     gst_toc_setter_set_toc (setter, toc);
289
290   gst_toc_free (toc);
291   g_timer_destroy (timer);
292   GST_INFO ("Done");
293
294   return NULL;
295 }
296
297 static gpointer
298 test_threads_thread_func2 (gpointer data)
299 {
300   GstToc *toc;
301   GstTocSetter *setter = GST_TOC_SETTER (data);
302   GTimer *timer;
303
304   toc = create_toc ();
305   timer = g_timer_new ();
306
307   g_atomic_int_inc (&threads_running);
308   while (g_atomic_int_get (&spin_and_wait))
309     g_usleep (0);
310
311   GST_INFO ("Go!");
312   g_timer_start (timer);
313
314   while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS)
315     gst_toc_setter_set_toc (setter, toc);
316
317   gst_toc_free (toc);
318   g_timer_destroy (timer);
319   GST_INFO ("Done");
320
321   return NULL;
322 }
323
324 static gpointer
325 test_threads_thread_func3 (gpointer data)
326 {
327   GstTocSetter *setter = GST_TOC_SETTER (data);
328   GTimer *timer;
329
330   timer = g_timer_new ();
331
332   g_atomic_int_inc (&threads_running);
333   while (g_atomic_int_get (&spin_and_wait))
334     g_usleep (0);
335
336   GST_INFO ("Go!");
337   g_timer_start (timer);
338
339   while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS) {
340     gst_toc_setter_reset_toc (setter);
341   }
342
343   g_timer_destroy (timer);
344   GST_INFO ("Done");
345
346   return NULL;
347 }
348
349 GST_START_TEST (test_threads)
350 {
351   GstTocSetter *setter;
352   GThread *threads[3];
353
354   setter = GST_TOC_SETTER (g_object_new (GST_TYPE_DUMMY_ENC, NULL));
355
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);
360
361   while (g_atomic_int_get (&threads_running) < 3)
362     g_usleep (10);
363
364   g_atomic_int_set (&spin_and_wait, FALSE);
365
366   g_thread_join (threads[0]);
367   g_thread_join (threads[1]);
368   g_thread_join (threads[2]);
369
370   g_object_unref (G_OBJECT (setter));
371 }
372
373 GST_END_TEST static Suite *
374 gst_toc_setter_suite (void)
375 {
376   Suite *s = suite_create ("GstTocSetter");
377   TCase *tc_chain = tcase_create ("general");
378
379   suite_add_tcase (s, tc_chain);
380   tcase_add_test (tc_chain, test_set);
381   tcase_add_test (tc_chain, test_threads);
382
383   return s;
384 }
385
386 GST_CHECK_MAIN (gst_toc_setter);