2007-05-22 11:55:33 +00:00
|
|
|
/* GStreamer unit test for the filesink element
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
|
|
|
|
* Copyright (C) 2007 Tim-Philipp Müller <tim centricular net>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 20:44:48 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2007-05-22 11:55:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h> /* for close() */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gst/check/gstcheck.h>
|
|
|
|
|
2008-02-29 13:59:24 +00:00
|
|
|
static GstPad *mysrcpad;
|
2007-05-22 11:55:33 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
|
|
|
static GstElement *
|
2008-02-29 13:59:24 +00:00
|
|
|
setup_filesink (void)
|
2007-05-22 11:55:33 +00:00
|
|
|
{
|
|
|
|
GstElement *filesink;
|
|
|
|
|
|
|
|
GST_DEBUG ("setup_filesink");
|
|
|
|
filesink = gst_check_setup_element ("filesink");
|
2011-11-24 20:36:12 +00:00
|
|
|
mysrcpad = gst_check_setup_src_pad (filesink, &srctemplate);
|
2007-05-22 11:55:33 +00:00
|
|
|
gst_pad_set_active (mysrcpad, TRUE);
|
|
|
|
return filesink;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup_filesink (GstElement * filesink)
|
|
|
|
{
|
|
|
|
gst_pad_set_active (mysrcpad, FALSE);
|
|
|
|
gst_check_teardown_src_pad (filesink);
|
|
|
|
gst_check_teardown_element (filesink);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* this queries via the element vfunc, which is currently not implemented */
|
|
|
|
#define CHECK_QUERY_POSITION(filesink,format,position) \
|
|
|
|
G_STMT_START { \
|
|
|
|
gint64 pos; \
|
2011-07-26 23:28:50 +00:00
|
|
|
fail_unless (gst_element_query_position (filesink, format, &pos)); \
|
2007-05-22 11:55:33 +00:00
|
|
|
fail_unless_equals_int (pos, position); \
|
|
|
|
} G_STMT_END
|
|
|
|
#else
|
|
|
|
#define CHECK_QUERY_POSITION(filesink,format,position) \
|
|
|
|
G_STMT_START { \
|
|
|
|
GstPad *pad; \
|
|
|
|
gint64 pos; \
|
2008-05-21 15:57:52 +00:00
|
|
|
pad = gst_element_get_static_pad (filesink, "sink"); \
|
2011-07-26 23:28:50 +00:00
|
|
|
fail_unless (gst_pad_query_position (pad, format, &pos)); \
|
2007-05-22 11:55:33 +00:00
|
|
|
fail_unless_equals_int (pos, position); \
|
|
|
|
gst_object_unref (pad); \
|
|
|
|
} G_STMT_END
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PUSH_BYTES(num_bytes) \
|
|
|
|
G_STMT_START { \
|
|
|
|
GstBuffer *buf = gst_buffer_new_and_alloc(num_bytes); \
|
|
|
|
GRand *rand = g_rand_new_with_seed (num_bytes); \
|
2012-01-20 13:23:57 +00:00
|
|
|
GstMapInfo info; \
|
2007-05-22 11:55:33 +00:00
|
|
|
guint i; \
|
2012-01-20 13:23:57 +00:00
|
|
|
fail_unless (gst_buffer_map (buf, &info, GST_MAP_WRITE)); \
|
2007-05-22 11:55:33 +00:00
|
|
|
for (i = 0; i < num_bytes; ++i) \
|
2012-01-20 13:23:57 +00:00
|
|
|
((guint8 *)info.data)[i] = (g_rand_int (rand) >> 24) & 0xff; \
|
|
|
|
gst_buffer_unmap (buf, &info); \
|
2007-05-22 11:55:33 +00:00
|
|
|
fail_unless_equals_int (gst_pad_push (mysrcpad, buf), GST_FLOW_OK); \
|
|
|
|
g_rand_free (rand); \
|
|
|
|
} G_STMT_END
|
|
|
|
|
2013-08-27 05:05:11 +00:00
|
|
|
#define CHECK_WRITTEN_BYTES(offset,written,file_size) \
|
|
|
|
G_STMT_START { \
|
|
|
|
gchar *data = NULL; \
|
|
|
|
gsize len; \
|
|
|
|
fail_unless (g_file_get_contents (tmp_fn, &data, &len, NULL), \
|
|
|
|
"Failed to read in newly-created file '%s'", tmp_fn); \
|
|
|
|
fail_unless_equals_int (len, file_size); \
|
|
|
|
{ \
|
|
|
|
/* we wrote <written> bytes at position 0 */ \
|
|
|
|
GRand *rand = g_rand_new_with_seed (written); \
|
|
|
|
guint i; \
|
|
|
|
for (i = 0; i < written; ++i) { \
|
|
|
|
guint8 byte_written = *(((guint8 *) data) + offset + i); \
|
|
|
|
\
|
|
|
|
fail_unless_equals_int (byte_written, g_rand_int (rand) >> 24); \
|
|
|
|
} \
|
|
|
|
g_rand_free (rand); \
|
|
|
|
} \
|
|
|
|
g_free (data); \
|
|
|
|
} G_STMT_END
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
create_temporary_file (void)
|
2007-05-22 11:55:33 +00:00
|
|
|
{
|
|
|
|
const gchar *tmpdir;
|
|
|
|
gchar *tmp_fn;
|
|
|
|
gint fd;
|
|
|
|
|
|
|
|
tmpdir = g_get_tmp_dir ();
|
|
|
|
if (tmpdir == NULL)
|
2013-08-27 05:05:11 +00:00
|
|
|
return NULL;
|
2007-05-22 11:55:33 +00:00
|
|
|
|
|
|
|
/* this is just silly, but gcc warns if we try to use tpmnam() */
|
|
|
|
tmp_fn = g_build_filename (tmpdir, "gstreamer-filesink-test-XXXXXX", NULL);
|
|
|
|
fd = g_mkstemp (tmp_fn);
|
|
|
|
if (fd < 0) {
|
|
|
|
GST_ERROR ("can't create temp file %s: %s", tmp_fn, g_strerror (errno));
|
|
|
|
g_free (tmp_fn);
|
2013-08-27 05:05:11 +00:00
|
|
|
return NULL;
|
2007-05-22 11:55:33 +00:00
|
|
|
}
|
|
|
|
/* don't want the file, just a filename (hence silly, see above) */
|
|
|
|
close (fd);
|
|
|
|
g_remove (tmp_fn);
|
|
|
|
|
2013-08-27 05:05:11 +00:00
|
|
|
return tmp_fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: we don't check that the data is actually written to the right
|
|
|
|
* position after a seek */
|
|
|
|
GST_START_TEST (test_seeking)
|
|
|
|
{
|
|
|
|
GstElement *filesink;
|
|
|
|
gchar *tmp_fn;
|
|
|
|
GstSegment segment;
|
|
|
|
|
|
|
|
tmp_fn = create_temporary_file ();
|
|
|
|
if (tmp_fn == NULL)
|
|
|
|
return;
|
2007-05-22 11:55:33 +00:00
|
|
|
filesink = setup_filesink ();
|
|
|
|
|
|
|
|
GST_LOG ("using temp file '%s'", tmp_fn);
|
|
|
|
g_object_set (filesink, "location", tmp_fn, NULL);
|
|
|
|
|
|
|
|
fail_unless_equals_int (gst_element_set_state (filesink, GST_STATE_PLAYING),
|
|
|
|
GST_STATE_CHANGE_ASYNC);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Test that filesink is seekable with a file fd */
|
|
|
|
/* filesink doesn't implement seekable query at the moment */
|
|
|
|
GstQuery *seeking_query;
|
|
|
|
gboolean seekable;
|
|
|
|
|
|
|
|
fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
|
|
|
|
!= NULL);
|
|
|
|
fail_unless (gst_element_query (filesink, seeking_query) == TRUE);
|
|
|
|
gst_query_parse_seeking (seeking_query, NULL, &seekable, NULL, NULL);
|
|
|
|
fail_unless (seekable == TRUE);
|
|
|
|
gst_query_unref (seeking_query);
|
|
|
|
#endif
|
|
|
|
|
2013-05-09 11:32:07 +00:00
|
|
|
fail_unless (gst_pad_push_event (mysrcpad,
|
|
|
|
gst_event_new_stream_start ("test")));
|
|
|
|
|
2011-05-13 16:07:24 +00:00
|
|
|
gst_segment_init (&segment, GST_FORMAT_BYTES);
|
|
|
|
fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
|
2007-05-22 11:55:33 +00:00
|
|
|
|
|
|
|
CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 0);
|
|
|
|
|
|
|
|
/* push buffer with size 0 and NULL data */
|
|
|
|
PUSH_BYTES (0);
|
|
|
|
CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 0);
|
|
|
|
|
|
|
|
PUSH_BYTES (1);
|
|
|
|
CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 1);
|
|
|
|
|
|
|
|
PUSH_BYTES (99);
|
|
|
|
CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 100);
|
|
|
|
|
|
|
|
PUSH_BYTES (8800);
|
|
|
|
CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 8900);
|
|
|
|
|
2011-05-13 16:07:24 +00:00
|
|
|
segment.start = 8800;
|
|
|
|
if (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment))) {
|
2007-05-22 11:55:33 +00:00
|
|
|
GST_LOG ("seek ok");
|
2011-09-07 11:14:38 +00:00
|
|
|
/* make sure that new position is reported immediately */
|
2007-05-22 11:55:33 +00:00
|
|
|
CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 8800);
|
|
|
|
PUSH_BYTES (1);
|
|
|
|
CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 8801);
|
|
|
|
PUSH_BYTES (9256);
|
|
|
|
CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 18057);
|
|
|
|
} else {
|
|
|
|
GST_INFO ("seeking not supported for tempfile?!");
|
|
|
|
}
|
|
|
|
|
|
|
|
fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
|
|
|
|
|
|
|
|
fail_unless_equals_int (gst_element_set_state (filesink, GST_STATE_NULL),
|
|
|
|
GST_STATE_CHANGE_SUCCESS);
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
cleanup_filesink (filesink);
|
|
|
|
|
2013-08-27 05:05:11 +00:00
|
|
|
CHECK_WRITTEN_BYTES (8801, 9256, 18057);
|
2007-05-22 11:55:33 +00:00
|
|
|
|
|
|
|
/* remove file */
|
|
|
|
g_remove (tmp_fn);
|
|
|
|
g_free (tmp_fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
2013-08-27 05:51:35 +00:00
|
|
|
GST_START_TEST (test_flush)
|
|
|
|
{
|
|
|
|
GstElement *filesink;
|
|
|
|
gchar *tmp_fn;
|
|
|
|
GstSegment segment;
|
|
|
|
|
|
|
|
tmp_fn = create_temporary_file ();
|
|
|
|
if (tmp_fn == NULL)
|
|
|
|
return;
|
|
|
|
filesink = setup_filesink ();
|
|
|
|
|
|
|
|
GST_LOG ("using temp file '%s'", tmp_fn);
|
|
|
|
g_object_set (filesink, "location", tmp_fn, NULL);
|
|
|
|
|
|
|
|
fail_unless_equals_int (gst_element_set_state (filesink, GST_STATE_PLAYING),
|
|
|
|
GST_STATE_CHANGE_ASYNC);
|
|
|
|
|
|
|
|
fail_unless (gst_pad_push_event (mysrcpad,
|
|
|
|
gst_event_new_stream_start ("test")));
|
|
|
|
|
|
|
|
gst_segment_init (&segment, GST_FORMAT_TIME);
|
|
|
|
fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
|
|
|
|
|
|
|
|
CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 0);
|
|
|
|
|
|
|
|
PUSH_BYTES (8);
|
|
|
|
CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 8);
|
|
|
|
|
|
|
|
fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_flush_start ()));
|
|
|
|
fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_flush_stop (TRUE)));
|
|
|
|
fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
|
|
|
|
|
|
|
|
fail_unless_equals_int (gst_element_set_state (filesink, GST_STATE_PLAYING),
|
|
|
|
GST_STATE_CHANGE_ASYNC);
|
|
|
|
|
|
|
|
CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 0);
|
|
|
|
|
|
|
|
PUSH_BYTES (4);
|
|
|
|
CHECK_QUERY_POSITION (filesink, GST_FORMAT_BYTES, 4);
|
|
|
|
|
|
|
|
cleanup_filesink (filesink);
|
|
|
|
|
|
|
|
CHECK_WRITTEN_BYTES (0, 4, 4);
|
|
|
|
|
|
|
|
g_remove (tmp_fn);
|
|
|
|
g_free (tmp_fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
2007-05-22 11:55:33 +00:00
|
|
|
GST_START_TEST (test_coverage)
|
|
|
|
{
|
|
|
|
GstElement *filesink;
|
|
|
|
gchar *location;
|
|
|
|
GstBus *bus;
|
|
|
|
GstMessage *message;
|
|
|
|
|
|
|
|
filesink = setup_filesink ();
|
|
|
|
bus = gst_bus_new ();
|
|
|
|
|
|
|
|
gst_element_set_bus (filesink, bus);
|
|
|
|
|
|
|
|
g_object_set (filesink, "location", "/i/do/not/exist", NULL);
|
|
|
|
g_object_get (filesink, "location", &location, NULL);
|
|
|
|
fail_unless_equals_string (location, "/i/do/not/exist");
|
|
|
|
g_free (location);
|
|
|
|
|
|
|
|
fail_unless_equals_int (gst_element_set_state (filesink, GST_STATE_PLAYING),
|
|
|
|
GST_STATE_CHANGE_FAILURE);
|
|
|
|
|
|
|
|
/* a state change and an error */
|
|
|
|
fail_if ((message = gst_bus_pop (bus)) == NULL);
|
|
|
|
fail_unless_message_error (message, RESOURCE, OPEN_WRITE);
|
|
|
|
gst_message_unref (message);
|
|
|
|
|
|
|
|
g_object_set (filesink, "location", NULL, NULL);
|
|
|
|
g_object_get (filesink, "location", &location, NULL);
|
|
|
|
fail_if (location);
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
gst_element_set_bus (filesink, NULL);
|
|
|
|
gst_object_unref (GST_OBJECT (bus));
|
|
|
|
cleanup_filesink (filesink);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
|
|
|
GST_START_TEST (test_uri_interface)
|
|
|
|
{
|
|
|
|
GstElement *filesink;
|
|
|
|
gchar *location;
|
|
|
|
GstBus *bus;
|
|
|
|
|
|
|
|
filesink = setup_filesink ();
|
|
|
|
bus = gst_bus_new ();
|
|
|
|
|
|
|
|
gst_element_set_bus (filesink, bus);
|
|
|
|
|
|
|
|
g_object_set (G_OBJECT (filesink), "location", "/i/do/not/exist", NULL);
|
|
|
|
g_object_get (G_OBJECT (filesink), "location", &location, NULL);
|
|
|
|
fail_unless_equals_string (location, "/i/do/not/exist");
|
|
|
|
g_free (location);
|
|
|
|
|
2011-11-13 17:45:19 +00:00
|
|
|
location = gst_uri_handler_get_uri (GST_URI_HANDLER (filesink));
|
2011-02-15 21:49:26 +00:00
|
|
|
fail_unless_equals_string (location, "file:///i/do/not/exist");
|
2011-11-13 17:45:19 +00:00
|
|
|
g_free (location);
|
2007-05-22 11:55:33 +00:00
|
|
|
|
|
|
|
/* should accept file:///foo/bar URIs */
|
|
|
|
fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (filesink),
|
2011-11-13 17:45:19 +00:00
|
|
|
"file:///foo/bar", NULL));
|
|
|
|
location = gst_uri_handler_get_uri (GST_URI_HANDLER (filesink));
|
2011-02-15 21:49:26 +00:00
|
|
|
fail_unless_equals_string (location, "file:///foo/bar");
|
2011-11-13 17:45:19 +00:00
|
|
|
g_free (location);
|
2007-05-22 11:55:33 +00:00
|
|
|
g_object_get (G_OBJECT (filesink), "location", &location, NULL);
|
|
|
|
fail_unless_equals_string (location, "/foo/bar");
|
|
|
|
g_free (location);
|
|
|
|
|
|
|
|
/* should accept file://localhost/foo/bar URIs */
|
|
|
|
fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (filesink),
|
2011-11-13 17:45:19 +00:00
|
|
|
"file://localhost/foo/baz", NULL));
|
|
|
|
location = gst_uri_handler_get_uri (GST_URI_HANDLER (filesink));
|
2011-02-15 21:49:26 +00:00
|
|
|
fail_unless_equals_string (location, "file:///foo/baz");
|
2011-11-13 17:45:19 +00:00
|
|
|
g_free (location);
|
2007-05-22 11:55:33 +00:00
|
|
|
g_object_get (G_OBJECT (filesink), "location", &location, NULL);
|
|
|
|
fail_unless_equals_string (location, "/foo/baz");
|
|
|
|
g_free (location);
|
|
|
|
|
2011-02-15 21:56:35 +00:00
|
|
|
/* should escape non-uri characters for the URI but not for the location */
|
|
|
|
g_object_set (G_OBJECT (filesink), "location", "/foo/b?r", NULL);
|
|
|
|
g_object_get (G_OBJECT (filesink), "location", &location, NULL);
|
|
|
|
fail_unless_equals_string (location, "/foo/b?r");
|
|
|
|
g_free (location);
|
2011-11-13 17:45:19 +00:00
|
|
|
location = gst_uri_handler_get_uri (GST_URI_HANDLER (filesink));
|
2011-02-15 21:56:35 +00:00
|
|
|
fail_unless_equals_string (location, "file:///foo/b%3Fr");
|
2011-11-13 17:45:19 +00:00
|
|
|
g_free (location);
|
2011-02-15 21:56:35 +00:00
|
|
|
|
2007-05-22 11:55:33 +00:00
|
|
|
/* should fail with other hostnames */
|
|
|
|
fail_if (gst_uri_handler_set_uri (GST_URI_HANDLER (filesink),
|
2011-11-13 17:45:19 +00:00
|
|
|
"file://hostname/foo/foo", NULL));
|
2007-05-22 11:55:33 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
gst_element_set_bus (filesink, NULL);
|
|
|
|
gst_object_unref (GST_OBJECT (bus));
|
|
|
|
cleanup_filesink (filesink);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
|
|
|
static Suite *
|
|
|
|
filesink_suite (void)
|
|
|
|
{
|
|
|
|
Suite *s = suite_create ("filesink");
|
|
|
|
TCase *tc_chain = tcase_create ("general");
|
|
|
|
|
|
|
|
suite_add_tcase (s, tc_chain);
|
|
|
|
|
|
|
|
tcase_add_test (tc_chain, test_coverage);
|
|
|
|
tcase_add_test (tc_chain, test_uri_interface);
|
|
|
|
tcase_add_test (tc_chain, test_seeking);
|
2013-08-27 05:51:35 +00:00
|
|
|
tcase_add_test (tc_chain, test_flush);
|
2007-05-22 11:55:33 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_CHECK_MAIN (filesink);
|