Commit ece2f61c authored by Josh Coalson's avatar Josh Coalson
Browse files

write encoded stream to actual file to improve testing

parent cf1422e2
...@@ -89,8 +89,9 @@ static void free_metadata_blocks_() ...@@ -89,8 +89,9 @@ static void free_metadata_blocks_()
class StreamEncoder : public FLAC::Encoder::Stream { class StreamEncoder : public FLAC::Encoder::Stream {
public: public:
Layer layer_; Layer layer_;
FILE *file_;
StreamEncoder(Layer layer): FLAC::Encoder::Stream(), layer_(layer) { } StreamEncoder(Layer layer): FLAC::Encoder::Stream(), layer_(layer), file_(0) { }
~StreamEncoder() { } ~StreamEncoder() { }
// from FLAC::Encoder::Stream // from FLAC::Encoder::Stream
...@@ -103,30 +104,50 @@ public: ...@@ -103,30 +104,50 @@ public:
::FLAC__StreamEncoderReadStatus StreamEncoder::read_callback(FLAC__byte buffer[], size_t *bytes) ::FLAC__StreamEncoderReadStatus StreamEncoder::read_callback(FLAC__byte buffer[], size_t *bytes)
{ {
(void)buffer, (void)bytes; if(*bytes > 0) {
*bytes = fread(buffer, sizeof(FLAC__byte), *bytes, file_);
return ::FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE; if(ferror(file_))
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;
} }
::FLAC__StreamEncoderWriteStatus StreamEncoder::write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame) ::FLAC__StreamEncoderWriteStatus StreamEncoder::write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame)
{ {
(void)buffer, (void)bytes, (void)samples, (void)current_frame; (void)samples, (void)current_frame;
return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK; if(fwrite(buffer, 1, bytes, file_) != bytes)
return ::FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
else
return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
} }
::FLAC__StreamEncoderSeekStatus StreamEncoder::seek_callback(FLAC__uint64 absolute_byte_offset) ::FLAC__StreamEncoderSeekStatus StreamEncoder::seek_callback(FLAC__uint64 absolute_byte_offset)
{ {
(void)absolute_byte_offset; if(layer_==LAYER_STREAM)
return ::FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED;
return layer_==LAYER_STREAM? ::FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED : ::FLAC__STREAM_ENCODER_SEEK_STATUS_OK; else if(fseek(file_, (long)absolute_byte_offset, SEEK_SET) < 0)
return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
else
return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
} }
::FLAC__StreamEncoderTellStatus StreamEncoder::tell_callback(FLAC__uint64 *absolute_byte_offset) ::FLAC__StreamEncoderTellStatus StreamEncoder::tell_callback(FLAC__uint64 *absolute_byte_offset)
{ {
*absolute_byte_offset = 0; long pos;
if(layer_==LAYER_STREAM)
return layer_==LAYER_STREAM? ::FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED : ::FLAC__STREAM_ENCODER_TELL_STATUS_OK; return ::FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED;
else if((pos = ftell(file_)) < 0)
return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
else {
*absolute_byte_offset = (FLAC__uint64)pos;
return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
}
} }
void StreamEncoder::metadata_callback(const ::FLAC__StreamMetadata *metadata) void StreamEncoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
...@@ -286,6 +307,18 @@ static bool test_stream_encoder(Layer layer, bool is_ogg) ...@@ -286,6 +307,18 @@ static bool test_stream_encoder(Layer layer, bool is_ogg)
return die_s_("returned false", encoder); return die_s_("returned false", encoder);
printf("OK\n"); 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");
if(layer < LAYER_FILE)
dynamic_cast<StreamEncoder*>(encoder)->file_ = file;
}
switch(layer) { switch(layer) {
case LAYER_STREAM: case LAYER_STREAM:
case LAYER_SEEKABLE_STREAM: case LAYER_SEEKABLE_STREAM:
...@@ -293,14 +326,6 @@ static bool test_stream_encoder(Layer layer, bool is_ogg) ...@@ -293,14 +326,6 @@ static bool test_stream_encoder(Layer layer, bool is_ogg)
init_status = is_ogg? encoder->init_ogg() : encoder->init(); init_status = is_ogg? encoder->init_ogg() : encoder->init();
break; break;
case LAYER_FILE: 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 init%s()... ", is_ogg? "_ogg":""); printf("testing init%s()... ", is_ogg? "_ogg":"");
init_status = is_ogg? init_status = is_ogg?
dynamic_cast<FLAC::Encoder::File*>(encoder)->init_ogg(file) : dynamic_cast<FLAC::Encoder::File*>(encoder)->init_ogg(file) :
...@@ -484,6 +509,9 @@ static bool test_stream_encoder(Layer layer, bool is_ogg) ...@@ -484,6 +509,9 @@ static bool test_stream_encoder(Layer layer, bool is_ogg)
} }
printf("OK\n"); printf("OK\n");
if(layer < LAYER_FILE)
::fclose(dynamic_cast<StreamEncoder*>(encoder)->file_);
printf("freeing encoder instance... "); printf("freeing encoder instance... ");
delete encoder; delete encoder;
printf("OK\n"); printf("OK\n");
......
...@@ -90,28 +90,52 @@ static void free_metadata_blocks_() ...@@ -90,28 +90,52 @@ static void free_metadata_blocks_()
static FLAC__StreamEncoderReadStatus stream_encoder_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data) 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; FILE *f = (FILE*)client_data;
memset(buffer, 0, *bytes); /* init buffer to avoid valgrind errors */ (void)encoder;
return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE; 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) 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; FILE *f = (FILE*)client_data;
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; (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) 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; FILE *f = (FILE*)client_data;
return FLAC__STREAM_ENCODER_SEEK_STATUS_OK; (void)encoder;
if(fseek(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) static FLAC__StreamEncoderTellStatus stream_encoder_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
{ {
(void)encoder, (void)client_data; FILE *f = (FILE*)client_data;
*absolute_byte_offset = 0; long pos;
return FLAC__STREAM_ENCODER_TELL_STATUS_OK; (void)encoder;
if((pos = ftell(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) static void stream_encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
...@@ -249,28 +273,30 @@ static FLAC__bool test_stream_encoder(Layer layer, FLAC__bool is_ogg) ...@@ -249,28 +273,30 @@ static FLAC__bool test_stream_encoder(Layer layer, FLAC__bool is_ogg)
return die_s_("returned false", encoder); return die_s_("returned false", encoder);
printf("OK\n"); 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) { switch(layer) {
case LAYER_STREAM: case LAYER_STREAM:
printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":""); printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":"");
init_status = is_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_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=*/0); FLAC__stream_encoder_init_stream(encoder, stream_encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, stream_encoder_metadata_callback_, /*client_data=*/file);
break; break;
case LAYER_SEEKABLE_STREAM: case LAYER_SEEKABLE_STREAM:
printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":""); printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":"");
init_status = is_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_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=*/0); 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; break;
case LAYER_FILE: 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_":""); printf("testing FLAC__stream_encoder_init_%sFILE()... ", is_ogg? "ogg_":"");
init_status = is_ogg? init_status = is_ogg?
FLAC__stream_encoder_init_ogg_FILE(encoder, file, stream_encoder_progress_callback_, /*client_data=*/0) : FLAC__stream_encoder_init_ogg_FILE(encoder, file, stream_encoder_progress_callback_, /*client_data=*/0) :
...@@ -451,6 +477,9 @@ static FLAC__bool test_stream_encoder(Layer layer, FLAC__bool is_ogg) ...@@ -451,6 +477,9 @@ static FLAC__bool test_stream_encoder(Layer layer, FLAC__bool is_ogg)
return die_s_("returned false", encoder); return die_s_("returned false", encoder);
printf("OK\n"); printf("OK\n");
if(layer < LAYER_FILE)
fclose(file);
printf("testing FLAC__stream_encoder_delete()... "); printf("testing FLAC__stream_encoder_delete()... ");
FLAC__stream_encoder_delete(encoder); FLAC__stream_encoder_delete(encoder);
printf("OK\n"); printf("OK\n");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment