3 * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
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.
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.
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.
22 #include <sys/types.h>
26 #include <gst/check/gstcheck.h>
28 static gboolean have_eos = FALSE;
29 static GCond *eos_cond;
30 static GMutex *event_mutex;
32 static GstPad *mysinkpad;
34 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
40 event_func (GstPad * pad, GstEvent * event)
44 g_mutex_lock (event_mutex);
45 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
47 GST_DEBUG ("signal EOS");
48 g_cond_broadcast (eos_cond);
50 g_mutex_unlock (event_mutex);
52 gst_event_unref (event);
60 g_mutex_lock (event_mutex);
61 GST_DEBUG ("waiting for EOS");
63 g_cond_wait (eos_cond, event_mutex);
65 GST_DEBUG ("received EOS");
66 g_mutex_unlock (event_mutex);
74 GST_DEBUG ("setup_filesrc");
75 filesrc = gst_check_setup_element ("filesrc");
76 mysinkpad = gst_check_setup_sink_pad (filesrc, &sinktemplate, NULL);
77 gst_pad_set_event_function (mysinkpad, event_func);
78 gst_pad_set_active (mysinkpad, TRUE);
80 eos_cond = g_cond_new ();
81 event_mutex = g_mutex_new ();
87 cleanup_filesrc (GstElement * filesrc)
89 gst_pad_set_active (mysinkpad, FALSE);
90 gst_check_teardown_sink_pad (filesrc);
91 gst_check_teardown_element (filesrc);
93 g_cond_free (eos_cond);
94 g_mutex_free (event_mutex);
97 GST_START_TEST (test_seeking)
100 GstQuery *seeking_query;
104 #error TESTFILE not defined
106 src = setup_filesrc ();
108 g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
109 fail_unless (gst_element_set_state (src,
110 GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
111 "could not set to paused");
113 /* Test that filesrc is seekable with a file fd */
114 fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
116 fail_unless (gst_element_query (src, seeking_query) == TRUE);
117 gst_query_parse_seeking (seeking_query, NULL, &seekable, NULL, NULL);
118 fail_unless (seekable == TRUE);
119 gst_query_unref (seeking_query);
121 fail_unless (gst_element_set_state (src,
122 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
125 cleanup_filesrc (src);
130 GST_START_TEST (test_reverse)
135 #error TESTFILE not defined
137 src = setup_filesrc ();
139 g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
140 /* we're going to perform the seek in ready */
141 fail_unless (gst_element_set_state (src,
142 GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS,
143 "could not set to ready");
145 /* reverse seek from end to start */
146 gst_element_seek (src, -1.0, GST_FORMAT_BYTES, 0, GST_SEEK_TYPE_SET, 100,
147 GST_SEEK_TYPE_SET, -1);
149 fail_unless (gst_element_set_state (src,
150 GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
151 "could not set to paused");
156 fail_unless (gst_element_set_state (src,
157 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
160 cleanup_filesrc (src);
165 GST_START_TEST (test_pull)
168 GstQuery *seeking_query;
169 gboolean res, seekable;
173 GstBuffer *buffer1, *buffer2;
174 guint8 *data1, *data2;
177 src = setup_filesrc ();
179 g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
180 fail_unless (gst_element_set_state (src,
181 GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS,
182 "could not set to ready");
184 /* get the source pad */
185 pad = gst_element_get_static_pad (src, "src");
186 fail_unless (pad != NULL);
188 /* activate the pad in pull mode */
189 res = gst_pad_activate_pull (pad, TRUE);
190 fail_unless (res == TRUE);
192 /* not start playing */
193 fail_unless (gst_element_set_state (src,
194 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
195 "could not set to paused");
197 /* Test that filesrc is seekable with a file fd */
198 fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
200 fail_unless (gst_element_query (src, seeking_query) == TRUE);
202 /* get the seeking capabilities */
203 gst_query_parse_seeking (seeking_query, NULL, &seekable, &start, &stop);
204 fail_unless (seekable == TRUE);
205 fail_unless (start == 0);
206 fail_unless (start != -1);
207 gst_query_unref (seeking_query);
210 ret = gst_pad_get_range (pad, 0, 100, &buffer1);
211 fail_unless (ret == GST_FLOW_OK);
212 fail_unless (buffer1 != NULL);
213 fail_unless (gst_buffer_get_size (buffer1) == 100);
215 ret = gst_pad_get_range (pad, 0, 50, &buffer2);
216 fail_unless (ret == GST_FLOW_OK);
217 fail_unless (buffer2 != NULL);
218 fail_unless (gst_buffer_get_size (buffer2) == 50);
220 /* this should be the same */
221 data1 = gst_buffer_map (buffer1, &size1, NULL, GST_MAP_READ);
222 data2 = gst_buffer_map (buffer2, &size2, NULL, GST_MAP_READ);
223 fail_unless (memcmp (data1, data2, 50) == 0);
224 gst_buffer_unmap (buffer2, data2, size2);
226 gst_buffer_unref (buffer2);
228 /* read next 50 bytes */
229 ret = gst_pad_get_range (pad, 50, 50, &buffer2);
230 fail_unless (ret == GST_FLOW_OK);
231 fail_unless (buffer2 != NULL);
232 fail_unless (gst_buffer_get_size (buffer2) == 50);
234 /* compare with previously read data */
235 data2 = gst_buffer_map (buffer2, &size2, NULL, GST_MAP_READ);
236 fail_unless (memcmp (data1 + 50, data2, 50) == 0);
237 gst_buffer_unmap (buffer2, data2, size2);
239 gst_buffer_unmap (buffer1, data1, size1);
240 gst_buffer_unref (buffer1);
241 gst_buffer_unref (buffer2);
243 /* read 10 bytes at end-10 should give exactly 10 bytes */
244 ret = gst_pad_get_range (pad, stop - 10, 10, &buffer1);
245 fail_unless (ret == GST_FLOW_OK);
246 fail_unless (buffer1 != NULL);
247 fail_unless (gst_buffer_get_size (buffer1) == 10);
248 gst_buffer_unref (buffer1);
250 /* read 20 bytes at end-10 should give exactly 10 bytes */
251 ret = gst_pad_get_range (pad, stop - 10, 20, &buffer1);
252 fail_unless (ret == GST_FLOW_OK);
253 fail_unless (buffer1 != NULL);
254 fail_unless (gst_buffer_get_size (buffer1) == 10);
255 gst_buffer_unref (buffer1);
257 /* read 0 bytes at end-1 should return 0 bytes */
258 ret = gst_pad_get_range (pad, stop - 1, 0, &buffer1);
259 fail_unless (ret == GST_FLOW_OK);
260 fail_unless (buffer1 != NULL);
261 fail_unless (gst_buffer_get_size (buffer1) == 0);
262 gst_buffer_unref (buffer1);
264 /* read 10 bytes at end-1 should return 1 byte */
265 ret = gst_pad_get_range (pad, stop - 1, 10, &buffer1);
266 fail_unless (ret == GST_FLOW_OK);
267 fail_unless (buffer1 != NULL);
268 fail_unless (gst_buffer_get_size (buffer1) == 1);
269 gst_buffer_unref (buffer1);
271 /* read 0 bytes at end should EOS */
272 ret = gst_pad_get_range (pad, stop, 0, &buffer1);
273 fail_unless (ret == GST_FLOW_EOS);
275 /* read 10 bytes before end should EOS */
276 ret = gst_pad_get_range (pad, stop, 10, &buffer1);
277 fail_unless (ret == GST_FLOW_EOS);
279 /* read 0 bytes after end should EOS */
280 ret = gst_pad_get_range (pad, stop + 10, 0, &buffer1);
281 fail_unless (ret == GST_FLOW_EOS);
283 /* read 10 bytes after end should EOS too */
284 ret = gst_pad_get_range (pad, stop + 10, 10, &buffer1);
285 fail_unless (ret == GST_FLOW_EOS);
287 fail_unless (gst_element_set_state (src,
288 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
291 gst_object_unref (pad);
292 cleanup_filesrc (src);
297 GST_START_TEST (test_coverage)
304 src = setup_filesrc ();
305 bus = gst_bus_new ();
307 gst_element_set_bus (src, bus);
309 g_object_set (G_OBJECT (src), "location", "/i/do/not/exist", NULL);
310 g_object_get (G_OBJECT (src), "location", &location, NULL);
311 fail_unless_equals_string (location, "/i/do/not/exist");
313 fail_unless (gst_element_set_state (src,
314 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE,
315 "could set to playing with wrong location");
317 /* a state change and an error */
318 fail_if ((message = gst_bus_pop (bus)) == NULL);
319 gst_message_unref (message);
320 fail_if ((message = gst_bus_pop (bus)) == NULL);
321 fail_unless_message_error (message, RESOURCE, NOT_FOUND);
322 gst_message_unref (message);
324 g_object_set (G_OBJECT (src), "location", NULL, NULL);
325 g_object_get (G_OBJECT (src), "location", &location, NULL);
329 gst_element_set_bus (src, NULL);
330 gst_object_unref (GST_OBJECT (bus));
331 cleanup_filesrc (src);
336 GST_START_TEST (test_uri_interface)
342 src = setup_filesrc ();
343 bus = gst_bus_new ();
345 gst_element_set_bus (src, bus);
347 g_object_set (G_OBJECT (src), "location", "/i/do/not/exist", NULL);
348 g_object_get (G_OBJECT (src), "location", &location, NULL);
349 fail_unless_equals_string (location, "/i/do/not/exist");
352 location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
353 fail_unless_equals_string (location, "file:///i/do/not/exist");
356 /* should accept file:///foo/bar URIs */
357 fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
358 "file:///foo/bar", NULL));
359 location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
360 fail_unless_equals_string (location, "file:///foo/bar");
362 g_object_get (G_OBJECT (src), "location", &location, NULL);
363 fail_unless_equals_string (location, "/foo/bar");
366 /* should accept file://localhost/foo/bar URIs */
367 fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
368 "file://localhost/foo/baz", NULL));
369 location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
370 fail_unless_equals_string (location, "file:///foo/baz");
372 g_object_get (G_OBJECT (src), "location", &location, NULL);
373 fail_unless_equals_string (location, "/foo/baz");
376 /* should escape non-uri characters for the URI but not for the location */
377 g_object_set (G_OBJECT (src), "location", "/foo/b?r", NULL);
378 g_object_get (G_OBJECT (src), "location", &location, NULL);
379 fail_unless_equals_string (location, "/foo/b?r");
381 location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
382 fail_unless_equals_string (location, "file:///foo/b%3Fr");
385 /* should fail with other hostnames */
386 fail_if (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
387 "file://hostname/foo/foo", NULL));
390 gst_element_set_bus (src, NULL);
391 gst_object_unref (GST_OBJECT (bus));
392 cleanup_filesrc (src);
398 check_uri_for_uri (GstElement * e, const gchar * in_uri, const gchar * uri)
401 gchar *query_uri = NULL;
403 gst_uri_handler_set_uri (GST_URI_HANDLER (e), in_uri, NULL);
405 query = gst_query_new_uri ();
406 fail_unless (gst_element_query (e, query));
407 gst_query_parse_uri (query, &query_uri);
408 gst_query_unref (query);
411 fail_unless_equals_string (query_uri, uri);
415 fail_unless (gst_uri_is_valid (query_uri));
416 fn = g_filename_from_uri (query_uri, NULL, NULL);
417 fail_unless (g_path_is_absolute (fn));
418 fail_unless (fn != NULL);
426 check_uri_for_location (GstElement * e, const gchar * location,
430 gchar *query_uri = NULL;
432 g_object_set (e, "location", location, NULL);
433 query = gst_query_new_uri ();
434 fail_unless (gst_element_query (e, query));
435 gst_query_parse_uri (query, &query_uri);
436 gst_query_unref (query);
439 fail_unless_equals_string (query_uri, uri);
443 fail_unless (gst_uri_is_valid (query_uri));
444 fn = g_filename_from_uri (query_uri, NULL, NULL);
445 fail_unless (g_path_is_absolute (fn));
446 fail_unless (fn != NULL);
453 GST_START_TEST (test_uri_query)
457 src = setup_filesrc ();
462 check_uri_for_location (src, "/i/do/not/exist", "file:///i/do/not/exist");
463 check_uri_for_location (src, "/i/do/not/../exist", "file:///i/do/exist");
464 check_uri_for_location (src, "/i/do/not/.././exist", "file:///i/do/exist");
465 check_uri_for_location (src, "/i/./do/not/../exist", "file:///i/do/exist");
466 check_uri_for_location (src, "/i/do/./not/../exist", "file:///i/do/exist");
467 check_uri_for_location (src, "/i/do/not/./../exist", "file:///i/do/exist");
468 check_uri_for_location (src, "/i/./do/./././././exist",
469 "file:///i/do/exist");
470 check_uri_for_location (src, "/i/do/not/../../exist", "file:///i/exist");
471 check_uri_for_location (src, "/i/../not/../exist", "file:///exist");
472 /* hard to test relative URIs, just make sure it returns an URI of sorts */
473 check_uri_for_location (src, "foo", NULL);
474 check_uri_for_location (src, "foo/../bar", NULL);
475 check_uri_for_location (src, "./foo", NULL);
476 check_uri_for_location (src, "../foo", NULL);
477 check_uri_for_location (src, "foo/./bar", NULL);
478 /* make sure non-ASCII characters are escaped properly (U+00F6 here) */
479 check_uri_for_location (src, "/i/./d\303\266/not/../exist",
480 "file:///i/d%C3%B6/exist");
481 /* let's see what happens if we set a malformed URI with ISO-8859-1 chars,
482 * i.e. one that the input characters haven't been escaped properly. We
483 * should get back a properly escaped URI */
484 check_uri_for_uri (src, "file:///M\366t\366r", "file:///M%F6t%F6r");
488 cleanup_filesrc (src);
496 Suite *s = suite_create ("filesrc");
497 TCase *tc_chain = tcase_create ("general");
499 suite_add_tcase (s, tc_chain);
500 tcase_add_test (tc_chain, test_seeking);
501 tcase_add_test (tc_chain, test_reverse);
502 tcase_add_test (tc_chain, test_pull);
503 tcase_add_test (tc_chain, test_coverage);
504 tcase_add_test (tc_chain, test_uri_interface);
505 tcase_add_test (tc_chain, test_uri_query);
510 GST_CHECK_MAIN (filesrc);