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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, 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, GstObject * parent, 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);
77 gst_pad_set_event_function (mysinkpad, event_func);
78 gst_pad_set_active (mysinkpad, TRUE);
84 cleanup_filesrc (GstElement * filesrc)
86 gst_check_drop_buffers ();
87 gst_pad_set_active (mysinkpad, FALSE);
88 gst_check_teardown_sink_pad (filesrc);
89 gst_check_teardown_element (filesrc);
92 GST_START_TEST (test_seeking)
95 GstQuery *seeking_query;
99 #error TESTFILE not defined
101 src = setup_filesrc ();
103 g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
104 fail_unless (gst_element_set_state (src,
105 GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
106 "could not set to paused");
108 /* Test that filesrc is seekable with a file fd */
109 fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
111 fail_unless (gst_element_query (src, seeking_query) == TRUE);
112 gst_query_parse_seeking (seeking_query, NULL, &seekable, NULL, NULL);
113 fail_unless (seekable == TRUE);
114 gst_query_unref (seeking_query);
116 fail_unless (gst_element_set_state (src,
117 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
120 cleanup_filesrc (src);
125 GST_START_TEST (test_reverse)
130 #error TESTFILE not defined
132 src = setup_filesrc ();
134 g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
135 /* we're going to perform the seek in ready */
136 fail_unless (gst_element_set_state (src,
137 GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS,
138 "could not set to ready");
140 /* reverse seek from end to start */
141 gst_element_seek (src, -1.0, GST_FORMAT_BYTES, 0, GST_SEEK_TYPE_SET, 100,
142 GST_SEEK_TYPE_SET, -1);
144 fail_unless (gst_element_set_state (src,
145 GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
146 "could not set to paused");
151 fail_unless (gst_element_set_state (src,
152 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
155 cleanup_filesrc (src);
160 GST_START_TEST (test_pull)
163 GstQuery *seeking_query;
164 gboolean res, seekable;
168 GstBuffer *buffer1, *buffer2;
169 GstMapInfo info1, info2;
171 src = setup_filesrc ();
173 g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
174 fail_unless (gst_element_set_state (src,
175 GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS,
176 "could not set to ready");
178 /* get the source pad */
179 pad = gst_element_get_static_pad (src, "src");
180 fail_unless (pad != NULL);
182 /* activate the pad in pull mode */
183 res = gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE);
184 fail_unless (res == TRUE);
186 /* not start playing */
187 fail_unless (gst_element_set_state (src,
188 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
189 "could not set to paused");
191 /* Test that filesrc is seekable with a file fd */
192 fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
194 fail_unless (gst_element_query (src, seeking_query) == TRUE);
196 /* get the seeking capabilities */
197 gst_query_parse_seeking (seeking_query, NULL, &seekable, &start, &stop);
198 fail_unless (seekable == TRUE);
199 fail_unless (start == 0);
200 fail_unless (start != -1);
201 gst_query_unref (seeking_query);
205 ret = gst_pad_get_range (pad, 0, 100, &buffer1);
206 fail_unless (ret == GST_FLOW_OK);
207 fail_unless (buffer1 != NULL);
208 fail_unless (gst_buffer_get_size (buffer1) == 100);
211 ret = gst_pad_get_range (pad, 0, 50, &buffer2);
212 fail_unless (ret == GST_FLOW_OK);
213 fail_unless (buffer2 != NULL);
214 fail_unless (gst_buffer_get_size (buffer2) == 50);
216 /* this should be the same */
217 fail_unless (gst_buffer_map (buffer1, &info1, GST_MAP_READ));
218 fail_unless (gst_buffer_map (buffer2, &info2, GST_MAP_READ));
219 fail_unless (memcmp (info1.data, info2.data, 50) == 0);
220 gst_buffer_unmap (buffer2, &info2);
222 gst_buffer_unref (buffer2);
224 /* read next 50 bytes */
226 ret = gst_pad_get_range (pad, 50, 50, &buffer2);
227 fail_unless (ret == GST_FLOW_OK);
228 fail_unless (buffer2 != NULL);
229 fail_unless (gst_buffer_get_size (buffer2) == 50);
231 /* compare with previously read data */
232 fail_unless (gst_buffer_map (buffer2, &info2, GST_MAP_READ));
233 fail_unless (memcmp ((guint8 *) info1.data + 50, info2.data, 50) == 0);
234 gst_buffer_unmap (buffer2, &info2);
236 gst_buffer_unmap (buffer1, &info1);
237 gst_buffer_unref (buffer1);
238 gst_buffer_unref (buffer2);
240 /* read 10 bytes at end-10 should give exactly 10 bytes */
242 ret = gst_pad_get_range (pad, stop - 10, 10, &buffer1);
243 fail_unless (ret == GST_FLOW_OK);
244 fail_unless (buffer1 != NULL);
245 fail_unless (gst_buffer_get_size (buffer1) == 10);
246 gst_buffer_unref (buffer1);
248 /* read 20 bytes at end-10 should give exactly 10 bytes */
250 ret = gst_pad_get_range (pad, stop - 10, 20, &buffer1);
251 fail_unless (ret == GST_FLOW_OK);
252 fail_unless (buffer1 != NULL);
253 fail_unless (gst_buffer_get_size (buffer1) == 10);
254 gst_buffer_unref (buffer1);
256 /* 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 */
266 ret = gst_pad_get_range (pad, stop - 1, 10, &buffer1);
267 fail_unless (ret == GST_FLOW_OK);
268 fail_unless (buffer1 != NULL);
269 fail_unless (gst_buffer_get_size (buffer1) == 1);
270 gst_buffer_unref (buffer1);
272 /* read 0 bytes at end should EOS */
274 ret = gst_pad_get_range (pad, stop, 0, &buffer1);
275 fail_unless (ret == GST_FLOW_EOS);
277 /* read 10 bytes before end should EOS */
279 ret = gst_pad_get_range (pad, stop, 10, &buffer1);
280 fail_unless (ret == GST_FLOW_EOS);
282 /* read 0 bytes after end should EOS */
284 ret = gst_pad_get_range (pad, stop + 10, 0, &buffer1);
285 fail_unless (ret == GST_FLOW_EOS);
287 /* read 10 bytes after end should EOS too */
289 ret = gst_pad_get_range (pad, stop + 10, 10, &buffer1);
290 fail_unless (ret == GST_FLOW_EOS);
292 fail_unless (gst_element_set_state (src,
293 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
296 gst_object_unref (pad);
297 cleanup_filesrc (src);
302 GST_START_TEST (test_coverage)
309 src = setup_filesrc ();
310 bus = gst_bus_new ();
312 gst_element_set_bus (src, bus);
314 g_object_set (G_OBJECT (src), "location", "/i/do/not/exist", NULL);
315 g_object_get (G_OBJECT (src), "location", &location, NULL);
316 fail_unless_equals_string (location, "/i/do/not/exist");
318 fail_unless (gst_element_set_state (src,
319 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE,
320 "could set to playing with wrong location");
322 /* a state change and an error */
323 fail_if ((message = gst_bus_pop (bus)) == NULL);
324 gst_message_unref (message);
325 fail_if ((message = gst_bus_pop (bus)) == NULL);
326 fail_unless_message_error (message, RESOURCE, NOT_FOUND);
327 gst_message_unref (message);
329 g_object_set (G_OBJECT (src), "location", NULL, NULL);
330 g_object_get (G_OBJECT (src), "location", &location, NULL);
334 gst_element_set_bus (src, NULL);
335 gst_object_unref (GST_OBJECT (bus));
336 cleanup_filesrc (src);
341 GST_START_TEST (test_uri_interface)
348 src = setup_filesrc ();
349 bus = gst_bus_new ();
351 gst_element_set_bus (src, bus);
353 g_object_set (G_OBJECT (src), "location", NULL, NULL);
354 g_object_get (G_OBJECT (src), "location", &location, NULL);
355 fail_unless (location == NULL);
357 g_object_set (G_OBJECT (src), "location", "/i/do/not/exist", NULL);
358 g_object_get (G_OBJECT (src), "location", &location, NULL);
359 fail_unless_equals_string (location, "/i/do/not/exist");
362 location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
363 fail_unless_equals_string (location, "file:///i/do/not/exist");
366 /* should accept file:///foo/bar URIs */
367 fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
368 "file:///foo/bar", NULL));
369 location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
370 fail_unless_equals_string (location, "file:///foo/bar");
372 g_object_get (G_OBJECT (src), "location", &location, NULL);
373 fail_unless_equals_string (location, "/foo/bar");
376 /* should accept file://localhost/foo/bar URIs */
377 fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
378 "file://localhost/foo/baz", NULL));
379 location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
380 fail_unless_equals_string (location, "file:///foo/baz");
382 g_object_get (G_OBJECT (src), "location", &location, NULL);
383 fail_unless_equals_string (location, "/foo/baz");
386 /* should escape non-uri characters for the URI but not for the location */
387 g_object_set (G_OBJECT (src), "location", "/foo/b?r", NULL);
388 g_object_get (G_OBJECT (src), "location", &location, NULL);
389 fail_unless_equals_string (location, "/foo/b?r");
391 location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
392 fail_unless_equals_string (location, "file:///foo/b%3Fr");
395 /* should fail with other hostnames */
396 fail_if (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
397 "file://hostname/foo/foo", NULL));
399 g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
401 pad = gst_element_get_static_pad (src, "src");
402 fail_unless (pad != NULL);
403 fail_unless (gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE));
404 gst_object_unref (pad);
406 fail_unless (gst_element_set_state (src,
407 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
408 "could not set to playing");
410 ASSERT_WARNING (g_object_set (G_OBJECT (src), "location", "/wrong", NULL));
411 g_object_get (G_OBJECT (src), "location", &location, NULL);
412 fail_unless_equals_string (location, TESTFILE);
415 fail_unless (gst_element_set_state (src,
416 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
419 gst_element_set_bus (src, NULL);
420 gst_object_unref (GST_OBJECT (bus));
421 cleanup_filesrc (src);
428 check_uri_for_uri (GstElement * e, const gchar * in_uri, const gchar * uri)
431 gchar *query_uri = NULL;
433 gst_uri_handler_set_uri (GST_URI_HANDLER (e), in_uri, NULL);
435 query = gst_query_new_uri ();
436 fail_unless (gst_element_query (e, query));
437 gst_query_parse_uri (query, &query_uri);
438 gst_query_unref (query);
441 fail_unless_equals_string (query_uri, uri);
445 fail_unless (gst_uri_is_valid (query_uri));
446 fn = g_filename_from_uri (query_uri, NULL, NULL);
447 fail_unless (g_path_is_absolute (fn));
448 fail_unless (fn != NULL);
456 check_uri_for_location (GstElement * e, const gchar * location,
460 gchar *query_uri = NULL;
462 g_object_set (e, "location", location, NULL);
463 query = gst_query_new_uri ();
464 fail_unless (gst_element_query (e, query));
465 gst_query_parse_uri (query, &query_uri);
466 gst_query_unref (query);
469 fail_unless_equals_string (query_uri, uri);
473 fail_unless (gst_uri_is_valid (query_uri));
474 fn = g_filename_from_uri (query_uri, NULL, NULL);
475 fail_unless (g_path_is_absolute (fn));
476 fail_unless (fn != NULL);
484 GST_START_TEST (test_uri_query)
488 src = setup_filesrc ();
493 check_uri_for_location (src, "/i/do/not/exist", "file:///i/do/not/exist");
494 check_uri_for_location (src, "/i/do/not/../exist", "file:///i/do/exist");
495 check_uri_for_location (src, "/i/do/not/.././exist", "file:///i/do/exist");
496 check_uri_for_location (src, "/i/./do/not/../exist", "file:///i/do/exist");
497 check_uri_for_location (src, "/i/do/./not/../exist", "file:///i/do/exist");
498 check_uri_for_location (src, "/i/do/not/./../exist", "file:///i/do/exist");
499 check_uri_for_location (src, "/i/./do/./././././exist",
500 "file:///i/do/exist");
501 check_uri_for_location (src, "/i/do/not/../../exist", "file:///i/exist");
502 check_uri_for_location (src, "/i/../not/../exist", "file:///exist");
503 /* hard to test relative URIs, just make sure it returns an URI of sorts */
504 check_uri_for_location (src, "foo", NULL);
505 check_uri_for_location (src, "foo/../bar", NULL);
506 check_uri_for_location (src, "./foo", NULL);
507 check_uri_for_location (src, "../foo", NULL);
508 check_uri_for_location (src, "foo/./bar", NULL);
509 /* make sure non-ASCII characters are escaped properly (U+00F6 here) */
510 check_uri_for_location (src, "/i/./d\303\266/not/../exist",
511 "file:///i/d%C3%B6/exist");
512 /* let's see what happens if we set a malformed URI with ISO-8859-1 chars,
513 * i.e. one that the input characters haven't been escaped properly. We
514 * should get back a properly escaped URI */
515 check_uri_for_uri (src, "file:///M\366t\366r", "file:///M%F6t%F6r");
519 cleanup_filesrc (src);
527 Suite *s = suite_create ("filesrc");
528 TCase *tc_chain = tcase_create ("general");
530 suite_add_tcase (s, tc_chain);
531 tcase_add_test (tc_chain, test_seeking);
532 tcase_add_test (tc_chain, test_reverse);
533 tcase_add_test (tc_chain, test_pull);
534 tcase_add_test (tc_chain, test_coverage);
535 tcase_add_test (tc_chain, test_uri_interface);
536 tcase_add_test (tc_chain, test_uri_query);
541 GST_CHECK_MAIN (filesrc);