/* test_libFLAC - Unit tester for libFLAC
- * Copyright (C) 2002,2003,2004,2005,2006 Josh Coalson
+ * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#if HAVE_CONFIG_H
#include "FLAC/assert.h"
#include "FLAC/stream_encoder.h"
#include "share/grabbag.h"
+#include "share/compat.h"
#include "test_libs_common/file_utils_flac.h"
#include "test_libs_common/metadata_utils.h"
static const char *flacfilename(FLAC__bool is_ogg)
{
- return is_ogg? "metadata.ogg" : "metadata.flac";
+ return is_ogg? "metadata.oga" : "metadata.flac";
}
static FLAC__bool die_(const char *msg)
return false;
}
-static void init_metadata_blocks_()
+static void init_metadata_blocks_(void)
{
mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
}
-static void free_metadata_blocks_()
+static void free_metadata_blocks_(void)
{
mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
}
static FLAC__StreamEncoderReadStatus stream_encoder_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
{
- (void)encoder, (void)buffer, (void)bytes, (void)client_data;
- memset(buffer, 0, *bytes); /* init buffer to avoid valgrind errors */
- return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
+ FILE *f = (FILE*)client_data;
+ (void)encoder;
+ if(*bytes > 0) {
+ *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, f);
+ if(ferror(f))
+ return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
+ else if(*bytes == 0)
+ return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
+ else
+ return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
+ }
+ else
+ return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
}
static FLAC__StreamEncoderWriteStatus stream_encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
{
- (void)encoder, (void)buffer, (void)bytes, (void)samples, (void)current_frame, (void)client_data;
- return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
+ FILE *f = (FILE*)client_data;
+ (void)encoder, (void)samples, (void)current_frame;
+ if(fwrite(buffer, 1, bytes, f) != bytes)
+ return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
+ else
+ return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
}
static FLAC__StreamEncoderSeekStatus stream_encoder_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
{
- (void)encoder, (void)absolute_byte_offset, (void)client_data;
- return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
+ FILE *f = (FILE*)client_data;
+ (void)encoder;
+ if(fseeko(f, (long)absolute_byte_offset, SEEK_SET) < 0)
+ return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
+ else
+ return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
}
static FLAC__StreamEncoderTellStatus stream_encoder_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
{
- (void)encoder, (void)client_data;
- *absolute_byte_offset = 0;
- return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
+ FILE *f = (FILE*)client_data;
+ FLAC__off_t pos;
+ (void)encoder;
+ if((pos = ftello(f)) < 0)
+ return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
+ else {
+ *absolute_byte_offset = (FLAC__uint64)pos;
+ return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
+ }
}
static void stream_encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
return die_s_("returned false", encoder);
printf("OK\n");
+ if(layer < LAYER_FILENAME) {
+ printf("opening file for FLAC output... ");
+ file = fopen(flacfilename(is_ogg), "w+b");
+ if(0 == file) {
+ printf("ERROR (%s)\n", strerror(errno));
+ return false;
+ }
+ printf("OK\n");
+ }
+
switch(layer) {
case LAYER_STREAM:
printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":"");
init_status = is_ogg?
- FLAC__stream_encoder_init_ogg_stream(encoder, /*read_callback=*/0, stream_encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, stream_encoder_metadata_callback_, /*client_data=*/0) :
- FLAC__stream_encoder_init_stream(encoder, stream_encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, stream_encoder_metadata_callback_, /*client_data=*/0);
+ FLAC__stream_encoder_init_ogg_stream(encoder, /*read_callback=*/0, stream_encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, stream_encoder_metadata_callback_, /*client_data=*/file) :
+ FLAC__stream_encoder_init_stream(encoder, stream_encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, stream_encoder_metadata_callback_, /*client_data=*/file);
break;
case LAYER_SEEKABLE_STREAM:
printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":"");
init_status = is_ogg?
- FLAC__stream_encoder_init_ogg_stream(encoder, stream_encoder_read_callback_, stream_encoder_write_callback_, stream_encoder_seek_callback_, stream_encoder_tell_callback_, /*metadata_callback=*/0, /*client_data=*/0) :
- FLAC__stream_encoder_init_stream(encoder, stream_encoder_write_callback_, stream_encoder_seek_callback_, stream_encoder_tell_callback_, /*metadata_callback=*/0, /*client_data=*/0);
+ FLAC__stream_encoder_init_ogg_stream(encoder, stream_encoder_read_callback_, stream_encoder_write_callback_, stream_encoder_seek_callback_, stream_encoder_tell_callback_, /*metadata_callback=*/0, /*client_data=*/file) :
+ FLAC__stream_encoder_init_stream(encoder, stream_encoder_write_callback_, stream_encoder_seek_callback_, stream_encoder_tell_callback_, /*metadata_callback=*/0, /*client_data=*/file);
break;
case LAYER_FILE:
- printf("opening file for FLAC output... ");
- file = fopen(flacfilename(is_ogg), "w+b");
- if(0 == file) {
- printf("ERROR (%s)\n", strerror(errno));
- return false;
- }
- printf("OK\n");
-
printf("testing FLAC__stream_encoder_init_%sFILE()... ", is_ogg? "ogg_":"");
init_status = is_ogg?
FLAC__stream_encoder_init_ogg_FILE(encoder, file, stream_encoder_progress_callback_, /*client_data=*/0) :
printf("testing FLAC__stream_encoder_get_total_samples_estimate()... ");
if(FLAC__stream_encoder_get_total_samples_estimate(encoder) != streaminfo_.data.stream_info.total_samples) {
- printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, FLAC__stream_encoder_get_total_samples_estimate(encoder));
+ printf("FAILED, expected %" PRIu64 ", got %" PRIu64 "\n", streaminfo_.data.stream_info.total_samples, FLAC__stream_encoder_get_total_samples_estimate(encoder));
return false;
}
printf("OK\n");
return die_s_("returned false", encoder);
printf("OK\n");
+ if(layer < LAYER_FILE)
+ fclose(file);
+
printf("testing FLAC__stream_encoder_delete()... ");
FLAC__stream_encoder_delete(encoder);
printf("OK\n");
return true;
}
-FLAC__bool test_encoders()
+FLAC__bool test_encoders(void)
{
FLAC__bool is_ogg = false;