libs/gst/base/gstbasesrc.c: Don't update the last_stop position in do_seek, that...
[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 gboolean
40 event_func (GstPad * pad, 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 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 GstElement *
70 setup_filesrc ()
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, NULL);
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 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
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_pad (src, "src");
184   fail_unless (pad != NULL);
185
186   /* activate the pad in pull mode */
187   res = gst_pad_activate_pull (pad, 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   ret = gst_pad_get_range (pad, 0, 100, &buffer1);
209   fail_unless (ret == GST_FLOW_OK);
210   fail_unless (buffer1 != NULL);
211   fail_unless (GST_BUFFER_SIZE (buffer1) == 100);
212
213   ret = gst_pad_get_range (pad, 0, 50, &buffer2);
214   fail_unless (ret == GST_FLOW_OK);
215   fail_unless (buffer2 != NULL);
216   fail_unless (GST_BUFFER_SIZE (buffer2) == 50);
217
218   /* this should be the same */
219   fail_unless (memcmp (GST_BUFFER_DATA (buffer1), GST_BUFFER_DATA (buffer2),
220           50) == 0);
221
222   gst_buffer_unref (buffer2);
223
224   /* read next 50 bytes */
225   ret = gst_pad_get_range (pad, 50, 50, &buffer2);
226   fail_unless (ret == GST_FLOW_OK);
227   fail_unless (buffer2 != NULL);
228   fail_unless (GST_BUFFER_SIZE (buffer2) == 50);
229
230   /* compare with previously read data */
231   fail_unless (memcmp (GST_BUFFER_DATA (buffer1) + 50,
232           GST_BUFFER_DATA (buffer2), 50) == 0);
233
234   gst_buffer_unref (buffer1);
235   gst_buffer_unref (buffer2);
236
237   /* read 10 bytes at end-10 should give exactly 10 bytes */
238   ret = gst_pad_get_range (pad, stop - 10, 10, &buffer1);
239   fail_unless (ret == GST_FLOW_OK);
240   fail_unless (buffer1 != NULL);
241   fail_unless (GST_BUFFER_SIZE (buffer1) == 10);
242   gst_buffer_unref (buffer1);
243
244   /* read 20 bytes at end-10 should give exactly 10 bytes */
245   ret = gst_pad_get_range (pad, stop - 10, 20, &buffer1);
246   fail_unless (ret == GST_FLOW_OK);
247   fail_unless (buffer1 != NULL);
248   fail_unless (GST_BUFFER_SIZE (buffer1) == 10);
249   gst_buffer_unref (buffer1);
250
251   /* read 0 bytes at end-1 should return 0 bytes */
252   ret = gst_pad_get_range (pad, stop - 1, 0, &buffer1);
253   fail_unless (ret == GST_FLOW_OK);
254   fail_unless (buffer1 != NULL);
255   fail_unless (GST_BUFFER_SIZE (buffer1) == 0);
256   gst_buffer_unref (buffer1);
257
258   /* read 10 bytes at end-1 should return 1 byte */
259   ret = gst_pad_get_range (pad, stop - 1, 10, &buffer1);
260   fail_unless (ret == GST_FLOW_OK);
261   fail_unless (buffer1 != NULL);
262   fail_unless (GST_BUFFER_SIZE (buffer1) == 1);
263   gst_buffer_unref (buffer1);
264
265   /* read 0 bytes at end should EOS */
266   ret = gst_pad_get_range (pad, stop, 0, &buffer1);
267   fail_unless (ret == GST_FLOW_UNEXPECTED);
268
269   /* read 10 bytes before end should EOS */
270   ret = gst_pad_get_range (pad, stop, 10, &buffer1);
271   fail_unless (ret == GST_FLOW_UNEXPECTED);
272
273   /* read 0 bytes after end should EOS */
274   ret = gst_pad_get_range (pad, stop + 10, 0, &buffer1);
275   fail_unless (ret == GST_FLOW_UNEXPECTED);
276
277   /* read 10 bytes after end should EOS too */
278   ret = gst_pad_get_range (pad, stop + 10, 10, &buffer1);
279   fail_unless (ret == GST_FLOW_UNEXPECTED);
280
281   fail_unless (gst_element_set_state (src,
282           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
283
284   /* cleanup */
285   gst_object_unref (pad);
286   cleanup_filesrc (src);
287 }
288
289 GST_END_TEST;
290
291 GST_START_TEST (test_coverage)
292 {
293   GstElement *src;
294   gchar *location;
295   GstBus *bus;
296   GstMessage *message;
297
298   src = setup_filesrc ();
299   bus = gst_bus_new ();
300
301   gst_element_set_bus (src, bus);
302
303   g_object_set (G_OBJECT (src), "location", "/i/do/not/exist", NULL);
304   g_object_get (G_OBJECT (src), "location", &location, NULL);
305   fail_unless_equals_string (location, "/i/do/not/exist");
306   g_free (location);
307   fail_unless (gst_element_set_state (src,
308           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE,
309       "could set to playing with wrong location");
310
311   /* a state change and an error */
312   fail_if ((message = gst_bus_pop (bus)) == NULL);
313   gst_message_unref (message);
314   fail_if ((message = gst_bus_pop (bus)) == NULL);
315   fail_unless_message_error (message, RESOURCE, NOT_FOUND);
316   gst_message_unref (message);
317
318   g_object_set (G_OBJECT (src), "location", NULL, NULL);
319   g_object_get (G_OBJECT (src), "location", &location, NULL);
320   fail_if (location);
321
322   /* cleanup */
323   gst_element_set_bus (src, NULL);
324   gst_object_unref (GST_OBJECT (bus));
325   cleanup_filesrc (src);
326 }
327
328 GST_END_TEST;
329
330 GST_START_TEST (test_uri_interface)
331 {
332   GstElement *src;
333   gchar *location;
334   GstBus *bus;
335
336   src = setup_filesrc ();
337   bus = gst_bus_new ();
338
339   gst_element_set_bus (src, bus);
340
341   g_object_set (G_OBJECT (src), "location", "/i/do/not/exist", NULL);
342   g_object_get (G_OBJECT (src), "location", &location, NULL);
343   fail_unless_equals_string (location, "/i/do/not/exist");
344   g_free (location);
345
346   location = (gchar *) gst_uri_handler_get_uri (GST_URI_HANDLER (src));
347   fail_unless_equals_string (location, "file://%2Fi%2Fdo%2Fnot%2Fexist");
348
349   /* should accept file:///foo/bar URIs */
350   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
351           "file:///foo/bar"));
352   location = (gchar *) gst_uri_handler_get_uri (GST_URI_HANDLER (src));
353   fail_unless_equals_string (location, "file://%2Ffoo%2Fbar");
354   g_object_get (G_OBJECT (src), "location", &location, NULL);
355   fail_unless_equals_string (location, "/foo/bar");
356   g_free (location);
357
358   /* should accept file://localhost/foo/bar URIs */
359   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
360           "file://localhost/foo/baz"));
361   location = (gchar *) gst_uri_handler_get_uri (GST_URI_HANDLER (src));
362   fail_unless_equals_string (location, "file://%2Ffoo%2Fbaz");
363   g_object_get (G_OBJECT (src), "location", &location, NULL);
364   fail_unless_equals_string (location, "/foo/baz");
365   g_free (location);
366
367   /* should fail with other hostnames */
368   fail_if (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
369           "file://hostname/foo/foo"));
370
371   /* cleanup */
372   gst_element_set_bus (src, NULL);
373   gst_object_unref (GST_OBJECT (bus));
374   cleanup_filesrc (src);
375 }
376
377 GST_END_TEST;
378
379 Suite *
380 filesrc_suite (void)
381 {
382   Suite *s = suite_create ("filesrc");
383   TCase *tc_chain = tcase_create ("general");
384
385   suite_add_tcase (s, tc_chain);
386   tcase_add_test (tc_chain, test_seeking);
387   tcase_add_test (tc_chain, test_reverse);
388   tcase_add_test (tc_chain, test_pull);
389   tcase_add_test (tc_chain, test_coverage);
390   tcase_add_test (tc_chain, test_uri_interface);
391
392   return s;
393 }
394
395 GST_CHECK_MAIN (filesrc);