mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2025-02-20 04:56:24 +00:00
gst/multifile/: Use g_file_[sg]et_contents() instead of using stdio functions.
Original commit message from CVS: * gst/multifile/gstmultifilesink.c: * gst/multifile/gstmultifilesrc.c: Use g_file_[sg]et_contents() instead of using stdio functions. Should be less error prone. * tests/check/elements/multifile.c: Create a temporary directory using standard functions instead of creating a directory in the current dir.
This commit is contained in:
parent
819b139e4e
commit
c8ae2ad1ca
4 changed files with 102 additions and 71 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,13 @@
|
||||||
|
2008-02-07 David Schleef <ds@schleef.org>
|
||||||
|
|
||||||
|
* gst/multifile/gstmultifilesink.c:
|
||||||
|
* gst/multifile/gstmultifilesrc.c:
|
||||||
|
Use g_file_[sg]et_contents() instead of using stdio functions.
|
||||||
|
Should be less error prone.
|
||||||
|
* tests/check/elements/multifile.c:
|
||||||
|
Create a temporary directory using standard functions instead of
|
||||||
|
creating a directory in the current dir.
|
||||||
|
|
||||||
2008-02-08 Jan Schmidt <jan.schmidt@sun.com>
|
2008-02-08 Jan Schmidt <jan.schmidt@sun.com>
|
||||||
|
|
||||||
* configure.ac:
|
* configure.ac:
|
||||||
|
|
|
@ -49,7 +49,7 @@ GST_DEBUG_CATEGORY_STATIC (gst_multi_file_sink_debug);
|
||||||
static const GstElementDetails gst_multi_file_sink_details =
|
static const GstElementDetails gst_multi_file_sink_details =
|
||||||
GST_ELEMENT_DETAILS ("Multi-File Sink",
|
GST_ELEMENT_DETAILS ("Multi-File Sink",
|
||||||
"Sink/File",
|
"Sink/File",
|
||||||
"Write stream to a file",
|
"Write buffers to a sequentially named set of files",
|
||||||
"David Schleef <ds@schleef.org>");
|
"David Schleef <ds@schleef.org>");
|
||||||
|
|
||||||
enum
|
enum
|
||||||
|
@ -113,11 +113,6 @@ gst_multi_file_sink_class_init (GstMultiFileSinkClass * klass)
|
||||||
|
|
||||||
gstbasesink_class->get_times = NULL;
|
gstbasesink_class->get_times = NULL;
|
||||||
gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_multi_file_sink_render);
|
gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_multi_file_sink_render);
|
||||||
|
|
||||||
if (sizeof (off_t) < 8) {
|
|
||||||
GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT,
|
|
||||||
sizeof (off_t));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -197,12 +192,6 @@ gst_multi_file_sink_get_property (GObject * object, guint prop_id,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef G_OS_UNIX
|
|
||||||
# define __GST_STDIO_SEEK_FUNCTION "lseek"
|
|
||||||
#else
|
|
||||||
# define __GST_STDIO_SEEK_FUNCTION "fseek"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static gchar *
|
static gchar *
|
||||||
gst_multi_file_sink_get_filename (GstMultiFileSink * multifilesink)
|
gst_multi_file_sink_get_filename (GstMultiFileSink * multifilesink)
|
||||||
{
|
{
|
||||||
|
@ -219,7 +208,8 @@ gst_multi_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
|
||||||
GstMultiFileSink *multifilesink;
|
GstMultiFileSink *multifilesink;
|
||||||
guint size;
|
guint size;
|
||||||
gchar *filename;
|
gchar *filename;
|
||||||
FILE *file;
|
gboolean ret;
|
||||||
|
GError *error = NULL;
|
||||||
|
|
||||||
size = GST_BUFFER_SIZE (buffer);
|
size = GST_BUFFER_SIZE (buffer);
|
||||||
|
|
||||||
|
@ -227,38 +217,28 @@ gst_multi_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
|
||||||
|
|
||||||
filename = gst_multi_file_sink_get_filename (multifilesink);
|
filename = gst_multi_file_sink_get_filename (multifilesink);
|
||||||
|
|
||||||
file = fopen (filename, "wb");
|
ret = g_file_set_contents (filename, (char *) GST_BUFFER_DATA (buffer),
|
||||||
if (!file) {
|
size, &error);
|
||||||
goto handle_error;
|
if (ret) {
|
||||||
|
multifilesink->index++;
|
||||||
|
g_free (filename);
|
||||||
|
return GST_FLOW_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch (error->code) {
|
||||||
|
case G_FILE_ERROR_NOSPC:{
|
||||||
|
GST_ELEMENT_ERROR (multifilesink, RESOURCE, NO_SPACE_LEFT, (NULL),
|
||||||
|
(NULL));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:{
|
||||||
|
GST_ELEMENT_ERROR (multifilesink, RESOURCE, WRITE,
|
||||||
|
("Error while writing to file \"%s\".", filename),
|
||||||
|
("%s", g_strerror (errno)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g_error_free (error);
|
||||||
g_free (filename);
|
g_free (filename);
|
||||||
|
|
||||||
if (size > 0 && GST_BUFFER_DATA (buffer) != NULL) {
|
return GST_FLOW_ERROR;
|
||||||
if (fwrite (GST_BUFFER_DATA (buffer), size, 1, file) != 1)
|
|
||||||
goto handle_error;
|
|
||||||
}
|
|
||||||
|
|
||||||
multifilesink->index++;
|
|
||||||
|
|
||||||
fclose (file);
|
|
||||||
|
|
||||||
return GST_FLOW_OK;
|
|
||||||
|
|
||||||
handle_error:
|
|
||||||
{
|
|
||||||
switch (errno) {
|
|
||||||
case ENOSPC:{
|
|
||||||
GST_ELEMENT_ERROR (multifilesink, RESOURCE, NO_SPACE_LEFT, (NULL),
|
|
||||||
(NULL));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:{
|
|
||||||
GST_ELEMENT_ERROR (multifilesink, RESOURCE, WRITE,
|
|
||||||
("Error while writing to file \"%s\".", multifilesink->filename),
|
|
||||||
("%s", g_strerror (errno)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return GST_FLOW_ERROR;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,7 +77,7 @@ GST_DEBUG_CATEGORY_STATIC (gst_multi_file_src_debug);
|
||||||
static const GstElementDetails gst_multi_file_src_details =
|
static const GstElementDetails gst_multi_file_src_details =
|
||||||
GST_ELEMENT_DETAILS ("Multi-File Source",
|
GST_ELEMENT_DETAILS ("Multi-File Source",
|
||||||
"Source/File",
|
"Source/File",
|
||||||
"Read stream from files",
|
"Read a sequentially named set of files into buffers",
|
||||||
"David Schleef <ds@schleef.org>");
|
"David Schleef <ds@schleef.org>");
|
||||||
|
|
||||||
enum
|
enum
|
||||||
|
@ -267,10 +267,11 @@ gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
|
||||||
{
|
{
|
||||||
GstMultiFileSrc *multifilesrc;
|
GstMultiFileSrc *multifilesrc;
|
||||||
guint size;
|
guint size;
|
||||||
|
gchar *data;
|
||||||
gchar *filename;
|
gchar *filename;
|
||||||
FILE *file;
|
|
||||||
GstBuffer *buf;
|
GstBuffer *buf;
|
||||||
int ret;
|
gboolean ret;
|
||||||
|
GError *error = NULL;
|
||||||
|
|
||||||
multifilesrc = GST_MULTI_FILE_SRC (src);
|
multifilesrc = GST_MULTI_FILE_SRC (src);
|
||||||
|
|
||||||
|
@ -278,8 +279,8 @@ gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
|
||||||
|
|
||||||
GST_DEBUG_OBJECT (multifilesrc, "reading from file \"%s\".", filename);
|
GST_DEBUG_OBJECT (multifilesrc, "reading from file \"%s\".", filename);
|
||||||
|
|
||||||
file = fopen (filename, "rb");
|
ret = g_file_get_contents (filename, &data, &size, &error);
|
||||||
if (!file) {
|
if (!ret) {
|
||||||
if (multifilesrc->successful_read) {
|
if (multifilesrc->successful_read) {
|
||||||
/* If we've read at least one buffer successfully, not finding the
|
/* If we've read at least one buffer successfully, not finding the
|
||||||
* next file is EOS. */
|
* next file is EOS. */
|
||||||
|
@ -290,20 +291,11 @@ gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fseek (file, 0, SEEK_END);
|
|
||||||
size = ftell (file);
|
|
||||||
fseek (file, 0, SEEK_SET);
|
|
||||||
|
|
||||||
buf = gst_buffer_new_and_alloc (size);
|
|
||||||
|
|
||||||
ret = fread (GST_BUFFER_DATA (buf), size, 1, file);
|
|
||||||
if (ret < 1) {
|
|
||||||
goto handle_error;
|
|
||||||
}
|
|
||||||
|
|
||||||
multifilesrc->successful_read = TRUE;
|
multifilesrc->successful_read = TRUE;
|
||||||
multifilesrc->index++;
|
multifilesrc->index++;
|
||||||
|
|
||||||
|
buf = gst_buffer_new ();
|
||||||
|
GST_BUFFER_DATA (buf) = (unsigned char *) data;
|
||||||
GST_BUFFER_SIZE (buf) = size;
|
GST_BUFFER_SIZE (buf) = size;
|
||||||
GST_BUFFER_OFFSET (buf) = multifilesrc->offset;
|
GST_BUFFER_OFFSET (buf) = multifilesrc->offset;
|
||||||
GST_BUFFER_OFFSET_END (buf) = multifilesrc->offset + size;
|
GST_BUFFER_OFFSET_END (buf) = multifilesrc->offset + size;
|
||||||
|
@ -312,7 +304,6 @@ gst_multi_file_src_create (GstPushSrc * src, GstBuffer ** buffer)
|
||||||
|
|
||||||
GST_DEBUG_OBJECT (multifilesrc, "read file \"%s\".", filename);
|
GST_DEBUG_OBJECT (multifilesrc, "read file \"%s\".", filename);
|
||||||
|
|
||||||
fclose (file);
|
|
||||||
g_free (filename);
|
g_free (filename);
|
||||||
*buffer = buf;
|
*buffer = buf;
|
||||||
return GST_FLOW_OK;
|
return GST_FLOW_OK;
|
||||||
|
|
|
@ -36,29 +36,59 @@ run_pipeline (GstElement * pipeline)
|
||||||
gst_element_set_state (pipeline, GST_STATE_NULL);
|
gst_element_set_state (pipeline, GST_STATE_NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gchar *
|
||||||
|
g_mkdtemp (const gchar * template)
|
||||||
|
{
|
||||||
|
gchar *s;
|
||||||
|
gchar *tmpdir;
|
||||||
|
|
||||||
|
s = g_strdup (template);
|
||||||
|
tmpdir = mkdtemp (s);
|
||||||
|
if (tmpdir == NULL) {
|
||||||
|
g_free (s);
|
||||||
|
}
|
||||||
|
return tmpdir;
|
||||||
|
}
|
||||||
|
|
||||||
GST_START_TEST (test_multifilesink)
|
GST_START_TEST (test_multifilesink)
|
||||||
{
|
{
|
||||||
GstElement *pipeline;
|
GstElement *pipeline;
|
||||||
|
GstElement *mfs;
|
||||||
int i;
|
int i;
|
||||||
|
const gchar *tmpdir;
|
||||||
|
gchar *my_tmpdir;
|
||||||
|
gchar *template;
|
||||||
|
gchar *mfs_pattern;
|
||||||
|
|
||||||
g_mkdir ("tmpdir", 0700);
|
tmpdir = g_get_tmp_dir ();
|
||||||
|
template = g_build_filename (tmpdir, "multifile-test-XXXXXX", NULL);
|
||||||
|
my_tmpdir = g_mkdtemp (template);
|
||||||
|
fail_if (my_tmpdir == NULL);
|
||||||
|
|
||||||
pipeline =
|
pipeline =
|
||||||
gst_parse_launch
|
gst_parse_launch
|
||||||
("videotestsrc num-buffers=10 ! video/x-raw-yuv,format=(fourcc)I420,width=320,height=240 ! multifilesink location=tmpdir/%05d",
|
("videotestsrc num-buffers=10 ! video/x-raw-yuv,format=(fourcc)I420,width=320,height=240 ! multifilesink",
|
||||||
NULL);
|
NULL);
|
||||||
fail_if (pipeline == NULL);
|
fail_if (pipeline == NULL);
|
||||||
|
mfs = gst_bin_get_by_name (GST_BIN (pipeline), "multifilesink0");
|
||||||
|
fail_if (mfs == NULL);
|
||||||
|
mfs_pattern = g_build_filename (my_tmpdir, "%05d", NULL);
|
||||||
|
g_object_set (G_OBJECT (mfs), "location", mfs_pattern, NULL);
|
||||||
run_pipeline (pipeline);
|
run_pipeline (pipeline);
|
||||||
gst_object_unref (pipeline);
|
gst_object_unref (pipeline);
|
||||||
|
|
||||||
for (i = 0; i < 10; i++) {
|
for (i = 0; i < 10; i++) {
|
||||||
char s[20];
|
char *s;
|
||||||
|
|
||||||
sprintf (s, "tmpdir/%05d", i);
|
s = g_strdup_printf (mfs_pattern, i);
|
||||||
fail_if (g_remove (s) != 0);
|
fail_if (g_remove (s) != 0);
|
||||||
|
g_free (s);
|
||||||
}
|
}
|
||||||
fail_if (g_remove ("tmpdir") != 0);
|
fail_if (g_remove (my_tmpdir) != 0);
|
||||||
|
|
||||||
|
g_free (mfs_pattern);
|
||||||
|
g_free (my_tmpdir);
|
||||||
|
g_free (template);
|
||||||
}
|
}
|
||||||
|
|
||||||
GST_END_TEST;
|
GST_END_TEST;
|
||||||
|
@ -66,34 +96,54 @@ GST_END_TEST;
|
||||||
GST_START_TEST (test_multifilesrc)
|
GST_START_TEST (test_multifilesrc)
|
||||||
{
|
{
|
||||||
GstElement *pipeline;
|
GstElement *pipeline;
|
||||||
|
GstElement *mfs;
|
||||||
int i;
|
int i;
|
||||||
|
const gchar *tmpdir;
|
||||||
|
gchar *my_tmpdir;
|
||||||
|
gchar *template;
|
||||||
|
gchar *mfs_pattern;
|
||||||
|
|
||||||
g_mkdir ("tmpdir", 0700);
|
tmpdir = g_get_tmp_dir ();
|
||||||
|
template = g_build_filename (tmpdir, "multifile-test-XXXXXX", NULL);
|
||||||
|
my_tmpdir = g_mkdtemp (template);
|
||||||
|
fail_if (my_tmpdir == NULL);
|
||||||
|
|
||||||
pipeline =
|
pipeline =
|
||||||
gst_parse_launch
|
gst_parse_launch
|
||||||
("videotestsrc num-buffers=10 ! video/x-raw-yuv,format=(fourcc)I420,width=320,height=240 ! multifilesink location=tmpdir/%05d",
|
("videotestsrc num-buffers=10 ! video/x-raw-yuv,format=(fourcc)I420,width=320,height=240 ! multifilesink",
|
||||||
NULL);
|
NULL);
|
||||||
fail_if (pipeline == NULL);
|
fail_if (pipeline == NULL);
|
||||||
|
mfs = gst_bin_get_by_name (GST_BIN (pipeline), "multifilesink0");
|
||||||
|
fail_if (mfs == NULL);
|
||||||
|
mfs_pattern = g_build_filename (my_tmpdir, "%05d", NULL);
|
||||||
|
g_object_set (G_OBJECT (mfs), "location", mfs_pattern, NULL);
|
||||||
run_pipeline (pipeline);
|
run_pipeline (pipeline);
|
||||||
gst_object_unref (pipeline);
|
gst_object_unref (pipeline);
|
||||||
|
|
||||||
pipeline =
|
pipeline =
|
||||||
gst_parse_launch
|
gst_parse_launch
|
||||||
("multifilesrc location=tmpdir/%05d ! video/x-raw-yuv,format=(fourcc)I420,width=320,height=240,framerate=10/1 ! fakesink",
|
("multifilesrc ! video/x-raw-yuv,format=(fourcc)I420,width=320,height=240,framerate=10/1 ! fakesink",
|
||||||
NULL);
|
NULL);
|
||||||
fail_if (pipeline == NULL);
|
fail_if (pipeline == NULL);
|
||||||
|
mfs = gst_bin_get_by_name (GST_BIN (pipeline), "multifilesrc0");
|
||||||
|
fail_if (mfs == NULL);
|
||||||
|
mfs_pattern = g_build_filename (my_tmpdir, "%05d", NULL);
|
||||||
|
g_object_set (G_OBJECT (mfs), "location", mfs_pattern, NULL);
|
||||||
run_pipeline (pipeline);
|
run_pipeline (pipeline);
|
||||||
gst_object_unref (pipeline);
|
gst_object_unref (pipeline);
|
||||||
|
|
||||||
for (i = 0; i < 10; i++) {
|
for (i = 0; i < 10; i++) {
|
||||||
char s[20];
|
char *s;
|
||||||
|
|
||||||
sprintf (s, "tmpdir/%05d", i);
|
s = g_strdup_printf (mfs_pattern, i);
|
||||||
fail_if (g_remove (s) != 0);
|
fail_if (g_remove (s) != 0);
|
||||||
|
g_free (s);
|
||||||
}
|
}
|
||||||
fail_if (g_remove ("tmpdir") != 0);
|
fail_if (g_remove (my_tmpdir) != 0);
|
||||||
|
|
||||||
|
g_free (mfs_pattern);
|
||||||
|
g_free (my_tmpdir);
|
||||||
|
g_free (template);
|
||||||
}
|
}
|
||||||
|
|
||||||
GST_END_TEST;
|
GST_END_TEST;
|
||||||
|
|
Loading…
Reference in a new issue