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