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