Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / check / elements / id3demux.c
1 /* GStreamer unit tests for id3demux
2  *
3  * Copyright (C) 2007 Tim-Philipp Müller  <tim centricular net>
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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <gst/check/gstcheck.h>
22
23 #include <gst/gst.h>
24
25 typedef void (CheckTagsFunc) (const GstTagList * tags, const gchar * file);
26
27 static void
28 pad_added_cb (GstElement * id3demux, GstPad * pad, GstBin * pipeline)
29 {
30   GstElement *sink;
31
32   sink = gst_bin_get_by_name (pipeline, "fakesink");
33   fail_unless (gst_element_link (id3demux, sink));
34   gst_object_unref (sink);
35
36   gst_element_set_state (sink, GST_STATE_PAUSED);
37 }
38
39 static GstBusSyncReply
40 error_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
41 {
42   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
43     const gchar *file = (const gchar *) user_data;
44     GError *err = NULL;
45     gchar *dbg = NULL;
46
47     gst_message_parse_error (msg, &err, &dbg);
48     g_error ("ERROR for %s: %s\n%s\n", file, err->message, dbg);
49   }
50
51   return GST_BUS_PASS;
52 }
53
54 static GstTagList *
55 read_tags_from_file (const gchar * file, gboolean push_mode)
56 {
57   GstStateChangeReturn state_ret;
58   GstTagList *tags = NULL;
59   GstMessage *msg;
60   GstElement *src, *sep, *sink, *id3demux, *pipeline;
61   GstBus *bus;
62   gchar *path;
63
64   pipeline = gst_pipeline_new ("pipeline");
65   fail_unless (pipeline != NULL, "Failed to create pipeline!");
66
67   bus = gst_element_get_bus (pipeline);
68
69   /* kids, don't use a sync handler for this at home, really; we do because
70    * we just want to abort and nothing else */
71   gst_bus_set_sync_handler (bus, error_cb, (gpointer) file);
72
73   src = gst_element_factory_make ("filesrc", "filesrc");
74   fail_unless (src != NULL, "Failed to create 'filesrc' element!");
75
76   if (push_mode) {
77     sep = gst_element_factory_make ("queue", "queue");
78     fail_unless (sep != NULL, "Failed to create 'queue' element");
79   } else {
80     sep = gst_element_factory_make ("identity", "identity");
81     fail_unless (sep != NULL, "Failed to create 'identity' element");
82   }
83
84   id3demux = gst_element_factory_make ("id3demux", "id3demux");
85   fail_unless (id3demux != NULL, "Failed to create 'id3demux' element!");
86
87   sink = gst_element_factory_make ("fakesink", "fakesink");
88   fail_unless (sink != NULL, "Failed to create 'fakesink' element!");
89
90   gst_bin_add_many (GST_BIN (pipeline), src, sep, id3demux, sink, NULL);
91
92   fail_unless (gst_element_link (src, sep));
93   fail_unless (gst_element_link (sep, id3demux));
94
95   /* can't link id3demux and sink yet, do that later */
96   g_signal_connect (id3demux, "pad-added", G_CALLBACK (pad_added_cb), pipeline);
97
98   path = g_build_filename (GST_TEST_FILES_PATH, file, NULL);
99   GST_LOG ("reading file '%s'", path);
100   g_object_set (src, "location", path, NULL);
101
102   state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
103   fail_unless (state_ret != GST_STATE_CHANGE_FAILURE);
104
105   if (state_ret == GST_STATE_CHANGE_ASYNC) {
106     GST_LOG ("waiting for pipeline to reach PAUSED state");
107     state_ret = gst_element_get_state (pipeline, NULL, NULL, -1);
108     fail_unless_equals_int (state_ret, GST_STATE_CHANGE_SUCCESS);
109   }
110
111   GST_LOG ("PAUSED, let's retrieve our tags");
112
113   msg = gst_bus_poll (bus, GST_MESSAGE_TAG, -1);
114   fail_unless (msg != NULL, "Expected TAG message on bus! (%s)", file);
115
116   gst_message_parse_tag (msg, &tags);
117   fail_unless (tags != NULL, "TAG message did not contain taglist! (%s)", file);
118
119   gst_message_unref (msg);
120   gst_object_unref (bus);
121
122   fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_NULL),
123       GST_STATE_CHANGE_SUCCESS);
124   gst_object_unref (pipeline);
125
126   g_free (path);
127
128   GST_INFO ("%s: tags = %" GST_PTR_FORMAT, file, tags);
129   return tags;
130 }
131
132 static void
133 run_check_for_file (const gchar * filename, CheckTagsFunc * check_func)
134 {
135   GstTagList *tags;
136
137   /* first, pull-based */
138   tags = read_tags_from_file (filename, FALSE);
139   fail_unless (tags != NULL, "Failed to extract tags from '%s'", filename);
140   check_func (tags, filename);
141   gst_tag_list_free (tags);
142
143   /* FIXME: need to fix id3demux for short content in push mode */
144 #if 0
145   /* now try push-based */
146   tags = read_tags_from_file (filename, TRUE);
147   fail_unless (tags != NULL, "Failed to extract tags from '%s'", filename);
148   check_func (tags, filename);
149   gst_tag_list_free (tags);
150 #endif
151 }
152
153 static void
154 check_date_1977_06_23 (const GstTagList * tags, const gchar * file)
155 {
156   GDate *date = NULL;
157
158   gst_tag_list_get_date (tags, GST_TAG_DATE, &date);
159   fail_unless (date != NULL, "Tags from %s should contain a GST_TAG_DATE tag");
160   fail_unless_equals_int (g_date_get_year (date), 1977);
161   fail_unless_equals_int (g_date_get_month (date), 6);
162   fail_unless_equals_int (g_date_get_day (date), 23);
163   g_date_free (date);
164 }
165
166 GST_START_TEST (test_tdat_tyer)
167 {
168   run_check_for_file ("id3-407349-1.tag", check_date_1977_06_23);
169   run_check_for_file ("id3-407349-2.tag", check_date_1977_06_23);
170 }
171
172 GST_END_TEST;
173
174 static void
175 check_wcop (const GstTagList * tags, const gchar * file)
176 {
177   gchar *copyright = NULL;
178   gchar *uri = NULL;
179
180   fail_unless (gst_tag_list_get_string (tags, GST_TAG_LICENSE_URI, &uri));
181   fail_unless (uri != NULL);
182   fail_unless_equals_string (uri,
183       "http://creativecommons.org/licenses/by/3.0/");
184   g_free (uri);
185
186   fail_unless (gst_tag_list_get_string (tags, GST_TAG_COPYRIGHT, &copyright));
187   fail_unless (copyright != NULL);
188   fail_unless_equals_string (copyright,
189       " Steadman. Licensed to the public under http://creativecommons.org/licenses/by/3.0/ verify at http://test.com");
190   g_free (copyright);
191 }
192
193 GST_START_TEST (test_wcop)
194 {
195   run_check_for_file ("id3-447000-wcop.tag", check_wcop);
196 }
197
198 GST_END_TEST;
199
200 static void
201 check_unsync_v23 (const GstTagList * tags, const gchar * file)
202 {
203   gchar *album = NULL;
204   gchar *title = NULL;
205   gchar *artist = NULL;
206
207   fail_unless (gst_tag_list_get_string (tags, GST_TAG_TITLE, &title));
208   fail_unless (title != NULL);
209   fail_unless_equals_string (title, "ARTIST");  /* sic */
210   g_free (title);
211
212   fail_unless (gst_tag_list_get_string (tags, GST_TAG_ALBUM, &album));
213   fail_unless (album != NULL);
214   fail_unless_equals_string (album, "Album");
215   g_free (album);
216
217   fail_unless (gst_tag_list_get_string (tags, GST_TAG_ARTIST, &artist));
218   fail_unless (artist != NULL);
219   fail_unless_equals_string (artist, "藝人");
220   g_free (artist);
221 }
222
223 GST_START_TEST (test_unsync_v23)
224 {
225   run_check_for_file ("id3-577468-unsynced-tag.tag", check_unsync_v23);
226 }
227
228 GST_END_TEST;
229
230 static void
231 check_unsync_v24 (const GstTagList * tags, const gchar * file)
232 {
233   const GValue *val;
234   GstBuffer *buf;
235   gchar *album = NULL;
236   gchar *title = NULL;
237   gchar *artist = NULL;
238
239   fail_unless (gst_tag_list_get_string (tags, GST_TAG_TITLE, &title));
240   fail_unless (title != NULL);
241   fail_unless_equals_string (title, "Starlight");
242   g_free (title);
243
244   fail_unless (gst_tag_list_get_string (tags, GST_TAG_ALBUM, &album));
245   fail_unless (album != NULL);
246   fail_unless_equals_string (album, "L'albumRockVol.4 CD1");
247   g_free (album);
248
249   fail_unless (gst_tag_list_get_string (tags, GST_TAG_ARTIST, &artist));
250   fail_unless (artist != NULL);
251   fail_unless_equals_string (artist, "Muse");
252   g_free (artist);
253
254   val = gst_tag_list_get_value_index (tags, GST_TAG_IMAGE, 0);
255   fail_unless (val != NULL);
256   fail_unless (GST_VALUE_HOLDS_BUFFER (val));
257   buf = gst_value_get_buffer (val);
258   fail_unless (buf != NULL);
259   fail_unless (GST_BUFFER_CAPS (buf) != NULL);
260   fail_unless_equals_int (GST_BUFFER_SIZE (buf), 38022);
261   /* check for jpeg start/end markers */
262   fail_unless_equals_int (GST_BUFFER_DATA (buf)[0], 0xff);
263   fail_unless_equals_int (GST_BUFFER_DATA (buf)[1], 0xd8);
264   fail_unless_equals_int (GST_BUFFER_DATA (buf)[38020], 0xff);
265   fail_unless_equals_int (GST_BUFFER_DATA (buf)[38021], 0xd9);
266 }
267
268 GST_START_TEST (test_unsync_v24)
269 {
270   run_check_for_file ("id3-588148-unsynced-v24.tag", check_unsync_v24);
271 }
272
273 GST_END_TEST;
274
275 static Suite *
276 id3demux_suite (void)
277 {
278   Suite *s = suite_create ("id3demux");
279   TCase *tc_chain = tcase_create ("general");
280
281   suite_add_tcase (s, tc_chain);
282   tcase_add_test (tc_chain, test_tdat_tyer);
283   tcase_add_test (tc_chain, test_wcop);
284   tcase_add_test (tc_chain, test_unsync_v23);
285   tcase_add_test (tc_chain, test_unsync_v24);
286
287   return s;
288 }
289
290 GST_CHECK_MAIN (id3demux)