1 /* GStreamer unit test for the filesink element
3 * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
4 * Copyright (C) 2007 Tim-Philipp Müller <tim centricular net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
29 #include <glib/gstdio.h>
32 #include <unistd.h> /* for close() */
35 #include <gst/check/gstcheck.h>
37 static GstPad *mysrcpad;
39 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
49 GST_DEBUG ("setup_filesink");
50 filesink = gst_check_setup_element ("filesink");
51 mysrcpad = gst_check_setup_src_pad (filesink, &srctemplate, NULL);
52 gst_pad_set_active (mysrcpad, TRUE);
57 cleanup_filesink (GstElement * filesink)
59 gst_pad_set_active (mysrcpad, FALSE);
60 gst_check_teardown_src_pad (filesink);
61 gst_check_teardown_element (filesink);
65 /* this queries via the element vfunc, which is currently not implemented */
66 #define CHECK_QUERY_POSITION(filesink,format,position) \
68 GstFormat fmt = format; \
70 fail_unless (gst_element_query_position (filesink, &fmt, &pos)); \
71 fail_unless_equals_int (pos, position); \
74 #define CHECK_QUERY_POSITION(filesink,format,position) \
76 GstFormat fmt = format; \
79 pad = gst_element_get_static_pad (filesink, "sink"); \
80 fail_unless (gst_pad_query_position (pad, &fmt, &pos)); \
81 fail_unless_equals_int (pos, position); \
82 gst_object_unref (pad); \
86 #define PUSH_BYTES(num_bytes) \
88 GstBuffer *buf = gst_buffer_new_and_alloc(num_bytes); \
89 GRand *rand = g_rand_new_with_seed (num_bytes); \
92 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE); \
93 for (i = 0; i < num_bytes; ++i) \
94 data[i] = (g_rand_int (rand) >> 24) & 0xff; \
95 gst_buffer_unmap (buf, data, num_bytes); \
96 fail_unless_equals_int (gst_pad_push (mysrcpad, buf), GST_FLOW_OK); \
100 /* TODO: we don't check that the data is actually written to the right
101 * position after a seek */
102 GST_START_TEST (test_seeking)
105 GstElement *filesink;
109 tmpdir = g_get_tmp_dir ();
113 /* this is just silly, but gcc warns if we try to use tpmnam() */
114 tmp_fn = g_build_filename (tmpdir, "gstreamer-filesink-test-XXXXXX", NULL);
115 fd = g_mkstemp (tmp_fn);
117 GST_ERROR ("can't create temp file %s: %s", tmp_fn, g_strerror (errno));
121 /* don't want the file, just a filename (hence silly, see above) */
125 filesink = setup_filesink ();
127 GST_LOG ("using temp file '%s'", tmp_fn);
128 g_object_set (filesink, "location", tmp_fn, NULL);
130 fail_unless_equals_int (gst_element_set_state (filesink, GST_STATE_PLAYING),
131 GST_STATE_CHANGE_ASYNC);
134 /* Test that filesink is seekable with a file fd */
135 /* filesink doesn't implement seekable query at the moment */
136 GstQuery *seeking_query;
139 fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
141 fail_unless (gst_element_query (filesink, seeking_query) == TRUE);
142 gst_query_parse_seeking (seeking_query, NULL, &seekable, NULL, NULL);
143 fail_unless (seekable == TRUE);
144 gst_query_unref (seeking_query);
147 fail_unless (gst_pad_push_event (mysrcpad,
148 gst_event_new_new_segment (FALSE, 1.0, 1.0, GST_FORMAT_BYTES, 0, -1,
151 CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 0);
153 /* push buffer with size 0 and NULL data */
155 CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 0);
158 CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 1);
161 CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 100);
164 CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 8900);
166 if (gst_pad_push_event (mysrcpad,
167 gst_event_new_new_segment (TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 8800, -1,
170 /* make sure that that new position is reported immediately */
171 CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 8800);
173 CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 8801);
175 CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 18057);
177 GST_INFO ("seeking not supported for tempfile?!");
180 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
182 fail_unless_equals_int (gst_element_set_state (filesink, GST_STATE_NULL),
183 GST_STATE_CHANGE_SUCCESS);
186 cleanup_filesink (filesink);
188 /* check that we wrote data to the right position after the seek */
193 fail_unless (g_file_get_contents (tmp_fn, &data, &len, NULL),
194 "Failed to read in newly-created file '%s'", tmp_fn);
195 fail_unless_equals_int (len, 18057);
197 /* we wrote 9256 bytes at position 8801 */
198 GRand *rand = g_rand_new_with_seed (9256);
201 for (i = 0; i < 9256; ++i) {
202 guint8 byte_written = *(((guint8 *) data) + 8801 + i);
204 fail_unless_equals_int (byte_written, g_rand_int (rand) >> 24);
218 GST_START_TEST (test_coverage)
220 GstElement *filesink;
225 filesink = setup_filesink ();
226 bus = gst_bus_new ();
228 gst_element_set_bus (filesink, bus);
230 g_object_set (filesink, "location", "/i/do/not/exist", NULL);
231 g_object_get (filesink, "location", &location, NULL);
232 fail_unless_equals_string (location, "/i/do/not/exist");
235 fail_unless_equals_int (gst_element_set_state (filesink, GST_STATE_PLAYING),
236 GST_STATE_CHANGE_FAILURE);
238 /* a state change and an error */
239 fail_if ((message = gst_bus_pop (bus)) == NULL);
240 fail_unless_message_error (message, RESOURCE, OPEN_WRITE);
241 gst_message_unref (message);
243 g_object_set (filesink, "location", NULL, NULL);
244 g_object_get (filesink, "location", &location, NULL);
248 gst_element_set_bus (filesink, NULL);
249 gst_object_unref (GST_OBJECT (bus));
250 cleanup_filesink (filesink);
255 GST_START_TEST (test_uri_interface)
257 GstElement *filesink;
261 filesink = setup_filesink ();
262 bus = gst_bus_new ();
264 gst_element_set_bus (filesink, bus);
266 g_object_set (G_OBJECT (filesink), "location", "/i/do/not/exist", NULL);
267 g_object_get (G_OBJECT (filesink), "location", &location, NULL);
268 fail_unless_equals_string (location, "/i/do/not/exist");
271 location = (gchar *) gst_uri_handler_get_uri (GST_URI_HANDLER (filesink));
272 fail_unless_equals_string (location, "file:///i/do/not/exist");
274 /* should accept file:///foo/bar URIs */
275 fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (filesink),
277 location = (gchar *) gst_uri_handler_get_uri (GST_URI_HANDLER (filesink));
278 fail_unless_equals_string (location, "file:///foo/bar");
279 g_object_get (G_OBJECT (filesink), "location", &location, NULL);
280 fail_unless_equals_string (location, "/foo/bar");
283 /* should accept file://localhost/foo/bar URIs */
284 fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (filesink),
285 "file://localhost/foo/baz"));
286 location = (gchar *) gst_uri_handler_get_uri (GST_URI_HANDLER (filesink));
287 fail_unless_equals_string (location, "file:///foo/baz");
288 g_object_get (G_OBJECT (filesink), "location", &location, NULL);
289 fail_unless_equals_string (location, "/foo/baz");
292 /* should escape non-uri characters for the URI but not for the location */
293 g_object_set (G_OBJECT (filesink), "location", "/foo/b?r", NULL);
294 g_object_get (G_OBJECT (filesink), "location", &location, NULL);
295 fail_unless_equals_string (location, "/foo/b?r");
297 location = (gchar *) gst_uri_handler_get_uri (GST_URI_HANDLER (filesink));
298 fail_unless_equals_string (location, "file:///foo/b%3Fr");
300 /* should fail with other hostnames */
301 fail_if (gst_uri_handler_set_uri (GST_URI_HANDLER (filesink),
302 "file://hostname/foo/foo"));
305 gst_element_set_bus (filesink, NULL);
306 gst_object_unref (GST_OBJECT (bus));
307 cleanup_filesink (filesink);
313 filesink_suite (void)
315 Suite *s = suite_create ("filesink");
316 TCase *tc_chain = tcase_create ("general");
318 suite_add_tcase (s, tc_chain);
320 tcase_add_test (tc_chain, test_coverage);
321 tcase_add_test (tc_chain, test_uri_interface);
322 tcase_add_test (tc_chain, test_seeking);
327 GST_CHECK_MAIN (filesink);