• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <gst/check/gstcheck.h>
25 
26 static gboolean have_eos = FALSE;
27 static GCond eos_cond;
28 static GMutex event_mutex;
29 
30 static GstPad *mysinkpad;
31 
32 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
33     GST_PAD_SINK,
34     GST_PAD_ALWAYS,
35     GST_STATIC_CAPS_ANY);
36 
37 static gboolean
event_func(GstPad * pad,GstObject * parent,GstEvent * event)38 event_func (GstPad * pad, GstObject * parent, GstEvent * event)
39 {
40   gboolean res = TRUE;
41 
42   g_mutex_lock (&event_mutex);
43   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
44     have_eos = TRUE;
45     GST_DEBUG ("signal EOS");
46     g_cond_broadcast (&eos_cond);
47   }
48   g_mutex_unlock (&event_mutex);
49 
50   gst_event_unref (event);
51 
52   return res;
53 }
54 
55 static void
wait_eos(void)56 wait_eos (void)
57 {
58   g_mutex_lock (&event_mutex);
59   GST_DEBUG ("waiting for EOS");
60   while (!have_eos) {
61     g_cond_wait (&eos_cond, &event_mutex);
62   }
63   GST_DEBUG ("received EOS");
64   g_mutex_unlock (&event_mutex);
65 }
66 
67 static GstElement *
setup_filesrc(void)68 setup_filesrc (void)
69 {
70   GstElement *filesrc;
71 
72   g_cond_init (&eos_cond);
73   g_mutex_init (&event_mutex);
74 
75   GST_DEBUG ("setup_filesrc");
76   filesrc = gst_check_setup_element ("filesrc");
77   mysinkpad = gst_check_setup_sink_pad (filesrc, &sinktemplate);
78   gst_pad_set_event_function (mysinkpad, event_func);
79   gst_pad_set_active (mysinkpad, TRUE);
80 
81   return filesrc;
82 }
83 
84 static void
cleanup_filesrc(GstElement * filesrc)85 cleanup_filesrc (GstElement * filesrc)
86 {
87   gst_check_drop_buffers ();
88   gst_pad_set_active (mysinkpad, FALSE);
89   gst_check_teardown_sink_pad (filesrc);
90   gst_check_teardown_element (filesrc);
91 
92   g_mutex_clear (&event_mutex);
93   g_cond_clear (&eos_cond);
94 }
95 
GST_START_TEST(test_seeking)96 GST_START_TEST (test_seeking)
97 {
98   GstElement *src;
99   GstQuery *seeking_query;
100   gboolean seekable;
101 
102 #ifndef TESTFILE
103 #error TESTFILE not defined
104 #endif
105   src = setup_filesrc ();
106 
107   g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
108   fail_unless (gst_element_set_state (src,
109           GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
110       "could not set to paused");
111 
112   /* Test that filesrc is seekable with a file fd */
113   fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
114       != NULL);
115   fail_unless (gst_element_query (src, seeking_query) == TRUE);
116   gst_query_parse_seeking (seeking_query, NULL, &seekable, NULL, NULL);
117   fail_unless (seekable == TRUE);
118   gst_query_unref (seeking_query);
119 
120   fail_unless (gst_element_set_state (src,
121           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
122 
123   /* cleanup */
124   cleanup_filesrc (src);
125 }
126 
127 GST_END_TEST;
128 
GST_START_TEST(test_reverse)129 GST_START_TEST (test_reverse)
130 {
131   GstElement *src;
132 
133 #ifndef TESTFILE
134 #error TESTFILE not defined
135 #endif
136   src = setup_filesrc ();
137 
138   g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
139   /* we're going to perform the seek in ready */
140   fail_unless (gst_element_set_state (src,
141           GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS,
142       "could not set to ready");
143 
144   /* reverse seek from end to start */
145   gst_element_seek (src, -1.0, GST_FORMAT_BYTES, 0, GST_SEEK_TYPE_SET, 100,
146       GST_SEEK_TYPE_SET, -1);
147 
148   fail_unless (gst_element_set_state (src,
149           GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
150       "could not set to paused");
151 
152   /* wait for EOS */
153   wait_eos ();
154 
155   fail_unless (gst_element_set_state (src,
156           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
157 
158   /* cleanup */
159   cleanup_filesrc (src);
160 }
161 
162 GST_END_TEST;
163 
GST_START_TEST(test_pull)164 GST_START_TEST (test_pull)
165 {
166   GstElement *src;
167   GstQuery *seeking_query;
168   gboolean res, seekable;
169   gint64 start, stop;
170   GstPad *pad;
171   GstFlowReturn ret;
172   GstBuffer *buffer1, *buffer2;
173   GstMapInfo info1, info2;
174 
175   src = setup_filesrc ();
176 
177   g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
178   fail_unless (gst_element_set_state (src,
179           GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS,
180       "could not set to ready");
181 
182   /* get the source pad */
183   pad = gst_element_get_static_pad (src, "src");
184   fail_unless (pad != NULL);
185 
186   /* activate the pad in pull mode */
187   res = gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE);
188   fail_unless (res == TRUE);
189 
190   /* not start playing */
191   fail_unless (gst_element_set_state (src,
192           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
193       "could not set to paused");
194 
195   /* Test that filesrc is seekable with a file fd */
196   fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
197       != NULL);
198   fail_unless (gst_element_query (src, seeking_query) == TRUE);
199 
200   /* get the seeking capabilities */
201   gst_query_parse_seeking (seeking_query, NULL, &seekable, &start, &stop);
202   fail_unless (seekable == TRUE);
203   fail_unless (start == 0);
204   fail_unless (start != -1);
205   gst_query_unref (seeking_query);
206 
207   /* do some pulls */
208   buffer1 = NULL;
209   ret = gst_pad_get_range (pad, 0, 100, &buffer1);
210   fail_unless (ret == GST_FLOW_OK);
211   fail_unless (buffer1 != NULL);
212   fail_unless (gst_buffer_get_size (buffer1) == 100);
213 
214   buffer2 = NULL;
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);
219 
220   /* this should be the same */
221   fail_unless (gst_buffer_map (buffer1, &info1, GST_MAP_READ));
222   fail_unless (gst_buffer_map (buffer2, &info2, GST_MAP_READ));
223   fail_unless (memcmp (info1.data, info2.data, 50) == 0);
224   gst_buffer_unmap (buffer2, &info2);
225 
226   gst_buffer_unref (buffer2);
227 
228   /* read next 50 bytes */
229   buffer2 = NULL;
230   ret = gst_pad_get_range (pad, 50, 50, &buffer2);
231   fail_unless (ret == GST_FLOW_OK);
232   fail_unless (buffer2 != NULL);
233   fail_unless (gst_buffer_get_size (buffer2) == 50);
234 
235   /* compare with previously read data */
236   fail_unless (gst_buffer_map (buffer2, &info2, GST_MAP_READ));
237   fail_unless (memcmp ((guint8 *) info1.data + 50, info2.data, 50) == 0);
238   gst_buffer_unmap (buffer2, &info2);
239 
240   gst_buffer_unmap (buffer1, &info1);
241   gst_buffer_unref (buffer1);
242   gst_buffer_unref (buffer2);
243 
244   /* read 10 bytes at end-10 should give exactly 10 bytes */
245   buffer1 = NULL;
246   ret = gst_pad_get_range (pad, stop - 10, 10, &buffer1);
247   fail_unless (ret == GST_FLOW_OK);
248   fail_unless (buffer1 != NULL);
249   fail_unless (gst_buffer_get_size (buffer1) == 10);
250   gst_buffer_unref (buffer1);
251 
252   /* read 20 bytes at end-10 should give exactly 10 bytes */
253   buffer1 = NULL;
254   ret = gst_pad_get_range (pad, stop - 10, 20, &buffer1);
255   fail_unless (ret == GST_FLOW_OK);
256   fail_unless (buffer1 != NULL);
257   fail_unless (gst_buffer_get_size (buffer1) == 10);
258   gst_buffer_unref (buffer1);
259 
260   /* read 0 bytes at end-1 should return 0 bytes */
261   buffer1 = NULL;
262   ret = gst_pad_get_range (pad, stop - 1, 0, &buffer1);
263   fail_unless (ret == GST_FLOW_OK);
264   fail_unless (buffer1 != NULL);
265   fail_unless (gst_buffer_get_size (buffer1) == 0);
266   gst_buffer_unref (buffer1);
267 
268   /* read 10 bytes at end-1 should return 1 byte */
269   buffer1 = NULL;
270   ret = gst_pad_get_range (pad, stop - 1, 10, &buffer1);
271   fail_unless (ret == GST_FLOW_OK);
272   fail_unless (buffer1 != NULL);
273   fail_unless (gst_buffer_get_size (buffer1) == 1);
274   gst_buffer_unref (buffer1);
275 
276   /* read 0 bytes at end should EOS */
277   buffer1 = NULL;
278   ret = gst_pad_get_range (pad, stop, 0, &buffer1);
279   fail_unless (ret == GST_FLOW_EOS);
280 
281   /* read 10 bytes before end should EOS */
282   buffer1 = NULL;
283   ret = gst_pad_get_range (pad, stop, 10, &buffer1);
284   fail_unless (ret == GST_FLOW_EOS);
285 
286   /* read 0 bytes after end should EOS */
287   buffer1 = NULL;
288   ret = gst_pad_get_range (pad, stop + 10, 0, &buffer1);
289   fail_unless (ret == GST_FLOW_EOS);
290 
291   /* read 10 bytes after end should EOS too */
292   buffer1 = NULL;
293   ret = gst_pad_get_range (pad, stop + 10, 10, &buffer1);
294   fail_unless (ret == GST_FLOW_EOS);
295 
296   fail_unless (gst_element_set_state (src,
297           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
298 
299   /* cleanup */
300   gst_object_unref (pad);
301   cleanup_filesrc (src);
302 }
303 
304 GST_END_TEST;
305 
GST_START_TEST(test_coverage)306 GST_START_TEST (test_coverage)
307 {
308   GstElement *src;
309   gchar *location;
310   GstBus *bus;
311   GstMessage *message;
312 
313   src = setup_filesrc ();
314   bus = gst_bus_new ();
315 
316   gst_element_set_bus (src, bus);
317 
318   g_object_set (G_OBJECT (src), "location", "/i/do/not/exist", NULL);
319   g_object_get (G_OBJECT (src), "location", &location, NULL);
320   fail_unless_equals_string (location, "/i/do/not/exist");
321   g_free (location);
322   fail_unless (gst_element_set_state (src,
323           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE,
324       "could set to playing with wrong location");
325 
326   /* a state change and an error */
327   fail_if ((message = gst_bus_pop (bus)) == NULL);
328   gst_message_unref (message);
329   fail_if ((message = gst_bus_pop (bus)) == NULL);
330   fail_unless_message_error (message, RESOURCE, NOT_FOUND);
331   gst_message_unref (message);
332 
333   g_object_set (G_OBJECT (src), "location", NULL, NULL);
334   g_object_get (G_OBJECT (src), "location", &location, NULL);
335   fail_if (location);
336 
337   /* cleanup */
338   gst_element_set_bus (src, NULL);
339   gst_object_unref (GST_OBJECT (bus));
340   cleanup_filesrc (src);
341 }
342 
343 GST_END_TEST;
344 
GST_START_TEST(test_uri_interface)345 GST_START_TEST (test_uri_interface)
346 {
347   GstElement *src;
348   gchar *location;
349   GstBus *bus;
350   GstPad *pad;
351 
352   src = setup_filesrc ();
353   bus = gst_bus_new ();
354 
355   gst_element_set_bus (src, bus);
356 
357   g_object_set (G_OBJECT (src), "location", NULL, NULL);
358   g_object_get (G_OBJECT (src), "location", &location, NULL);
359   fail_unless (location == NULL);
360 
361   g_object_set (G_OBJECT (src), "location", "/i/do/not/exist", NULL);
362   g_object_get (G_OBJECT (src), "location", &location, NULL);
363   fail_unless_equals_string (location, "/i/do/not/exist");
364   g_free (location);
365 
366   location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
367   fail_unless_equals_string (location, "file:///i/do/not/exist");
368   g_free (location);
369 
370 #define DSEP G_DIR_SEPARATOR_S
371 
372   /* should accept file:///foo/bar URIs */
373   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
374           "file:///foo/bar", NULL));
375   location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
376   fail_unless_equals_string (location, "file:///foo/bar");
377   g_free (location);
378   location = NULL;
379   g_object_get (G_OBJECT (src), "location", &location, NULL);
380   fail_unless_equals_string (location, DSEP "foo" DSEP "bar");
381   g_free (location);
382 
383 #ifdef G_OS_WIN32
384   /* should accept file:///c:/foo/bar.txt URIs */
385   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
386           "file:///c:/foo/bar", NULL));
387   location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
388   fail_unless_equals_string (location, "file:///c:/foo/bar");
389   g_free (location);
390   location = NULL;
391   g_object_get (G_OBJECT (src), "location", &location, NULL);
392   fail_unless_equals_string (location, "c:" DSEP "foo" DSEP "bar");
393   g_free (location);
394 #endif
395 
396   /* should accept file://localhost/foo/bar URIs */
397   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
398           "file://localhost/foo/baz", NULL));
399   location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
400   fail_unless_equals_string (location, "file:///foo/baz");
401   g_free (location);
402   location = NULL;
403   g_object_get (G_OBJECT (src), "location", &location, NULL);
404   fail_unless_equals_string (location, DSEP "foo" DSEP "baz");
405   g_free (location);
406 
407 #undef DSEP
408 
409   /* should escape non-uri characters for the URI but not for the location */
410   g_object_set (G_OBJECT (src), "location", "/foo/b?r", NULL);
411   g_object_get (G_OBJECT (src), "location", &location, NULL);
412   fail_unless_equals_string (location, "/foo/b?r");
413   g_free (location);
414   location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
415   fail_unless_equals_string (location, "file:///foo/b%3Fr");
416   g_free (location);
417 
418   /* should fail with other hostnames */
419   fail_if (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
420           "file://hostname/foo/foo", NULL));
421 
422   g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
423 
424   pad = gst_element_get_static_pad (src, "src");
425   fail_unless (pad != NULL);
426   fail_unless (gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE));
427   gst_object_unref (pad);
428 
429   fail_unless (gst_element_set_state (src,
430           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
431       "could not set to playing");
432 
433   ASSERT_WARNING (g_object_set (G_OBJECT (src), "location", "/wrong", NULL));
434   g_object_get (G_OBJECT (src), "location", &location, NULL);
435   fail_unless_equals_string (location, TESTFILE);
436   g_free (location);
437 
438   fail_unless (gst_element_set_state (src,
439           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
440 
441   /* cleanup */
442   gst_element_set_bus (src, NULL);
443   gst_object_unref (GST_OBJECT (bus));
444   cleanup_filesrc (src);
445 }
446 
447 GST_END_TEST;
448 
449 #ifdef G_OS_UNIX
450 static void
check_uri_for_uri(GstElement * e,const gchar * in_uri,const gchar * uri)451 check_uri_for_uri (GstElement * e, const gchar * in_uri, const gchar * uri)
452 {
453   GstQuery *query;
454   gchar *query_uri = NULL;
455 
456   gst_uri_handler_set_uri (GST_URI_HANDLER (e), in_uri, NULL);
457 
458   query = gst_query_new_uri ();
459   fail_unless (gst_element_query (e, query));
460   gst_query_parse_uri (query, &query_uri);
461   gst_query_unref (query);
462 
463   if (uri != NULL) {
464     fail_unless_equals_string (query_uri, uri);
465   } else {
466     gchar *fn;
467 
468     fail_unless (gst_uri_is_valid (query_uri));
469     fn = g_filename_from_uri (query_uri, NULL, NULL);
470     fail_unless (g_path_is_absolute (fn));
471     fail_unless (fn != NULL);
472     g_free (fn);
473   }
474 
475   g_free (query_uri);
476 }
477 
478 static void
check_uri_for_location(GstElement * e,const gchar * location,const gchar * uri)479 check_uri_for_location (GstElement * e, const gchar * location,
480     const gchar * uri)
481 {
482   GstQuery *query;
483   gchar *query_uri = NULL;
484 
485   g_object_set (e, "location", location, NULL);
486   query = gst_query_new_uri ();
487   fail_unless (gst_element_query (e, query));
488   gst_query_parse_uri (query, &query_uri);
489   gst_query_unref (query);
490 
491   if (uri != NULL) {
492     fail_unless_equals_string (query_uri, uri);
493   } else {
494     gchar *fn;
495 
496     fail_unless (gst_uri_is_valid (query_uri));
497     fn = g_filename_from_uri (query_uri, NULL, NULL);
498     fail_unless (g_path_is_absolute (fn));
499     fail_unless (fn != NULL);
500     g_free (fn);
501   }
502 
503   g_free (query_uri);
504 }
505 #endif
506 
GST_START_TEST(test_uri_query)507 GST_START_TEST (test_uri_query)
508 {
509   GstElement *src;
510 
511   src = setup_filesrc ();
512 
513 #ifdef G_OS_UNIX
514   {
515     GST_INFO ("*nix");
516     check_uri_for_location (src, "/i/do/not/exist", "file:///i/do/not/exist");
517     check_uri_for_location (src, "/i/do/not/../exist", "file:///i/do/exist");
518     check_uri_for_location (src, "/i/do/not/.././exist", "file:///i/do/exist");
519     check_uri_for_location (src, "/i/./do/not/../exist", "file:///i/do/exist");
520     check_uri_for_location (src, "/i/do/./not/../exist", "file:///i/do/exist");
521     check_uri_for_location (src, "/i/do/not/./../exist", "file:///i/do/exist");
522     check_uri_for_location (src, "/i/./do/./././././exist",
523         "file:///i/do/exist");
524     check_uri_for_location (src, "/i/do/not/../../exist", "file:///i/exist");
525     check_uri_for_location (src, "/i/../not/../exist", "file:///exist");
526     /* hard to test relative URIs, just make sure it returns an URI of sorts */
527     check_uri_for_location (src, "foo", NULL);
528     check_uri_for_location (src, "foo/../bar", NULL);
529     check_uri_for_location (src, "./foo", NULL);
530     check_uri_for_location (src, "../foo", NULL);
531     check_uri_for_location (src, "foo/./bar", NULL);
532     /* make sure non-ASCII characters are escaped properly (U+00F6 here) */
533     check_uri_for_location (src, "/i/./d\303\266/not/../exist",
534         "file:///i/d%C3%B6/exist");
535     /* let's see what happens if we set a malformed URI with ISO-8859-1 chars,
536      * i.e. one that the input characters haven't been escaped properly. We
537      * should get back a properly escaped URI */
538     check_uri_for_uri (src, "file:///M\366t\366r", "file:///M%F6t%F6r");
539   }
540 #endif
541 
542   cleanup_filesrc (src);
543 }
544 
545 GST_END_TEST;
546 
547 static Suite *
filesrc_suite(void)548 filesrc_suite (void)
549 {
550   Suite *s = suite_create ("filesrc");
551   TCase *tc_chain = tcase_create ("general");
552 
553   suite_add_tcase (s, tc_chain);
554   tcase_add_test (tc_chain, test_seeking);
555   tcase_add_test (tc_chain, test_reverse);
556   tcase_add_test (tc_chain, test_pull);
557   tcase_add_test (tc_chain, test_coverage);
558   tcase_add_test (tc_chain, test_uri_interface);
559   tcase_add_test (tc_chain, test_uri_query);
560 
561   return s;
562 }
563 
564 GST_CHECK_MAIN (filesrc);
565