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 static void gst_dummy_enc_add_interfaces (GType enc_type);
118
119 GType gst_dummy_enc_get_type (void);
120 GST_BOILERPLATE_FULL (GstDummyEnc, gst_dummy_enc, GstElement,
121     GST_TYPE_ELEMENT, gst_dummy_enc_add_interfaces);
122
123 static void
124 gst_dummy_enc_add_interfaces (GType enc_type)
125 {
126   static const GInterfaceInfo toc_setter_info = { NULL, NULL, NULL };
127
128   g_type_add_interface_static (enc_type, GST_TYPE_TOC_SETTER, &toc_setter_info);
129 }
130
131 static void
132 gst_dummy_enc_base_init (gpointer g_class)
133 {
134 }
135
136 static void
137 gst_dummy_enc_class_init (GstDummyEncClass * klass)
138 {
139 }
140
141 static void
142 gst_dummy_enc_init (GstDummyEnc * enc, GstDummyEncClass * klass)
143 {
144 }
145
146 static GstToc *
147 create_toc (void)
148 {
149   GstToc *toc;
150   GstTocEntry *ed, *ch, *subch;
151
152   toc = gst_toc_new ();
153   gst_tag_list_add (toc->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
154       TOC_TAG, NULL);
155   toc->info =
156       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_TOC,
157       NULL);
158
159   /* create edition1 */
160   ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);
161   gst_tag_list_add (ed->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
162       ENTRY_TAG, NULL);
163   ed->info =
164       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
165       NULL);
166
167   /* append chapter1 to edition1 */
168   ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1);
169   gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
170       ENTRY_TAG, NULL);
171   ch->info =
172       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
173       NULL);
174
175   ed->subentries = g_list_append (ed->subentries, ch);
176
177   /* append chapter2 to edition1 */
178   ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2);
179   gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
180       ENTRY_TAG, NULL);
181   ch->info =
182       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
183       NULL);
184
185   ed->subentries = g_list_append (ed->subentries, ch);
186
187   /* append edition1 to the TOC */
188   toc->entries = g_list_append (toc->entries, ed);
189
190   /* create edition2 */
191   ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2);
192   gst_tag_list_add (ed->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
193       ENTRY_TAG, NULL);
194   ed->info =
195       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
196       NULL);
197
198   /* create chapter3 */
199   ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3);
200   gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
201       ENTRY_TAG, NULL);
202   ch->info =
203       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
204       NULL);
205
206   /* create subchapter1 */
207   subch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);
208   gst_tag_list_add (subch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
209       ENTRY_TAG, NULL);
210   subch->info =
211       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
212       NULL);
213
214   /* append subchapter1 to chapter3 */
215   ch->subentries = g_list_append (ch->subentries, subch);
216
217   /* append chapter3 to edition2 */
218   ed->subentries = g_list_append (ed->subentries, ch);
219
220   /* finally append edition2 to the TOC */
221   toc->entries = g_list_append (toc->entries, ed);
222
223   return toc;
224 }
225
226 GST_START_TEST (test_set)
227 {
228   GstToc *toc;
229   GstTocEntry *entry, *ed;
230   GstTocSetter *setter;
231   GstElement *enc;
232
233   enc = g_object_new (GST_TYPE_DUMMY_ENC, NULL);
234   fail_unless (enc != NULL);
235
236   setter = GST_TOC_SETTER (enc);
237
238   toc = create_toc ();
239   fail_unless (toc != NULL);
240
241   gst_toc_setter_set_toc (setter, toc);
242
243   gst_toc_free (toc);
244   toc = gst_toc_setter_get_toc_copy (setter);
245
246   CHECK_TOC (toc);
247
248   /* test entry adding into the root TOC */
249   entry = g_list_last (toc->entries)->data;
250   toc->entries = g_list_remove (toc->entries, entry);
251
252   gst_toc_setter_set_toc (setter, toc);
253   gst_toc_setter_add_toc_entry (setter, "0", entry);
254
255   gst_toc_free (toc);
256   toc = gst_toc_setter_get_toc_copy (setter);
257
258   CHECK_TOC (toc);
259
260   /* test entry adding into the arbitrary entry */
261   entry = gst_toc_find_entry (toc, ENTRY_CH2);
262   fail_if (entry == NULL);
263
264   ed = toc->entries->data;
265   ed->subentries = g_list_remove (ed->subentries, entry);
266
267   gst_toc_setter_add_toc_entry (setter, ed->uid, entry);
268
269   CHECK_TOC (toc);
270
271   gst_toc_free (toc);
272   gst_toc_setter_reset_toc (setter);
273   toc = gst_toc_setter_get_toc_copy (setter);
274
275   fail_unless (toc == NULL);
276
277   g_object_unref (enc);
278 }
279
280 GST_END_TEST static int spin_and_wait = 1;
281 static int threads_running = 0;
282
283 #define THREADS_TEST_SECONDS 1.5
284
285 static gpointer
286 test_threads_thread_func1 (gpointer data)
287 {
288   GstToc *toc;
289   GstTocSetter *setter = GST_TOC_SETTER (data);
290   GTimer *timer;
291
292   toc = create_toc ();
293   timer = g_timer_new ();
294
295   g_atomic_int_inc (&threads_running);
296   while (g_atomic_int_get (&spin_and_wait))
297     g_usleep (0);
298
299   GST_INFO ("Go!");
300   g_timer_start (timer);
301
302   while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS)
303     gst_toc_setter_set_toc (setter, toc);
304
305   gst_toc_free (toc);
306   g_timer_destroy (timer);
307   GST_INFO ("Done");
308
309   return NULL;
310 }
311
312 static gpointer
313 test_threads_thread_func2 (gpointer data)
314 {
315   GstToc *toc;
316   GstTocSetter *setter = GST_TOC_SETTER (data);
317   GTimer *timer;
318
319   toc = create_toc ();
320   timer = g_timer_new ();
321
322   g_atomic_int_inc (&threads_running);
323   while (g_atomic_int_get (&spin_and_wait))
324     g_usleep (0);
325
326   GST_INFO ("Go!");
327   g_timer_start (timer);
328
329   while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS)
330     gst_toc_setter_set_toc (setter, toc);
331
332   gst_toc_free (toc);
333   g_timer_destroy (timer);
334   GST_INFO ("Done");
335
336   return NULL;
337 }
338
339 static gpointer
340 test_threads_thread_func3 (gpointer data)
341 {
342   GstTocSetter *setter = GST_TOC_SETTER (data);
343   GTimer *timer;
344
345   timer = g_timer_new ();
346
347   g_atomic_int_inc (&threads_running);
348   while (g_atomic_int_get (&spin_and_wait))
349     g_usleep (0);
350
351   GST_INFO ("Go!");
352   g_timer_start (timer);
353
354   while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS) {
355     gst_toc_setter_reset_toc (setter);
356   }
357
358   g_timer_destroy (timer);
359   GST_INFO ("Done");
360
361   return NULL;
362 }
363
364 GST_START_TEST (test_threads)
365 {
366   GstTocSetter *setter;
367   GThread *threads[3];
368
369   setter = GST_TOC_SETTER (g_object_new (GST_TYPE_DUMMY_ENC, NULL));
370
371   spin_and_wait = TRUE;
372   threads[0] = g_thread_create (test_threads_thread_func1, setter, TRUE, NULL);
373   threads[1] = g_thread_create (test_threads_thread_func2, setter, TRUE, NULL);
374   threads[2] = g_thread_create (test_threads_thread_func3, setter, TRUE, NULL);
375
376   while (g_atomic_int_get (&threads_running) < 3)
377     g_usleep (10);
378
379   g_atomic_int_set (&spin_and_wait, FALSE);
380
381   g_thread_join (threads[0]);
382   g_thread_join (threads[1]);
383   g_thread_join (threads[2]);
384
385   g_object_unref (G_OBJECT (setter));
386 }
387
388 GST_END_TEST static Suite *
389 gst_toc_setter_suite (void)
390 {
391   Suite *s = suite_create ("GstTocSetter");
392   TCase *tc_chain = tcase_create ("general");
393
394   suite_add_tcase (s, tc_chain);
395   tcase_add_test (tc_chain, test_set);
396   tcase_add_test (tc_chain, test_threads);
397
398   return s;
399 }
400
401 GST_CHECK_MAIN (gst_toc_setter);