tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.git] / tests / check / libs / cddabasesrc.c
1 /* GStreamer
2  *
3  * unit test for cddabasesrc
4  *
5  * Copyright (C) <2005> Tim-Philipp Müller <tim centricular net>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* TODO:
24  *  - test different modes (when seeking to tracks in track mode, buffer
25  *    timestamps should start from 0, when seeking to tracks in disc mode,
26  *    buffer timestamps should increment, etc.)
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <unistd.h>
34
35 #include <gst/check/gstcheck.h>
36 #include <gst/check/gstbufferstraw.h>
37
38 #include <gst/cdda/gstcddabasesrc.h>
39 #include <string.h>
40
41 #define CD_FRAMESIZE_RAW 2352
42
43 #define GST_TYPE_CD_FOO_SRC            (gst_cd_foo_src_get_type())
44 #define GST_CD_FOO_SRC(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_CD_FOO_SRC,GstCdFooSrc))
45 #define GST_CD_FOO_SRC_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_CD_FOO_SRC,GstCdFooSrcClass))
46 #define GST_IS_CD_FOO_SRC(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_CD_FOO_SRC))
47 #define GST_IS_CD_FOO_SRC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_CD_FOO_SRC))
48 #define GST_CD_FOO_SRC_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_CDDA_BASAE_SRC, GstCdFooSrcClass))
49
50 typedef struct _GstCdFooSrc GstCdFooSrc;
51 typedef struct _GstCdFooSrcClass GstCdFooSrcClass;
52
53
54 /* Neue Heimat (CD 2) */
55 static GstCddaBaseSrcTrack nh_cd2_tracks[] = {
56   {TRUE, 1, 0, 20664, NULL,},
57   {TRUE, 2, 20665, 52377, NULL,},
58   {TRUE, 3, 52378, 84100, NULL,},
59   {TRUE, 4, 84101, 105401, NULL,},
60   {TRUE, 5, 105402, 123060, NULL,},
61   {TRUE, 6, 123061, 146497, NULL,},
62   {TRUE, 7, 146498, 175693, NULL,},
63   {TRUE, 8, 175694, 203272, NULL,},
64   {TRUE, 9, 203273, 217909, NULL,},
65   {TRUE, 10, 217910, 240938, NULL,},
66   {TRUE, 11, 240939, 256169, NULL,},
67   {TRUE, 12, 256170, 282237, NULL,},
68   {TRUE, 13, 282238, 307606, NULL,},
69   {TRUE, 14, 307607, 337245, NULL,}
70 };
71
72 /* Offspring - Smash */
73 static GstCddaBaseSrcTrack offspring_tracks[] = {
74   {TRUE, 1, 0, 1924, NULL,},
75   {TRUE, 2, 1925, 12947, NULL,},
76   {TRUE, 3, 12948, 29739, NULL,},
77   {TRUE, 4, 29740, 47202, NULL,},
78   {TRUE, 5, 47203, 63134, NULL,},
79   {TRUE, 6, 63135, 77954, NULL,},
80   {TRUE, 7, 77955, 92789, NULL,},
81   {TRUE, 8, 92790, 112127, NULL,},
82   {TRUE, 9, 112128, 124372, NULL,},
83   {TRUE, 10, 124373, 133574, NULL,},
84   {TRUE, 11, 133575, 143484, NULL,},
85   {TRUE, 12, 143485, 149279, NULL,},
86   {TRUE, 13, 149280, 162357, NULL,},
87   {TRUE, 14, 162358, 210372, NULL,}
88 };
89
90 /* this matches the sample TOC from the DiscIDCalculation
91  * page in the Musicbrainz wiki. It's a tricky one because
92  * it's got a data track as well. */
93 static GstCddaBaseSrcTrack mb_sample_tracks[] = {
94   {TRUE, 1, 0, 18640, NULL,},
95   {TRUE, 2, 18641, 34666, NULL,},
96   {TRUE, 3, 34667, 56349, NULL,},
97   {TRUE, 4, 56350, 77005, NULL,},
98   {TRUE, 5, 77006, 106093, NULL,},
99   {TRUE, 6, 106094, 125728, NULL,},
100   {TRUE, 7, 125729, 149784, NULL,},
101   {TRUE, 8, 149785, 168884, NULL,},
102   {TRUE, 9, 168885, 185909, NULL,},
103   {TRUE, 10, 185910, 205828, NULL,},
104   {TRUE, 11, 205829, 230141, NULL,},
105   {TRUE, 12, 230142, 246658, NULL,},
106   {TRUE, 13, 246659, 265613, NULL,},
107   {TRUE, 14, 265614, 289478, NULL,},
108   {FALSE, 15, 289479, 325731, NULL,}
109 };
110
111 /* Nicola Conte - Other Directions (also
112  * tricky due to the extra data track) */
113 static GstCddaBaseSrcTrack nconte_odir_tracks[] = {
114   {TRUE, 1, 0, 17852, NULL,},
115   {TRUE, 2, 17853, 39956, NULL,},
116   {TRUE, 3, 39957, 68449, NULL,},
117   {TRUE, 4, 68450, 88725, NULL,},
118   {TRUE, 5, 88726, 106413, NULL,},
119   {TRUE, 6, 106414, 131966, NULL,},
120   {TRUE, 7, 131967, 152372, NULL,},
121   {TRUE, 8, 152373, 168602, NULL,},
122   {TRUE, 9, 168603, 190348, NULL,},
123   {TRUE, 10, 190349, 209044, NULL,},
124   {TRUE, 11, 209045, 235586, NULL,},
125   {TRUE, 12, 235587, 253830, NULL,},
126   {TRUE, 13, 253831, 272213, NULL,},
127   {FALSE, 14, 272214, 332849, NULL,}
128 };
129
130 /* Pink Martini - Sympathique (11 track version) */
131 static GstCddaBaseSrcTrack pm_symp_tracks[] = {
132   {TRUE, 1, 0, 21667, NULL,},
133   {TRUE, 2, 21668, 49576, NULL,},
134   {TRUE, 3, 49577, 62397, NULL,},
135   {TRUE, 4, 62398, 81087, NULL,},
136   {TRUE, 5, 81088, 106595, NULL,},
137   {TRUE, 6, 106596, 122012, NULL,},
138   {TRUE, 7, 122013, 138469, NULL,},
139   {TRUE, 8, 138470, 157306, NULL,},
140   {TRUE, 9, 157307, 179635, NULL,},
141   {TRUE, 10, 179636, 203673, NULL,},
142   {TRUE, 11, 203674, 213645, NULL,}
143 };
144
145 #define NUM_TEST_DISCS 5
146
147 struct _test_disc
148 {
149   GstCddaBaseSrcTrack *tracks;
150   guint num_tracks;
151   guint32 cddb_discid;
152   const gchar *musicbrainz_discid;
153 };
154
155 /* FIXME: now we just need to find out how to treat
156  * data tracks for the cddb id calculation .... */
157 static struct _test_disc test_discs[NUM_TEST_DISCS] = {
158   {nh_cd2_tracks, G_N_ELEMENTS (nh_cd2_tracks), 0xae11900e,
159       NULL},
160   {mb_sample_tracks, G_N_ELEMENTS (mb_sample_tracks), 0x00000000,
161       "MUtMmKN402WPj3_VFsgUelxpc8U-"},
162   {offspring_tracks, G_N_ELEMENTS (offspring_tracks), 0xc20af40e,
163       "ahg7JUcfR3vCYBphSDIogOOWrr0-"},
164   {nconte_odir_tracks, G_N_ELEMENTS (nconte_odir_tracks), 0x00000000,
165         /* hKx_PejjG47X161ND_Sh0HyqaS0- according to libmusicbrainz, but that's
166          * wrong according to the wiki docs (or not?) (neither discid is listed) */
167       "fboaOQtfqwENv8WyXa9tRyvyUbQ-"},
168   {pm_symp_tracks, G_N_ELEMENTS (pm_symp_tracks), 0xa00b200b,
169       "iP0DOLdr4vt_IfKSIXoRUR.q_Wc-"}
170 };
171
172 struct _GstCdFooSrc
173 {
174   GstCddaBaseSrc cddabasesrc;
175
176   struct _test_disc *cur_test;
177   guint cur_disc;
178 };
179
180 struct _GstCdFooSrcClass
181 {
182   GstCddaBaseSrcClass parent_class;
183 };
184
185 GType gst_cd_foo_src_get_type (void);
186 GST_BOILERPLATE (GstCdFooSrc, gst_cd_foo_src, GstCddaBaseSrc,
187     GST_TYPE_CDDA_BASE_SRC);
188
189 static GstBuffer *gst_cd_foo_src_read_sector (GstCddaBaseSrc * src,
190     gint sector);
191 static gboolean gst_cd_foo_src_open (GstCddaBaseSrc * src,
192     const gchar * device);
193 static void gst_cd_foo_src_close (GstCddaBaseSrc * src);
194
195 static void
196 gst_cd_foo_src_base_init (gpointer g_class)
197 {
198   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
199
200   gst_element_class_set_details_simple (element_class,
201       "CD Audio (cdda) Source, FooBar", "Source/File",
202       "Read audio from CD", "Foo Bar <foo@bar.com>");
203 }
204
205 static void
206 gst_cd_foo_src_init (GstCdFooSrc * src, GstCdFooSrcClass * klass)
207 {
208   src->cur_disc = 0;
209 }
210
211 static void
212 gst_cd_foo_src_class_init (GstCdFooSrcClass * klass)
213 {
214   GstCddaBaseSrcClass *cddabasesrc_class = GST_CDDA_BASE_SRC_CLASS (klass);
215
216   cddabasesrc_class->open = gst_cd_foo_src_open;
217   cddabasesrc_class->close = gst_cd_foo_src_close;
218   cddabasesrc_class->read_sector = gst_cd_foo_src_read_sector;
219 }
220
221 static gboolean
222 gst_cd_foo_src_open (GstCddaBaseSrc * cddabasesrc, const gchar * device)
223 {
224   GstCddaBaseSrcTrack *tracks;
225   GstCdFooSrc *src;
226   gint i;
227
228   src = GST_CD_FOO_SRC (cddabasesrc);
229
230   /* if this fails, the test is wrong */
231   g_assert (src->cur_disc < NUM_TEST_DISCS);
232
233   src->cur_test = &test_discs[src->cur_disc];
234
235   /* add tracks */
236   tracks = src->cur_test->tracks;
237   for (i = 0; i < src->cur_test->num_tracks; ++i) {
238     gst_cdda_base_src_add_track (GST_CDDA_BASE_SRC (src), &tracks[i]);
239   }
240
241   return TRUE;
242 }
243
244 static void
245 gst_cd_foo_src_close (GstCddaBaseSrc * cddabasesrc)
246 {
247   GstCdFooSrc *src = GST_CD_FOO_SRC (cddabasesrc);
248
249   if (src->cur_test->cddb_discid != 0) {
250     g_assert (cddabasesrc->discid == src->cur_test->cddb_discid);
251   }
252
253   if (src->cur_test->musicbrainz_discid != NULL) {
254     g_assert (g_str_equal (cddabasesrc->mb_discid,
255             src->cur_test->musicbrainz_discid));
256   }
257 }
258
259 static GstBuffer *
260 gst_cd_foo_src_read_sector (GstCddaBaseSrc * cddabasesrc, gint sector)
261 {
262   GstBuffer *buf;
263
264   buf = gst_buffer_new_and_alloc (CD_FRAMESIZE_RAW);
265   memset (GST_BUFFER_DATA (buf), 0, CD_FRAMESIZE_RAW);
266
267   return buf;
268 }
269
270 static inline gboolean
271 tag_list_has_tag (GstTagList * list, const gchar * tag, GType type)
272 {
273   const GValue *val = gst_tag_list_get_value_index (list, tag, 0);
274
275   if (val == NULL) {
276     GST_LOG ("no tag '%s' in taglist %" GST_PTR_FORMAT, tag, list);
277     return FALSE;
278   }
279
280   if (!G_VALUE_HOLDS (val, type)) {
281     GST_LOG ("tag '%s' in taglist %" GST_PTR_FORMAT " is not of type %s",
282         tag, list, g_type_name (type));
283     return FALSE;
284   }
285
286   return TRUE;
287 }
288
289 static void
290 test_uri_parse (const gchar * uri, const gchar * device, gint track)
291 {
292   GstElement *foosrc;
293   gchar *set_device = NULL;
294   gint set_track = 0;
295
296   foosrc = gst_element_factory_make ("cdfoosrc", "cdfoosrc");
297   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (foosrc), uri) == TRUE,
298       "couldn't set uri %s", uri);
299   g_object_get (foosrc, "device", &set_device, "track", &set_track, NULL);
300   fail_unless (set_device != NULL);
301   fail_unless (strcmp (set_device, device) == 0,
302       "device set was %s, expected %s", set_device, device);
303   fail_unless (set_track == track, "track set was %d, expected %d", set_track,
304       track);
305   g_free (set_device);
306   gst_object_unref (foosrc);
307 }
308
309 GST_START_TEST (test_discid_calculations)
310 {
311   GstElement *foosrc, *pipeline, *sink;
312   gint i;
313
314   fail_unless (gst_element_register (NULL, "cdfoosrc", GST_RANK_SECONDARY,
315           GST_TYPE_CD_FOO_SRC));
316
317   pipeline = gst_pipeline_new ("pipeline");
318
319   sink = gst_element_factory_make ("fakesink", "sink");
320   fail_unless (sink != NULL, "couldn't create fakesink");
321
322   foosrc = gst_element_factory_make ("cdfoosrc", "cdfoosrc");
323   fail_unless (foosrc != NULL, "couldn't create cdfoosrc");
324
325   gst_bin_add (GST_BIN (pipeline), foosrc);
326   gst_bin_add (GST_BIN (pipeline), sink);
327   fail_unless (gst_element_link (foosrc, sink));
328
329   for (i = 0; i < G_N_ELEMENTS (test_discs); ++i) {
330     GstTagList *tags = NULL;
331     GstMessage *msg;
332
333     GST_LOG ("Testing disc layout %u ...", i);
334     GST_CD_FOO_SRC (foosrc)->cur_disc = i;
335     gst_element_set_state (pipeline, GST_STATE_PLAYING);
336
337     msg =
338         gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipeline),
339         GST_CLOCK_TIME_NONE, GST_MESSAGE_TAG);
340     gst_message_parse_tag (msg, &tags);
341     fail_unless (tags != NULL);
342     fail_unless (tag_list_has_tag (tags, "track-count", G_TYPE_UINT));
343     fail_unless (tag_list_has_tag (tags, "track-number", G_TYPE_UINT));
344     fail_unless (tag_list_has_tag (tags, "duration", G_TYPE_UINT64));
345     fail_unless (tag_list_has_tag (tags, "discid", G_TYPE_STRING));
346     fail_unless (tag_list_has_tag (tags, "discid-full", G_TYPE_STRING));
347     fail_unless (tag_list_has_tag (tags, "musicbrainz-discid", G_TYPE_STRING));
348     fail_unless (tag_list_has_tag (tags, "musicbrainz-discid-full",
349             G_TYPE_STRING));
350     gst_tag_list_free (tags);
351     gst_message_unref (msg);
352
353     msg =
354         gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipeline),
355         GST_CLOCK_TIME_NONE, GST_MESSAGE_ASYNC_DONE);
356     gst_message_unref (msg);
357
358     gst_element_set_state (pipeline, GST_STATE_NULL);
359   }
360
361   gst_object_unref (pipeline);
362
363   gst_task_cleanup_all ();
364 }
365
366 GST_END_TEST;
367
368 GST_START_TEST (test_buffer_timestamps)
369 {
370   GstElement *foosrc, *pipeline, *fakesink;
371   GstClockTime prev_ts, prev_duration, ts;
372   GstPad *sinkpad;
373   gint i;
374
375   fail_unless (gst_element_register (NULL, "cdfoosrc", GST_RANK_SECONDARY,
376           GST_TYPE_CD_FOO_SRC));
377
378   pipeline = gst_pipeline_new ("pipeline");
379   foosrc = gst_element_factory_make ("cdfoosrc", "cdfoosrc");
380   fakesink = gst_element_factory_make ("fakesink", "fakesink");
381   gst_bin_add_many (GST_BIN (pipeline), foosrc, fakesink, NULL);
382   fail_unless (gst_element_link (foosrc, fakesink));
383   sinkpad = gst_element_get_static_pad (fakesink, "sink");
384
385   GST_CD_FOO_SRC (foosrc)->cur_disc = 0;
386
387   gst_buffer_straw_start_pipeline (pipeline, sinkpad);
388
389   prev_ts = GST_CLOCK_TIME_NONE;
390   prev_duration = GST_CLOCK_TIME_NONE;
391
392   for (i = 0; i < 100; ++i) {
393     GstBuffer *buf;
394
395     buf = gst_buffer_straw_get_buffer (pipeline, sinkpad);
396     GST_LOG ("buffer, ts=%" GST_TIME_FORMAT ", dur=%" GST_TIME_FORMAT,
397         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
398         GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
399     ts = GST_BUFFER_TIMESTAMP (buf);
400     fail_unless (GST_CLOCK_TIME_IS_VALID (ts));
401     fail_unless (GST_BUFFER_DURATION_IS_VALID (buf));
402     if (i > 0) {
403       fail_unless (GST_CLOCK_TIME_IS_VALID (prev_ts));
404       fail_unless (GST_CLOCK_TIME_IS_VALID (prev_duration));
405       fail_unless ((prev_ts + prev_duration) == ts);
406     }
407     prev_ts = ts;
408     prev_duration = GST_BUFFER_DURATION (buf);
409     gst_buffer_unref (buf);
410   }
411
412   gst_buffer_straw_stop_pipeline (pipeline, sinkpad);
413
414   gst_task_cleanup_all ();
415   gst_object_unref (pipeline);
416   gst_object_unref (sinkpad);
417 }
418
419 GST_END_TEST;
420
421 GST_START_TEST (test_uri_parsing)
422 {
423   GstElement *foosrc;
424
425   fail_unless (gst_element_register (NULL, "cdfoosrc", GST_RANK_SECONDARY,
426           GST_TYPE_CD_FOO_SRC));
427
428   /* wrong protocol */
429   foosrc = gst_element_factory_make ("cdfoosrc", "cdfoosrc");
430   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (foosrc),
431           "x://") == FALSE);
432   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (foosrc),
433           "cddaq://") == FALSE);
434
435   /* cdda://track */
436   test_uri_parse ("cdda://", "/dev/cdrom", 1);
437   test_uri_parse ("cdda://2", "/dev/cdrom", 2);
438   test_uri_parse ("cdda://47", "/dev/cdrom", 47);
439   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (foosrc),
440           "cdda://-1") == FALSE);
441   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (foosrc),
442           "cdda://what") == FALSE);
443
444   /* cdda://device#track */
445   test_uri_parse ("cdda:///dev/hdb#1", "/dev/hdb", 1);
446   test_uri_parse ("cdda://anything#8", "anything", 8);
447   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (foosrc),
448           "cdda:///dev/hdb#nonsense") == FALSE);
449   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (foosrc),
450           "cdda:///dev/hdb#-2") == FALSE);
451
452   /* cdda://track#device (device should be ignored - FIXME 0.11) */
453   test_uri_parse ("cdda://8#/dev/hdb", "/dev/cdrom", 8);
454
455   gst_object_unref (foosrc);
456 }
457
458 GST_END_TEST;
459
460 GST_START_TEST (test_properties)
461 {
462   GstElement *foosrc;
463   gchar *device;
464   guint track;
465
466   fail_unless (gst_element_register (NULL, "cdfoosrc", GST_RANK_SECONDARY,
467           GST_TYPE_CD_FOO_SRC));
468
469   foosrc = gst_element_factory_make ("cdfoosrc", "cdfoosrc");
470
471   g_object_set (foosrc, "device", "/dev/cdrom", NULL);
472   g_object_get (foosrc, "device", &device, "track", &track, NULL);
473   fail_unless (g_str_equal (device, "/dev/cdrom"));
474   fail_unless_equals_int (track, 1);
475   g_free (device);
476
477   g_object_set (foosrc, "device", "/dev/cdrom1", "track", 17, NULL);
478   g_object_get (foosrc, "device", &device, "track", &track, NULL);
479   fail_unless (g_str_equal (device, "/dev/cdrom1"));
480   fail_unless_equals_int (track, 17);
481   g_free (device);
482
483   g_object_set (foosrc, "track", 17, "device", "/dev/cdrom1", NULL);
484   g_object_get (foosrc, "device", &device, "track", &track, NULL);
485   fail_unless (g_str_equal (device, "/dev/cdrom1"));
486   fail_unless_equals_int (track, 17);
487   g_free (device);
488
489   g_object_set (foosrc, "track", 12, NULL);
490   g_object_get (foosrc, "device", &device, "track", &track, NULL);
491   fail_unless (g_str_equal (device, "/dev/cdrom1"));
492   fail_unless_equals_int (track, 12);
493   g_free (device);
494
495   gst_object_unref (foosrc);
496 }
497
498 GST_END_TEST;
499
500 static Suite *
501 cddabasesrc_suite (void)
502 {
503   Suite *s = suite_create ("cddabasesrc");
504   TCase *tc_chain = tcase_create ("general");
505
506   suite_add_tcase (s, tc_chain);
507   tcase_add_test (tc_chain, test_discid_calculations);
508   tcase_add_test (tc_chain, test_buffer_timestamps);
509   tcase_add_test (tc_chain, test_uri_parsing);
510   tcase_add_test (tc_chain, test_properties);
511
512   return s;
513 }
514
515 GST_CHECK_MAIN (cddabasesrc)